mini_inflate.c 11 KB

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