• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright © 2001-2007 Red Hat, Inc.
5  * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
6  *
7  * Created by Arjan van de Ven <arjanv@redhat.com>
8  *
9  * For licensing information, see the file 'LICENCE' in this directory.
10  *
11  */
12 
13 #include <linux/string.h>
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include "jffs2.h"
17 #include "compr.h"
18 
19 #define RUBIN_REG_SIZE   16
20 #define UPPER_BIT_RUBIN    (((long) 1)<<(RUBIN_REG_SIZE-1))
21 #define LOWER_BITS_RUBIN   ((((long) 1)<<(RUBIN_REG_SIZE-1))-1)
22 
23 
24 #define BIT_DIVIDER_MIPS 1043
25 static int bits_mips[8] = { 277, 249, 290, 267, 229, 341, 212, 241};
26 
27 struct pushpull {
28 	unsigned char *buf;
29 	unsigned int buflen;
30 	unsigned int ofs;
31 	unsigned int reserve;
32 };
33 
34 struct rubin_state {
35 	unsigned long p;
36 	unsigned long q;
37 	unsigned long rec_q;
38 	long bit_number;
39 	struct pushpull pp;
40 	int bit_divider;
41 	int bits[8];
42 };
43 
init_pushpull(struct pushpull * pp,char * buf,unsigned buflen,unsigned ofs,unsigned reserve)44 static inline void init_pushpull(struct pushpull *pp, char *buf,
45 				 unsigned buflen, unsigned ofs,
46 				 unsigned reserve)
47 {
48 	pp->buf = (unsigned char *)buf;
49 	pp->buflen = buflen;
50 	pp->ofs = ofs;
51 	pp->reserve = reserve;
52 }
53 
pushbit(struct pushpull * pp,int bit,int use_reserved)54 static inline int pushbit(struct pushpull *pp, int bit, int use_reserved)
55 {
56 	if (pp->ofs >= pp->buflen - (use_reserved?0:pp->reserve))
57 		return -ENOSPC;
58 
59 	if (bit)
60 		pp->buf[pp->ofs >> 3] |= (1<<(7-(pp->ofs & 7)));
61 	else
62 		pp->buf[pp->ofs >> 3] &= ~(1<<(7-(pp->ofs & 7)));
63 
64 	pp->ofs++;
65 
66 	return 0;
67 }
68 
pushedbits(struct pushpull * pp)69 static inline int pushedbits(struct pushpull *pp)
70 {
71 	return pp->ofs;
72 }
73 
pullbit(struct pushpull * pp)74 static inline int pullbit(struct pushpull *pp)
75 {
76 	int bit;
77 
78 	bit = (pp->buf[pp->ofs >> 3] >> (7-(pp->ofs & 7))) & 1;
79 
80 	pp->ofs++;
81 	return bit;
82 }
83 
84 
init_rubin(struct rubin_state * rs,int div,int * bits)85 static void init_rubin(struct rubin_state *rs, int div, int *bits)
86 {
87 	int c;
88 
89 	rs->q = 0;
90 	rs->p = (long) (2 * UPPER_BIT_RUBIN);
91 	rs->bit_number = (long) 0;
92 	rs->bit_divider = div;
93 
94 	for (c=0; c<8; c++)
95 		rs->bits[c] = bits[c];
96 }
97 
98 
encode(struct rubin_state * rs,long A,long B,int symbol)99 static int encode(struct rubin_state *rs, long A, long B, int symbol)
100 {
101 
102 	long i0, i1;
103 	int ret;
104 
105 	while ((rs->q >= UPPER_BIT_RUBIN) ||
106 	       ((rs->p + rs->q) <= UPPER_BIT_RUBIN)) {
107 		rs->bit_number++;
108 
109 		ret = pushbit(&rs->pp, (rs->q & UPPER_BIT_RUBIN) ? 1 : 0, 0);
110 		if (ret)
111 			return ret;
112 		rs->q &= LOWER_BITS_RUBIN;
113 		rs->q <<= 1;
114 		rs->p <<= 1;
115 	}
116 	i0 = A * rs->p / (A + B);
117 	if (i0 <= 0)
118 		i0 = 1;
119 
120 	if (i0 >= rs->p)
121 		i0 = rs->p - 1;
122 
123 	i1 = rs->p - i0;
124 
125 	if (symbol == 0)
126 		rs->p = i0;
127 	else {
128 		rs->p = i1;
129 		rs->q += i0;
130 	}
131 	return 0;
132 }
133 
134 
end_rubin(struct rubin_state * rs)135 static void end_rubin(struct rubin_state *rs)
136 {
137 
138 	int i;
139 
140 	for (i = 0; i < RUBIN_REG_SIZE; i++) {
141 		pushbit(&rs->pp, (UPPER_BIT_RUBIN & rs->q) ? 1 : 0, 1);
142 		rs->q &= LOWER_BITS_RUBIN;
143 		rs->q <<= 1;
144 	}
145 }
146 
147 
init_decode(struct rubin_state * rs,int div,int * bits)148 static void init_decode(struct rubin_state *rs, int div, int *bits)
149 {
150 	init_rubin(rs, div, bits);
151 
152 	/* behalve lower */
153 	rs->rec_q = 0;
154 
155 	for (rs->bit_number = 0; rs->bit_number++ < RUBIN_REG_SIZE;
156 	     rs->rec_q = rs->rec_q * 2 + (long) (pullbit(&rs->pp)))
157 		;
158 }
159 
__do_decode(struct rubin_state * rs,unsigned long p,unsigned long q)160 static void __do_decode(struct rubin_state *rs, unsigned long p,
161 			unsigned long q)
162 {
163 	register unsigned long lower_bits_rubin = LOWER_BITS_RUBIN;
164 	unsigned long rec_q;
165 	int c, bits = 0;
166 
167 	/*
168 	 * First, work out how many bits we need from the input stream.
169 	 * Note that we have already done the initial check on this
170 	 * loop prior to calling this function.
171 	 */
172 	do {
173 		bits++;
174 		q &= lower_bits_rubin;
175 		q <<= 1;
176 		p <<= 1;
177 	} while ((q >= UPPER_BIT_RUBIN) || ((p + q) <= UPPER_BIT_RUBIN));
178 
179 	rs->p = p;
180 	rs->q = q;
181 
182 	rs->bit_number += bits;
183 
184 	/*
185 	 * Now get the bits.  We really want this to be "get n bits".
186 	 */
187 	rec_q = rs->rec_q;
188 	do {
189 		c = pullbit(&rs->pp);
190 		rec_q &= lower_bits_rubin;
191 		rec_q <<= 1;
192 		rec_q += c;
193 	} while (--bits);
194 	rs->rec_q = rec_q;
195 }
196 
decode(struct rubin_state * rs,long A,long B)197 static int decode(struct rubin_state *rs, long A, long B)
198 {
199 	unsigned long p = rs->p, q = rs->q;
200 	long i0, threshold;
201 	int symbol;
202 
203 	if (q >= UPPER_BIT_RUBIN || ((p + q) <= UPPER_BIT_RUBIN))
204 		__do_decode(rs, p, q);
205 
206 	i0 = A * rs->p / (A + B);
207 	if (i0 <= 0)
208 		i0 = 1;
209 
210 	if (i0 >= rs->p)
211 		i0 = rs->p - 1;
212 
213 	threshold = rs->q + i0;
214 	symbol = rs->rec_q >= threshold;
215 	if (rs->rec_q >= threshold) {
216 		rs->q += i0;
217 		i0 = rs->p - i0;
218 	}
219 
220 	rs->p = i0;
221 
222 	return symbol;
223 }
224 
225 
226 
out_byte(struct rubin_state * rs,unsigned char byte)227 static int out_byte(struct rubin_state *rs, unsigned char byte)
228 {
229 	int i, ret;
230 	struct rubin_state rs_copy;
231 	rs_copy = *rs;
232 
233 	for (i=0; i<8; i++) {
234 		ret = encode(rs, rs->bit_divider-rs->bits[i],
235 			     rs->bits[i], byte & 1);
236 		if (ret) {
237 			/* Failed. Restore old state */
238 			*rs = rs_copy;
239 			return ret;
240 		}
241 		byte >>= 1 ;
242 	}
243 	return 0;
244 }
245 
in_byte(struct rubin_state * rs)246 static int in_byte(struct rubin_state *rs)
247 {
248 	int i, result = 0, bit_divider = rs->bit_divider;
249 
250 	for (i = 0; i < 8; i++)
251 		result |= decode(rs, bit_divider - rs->bits[i],
252 				 rs->bits[i]) << i;
253 
254 	return result;
255 }
256 
257 
258 
rubin_do_compress(int bit_divider,int * bits,unsigned char * data_in,unsigned char * cpage_out,uint32_t * sourcelen,uint32_t * dstlen)259 static int rubin_do_compress(int bit_divider, int *bits, unsigned char *data_in,
260 			     unsigned char *cpage_out, uint32_t *sourcelen,
261 			     uint32_t *dstlen)
262 	{
263 	int outpos = 0;
264 	int pos=0;
265 	struct rubin_state rs;
266 
267 	init_pushpull(&rs.pp, (char *)cpage_out, *dstlen * 8, 0, 32);
268 
269 	init_rubin(&rs, bit_divider, bits);
270 
271 	while (pos < (*sourcelen) && !out_byte(&rs, data_in[pos]))
272 		pos++;
273 
274 	end_rubin(&rs);
275 
276 	if (outpos > pos) {
277 		/* We failed */
278 		return -1;
279 	}
280 
281 	/* Tell the caller how much we managed to compress,
282 	 * and how much space it took */
283 
284 	outpos = (pushedbits(&rs.pp)+7)/8;
285 
286 	if (outpos >= pos)
287 		return -1; /* We didn't actually compress */
288 	*sourcelen = pos;
289 	*dstlen = outpos;
290 	return 0;
291 }
292 #if 0
293 /* _compress returns the compressed size, -1 if bigger */
294 int jffs2_rubinmips_compress(unsigned char *data_in, unsigned char *cpage_out,
295 		   uint32_t *sourcelen, uint32_t *dstlen)
296 {
297 	return rubin_do_compress(BIT_DIVIDER_MIPS, bits_mips, data_in,
298 				 cpage_out, sourcelen, dstlen);
299 }
300 #endif
jffs2_dynrubin_compress(unsigned char * data_in,unsigned char * cpage_out,uint32_t * sourcelen,uint32_t * dstlen)301 static int jffs2_dynrubin_compress(unsigned char *data_in,
302 				   unsigned char *cpage_out,
303 				   uint32_t *sourcelen, uint32_t *dstlen)
304 {
305 	int bits[8];
306 	unsigned char histo[256];
307 	int i;
308 	int ret;
309 	uint32_t mysrclen, mydstlen;
310 
311 	mysrclen = *sourcelen;
312 	mydstlen = *dstlen - 8;
313 
314 	if (*dstlen <= 12)
315 		return -1;
316 
317 	memset(histo, 0, 256);
318 	for (i=0; i<mysrclen; i++)
319 		histo[data_in[i]]++;
320 	memset(bits, 0, sizeof(int)*8);
321 	for (i=0; i<256; i++) {
322 		if (i&128)
323 			bits[7] += histo[i];
324 		if (i&64)
325 			bits[6] += histo[i];
326 		if (i&32)
327 			bits[5] += histo[i];
328 		if (i&16)
329 			bits[4] += histo[i];
330 		if (i&8)
331 			bits[3] += histo[i];
332 		if (i&4)
333 			bits[2] += histo[i];
334 		if (i&2)
335 			bits[1] += histo[i];
336 		if (i&1)
337 			bits[0] += histo[i];
338 	}
339 
340 	for (i=0; i<8; i++) {
341 		bits[i] = (bits[i] * 256) / mysrclen;
342 		if (!bits[i]) bits[i] = 1;
343 		if (bits[i] > 255) bits[i] = 255;
344 		cpage_out[i] = bits[i];
345 	}
346 
347 	ret = rubin_do_compress(256, bits, data_in, cpage_out+8, &mysrclen,
348 				&mydstlen);
349 	if (ret)
350 		return ret;
351 
352 	/* Add back the 8 bytes we took for the probabilities */
353 	mydstlen += 8;
354 
355 	if (mysrclen <= mydstlen) {
356 		/* We compressed */
357 		return -1;
358 	}
359 
360 	*sourcelen = mysrclen;
361 	*dstlen = mydstlen;
362 	return 0;
363 }
364 
rubin_do_decompress(int bit_divider,int * bits,unsigned char * cdata_in,unsigned char * page_out,uint32_t srclen,uint32_t destlen)365 static void rubin_do_decompress(int bit_divider, int *bits,
366 				unsigned char *cdata_in,
367 				unsigned char *page_out, uint32_t srclen,
368 				uint32_t destlen)
369 {
370 	int outpos = 0;
371 	struct rubin_state rs;
372 
373 	init_pushpull(&rs.pp, (char *)cdata_in, srclen, 0, 0);
374 	init_decode(&rs, bit_divider, bits);
375 
376 	while (outpos < destlen)
377 		page_out[outpos++] = in_byte(&rs);
378 }
379 
380 
jffs2_rubinmips_decompress(unsigned char * data_in,unsigned char * cpage_out,uint32_t sourcelen,uint32_t dstlen)381 static int jffs2_rubinmips_decompress(unsigned char *data_in,
382 				      unsigned char *cpage_out,
383 				      uint32_t sourcelen, uint32_t dstlen)
384 {
385 	rubin_do_decompress(BIT_DIVIDER_MIPS, bits_mips, data_in,
386 			    cpage_out, sourcelen, dstlen);
387 	return 0;
388 }
389 
jffs2_dynrubin_decompress(unsigned char * data_in,unsigned char * cpage_out,uint32_t sourcelen,uint32_t dstlen)390 static int jffs2_dynrubin_decompress(unsigned char *data_in,
391 				     unsigned char *cpage_out,
392 				     uint32_t sourcelen, uint32_t dstlen)
393 {
394 	int bits[8];
395 	int c;
396 
397 	for (c=0; c<8; c++)
398 		bits[c] = data_in[c];
399 
400 	rubin_do_decompress(256, bits, data_in+8, cpage_out, sourcelen-8,
401 			    dstlen);
402 	return 0;
403 }
404 
405 static struct jffs2_compressor jffs2_rubinmips_comp = {
406 	.priority = JFFS2_RUBINMIPS_PRIORITY,
407 	.name = "rubinmips",
408 	.compr = JFFS2_COMPR_DYNRUBIN,
409 	.compress = NULL, /*&jffs2_rubinmips_compress,*/
410 	.decompress = &jffs2_rubinmips_decompress,
411 #ifdef JFFS2_RUBINMIPS_DISABLED
412 	.disabled = 1,
413 #else
414 	.disabled = 0,
415 #endif
416 };
417 
jffs2_rubinmips_init(void)418 int jffs2_rubinmips_init(void)
419 {
420 	return jffs2_register_compressor(&jffs2_rubinmips_comp);
421 }
422 
jffs2_rubinmips_exit(void)423 void jffs2_rubinmips_exit(void)
424 {
425 	jffs2_unregister_compressor(&jffs2_rubinmips_comp);
426 }
427 
428 static struct jffs2_compressor jffs2_dynrubin_comp = {
429 	.priority = JFFS2_DYNRUBIN_PRIORITY,
430 	.name = "dynrubin",
431 	.compr = JFFS2_COMPR_RUBINMIPS,
432 	.compress = jffs2_dynrubin_compress,
433 	.decompress = &jffs2_dynrubin_decompress,
434 #ifdef JFFS2_DYNRUBIN_DISABLED
435 	.disabled = 1,
436 #else
437 	.disabled = 0,
438 #endif
439 };
440 
jffs2_dynrubin_init(void)441 int jffs2_dynrubin_init(void)
442 {
443 	return jffs2_register_compressor(&jffs2_dynrubin_comp);
444 }
445 
jffs2_dynrubin_exit(void)446 void jffs2_dynrubin_exit(void)
447 {
448 	jffs2_unregister_compressor(&jffs2_dynrubin_comp);
449 }
450