13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/string.h>
16 #include <linux/types.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,
57 static inline int pushbit(
struct pushpull *
pp,
int bit,
int use_reserved)
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;
87 static inline int pulledbits(
struct pushpull *pp)
103 rs->
bits[c] = bits[c];
124 i0 = A * rs->
p / (A +
B);
156 static void init_decode(
struct rubin_state *rs,
int div,
int *bits)
158 init_rubin(rs, div, bits);
168 static void __do_decode(
struct rubin_state *rs,
unsigned long p,
182 q &= lower_bits_rubin;
197 c = pullbit(&rs->
pp);
198 rec_q &= lower_bits_rubin;
205 static int decode(
struct rubin_state *rs,
long A,
long B)
207 unsigned long p = rs->
p, q = rs->
q;
212 __do_decode(rs, p, q);
214 i0 = A * rs->
p / (A +
B);
221 threshold = rs->
q + i0;
223 if (rs->
rec_q >= threshold) {
241 for (i=0; i<8; i++) {
243 rs->
bits[i], byte & 1);
258 for (i = 0; i < 8; i++)
267 static int rubin_do_compress(
int bit_divider,
int *bits,
unsigned char *data_in,
268 unsigned char *cpage_out,
uint32_t *sourcelen,
275 init_pushpull(&rs.
pp, cpage_out, *dstlen * 8, 0, 32);
277 init_rubin(&rs, bit_divider, bits);
279 while (pos < (*sourcelen) && !out_byte(&rs, data_in[pos]))
292 outpos = (pushedbits(&rs.
pp)+7)/8;
302 int jffs2_rubinmips_compress(
unsigned char *data_in,
unsigned char *cpage_out,
306 cpage_out, sourcelen, dstlen);
309 static int jffs2_dynrubin_compress(
unsigned char *data_in,
310 unsigned char *cpage_out,
314 unsigned char histo[256];
319 mysrclen = *sourcelen;
320 mydstlen = *dstlen - 8;
326 for (i=0; i<mysrclen; i++)
328 memset(bits, 0,
sizeof(
int)*8);
329 for (i=0; i<256; i++) {
348 for (i=0; i<8; i++) {
349 bits[
i] = (bits[
i] * 256) / mysrclen;
350 if (!bits[i]) bits[
i] = 1;
351 if (bits[i] > 255) bits[
i] = 255;
352 cpage_out[
i] = bits[
i];
355 ret = rubin_do_compress(256, bits, data_in, cpage_out+8, &mysrclen,
363 if (mysrclen <= mydstlen) {
368 *sourcelen = mysrclen;
373 static void rubin_do_decompress(
int bit_divider,
int *bits,
374 unsigned char *cdata_in,
375 unsigned char *page_out,
uint32_t srclen,
381 init_pushpull(&rs.
pp, cdata_in, srclen, 0, 0);
382 init_decode(&rs, bit_divider, bits);
384 while (outpos < destlen)
385 page_out[outpos++] = in_byte(&rs);
389 static int jffs2_rubinmips_decompress(
unsigned char *data_in,
390 unsigned char *cpage_out,
394 cpage_out, sourcelen, dstlen);
398 static int jffs2_dynrubin_decompress(
unsigned char *data_in,
399 unsigned char *cpage_out,
406 bits[c] = data_in[c];
408 rubin_do_decompress(256, bits, data_in+8, cpage_out, sourcelen-8,
418 .decompress = &jffs2_rubinmips_decompress,
419 #ifdef JFFS2_RUBINMIPS_DISABLED
440 .compress = jffs2_dynrubin_compress,
441 .decompress = &jffs2_dynrubin_decompress,
442 #ifdef JFFS2_DYNRUBIN_DISABLED