blob: 556de100ebd5a5318fec58cc47c6187315ed2b7b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
David Woodhousec00c3102007-04-25 14:16:47 +01004 * Copyright © 2001-2007 Red Hat, Inc.
David Woodhouse6088c052010-08-08 14:15:22 +01005 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Created by Arjan van de Ven <arjanv@redhat.com>
8 *
9 * For licensing information, see the file 'LICENCE' in this directory.
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Joe Perches5a528952012-02-15 15:56:45 -080013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/string.h>
16#include <linux/types.h>
17#include <linux/jffs2.h>
David Woodhousec00c3102007-04-25 14:16:47 +010018#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "compr.h"
20
David Woodhousec00c3102007-04-25 14:16:47 +010021
22#define RUBIN_REG_SIZE 16
23#define UPPER_BIT_RUBIN (((long) 1)<<(RUBIN_REG_SIZE-1))
24#define LOWER_BITS_RUBIN ((((long) 1)<<(RUBIN_REG_SIZE-1))-1)
25
26
David Woodhousec00c3102007-04-25 14:16:47 +010027#define BIT_DIVIDER_MIPS 1043
David Woodhouse0bc43822008-12-10 15:34:49 +000028static int bits_mips[8] = { 277, 249, 290, 267, 229, 341, 212, 241};
David Woodhousec00c3102007-04-25 14:16:47 +010029
30struct pushpull {
31 unsigned char *buf;
32 unsigned int buflen;
33 unsigned int ofs;
34 unsigned int reserve;
35};
36
Andrew Mortonf6449f42007-04-26 07:27:04 +010037struct rubin_state {
38 unsigned long p;
39 unsigned long q;
40 unsigned long rec_q;
41 long bit_number;
42 struct pushpull pp;
43 int bit_divider;
44 int bits[8];
45};
David Woodhousec00c3102007-04-25 14:16:47 +010046
David Woodhouse0bc43822008-12-10 15:34:49 +000047static inline void init_pushpull(struct pushpull *pp, char *buf,
48 unsigned buflen, unsigned ofs,
49 unsigned reserve)
David Woodhousec00c3102007-04-25 14:16:47 +010050{
51 pp->buf = buf;
52 pp->buflen = buflen;
53 pp->ofs = ofs;
54 pp->reserve = reserve;
55}
56
57static inline int pushbit(struct pushpull *pp, int bit, int use_reserved)
58{
David Woodhouse0bc43822008-12-10 15:34:49 +000059 if (pp->ofs >= pp->buflen - (use_reserved?0:pp->reserve))
David Woodhousec00c3102007-04-25 14:16:47 +010060 return -ENOSPC;
David Woodhousec00c3102007-04-25 14:16:47 +010061
David Woodhouse0bc43822008-12-10 15:34:49 +000062 if (bit)
63 pp->buf[pp->ofs >> 3] |= (1<<(7-(pp->ofs & 7)));
64 else
65 pp->buf[pp->ofs >> 3] &= ~(1<<(7-(pp->ofs & 7)));
66
David Woodhousec00c3102007-04-25 14:16:47 +010067 pp->ofs++;
68
69 return 0;
70}
71
72static inline int pushedbits(struct pushpull *pp)
73{
74 return pp->ofs;
75}
76
77static inline int pullbit(struct pushpull *pp)
78{
79 int bit;
80
81 bit = (pp->buf[pp->ofs >> 3] >> (7-(pp->ofs & 7))) & 1;
82
83 pp->ofs++;
84 return bit;
85}
86
David Woodhousec00c3102007-04-25 14:16:47 +010087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static void init_rubin(struct rubin_state *rs, int div, int *bits)
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000089{
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 int c;
91
92 rs->q = 0;
93 rs->p = (long) (2 * UPPER_BIT_RUBIN);
94 rs->bit_number = (long) 0;
95 rs->bit_divider = div;
David Woodhouse0bc43822008-12-10 15:34:49 +000096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 for (c=0; c<8; c++)
98 rs->bits[c] = bits[c];
99}
100
101
102static int encode(struct rubin_state *rs, long A, long B, int symbol)
103{
104
105 long i0, i1;
106 int ret;
107
David Woodhouse0bc43822008-12-10 15:34:49 +0000108 while ((rs->q >= UPPER_BIT_RUBIN) ||
109 ((rs->p + rs->q) <= UPPER_BIT_RUBIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 rs->bit_number++;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 ret = pushbit(&rs->pp, (rs->q & UPPER_BIT_RUBIN) ? 1 : 0, 0);
113 if (ret)
114 return ret;
115 rs->q &= LOWER_BITS_RUBIN;
116 rs->q <<= 1;
117 rs->p <<= 1;
118 }
119 i0 = A * rs->p / (A + B);
David Woodhouse0bc43822008-12-10 15:34:49 +0000120 if (i0 <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 i0 = 1;
David Woodhouse0bc43822008-12-10 15:34:49 +0000122
123 if (i0 >= rs->p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 i0 = rs->p - 1;
David Woodhouse0bc43822008-12-10 15:34:49 +0000125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 i1 = rs->p - i0;
127
128 if (symbol == 0)
129 rs->p = i0;
130 else {
131 rs->p = i1;
132 rs->q += i0;
133 }
134 return 0;
135}
136
137
138static void end_rubin(struct rubin_state *rs)
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000139{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 int i;
142
143 for (i = 0; i < RUBIN_REG_SIZE; i++) {
144 pushbit(&rs->pp, (UPPER_BIT_RUBIN & rs->q) ? 1 : 0, 1);
145 rs->q &= LOWER_BITS_RUBIN;
146 rs->q <<= 1;
147 }
148}
149
150
151static void init_decode(struct rubin_state *rs, int div, int *bits)
152{
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000153 init_rubin(rs, div, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /* behalve lower */
156 rs->rec_q = 0;
157
David Woodhouse0bc43822008-12-10 15:34:49 +0000158 for (rs->bit_number = 0; rs->bit_number++ < RUBIN_REG_SIZE;
159 rs->rec_q = rs->rec_q * 2 + (long) (pullbit(&rs->pp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 ;
161}
162
David Woodhouse0bc43822008-12-10 15:34:49 +0000163static void __do_decode(struct rubin_state *rs, unsigned long p,
164 unsigned long q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 register unsigned long lower_bits_rubin = LOWER_BITS_RUBIN;
167 unsigned long rec_q;
168 int c, bits = 0;
169
170 /*
171 * First, work out how many bits we need from the input stream.
172 * Note that we have already done the initial check on this
173 * loop prior to calling this function.
174 */
175 do {
176 bits++;
177 q &= lower_bits_rubin;
178 q <<= 1;
179 p <<= 1;
180 } while ((q >= UPPER_BIT_RUBIN) || ((p + q) <= UPPER_BIT_RUBIN));
181
182 rs->p = p;
183 rs->q = q;
184
185 rs->bit_number += bits;
186
187 /*
188 * Now get the bits. We really want this to be "get n bits".
189 */
190 rec_q = rs->rec_q;
191 do {
192 c = pullbit(&rs->pp);
193 rec_q &= lower_bits_rubin;
194 rec_q <<= 1;
195 rec_q += c;
196 } while (--bits);
197 rs->rec_q = rec_q;
198}
199
200static int decode(struct rubin_state *rs, long A, long B)
201{
202 unsigned long p = rs->p, q = rs->q;
203 long i0, threshold;
204 int symbol;
205
206 if (q >= UPPER_BIT_RUBIN || ((p + q) <= UPPER_BIT_RUBIN))
207 __do_decode(rs, p, q);
208
209 i0 = A * rs->p / (A + B);
David Woodhouse0bc43822008-12-10 15:34:49 +0000210 if (i0 <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 i0 = 1;
David Woodhouse0bc43822008-12-10 15:34:49 +0000212
213 if (i0 >= rs->p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 i0 = rs->p - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 threshold = rs->q + i0;
217 symbol = rs->rec_q >= threshold;
218 if (rs->rec_q >= threshold) {
219 rs->q += i0;
220 i0 = rs->p - i0;
221 }
222
223 rs->p = i0;
224
225 return symbol;
226}
227
228
229
230static int out_byte(struct rubin_state *rs, unsigned char byte)
231{
232 int i, ret;
233 struct rubin_state rs_copy;
234 rs_copy = *rs;
235
David Woodhouse0bc43822008-12-10 15:34:49 +0000236 for (i=0; i<8; i++) {
237 ret = encode(rs, rs->bit_divider-rs->bits[i],
238 rs->bits[i], byte & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (ret) {
240 /* Failed. Restore old state */
241 *rs = rs_copy;
242 return ret;
243 }
David Woodhouse0bc43822008-12-10 15:34:49 +0000244 byte >>= 1 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246 return 0;
247}
248
249static int in_byte(struct rubin_state *rs)
250{
251 int i, result = 0, bit_divider = rs->bit_divider;
252
253 for (i = 0; i < 8; i++)
David Woodhouse0bc43822008-12-10 15:34:49 +0000254 result |= decode(rs, bit_divider - rs->bits[i],
255 rs->bits[i]) << i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 return result;
258}
259
260
261
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000262static int rubin_do_compress(int bit_divider, int *bits, unsigned char *data_in,
David Woodhouse0bc43822008-12-10 15:34:49 +0000263 unsigned char *cpage_out, uint32_t *sourcelen,
264 uint32_t *dstlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 {
266 int outpos = 0;
267 int pos=0;
268 struct rubin_state rs;
269
270 init_pushpull(&rs.pp, cpage_out, *dstlen * 8, 0, 32);
271
272 init_rubin(&rs, bit_divider, bits);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 while (pos < (*sourcelen) && !out_byte(&rs, data_in[pos]))
275 pos++;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 end_rubin(&rs);
278
279 if (outpos > pos) {
280 /* We failed */
281 return -1;
282 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000283
284 /* Tell the caller how much we managed to compress,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * and how much space it took */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 outpos = (pushedbits(&rs.pp)+7)/8;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (outpos >= pos)
290 return -1; /* We didn't actually compress */
291 *sourcelen = pos;
292 *dstlen = outpos;
293 return 0;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000294}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295#if 0
296/* _compress returns the compressed size, -1 if bigger */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000297int jffs2_rubinmips_compress(unsigned char *data_in, unsigned char *cpage_out,
Mike Frysinger088bd452010-09-22 22:52:33 -0400298 uint32_t *sourcelen, uint32_t *dstlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
David Woodhouse0bc43822008-12-10 15:34:49 +0000300 return rubin_do_compress(BIT_DIVIDER_MIPS, bits_mips, data_in,
301 cpage_out, sourcelen, dstlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303#endif
Adrian Bunk75c96f82005-05-05 16:16:09 -0700304static int jffs2_dynrubin_compress(unsigned char *data_in,
305 unsigned char *cpage_out,
Mike Frysinger088bd452010-09-22 22:52:33 -0400306 uint32_t *sourcelen, uint32_t *dstlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 int bits[8];
309 unsigned char histo[256];
310 int i;
311 int ret;
312 uint32_t mysrclen, mydstlen;
313
314 mysrclen = *sourcelen;
315 mydstlen = *dstlen - 8;
316
317 if (*dstlen <= 12)
318 return -1;
319
320 memset(histo, 0, 256);
David Woodhouse0bc43822008-12-10 15:34:49 +0000321 for (i=0; i<mysrclen; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 histo[data_in[i]]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 memset(bits, 0, sizeof(int)*8);
324 for (i=0; i<256; i++) {
325 if (i&128)
326 bits[7] += histo[i];
327 if (i&64)
328 bits[6] += histo[i];
329 if (i&32)
330 bits[5] += histo[i];
331 if (i&16)
332 bits[4] += histo[i];
333 if (i&8)
334 bits[3] += histo[i];
335 if (i&4)
336 bits[2] += histo[i];
337 if (i&2)
338 bits[1] += histo[i];
339 if (i&1)
340 bits[0] += histo[i];
341 }
342
343 for (i=0; i<8; i++) {
344 bits[i] = (bits[i] * 256) / mysrclen;
345 if (!bits[i]) bits[i] = 1;
346 if (bits[i] > 255) bits[i] = 255;
347 cpage_out[i] = bits[i];
348 }
349
David Woodhouse0bc43822008-12-10 15:34:49 +0000350 ret = rubin_do_compress(256, bits, data_in, cpage_out+8, &mysrclen,
351 &mydstlen);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000352 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return ret;
354
355 /* Add back the 8 bytes we took for the probabilities */
356 mydstlen += 8;
357
358 if (mysrclen <= mydstlen) {
359 /* We compressed */
360 return -1;
361 }
362
363 *sourcelen = mysrclen;
364 *dstlen = mydstlen;
365 return 0;
366}
367
David Woodhouse0bc43822008-12-10 15:34:49 +0000368static void rubin_do_decompress(int bit_divider, int *bits,
369 unsigned char *cdata_in,
370 unsigned char *page_out, uint32_t srclen,
371 uint32_t destlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 int outpos = 0;
374 struct rubin_state rs;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 init_pushpull(&rs.pp, cdata_in, srclen, 0, 0);
377 init_decode(&rs, bit_divider, bits);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000378
David Woodhouse0bc43822008-12-10 15:34:49 +0000379 while (outpos < destlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 page_out[outpos++] = in_byte(&rs);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000381}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383
Adrian Bunk75c96f82005-05-05 16:16:09 -0700384static int jffs2_rubinmips_decompress(unsigned char *data_in,
385 unsigned char *cpage_out,
Mike Frysinger088bd452010-09-22 22:52:33 -0400386 uint32_t sourcelen, uint32_t dstlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
David Woodhouse0bc43822008-12-10 15:34:49 +0000388 rubin_do_decompress(BIT_DIVIDER_MIPS, bits_mips, data_in,
389 cpage_out, sourcelen, dstlen);
David Woodhouseef53cb02007-07-10 10:01:22 +0100390 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Adrian Bunk75c96f82005-05-05 16:16:09 -0700393static int jffs2_dynrubin_decompress(unsigned char *data_in,
394 unsigned char *cpage_out,
Mike Frysinger088bd452010-09-22 22:52:33 -0400395 uint32_t sourcelen, uint32_t dstlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 int bits[8];
398 int c;
399
400 for (c=0; c<8; c++)
401 bits[c] = data_in[c];
402
David Woodhouse0bc43822008-12-10 15:34:49 +0000403 rubin_do_decompress(256, bits, data_in+8, cpage_out, sourcelen-8,
404 dstlen);
David Woodhouseef53cb02007-07-10 10:01:22 +0100405 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408static struct jffs2_compressor jffs2_rubinmips_comp = {
David Woodhouse0bc43822008-12-10 15:34:49 +0000409 .priority = JFFS2_RUBINMIPS_PRIORITY,
410 .name = "rubinmips",
411 .compr = JFFS2_COMPR_DYNRUBIN,
412 .compress = NULL, /*&jffs2_rubinmips_compress,*/
413 .decompress = &jffs2_rubinmips_decompress,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414#ifdef JFFS2_RUBINMIPS_DISABLED
David Woodhouse0bc43822008-12-10 15:34:49 +0000415 .disabled = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416#else
David Woodhouse0bc43822008-12-10 15:34:49 +0000417 .disabled = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418#endif
419};
420
421int jffs2_rubinmips_init(void)
422{
David Woodhouse0bc43822008-12-10 15:34:49 +0000423 return jffs2_register_compressor(&jffs2_rubinmips_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426void jffs2_rubinmips_exit(void)
427{
David Woodhouse0bc43822008-12-10 15:34:49 +0000428 jffs2_unregister_compressor(&jffs2_rubinmips_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431static struct jffs2_compressor jffs2_dynrubin_comp = {
David Woodhouse0bc43822008-12-10 15:34:49 +0000432 .priority = JFFS2_DYNRUBIN_PRIORITY,
433 .name = "dynrubin",
434 .compr = JFFS2_COMPR_RUBINMIPS,
435 .compress = jffs2_dynrubin_compress,
436 .decompress = &jffs2_dynrubin_decompress,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437#ifdef JFFS2_DYNRUBIN_DISABLED
David Woodhouse0bc43822008-12-10 15:34:49 +0000438 .disabled = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439#else
David Woodhouse0bc43822008-12-10 15:34:49 +0000440 .disabled = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441#endif
442};
443
444int jffs2_dynrubin_init(void)
445{
David Woodhouse0bc43822008-12-10 15:34:49 +0000446 return jffs2_register_compressor(&jffs2_dynrubin_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449void jffs2_dynrubin_exit(void)
450{
David Woodhouse0bc43822008-12-10 15:34:49 +0000451 jffs2_unregister_compressor(&jffs2_dynrubin_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}