xref: /openbmc/u-boot/fs/jffs2/compr_rubin.c (revision e99e9575bbeba1b7c48e046547cae065ec0071de)
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 #include <jffs2/jffs2.h>
43fe8c2806Swdenk #include <jffs2/compr_rubin.h>
44fe8c2806Swdenk 
45fe8c2806Swdenk 
rubin_do_decompress(unsigned char * bits,unsigned char * in,unsigned char * page_out,__u32 destlen)46fe8c2806Swdenk void rubin_do_decompress(unsigned char *bits, unsigned char *in,
47fe8c2806Swdenk 			 unsigned char *page_out, __u32 destlen)
48fe8c2806Swdenk {
49*77ddac94SWolfgang Denk 	register char *curr = (char *)page_out;
50*77ddac94SWolfgang Denk 	char *end = (char *)(page_out + destlen);
51fe8c2806Swdenk 	register unsigned long temp;
52fe8c2806Swdenk 	register unsigned long result;
53fe8c2806Swdenk 	register unsigned long p;
54fe8c2806Swdenk 	register unsigned long q;
55fe8c2806Swdenk 	register unsigned long rec_q;
56fe8c2806Swdenk 	register unsigned long bit;
57fe8c2806Swdenk 	register long i0;
58fe8c2806Swdenk 	unsigned long i;
59fe8c2806Swdenk 
60fe8c2806Swdenk 	/* init_pushpull */
61fe8c2806Swdenk 	temp = *(u32 *) in;
62fe8c2806Swdenk 	bit = 16;
63fe8c2806Swdenk 
64fe8c2806Swdenk 	/* init_rubin */
65fe8c2806Swdenk 	q = 0;
66fe8c2806Swdenk 	p = (long) (2 * UPPER_BIT_RUBIN);
67fe8c2806Swdenk 
68fe8c2806Swdenk 	/* init_decode */
69fe8c2806Swdenk 	rec_q = (in[0] << 8) | in[1];
70fe8c2806Swdenk 
71fe8c2806Swdenk 	while (curr < end) {
72fe8c2806Swdenk 		/* in byte */
73fe8c2806Swdenk 
74fe8c2806Swdenk 		result = 0;
75fe8c2806Swdenk 		for (i = 0; i < 8; i++) {
76fe8c2806Swdenk 			/* decode */
77fe8c2806Swdenk 
78fe8c2806Swdenk 			while ((q & UPPER_BIT_RUBIN) || ((p + q) <= UPPER_BIT_RUBIN)) {
79fe8c2806Swdenk 				q &= ~UPPER_BIT_RUBIN;
80fe8c2806Swdenk 				q <<= 1;
81fe8c2806Swdenk 				p <<= 1;
82fe8c2806Swdenk 				rec_q &= ~UPPER_BIT_RUBIN;
83fe8c2806Swdenk 				rec_q <<= 1;
84fe8c2806Swdenk 				rec_q |= (temp >> (bit++ ^ 7)) & 1;
85fe8c2806Swdenk 				if (bit > 31) {
86*77ddac94SWolfgang Denk 					u32 *p = (u32 *)in;
87fe8c2806Swdenk 					bit = 0;
88*77ddac94SWolfgang Denk 					temp = *(++p);
89*77ddac94SWolfgang Denk 					in = (unsigned char *)p;
90fe8c2806Swdenk 				}
91fe8c2806Swdenk 			}
92fe8c2806Swdenk 			i0 =  (bits[i] * p) >> 8;
93fe8c2806Swdenk 
94fe8c2806Swdenk 			if (i0 <= 0) i0 = 1;
95fe8c2806Swdenk 			/* if it fails, it fails, we have our crc
96fe8c2806Swdenk 			if (i0 >= p) i0 = p - 1; */
97fe8c2806Swdenk 
98fe8c2806Swdenk 			result >>= 1;
99fe8c2806Swdenk 			if (rec_q < q + i0) {
100fe8c2806Swdenk 				/* result |= 0x00; */
101fe8c2806Swdenk 				p = i0;
102fe8c2806Swdenk 			} else {
103fe8c2806Swdenk 				result |= 0x80;
104fe8c2806Swdenk 				p -= i0;
105fe8c2806Swdenk 				q += i0;
106fe8c2806Swdenk 			}
107fe8c2806Swdenk 		}
108fe8c2806Swdenk 		*(curr++) = result;
109fe8c2806Swdenk 	}
110fe8c2806Swdenk }
111fe8c2806Swdenk 
dynrubin_decompress(unsigned char * data_in,unsigned char * cpage_out,unsigned long sourcelen,unsigned long dstlen)112fe8c2806Swdenk void dynrubin_decompress(unsigned char *data_in, unsigned char *cpage_out,
113fe8c2806Swdenk 		   unsigned long sourcelen, unsigned long dstlen)
114fe8c2806Swdenk {
115fe8c2806Swdenk 	unsigned char bits[8];
116fe8c2806Swdenk 	int c;
117fe8c2806Swdenk 
118fe8c2806Swdenk 	for (c=0; c<8; c++)
119fe8c2806Swdenk 		bits[c] = (256 - data_in[c]);
120fe8c2806Swdenk 
121fe8c2806Swdenk 	rubin_do_decompress(bits, data_in+8, cpage_out, dstlen);
122fe8c2806Swdenk }
123