tinflate.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * tinflate - tiny inflate
  3. *
  4. * Copyright (c) 2003 by Joergen Ibsen / Jibz
  5. * All Rights Reserved
  6. * http://www.ibsensoftware.com/
  7. *
  8. * Copyright (c) 2014-2016 by Paul Sokolovsky
  9. *
  10. * This software is provided 'as-is', without any express
  11. * or implied warranty. In no event will the authors be
  12. * held liable for any damages arising from the use of
  13. * this software.
  14. *
  15. * Permission is granted to anyone to use this software
  16. * for any purpose, including commercial applications,
  17. * and to alter it and redistribute it freely, subject to
  18. * the following restrictions:
  19. *
  20. * 1. The origin of this software must not be
  21. * misrepresented; you must not claim that you
  22. * wrote the original software. If you use this
  23. * software in a product, an acknowledgment in
  24. * the product documentation would be appreciated
  25. * but is not required.
  26. *
  27. * 2. Altered source versions must be plainly marked
  28. * as such, and must not be misrepresented as
  29. * being the original software.
  30. *
  31. * 3. This notice may not be removed or altered from
  32. * any source distribution.
  33. */
  34. #include "tinf.h"
  35. uint32_t tinf_get_le_uint32(TINF_DATA *d);
  36. uint32_t tinf_get_be_uint32(TINF_DATA *d);
  37. /* --------------------------------------------------- *
  38. * -- uninitialized global data (static structures) -- *
  39. * --------------------------------------------------- */
  40. #ifdef RUNTIME_BITS_TABLES
  41. /* extra bits and base tables for length codes */
  42. unsigned char length_bits[30];
  43. unsigned short length_base[30];
  44. /* extra bits and base tables for distance codes */
  45. unsigned char dist_bits[30];
  46. unsigned short dist_base[30];
  47. #else
  48. const unsigned char length_bits[30] = {
  49. 0, 0, 0, 0, 0, 0, 0, 0,
  50. 1, 1, 1, 1, 2, 2, 2, 2,
  51. 3, 3, 3, 3, 4, 4, 4, 4,
  52. 5, 5, 5, 5
  53. };
  54. const unsigned short length_base[30] = {
  55. 3, 4, 5, 6, 7, 8, 9, 10,
  56. 11, 13, 15, 17, 19, 23, 27, 31,
  57. 35, 43, 51, 59, 67, 83, 99, 115,
  58. 131, 163, 195, 227, 258
  59. };
  60. const unsigned char dist_bits[30] = {
  61. 0, 0, 0, 0, 1, 1, 2, 2,
  62. 3, 3, 4, 4, 5, 5, 6, 6,
  63. 7, 7, 8, 8, 9, 9, 10, 10,
  64. 11, 11, 12, 12, 13, 13
  65. };
  66. const unsigned short dist_base[30] = {
  67. 1, 2, 3, 4, 5, 7, 9, 13,
  68. 17, 25, 33, 49, 65, 97, 129, 193,
  69. 257, 385, 513, 769, 1025, 1537, 2049, 3073,
  70. 4097, 6145, 8193, 12289, 16385, 24577
  71. };
  72. #endif
  73. /* special ordering of code length codes */
  74. const unsigned char clcidx[] = {
  75. 16, 17, 18, 0, 8, 7, 9, 6,
  76. 10, 5, 11, 4, 12, 3, 13, 2,
  77. 14, 1, 15
  78. };
  79. /* ----------------------- *
  80. * -- utility functions -- *
  81. * ----------------------- */
  82. #ifdef RUNTIME_BITS_TABLES
  83. /* build extra bits and base tables */
  84. static void tinf_build_bits_base(unsigned char *bits, unsigned short *base, int delta, int first)
  85. {
  86. int i, sum;
  87. /* build bits table */
  88. for (i = 0; i < delta; ++i) bits[i] = 0;
  89. for (i = 0; i < 30 - delta; ++i) bits[i + delta] = i / delta;
  90. /* build base table */
  91. for (sum = first, i = 0; i < 30; ++i)
  92. {
  93. base[i] = sum;
  94. sum += 1 << bits[i];
  95. }
  96. }
  97. #endif
  98. /* build the fixed huffman trees */
  99. static void tinf_build_fixed_trees(volatile TINF_TREE *lt, volatile TINF_TREE *dt)
  100. {
  101. int i;
  102. /* build fixed length tree */
  103. for (i = 0; i < 7; ++i) lt->table[i] = 0;
  104. lt->table[7] = 24;
  105. lt->table[8] = 152;
  106. lt->table[9] = 112;
  107. for (i = 0; i < 24; ++i) lt->trans[i] = 256 + i;
  108. for (i = 0; i < 144; ++i) lt->trans[24 + i] = i;
  109. for (i = 0; i < 8; ++i) lt->trans[24 + 144 + i] = 280 + i;
  110. for (i = 0; i < 112; ++i) lt->trans[24 + 144 + 8 + i] = 144 + i;
  111. /* build fixed distance tree */
  112. for (i = 0; i < 5; ++i) dt->table[i] = 0;
  113. dt->table[5] = 32;
  114. for (i = 0; i < 32; ++i) dt->trans[i] = i;
  115. }
  116. /* given an array of code lengths, build a tree */
  117. static void tinf_build_tree(volatile TINF_TREE *t, const unsigned char *lengths, unsigned int num)
  118. {
  119. unsigned short offs[16];
  120. unsigned int i, sum;
  121. /* clear code length count table */
  122. for (i = 0; i < 16; ++i) t->table[i] = 0;
  123. /* scan symbol lengths, and sum code length counts */
  124. for (i = 0; i < num; ++i) t->table[lengths[i]]++;
  125. t->table[0] = 0;
  126. /* compute offset table for distribution sort */
  127. for (sum = 0, i = 0; i < 16; ++i)
  128. {
  129. offs[i] = sum;
  130. sum += t->table[i];
  131. }
  132. /* create code->symbol translation table (symbols sorted by code) */
  133. for (i = 0; i < num; ++i)
  134. {
  135. if (lengths[i]) t->trans[offs[lengths[i]]++] = i;
  136. }
  137. }
  138. /* ---------------------- *
  139. * -- decode functions -- *
  140. * ---------------------- */
  141. unsigned char uzlib_get_byte(volatile TINF_DATA *d)
  142. {
  143. if (d->source) {
  144. return *d->source++;
  145. }
  146. return d->readSource(d);
  147. }
  148. uint32_t tinf_get_le_uint32(TINF_DATA *d)
  149. {
  150. uint32_t val = 0;
  151. int i;
  152. for (i = 4; i--;) {
  153. val = val >> 8 | uzlib_get_byte(d) << 24;
  154. }
  155. return val;
  156. }
  157. uint32_t tinf_get_be_uint32(TINF_DATA *d)
  158. {
  159. uint32_t val = 0;
  160. int i;
  161. for (i = 4; i--;) {
  162. val = val << 8 | uzlib_get_byte(d);
  163. }
  164. return val;
  165. }
  166. /* get one bit from source stream */
  167. static int tinf_getbit(volatile TINF_DATA *d)
  168. {
  169. unsigned int bit;
  170. /* check if tag is empty */
  171. if (!d->bitcount--)
  172. {
  173. /* load next tag */
  174. d->tag = uzlib_get_byte(d);
  175. d->bitcount = 7;
  176. }
  177. /* shift bit out of tag */
  178. bit = d->tag & 0x01;
  179. d->tag >>= 1;
  180. return bit;
  181. }
  182. /* read a num bit value from a stream and add base */
  183. static unsigned int tinf_read_bits(volatile TINF_DATA *d, int num, int base)
  184. {
  185. unsigned int val = 0;
  186. /* read num bits */
  187. if (num)
  188. {
  189. unsigned int limit = 1 << (num);
  190. unsigned int mask;
  191. for (mask = 1; mask < limit; mask *= 2)
  192. if (tinf_getbit(d)) val += mask;
  193. }
  194. return val + base;
  195. }
  196. /* given a data stream and a tree, decode a symbol */
  197. static int tinf_decode_symbol(volatile TINF_DATA *d, volatile TINF_TREE *t)
  198. {
  199. int sum = 0, cur = 0, len = 0;
  200. /* get more bits while code value is above sum */
  201. do {
  202. cur = 2*cur + tinf_getbit(d);
  203. ++len;
  204. sum += t->table[len];
  205. cur -= t->table[len];
  206. } while (cur >= 0);
  207. return t->trans[sum + cur];
  208. }
  209. /* given a data stream, decode dynamic trees from it */
  210. static void tinf_decode_trees(volatile TINF_DATA *d, volatile TINF_TREE *lt, volatile TINF_TREE *dt)
  211. {
  212. unsigned char lengths[288+32];
  213. unsigned int hlit, hdist, hclen;
  214. unsigned int i, num, length;
  215. /* get 5 bits HLIT (257-286) */
  216. hlit = tinf_read_bits(d, 5, 257);
  217. /* get 5 bits HDIST (1-32) */
  218. hdist = tinf_read_bits(d, 5, 1);
  219. /* get 4 bits HCLEN (4-19) */
  220. hclen = tinf_read_bits(d, 4, 4);
  221. for (i = 0; i < 19; ++i) lengths[i] = 0;
  222. /* read code lengths for code length alphabet */
  223. for (i = 0; i < hclen; ++i)
  224. {
  225. /* get 3 bits code length (0-7) */
  226. unsigned int clen = tinf_read_bits(d, 3, 0);
  227. lengths[clcidx[i]] = clen;
  228. }
  229. /* build code length tree, temporarily use length tree */
  230. tinf_build_tree(lt, lengths, 19);
  231. /* decode code lengths for the dynamic trees */
  232. for (num = 0; num < hlit + hdist; )
  233. {
  234. int sym = tinf_decode_symbol(d, lt);
  235. switch (sym)
  236. {
  237. case 16:
  238. /* copy previous code length 3-6 times (read 2 bits) */
  239. {
  240. unsigned char prev = lengths[num - 1];
  241. for (length = tinf_read_bits(d, 2, 3); length; --length)
  242. {
  243. lengths[num++] = prev;
  244. }
  245. }
  246. break;
  247. case 17:
  248. /* repeat code length 0 for 3-10 times (read 3 bits) */
  249. for (length = tinf_read_bits(d, 3, 3); length; --length)
  250. {
  251. lengths[num++] = 0;
  252. }
  253. break;
  254. case 18:
  255. /* repeat code length 0 for 11-138 times (read 7 bits) */
  256. for (length = tinf_read_bits(d, 7, 11); length; --length)
  257. {
  258. lengths[num++] = 0;
  259. }
  260. break;
  261. default:
  262. /* values 0-15 represent the actual code lengths */
  263. lengths[num++] = sym;
  264. break;
  265. }
  266. }
  267. /* build dynamic trees */
  268. tinf_build_tree(lt, lengths, hlit);
  269. tinf_build_tree(dt, lengths + hlit, hdist);
  270. }
  271. /* ----------------------------- *
  272. * -- block inflate functions -- *
  273. * ----------------------------- */
  274. /* given a stream and two trees, inflate a block of data */
  275. static int tinf_inflate_block_data(volatile TINF_DATA *d, volatile TINF_TREE *lt, volatile TINF_TREE *dt)
  276. {
  277. if (d->curlen == 0) {
  278. unsigned int offs;
  279. int dist;
  280. int sym = tinf_decode_symbol(d, lt);
  281. //printf("huff sym: %02x\n", sym);
  282. /* literal byte */
  283. if (sym < 256) {
  284. TINF_PUT(d, sym);
  285. return TINF_OK;
  286. }
  287. /* end of block */
  288. if (sym == 256) {
  289. return TINF_DONE;
  290. }
  291. /* substring from sliding dictionary */
  292. sym -= 257;
  293. /* possibly get more bits from length code */
  294. d->curlen = tinf_read_bits(d, length_bits[sym], length_base[sym]);
  295. dist = tinf_decode_symbol(d, dt);
  296. /* possibly get more bits from distance code */
  297. offs = tinf_read_bits(d, dist_bits[dist], dist_base[dist]);
  298. d->lzOff = -offs;
  299. }
  300. /* copy next byte from dict substring */
  301. d->dest[0] = d->dest[d->lzOff];
  302. d->dest++;
  303. d->curlen--;
  304. return TINF_OK;
  305. }
  306. /* inflate an uncompressed block of data */
  307. static int tinf_inflate_uncompressed_block(volatile TINF_DATA *d)
  308. {
  309. if (d->curlen == 0) {
  310. unsigned int length, invlength;
  311. /* get length */
  312. length = uzlib_get_byte(d) + 256 * uzlib_get_byte(d);
  313. /* get one's complement of length */
  314. invlength = uzlib_get_byte(d) + 256 * uzlib_get_byte(d);
  315. /* check length */
  316. if (length != (~invlength & 0x0000ffff)) return TINF_DATA_ERROR;
  317. /* increment length to properly return TINF_DONE below, without
  318. producing data at the same time */
  319. d->curlen = length + 1;
  320. /* make sure we start next block on a byte boundary */
  321. d->bitcount = 0;
  322. }
  323. if (--d->curlen == 0) {
  324. return TINF_DONE;
  325. }
  326. unsigned char c = uzlib_get_byte(d);
  327. TINF_PUT(d, c);
  328. return TINF_OK;
  329. }
  330. /* ---------------------- *
  331. * -- public functions -- *
  332. * ---------------------- */
  333. /* initialize global (static) data */
  334. void uzlib_init(void)
  335. {
  336. #ifdef RUNTIME_BITS_TABLES
  337. /* build extra bits and base tables */
  338. tinf_build_bits_base(length_bits, length_base, 4, 3);
  339. tinf_build_bits_base(dist_bits, dist_base, 2, 1);
  340. /* fix a special case */
  341. length_bits[28] = 0;
  342. length_base[28] = 258;
  343. #endif
  344. }
  345. /* inflate next byte of compressed stream */
  346. int uzlib_uncompress(volatile TINF_DATA *d)
  347. {
  348. do {
  349. int res;
  350. /* start a new block */
  351. if (d->btype == -1) {
  352. next_blk:
  353. /* read final block flag */
  354. d->bfinal = tinf_getbit(d);
  355. /* read block type (2 bits) */
  356. d->btype = tinf_read_bits(d, 2, 0);
  357. //printf("Started new block: type=%d final=%d\n", d->btype, d->bfinal);
  358. if (d->btype == 1) {
  359. /* build fixed huffman trees */
  360. tinf_build_fixed_trees(&d->ltree, &d->dtree);
  361. } else if (d->btype == 2) {
  362. /* decode trees from stream */
  363. tinf_decode_trees(d, &d->ltree, &d->dtree);
  364. }
  365. }
  366. /* process current block */
  367. switch (d->btype)
  368. {
  369. case 0:
  370. /* decompress uncompressed block */
  371. res = tinf_inflate_uncompressed_block(d);
  372. break;
  373. case 1:
  374. case 2:
  375. /* decompress block with fixed/dyanamic huffman trees */
  376. /* trees were decoded previously, so it's the same routine for both */
  377. res = tinf_inflate_block_data(d, &d->ltree, &d->dtree);
  378. break;
  379. default:
  380. return TINF_DATA_ERROR;
  381. }
  382. if (res == TINF_DONE && !d->bfinal) {
  383. /* the block has ended (without producing more data), but we
  384. can't return without data, so start procesing next block */
  385. goto next_blk;
  386. }
  387. if (res != TINF_OK) {
  388. return res;
  389. }
  390. } while (--d->destSize);
  391. return TINF_OK;
  392. }