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