3
0

decompress_unzip.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * gunzip implementation for busybox
  4. *
  5. * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
  6. *
  7. * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
  8. * based on gzip sources
  9. *
  10. * Adjusted further by Erik Andersen <andersen@codepoet.org> to support
  11. * files as well as stdin/stdout, and to generally behave itself wrt
  12. * command line handling.
  13. *
  14. * General cleanup to better adhere to the style guide and make use of standard
  15. * busybox functions by Glenn McGrath <bug1@iinet.net.au>
  16. *
  17. * read_gz interface + associated hacking by Laurence Anderson
  18. *
  19. * Fixed huft_build() so decoding end-of-block code does not grab more bits
  20. * than necessary (this is required by unzip applet), added inflate_cleanup()
  21. * to free leaked bytebuffer memory (used in unzip.c), and some minor style
  22. * guide cleanups by Ed Clark
  23. *
  24. * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
  25. * Copyright (C) 1992-1993 Jean-loup Gailly
  26. * The unzip code was written and put in the public domain by Mark Adler.
  27. * Portions of the lzw code are derived from the public domain 'compress'
  28. * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
  29. * Ken Turkowski, Dave Mack and Peter Jannesen.
  30. *
  31. * See the file algorithm.doc for the compression algorithms and file formats.
  32. *
  33. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  34. */
  35. #include "libbb.h"
  36. #include "unarchive.h"
  37. typedef struct huft_s {
  38. unsigned char e; /* number of extra bits or operation */
  39. unsigned char b; /* number of bits in this code or subcode */
  40. union {
  41. unsigned short n; /* literal, length base, or distance base */
  42. struct huft_s *t; /* pointer to next level of table */
  43. } v;
  44. } huft_t;
  45. static int gunzip_src_fd;
  46. unsigned int gunzip_bytes_out; /* number of output bytes */
  47. static unsigned int gunzip_outbuf_count; /* bytes in output buffer */
  48. /* gunzip_window size--must be a power of two, and
  49. * at least 32K for zip's deflate method */
  50. enum { gunzip_wsize = 0x8000 };
  51. static unsigned char *gunzip_window;
  52. static uint32_t *gunzip_crc_table;
  53. uint32_t gunzip_crc;
  54. /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
  55. #define BMAX 16 /* maximum bit length of any code (16 for explode) */
  56. #define N_MAX 288 /* maximum number of codes in any set */
  57. /* bitbuffer */
  58. static unsigned int gunzip_bb; /* bit buffer */
  59. static unsigned char gunzip_bk; /* bits in bit buffer */
  60. /* These control the size of the bytebuffer */
  61. static unsigned int bytebuffer_max = 0x8000;
  62. static unsigned char *bytebuffer = NULL;
  63. static unsigned int bytebuffer_offset = 0;
  64. static unsigned int bytebuffer_size = 0;
  65. static const unsigned short mask_bits[] = {
  66. 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  67. 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
  68. };
  69. /* Copy lengths for literal codes 257..285 */
  70. static const unsigned short cplens[] = {
  71. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
  72. 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
  73. };
  74. /* note: see note #13 above about the 258 in this list. */
  75. /* Extra bits for literal codes 257..285 */
  76. static const unsigned char cplext[] = {
  77. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5,
  78. 5, 5, 5, 0, 99, 99
  79. }; /* 99==invalid */
  80. /* Copy offsets for distance codes 0..29 */
  81. static const unsigned short cpdist[] = {
  82. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
  83. 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
  84. };
  85. /* Extra bits for distance codes */
  86. static const unsigned char cpdext[] = {
  87. 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
  88. 11, 11, 12, 12, 13, 13
  89. };
  90. /* Tables for deflate from PKZIP's appnote.txt. */
  91. /* Order of the bit length code lengths */
  92. static const unsigned char border[] = {
  93. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
  94. };
  95. static unsigned int fill_bitbuffer(unsigned int bitbuffer, unsigned int *current, const unsigned int required)
  96. {
  97. while (*current < required) {
  98. if (bytebuffer_offset >= bytebuffer_size) {
  99. /* Leave the first 4 bytes empty so we can always unwind the bitbuffer
  100. * to the front of the bytebuffer, leave 4 bytes free at end of tail
  101. * so we can easily top up buffer in check_trailer_gzip() */
  102. if (1 > (bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8)))
  103. bb_error_msg_and_die("unexpected end of file");
  104. bytebuffer_size += 4;
  105. bytebuffer_offset = 4;
  106. }
  107. bitbuffer |= ((unsigned int) bytebuffer[bytebuffer_offset]) << *current;
  108. bytebuffer_offset++;
  109. *current += 8;
  110. }
  111. return bitbuffer;
  112. }
  113. /*
  114. * Free the malloc'ed tables built by huft_build(), which makes a linked
  115. * list of the tables it made, with the links in a dummy first entry of
  116. * each table.
  117. * t: table to free
  118. */
  119. static int huft_free(huft_t * t)
  120. {
  121. huft_t *p;
  122. huft_t *q;
  123. /* Go through linked list, freeing from the malloced (t[-1]) address. */
  124. p = t;
  125. while (p != (huft_t *) NULL) {
  126. q = (--p)->v.t;
  127. free((char *) p);
  128. p = q;
  129. }
  130. return 0;
  131. }
  132. /* Given a list of code lengths and a maximum table size, make a set of
  133. * tables to decode that set of codes. Return zero on success, one if
  134. * the given code set is incomplete (the tables are still built in this
  135. * case), two if the input is invalid (all zero length codes or an
  136. * oversubscribed set of lengths), and three if not enough memory.
  137. *
  138. * b: code lengths in bits (all assumed <= BMAX)
  139. * n: number of codes (assumed <= N_MAX)
  140. * s: number of simple-valued codes (0..s-1)
  141. * d: list of base values for non-simple codes
  142. * e: list of extra bits for non-simple codes
  143. * t: result: starting table
  144. * m: maximum lookup bits, returns actual
  145. */
  146. static
  147. int huft_build(unsigned int *b, const unsigned int n,
  148. const unsigned int s, const unsigned short *d,
  149. const unsigned char *e, huft_t ** t, unsigned int *m)
  150. {
  151. unsigned a; /* counter for codes of length k */
  152. unsigned c[BMAX + 1]; /* bit length count table */
  153. unsigned eob_len; /* length of end-of-block code (value 256) */
  154. unsigned f; /* i repeats in table every f entries */
  155. int g; /* maximum code length */
  156. int htl; /* table level */
  157. unsigned i; /* counter, current code */
  158. unsigned j; /* counter */
  159. int k; /* number of bits in current code */
  160. unsigned *p; /* pointer into c[], b[], or v[] */
  161. huft_t *q; /* points to current table */
  162. huft_t r; /* table entry for structure assignment */
  163. huft_t *u[BMAX]; /* table stack */
  164. unsigned v[N_MAX]; /* values in order of bit length */
  165. int ws[BMAX+1]; /* bits decoded stack */
  166. int w; /* bits decoded */
  167. unsigned x[BMAX + 1]; /* bit offsets, then code stack */
  168. unsigned *xp; /* pointer into x */
  169. int y; /* number of dummy codes added */
  170. unsigned z; /* number of entries in current table */
  171. /* Length of EOB code, if any */
  172. eob_len = n > 256 ? b[256] : BMAX;
  173. /* Generate counts for each bit length */
  174. memset((void *)c, 0, sizeof(c));
  175. p = b;
  176. i = n;
  177. do {
  178. c[*p]++; /* assume all entries <= BMAX */
  179. p++; /* Can't combine with above line (Solaris bug) */
  180. } while (--i);
  181. if (c[0] == n) { /* null input--all zero length codes */
  182. *t = (huft_t *) NULL;
  183. *m = 0;
  184. return 2;
  185. }
  186. /* Find minimum and maximum length, bound *m by those */
  187. for (j = 1; (c[j] == 0) && (j <= BMAX); j++);
  188. k = j; /* minimum code length */
  189. for (i = BMAX; (c[i] == 0) && i; i--);
  190. g = i; /* maximum code length */
  191. *m = (*m < j) ? j : ((*m > i) ? i : *m);
  192. /* Adjust last length count to fill out codes, if needed */
  193. for (y = 1 << j; j < i; j++, y <<= 1) {
  194. if ((y -= c[j]) < 0) {
  195. return 2; /* bad input: more codes than bits */
  196. }
  197. }
  198. if ((y -= c[i]) < 0) {
  199. return 2;
  200. }
  201. c[i] += y;
  202. /* Generate starting offsets into the value table for each length */
  203. x[1] = j = 0;
  204. p = c + 1;
  205. xp = x + 2;
  206. while (--i) { /* note that i == g from above */
  207. *xp++ = (j += *p++);
  208. }
  209. /* Make a table of values in order of bit lengths */
  210. p = b;
  211. i = 0;
  212. do {
  213. if ((j = *p++) != 0) {
  214. v[x[j]++] = i;
  215. }
  216. } while (++i < n);
  217. /* Generate the Huffman codes and for each, make the table entries */
  218. x[0] = i = 0; /* first Huffman code is zero */
  219. p = v; /* grab values in bit order */
  220. htl = -1; /* no tables yet--level -1 */
  221. w = ws[0] = 0; /* bits decoded */
  222. u[0] = (huft_t *) NULL; /* just to keep compilers happy */
  223. q = (huft_t *) NULL; /* ditto */
  224. z = 0; /* ditto */
  225. /* go through the bit lengths (k already is bits in shortest code) */
  226. for (; k <= g; k++) {
  227. a = c[k];
  228. while (a--) {
  229. /* here i is the Huffman code of length k bits for value *p */
  230. /* make tables up to required level */
  231. while (k > ws[htl + 1]) {
  232. w = ws[++htl];
  233. /* compute minimum size table less than or equal to *m bits */
  234. z = (z = g - w) > *m ? *m : z; /* upper limit on table size */
  235. if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table */
  236. /* too few codes for k-w bit table */
  237. f -= a + 1; /* deduct codes from patterns left */
  238. xp = c + k;
  239. while (++j < z) { /* try smaller tables up to z bits */
  240. if ((f <<= 1) <= *++xp) {
  241. break; /* enough codes to use up j bits */
  242. }
  243. f -= *xp; /* else deduct codes from patterns */
  244. }
  245. }
  246. j = (w + j > eob_len && w < eob_len) ? eob_len - w : j; /* make EOB code end at table */
  247. z = 1 << j; /* table entries for j-bit table */
  248. ws[htl+1] = w + j; /* set bits decoded in stack */
  249. /* allocate and link in new table */
  250. q = (huft_t *) xzalloc((z + 1) * sizeof(huft_t));
  251. *t = q + 1; /* link to list for huft_free() */
  252. t = &(q->v.t);
  253. u[htl] = ++q; /* table starts after link */
  254. /* connect to last table, if there is one */
  255. if (htl) {
  256. x[htl] = i; /* save pattern for backing up */
  257. r.b = (unsigned char) (w - ws[htl - 1]); /* bits to dump before this table */
  258. r.e = (unsigned char) (16 + j); /* bits in this table */
  259. r.v.t = q; /* pointer to this table */
  260. j = (i & ((1 << w) - 1)) >> ws[htl - 1];
  261. u[htl - 1][j] = r; /* connect to last table */
  262. }
  263. }
  264. /* set up table entry in r */
  265. r.b = (unsigned char) (k - w);
  266. if (p >= v + n) {
  267. r.e = 99; /* out of values--invalid code */
  268. } else if (*p < s) {
  269. r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */
  270. r.v.n = (unsigned short) (*p++); /* simple code is just the value */
  271. } else {
  272. r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */
  273. r.v.n = d[*p++ - s];
  274. }
  275. /* fill code-like entries with r */
  276. f = 1 << (k - w);
  277. for (j = i >> w; j < z; j += f) {
  278. q[j] = r;
  279. }
  280. /* backwards increment the k-bit code i */
  281. for (j = 1 << (k - 1); i & j; j >>= 1) {
  282. i ^= j;
  283. }
  284. i ^= j;
  285. /* backup over finished tables */
  286. while ((i & ((1 << w) - 1)) != x[htl]) {
  287. w = ws[--htl];
  288. }
  289. }
  290. }
  291. /* return actual size of base table */
  292. *m = ws[1];
  293. /* Return true (1) if we were given an incomplete table */
  294. return y != 0 && g != 1;
  295. }
  296. /*
  297. * inflate (decompress) the codes in a deflated (compressed) block.
  298. * Return an error code or zero if it all goes ok.
  299. *
  300. * tl, td: literal/length and distance decoder tables
  301. * bl, bd: number of bits decoded by tl[] and td[]
  302. */
  303. static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned int my_bl, const unsigned int my_bd, int setup)
  304. {
  305. static unsigned int e; /* table entry flag/number of extra bits */
  306. static unsigned int n, d; /* length and index for copy */
  307. static unsigned int w; /* current gunzip_window position */
  308. static huft_t *t; /* pointer to table entry */
  309. static unsigned int ml, md; /* masks for bl and bd bits */
  310. static unsigned int b; /* bit buffer */
  311. static unsigned int k; /* number of bits in bit buffer */
  312. static huft_t *tl, *td;
  313. static unsigned int bl, bd;
  314. static int resumeCopy = 0;
  315. if (setup) { // 1st time we are called, copy in variables
  316. tl = my_tl;
  317. td = my_td;
  318. bl = my_bl;
  319. bd = my_bd;
  320. /* make local copies of globals */
  321. b = gunzip_bb; /* initialize bit buffer */
  322. k = gunzip_bk;
  323. w = gunzip_outbuf_count; /* initialize gunzip_window position */
  324. /* inflate the coded data */
  325. ml = mask_bits[bl]; /* precompute masks for speed */
  326. md = mask_bits[bd];
  327. return 0; // Don't actually do anything the first time
  328. }
  329. if (resumeCopy) goto do_copy;
  330. while (1) { /* do until end of block */
  331. b = fill_bitbuffer(b, &k, bl);
  332. if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
  333. do {
  334. if (e == 99) {
  335. bb_error_msg_and_die("inflate_codes error 1");
  336. }
  337. b >>= t->b;
  338. k -= t->b;
  339. e -= 16;
  340. b = fill_bitbuffer(b, &k, e);
  341. } while ((e =
  342. (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
  343. b >>= t->b;
  344. k -= t->b;
  345. if (e == 16) { /* then it's a literal */
  346. gunzip_window[w++] = (unsigned char) t->v.n;
  347. if (w == gunzip_wsize) {
  348. gunzip_outbuf_count = (w);
  349. //flush_gunzip_window();
  350. w = 0;
  351. return 1; // We have a block to read
  352. }
  353. } else { /* it's an EOB or a length */
  354. /* exit if end of block */
  355. if (e == 15) {
  356. break;
  357. }
  358. /* get length of block to copy */
  359. b = fill_bitbuffer(b, &k, e);
  360. n = t->v.n + ((unsigned) b & mask_bits[e]);
  361. b >>= e;
  362. k -= e;
  363. /* decode distance of block to copy */
  364. b = fill_bitbuffer(b, &k, bd);
  365. if ((e = (t = td + ((unsigned) b & md))->e) > 16)
  366. do {
  367. if (e == 99)
  368. bb_error_msg_and_die("inflate_codes error 2");
  369. b >>= t->b;
  370. k -= t->b;
  371. e -= 16;
  372. b = fill_bitbuffer(b, &k, e);
  373. } while ((e =
  374. (t =
  375. t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
  376. b >>= t->b;
  377. k -= t->b;
  378. b = fill_bitbuffer(b, &k, e);
  379. d = w - t->v.n - ((unsigned) b & mask_bits[e]);
  380. b >>= e;
  381. k -= e;
  382. /* do the copy */
  383. do_copy: do {
  384. n -= (e =
  385. (e =
  386. gunzip_wsize - ((d &= gunzip_wsize - 1) > w ? d : w)) > n ? n : e);
  387. /* copy to new buffer to prevent possible overwrite */
  388. if (w - d >= e) { /* (this test assumes unsigned comparison) */
  389. memcpy(gunzip_window + w, gunzip_window + d, e);
  390. w += e;
  391. d += e;
  392. } else {
  393. /* do it slow to avoid memcpy() overlap */
  394. /* !NOMEMCPY */
  395. do {
  396. gunzip_window[w++] = gunzip_window[d++];
  397. } while (--e);
  398. }
  399. if (w == gunzip_wsize) {
  400. gunzip_outbuf_count = (w);
  401. if (n) resumeCopy = 1;
  402. else resumeCopy = 0;
  403. //flush_gunzip_window();
  404. w = 0;
  405. return 1;
  406. }
  407. } while (n);
  408. resumeCopy = 0;
  409. }
  410. }
  411. /* restore the globals from the locals */
  412. gunzip_outbuf_count = w; /* restore global gunzip_window pointer */
  413. gunzip_bb = b; /* restore global bit buffer */
  414. gunzip_bk = k;
  415. /* normally just after call to inflate_codes, but save code by putting it here */
  416. /* free the decoding tables, return */
  417. huft_free(tl);
  418. huft_free(td);
  419. /* done */
  420. return 0;
  421. }
  422. static int inflate_stored(int my_n, int my_b_stored, int my_k_stored, int setup)
  423. {
  424. static unsigned int n, b_stored, k_stored, w;
  425. if (setup) {
  426. n = my_n;
  427. b_stored = my_b_stored;
  428. k_stored = my_k_stored;
  429. w = gunzip_outbuf_count; /* initialize gunzip_window position */
  430. return 0; // Don't do anything first time
  431. }
  432. /* read and output the compressed data */
  433. while (n--) {
  434. b_stored = fill_bitbuffer(b_stored, &k_stored, 8);
  435. gunzip_window[w++] = (unsigned char) b_stored;
  436. if (w == gunzip_wsize) {
  437. gunzip_outbuf_count = (w);
  438. //flush_gunzip_window();
  439. w = 0;
  440. b_stored >>= 8;
  441. k_stored -= 8;
  442. return 1; // We have a block
  443. }
  444. b_stored >>= 8;
  445. k_stored -= 8;
  446. }
  447. /* restore the globals from the locals */
  448. gunzip_outbuf_count = w; /* restore global gunzip_window pointer */
  449. gunzip_bb = b_stored; /* restore global bit buffer */
  450. gunzip_bk = k_stored;
  451. return 0; // Finished
  452. }
  453. /*
  454. * decompress an inflated block
  455. * e: last block flag
  456. *
  457. * GLOBAL VARIABLES: bb, kk,
  458. */
  459. // Return values: -1 = inflate_stored, -2 = inflate_codes
  460. static int inflate_block(int *e)
  461. {
  462. unsigned t; /* block type */
  463. unsigned int b; /* bit buffer */
  464. unsigned int k; /* number of bits in bit buffer */
  465. /* make local bit buffer */
  466. b = gunzip_bb;
  467. k = gunzip_bk;
  468. /* read in last block bit */
  469. b = fill_bitbuffer(b, &k, 1);
  470. *e = (int) b & 1;
  471. b >>= 1;
  472. k -= 1;
  473. /* read in block type */
  474. b = fill_bitbuffer(b, &k, 2);
  475. t = (unsigned) b & 3;
  476. b >>= 2;
  477. k -= 2;
  478. /* restore the global bit buffer */
  479. gunzip_bb = b;
  480. gunzip_bk = k;
  481. /* inflate that block type */
  482. switch (t) {
  483. case 0: /* Inflate stored */
  484. {
  485. unsigned int n; /* number of bytes in block */
  486. unsigned int b_stored; /* bit buffer */
  487. unsigned int k_stored; /* number of bits in bit buffer */
  488. /* make local copies of globals */
  489. b_stored = gunzip_bb; /* initialize bit buffer */
  490. k_stored = gunzip_bk;
  491. /* go to byte boundary */
  492. n = k_stored & 7;
  493. b_stored >>= n;
  494. k_stored -= n;
  495. /* get the length and its complement */
  496. b_stored = fill_bitbuffer(b_stored, &k_stored, 16);
  497. n = ((unsigned) b_stored & 0xffff);
  498. b_stored >>= 16;
  499. k_stored -= 16;
  500. b_stored = fill_bitbuffer(b_stored, &k_stored, 16);
  501. if (n != (unsigned) ((~b_stored) & 0xffff)) {
  502. return 1; /* error in compressed data */
  503. }
  504. b_stored >>= 16;
  505. k_stored -= 16;
  506. inflate_stored(n, b_stored, k_stored, 1); // Setup inflate_stored
  507. return -1;
  508. }
  509. case 1: /* Inflate fixed
  510. * decompress an inflated type 1 (fixed Huffman codes) block. We should
  511. * either replace this with a custom decoder, or at least precompute the
  512. * Huffman tables.
  513. */
  514. {
  515. int i; /* temporary variable */
  516. huft_t *tl; /* literal/length code table */
  517. huft_t *td; /* distance code table */
  518. unsigned int bl; /* lookup bits for tl */
  519. unsigned int bd; /* lookup bits for td */
  520. unsigned int l[288]; /* length list for huft_build */
  521. /* set up literal table */
  522. for (i = 0; i < 144; i++) {
  523. l[i] = 8;
  524. }
  525. for (; i < 256; i++) {
  526. l[i] = 9;
  527. }
  528. for (; i < 280; i++) {
  529. l[i] = 7;
  530. }
  531. for (; i < 288; i++) { /* make a complete, but wrong code set */
  532. l[i] = 8;
  533. }
  534. bl = 7;
  535. if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
  536. return i;
  537. }
  538. /* set up distance table */
  539. for (i = 0; i < 30; i++) { /* make an incomplete code set */
  540. l[i] = 5;
  541. }
  542. bd = 5;
  543. if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) {
  544. huft_free(tl);
  545. return i;
  546. }
  547. /* decompress until an end-of-block code */
  548. inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes
  549. /* huft_free code moved into inflate_codes */
  550. return -2;
  551. }
  552. case 2: /* Inflate dynamic */
  553. {
  554. const int dbits = 6; /* bits in base distance lookup table */
  555. const int lbits = 9; /* bits in base literal/length lookup table */
  556. huft_t *tl; /* literal/length code table */
  557. huft_t *td; /* distance code table */
  558. unsigned int i; /* temporary variables */
  559. unsigned int j;
  560. unsigned int l; /* last length */
  561. unsigned int m; /* mask for bit lengths table */
  562. unsigned int n; /* number of lengths to get */
  563. unsigned int bl; /* lookup bits for tl */
  564. unsigned int bd; /* lookup bits for td */
  565. unsigned int nb; /* number of bit length codes */
  566. unsigned int nl; /* number of literal/length codes */
  567. unsigned int nd; /* number of distance codes */
  568. unsigned int ll[286 + 30]; /* literal/length and distance code lengths */
  569. unsigned int b_dynamic; /* bit buffer */
  570. unsigned int k_dynamic; /* number of bits in bit buffer */
  571. /* make local bit buffer */
  572. b_dynamic = gunzip_bb;
  573. k_dynamic = gunzip_bk;
  574. /* read in table lengths */
  575. b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 5);
  576. nl = 257 + ((unsigned int) b_dynamic & 0x1f); /* number of literal/length codes */
  577. b_dynamic >>= 5;
  578. k_dynamic -= 5;
  579. b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 5);
  580. nd = 1 + ((unsigned int) b_dynamic & 0x1f); /* number of distance codes */
  581. b_dynamic >>= 5;
  582. k_dynamic -= 5;
  583. b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 4);
  584. nb = 4 + ((unsigned int) b_dynamic & 0xf); /* number of bit length codes */
  585. b_dynamic >>= 4;
  586. k_dynamic -= 4;
  587. if (nl > 286 || nd > 30) {
  588. return 1; /* bad lengths */
  589. }
  590. /* read in bit-length-code lengths */
  591. for (j = 0; j < nb; j++) {
  592. b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 3);
  593. ll[border[j]] = (unsigned int) b_dynamic & 7;
  594. b_dynamic >>= 3;
  595. k_dynamic -= 3;
  596. }
  597. for (; j < 19; j++) {
  598. ll[border[j]] = 0;
  599. }
  600. /* build decoding table for trees--single level, 7 bit lookup */
  601. bl = 7;
  602. i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl);
  603. if (i != 0) {
  604. if (i == 1) {
  605. huft_free(tl);
  606. }
  607. return i; /* incomplete code set */
  608. }
  609. /* read in literal and distance code lengths */
  610. n = nl + nd;
  611. m = mask_bits[bl];
  612. i = l = 0;
  613. while ((unsigned int) i < n) {
  614. b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, (unsigned int)bl);
  615. j = (td = tl + ((unsigned int) b_dynamic & m))->b;
  616. b_dynamic >>= j;
  617. k_dynamic -= j;
  618. j = td->v.n;
  619. if (j < 16) { /* length of code in bits (0..15) */
  620. ll[i++] = l = j; /* save last length in l */
  621. } else if (j == 16) { /* repeat last length 3 to 6 times */
  622. b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 2);
  623. j = 3 + ((unsigned int) b_dynamic & 3);
  624. b_dynamic >>= 2;
  625. k_dynamic -= 2;
  626. if ((unsigned int) i + j > n) {
  627. return 1;
  628. }
  629. while (j--) {
  630. ll[i++] = l;
  631. }
  632. } else if (j == 17) { /* 3 to 10 zero length codes */
  633. b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 3);
  634. j = 3 + ((unsigned int) b_dynamic & 7);
  635. b_dynamic >>= 3;
  636. k_dynamic -= 3;
  637. if ((unsigned int) i + j > n) {
  638. return 1;
  639. }
  640. while (j--) {
  641. ll[i++] = 0;
  642. }
  643. l = 0;
  644. } else { /* j == 18: 11 to 138 zero length codes */
  645. b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 7);
  646. j = 11 + ((unsigned int) b_dynamic & 0x7f);
  647. b_dynamic >>= 7;
  648. k_dynamic -= 7;
  649. if ((unsigned int) i + j > n) {
  650. return 1;
  651. }
  652. while (j--) {
  653. ll[i++] = 0;
  654. }
  655. l = 0;
  656. }
  657. }
  658. /* free decoding table for trees */
  659. huft_free(tl);
  660. /* restore the global bit buffer */
  661. gunzip_bb = b_dynamic;
  662. gunzip_bk = k_dynamic;
  663. /* build the decoding tables for literal/length and distance codes */
  664. bl = lbits;
  665. if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
  666. if (i == 1) {
  667. bb_error_msg_and_die("incomplete literal tree");
  668. huft_free(tl);
  669. }
  670. return i; /* incomplete code set */
  671. }
  672. bd = dbits;
  673. if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
  674. if (i == 1) {
  675. bb_error_msg_and_die("incomplete distance tree");
  676. huft_free(td);
  677. }
  678. huft_free(tl);
  679. return i; /* incomplete code set */
  680. }
  681. /* decompress until an end-of-block code */
  682. inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes
  683. /* huft_free code moved into inflate_codes */
  684. return -2;
  685. }
  686. default:
  687. /* bad block type */
  688. bb_error_msg_and_die("bad block type %d", t);
  689. }
  690. }
  691. static void calculate_gunzip_crc(void)
  692. {
  693. int n;
  694. for (n = 0; n < gunzip_outbuf_count; n++) {
  695. gunzip_crc = gunzip_crc_table[((int) gunzip_crc ^ (gunzip_window[n])) & 0xff] ^ (gunzip_crc >> 8);
  696. }
  697. gunzip_bytes_out += gunzip_outbuf_count;
  698. }
  699. static int inflate_get_next_window(void)
  700. {
  701. static int method = -1; // Method == -1 for stored, -2 for codes
  702. static int e = 0;
  703. static int needAnotherBlock = 1;
  704. gunzip_outbuf_count = 0;
  705. while(1) {
  706. int ret;
  707. if (needAnotherBlock) {
  708. if(e) {
  709. calculate_gunzip_crc();
  710. e = 0;
  711. needAnotherBlock = 1;
  712. return 0;
  713. } // Last block
  714. method = inflate_block(&e);
  715. needAnotherBlock = 0;
  716. }
  717. switch (method) {
  718. case -1: ret = inflate_stored(0,0,0,0);
  719. break;
  720. case -2: ret = inflate_codes(0,0,0,0,0);
  721. break;
  722. default: bb_error_msg_and_die("inflate error %d", method);
  723. }
  724. if (ret == 1) {
  725. calculate_gunzip_crc();
  726. return 1; // More data left
  727. } else needAnotherBlock = 1; // End of that block
  728. }
  729. /* Doesnt get here */
  730. }
  731. /* Initialise bytebuffer, be careful not to overfill the buffer */
  732. void inflate_init(unsigned int bufsize)
  733. {
  734. /* Set the bytebuffer size, default is same as gunzip_wsize */
  735. bytebuffer_max = bufsize + 8;
  736. bytebuffer_offset = 4;
  737. bytebuffer_size = 0;
  738. }
  739. void inflate_cleanup(void)
  740. {
  741. free(bytebuffer);
  742. }
  743. USE_DESKTOP(long long) int
  744. inflate_unzip(int in, int out)
  745. {
  746. USE_DESKTOP(long long total = 0;)
  747. ssize_t nwrote;
  748. typedef void (*sig_type) (int);
  749. /* Allocate all global buffers (for DYN_ALLOC option) */
  750. gunzip_window = xmalloc(gunzip_wsize);
  751. gunzip_outbuf_count = 0;
  752. gunzip_bytes_out = 0;
  753. gunzip_src_fd = in;
  754. /* initialize gunzip_window, bit buffer */
  755. gunzip_bk = 0;
  756. gunzip_bb = 0;
  757. /* Create the crc table */
  758. gunzip_crc_table = crc32_filltable(0);
  759. gunzip_crc = ~0;
  760. /* Allocate space for buffer */
  761. bytebuffer = xmalloc(bytebuffer_max);
  762. while(1) {
  763. int ret = inflate_get_next_window();
  764. nwrote = full_write(out, gunzip_window, gunzip_outbuf_count);
  765. if (nwrote == -1) {
  766. bb_perror_msg("write");
  767. return -1;
  768. }
  769. USE_DESKTOP(total += nwrote;)
  770. if (ret == 0) break;
  771. }
  772. /* Cleanup */
  773. free(gunzip_window);
  774. free(gunzip_crc_table);
  775. /* Store unused bytes in a global buffer so calling applets can access it */
  776. if (gunzip_bk >= 8) {
  777. /* Undo too much lookahead. The next read will be byte aligned
  778. * so we can discard unused bits in the last meaningful byte. */
  779. bytebuffer_offset--;
  780. bytebuffer[bytebuffer_offset] = gunzip_bb & 0xff;
  781. gunzip_bb >>= 8;
  782. gunzip_bk -= 8;
  783. }
  784. return USE_DESKTOP(total) + 0;
  785. }
  786. USE_DESKTOP(long long) int
  787. inflate_gunzip(int in, int out)
  788. {
  789. uint32_t stored_crc = 0;
  790. unsigned int count;
  791. USE_DESKTOP(long long total = )inflate_unzip(in, out);
  792. USE_DESKTOP(if (total < 0) return total;)
  793. /* top up the input buffer with the rest of the trailer */
  794. count = bytebuffer_size - bytebuffer_offset;
  795. if (count < 8) {
  796. xread(in, &bytebuffer[bytebuffer_size], 8 - count);
  797. bytebuffer_size += 8 - count;
  798. }
  799. for (count = 0; count != 4; count++) {
  800. stored_crc |= (bytebuffer[bytebuffer_offset] << (count * 8));
  801. bytebuffer_offset++;
  802. }
  803. /* Validate decompression - crc */
  804. if (stored_crc != (~gunzip_crc)) {
  805. bb_error_msg("crc error");
  806. return -1;
  807. }
  808. /* Validate decompression - size */
  809. if (gunzip_bytes_out !=
  810. (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) |
  811. (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))) {
  812. bb_error_msg("incorrect length");
  813. return -1;
  814. }
  815. return USE_DESKTOP(total) + 0;
  816. }