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