[SCSI] aacraid: regression fix
[linux-2.6.22.y-op.git] / fs / jffs2 / compr_rubin.c
blob09422388fb96716681b151871144eed992b72be7
1 /*
2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001, 2002 Red Hat, Inc.
6 * Created by Arjan van de Ven <arjanv@redhat.com>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: compr_rubin.c,v 1.20 2004/06/23 16:34:40 havasi Exp $
15 #include <linux/string.h>
16 #include <linux/types.h>
17 #include <linux/jffs2.h>
18 #include "compr_rubin.h"
19 #include "histo_mips.h"
20 #include "compr.h"
22 static void init_rubin(struct rubin_state *rs, int div, int *bits)
24 int c;
26 rs->q = 0;
27 rs->p = (long) (2 * UPPER_BIT_RUBIN);
28 rs->bit_number = (long) 0;
29 rs->bit_divider = div;
30 for (c=0; c<8; c++)
31 rs->bits[c] = bits[c];
35 static int encode(struct rubin_state *rs, long A, long B, int symbol)
38 long i0, i1;
39 int ret;
41 while ((rs->q >= UPPER_BIT_RUBIN) || ((rs->p + rs->q) <= UPPER_BIT_RUBIN)) {
42 rs->bit_number++;
44 ret = pushbit(&rs->pp, (rs->q & UPPER_BIT_RUBIN) ? 1 : 0, 0);
45 if (ret)
46 return ret;
47 rs->q &= LOWER_BITS_RUBIN;
48 rs->q <<= 1;
49 rs->p <<= 1;
51 i0 = A * rs->p / (A + B);
52 if (i0 <= 0) {
53 i0 = 1;
55 if (i0 >= rs->p) {
56 i0 = rs->p - 1;
58 i1 = rs->p - i0;
60 if (symbol == 0)
61 rs->p = i0;
62 else {
63 rs->p = i1;
64 rs->q += i0;
66 return 0;
70 static void end_rubin(struct rubin_state *rs)
73 int i;
75 for (i = 0; i < RUBIN_REG_SIZE; i++) {
76 pushbit(&rs->pp, (UPPER_BIT_RUBIN & rs->q) ? 1 : 0, 1);
77 rs->q &= LOWER_BITS_RUBIN;
78 rs->q <<= 1;
83 static void init_decode(struct rubin_state *rs, int div, int *bits)
85 init_rubin(rs, div, bits);
87 /* behalve lower */
88 rs->rec_q = 0;
90 for (rs->bit_number = 0; rs->bit_number++ < RUBIN_REG_SIZE; rs->rec_q = rs->rec_q * 2 + (long) (pullbit(&rs->pp)))
94 static void __do_decode(struct rubin_state *rs, unsigned long p, unsigned long q)
96 register unsigned long lower_bits_rubin = LOWER_BITS_RUBIN;
97 unsigned long rec_q;
98 int c, bits = 0;
101 * First, work out how many bits we need from the input stream.
102 * Note that we have already done the initial check on this
103 * loop prior to calling this function.
105 do {
106 bits++;
107 q &= lower_bits_rubin;
108 q <<= 1;
109 p <<= 1;
110 } while ((q >= UPPER_BIT_RUBIN) || ((p + q) <= UPPER_BIT_RUBIN));
112 rs->p = p;
113 rs->q = q;
115 rs->bit_number += bits;
118 * Now get the bits. We really want this to be "get n bits".
120 rec_q = rs->rec_q;
121 do {
122 c = pullbit(&rs->pp);
123 rec_q &= lower_bits_rubin;
124 rec_q <<= 1;
125 rec_q += c;
126 } while (--bits);
127 rs->rec_q = rec_q;
130 static int decode(struct rubin_state *rs, long A, long B)
132 unsigned long p = rs->p, q = rs->q;
133 long i0, threshold;
134 int symbol;
136 if (q >= UPPER_BIT_RUBIN || ((p + q) <= UPPER_BIT_RUBIN))
137 __do_decode(rs, p, q);
139 i0 = A * rs->p / (A + B);
140 if (i0 <= 0) {
141 i0 = 1;
143 if (i0 >= rs->p) {
144 i0 = rs->p - 1;
147 threshold = rs->q + i0;
148 symbol = rs->rec_q >= threshold;
149 if (rs->rec_q >= threshold) {
150 rs->q += i0;
151 i0 = rs->p - i0;
154 rs->p = i0;
156 return symbol;
161 static int out_byte(struct rubin_state *rs, unsigned char byte)
163 int i, ret;
164 struct rubin_state rs_copy;
165 rs_copy = *rs;
167 for (i=0;i<8;i++) {
168 ret = encode(rs, rs->bit_divider-rs->bits[i],rs->bits[i],byte&1);
169 if (ret) {
170 /* Failed. Restore old state */
171 *rs = rs_copy;
172 return ret;
174 byte=byte>>1;
176 return 0;
179 static int in_byte(struct rubin_state *rs)
181 int i, result = 0, bit_divider = rs->bit_divider;
183 for (i = 0; i < 8; i++)
184 result |= decode(rs, bit_divider - rs->bits[i], rs->bits[i]) << i;
186 return result;
191 static int rubin_do_compress(int bit_divider, int *bits, unsigned char *data_in,
192 unsigned char *cpage_out, uint32_t *sourcelen, uint32_t *dstlen)
194 int outpos = 0;
195 int pos=0;
196 struct rubin_state rs;
198 init_pushpull(&rs.pp, cpage_out, *dstlen * 8, 0, 32);
200 init_rubin(&rs, bit_divider, bits);
202 while (pos < (*sourcelen) && !out_byte(&rs, data_in[pos]))
203 pos++;
205 end_rubin(&rs);
207 if (outpos > pos) {
208 /* We failed */
209 return -1;
212 /* Tell the caller how much we managed to compress,
213 * and how much space it took */
215 outpos = (pushedbits(&rs.pp)+7)/8;
217 if (outpos >= pos)
218 return -1; /* We didn't actually compress */
219 *sourcelen = pos;
220 *dstlen = outpos;
221 return 0;
223 #if 0
224 /* _compress returns the compressed size, -1 if bigger */
225 int jffs2_rubinmips_compress(unsigned char *data_in, unsigned char *cpage_out,
226 uint32_t *sourcelen, uint32_t *dstlen, void *model)
228 return rubin_do_compress(BIT_DIVIDER_MIPS, bits_mips, data_in, cpage_out, sourcelen, dstlen);
230 #endif
231 static int jffs2_dynrubin_compress(unsigned char *data_in,
232 unsigned char *cpage_out,
233 uint32_t *sourcelen, uint32_t *dstlen,
234 void *model)
236 int bits[8];
237 unsigned char histo[256];
238 int i;
239 int ret;
240 uint32_t mysrclen, mydstlen;
242 mysrclen = *sourcelen;
243 mydstlen = *dstlen - 8;
245 if (*dstlen <= 12)
246 return -1;
248 memset(histo, 0, 256);
249 for (i=0; i<mysrclen; i++) {
250 histo[data_in[i]]++;
252 memset(bits, 0, sizeof(int)*8);
253 for (i=0; i<256; i++) {
254 if (i&128)
255 bits[7] += histo[i];
256 if (i&64)
257 bits[6] += histo[i];
258 if (i&32)
259 bits[5] += histo[i];
260 if (i&16)
261 bits[4] += histo[i];
262 if (i&8)
263 bits[3] += histo[i];
264 if (i&4)
265 bits[2] += histo[i];
266 if (i&2)
267 bits[1] += histo[i];
268 if (i&1)
269 bits[0] += histo[i];
272 for (i=0; i<8; i++) {
273 bits[i] = (bits[i] * 256) / mysrclen;
274 if (!bits[i]) bits[i] = 1;
275 if (bits[i] > 255) bits[i] = 255;
276 cpage_out[i] = bits[i];
279 ret = rubin_do_compress(256, bits, data_in, cpage_out+8, &mysrclen, &mydstlen);
280 if (ret)
281 return ret;
283 /* Add back the 8 bytes we took for the probabilities */
284 mydstlen += 8;
286 if (mysrclen <= mydstlen) {
287 /* We compressed */
288 return -1;
291 *sourcelen = mysrclen;
292 *dstlen = mydstlen;
293 return 0;
296 static void rubin_do_decompress(int bit_divider, int *bits, unsigned char *cdata_in,
297 unsigned char *page_out, uint32_t srclen, uint32_t destlen)
299 int outpos = 0;
300 struct rubin_state rs;
302 init_pushpull(&rs.pp, cdata_in, srclen, 0, 0);
303 init_decode(&rs, bit_divider, bits);
305 while (outpos < destlen) {
306 page_out[outpos++] = in_byte(&rs);
311 static int jffs2_rubinmips_decompress(unsigned char *data_in,
312 unsigned char *cpage_out,
313 uint32_t sourcelen, uint32_t dstlen,
314 void *model)
316 rubin_do_decompress(BIT_DIVIDER_MIPS, bits_mips, data_in, cpage_out, sourcelen, dstlen);
317 return 0;
320 static int jffs2_dynrubin_decompress(unsigned char *data_in,
321 unsigned char *cpage_out,
322 uint32_t sourcelen, uint32_t dstlen,
323 void *model)
325 int bits[8];
326 int c;
328 for (c=0; c<8; c++)
329 bits[c] = data_in[c];
331 rubin_do_decompress(256, bits, data_in+8, cpage_out, sourcelen-8, dstlen);
332 return 0;
335 static struct jffs2_compressor jffs2_rubinmips_comp = {
336 .priority = JFFS2_RUBINMIPS_PRIORITY,
337 .name = "rubinmips",
338 .compr = JFFS2_COMPR_DYNRUBIN,
339 .compress = NULL, /*&jffs2_rubinmips_compress,*/
340 .decompress = &jffs2_rubinmips_decompress,
341 #ifdef JFFS2_RUBINMIPS_DISABLED
342 .disabled = 1,
343 #else
344 .disabled = 0,
345 #endif
348 int jffs2_rubinmips_init(void)
350 return jffs2_register_compressor(&jffs2_rubinmips_comp);
353 void jffs2_rubinmips_exit(void)
355 jffs2_unregister_compressor(&jffs2_rubinmips_comp);
358 static struct jffs2_compressor jffs2_dynrubin_comp = {
359 .priority = JFFS2_DYNRUBIN_PRIORITY,
360 .name = "dynrubin",
361 .compr = JFFS2_COMPR_RUBINMIPS,
362 .compress = jffs2_dynrubin_compress,
363 .decompress = &jffs2_dynrubin_decompress,
364 #ifdef JFFS2_DYNRUBIN_DISABLED
365 .disabled = 1,
366 #else
367 .disabled = 0,
368 #endif
371 int jffs2_dynrubin_init(void)
373 return jffs2_register_compressor(&jffs2_dynrubin_comp);
376 void jffs2_dynrubin_exit(void)
378 jffs2_unregister_compressor(&jffs2_dynrubin_comp);