2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2001-2007 Red Hat, Inc.
9 * For licensing information, see the file 'LICENCE' in this directory.
13 #include <linux/string.h>
14 #include <linux/types.h>
15 #include <linux/jffs2.h>
16 #include <linux/errno.h>
20 #define RUBIN_REG_SIZE 16
21 #define UPPER_BIT_RUBIN (((long) 1)<<(RUBIN_REG_SIZE-1))
22 #define LOWER_BITS_RUBIN ((((long) 1)<<(RUBIN_REG_SIZE-1))-1)
25 #define BIT_DIVIDER_MIPS 1043
26 static int bits_mips[8] = { 277, 249, 290, 267, 229, 341, 212, 241};
45 static inline void init_pushpull(struct pushpull *pp, char *buf,
46 unsigned buflen, unsigned ofs,
52 pp->reserve = reserve;
55 static inline int pushbit(struct pushpull *pp, int bit, int use_reserved)
57 if (pp->ofs >= pp->buflen - (use_reserved?0:pp->reserve))
61 pp->buf[pp->ofs >> 3] |= (1<<(7-(pp->ofs & 7)));
63 pp->buf[pp->ofs >> 3] &= ~(1<<(7-(pp->ofs & 7)));
70 static inline int pushedbits(struct pushpull *pp)
75 static inline int pullbit(struct pushpull *pp)
79 bit = (pp->buf[pp->ofs >> 3] >> (7-(pp->ofs & 7))) & 1;
85 static inline int pulledbits(struct pushpull *pp)
91 static void init_rubin(struct rubin_state *rs, int div, int *bits)
96 rs->p = (long) (2 * UPPER_BIT_RUBIN);
97 rs->bit_number = (long) 0;
98 rs->bit_divider = div;
101 rs->bits[c] = bits[c];
105 static int encode(struct rubin_state *rs, long A, long B, int symbol)
111 while ((rs->q >= UPPER_BIT_RUBIN) ||
112 ((rs->p + rs->q) <= UPPER_BIT_RUBIN)) {
115 ret = pushbit(&rs->pp, (rs->q & UPPER_BIT_RUBIN) ? 1 : 0, 0);
118 rs->q &= LOWER_BITS_RUBIN;
122 i0 = A * rs->p / (A + B);
141 static void end_rubin(struct rubin_state *rs)
146 for (i = 0; i < RUBIN_REG_SIZE; i++) {
147 pushbit(&rs->pp, (UPPER_BIT_RUBIN & rs->q) ? 1 : 0, 1);
148 rs->q &= LOWER_BITS_RUBIN;
154 static void init_decode(struct rubin_state *rs, int div, int *bits)
156 init_rubin(rs, div, bits);
161 for (rs->bit_number = 0; rs->bit_number++ < RUBIN_REG_SIZE;
162 rs->rec_q = rs->rec_q * 2 + (long) (pullbit(&rs->pp)))
166 static void __do_decode(struct rubin_state *rs, unsigned long p,
169 register unsigned long lower_bits_rubin = LOWER_BITS_RUBIN;
174 * First, work out how many bits we need from the input stream.
175 * Note that we have already done the initial check on this
176 * loop prior to calling this function.
180 q &= lower_bits_rubin;
183 } while ((q >= UPPER_BIT_RUBIN) || ((p + q) <= UPPER_BIT_RUBIN));
188 rs->bit_number += bits;
191 * Now get the bits. We really want this to be "get n bits".
195 c = pullbit(&rs->pp);
196 rec_q &= lower_bits_rubin;
203 static int decode(struct rubin_state *rs, long A, long B)
205 unsigned long p = rs->p, q = rs->q;
209 if (q >= UPPER_BIT_RUBIN || ((p + q) <= UPPER_BIT_RUBIN))
210 __do_decode(rs, p, q);
212 i0 = A * rs->p / (A + B);
219 threshold = rs->q + i0;
220 symbol = rs->rec_q >= threshold;
221 if (rs->rec_q >= threshold) {
233 static int out_byte(struct rubin_state *rs, unsigned char byte)
236 struct rubin_state rs_copy;
239 for (i=0; i<8; i++) {
240 ret = encode(rs, rs->bit_divider-rs->bits[i],
241 rs->bits[i], byte & 1);
243 /* Failed. Restore old state */
252 static int in_byte(struct rubin_state *rs)
254 int i, result = 0, bit_divider = rs->bit_divider;
256 for (i = 0; i < 8; i++)
257 result |= decode(rs, bit_divider - rs->bits[i],
265 static int rubin_do_compress(int bit_divider, int *bits, unsigned char *data_in,
266 unsigned char *cpage_out, uint32_t *sourcelen,
271 struct rubin_state rs;
273 init_pushpull(&rs.pp, cpage_out, *dstlen * 8, 0, 32);
275 init_rubin(&rs, bit_divider, bits);
277 while (pos < (*sourcelen) && !out_byte(&rs, data_in[pos]))
287 /* Tell the caller how much we managed to compress,
288 * and how much space it took */
290 outpos = (pushedbits(&rs.pp)+7)/8;
293 return -1; /* We didn't actually compress */
299 /* _compress returns the compressed size, -1 if bigger */
300 int jffs2_rubinmips_compress(unsigned char *data_in, unsigned char *cpage_out,
301 uint32_t *sourcelen, uint32_t *dstlen, void *model)
303 return rubin_do_compress(BIT_DIVIDER_MIPS, bits_mips, data_in,
304 cpage_out, sourcelen, dstlen);
307 static int jffs2_dynrubin_compress(unsigned char *data_in,
308 unsigned char *cpage_out,
309 uint32_t *sourcelen, uint32_t *dstlen,
313 unsigned char histo[256];
316 uint32_t mysrclen, mydstlen;
318 mysrclen = *sourcelen;
319 mydstlen = *dstlen - 8;
324 memset(histo, 0, 256);
325 for (i=0; i<mysrclen; i++)
327 memset(bits, 0, sizeof(int)*8);
328 for (i=0; i<256; i++) {
347 for (i=0; i<8; i++) {
348 bits[i] = (bits[i] * 256) / mysrclen;
349 if (!bits[i]) bits[i] = 1;
350 if (bits[i] > 255) bits[i] = 255;
351 cpage_out[i] = bits[i];
354 ret = rubin_do_compress(256, bits, data_in, cpage_out+8, &mysrclen,
359 /* Add back the 8 bytes we took for the probabilities */
362 if (mysrclen <= mydstlen) {
367 *sourcelen = mysrclen;
372 static void rubin_do_decompress(int bit_divider, int *bits,
373 unsigned char *cdata_in,
374 unsigned char *page_out, uint32_t srclen,
378 struct rubin_state rs;
380 init_pushpull(&rs.pp, cdata_in, srclen, 0, 0);
381 init_decode(&rs, bit_divider, bits);
383 while (outpos < destlen)
384 page_out[outpos++] = in_byte(&rs);
388 static int jffs2_rubinmips_decompress(unsigned char *data_in,
389 unsigned char *cpage_out,
390 uint32_t sourcelen, uint32_t dstlen,
393 rubin_do_decompress(BIT_DIVIDER_MIPS, bits_mips, data_in,
394 cpage_out, sourcelen, dstlen);
398 static int jffs2_dynrubin_decompress(unsigned char *data_in,
399 unsigned char *cpage_out,
400 uint32_t sourcelen, uint32_t dstlen,
407 bits[c] = data_in[c];
409 rubin_do_decompress(256, bits, data_in+8, cpage_out, sourcelen-8,
414 static struct jffs2_compressor jffs2_rubinmips_comp = {
415 .priority = JFFS2_RUBINMIPS_PRIORITY,
417 .compr = JFFS2_COMPR_DYNRUBIN,
418 .compress = NULL, /*&jffs2_rubinmips_compress,*/
419 .decompress = &jffs2_rubinmips_decompress,
420 #ifdef JFFS2_RUBINMIPS_DISABLED
427 int jffs2_rubinmips_init(void)
429 return jffs2_register_compressor(&jffs2_rubinmips_comp);
432 void jffs2_rubinmips_exit(void)
434 jffs2_unregister_compressor(&jffs2_rubinmips_comp);
437 static struct jffs2_compressor jffs2_dynrubin_comp = {
438 .priority = JFFS2_DYNRUBIN_PRIORITY,
440 .compr = JFFS2_COMPR_RUBINMIPS,
441 .compress = jffs2_dynrubin_compress,
442 .decompress = &jffs2_dynrubin_decompress,
443 #ifdef JFFS2_DYNRUBIN_DISABLED
450 int jffs2_dynrubin_init(void)
452 return jffs2_register_compressor(&jffs2_dynrubin_comp);
455 void jffs2_dynrubin_exit(void)
457 jffs2_unregister_compressor(&jffs2_dynrubin_comp);