📄 gzipfs.fist
字号:
%{#define MY_ZCALLOC /* Define our own alloc/free functions */#define GZIPFS_DEFLATE_LEVEL 9#include "zlib.h"#include "fist.h"%}debug on;filter sca;filter data;mod_src inflate.c zutil.c infblock.c deflate.c trees.c infutil.c inftrees.c infcodes.c adler32.c inffast.c;mod_hdr zutil.h infblock.h deflate.h zlib.h zconf.h trees.h infutil.h inftrees.h infcodes.h inffast.h inffixed.h;add_mk gzipfs.mk;%%%%void *zcalloc (void *opaque, unsigned items, unsigned size){ void *out; int cnt = size * items; print_entry_location(); out = (void *)fistMalloc(cnt); print_exit_pointer(out); return(out);}void zcfree (void *opaque, void *ptr){ print_entry_location(); kfree(ptr); print_exit_location();}intgzipfs_encode_buffers(char *hidden_pages_data, /* A PAGE_SIZE buffer (already allocated) passed to us to fill in */ char *page_data, /* The data we are to encode */ int *need_to_call, /* Call us again? */ unsigned to, /* how far into page_data to encode */ inode_t *inode, /* The inode in question */ vfs_t *vfs, /* vfs_t ??? unused */ void **opaque) /* Opaque data */ /* encodes the data in page_data into hidden_pages_data. Returns -errno for error, and the size of hidden_pages_data for success */{ z_stream *zptr; int rc, err; print_entry_location(); if (*opaque != NULL) { zptr = (z_stream *) *opaque; zptr->next_out = hidden_pages_data; zptr->avail_out = PAGE_CACHE_SIZE; zptr->total_out = 0; } else { zptr = kmalloc(sizeof(z_stream), GFP_KERNEL); if (zptr == NULL) { err = -ENOMEM; goto out; } zptr->zalloc = (alloc_func)0; /* Custom memory allocator called with opaque */ zptr->zfree = (free_func)0; /* Custom memory free-er called with opaque as an argument */ zptr->opaque = (voidpf)0; /* Opaque argument to zalloc/zfree */ zptr->next_in = page_data; /* + *(from);*/ zptr->avail_in = to; /* to - *(from) */ zptr->next_out = hidden_pages_data; zptr->avail_out = PAGE_CACHE_SIZE; /* * First arg is a stream object * Second arg is compression level (0-9) */ rc = deflateInit(zptr, GZIPFS_DEFLATE_LEVEL); if (rc != Z_OK ) { printk("inflateInit error %d: Abort\n",rc); /* This is bad. Lack of memory is the usual cause */ err = -ENOMEM; goto out; } while ((zptr->avail_out > 0) && (zptr->avail_in > 0)) { /* While we're not finished */ rc = deflate(zptr, Z_FULL_FLUSH); /* Do a deflate */ if ((rc != Z_OK) && (rc != Z_STREAM_END)) { printk("Compression error! rc=%d\n",rc); err = -EIO; goto out; } } } rc = deflate(zptr, Z_FINISH); err = zptr->total_out; /* update encoded bytes */ if (rc == Z_STREAM_END) { deflateEnd(zptr); err = zptr->total_out; /* update encoded bytes */ kfree(zptr); *need_to_call = 0; } else if (rc == Z_BUF_ERROR || rc == Z_OK) { *opaque = zptr; } else printk("encode_buffers error: rc=%d\n", rc); out: print_exit_status(err); return(err);}intgzipfs_decode_buffers(int num_hidden_pages, /* The number of pages in hidden_pages_data */ char **hidden_pages_data, /* An array of pages containing encoded data */ char *page_data, /* A pre-allocated PAGE_SIZE buffer to write the decoded data to */ vnode_t *vnode, /* The vnode of the file in question, unused */ vfs_t *vfs, /* vfs_t, unused */ void *opaque) /* opaque data filled in by wrapfs_Sca_count_pages, containing an int, the starting offset within the first page of hidden_pages_data of the encoded page we want */ /* Returns -errno for error, or the number of bytes decoded on success (Should usually return PAGE_SIZE bytes) */{ z_stream stream; /* Decompression stream obj */ int i, offset, rc; int err = 0; print_entry_location(); stream.zalloc = (alloc_func)0; /* Custom memory allocator called with stream.opaque */ stream.zfree = (free_func)0; /* Custom memory free-er called with stream.opaque as an argument */ stream.opaque = (voidpf)0; /* Opaque argument to zalloc/zfree */ offset = (int)opaque; /* This is filled in with the starting offset in wrapfs_count_hidden_pages */ stream.next_in = hidden_pages_data[0] + offset; /* start offset bytes into page 0. */ stream.avail_in = PAGE_CACHE_SIZE - offset; stream.next_out = page_data; /* Set the output buffer to what we were passed */ stream.avail_out = PAGE_CACHE_SIZE; fist_dprint(6, "next_in = 0x%x\n", stream.next_in); fist_dprint(6, "avail_in = 0x%x\n", stream.avail_in); fist_dprint(6, "next_out = 0x%x\n", stream.next_out); fist_dprint(6, "avail_out = 0x%x\n", stream.avail_out); rc = inflateInit(&stream); /* Initialize the decompression stream */ if (rc != Z_OK) { printk("inflateInit error %d: Abort\n",rc); /* This is bad. Lack of memory is the usual cause */ err = -ENOMEM; goto out; } fist_dprint(8, "Page 0: %02x%02x%02x%02x\n", (u8)hidden_pages_data[0][0], (u8)hidden_pages_data[0][1], (u8)hidden_pages_data[0][2], (u8)hidden_pages_data[0][3]); rc = inflate(&stream, Z_NO_FLUSH); /* Inflate some data */ /* If there was more than one page in hidden_pages_data, we go here */ for (i = 1; i < num_hidden_pages; i++) { /* Step over each encoded page */ fist_dprint(6, "Crossed a page boundary (%d)\n", rc); fist_dprint(8, "Page %d: %02x%02x%02x%02x\n", i, (u8)hidden_pages_data[i][0], (u8)hidden_pages_data[i][1], (u8)hidden_pages_data[i][2], (u8)hidden_pages_data[i][3]); if (rc == Z_STREAM_END) { /* We've finished early? Bug? */ printk("gzipfs: Premature end of compressed data!\n"); err = stream.total_out; goto out; } else if (rc == Z_OK) { /* Normal case, successful inflation, need more input data */ stream.next_in = hidden_pages_data[i]; stream.avail_in = PAGE_CACHE_SIZE; /* This is a lie for the last page, but zlib is smart enough to figure it out. */ fist_dprint(6, "***next_in = 0x%x\n", stream.next_in); fist_dprint(6, "***avail_in = 0x%x\n", stream.avail_in); fist_dprint(6, "***next_out = 0x%x\n", stream.next_out); fist_dprint(6, "***avail_out = 0x%x\n", stream.avail_out); rc = inflate(&stream, Z_FULL_FLUSH); /* Inflate some data */ /* If, after this inflate, there is no space left, and we are not finished, bomb out */ if (rc == Z_BUF_ERROR) { printk("Data contains more than %ld bytes\n",stream.total_out); printk("%ld written out, %d left to decode %ld decoded(%d)\n", stream.total_out, stream.avail_in,stream.total_in,rc); err = -EIO; goto out; } } else { /* Error condition! */ printk("zlib error %d\n", rc); err = -EIO; goto out; } } err = stream.total_out; /* Return the number of bytes we decoded */ if (rc != Z_STREAM_END && stream.avail_in > 0) /* We finished out input data without getting an end-of-stream */ printk("Finished loop without reaching end of chunk(%d)\n",rc); out: inflateEnd(&stream); /* Close the stream object */ print_exit_status(err); return(err);}/* * Local variables: * c-basic-offset: 4 * End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -