2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by Arjan van de Ven <arjanv@redhat.com>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: compr.c,v 1.27 2003/10/04 08:33:06 dwmw2 Exp $
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>
29 #include <linux/jffs2.h>
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);
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
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.
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.
58 unsigned char jffs2_compress(unsigned char *data_in, unsigned char *cpage_out,
59 uint32_t *datalen, uint32_t *cdatalen)
63 ret = jffs2_zlib_compress(data_in, cpage_out, datalen, cdatalen);
65 return JFFS2_COMPR_ZLIB;
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);
70 return JFFS2_COMPR_DYNRUBIN;
73 #if 0 /* Disabled 26/2/1. Obsoleted by dynrubin */
74 ret = jffs2_rubinmips_compress(data_in, cpage_out, datalen, cdatalen);
76 return JFFS2_COMPR_RUBINMIPS;
79 /* rtime does manage to recompress already-compressed data */
80 ret = jffs2_rtime_compress(data_in, cpage_out, datalen, cdatalen);
82 return JFFS2_COMPR_RTIME;
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)
92 return JFFS2_COMPR_NONE; /* We failed to compress */
97 int jffs2_decompress(unsigned char comprtype, unsigned char *cdata_in,
98 unsigned char *data_out, uint32_t cdatalen, uint32_t datalen)
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);
106 case JFFS2_COMPR_ZERO:
107 memset(data_out, 0, datalen);
110 case JFFS2_COMPR_ZLIB:
111 jffs2_zlib_decompress(cdata_in, data_out, cdatalen, datalen);
114 case JFFS2_COMPR_RTIME:
115 jffs2_rtime_decompress(cdata_in, data_out, cdatalen, datalen);
118 case JFFS2_COMPR_RUBINMIPS:
119 #if 0 /* Disabled 23/9/1 */
120 jffs2_rubinmips_decompress(cdata_in, data_out, cdatalen, datalen);
122 printk(KERN_WARNING "JFFS2: Rubinmips compression encountered but support not compiled in!\n");
125 case JFFS2_COMPR_DYNRUBIN:
126 #if 1 /* Phase this one out */
127 jffs2_dynrubin_decompress(cdata_in, data_out, cdatalen, datalen);
129 printk(KERN_WARNING "JFFS2: Dynrubin compression encountered but support not compiled in!\n");
134 printk(KERN_NOTICE "Unknown JFFS2 compression type 0x%02x\n", comprtype);