compr_rubin.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. int jffs2_dynrubin_compress(unsigned char *data_in, unsigned char *cpage_out,
  185. uint32_t *sourcelen, uint32_t *dstlen, void *model)
  186. {
  187. int bits[8];
  188. unsigned char histo[256];
  189. int i;
  190. int ret;
  191. uint32_t mysrclen, mydstlen;
  192. mysrclen = *sourcelen;
  193. mydstlen = *dstlen - 8;
  194. if (*dstlen <= 12)
  195. return -1;
  196. memset(histo, 0, 256);
  197. for (i=0; i<mysrclen; i++) {
  198. histo[data_in[i]]++;
  199. }
  200. memset(bits, 0, sizeof(int)*8);
  201. for (i=0; i<256; i++) {
  202. if (i&128)
  203. bits[7] += histo[i];
  204. if (i&64)
  205. bits[6] += histo[i];
  206. if (i&32)
  207. bits[5] += histo[i];
  208. if (i&16)
  209. bits[4] += histo[i];
  210. if (i&8)
  211. bits[3] += histo[i];
  212. if (i&4)
  213. bits[2] += histo[i];
  214. if (i&2)
  215. bits[1] += histo[i];
  216. if (i&1)
  217. bits[0] += histo[i];
  218. }
  219. for (i=0; i<8; i++) {
  220. bits[i] = (bits[i] * 256) / mysrclen;
  221. if (!bits[i]) bits[i] = 1;
  222. if (bits[i] > 255) bits[i] = 255;
  223. cpage_out[i] = bits[i];
  224. }
  225. ret = rubin_do_compress(256, bits, data_in, cpage_out+8, &mysrclen, &mydstlen);
  226. if (ret)
  227. return ret;
  228. /* Add back the 8 bytes we took for the probabilities */
  229. mydstlen += 8;
  230. if (mysrclen <= mydstlen) {
  231. /* We compressed */
  232. return -1;
  233. }
  234. *sourcelen = mysrclen;
  235. *dstlen = mydstlen;
  236. return 0;
  237. }
  238. static void rubin_do_decompress(int bit_divider, int *bits, unsigned char *cdata_in,
  239. unsigned char *page_out, uint32_t srclen, uint32_t destlen)
  240. {
  241. int outpos = 0;
  242. struct rubin_state rs;
  243. init_pushpull(&rs.pp, cdata_in, srclen, 0, 0);
  244. init_decode(&rs, bit_divider, bits);
  245. while (outpos < destlen) {
  246. page_out[outpos++] = in_byte(&rs);
  247. }
  248. }
  249. int jffs2_rubinmips_decompress(unsigned char *data_in, unsigned char *cpage_out,
  250. uint32_t sourcelen, uint32_t dstlen, void *model)
  251. {
  252. rubin_do_decompress(BIT_DIVIDER_MIPS, bits_mips, data_in, cpage_out, sourcelen, dstlen);
  253. return 0;
  254. }
  255. int jffs2_dynrubin_decompress(unsigned char *data_in, unsigned char *cpage_out,
  256. uint32_t sourcelen, uint32_t dstlen, void *model)
  257. {
  258. int bits[8];
  259. int c;
  260. for (c=0; c<8; c++)
  261. bits[c] = data_in[c];
  262. rubin_do_decompress(256, bits, data_in+8, cpage_out, sourcelen-8, dstlen);
  263. return 0;
  264. }
  265. static struct jffs2_compressor jffs2_rubinmips_comp = {
  266. .priority = JFFS2_RUBINMIPS_PRIORITY,
  267. .name = "rubinmips",
  268. .compr = JFFS2_COMPR_DYNRUBIN,
  269. .compress = NULL, /*&jffs2_rubinmips_compress,*/
  270. .decompress = &jffs2_rubinmips_decompress,
  271. #ifdef JFFS2_RUBINMIPS_DISABLED
  272. .disabled = 1,
  273. #else
  274. .disabled = 0,
  275. #endif
  276. };
  277. int jffs2_rubinmips_init(void)
  278. {
  279. return jffs2_register_compressor(&jffs2_rubinmips_comp);
  280. }
  281. void jffs2_rubinmips_exit(void)
  282. {
  283. jffs2_unregister_compressor(&jffs2_rubinmips_comp);
  284. }
  285. static struct jffs2_compressor jffs2_dynrubin_comp = {
  286. .priority = JFFS2_DYNRUBIN_PRIORITY,
  287. .name = "dynrubin",
  288. .compr = JFFS2_COMPR_RUBINMIPS,
  289. .compress = jffs2_dynrubin_compress,
  290. .decompress = &jffs2_dynrubin_decompress,
  291. #ifdef JFFS2_DYNRUBIN_DISABLED
  292. .disabled = 1,
  293. #else
  294. .disabled = 0,
  295. #endif
  296. };
  297. int jffs2_dynrubin_init(void)
  298. {
  299. return jffs2_register_compressor(&jffs2_dynrubin_comp);
  300. }
  301. void jffs2_dynrubin_exit(void)
  302. {
  303. jffs2_unregister_compressor(&jffs2_dynrubin_comp);
  304. }