2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2001-2007 Red Hat, Inc.
5 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
7 * Created by Arjan van de Ven <arjanv@redhat.com>
9 * For licensing information, see the file 'LICENCE' in this directory.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/string.h>
16 #include <linux/types.h>
17 #include <linux/jffs2.h>
18 #include <linux/errno.h>
22 #define RUBIN_REG_SIZE 16
23 #define UPPER_BIT_RUBIN (((long) 1)<<(RUBIN_REG_SIZE-1))
24 #define LOWER_BITS_RUBIN ((((long) 1)<<(RUBIN_REG_SIZE-1))-1)
27 #define BIT_DIVIDER_MIPS 1043
28 static int bits_mips
[8] = { 277, 249, 290, 267, 229, 341, 212, 241};
47 static inline void init_pushpull(struct pushpull
*pp
, char *buf
,
48 unsigned buflen
, unsigned ofs
,
54 pp
->reserve
= reserve
;
57 static inline int pushbit(struct pushpull
*pp
, int bit
, int use_reserved
)
59 if (pp
->ofs
>= pp
->buflen
- (use_reserved
?0:pp
->reserve
))
63 pp
->buf
[pp
->ofs
>> 3] |= (1<<(7-(pp
->ofs
& 7)));
65 pp
->buf
[pp
->ofs
>> 3] &= ~(1<<(7-(pp
->ofs
& 7)));
72 static inline int pushedbits(struct pushpull
*pp
)
77 static inline int pullbit(struct pushpull
*pp
)
81 bit
= (pp
->buf
[pp
->ofs
>> 3] >> (7-(pp
->ofs
& 7))) & 1;
88 static void init_rubin(struct rubin_state
*rs
, int div
, int *bits
)
93 rs
->p
= (long) (2 * UPPER_BIT_RUBIN
);
94 rs
->bit_number
= (long) 0;
95 rs
->bit_divider
= div
;
98 rs
->bits
[c
] = bits
[c
];
102 static int encode(struct rubin_state
*rs
, long A
, long B
, int symbol
)
108 while ((rs
->q
>= UPPER_BIT_RUBIN
) ||
109 ((rs
->p
+ rs
->q
) <= UPPER_BIT_RUBIN
)) {
112 ret
= pushbit(&rs
->pp
, (rs
->q
& UPPER_BIT_RUBIN
) ? 1 : 0, 0);
115 rs
->q
&= LOWER_BITS_RUBIN
;
119 i0
= A
* rs
->p
/ (A
+ B
);
138 static void end_rubin(struct rubin_state
*rs
)
143 for (i
= 0; i
< RUBIN_REG_SIZE
; i
++) {
144 pushbit(&rs
->pp
, (UPPER_BIT_RUBIN
& rs
->q
) ? 1 : 0, 1);
145 rs
->q
&= LOWER_BITS_RUBIN
;
151 static void init_decode(struct rubin_state
*rs
, int div
, int *bits
)
153 init_rubin(rs
, div
, bits
);
158 for (rs
->bit_number
= 0; rs
->bit_number
++ < RUBIN_REG_SIZE
;
159 rs
->rec_q
= rs
->rec_q
* 2 + (long) (pullbit(&rs
->pp
)))
163 static void __do_decode(struct rubin_state
*rs
, unsigned long p
,
166 register unsigned long lower_bits_rubin
= LOWER_BITS_RUBIN
;
171 * First, work out how many bits we need from the input stream.
172 * Note that we have already done the initial check on this
173 * loop prior to calling this function.
177 q
&= lower_bits_rubin
;
180 } while ((q
>= UPPER_BIT_RUBIN
) || ((p
+ q
) <= UPPER_BIT_RUBIN
));
185 rs
->bit_number
+= bits
;
188 * Now get the bits. We really want this to be "get n bits".
192 c
= pullbit(&rs
->pp
);
193 rec_q
&= lower_bits_rubin
;
200 static int decode(struct rubin_state
*rs
, long A
, long B
)
202 unsigned long p
= rs
->p
, q
= rs
->q
;
206 if (q
>= UPPER_BIT_RUBIN
|| ((p
+ q
) <= UPPER_BIT_RUBIN
))
207 __do_decode(rs
, p
, q
);
209 i0
= A
* rs
->p
/ (A
+ B
);
216 threshold
= rs
->q
+ i0
;
217 symbol
= rs
->rec_q
>= threshold
;
218 if (rs
->rec_q
>= threshold
) {
230 static int out_byte(struct rubin_state
*rs
, unsigned char byte
)
233 struct rubin_state rs_copy
;
236 for (i
=0; i
<8; i
++) {
237 ret
= encode(rs
, rs
->bit_divider
-rs
->bits
[i
],
238 rs
->bits
[i
], byte
& 1);
240 /* Failed. Restore old state */
249 static int in_byte(struct rubin_state
*rs
)
251 int i
, result
= 0, bit_divider
= rs
->bit_divider
;
253 for (i
= 0; i
< 8; i
++)
254 result
|= decode(rs
, bit_divider
- rs
->bits
[i
],
262 static int rubin_do_compress(int bit_divider
, int *bits
, unsigned char *data_in
,
263 unsigned char *cpage_out
, uint32_t *sourcelen
,
268 struct rubin_state rs
;
270 init_pushpull(&rs
.pp
, cpage_out
, *dstlen
* 8, 0, 32);
272 init_rubin(&rs
, bit_divider
, bits
);
274 while (pos
< (*sourcelen
) && !out_byte(&rs
, data_in
[pos
]))
279 /* Tell the caller how much we managed to compress,
280 * and how much space it took */
282 outpos
= (pushedbits(&rs
.pp
)+7)/8;
285 return -1; /* We didn't actually compress */
291 /* _compress returns the compressed size, -1 if bigger */
292 int jffs2_rubinmips_compress(unsigned char *data_in
, unsigned char *cpage_out
,
293 uint32_t *sourcelen
, uint32_t *dstlen
)
295 return rubin_do_compress(BIT_DIVIDER_MIPS
, bits_mips
, data_in
,
296 cpage_out
, sourcelen
, dstlen
);
299 static int jffs2_dynrubin_compress(unsigned char *data_in
,
300 unsigned char *cpage_out
,
301 uint32_t *sourcelen
, uint32_t *dstlen
)
304 unsigned char histo
[256];
307 uint32_t mysrclen
, mydstlen
;
309 mysrclen
= *sourcelen
;
310 mydstlen
= *dstlen
- 8;
315 memset(histo
, 0, 256);
316 for (i
=0; i
<mysrclen
; i
++)
318 memset(bits
, 0, sizeof(int)*8);
319 for (i
=0; i
<256; i
++) {
338 for (i
=0; i
<8; i
++) {
339 bits
[i
] = (bits
[i
] * 256) / mysrclen
;
340 if (!bits
[i
]) bits
[i
] = 1;
341 if (bits
[i
] > 255) bits
[i
] = 255;
342 cpage_out
[i
] = bits
[i
];
345 ret
= rubin_do_compress(256, bits
, data_in
, cpage_out
+8, &mysrclen
,
350 /* Add back the 8 bytes we took for the probabilities */
353 if (mysrclen
<= mydstlen
) {
358 *sourcelen
= mysrclen
;
363 static void rubin_do_decompress(int bit_divider
, int *bits
,
364 unsigned char *cdata_in
,
365 unsigned char *page_out
, uint32_t srclen
,
369 struct rubin_state rs
;
371 init_pushpull(&rs
.pp
, cdata_in
, srclen
, 0, 0);
372 init_decode(&rs
, bit_divider
, bits
);
374 while (outpos
< destlen
)
375 page_out
[outpos
++] = in_byte(&rs
);
379 static int jffs2_rubinmips_decompress(unsigned char *data_in
,
380 unsigned char *cpage_out
,
381 uint32_t sourcelen
, uint32_t dstlen
)
383 rubin_do_decompress(BIT_DIVIDER_MIPS
, bits_mips
, data_in
,
384 cpage_out
, sourcelen
, dstlen
);
388 static int jffs2_dynrubin_decompress(unsigned char *data_in
,
389 unsigned char *cpage_out
,
390 uint32_t sourcelen
, uint32_t dstlen
)
396 bits
[c
] = data_in
[c
];
398 rubin_do_decompress(256, bits
, data_in
+8, cpage_out
, sourcelen
-8,
403 static struct jffs2_compressor jffs2_rubinmips_comp
= {
404 .priority
= JFFS2_RUBINMIPS_PRIORITY
,
406 .compr
= JFFS2_COMPR_DYNRUBIN
,
407 .compress
= NULL
, /*&jffs2_rubinmips_compress,*/
408 .decompress
= &jffs2_rubinmips_decompress
,
409 #ifdef JFFS2_RUBINMIPS_DISABLED
416 int jffs2_rubinmips_init(void)
418 return jffs2_register_compressor(&jffs2_rubinmips_comp
);
421 void jffs2_rubinmips_exit(void)
423 jffs2_unregister_compressor(&jffs2_rubinmips_comp
);
426 static struct jffs2_compressor jffs2_dynrubin_comp
= {
427 .priority
= JFFS2_DYNRUBIN_PRIORITY
,
429 .compr
= JFFS2_COMPR_RUBINMIPS
,
430 .compress
= jffs2_dynrubin_compress
,
431 .decompress
= &jffs2_dynrubin_decompress
,
432 #ifdef JFFS2_DYNRUBIN_DISABLED
439 int jffs2_dynrubin_init(void)
441 return jffs2_register_compressor(&jffs2_dynrubin_comp
);
444 void jffs2_dynrubin_exit(void)
446 jffs2_unregister_compressor(&jffs2_dynrubin_comp
);