123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- #define PROGRAM_NAME "compr_zlib"
- #include <stdint.h>
- #define crc32 __zlib_crc32
- #include <zlib.h>
- #undef crc32
- #include <stdio.h>
- #include <asm/types.h>
- #include <linux/jffs2.h>
- #include "common.h"
- #include "compr.h"
- #define STREAM_END_SPACE 12
- static int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
- uint32_t *sourcelen, uint32_t *dstlen)
- {
- z_stream strm;
- int ret;
- if (*dstlen <= STREAM_END_SPACE)
- return -1;
- strm.zalloc = (void *)0;
- strm.zfree = (void *)0;
- if (Z_OK != deflateInit(&strm, 3)) {
- return -1;
- }
- strm.next_in = data_in;
- strm.total_in = 0;
- strm.next_out = cpage_out;
- strm.total_out = 0;
- while (strm.total_out < *dstlen - STREAM_END_SPACE && strm.total_in < *sourcelen) {
- strm.avail_out = *dstlen - (strm.total_out + STREAM_END_SPACE);
- strm.avail_in = min((unsigned)(*sourcelen-strm.total_in), strm.avail_out);
- ret = deflate(&strm, Z_PARTIAL_FLUSH);
- if (ret != Z_OK) {
- deflateEnd(&strm);
- return -1;
- }
- }
- strm.avail_out += STREAM_END_SPACE;
- strm.avail_in = 0;
- ret = deflate(&strm, Z_FINISH);
- if (ret != Z_STREAM_END) {
- deflateEnd(&strm);
- return -1;
- }
- deflateEnd(&strm);
- if (strm.total_out >= strm.total_in)
- return -1;
- *dstlen = strm.total_out;
- *sourcelen = strm.total_in;
- return 0;
- }
- static int jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
- uint32_t srclen, uint32_t destlen)
- {
- z_stream strm;
- int ret;
- strm.zalloc = (void *)0;
- strm.zfree = (void *)0;
- if (Z_OK != inflateInit(&strm)) {
- return 1;
- }
- strm.next_in = data_in;
- strm.avail_in = srclen;
- strm.total_in = 0;
- strm.next_out = cpage_out;
- strm.avail_out = destlen;
- strm.total_out = 0;
- while((ret = inflate(&strm, Z_FINISH)) == Z_OK)
- ;
- inflateEnd(&strm);
- return 0;
- }
- static struct jffs2_compressor jffs2_zlib_comp = {
- .priority = JFFS2_ZLIB_PRIORITY,
- .name = "zlib",
- .disabled = 0,
- .compr = JFFS2_COMPR_ZLIB,
- .compress = &jffs2_zlib_compress,
- .decompress = &jffs2_zlib_decompress,
- };
- int jffs2_zlib_init(void)
- {
- return jffs2_register_compressor(&jffs2_zlib_comp);
- }
- void jffs2_zlib_exit(void)
- {
- jffs2_unregister_compressor(&jffs2_zlib_comp);
- }
|