mini_inflate.c 11 KB

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