compr_rubin.c 8.7 KB

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