mini_inflate.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*-------------------------------------------------------------------------
  2. * Filename: mini_inflate.c
  3. * Version: $Id: mini_inflate.c,v 1.3 2002/01/24 22:58:42 rfeany Exp $
  4. * Copyright: Copyright (C) 2001, Russ Dill
  5. * Author: Russ Dill <Russ.Dill@asu.edu>
  6. * Description: Mini inflate implementation (RFC 1951)
  7. *-----------------------------------------------------------------------*/
  8. /*
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <config.h>
  26. #if defined(CONFIG_CMD_JFFS2)
  27. #include <jffs2/mini_inflate.h>
  28. /* The order that the code lengths in section 3.2.7 are in */
  29. static unsigned char huffman_order[] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5,
  30. 11, 4, 12, 3, 13, 2, 14, 1, 15};
  31. inline void cramfs_memset(int *s, const int c, size n)
  32. {
  33. n--;
  34. for (;n > 0; n--) s[n] = c;
  35. s[0] = c;
  36. }
  37. /* associate a stream with a block of data and reset the stream */
  38. static void init_stream(struct bitstream *stream, unsigned char *data,
  39. void *(*inflate_memcpy)(void *, const void *, size))
  40. {
  41. stream->error = NO_ERROR;
  42. stream->memcpy = inflate_memcpy;
  43. stream->decoded = 0;
  44. stream->data = data;
  45. stream->bit = 0; /* The first bit of the stream is the lsb of the
  46. * first byte */
  47. /* really sorry about all this initialization, think of a better way,
  48. * let me know and it will get cleaned up */
  49. stream->codes.bits = 8;
  50. stream->codes.num_symbols = 19;
  51. stream->codes.lengths = stream->code_lengths;
  52. stream->codes.symbols = stream->code_symbols;
  53. stream->codes.count = stream->code_count;
  54. stream->codes.first = stream->code_first;
  55. stream->codes.pos = stream->code_pos;
  56. stream->lengths.bits = 16;
  57. stream->lengths.num_symbols = 288;
  58. stream->lengths.lengths = stream->length_lengths;
  59. stream->lengths.symbols = stream->length_symbols;
  60. stream->lengths.count = stream->length_count;
  61. stream->lengths.first = stream->length_first;
  62. stream->lengths.pos = stream->length_pos;
  63. stream->distance.bits = 16;
  64. stream->distance.num_symbols = 32;
  65. stream->distance.lengths = stream->distance_lengths;
  66. stream->distance.symbols = stream->distance_symbols;
  67. stream->distance.count = stream->distance_count;
  68. stream->distance.first = stream->distance_first;
  69. stream->distance.pos = stream->distance_pos;
  70. }
  71. /* pull 'bits' bits out of the stream. The last bit pulled it returned as the
  72. * msb. (section 3.1.1)
  73. */
  74. inline unsigned long pull_bits(struct bitstream *stream,
  75. const unsigned int bits)
  76. {
  77. unsigned long ret;
  78. int i;
  79. ret = 0;
  80. for (i = 0; i < bits; i++) {
  81. ret += ((*(stream->data) >> stream->bit) & 1) << i;
  82. /* if, before incrementing, we are on bit 7,
  83. * go to the lsb of the next byte */
  84. if (stream->bit++ == 7) {
  85. stream->bit = 0;
  86. stream->data++;
  87. }
  88. }
  89. return ret;
  90. }
  91. inline int pull_bit(struct bitstream *stream)
  92. {
  93. int ret = ((*(stream->data) >> stream->bit) & 1);
  94. if (stream->bit++ == 7) {
  95. stream->bit = 0;
  96. stream->data++;
  97. }
  98. return ret;
  99. }
  100. /* discard bits up to the next whole byte */
  101. static void discard_bits(struct bitstream *stream)
  102. {
  103. if (stream->bit != 0) {
  104. stream->bit = 0;
  105. stream->data++;
  106. }
  107. }
  108. /* No decompression, the data is all literals (section 3.2.4) */
  109. static void decompress_none(struct bitstream *stream, unsigned char *dest)
  110. {
  111. unsigned int length;
  112. discard_bits(stream);
  113. length = *(stream->data++);
  114. length += *(stream->data++) << 8;
  115. pull_bits(stream, 16); /* throw away the inverse of the size */
  116. stream->decoded += length;
  117. stream->memcpy(dest, stream->data, length);
  118. stream->data += length;
  119. }
  120. /* Read in a symbol from the stream (section 3.2.2) */
  121. static int read_symbol(struct bitstream *stream, struct huffman_set *set)
  122. {
  123. int bits = 0;
  124. int code = 0;
  125. while (!(set->count[bits] && code < set->first[bits] +
  126. set->count[bits])) {
  127. code = (code << 1) + pull_bit(stream);
  128. if (++bits > set->bits) {
  129. /* error decoding (corrupted data?) */
  130. stream->error = CODE_NOT_FOUND;
  131. return -1;
  132. }
  133. }
  134. return set->symbols[set->pos[bits] + code - set->first[bits]];
  135. }
  136. /* decompress a stream of data encoded with the passed length and distance
  137. * huffman codes */
  138. static void decompress_huffman(struct bitstream *stream, unsigned char *dest)
  139. {
  140. struct huffman_set *lengths = &(stream->lengths);
  141. struct huffman_set *distance = &(stream->distance);
  142. int symbol, length, dist, i;
  143. do {
  144. if ((symbol = read_symbol(stream, lengths)) < 0) return;
  145. if (symbol < 256) {
  146. *(dest++) = symbol; /* symbol is a literal */
  147. stream->decoded++;
  148. } else if (symbol > 256) {
  149. /* Determine the length of the repitition
  150. * (section 3.2.5) */
  151. if (symbol < 265) length = symbol - 254;
  152. else if (symbol == 285) length = 258;
  153. else {
  154. length = pull_bits(stream, (symbol - 261) >> 2);
  155. length += (4 << ((symbol - 261) >> 2)) + 3;
  156. length += ((symbol - 1) % 4) <<
  157. ((symbol - 261) >> 2);
  158. }
  159. /* Determine how far back to go */
  160. if ((symbol = read_symbol(stream, distance)) < 0)
  161. return;
  162. if (symbol < 4) dist = symbol + 1;
  163. else {
  164. dist = pull_bits(stream, (symbol - 2) >> 1);
  165. dist += (2 << ((symbol - 2) >> 1)) + 1;
  166. dist += (symbol % 2) << ((symbol - 2) >> 1);
  167. }
  168. stream->decoded += length;
  169. for (i = 0; i < length; i++) {
  170. *dest = dest[-dist];
  171. dest++;
  172. }
  173. }
  174. } while (symbol != 256); /* 256 is the end of the data block */
  175. }
  176. /* Fill the lookup tables (section 3.2.2) */
  177. static void fill_code_tables(struct huffman_set *set)
  178. {
  179. int code = 0, i, length;
  180. /* fill in the first code of each bit length, and the pos pointer */
  181. set->pos[0] = 0;
  182. for (i = 1; i < set->bits; i++) {
  183. code = (code + set->count[i - 1]) << 1;
  184. set->first[i] = code;
  185. set->pos[i] = set->pos[i - 1] + set->count[i - 1];
  186. }
  187. /* Fill in the table of symbols in order of their huffman code */
  188. for (i = 0; i < set->num_symbols; i++) {
  189. if ((length = set->lengths[i]))
  190. set->symbols[set->pos[length]++] = i;
  191. }
  192. /* reset the pos pointer */
  193. for (i = 1; i < set->bits; i++) set->pos[i] -= set->count[i];
  194. }
  195. static void init_code_tables(struct huffman_set *set)
  196. {
  197. cramfs_memset(set->lengths, 0, set->num_symbols);
  198. cramfs_memset(set->count, 0, set->bits);
  199. cramfs_memset(set->first, 0, set->bits);
  200. }
  201. /* read in the huffman codes for dynamic decoding (section 3.2.7) */
  202. static void decompress_dynamic(struct bitstream *stream, unsigned char *dest)
  203. {
  204. /* I tried my best to minimize the memory footprint here, while still
  205. * keeping up performance. I really dislike the _lengths[] tables, but
  206. * I see no way of eliminating them without a sizable performance
  207. * impact. The first struct table keeps track of stats on each bit
  208. * length. The _length table keeps a record of the bit length of each
  209. * symbol. The _symbols table is for looking up symbols by the huffman
  210. * code (the pos element points to the first place in the symbol table
  211. * where that bit length occurs). I also hate the initization of these
  212. * structs, if someone knows how to compact these, lemme know. */
  213. struct huffman_set *codes = &(stream->codes);
  214. struct huffman_set *lengths = &(stream->lengths);
  215. struct huffman_set *distance = &(stream->distance);
  216. int hlit = pull_bits(stream, 5) + 257;
  217. int hdist = pull_bits(stream, 5) + 1;
  218. int hclen = pull_bits(stream, 4) + 4;
  219. int length, curr_code, symbol, i, last_code;
  220. last_code = 0;
  221. init_code_tables(codes);
  222. init_code_tables(lengths);
  223. init_code_tables(distance);
  224. /* fill in the count of each bit length' as well as the lengths
  225. * table */
  226. for (i = 0; i < hclen; i++) {
  227. length = pull_bits(stream, 3);
  228. codes->lengths[huffman_order[i]] = length;
  229. if (length) codes->count[length]++;
  230. }
  231. fill_code_tables(codes);
  232. /* Do the same for the length codes, being carefull of wrap through
  233. * to the distance table */
  234. curr_code = 0;
  235. while (curr_code < hlit) {
  236. if ((symbol = read_symbol(stream, codes)) < 0) return;
  237. if (symbol == 0) {
  238. curr_code++;
  239. last_code = 0;
  240. } else if (symbol < 16) { /* Literal length */
  241. lengths->lengths[curr_code] = last_code = symbol;
  242. lengths->count[symbol]++;
  243. curr_code++;
  244. } else if (symbol == 16) { /* repeat the last symbol 3 - 6
  245. * times */
  246. length = 3 + pull_bits(stream, 2);
  247. for (;length; length--, curr_code++)
  248. if (curr_code < hlit) {
  249. lengths->lengths[curr_code] =
  250. last_code;
  251. lengths->count[last_code]++;
  252. } else { /* wrap to the distance table */
  253. distance->lengths[curr_code - hlit] =
  254. last_code;
  255. distance->count[last_code]++;
  256. }
  257. } else if (symbol == 17) { /* repeat a bit length 0 */
  258. curr_code += 3 + pull_bits(stream, 3);
  259. last_code = 0;
  260. } else { /* same, but more times */
  261. curr_code += 11 + pull_bits(stream, 7);
  262. last_code = 0;
  263. }
  264. }
  265. fill_code_tables(lengths);
  266. /* Fill the distance table, don't need to worry about wrapthrough
  267. * here */
  268. curr_code -= hlit;
  269. while (curr_code < hdist) {
  270. if ((symbol = read_symbol(stream, codes)) < 0) return;
  271. if (symbol == 0) {
  272. curr_code++;
  273. last_code = 0;
  274. } else if (symbol < 16) {
  275. distance->lengths[curr_code] = last_code = symbol;
  276. distance->count[symbol]++;
  277. curr_code++;
  278. } else if (symbol == 16) {
  279. length = 3 + pull_bits(stream, 2);
  280. for (;length; length--, curr_code++) {
  281. distance->lengths[curr_code] =
  282. last_code;
  283. distance->count[last_code]++;
  284. }
  285. } else if (symbol == 17) {
  286. curr_code += 3 + pull_bits(stream, 3);
  287. last_code = 0;
  288. } else {
  289. curr_code += 11 + pull_bits(stream, 7);
  290. last_code = 0;
  291. }
  292. }
  293. fill_code_tables(distance);
  294. decompress_huffman(stream, dest);
  295. }
  296. /* fill in the length and distance huffman codes for fixed encoding
  297. * (section 3.2.6) */
  298. static void decompress_fixed(struct bitstream *stream, unsigned char *dest)
  299. {
  300. /* let gcc fill in the initial values */
  301. struct huffman_set *lengths = &(stream->lengths);
  302. struct huffman_set *distance = &(stream->distance);
  303. cramfs_memset(lengths->count, 0, 16);
  304. cramfs_memset(lengths->first, 0, 16);
  305. cramfs_memset(lengths->lengths, 8, 144);
  306. cramfs_memset(lengths->lengths + 144, 9, 112);
  307. cramfs_memset(lengths->lengths + 256, 7, 24);
  308. cramfs_memset(lengths->lengths + 280, 8, 8);
  309. lengths->count[7] = 24;
  310. lengths->count[8] = 152;
  311. lengths->count[9] = 112;
  312. cramfs_memset(distance->count, 0, 16);
  313. cramfs_memset(distance->first, 0, 16);
  314. cramfs_memset(distance->lengths, 5, 32);
  315. distance->count[5] = 32;
  316. fill_code_tables(lengths);
  317. fill_code_tables(distance);
  318. decompress_huffman(stream, dest);
  319. }
  320. /* returns the number of bytes decoded, < 0 if there was an error. Note that
  321. * this function assumes that the block starts on a byte boundry
  322. * (non-compliant, but I don't see where this would happen). section 3.2.3 */
  323. long decompress_block(unsigned char *dest, unsigned char *source,
  324. void *(*inflate_memcpy)(void *, const void *, size))
  325. {
  326. int bfinal, btype;
  327. struct bitstream stream;
  328. init_stream(&stream, source, inflate_memcpy);
  329. do {
  330. bfinal = pull_bit(&stream);
  331. btype = pull_bits(&stream, 2);
  332. if (btype == NO_COMP) decompress_none(&stream, dest + stream.decoded);
  333. else if (btype == DYNAMIC_COMP)
  334. decompress_dynamic(&stream, dest + stream.decoded);
  335. else if (btype == FIXED_COMP) decompress_fixed(&stream, dest + stream.decoded);
  336. else stream.error = COMP_UNKNOWN;
  337. } while (!bfinal && !stream.error);
  338. #if 0
  339. putstr("decompress_block start\r\n");
  340. putLabeledWord("stream.error = ",stream.error);
  341. putLabeledWord("stream.decoded = ",stream.decoded);
  342. putLabeledWord("dest = ",dest);
  343. putstr("decompress_block end\r\n");
  344. #endif
  345. return stream.error ? -stream.error : stream.decoded;
  346. }
  347. #endif