tinflate.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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 <assert.h>
  35. #include "tinf.h"
  36. uint32_t tinf_get_le_uint32(TINF_DATA *d);
  37. uint32_t tinf_get_be_uint32(TINF_DATA *d);
  38. /* --------------------------------------------------- *
  39. * -- uninitialized global data (static structures) -- *
  40. * --------------------------------------------------- */
  41. #ifdef RUNTIME_BITS_TABLES
  42. /* extra bits and base tables for length codes */
  43. unsigned char length_bits[30];
  44. unsigned short length_base[30];
  45. /* extra bits and base tables for distance codes */
  46. unsigned char dist_bits[30];
  47. unsigned short dist_base[30];
  48. #else
  49. const unsigned char length_bits[30] = {
  50. 0, 0, 0, 0, 0, 0, 0, 0,
  51. 1, 1, 1, 1, 2, 2, 2, 2,
  52. 3, 3, 3, 3, 4, 4, 4, 4,
  53. 5, 5, 5, 5
  54. };
  55. const unsigned short length_base[30] = {
  56. 3, 4, 5, 6, 7, 8, 9, 10,
  57. 11, 13, 15, 17, 19, 23, 27, 31,
  58. 35, 43, 51, 59, 67, 83, 99, 115,
  59. 131, 163, 195, 227, 258
  60. };
  61. const unsigned char dist_bits[30] = {
  62. 0, 0, 0, 0, 1, 1, 2, 2,
  63. 3, 3, 4, 4, 5, 5, 6, 6,
  64. 7, 7, 8, 8, 9, 9, 10, 10,
  65. 11, 11, 12, 12, 13, 13
  66. };
  67. const unsigned short dist_base[30] = {
  68. 1, 2, 3, 4, 5, 7, 9, 13,
  69. 17, 25, 33, 49, 65, 97, 129, 193,
  70. 257, 385, 513, 769, 1025, 1537, 2049, 3073,
  71. 4097, 6145, 8193, 12289, 16385, 24577
  72. };
  73. #endif
  74. /* special ordering of code length codes */
  75. const unsigned char clcidx[] = {
  76. 16, 17, 18, 0, 8, 7, 9, 6,
  77. 10, 5, 11, 4, 12, 3, 13, 2,
  78. 14, 1, 15
  79. };
  80. /* ----------------------- *
  81. * -- utility functions -- *
  82. * ----------------------- */
  83. #ifdef RUNTIME_BITS_TABLES
  84. /* build extra bits and base tables */
  85. static void tinf_build_bits_base(unsigned char *bits, unsigned short *base, int delta, int first)
  86. {
  87. int i, sum;
  88. /* build bits table */
  89. for (i = 0; i < delta; ++i) bits[i] = 0;
  90. for (i = 0; i < 30 - delta; ++i) bits[i + delta] = i / delta;
  91. /* build base table */
  92. for (sum = first, i = 0; i < 30; ++i)
  93. {
  94. base[i] = sum;
  95. sum += 1 << bits[i];
  96. }
  97. }
  98. #endif
  99. /* build the fixed huffman trees */
  100. static void tinf_build_fixed_trees(TINF_TREE *lt, TINF_TREE *dt)
  101. {
  102. int i;
  103. /* build fixed length tree */
  104. for (i = 0; i < 7; ++i) lt->table[i] = 0;
  105. lt->table[7] = 24;
  106. lt->table[8] = 152;
  107. lt->table[9] = 112;
  108. for (i = 0; i < 24; ++i) lt->trans[i] = 256 + i;
  109. for (i = 0; i < 144; ++i) lt->trans[24 + i] = i;
  110. for (i = 0; i < 8; ++i) lt->trans[24 + 144 + i] = 280 + i;
  111. for (i = 0; i < 112; ++i) lt->trans[24 + 144 + 8 + i] = 144 + i;
  112. /* build fixed distance tree */
  113. for (i = 0; i < 5; ++i) dt->table[i] = 0;
  114. dt->table[5] = 32;
  115. for (i = 0; i < 32; ++i) dt->trans[i] = i;
  116. }
  117. /* given an array of code lengths, build a tree */
  118. static void tinf_build_tree(TINF_TREE *t, const unsigned char *lengths, unsigned int num)
  119. {
  120. unsigned short offs[16];
  121. unsigned int i, sum;
  122. /* clear code length count table */
  123. for (i = 0; i < 16; ++i) t->table[i] = 0;
  124. /* scan symbol lengths, and sum code length counts */
  125. for (i = 0; i < num; ++i) t->table[lengths[i]]++;
  126. t->table[0] = 0;
  127. /* compute offset table for distribution sort */
  128. for (sum = 0, i = 0; i < 16; ++i)
  129. {
  130. offs[i] = sum;
  131. sum += t->table[i];
  132. }
  133. /* create code->symbol translation table (symbols sorted by code) */
  134. for (i = 0; i < num; ++i)
  135. {
  136. if (lengths[i]) t->trans[offs[lengths[i]]++] = i;
  137. }
  138. }
  139. /* ---------------------- *
  140. * -- decode functions -- *
  141. * ---------------------- */
  142. unsigned char uzlib_get_byte(TINF_DATA *d)
  143. {
  144. if (d->source) {
  145. return *d->source++;
  146. }
  147. return d->readSource(d);
  148. }
  149. uint32_t tinf_get_le_uint32(TINF_DATA *d)
  150. {
  151. uint32_t val = 0;
  152. int i;
  153. for (i = 4; i--;) {
  154. val = val >> 8 | uzlib_get_byte(d) << 24;
  155. }
  156. return val;
  157. }
  158. uint32_t tinf_get_be_uint32(TINF_DATA *d)
  159. {
  160. uint32_t val = 0;
  161. int i;
  162. for (i = 4; i--;) {
  163. val = val << 8 | uzlib_get_byte(d);
  164. }
  165. return val;
  166. }
  167. /* get one bit from source stream */
  168. static int tinf_getbit(TINF_DATA *d)
  169. {
  170. unsigned int bit;
  171. /* check if tag is empty */
  172. if (!d->bitcount--)
  173. {
  174. /* load next tag */
  175. d->tag = uzlib_get_byte(d);
  176. d->bitcount = 7;
  177. }
  178. /* shift bit out of tag */
  179. bit = d->tag & 0x01;
  180. d->tag >>= 1;
  181. return bit;
  182. }
  183. /* read a num bit value from a stream and add base */
  184. static unsigned int tinf_read_bits(TINF_DATA *d, int num, int base)
  185. {
  186. unsigned int val = 0;
  187. /* read num bits */
  188. if (num)
  189. {
  190. unsigned int limit = 1 << (num);
  191. unsigned int mask;
  192. for (mask = 1; mask < limit; mask *= 2)
  193. if (tinf_getbit(d)) val += mask;
  194. }
  195. return val + base;
  196. }
  197. /* given a data stream and a tree, decode a symbol */
  198. static int tinf_decode_symbol(TINF_DATA *d, TINF_TREE *t)
  199. {
  200. int sum = 0, cur = 0, len = 0;
  201. /* get more bits while code value is above sum */
  202. do {
  203. cur = 2*cur + tinf_getbit(d);
  204. ++len;
  205. sum += t->table[len];
  206. cur -= t->table[len];
  207. } while (cur >= 0);
  208. return t->trans[sum + cur];
  209. }
  210. /* given a data stream, decode dynamic trees from it */
  211. static void tinf_decode_trees(TINF_DATA *d, TINF_TREE *lt, TINF_TREE *dt)
  212. {
  213. unsigned char lengths[288+32];
  214. unsigned int hlit, hdist, hclen;
  215. unsigned int i, num, length;
  216. /* get 5 bits HLIT (257-286) */
  217. hlit = tinf_read_bits(d, 5, 257);
  218. /* get 5 bits HDIST (1-32) */
  219. hdist = tinf_read_bits(d, 5, 1);
  220. /* get 4 bits HCLEN (4-19) */
  221. hclen = tinf_read_bits(d, 4, 4);
  222. for (i = 0; i < 19; ++i) lengths[i] = 0;
  223. /* read code lengths for code length alphabet */
  224. for (i = 0; i < hclen; ++i)
  225. {
  226. /* get 3 bits code length (0-7) */
  227. unsigned int clen = tinf_read_bits(d, 3, 0);
  228. lengths[clcidx[i]] = clen;
  229. }
  230. /* build code length tree, temporarily use length tree */
  231. tinf_build_tree(lt, lengths, 19);
  232. /* decode code lengths for the dynamic trees */
  233. for (num = 0; num < hlit + hdist; )
  234. {
  235. int sym = tinf_decode_symbol(d, lt);
  236. switch (sym)
  237. {
  238. case 16:
  239. /* copy previous code length 3-6 times (read 2 bits) */
  240. {
  241. unsigned char prev = lengths[num - 1];
  242. for (length = tinf_read_bits(d, 2, 3); length; --length)
  243. {
  244. lengths[num++] = prev;
  245. }
  246. }
  247. break;
  248. case 17:
  249. /* repeat code length 0 for 3-10 times (read 3 bits) */
  250. for (length = tinf_read_bits(d, 3, 3); length; --length)
  251. {
  252. lengths[num++] = 0;
  253. }
  254. break;
  255. case 18:
  256. /* repeat code length 0 for 11-138 times (read 7 bits) */
  257. for (length = tinf_read_bits(d, 7, 11); length; --length)
  258. {
  259. lengths[num++] = 0;
  260. }
  261. break;
  262. default:
  263. /* values 0-15 represent the actual code lengths */
  264. lengths[num++] = sym;
  265. break;
  266. }
  267. }
  268. /* build dynamic trees */
  269. tinf_build_tree(lt, lengths, hlit);
  270. tinf_build_tree(dt, lengths + hlit, hdist);
  271. }
  272. /* ----------------------------- *
  273. * -- block inflate functions -- *
  274. * ----------------------------- */
  275. /* given a stream and two trees, inflate a block of data */
  276. static int tinf_inflate_block_data(TINF_DATA *d, TINF_TREE *lt, TINF_TREE *dt)
  277. {
  278. if (d->curlen == 0) {
  279. unsigned int offs;
  280. int dist;
  281. int sym = tinf_decode_symbol(d, lt);
  282. //printf("huff sym: %02x\n", sym);
  283. /* literal byte */
  284. if (sym < 256) {
  285. TINF_PUT(d, sym);
  286. return TINF_OK;
  287. }
  288. /* end of block */
  289. if (sym == 256) {
  290. return TINF_DONE;
  291. }
  292. /* substring from sliding dictionary */
  293. sym -= 257;
  294. /* possibly get more bits from length code */
  295. d->curlen = tinf_read_bits(d, length_bits[sym], length_base[sym]);
  296. dist = tinf_decode_symbol(d, dt);
  297. /* possibly get more bits from distance code */
  298. offs = tinf_read_bits(d, dist_bits[dist], dist_base[dist]);
  299. if (d->dict_ring) {
  300. if (offs > d->dict_size) {
  301. return TINF_DICT_ERROR;
  302. }
  303. d->lzOff = d->dict_idx - offs;
  304. if (d->lzOff < 0) {
  305. d->lzOff += d->dict_size;
  306. }
  307. } else {
  308. d->lzOff = -offs;
  309. }
  310. }
  311. /* copy next byte from dict substring */
  312. if (d->dict_ring) {
  313. TINF_PUT(d, d->dict_ring[d->lzOff]);
  314. if ((unsigned)++d->lzOff == d->dict_size) {
  315. d->lzOff = 0;
  316. }
  317. } else {
  318. d->dest[0] = d->dest[d->lzOff];
  319. d->dest++;
  320. }
  321. d->curlen--;
  322. return TINF_OK;
  323. }
  324. /* inflate an uncompressed block of data */
  325. static int tinf_inflate_uncompressed_block(TINF_DATA *d)
  326. {
  327. if (d->curlen == 0) {
  328. unsigned int length, invlength;
  329. /* get length */
  330. length = uzlib_get_byte(d);
  331. length += 256 * uzlib_get_byte(d);
  332. /* get one's complement of length */
  333. invlength = uzlib_get_byte(d);
  334. invlength += 256 * uzlib_get_byte(d);
  335. /* check length */
  336. if (length != (~invlength & 0x0000ffff)) return TINF_DATA_ERROR;
  337. /* increment length to properly return TINF_DONE below, without
  338. producing data at the same time */
  339. d->curlen = length + 1;
  340. /* make sure we start next block on a byte boundary */
  341. d->bitcount = 0;
  342. }
  343. if (--d->curlen == 0) {
  344. return TINF_DONE;
  345. }
  346. unsigned char c = uzlib_get_byte(d);
  347. TINF_PUT(d, c);
  348. return TINF_OK;
  349. }
  350. /* ---------------------- *
  351. * -- public functions -- *
  352. * ---------------------- */
  353. /* initialize global (static) data */
  354. void uzlib_init(void)
  355. {
  356. #ifdef RUNTIME_BITS_TABLES
  357. /* build extra bits and base tables */
  358. tinf_build_bits_base(length_bits, length_base, 4, 3);
  359. tinf_build_bits_base(dist_bits, dist_base, 2, 1);
  360. /* fix a special case */
  361. length_bits[28] = 0;
  362. length_base[28] = 258;
  363. #endif
  364. }
  365. /* initialize decompression structure */
  366. void uzlib_uncompress_init(TINF_DATA *d, void *dict, unsigned int dictLen)
  367. {
  368. d->bitcount = 0;
  369. d->bfinal = 0;
  370. d->btype = -1;
  371. d->dict_size = dictLen;
  372. d->dict_ring = dict;
  373. d->dict_idx = 0;
  374. d->curlen = 0;
  375. }
  376. /* inflate next byte of compressed stream */
  377. int uzlib_uncompress(TINF_DATA *d)
  378. {
  379. do {
  380. int res;
  381. /* start a new block */
  382. if (d->btype == -1) {
  383. next_blk:
  384. /* read final block flag */
  385. d->bfinal = tinf_getbit(d);
  386. /* read block type (2 bits) */
  387. d->btype = tinf_read_bits(d, 2, 0);
  388. //printf("Started new block: type=%d final=%d\n", d->btype, d->bfinal);
  389. if (d->btype == 1) {
  390. /* build fixed huffman trees */
  391. tinf_build_fixed_trees(&d->ltree, &d->dtree);
  392. } else if (d->btype == 2) {
  393. /* decode trees from stream */
  394. tinf_decode_trees(d, &d->ltree, &d->dtree);
  395. }
  396. }
  397. /* process current block */
  398. switch (d->btype)
  399. {
  400. case 0:
  401. /* decompress uncompressed block */
  402. res = tinf_inflate_uncompressed_block(d);
  403. break;
  404. case 1:
  405. case 2:
  406. /* decompress block with fixed/dyanamic huffman trees */
  407. /* trees were decoded previously, so it's the same routine for both */
  408. res = tinf_inflate_block_data(d, &d->ltree, &d->dtree);
  409. break;
  410. default:
  411. return TINF_DATA_ERROR;
  412. }
  413. if (res == TINF_DONE && !d->bfinal) {
  414. /* the block has ended (without producing more data), but we
  415. can't return without data, so start procesing next block */
  416. goto next_blk;
  417. }
  418. if (res != TINF_OK) {
  419. return res;
  420. }
  421. } while (--d->destSize);
  422. return TINF_OK;
  423. }
  424. int uzlib_uncompress_chksum(TINF_DATA *d)
  425. {
  426. int res;
  427. unsigned char *data = d->dest;
  428. res = uzlib_uncompress(d);
  429. if (res < 0) return res;
  430. switch (d->checksum_type) {
  431. case TINF_CHKSUM_ADLER:
  432. d->checksum = uzlib_adler32(data, d->dest - data, d->checksum);
  433. break;
  434. case TINF_CHKSUM_CRC:
  435. d->checksum = uzlib_crc32(data, d->dest - data, d->checksum);
  436. break;
  437. }
  438. if (res == TINF_DONE) {
  439. unsigned int val;
  440. switch (d->checksum_type) {
  441. case TINF_CHKSUM_ADLER:
  442. val = tinf_get_be_uint32(d);
  443. if (d->checksum != val) {
  444. return TINF_CHKSUM_ERROR;
  445. }
  446. break;
  447. case TINF_CHKSUM_CRC:
  448. val = tinf_get_le_uint32(d);
  449. if (~d->checksum != val) {
  450. return TINF_CHKSUM_ERROR;
  451. }
  452. // Uncompressed size. TODO: Check
  453. val = tinf_get_le_uint32(d);
  454. break;
  455. }
  456. }
  457. return res;
  458. }