compr_rubin.c 8.7 KB

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