ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / jffs2 / compr_zlib.c
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001-2003 Red Hat, Inc.
5  *
6  * Created by David Woodhouse <dwmw2@redhat.com>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  * $Id: compr_zlib.c,v 1.24 2003/10/04 08:33:06 dwmw2 Exp $
11  *
12  */
13
14 #if !defined(__KERNEL__) && !defined(__ECOS)
15 #error "The userspace support got too messy and was removed. Update your mkfs.jffs2"
16 #endif
17
18 #include <linux/config.h>
19 #include <linux/kernel.h>
20 #include <linux/vmalloc.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/zlib.h>
24 #include <linux/zutil.h>
25 #include <asm/semaphore.h>
26 #include "nodelist.h"
27
28         /* Plan: call deflate() with avail_in == *sourcelen, 
29                 avail_out = *dstlen - 12 and flush == Z_FINISH. 
30                 If it doesn't manage to finish, call it again with
31                 avail_in == 0 and avail_out set to the remaining 12
32                 bytes for it to clean up. 
33            Q: Is 12 bytes sufficient?
34         */
35 #define STREAM_END_SPACE 12
36
37 static DECLARE_MUTEX(deflate_sem);
38 static DECLARE_MUTEX(inflate_sem);
39 static z_stream inf_strm, def_strm;
40
41 #ifdef __KERNEL__ /* Linux-only */
42 int __init jffs2_zlib_init(void)
43 {
44         def_strm.workspace = vmalloc(zlib_deflate_workspacesize());
45         if (!def_strm.workspace) {
46                 printk(KERN_WARNING "Failed to allocate %d bytes for deflate workspace\n", zlib_deflate_workspacesize());
47                 return -ENOMEM;
48         }
49         D1(printk(KERN_DEBUG "Allocated %d bytes for deflate workspace\n", zlib_deflate_workspacesize()));
50         inf_strm.workspace = vmalloc(zlib_inflate_workspacesize());
51         if (!inf_strm.workspace) {
52                 printk(KERN_WARNING "Failed to allocate %d bytes for inflate workspace\n", zlib_inflate_workspacesize());
53                 vfree(def_strm.workspace);
54                 return -ENOMEM;
55         }
56         D1(printk(KERN_DEBUG "Allocated %d bytes for inflate workspace\n", zlib_inflate_workspacesize()));
57         return 0;
58 }
59
60 void jffs2_zlib_exit(void)
61 {
62         vfree(def_strm.workspace);
63         vfree(inf_strm.workspace);
64 }
65 #endif /* __KERNEL__ */
66
67 int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out, 
68                    uint32_t *sourcelen, uint32_t *dstlen)
69 {
70         int ret;
71
72         if (*dstlen <= STREAM_END_SPACE)
73                 return -1;
74
75         down(&deflate_sem);
76
77         if (Z_OK != zlib_deflateInit(&def_strm, 3)) {
78                 printk(KERN_WARNING "deflateInit failed\n");
79                 up(&deflate_sem);
80                 return -1;
81         }
82
83         def_strm.next_in = data_in;
84         def_strm.total_in = 0;
85         
86         def_strm.next_out = cpage_out;
87         def_strm.total_out = 0;
88
89         while (def_strm.total_out < *dstlen - STREAM_END_SPACE && def_strm.total_in < *sourcelen) {
90                 def_strm.avail_out = *dstlen - (def_strm.total_out + STREAM_END_SPACE);
91                 def_strm.avail_in = min((unsigned)(*sourcelen-def_strm.total_in), def_strm.avail_out);
92                 D1(printk(KERN_DEBUG "calling deflate with avail_in %d, avail_out %d\n",
93                           def_strm.avail_in, def_strm.avail_out));
94                 ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH);
95                 D1(printk(KERN_DEBUG "deflate returned with avail_in %d, avail_out %d, total_in %ld, total_out %ld\n", 
96                           def_strm.avail_in, def_strm.avail_out, def_strm.total_in, def_strm.total_out));
97                 if (ret != Z_OK) {
98                         D1(printk(KERN_DEBUG "deflate in loop returned %d\n", ret));
99                         zlib_deflateEnd(&def_strm);
100                         up(&deflate_sem);
101                         return -1;
102                 }
103         }
104         def_strm.avail_out += STREAM_END_SPACE;
105         def_strm.avail_in = 0;
106         ret = zlib_deflate(&def_strm, Z_FINISH);
107         zlib_deflateEnd(&def_strm);
108
109         if (ret != Z_STREAM_END) {
110                 D1(printk(KERN_DEBUG "final deflate returned %d\n", ret));
111                 ret = -1;
112                 goto out;
113         }
114
115         if (def_strm.total_out >= def_strm.total_in) {
116                 D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld; failing\n",
117                           def_strm.total_in, def_strm.total_out));
118                 ret = -1;
119                 goto out;
120         }
121
122         D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld\n",
123                   def_strm.total_in, def_strm.total_out));
124
125         *dstlen = def_strm.total_out;
126         *sourcelen = def_strm.total_in;
127         ret = 0;
128  out:
129         up(&deflate_sem);
130         return ret;
131 }
132
133 void jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
134                       uint32_t srclen, uint32_t destlen)
135 {
136         int ret;
137         int wbits = MAX_WBITS;
138
139         down(&inflate_sem);
140
141         inf_strm.next_in = data_in;
142         inf_strm.avail_in = srclen;
143         inf_strm.total_in = 0;
144         
145         inf_strm.next_out = cpage_out;
146         inf_strm.avail_out = destlen;
147         inf_strm.total_out = 0;
148
149         /* If it's deflate, and it's got no preset dictionary, then
150            we can tell zlib to skip the adler32 check. */
151         if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
152             ((data_in[0] & 0x0f) == Z_DEFLATED) &&
153             !(((data_in[0]<<8) + data_in[1]) % 31)) {
154
155                 D2(printk(KERN_DEBUG "inflate skipping adler32\n"));
156                 wbits = -((data_in[0] >> 4) + 8);
157                 inf_strm.next_in += 2;
158                 inf_strm.avail_in -= 2;
159         } else {
160                 /* Let this remain D1 for now -- it should never happen */
161                 D1(printk(KERN_DEBUG "inflate not skipping adler32\n"));
162         }
163
164
165         if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) {
166                 printk(KERN_WARNING "inflateInit failed\n");
167                 up(&inflate_sem);
168                 return;
169         }
170
171         while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK)
172                 ;
173         if (ret != Z_STREAM_END) {
174                 printk(KERN_NOTICE "inflate returned %d\n", ret);
175         }
176         zlib_inflateEnd(&inf_strm);
177         up(&inflate_sem);
178 }