more changes on original files
[linux-2.4.git] / fs / jffs2 / compr.c
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001 Red Hat, Inc.
5  *
6  * Created by Arjan van de Ven <arjanv@redhat.com>
7  *
8  * The original JFFS, from which the design for JFFS2 was derived,
9  * was designed and implemented by Axis Communications AB.
10  *
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/
15  *
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.
20  *
21  * The Original Code is JFFS2 - Journalling Flash File System, version 2
22  *
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.
33  *
34  * $Id: compr.c,v 1.17 2001/09/23 09:56:46 dwmw2 Exp $
35  *
36  */
37
38 #include <linux/kernel.h>
39 #include <linux/string.h>
40 #include <linux/types.h>
41 #include <linux/errno.h>
42 #include <linux/jffs2.h>
43
44 int zlib_compress(unsigned char *data_in, unsigned char *cpage_out, __u32 *sourcelen, __u32 *dstlen);
45 void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out, __u32 srclen, __u32 destlen);
46 int rtime_compress(unsigned char *data_in, unsigned char *cpage_out, __u32 *sourcelen, __u32 *dstlen);
47 void rtime_decompress(unsigned char *data_in, unsigned char *cpage_out, __u32 srclen, __u32 destlen);
48 int rubinmips_compress(unsigned char *data_in, unsigned char *cpage_out, __u32 *sourcelen, __u32 *dstlen);
49 void rubinmips_decompress(unsigned char *data_in, unsigned char *cpage_out, __u32 srclen, __u32 destlen);
50 int dynrubin_compress(unsigned char *data_in, unsigned char *cpage_out, __u32 *sourcelen, __u32 *dstlen);
51 void dynrubin_decompress(unsigned char *data_in, unsigned char *cpage_out, __u32 srclen, __u32 destlen);
52
53
54 /* jffs2_compress:
55  * @data: Pointer to uncompressed data
56  * @cdata: Pointer to buffer for compressed data
57  * @datalen: On entry, holds the amount of data available for compression.
58  *      On exit, expected to hold the amount of data actually compressed.
59  * @cdatalen: On entry, holds the amount of space available for compressed
60  *      data. On exit, expected to hold the actual size of the compressed
61  *      data.
62  *
63  * Returns: Byte to be stored with data indicating compression type used.
64  * Zero is used to show that the data could not be compressed - the 
65  * compressed version was actually larger than the original.
66  *
67  * If the cdata buffer isn't large enough to hold all the uncompressed data,
68  * jffs2_compress should compress as much as will fit, and should set 
69  * *datalen accordingly to show the amount of data which were compressed.
70  */
71 unsigned char jffs2_compress(unsigned char *data_in, unsigned char *cpage_out, 
72                     __u32 *datalen, __u32 *cdatalen)
73 {
74         int ret;
75
76         ret = zlib_compress(data_in, cpage_out, datalen, cdatalen);
77         if (!ret) {
78                 return JFFS2_COMPR_ZLIB;
79         }
80 #if 0 /* Disabled 23/9/1. With zlib it hardly ever gets a look in */
81         ret = dynrubin_compress(data_in, cpage_out, datalen, cdatalen);
82         if (!ret) {
83                 return JFFS2_COMPR_DYNRUBIN;
84         }
85 #endif
86 #if 0 /* Disabled 26/2/1. Obsoleted by dynrubin */
87         ret = rubinmips_compress(data_in, cpage_out, datalen, cdatalen);
88         if (!ret) {
89                 return JFFS2_COMPR_RUBINMIPS;
90         }
91 #endif
92         /* rtime does manage to recompress already-compressed data */
93         ret = rtime_compress(data_in, cpage_out, datalen, cdatalen);
94         if (!ret) {
95                 return JFFS2_COMPR_RTIME;
96         }
97 #if 0
98         /* We don't need to copy. Let the caller special-case the COMPR_NONE case. */
99         /* If we get here, no compression is going to work */
100         /* But we might want to use the fragmentation part -- Arjan */
101         memcpy(cpage_out,data_in,min(*datalen,*cdatalen));
102         if (*datalen > *cdatalen)
103                 *datalen = *cdatalen;
104 #endif          
105         return JFFS2_COMPR_NONE; /* We failed to compress */
106
107 }
108
109
110 int jffs2_decompress(unsigned char comprtype, unsigned char *cdata_in, 
111                      unsigned char *data_out, __u32 cdatalen, __u32 datalen)
112 {
113         switch (comprtype) {
114         case JFFS2_COMPR_NONE:
115                 /* This should be special-cased elsewhere, but we might as well deal with it */
116                 memcpy(data_out, cdata_in, datalen);
117                 break;
118
119         case JFFS2_COMPR_ZERO:
120                 memset(data_out, 0, datalen);
121                 break;
122
123         case JFFS2_COMPR_ZLIB:
124                 zlib_decompress(cdata_in, data_out, cdatalen, datalen);
125                 break;
126
127         case JFFS2_COMPR_RTIME:
128                 rtime_decompress(cdata_in, data_out, cdatalen, datalen);
129                 break;
130
131         case JFFS2_COMPR_RUBINMIPS:
132 #if 0 /* Disabled 23/9/1 */
133                 rubinmips_decompress(cdata_in, data_out, cdatalen, datalen);
134 #else
135                 printk(KERN_WARNING "JFFS2: Rubinmips compression encountered but support not compiled in!\n");
136 #endif
137                 break;
138         case JFFS2_COMPR_DYNRUBIN:
139 #if 1 /* Phase this one out */
140                 dynrubin_decompress(cdata_in, data_out, cdatalen, datalen);
141 #else
142                 printk(KERN_WARNING "JFFS2: Dynrubin compression encountered but support not compiled in!\n");
143 #endif
144                 break;
145
146         default:
147                 printk(KERN_NOTICE "Unknown JFFS2 compression type 0x%02x\n", comprtype);
148                 return -EIO;
149         }
150         return 0;
151 }