tinflate.c 12 KB

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