| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * JFFS2 -- Journalling Flash File System, Version 2. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2007-04-25 14:16:47 +01:00
										 |  |  |  * Copyright © 2001-2007 Red Hat, Inc. | 
					
						
							| 
									
										
										
										
											2010-08-08 14:15:22 +01:00
										 |  |  |  * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org> | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Created by Arjan van de Ven <arjanv@redhat.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * For licensing information, see the file 'LICENCE' in this directory. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-15 15:56:45 -08:00
										 |  |  | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | #include <linux/string.h>
 | 
					
						
							|  |  |  | #include <linux/types.h>
 | 
					
						
							|  |  |  | #include <linux/jffs2.h>
 | 
					
						
							| 
									
										
										
										
											2007-04-25 14:16:47 +01:00
										 |  |  | #include <linux/errno.h>
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | #include "compr.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-25 14:16:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define RUBIN_REG_SIZE   16
 | 
					
						
							|  |  |  | #define UPPER_BIT_RUBIN    (((long) 1)<<(RUBIN_REG_SIZE-1))
 | 
					
						
							|  |  |  | #define LOWER_BITS_RUBIN   ((((long) 1)<<(RUBIN_REG_SIZE-1))-1)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define BIT_DIVIDER_MIPS 1043
 | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | static int bits_mips[8] = { 277, 249, 290, 267, 229, 341, 212, 241}; | 
					
						
							| 
									
										
										
										
											2007-04-25 14:16:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct pushpull { | 
					
						
							|  |  |  | 	unsigned char *buf; | 
					
						
							|  |  |  | 	unsigned int buflen; | 
					
						
							|  |  |  | 	unsigned int ofs; | 
					
						
							|  |  |  | 	unsigned int reserve; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-26 07:27:04 +01:00
										 |  |  | struct rubin_state { | 
					
						
							|  |  |  | 	unsigned long p; | 
					
						
							|  |  |  | 	unsigned long q; | 
					
						
							|  |  |  | 	unsigned long rec_q; | 
					
						
							|  |  |  | 	long bit_number; | 
					
						
							|  |  |  | 	struct pushpull pp; | 
					
						
							|  |  |  | 	int bit_divider; | 
					
						
							|  |  |  | 	int bits[8]; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2007-04-25 14:16:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | static inline void init_pushpull(struct pushpull *pp, char *buf, | 
					
						
							|  |  |  | 				 unsigned buflen, unsigned ofs, | 
					
						
							|  |  |  | 				 unsigned reserve) | 
					
						
							| 
									
										
										
										
											2007-04-25 14:16:47 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	pp->buf = buf; | 
					
						
							|  |  |  | 	pp->buflen = buflen; | 
					
						
							|  |  |  | 	pp->ofs = ofs; | 
					
						
							|  |  |  | 	pp->reserve = reserve; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline int pushbit(struct pushpull *pp, int bit, int use_reserved) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	if (pp->ofs >= pp->buflen - (use_reserved?0:pp->reserve)) | 
					
						
							| 
									
										
										
										
											2007-04-25 14:16:47 +01:00
										 |  |  | 		return -ENOSPC; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	if (bit) | 
					
						
							|  |  |  | 		pp->buf[pp->ofs >> 3] |= (1<<(7-(pp->ofs & 7))); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		pp->buf[pp->ofs >> 3] &= ~(1<<(7-(pp->ofs & 7))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-25 14:16:47 +01:00
										 |  |  | 	pp->ofs++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline int pushedbits(struct pushpull *pp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return pp->ofs; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline int pullbit(struct pushpull *pp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int bit; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bit = (pp->buf[pp->ofs >> 3] >> (7-(pp->ofs & 7))) & 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pp->ofs++; | 
					
						
							|  |  |  | 	return bit; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline int pulledbits(struct pushpull *pp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return pp->ofs; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | static void init_rubin(struct rubin_state *rs, int div, int *bits) | 
					
						
							| 
									
										
										
										
											2005-11-07 11:16:07 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	int c; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rs->q = 0; | 
					
						
							|  |  |  | 	rs->p = (long) (2 * UPPER_BIT_RUBIN); | 
					
						
							|  |  |  | 	rs->bit_number = (long) 0; | 
					
						
							|  |  |  | 	rs->bit_divider = div; | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	for (c=0; c<8; c++) | 
					
						
							|  |  |  | 		rs->bits[c] = bits[c]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int encode(struct rubin_state *rs, long A, long B, int symbol) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	long i0, i1; | 
					
						
							|  |  |  | 	int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	while ((rs->q >= UPPER_BIT_RUBIN) || | 
					
						
							|  |  |  | 	       ((rs->p + rs->q) <= UPPER_BIT_RUBIN)) { | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		rs->bit_number++; | 
					
						
							| 
									
										
										
										
											2005-11-07 11:16:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		ret = pushbit(&rs->pp, (rs->q & UPPER_BIT_RUBIN) ? 1 : 0, 0); | 
					
						
							|  |  |  | 		if (ret) | 
					
						
							|  |  |  | 			return ret; | 
					
						
							|  |  |  | 		rs->q &= LOWER_BITS_RUBIN; | 
					
						
							|  |  |  | 		rs->q <<= 1; | 
					
						
							|  |  |  | 		rs->p <<= 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	i0 = A * rs->p / (A + B); | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	if (i0 <= 0) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		i0 = 1; | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (i0 >= rs->p) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		i0 = rs->p - 1; | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	i1 = rs->p - i0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (symbol == 0) | 
					
						
							|  |  |  | 		rs->p = i0; | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		rs->p = i1; | 
					
						
							|  |  |  | 		rs->q += i0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void end_rubin(struct rubin_state *rs) | 
					
						
							| 
									
										
										
										
											2005-11-07 11:16:07 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < RUBIN_REG_SIZE; i++) { | 
					
						
							|  |  |  | 		pushbit(&rs->pp, (UPPER_BIT_RUBIN & rs->q) ? 1 : 0, 1); | 
					
						
							|  |  |  | 		rs->q &= LOWER_BITS_RUBIN; | 
					
						
							|  |  |  | 		rs->q <<= 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void init_decode(struct rubin_state *rs, int div, int *bits) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2005-11-07 11:16:07 +00:00
										 |  |  | 	init_rubin(rs, div, bits); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* behalve lower */ | 
					
						
							|  |  |  | 	rs->rec_q = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	for (rs->bit_number = 0; rs->bit_number++ < RUBIN_REG_SIZE; | 
					
						
							|  |  |  | 	     rs->rec_q = rs->rec_q * 2 + (long) (pullbit(&rs->pp))) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | static void __do_decode(struct rubin_state *rs, unsigned long p, | 
					
						
							|  |  |  | 			unsigned long q) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	register unsigned long lower_bits_rubin = LOWER_BITS_RUBIN; | 
					
						
							|  |  |  | 	unsigned long rec_q; | 
					
						
							|  |  |  | 	int c, bits = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * First, work out how many bits we need from the input stream. | 
					
						
							|  |  |  | 	 * Note that we have already done the initial check on this | 
					
						
							|  |  |  | 	 * loop prior to calling this function. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	do { | 
					
						
							|  |  |  | 		bits++; | 
					
						
							|  |  |  | 		q &= lower_bits_rubin; | 
					
						
							|  |  |  | 		q <<= 1; | 
					
						
							|  |  |  | 		p <<= 1; | 
					
						
							|  |  |  | 	} while ((q >= UPPER_BIT_RUBIN) || ((p + q) <= UPPER_BIT_RUBIN)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rs->p = p; | 
					
						
							|  |  |  | 	rs->q = q; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rs->bit_number += bits; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * Now get the bits.  We really want this to be "get n bits". | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	rec_q = rs->rec_q; | 
					
						
							|  |  |  | 	do { | 
					
						
							|  |  |  | 		c = pullbit(&rs->pp); | 
					
						
							|  |  |  | 		rec_q &= lower_bits_rubin; | 
					
						
							|  |  |  | 		rec_q <<= 1; | 
					
						
							|  |  |  | 		rec_q += c; | 
					
						
							|  |  |  | 	} while (--bits); | 
					
						
							|  |  |  | 	rs->rec_q = rec_q; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int decode(struct rubin_state *rs, long A, long B) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	unsigned long p = rs->p, q = rs->q; | 
					
						
							|  |  |  | 	long i0, threshold; | 
					
						
							|  |  |  | 	int symbol; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (q >= UPPER_BIT_RUBIN || ((p + q) <= UPPER_BIT_RUBIN)) | 
					
						
							|  |  |  | 		__do_decode(rs, p, q); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	i0 = A * rs->p / (A + B); | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	if (i0 <= 0) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		i0 = 1; | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (i0 >= rs->p) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		i0 = rs->p - 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	threshold = rs->q + i0; | 
					
						
							|  |  |  | 	symbol = rs->rec_q >= threshold; | 
					
						
							|  |  |  | 	if (rs->rec_q >= threshold) { | 
					
						
							|  |  |  | 		rs->q += i0; | 
					
						
							|  |  |  | 		i0 = rs->p - i0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rs->p = i0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return symbol; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int out_byte(struct rubin_state *rs, unsigned char byte) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i, ret; | 
					
						
							|  |  |  | 	struct rubin_state rs_copy; | 
					
						
							|  |  |  | 	rs_copy = *rs; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	for (i=0; i<8; i++) { | 
					
						
							|  |  |  | 		ret = encode(rs, rs->bit_divider-rs->bits[i], | 
					
						
							|  |  |  | 			     rs->bits[i], byte & 1); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		if (ret) { | 
					
						
							|  |  |  | 			/* Failed. Restore old state */ | 
					
						
							|  |  |  | 			*rs = rs_copy; | 
					
						
							|  |  |  | 			return ret; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 		byte >>= 1 ; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int in_byte(struct rubin_state *rs) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i, result = 0, bit_divider = rs->bit_divider; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < 8; i++) | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 		result |= decode(rs, bit_divider - rs->bits[i], | 
					
						
							|  |  |  | 				 rs->bits[i]) << i; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-07 11:16:07 +00:00
										 |  |  | static int rubin_do_compress(int bit_divider, int *bits, unsigned char *data_in, | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 			     unsigned char *cpage_out, uint32_t *sourcelen, | 
					
						
							|  |  |  | 			     uint32_t *dstlen) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 	int outpos = 0; | 
					
						
							|  |  |  | 	int pos=0; | 
					
						
							|  |  |  | 	struct rubin_state rs; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	init_pushpull(&rs.pp, cpage_out, *dstlen * 8, 0, 32); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	init_rubin(&rs, bit_divider, bits); | 
					
						
							| 
									
										
										
										
											2005-11-07 11:16:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	while (pos < (*sourcelen) && !out_byte(&rs, data_in[pos])) | 
					
						
							|  |  |  | 		pos++; | 
					
						
							| 
									
										
										
										
											2005-11-07 11:16:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	end_rubin(&rs); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (outpos > pos) { | 
					
						
							|  |  |  | 		/* We failed */ | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-11-07 11:16:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Tell the caller how much we managed to compress,
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	 * and how much space it took */ | 
					
						
							| 
									
										
										
										
											2005-11-07 11:16:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	outpos = (pushedbits(&rs.pp)+7)/8; | 
					
						
							| 
									
										
										
										
											2005-11-07 11:16:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	if (outpos >= pos) | 
					
						
							|  |  |  | 		return -1; /* We didn't actually compress */ | 
					
						
							|  |  |  | 	*sourcelen = pos; | 
					
						
							|  |  |  | 	*dstlen = outpos; | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2005-11-07 11:16:07 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | #if 0
 | 
					
						
							|  |  |  | /* _compress returns the compressed size, -1 if bigger */ | 
					
						
							| 
									
										
										
										
											2005-11-07 11:16:07 +00:00
										 |  |  | int jffs2_rubinmips_compress(unsigned char *data_in, unsigned char *cpage_out, | 
					
						
							| 
									
										
										
										
											2010-09-22 22:52:33 -04:00
										 |  |  | 		   uint32_t *sourcelen, uint32_t *dstlen) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	return rubin_do_compress(BIT_DIVIDER_MIPS, bits_mips, data_in, | 
					
						
							|  |  |  | 				 cpage_out, sourcelen, dstlen); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2005-05-05 16:16:09 -07:00
										 |  |  | static int jffs2_dynrubin_compress(unsigned char *data_in, | 
					
						
							|  |  |  | 				   unsigned char *cpage_out, | 
					
						
							| 
									
										
										
										
											2010-09-22 22:52:33 -04:00
										 |  |  | 				   uint32_t *sourcelen, uint32_t *dstlen) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	int bits[8]; | 
					
						
							|  |  |  | 	unsigned char histo[256]; | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 	int ret; | 
					
						
							|  |  |  | 	uint32_t mysrclen, mydstlen; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mysrclen = *sourcelen; | 
					
						
							|  |  |  | 	mydstlen = *dstlen - 8; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (*dstlen <= 12) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	memset(histo, 0, 256); | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	for (i=0; i<mysrclen; i++) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		histo[data_in[i]]++; | 
					
						
							|  |  |  | 	memset(bits, 0, sizeof(int)*8); | 
					
						
							|  |  |  | 	for (i=0; i<256; i++) { | 
					
						
							|  |  |  | 		if (i&128) | 
					
						
							|  |  |  | 			bits[7] += histo[i]; | 
					
						
							|  |  |  | 		if (i&64) | 
					
						
							|  |  |  | 			bits[6] += histo[i]; | 
					
						
							|  |  |  | 		if (i&32) | 
					
						
							|  |  |  | 			bits[5] += histo[i]; | 
					
						
							|  |  |  | 		if (i&16) | 
					
						
							|  |  |  | 			bits[4] += histo[i]; | 
					
						
							|  |  |  | 		if (i&8) | 
					
						
							|  |  |  | 			bits[3] += histo[i]; | 
					
						
							|  |  |  | 		if (i&4) | 
					
						
							|  |  |  | 			bits[2] += histo[i]; | 
					
						
							|  |  |  | 		if (i&2) | 
					
						
							|  |  |  | 			bits[1] += histo[i]; | 
					
						
							|  |  |  | 		if (i&1) | 
					
						
							|  |  |  | 			bits[0] += histo[i]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i=0; i<8; i++) { | 
					
						
							|  |  |  | 		bits[i] = (bits[i] * 256) / mysrclen; | 
					
						
							|  |  |  | 		if (!bits[i]) bits[i] = 1; | 
					
						
							|  |  |  | 		if (bits[i] > 255) bits[i] = 255; | 
					
						
							|  |  |  | 		cpage_out[i] = bits[i]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	ret = rubin_do_compress(256, bits, data_in, cpage_out+8, &mysrclen, | 
					
						
							|  |  |  | 				&mydstlen); | 
					
						
							| 
									
										
										
										
											2005-11-07 11:16:07 +00:00
										 |  |  | 	if (ret) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		return ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Add back the 8 bytes we took for the probabilities */ | 
					
						
							|  |  |  | 	mydstlen += 8; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (mysrclen <= mydstlen) { | 
					
						
							|  |  |  | 		/* We compressed */ | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	*sourcelen = mysrclen; | 
					
						
							|  |  |  | 	*dstlen = mydstlen; | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | static void rubin_do_decompress(int bit_divider, int *bits, | 
					
						
							|  |  |  | 				unsigned char *cdata_in,  | 
					
						
							|  |  |  | 				unsigned char *page_out, uint32_t srclen, | 
					
						
							|  |  |  | 				uint32_t destlen) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	int outpos = 0; | 
					
						
							|  |  |  | 	struct rubin_state rs; | 
					
						
							| 
									
										
										
										
											2005-11-07 11:16:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	init_pushpull(&rs.pp, cdata_in, srclen, 0, 0); | 
					
						
							|  |  |  | 	init_decode(&rs, bit_divider, bits); | 
					
						
							| 
									
										
										
										
											2005-11-07 11:16:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	while (outpos < destlen) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		page_out[outpos++] = in_byte(&rs); | 
					
						
							| 
									
										
										
										
											2005-11-07 11:16:07 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-05-05 16:16:09 -07:00
										 |  |  | static int jffs2_rubinmips_decompress(unsigned char *data_in, | 
					
						
							|  |  |  | 				      unsigned char *cpage_out, | 
					
						
							| 
									
										
										
										
											2010-09-22 22:52:33 -04:00
										 |  |  | 				      uint32_t sourcelen, uint32_t dstlen) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	rubin_do_decompress(BIT_DIVIDER_MIPS, bits_mips, data_in, | 
					
						
							|  |  |  | 			    cpage_out, sourcelen, dstlen); | 
					
						
							| 
									
										
										
										
											2007-07-10 10:01:22 +01:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-05-05 16:16:09 -07:00
										 |  |  | static int jffs2_dynrubin_decompress(unsigned char *data_in, | 
					
						
							|  |  |  | 				     unsigned char *cpage_out, | 
					
						
							| 
									
										
										
										
											2010-09-22 22:52:33 -04:00
										 |  |  | 				     uint32_t sourcelen, uint32_t dstlen) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	int bits[8]; | 
					
						
							|  |  |  | 	int c; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (c=0; c<8; c++) | 
					
						
							|  |  |  | 		bits[c] = data_in[c]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	rubin_do_decompress(256, bits, data_in+8, cpage_out, sourcelen-8, | 
					
						
							|  |  |  | 			    dstlen); | 
					
						
							| 
									
										
										
										
											2007-07-10 10:01:22 +01:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct jffs2_compressor jffs2_rubinmips_comp = { | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	.priority = JFFS2_RUBINMIPS_PRIORITY, | 
					
						
							|  |  |  | 	.name = "rubinmips", | 
					
						
							|  |  |  | 	.compr = JFFS2_COMPR_DYNRUBIN, | 
					
						
							|  |  |  | 	.compress = NULL, /*&jffs2_rubinmips_compress,*/ | 
					
						
							|  |  |  | 	.decompress = &jffs2_rubinmips_decompress, | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | #ifdef JFFS2_RUBINMIPS_DISABLED
 | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	.disabled = 1, | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	.disabled = 0, | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int jffs2_rubinmips_init(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	return jffs2_register_compressor(&jffs2_rubinmips_comp); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void jffs2_rubinmips_exit(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	jffs2_unregister_compressor(&jffs2_rubinmips_comp); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct jffs2_compressor jffs2_dynrubin_comp = { | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	.priority = JFFS2_DYNRUBIN_PRIORITY, | 
					
						
							|  |  |  | 	.name = "dynrubin", | 
					
						
							|  |  |  | 	.compr = JFFS2_COMPR_RUBINMIPS, | 
					
						
							|  |  |  | 	.compress = jffs2_dynrubin_compress, | 
					
						
							|  |  |  | 	.decompress = &jffs2_dynrubin_decompress, | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | #ifdef JFFS2_DYNRUBIN_DISABLED
 | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	.disabled = 1, | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	.disabled = 0, | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int jffs2_dynrubin_init(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	return jffs2_register_compressor(&jffs2_dynrubin_comp); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void jffs2_dynrubin_exit(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-10 15:34:49 +00:00
										 |  |  | 	jffs2_unregister_compressor(&jffs2_dynrubin_comp); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | } |