tinflate.cpp 15 KB

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