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