1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * JFFS2 -- Journalling Flash File System, Version 2.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2001 Red Hat, Inc.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Created by Arjan van de Ven <arjanv@redhat.com>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Heavily modified by Russ Dill <Russ.Dill@asu.edu> in an attempt at
9*4882a593Smuzhiyun * a little more speed.
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * The original JFFS, from which the design for JFFS2 was derived,
12*4882a593Smuzhiyun * was designed and implemented by Axis Communications AB.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * The contents of this file are subject to the Red Hat eCos Public
15*4882a593Smuzhiyun * License Version 1.1 (the "Licence"); you may not use this file
16*4882a593Smuzhiyun * except in compliance with the Licence. You may obtain a copy of
17*4882a593Smuzhiyun * the Licence at http://www.redhat.com/
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * Software distributed under the Licence is distributed on an "AS IS"
20*4882a593Smuzhiyun * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
21*4882a593Smuzhiyun * See the Licence for the specific language governing rights and
22*4882a593Smuzhiyun * limitations under the Licence.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * The Original Code is JFFS2 - Journalling Flash File System, version 2
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * Alternatively, the contents of this file may be used under the
27*4882a593Smuzhiyun * terms of the GNU General Public License version 2 (the "GPL"), in
28*4882a593Smuzhiyun * which case the provisions of the GPL are applicable instead of the
29*4882a593Smuzhiyun * above. If you wish to allow the use of your version of this file
30*4882a593Smuzhiyun * only under the terms of the GPL and not to allow others to use your
31*4882a593Smuzhiyun * version of this file under the RHEPL, indicate your decision by
32*4882a593Smuzhiyun * deleting the provisions above and replace them with the notice and
33*4882a593Smuzhiyun * other provisions required by the GPL. If you do not delete the
34*4882a593Smuzhiyun * provisions above, a recipient may use your version of this file
35*4882a593Smuzhiyun * under either the RHEPL or the GPL.
36*4882a593Smuzhiyun *
37*4882a593Smuzhiyun * $Id: compr_rubin.c,v 1.2 2002/01/24 22:58:42 rfeany Exp $
38*4882a593Smuzhiyun *
39*4882a593Smuzhiyun */
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun #include <config.h>
42*4882a593Smuzhiyun #include <jffs2/jffs2.h>
43*4882a593Smuzhiyun #include <jffs2/compr_rubin.h>
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun
rubin_do_decompress(unsigned char * bits,unsigned char * in,unsigned char * page_out,__u32 destlen)46*4882a593Smuzhiyun void rubin_do_decompress(unsigned char *bits, unsigned char *in,
47*4882a593Smuzhiyun unsigned char *page_out, __u32 destlen)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun register char *curr = (char *)page_out;
50*4882a593Smuzhiyun char *end = (char *)(page_out + destlen);
51*4882a593Smuzhiyun register unsigned long temp;
52*4882a593Smuzhiyun register unsigned long result;
53*4882a593Smuzhiyun register unsigned long p;
54*4882a593Smuzhiyun register unsigned long q;
55*4882a593Smuzhiyun register unsigned long rec_q;
56*4882a593Smuzhiyun register unsigned long bit;
57*4882a593Smuzhiyun register long i0;
58*4882a593Smuzhiyun unsigned long i;
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /* init_pushpull */
61*4882a593Smuzhiyun temp = *(u32 *) in;
62*4882a593Smuzhiyun bit = 16;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun /* init_rubin */
65*4882a593Smuzhiyun q = 0;
66*4882a593Smuzhiyun p = (long) (2 * UPPER_BIT_RUBIN);
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /* init_decode */
69*4882a593Smuzhiyun rec_q = (in[0] << 8) | in[1];
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun while (curr < end) {
72*4882a593Smuzhiyun /* in byte */
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun result = 0;
75*4882a593Smuzhiyun for (i = 0; i < 8; i++) {
76*4882a593Smuzhiyun /* decode */
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun while ((q & UPPER_BIT_RUBIN) || ((p + q) <= UPPER_BIT_RUBIN)) {
79*4882a593Smuzhiyun q &= ~UPPER_BIT_RUBIN;
80*4882a593Smuzhiyun q <<= 1;
81*4882a593Smuzhiyun p <<= 1;
82*4882a593Smuzhiyun rec_q &= ~UPPER_BIT_RUBIN;
83*4882a593Smuzhiyun rec_q <<= 1;
84*4882a593Smuzhiyun rec_q |= (temp >> (bit++ ^ 7)) & 1;
85*4882a593Smuzhiyun if (bit > 31) {
86*4882a593Smuzhiyun u32 *p = (u32 *)in;
87*4882a593Smuzhiyun bit = 0;
88*4882a593Smuzhiyun temp = *(++p);
89*4882a593Smuzhiyun in = (unsigned char *)p;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun i0 = (bits[i] * p) >> 8;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun if (i0 <= 0) i0 = 1;
95*4882a593Smuzhiyun /* if it fails, it fails, we have our crc
96*4882a593Smuzhiyun if (i0 >= p) i0 = p - 1; */
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun result >>= 1;
99*4882a593Smuzhiyun if (rec_q < q + i0) {
100*4882a593Smuzhiyun /* result |= 0x00; */
101*4882a593Smuzhiyun p = i0;
102*4882a593Smuzhiyun } else {
103*4882a593Smuzhiyun result |= 0x80;
104*4882a593Smuzhiyun p -= i0;
105*4882a593Smuzhiyun q += i0;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun *(curr++) = result;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
dynrubin_decompress(unsigned char * data_in,unsigned char * cpage_out,unsigned long sourcelen,unsigned long dstlen)112*4882a593Smuzhiyun void dynrubin_decompress(unsigned char *data_in, unsigned char *cpage_out,
113*4882a593Smuzhiyun unsigned long sourcelen, unsigned long dstlen)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun unsigned char bits[8];
116*4882a593Smuzhiyun int c;
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun for (c=0; c<8; c++)
119*4882a593Smuzhiyun bits[c] = (256 - data_in[c]);
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun rubin_do_decompress(bits, data_in+8, cpage_out, dstlen);
122*4882a593Smuzhiyun }
123