GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / jffs2 / compr_rubin.c
blob595184ea5d54bf71f456d9bc97a156733b61026c
1 /*
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 #include <linux/string.h>
14 #include <linux/types.h>
15 #include <linux/jffs2.h>
16 #include <linux/errno.h>
17 #include "compr.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};
28 struct pushpull {
29 unsigned char *buf;
30 unsigned int buflen;
31 unsigned int ofs;
32 unsigned int reserve;
35 struct rubin_state {
36 unsigned long p;
37 unsigned long q;
38 unsigned long rec_q;
39 long bit_number;
40 struct pushpull pp;
41 int bit_divider;
42 int bits[8];
45 static inline void init_pushpull(struct pushpull *pp, char *buf,
46 unsigned buflen, unsigned ofs,
47 unsigned reserve)
49 pp->buf = buf;
50 pp->buflen = buflen;
51 pp->ofs = 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))
58 return -ENOSPC;
60 if (bit)
61 pp->buf[pp->ofs >> 3] |= (1<<(7-(pp->ofs & 7)));
62 else
63 pp->buf[pp->ofs >> 3] &= ~(1<<(7-(pp->ofs & 7)));
65 pp->ofs++;
67 return 0;
70 static inline int pushedbits(struct pushpull *pp)
72 return pp->ofs;
75 static inline int pullbit(struct pushpull *pp)
77 int bit;
79 bit = (pp->buf[pp->ofs >> 3] >> (7-(pp->ofs & 7))) & 1;
81 pp->ofs++;
82 return bit;
85 static inline int pulledbits(struct pushpull *pp)
87 return pp->ofs;
91 static void init_rubin(struct rubin_state *rs, int div, int *bits)
93 int c;
95 rs->q = 0;
96 rs->p = (long) (2 * UPPER_BIT_RUBIN);
97 rs->bit_number = (long) 0;
98 rs->bit_divider = div;
100 for (c=0; c<8; c++)
101 rs->bits[c] = bits[c];
105 static int encode(struct rubin_state *rs, long A, long B, int symbol)
108 long i0, i1;
109 int ret;
111 while ((rs->q >= UPPER_BIT_RUBIN) ||
112 ((rs->p + rs->q) <= UPPER_BIT_RUBIN)) {
113 rs->bit_number++;
115 ret = pushbit(&rs->pp, (rs->q & UPPER_BIT_RUBIN) ? 1 : 0, 0);
116 if (ret)
117 return ret;
118 rs->q &= LOWER_BITS_RUBIN;
119 rs->q <<= 1;
120 rs->p <<= 1;
122 i0 = A * rs->p / (A + B);
123 if (i0 <= 0)
124 i0 = 1;
126 if (i0 >= rs->p)
127 i0 = rs->p - 1;
129 i1 = rs->p - i0;
131 if (symbol == 0)
132 rs->p = i0;
133 else {
134 rs->p = i1;
135 rs->q += i0;
137 return 0;
141 static void end_rubin(struct rubin_state *rs)
144 int i;
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;
149 rs->q <<= 1;
154 static void init_decode(struct rubin_state *rs, int div, int *bits)
156 init_rubin(rs, div, bits);
158 /* behalve lower */
159 rs->rec_q = 0;
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,
167 unsigned long q)
169 register unsigned long lower_bits_rubin = LOWER_BITS_RUBIN;
170 unsigned long rec_q;
171 int c, bits = 0;
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.
178 do {
179 bits++;
180 q &= lower_bits_rubin;
181 q <<= 1;
182 p <<= 1;
183 } while ((q >= UPPER_BIT_RUBIN) || ((p + q) <= UPPER_BIT_RUBIN));
185 rs->p = p;
186 rs->q = q;
188 rs->bit_number += bits;
191 * Now get the bits. We really want this to be "get n bits".
193 rec_q = rs->rec_q;
194 do {
195 c = pullbit(&rs->pp);
196 rec_q &= lower_bits_rubin;
197 rec_q <<= 1;
198 rec_q += c;
199 } while (--bits);
200 rs->rec_q = rec_q;
203 static int decode(struct rubin_state *rs, long A, long B)
205 unsigned long p = rs->p, q = rs->q;
206 long i0, threshold;
207 int symbol;
209 if (q >= UPPER_BIT_RUBIN || ((p + q) <= UPPER_BIT_RUBIN))
210 __do_decode(rs, p, q);
212 i0 = A * rs->p / (A + B);
213 if (i0 <= 0)
214 i0 = 1;
216 if (i0 >= rs->p)
217 i0 = rs->p - 1;
219 threshold = rs->q + i0;
220 symbol = rs->rec_q >= threshold;
221 if (rs->rec_q >= threshold) {
222 rs->q += i0;
223 i0 = rs->p - i0;
226 rs->p = i0;
228 return symbol;
233 static int out_byte(struct rubin_state *rs, unsigned char byte)
235 int i, ret;
236 struct rubin_state rs_copy;
237 rs_copy = *rs;
239 for (i=0; i<8; i++) {
240 ret = encode(rs, rs->bit_divider-rs->bits[i],
241 rs->bits[i], byte & 1);
242 if (ret) {
243 /* Failed. Restore old state */
244 *rs = rs_copy;
245 return ret;
247 byte >>= 1 ;
249 return 0;
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],
258 rs->bits[i]) << i;
260 return result;
265 static int rubin_do_compress(int bit_divider, int *bits, unsigned char *data_in,
266 unsigned char *cpage_out, uint32_t *sourcelen,
267 uint32_t *dstlen)
269 int outpos = 0;
270 int pos=0;
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]))
278 pos++;
280 end_rubin(&rs);
282 if (outpos > pos) {
283 /* We failed */
284 return -1;
287 /* Tell the caller how much we managed to compress,
288 * and how much space it took */
290 outpos = (pushedbits(&rs.pp)+7)/8;
292 if (outpos >= pos)
293 return -1; /* We didn't actually compress */
294 *sourcelen = pos;
295 *dstlen = outpos;
296 return 0;
298 static int jffs2_dynrubin_compress(unsigned char *data_in,
299 unsigned char *cpage_out,
300 uint32_t *sourcelen, uint32_t *dstlen,
301 void *model)
303 int bits[8];
304 unsigned char histo[256];
305 int i;
306 int ret;
307 uint32_t mysrclen, mydstlen;
309 mysrclen = *sourcelen;
310 mydstlen = *dstlen - 8;
312 if (*dstlen <= 12)
313 return -1;
315 memset(histo, 0, 256);
316 for (i=0; i<mysrclen; i++)
317 histo[data_in[i]]++;
318 memset(bits, 0, sizeof(int)*8);
319 for (i=0; i<256; i++) {
320 if (i&128)
321 bits[7] += histo[i];
322 if (i&64)
323 bits[6] += histo[i];
324 if (i&32)
325 bits[5] += histo[i];
326 if (i&16)
327 bits[4] += histo[i];
328 if (i&8)
329 bits[3] += histo[i];
330 if (i&4)
331 bits[2] += histo[i];
332 if (i&2)
333 bits[1] += histo[i];
334 if (i&1)
335 bits[0] += histo[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,
346 &mydstlen);
347 if (ret)
348 return ret;
350 /* Add back the 8 bytes we took for the probabilities */
351 mydstlen += 8;
353 if (mysrclen <= mydstlen) {
354 /* We compressed */
355 return -1;
358 *sourcelen = mysrclen;
359 *dstlen = mydstlen;
360 return 0;
363 static void rubin_do_decompress(int bit_divider, int *bits,
364 unsigned char *cdata_in,
365 unsigned char *page_out, uint32_t srclen,
366 uint32_t destlen)
368 int outpos = 0;
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,
382 void *model)
384 rubin_do_decompress(BIT_DIVIDER_MIPS, bits_mips, data_in,
385 cpage_out, sourcelen, dstlen);
386 return 0;
389 static int jffs2_dynrubin_decompress(unsigned char *data_in,
390 unsigned char *cpage_out,
391 uint32_t sourcelen, uint32_t dstlen,
392 void *model)
394 int bits[8];
395 int c;
397 for (c=0; c<8; c++)
398 bits[c] = data_in[c];
400 rubin_do_decompress(256, bits, data_in+8, cpage_out, sourcelen-8,
401 dstlen);
402 return 0;
405 static struct jffs2_compressor jffs2_rubinmips_comp = {
406 .priority = JFFS2_RUBINMIPS_PRIORITY,
407 .name = "rubinmips",
408 .compr = JFFS2_COMPR_DYNRUBIN,
409 .compress = NULL, /*&jffs2_rubinmips_compress,*/
410 .decompress = &jffs2_rubinmips_decompress,
411 #ifdef JFFS2_RUBINMIPS_DISABLED
412 .disabled = 1,
413 #else
414 .disabled = 0,
415 #endif
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,
430 .name = "dynrubin",
431 .compr = JFFS2_COMPR_RUBINMIPS,
432 .compress = jffs2_dynrubin_compress,
433 .decompress = &jffs2_dynrubin_decompress,
434 #ifdef JFFS2_DYNRUBIN_DISABLED
435 .disabled = 1,
436 #else
437 .disabled = 0,
438 #endif
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);