2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001 Red Hat, Inc.
6 * Created by Arjan van de Ven <arjanv@redhat.com>
8 * The original JFFS, from which the design for JFFS2 was derived,
9 * was designed and implemented by Axis Communications AB.
11 * The contents of this file are subject to the Red Hat eCos Public
12 * License Version 1.1 (the "Licence"); you may not use this file
13 * except in compliance with the Licence. You may obtain a copy of
14 * the Licence at http://www.redhat.com/
16 * Software distributed under the Licence is distributed on an "AS IS"
17 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
18 * See the Licence for the specific language governing rights and
19 * limitations under the Licence.
21 * The Original Code is JFFS2 - Journalling Flash File System, version 2
23 * Alternatively, the contents of this file may be used under the
24 * terms of the GNU General Public License version 2 (the "GPL"), in
25 * which case the provisions of the GPL are applicable instead of the
26 * above. If you wish to allow the use of your version of this file
27 * only under the terms of the GPL and not to allow others to use your
28 * version of this file under the RHEPL, indicate your decision by
29 * deleting the provisions above and replace them with the notice and
30 * other provisions required by the GPL. If you do not delete the
31 * provisions above, a recipient may use your version of this file
32 * under either the RHEPL or the GPL.
34 * $Id: compr_rubin.c,v 1.13 2001/09/23 10:06:05 rmk Exp $
39 #include <linux/string.h>
40 #include <linux/types.h>
41 #include "compr_rubin.h"
42 #include "histo_mips.h"
46 void init_rubin(struct rubin_state *rs, int div, int *bits)
51 rs->p = (long) (2 * UPPER_BIT_RUBIN);
52 rs->bit_number = (long) 0;
53 rs->bit_divider = div;
55 rs->bits[c] = bits[c];
59 int encode(struct rubin_state *rs, long A, long B, int symbol)
65 while ((rs->q >= UPPER_BIT_RUBIN) || ((rs->p + rs->q) <= UPPER_BIT_RUBIN)) {
68 ret = pushbit(&rs->pp, (rs->q & UPPER_BIT_RUBIN) ? 1 : 0, 0);
71 rs->q &= LOWER_BITS_RUBIN;
75 i0 = A * rs->p / (A + B);
94 void end_rubin(struct rubin_state *rs)
99 for (i = 0; i < RUBIN_REG_SIZE; i++) {
100 pushbit(&rs->pp, (UPPER_BIT_RUBIN & rs->q) ? 1 : 0, 1);
101 rs->q &= LOWER_BITS_RUBIN;
107 void init_decode(struct rubin_state *rs, int div, int *bits)
109 init_rubin(rs, div, bits);
114 for (rs->bit_number = 0; rs->bit_number++ < RUBIN_REG_SIZE; rs->rec_q = rs->rec_q * 2 + (long) (pullbit(&rs->pp)))
118 static void __do_decode(struct rubin_state *rs, unsigned long p, unsigned long q)
120 register unsigned long lower_bits_rubin = LOWER_BITS_RUBIN;
125 * First, work out how many bits we need from the input stream.
126 * Note that we have already done the initial check on this
127 * loop prior to calling this function.
131 q &= lower_bits_rubin;
134 } while ((q >= UPPER_BIT_RUBIN) || ((p + q) <= UPPER_BIT_RUBIN));
139 rs->bit_number += bits;
142 * Now get the bits. We really want this to be "get n bits".
146 c = pullbit(&rs->pp);
147 rec_q &= lower_bits_rubin;
154 int decode(struct rubin_state *rs, long A, long B)
156 unsigned long p = rs->p, q = rs->q;
160 if (q >= UPPER_BIT_RUBIN || ((p + q) <= UPPER_BIT_RUBIN))
161 __do_decode(rs, p, q);
163 i0 = A * rs->p / (A + B);
171 threshold = rs->q + i0;
172 symbol = rs->rec_q >= threshold;
173 if (rs->rec_q >= threshold) {
185 static int out_byte(struct rubin_state *rs, unsigned char byte)
188 struct rubin_state rs_copy;
192 ret = encode(rs, rs->bit_divider-rs->bits[i],rs->bits[i],byte&1);
194 /* Failed. Restore old state */
203 static int in_byte(struct rubin_state *rs)
205 int i, result = 0, bit_divider = rs->bit_divider;
207 for (i = 0; i < 8; i++)
208 result |= decode(rs, bit_divider - rs->bits[i], rs->bits[i]) << i;
215 int rubin_do_compress(int bit_divider, int *bits, unsigned char *data_in,
216 unsigned char *cpage_out, __u32 *sourcelen, __u32 *dstlen)
220 struct rubin_state rs;
222 init_pushpull(&rs.pp, cpage_out, *dstlen * 8, 0, 32);
224 init_rubin(&rs, bit_divider, bits);
226 while (pos < (*sourcelen) && !out_byte(&rs, data_in[pos]))
236 /* Tell the caller how much we managed to compress,
237 * and how much space it took */
239 outpos = (pushedbits(&rs.pp)+7)/8;
242 return -1; /* We didn't actually compress */
248 /* _compress returns the compressed size, -1 if bigger */
249 int rubinmips_compress(unsigned char *data_in, unsigned char *cpage_out,
250 __u32 *sourcelen, __u32 *dstlen)
252 return rubin_do_compress(BIT_DIVIDER_MIPS, bits_mips, data_in, cpage_out, sourcelen, dstlen);
255 int dynrubin_compress(unsigned char *data_in, unsigned char *cpage_out,
256 __u32 *sourcelen, __u32 *dstlen)
259 unsigned char histo[256];
262 __u32 mysrclen, mydstlen;
264 mysrclen = *sourcelen;
265 mydstlen = *dstlen - 8;
270 memset(histo, 0, 256);
271 for (i=0; i<mysrclen; i++) {
274 memset(bits, 0, sizeof(int)*8);
275 for (i=0; i<256; i++) {
294 for (i=0; i<8; i++) {
295 bits[i] = (bits[i] * 256) / mysrclen;
296 if (!bits[i]) bits[i] = 1;
297 if (bits[i] > 255) bits[i] = 255;
298 cpage_out[i] = bits[i];
301 ret = rubin_do_compress(256, bits, data_in, cpage_out+8, &mysrclen, &mydstlen);
305 /* Add back the 8 bytes we took for the probabilities */
308 if (mysrclen <= mydstlen) {
313 *sourcelen = mysrclen;
318 void rubin_do_decompress(int bit_divider, int *bits, unsigned char *cdata_in,
319 unsigned char *page_out, __u32 srclen, __u32 destlen)
322 struct rubin_state rs;
324 init_pushpull(&rs.pp, cdata_in, srclen, 0, 0);
325 init_decode(&rs, bit_divider, bits);
327 while (outpos < destlen) {
328 page_out[outpos++] = in_byte(&rs);
333 void rubinmips_decompress(unsigned char *data_in, unsigned char *cpage_out,
334 __u32 sourcelen, __u32 dstlen)
336 rubin_do_decompress(BIT_DIVIDER_MIPS, bits_mips, data_in, cpage_out, sourcelen, dstlen);
339 void dynrubin_decompress(unsigned char *data_in, unsigned char *cpage_out,
340 __u32 sourcelen, __u32 dstlen)
346 bits[c] = data_in[c];
348 rubin_do_decompress(256, bits, data_in+8, cpage_out, sourcelen-8, dstlen);