ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / jffs2 / compr.c
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001-2003 Red Hat, Inc.
5  *
6  * Created by Arjan van de Ven <arjanv@redhat.com>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  * $Id: compr.c,v 1.27 2003/10/04 08:33:06 dwmw2 Exp $
11  *
12  */
13
14 #if defined(__KERNEL__) || defined (__ECOS)
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
18 #include <linux/types.h>
19 #else 
20 #define KERN_DEBUG
21 #define KERN_NOTICE
22 #define KERN_WARNING
23 #define printk printf
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <errno.h>
27 #endif
28
29 #include <linux/jffs2.h>
30
31 int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out, uint32_t *sourcelen, uint32_t *dstlen);
32 void jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out, uint32_t srclen, uint32_t destlen);
33 int jffs2_rtime_compress(unsigned char *data_in, unsigned char *cpage_out, uint32_t *sourcelen, uint32_t *dstlen);
34 void jffs2_rtime_decompress(unsigned char *data_in, unsigned char *cpage_out, uint32_t srclen, uint32_t destlen);
35 int jffs2_rubinmips_compress(unsigned char *data_in, unsigned char *cpage_out, uint32_t *sourcelen, uint32_t *dstlen);
36 void jffs2_rubinmips_decompress(unsigned char *data_in, unsigned char *cpage_out, uint32_t srclen, uint32_t destlen);
37 int jffs2_dynrubin_compress(unsigned char *data_in, unsigned char *cpage_out, uint32_t *sourcelen, uint32_t *dstlen);
38 void jffs2_dynrubin_decompress(unsigned char *data_in, unsigned char *cpage_out, uint32_t srclen, uint32_t destlen);
39
40
41 /* jffs2_compress:
42  * @data: Pointer to uncompressed data
43  * @cdata: Pointer to buffer for compressed data
44  * @datalen: On entry, holds the amount of data available for compression.
45  *      On exit, expected to hold the amount of data actually compressed.
46  * @cdatalen: On entry, holds the amount of space available for compressed
47  *      data. On exit, expected to hold the actual size of the compressed
48  *      data.
49  *
50  * Returns: Byte to be stored with data indicating compression type used.
51  * Zero is used to show that the data could not be compressed - the 
52  * compressed version was actually larger than the original.
53  *
54  * If the cdata buffer isn't large enough to hold all the uncompressed data,
55  * jffs2_compress should compress as much as will fit, and should set 
56  * *datalen accordingly to show the amount of data which were compressed.
57  */
58 unsigned char jffs2_compress(unsigned char *data_in, unsigned char *cpage_out, 
59                     uint32_t *datalen, uint32_t *cdatalen)
60 {
61         int ret;
62
63         ret = jffs2_zlib_compress(data_in, cpage_out, datalen, cdatalen);
64         if (!ret) {
65                 return JFFS2_COMPR_ZLIB;
66         }
67 #if 0 /* Disabled 23/9/1. With zlib it hardly ever gets a look in */
68         ret = jffs2_dynrubin_compress(data_in, cpage_out, datalen, cdatalen);
69         if (!ret) {
70                 return JFFS2_COMPR_DYNRUBIN;
71         }
72 #endif
73 #if 0 /* Disabled 26/2/1. Obsoleted by dynrubin */
74         ret = jffs2_rubinmips_compress(data_in, cpage_out, datalen, cdatalen);
75         if (!ret) {
76                 return JFFS2_COMPR_RUBINMIPS;
77         }
78 #endif
79         /* rtime does manage to recompress already-compressed data */
80         ret = jffs2_rtime_compress(data_in, cpage_out, datalen, cdatalen);
81         if (!ret) {
82                 return JFFS2_COMPR_RTIME;
83         }
84 #if 0
85         /* We don't need to copy. Let the caller special-case the COMPR_NONE case. */
86         /* If we get here, no compression is going to work */
87         /* But we might want to use the fragmentation part -- Arjan */
88         memcpy(cpage_out,data_in,min(*datalen,*cdatalen));
89         if (*datalen > *cdatalen)
90                 *datalen = *cdatalen;
91 #endif          
92         return JFFS2_COMPR_NONE; /* We failed to compress */
93
94 }
95
96
97 int jffs2_decompress(unsigned char comprtype, unsigned char *cdata_in, 
98                      unsigned char *data_out, uint32_t cdatalen, uint32_t datalen)
99 {
100         switch (comprtype) {
101         case JFFS2_COMPR_NONE:
102                 /* This should be special-cased elsewhere, but we might as well deal with it */
103                 memcpy(data_out, cdata_in, datalen);
104                 break;
105
106         case JFFS2_COMPR_ZERO:
107                 memset(data_out, 0, datalen);
108                 break;
109
110         case JFFS2_COMPR_ZLIB:
111                 jffs2_zlib_decompress(cdata_in, data_out, cdatalen, datalen);
112                 break;
113
114         case JFFS2_COMPR_RTIME:
115                 jffs2_rtime_decompress(cdata_in, data_out, cdatalen, datalen);
116                 break;
117
118         case JFFS2_COMPR_RUBINMIPS:
119 #if 0 /* Disabled 23/9/1 */
120                 jffs2_rubinmips_decompress(cdata_in, data_out, cdatalen, datalen);
121 #else
122                 printk(KERN_WARNING "JFFS2: Rubinmips compression encountered but support not compiled in!\n");
123 #endif
124                 break;
125         case JFFS2_COMPR_DYNRUBIN:
126 #if 1 /* Phase this one out */
127                 jffs2_dynrubin_decompress(cdata_in, data_out, cdatalen, datalen);
128 #else
129                 printk(KERN_WARNING "JFFS2: Dynrubin compression encountered but support not compiled in!\n");
130 #endif
131                 break;
132
133         default:
134                 printk(KERN_NOTICE "Unknown JFFS2 compression type 0x%02x\n", comprtype);
135                 return -EIO;
136         }
137         return 0;
138 }