compr_rubin.c 7.5 KB

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