14
#include <linux/zutil.h>
17
#include <linux/config.h>
18
#include <linux/kernel.h>
19
#include <linux/sched.h>
20
#include <linux/slab.h>
21
#include <linux/zlib.h>
32
#define STREAM_END_SPACE 12
34
static struct mutex deflate_sem = MUTEX_INIT(deflate_sem);
35
static struct mutex inflate_sem = MUTEX_INIT(inflate_sem);;
37
static z_stream inf_strm, def_strm;
39
#define alloc_workspaces() (0)
40
#define free_workspaces() do { } while(0)
42
static int jffs2_zlib_compress(unsigned char *data_in,
43
unsigned char *cpage_out,
44
uint32_t *sourcelen, uint32_t *dstlen,
48
if (*dstlen <= STREAM_END_SPACE) {
52
mutex_lock(&deflate_sem);
54
if (Z_OK != deflateInit(&def_strm, 3)) {
55
printk(KERN_WARNING "deflateInit failed\n");
56
mutex_unlock(&deflate_sem);
60
def_strm.next_in = data_in;
61
def_strm.total_in = 0;
63
def_strm.next_out = cpage_out;
64
def_strm.total_out = 0;
66
while (def_strm.total_out < *dstlen - STREAM_END_SPACE &&
67
def_strm.total_in < *sourcelen) {
68
def_strm.avail_out = *dstlen - (def_strm.total_out + STREAM_END_SPACE);
69
def_strm.avail_in = min((unsigned)(*sourcelen-def_strm.total_in),
71
D1(printk(KERN_DEBUG "calling deflate with avail_in %d, avail_out %d\n",
72
def_strm.avail_in, def_strm.avail_out));
73
ret = deflate(&def_strm, Z_PARTIAL_FLUSH);
74
D1(printk(KERN_DEBUG "deflate returned with avail_in %d, avail_out %d, total_in %ld, total_out %ld\n",
75
def_strm.avail_in, def_strm.avail_out, def_strm.total_in, def_strm.total_out));
77
D1(printk(KERN_DEBUG "deflate in loop returned %d\n", ret));
78
deflateEnd(&def_strm);
79
mutex_unlock(&deflate_sem);
83
def_strm.avail_out += STREAM_END_SPACE;
84
def_strm.avail_in = 0;
85
ret = deflate(&def_strm, Z_FINISH);
86
deflateEnd(&def_strm);
88
if (ret != Z_STREAM_END) {
89
D1(printk(KERN_DEBUG "final deflate returned %d\n", ret));
94
if (def_strm.total_out >= def_strm.total_in) {
95
D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld; failing\n",
96
def_strm.total_in, def_strm.total_out));
101
D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld\n",
102
def_strm.total_in, def_strm.total_out));
104
*dstlen = def_strm.total_out;
105
*sourcelen = def_strm.total_in;
108
mutex_unlock(&deflate_sem);
112
static int jffs2_zlib_decompress(unsigned char *data_in,
113
unsigned char *cpage_out,
114
uint32_t srclen, uint32_t destlen,
117
int wbits = MAX_WBITS;
119
mutex_lock(&inflate_sem);
121
inf_strm.next_in = data_in;
122
inf_strm.avail_in = srclen;
123
inf_strm.total_in = 0;
125
inf_strm.next_out = cpage_out;
126
inf_strm.avail_out = destlen;
127
inf_strm.total_out = 0;
132
if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
133
((data_in[0] & 0x0f) == Z_DEFLATED) &&
134
!(((data_in[0]<<8) + data_in[1]) % 31)) {
136
D2(printk(KERN_DEBUG "inflate skipping adler32\n"));
137
wbits = -((data_in[0] >> 4) + 8);
138
inf_strm.next_in += 2;
139
inf_strm.avail_in -= 2;
142
D1(printk(KERN_DEBUG "inflate not skipping adler32\n"));
146
if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) {
147
printk(KERN_WARNING "inflateInit failed\n");
148
mutex_unlock(&inflate_sem);
152
while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK) {
155
if (ret != Z_STREAM_END) {
156
printk(KERN_NOTICE "inflate returned %d\n", ret);
158
zlib_inflateEnd(&inf_strm);
159
mutex_unlock(&inflate_sem);
163
static struct jffs2_compressor jffs2_zlib_comp = {
164
.priority = JFFS2_ZLIB_PRIORITY,
166
.compr = JFFS2_COMPR_ZLIB,
167
.compress = &jffs2_zlib_compress,
168
.decompress = &jffs2_zlib_decompress,
169
#ifdef JFFS2_ZLIB_DISABLED
176
int __init jffs2_zlib_init(void) {
179
ret = alloc_workspaces();
184
ret = jffs2_register_compressor(&jffs2_zlib_comp);
192
void jffs2_zlib_exit(void) {
193
jffs2_unregister_compressor(&jffs2_zlib_comp);