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