decompress_unzip.c 28 KB

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