2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001, 2002 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
8 * The original JFFS, from which the design for JFFS2 was derived,
9 * was designed and implemented by Axis Communications AB.
11 * The contents of this file are subject to the Red Hat eCos Public
12 * License Version 1.1 (the "Licence"); you may not use this file
13 * except in compliance with the Licence. You may obtain a copy of
14 * the Licence at http://www.redhat.com/
16 * Software distributed under the Licence is distributed on an "AS IS"
17 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
18 * See the Licence for the specific language governing rights and
19 * limitations under the Licence.
21 * The Original Code is JFFS2 - Journalling Flash File System, version 2
23 * Alternatively, the contents of this file may be used under the
24 * terms of the GNU General Public License version 2 (the "GPL"), in
25 * which case the provisions of the GPL are applicable instead of the
26 * above. If you wish to allow the use of your version of this file
27 * only under the terms of the GPL and not to allow others to use your
28 * version of this file under the RHEPL, indicate your decision by
29 * deleting the provisions above and replace them with the notice and
30 * other provisions required by the GPL. If you do not delete the
31 * provisions above, a recipient may use your version of this file
32 * under either the RHEPL or the GPL.
34 * $Id: compr_zlib.c,v 1.8.2.1 2002/10/11 09:04:44 dwmw2 Exp $
39 #error "The userspace support got too messy and was removed. Update your mkfs.jffs2"
42 #include <linux/config.h>
43 #include <linux/kernel.h>
44 #include <linux/mtd/compatmac.h> /* for min() */
45 #include <linux/slab.h>
46 #include <linux/jffs2.h>
47 #include <linux/zlib.h>
50 /* Plan: call deflate() with avail_in == *sourcelen,
51 avail_out = *dstlen - 12 and flush == Z_FINISH.
52 If it doesn't manage to finish, call it again with
53 avail_in == 0 and avail_out set to the remaining 12
54 bytes for it to clean up.
55 Q: Is 12 bytes sufficient?
57 #define STREAM_END_SPACE 12
59 static DECLARE_MUTEX(deflate_sem);
60 static DECLARE_MUTEX(inflate_sem);
61 static void *deflate_workspace;
62 static void *inflate_workspace;
64 int __init jffs2_zlib_init(void)
66 deflate_workspace = vmalloc(zlib_deflate_workspacesize());
67 if (!deflate_workspace) {
68 printk(KERN_WARNING "Failed to allocate %d bytes for deflate workspace\n", zlib_deflate_workspacesize());
71 D1(printk(KERN_DEBUG "Allocated %d bytes for deflate workspace\n", zlib_deflate_workspacesize()));
72 inflate_workspace = vmalloc(zlib_inflate_workspacesize());
73 if (!inflate_workspace) {
74 printk(KERN_WARNING "Failed to allocate %d bytes for inflate workspace\n", zlib_inflate_workspacesize());
75 vfree(deflate_workspace);
78 D1(printk(KERN_DEBUG "Allocated %d bytes for inflate workspace\n", zlib_inflate_workspacesize()));
82 void jffs2_zlib_exit(void)
84 vfree(deflate_workspace);
85 vfree(inflate_workspace);
88 int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
89 __u32 *sourcelen, __u32 *dstlen)
94 if (*dstlen <= STREAM_END_SPACE)
98 strm.workspace = deflate_workspace;
100 if (Z_OK != zlib_deflateInit(&strm, 3)) {
101 printk(KERN_WARNING "deflateInit failed\n");
106 strm.next_in = data_in;
109 strm.next_out = cpage_out;
112 while (strm.total_out < *dstlen - STREAM_END_SPACE && strm.total_in < *sourcelen) {
113 strm.avail_out = *dstlen - (strm.total_out + STREAM_END_SPACE);
114 strm.avail_in = min((unsigned)(*sourcelen-strm.total_in), strm.avail_out);
115 D1(printk(KERN_DEBUG "calling deflate with avail_in %d, avail_out %d\n",
116 strm.avail_in, strm.avail_out));
117 ret = zlib_deflate(&strm, Z_PARTIAL_FLUSH);
118 D1(printk(KERN_DEBUG "deflate returned with avail_in %d, avail_out %d, total_in %ld, total_out %ld\n",
119 strm.avail_in, strm.avail_out, strm.total_in, strm.total_out));
121 D1(printk(KERN_DEBUG "deflate in loop returned %d\n", ret));
122 zlib_deflateEnd(&strm);
127 strm.avail_out += STREAM_END_SPACE;
129 ret = zlib_deflate(&strm, Z_FINISH);
130 zlib_deflateEnd(&strm);
132 if (ret != Z_STREAM_END) {
133 D1(printk(KERN_DEBUG "final deflate returned %d\n", ret));
137 D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld\n",
138 strm.total_in, strm.total_out));
140 if (strm.total_out >= strm.total_in)
143 *dstlen = strm.total_out;
144 *sourcelen = strm.total_in;
148 void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
149 __u32 srclen, __u32 destlen)
155 strm.workspace = inflate_workspace;
157 if (Z_OK != zlib_inflateInit(&strm)) {
158 printk(KERN_WARNING "inflateInit failed\n");
162 strm.next_in = data_in;
163 strm.avail_in = srclen;
166 strm.next_out = cpage_out;
167 strm.avail_out = destlen;
170 while((ret = zlib_inflate(&strm, Z_FINISH)) == Z_OK)
172 if (ret != Z_STREAM_END) {
173 printk(KERN_NOTICE "inflate returned %d\n", ret);
175 zlib_inflateEnd(&strm);