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