2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2001-2007 Red Hat, Inc.
6 * Created by Arjan van de Ven <arjanv@redhat.com>
8 * For licensing information, see the file 'LICENCE' in this directory.
12 #include <linux/string.h>
13 #include <linux/types.h>
14 #include <linux/jffs2.h>
15 #include <linux/errno.h>
19 #define RUBIN_REG_SIZE 16
20 #define UPPER_BIT_RUBIN (((long) 1)<<(RUBIN_REG_SIZE-1))
21 #define LOWER_BITS_RUBIN ((((long) 1)<<(RUBIN_REG_SIZE-1))-1)
24 #define BIT_DIVIDER_MIPS 1043
25 static int bits_mips
[8] = { 277,249,290,267,229,341,212,241}; /* mips32 */
27 #include <linux/errno.h>
46 static inline void init_pushpull(struct pushpull
*pp
, char *buf
, unsigned buflen
, unsigned ofs
, unsigned reserve
)
51 pp
->reserve
= reserve
;
54 static inline int pushbit(struct pushpull
*pp
, int bit
, int use_reserved
)
56 if (pp
->ofs
>= pp
->buflen
- (use_reserved
?0:pp
->reserve
)) {
61 pp
->buf
[pp
->ofs
>> 3] |= (1<<(7-(pp
->ofs
&7)));
64 pp
->buf
[pp
->ofs
>> 3] &= ~(1<<(7-(pp
->ofs
&7)));
71 static inline int pushedbits(struct pushpull
*pp
)
76 static inline int pullbit(struct pushpull
*pp
)
80 bit
= (pp
->buf
[pp
->ofs
>> 3] >> (7-(pp
->ofs
& 7))) & 1;
86 static inline int pulledbits(struct pushpull
*pp
)
92 static void init_rubin(struct rubin_state
*rs
, int div
, int *bits
)
97 rs
->p
= (long) (2 * UPPER_BIT_RUBIN
);
98 rs
->bit_number
= (long) 0;
99 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
) || ((rs
->p
+ rs
->q
) <= UPPER_BIT_RUBIN
)) {
114 ret
= pushbit(&rs
->pp
, (rs
->q
& UPPER_BIT_RUBIN
) ? 1 : 0, 0);
117 rs
->q
&= LOWER_BITS_RUBIN
;
121 i0
= A
* rs
->p
/ (A
+ B
);
140 static void end_rubin(struct rubin_state
*rs
)
145 for (i
= 0; i
< RUBIN_REG_SIZE
; i
++) {
146 pushbit(&rs
->pp
, (UPPER_BIT_RUBIN
& rs
->q
) ? 1 : 0, 1);
147 rs
->q
&= LOWER_BITS_RUBIN
;
153 static void init_decode(struct rubin_state
*rs
, int div
, int *bits
)
155 init_rubin(rs
, div
, bits
);
160 for (rs
->bit_number
= 0; rs
->bit_number
++ < RUBIN_REG_SIZE
; rs
->rec_q
= rs
->rec_q
* 2 + (long) (pullbit(&rs
->pp
)))
164 static void __do_decode(struct rubin_state
*rs
, unsigned long p
, unsigned long q
)
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
);
217 threshold
= rs
->q
+ i0
;
218 symbol
= rs
->rec_q
>= threshold
;
219 if (rs
->rec_q
>= threshold
) {
231 static int out_byte(struct rubin_state
*rs
, unsigned char byte
)
234 struct rubin_state rs_copy
;
238 ret
= encode(rs
, rs
->bit_divider
-rs
->bits
[i
],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
], rs
->bits
[i
]) << i
;
261 static int rubin_do_compress(int bit_divider
, int *bits
, unsigned char *data_in
,
262 unsigned char *cpage_out
, uint32_t *sourcelen
, uint32_t *dstlen
)
266 struct rubin_state rs
;
268 init_pushpull(&rs
.pp
, cpage_out
, *dstlen
* 8, 0, 32);
270 init_rubin(&rs
, bit_divider
, bits
);
272 while (pos
< (*sourcelen
) && !out_byte(&rs
, data_in
[pos
]))
282 /* Tell the caller how much we managed to compress,
283 * and how much space it took */
285 outpos
= (pushedbits(&rs
.pp
)+7)/8;
288 return -1; /* We didn't actually compress */
294 /* _compress returns the compressed size, -1 if bigger */
295 int jffs2_rubinmips_compress(unsigned char *data_in
, unsigned char *cpage_out
,
296 uint32_t *sourcelen
, uint32_t *dstlen
, void *model
)
298 return rubin_do_compress(BIT_DIVIDER_MIPS
, bits_mips
, data_in
, cpage_out
, sourcelen
, dstlen
);
301 static int jffs2_dynrubin_compress(unsigned char *data_in
,
302 unsigned char *cpage_out
,
303 uint32_t *sourcelen
, uint32_t *dstlen
,
307 unsigned char histo
[256];
310 uint32_t mysrclen
, mydstlen
;
312 mysrclen
= *sourcelen
;
313 mydstlen
= *dstlen
- 8;
318 memset(histo
, 0, 256);
319 for (i
=0; i
<mysrclen
; i
++) {
322 memset(bits
, 0, sizeof(int)*8);
323 for (i
=0; i
<256; i
++) {
342 for (i
=0; i
<8; i
++) {
343 bits
[i
] = (bits
[i
] * 256) / mysrclen
;
344 if (!bits
[i
]) bits
[i
] = 1;
345 if (bits
[i
] > 255) bits
[i
] = 255;
346 cpage_out
[i
] = bits
[i
];
349 ret
= rubin_do_compress(256, bits
, data_in
, cpage_out
+8, &mysrclen
, &mydstlen
);
353 /* Add back the 8 bytes we took for the probabilities */
356 if (mysrclen
<= mydstlen
) {
361 *sourcelen
= mysrclen
;
366 static void rubin_do_decompress(int bit_divider
, int *bits
, unsigned char *cdata_in
,
367 unsigned char *page_out
, uint32_t srclen
, uint32_t destlen
)
370 struct rubin_state rs
;
372 init_pushpull(&rs
.pp
, cdata_in
, srclen
, 0, 0);
373 init_decode(&rs
, bit_divider
, bits
);
375 while (outpos
< destlen
) {
376 page_out
[outpos
++] = in_byte(&rs
);
381 static int jffs2_rubinmips_decompress(unsigned char *data_in
,
382 unsigned char *cpage_out
,
383 uint32_t sourcelen
, uint32_t dstlen
,
386 rubin_do_decompress(BIT_DIVIDER_MIPS
, bits_mips
, data_in
, cpage_out
, sourcelen
, dstlen
);
390 static int jffs2_dynrubin_decompress(unsigned char *data_in
,
391 unsigned char *cpage_out
,
392 uint32_t sourcelen
, uint32_t dstlen
,
399 bits
[c
] = data_in
[c
];
401 rubin_do_decompress(256, bits
, data_in
+8, cpage_out
, sourcelen
-8, dstlen
);
405 static struct jffs2_compressor jffs2_rubinmips_comp
= {
406 .priority
= JFFS2_RUBINMIPS_PRIORITY
,
408 .compr
= JFFS2_COMPR_DYNRUBIN
,
409 .compress
= NULL
, /*&jffs2_rubinmips_compress,*/
410 .decompress
= &jffs2_rubinmips_decompress
,
411 #ifdef JFFS2_RUBINMIPS_DISABLED
418 int jffs2_rubinmips_init(void)
420 return jffs2_register_compressor(&jffs2_rubinmips_comp
);
423 void jffs2_rubinmips_exit(void)
425 jffs2_unregister_compressor(&jffs2_rubinmips_comp
);
428 static struct jffs2_compressor jffs2_dynrubin_comp
= {
429 .priority
= JFFS2_DYNRUBIN_PRIORITY
,
431 .compr
= JFFS2_COMPR_RUBINMIPS
,
432 .compress
= jffs2_dynrubin_compress
,
433 .decompress
= &jffs2_dynrubin_decompress
,
434 #ifdef JFFS2_DYNRUBIN_DISABLED
441 int jffs2_dynrubin_init(void)
443 return jffs2_register_compressor(&jffs2_dynrubin_comp
);
446 void jffs2_dynrubin_exit(void)
448 jffs2_unregister_compressor(&jffs2_dynrubin_comp
);