15
#include <linux/string.h>
16
#include <linux/types.h>
18
#include "compr_rubin.h"
19
#include "histo_mips.h"
22
static void init_rubin(struct rubin_state *rs, int div, int *bits) {
26
rs->p = (long) (2 * UPPER_BIT_RUBIN);
27
rs->bit_number = (long) 0;
28
rs->bit_divider = div;
30
rs->bits[c] = bits[c];
35
static int encode(struct rubin_state *rs, long A, long B, int symbol) {
40
while ((rs->q >= UPPER_BIT_RUBIN) ||
41
((rs->p + rs->q) <= UPPER_BIT_RUBIN)) {
44
ret = pushbit(&rs->pp, (rs->q & UPPER_BIT_RUBIN) ? 1 : 0, 0);
48
rs->q &= LOWER_BITS_RUBIN;
52
i0 = A * rs->p / (A + B);
71
static void end_rubin(struct rubin_state *rs) {
75
for (i = 0; i < RUBIN_REG_SIZE; i++) {
76
pushbit(&rs->pp, (UPPER_BIT_RUBIN & rs->q) ? 1 : 0, 1);
77
rs->q &= LOWER_BITS_RUBIN;
83
static void init_decode(struct rubin_state *rs, int div, int *bits) {
84
init_rubin(rs, div, bits);
89
for (rs->bit_number = 0; rs->bit_number++ < RUBIN_REG_SIZE;
90
rs->rec_q = rs->rec_q * 2 + (long) (pullbit(&rs->pp))) {
95
static void __do_decode(struct rubin_state *rs,
96
unsigned long p, unsigned long q) {
97
register unsigned long lower_bits_rubin = LOWER_BITS_RUBIN;
108
q &= lower_bits_rubin;
111
} while ((q >= UPPER_BIT_RUBIN) || ((p + q) <= UPPER_BIT_RUBIN));
116
rs->bit_number += bits;
123
c = pullbit(&rs->pp);
124
rec_q &= lower_bits_rubin;
131
static int decode(struct rubin_state *rs, long A, long B) {
132
unsigned long p = rs->p, q = rs->q;
136
if (q >= UPPER_BIT_RUBIN || ((p + q) <= UPPER_BIT_RUBIN)) {
137
__do_decode(rs, p, q);
140
i0 = A * rs->p / (A + B);
148
threshold = rs->q + i0;
149
symbol = rs->rec_q >= threshold;
150
if (rs->rec_q >= threshold) {
162
static int out_byte(struct rubin_state *rs, unsigned char byte) {
164
struct rubin_state rs_copy;
168
ret = encode(rs, rs->bit_divider-rs->bits[i],rs->bits[i],byte&1);
179
static int in_byte(struct rubin_state *rs) {
180
int i, result = 0, bit_divider = rs->bit_divider;
182
for (i = 0; i < 8; i++) {
183
result |= decode(rs, bit_divider - rs->bits[i], rs->bits[i]) << i;
191
static int rubin_do_compress(int bit_divider, int *bits,
192
unsigned char *data_in,unsigned char *cpage_out,
193
uint32_t *sourcelen, uint32_t *dstlen) {
196
struct rubin_state rs;
198
init_pushpull(&rs.pp, (char *) cpage_out, *dstlen * 8, 0, 32);
200
init_rubin(&rs, bit_divider, bits);
202
while (pos < (*sourcelen) && !out_byte(&rs, data_in[pos])) {
216
outpos = (pushedbits(&rs.pp)+7)/8;
226
static int jffs2_dynrubin_compress(unsigned char *data_in,
227
unsigned char *cpage_out,
228
uint32_t *sourcelen, uint32_t *dstlen,
231
unsigned char histo[256];
234
uint32_t mysrclen, mydstlen;
236
mysrclen = *sourcelen;
237
mydstlen = *dstlen - 8;
243
memset(histo, 0, 256);
244
for (i=0; i<mysrclen; i++) {
247
memset(bits, 0, sizeof(int)*8);
248
for (i=0; i<256; i++) {
275
for (i=0; i<8; i++) {
276
bits[i] = (bits[i] * 256) / mysrclen;
283
cpage_out[i] = bits[i];
286
ret = rubin_do_compress(256, bits,
287
data_in, cpage_out+8, &mysrclen, &mydstlen);
295
if (mysrclen <= mydstlen) {
300
*sourcelen = mysrclen;
305
static void rubin_do_decompress(int bit_divider, int *bits,
306
unsigned char *cdata_in, unsigned char *page_out,
307
uint32_t srclen, uint32_t destlen) {
309
struct rubin_state rs;
311
init_pushpull(&rs.pp, (char *) cdata_in, srclen, 0, 0);
312
init_decode(&rs, bit_divider, bits);
314
while (outpos < destlen) {
315
page_out[outpos++] = in_byte(&rs);
320
static int jffs2_rubinmips_decompress(unsigned char *data_in,
321
unsigned char *cpage_out,
322
uint32_t sourcelen, uint32_t dstlen,
324
rubin_do_decompress(BIT_DIVIDER_MIPS,
325
bits_mips, data_in, cpage_out, sourcelen, dstlen);
329
static int jffs2_dynrubin_decompress(unsigned char *data_in,
330
unsigned char *cpage_out,
331
uint32_t sourcelen, uint32_t dstlen,
336
for (c=0; c<8; c++) {
337
bits[c] = data_in[c];
340
rubin_do_decompress(256, bits,
341
data_in+8, cpage_out, sourcelen-8, dstlen);
345
static struct jffs2_compressor jffs2_rubinmips_comp = {
346
.priority = JFFS2_RUBINMIPS_PRIORITY,
348
.compr = JFFS2_COMPR_DYNRUBIN,
350
.decompress = &jffs2_rubinmips_decompress,
351
#ifdef JFFS2_RUBINMIPS_DISABLED
358
int jffs2_rubinmips_init(void) {
359
return jffs2_register_compressor(&jffs2_rubinmips_comp);
362
void jffs2_rubinmips_exit(void) {
363
jffs2_unregister_compressor(&jffs2_rubinmips_comp);
366
static struct jffs2_compressor jffs2_dynrubin_comp = {
367
.priority = JFFS2_DYNRUBIN_PRIORITY,
369
.compr = JFFS2_COMPR_RUBINMIPS,
370
.compress = jffs2_dynrubin_compress,
371
.decompress = &jffs2_dynrubin_decompress,
372
#ifdef JFFS2_DYNRUBIN_DISABLED
379
int jffs2_dynrubin_init(void) {
380
return jffs2_register_compressor(&jffs2_dynrubin_comp);
383
void jffs2_dynrubin_exit(void) {
384
jffs2_unregister_compressor(&jffs2_dynrubin_comp);