decompress_unzip.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  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
  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 <setjmp.h>
  36. #include "libbb.h"
  37. #include "unarchive.h"
  38. typedef struct huft_t {
  39. unsigned char e; /* number of extra bits or operation */
  40. unsigned char b; /* number of bits in this code or subcode */
  41. union {
  42. unsigned short n; /* literal, length base, or distance base */
  43. struct huft_t *t; /* pointer to next level of table */
  44. } v;
  45. } huft_t;
  46. enum {
  47. /* gunzip_window size--must be a power of two, and
  48. * at least 32K for zip's deflate method */
  49. GUNZIP_WSIZE = 0x8000,
  50. /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
  51. BMAX = 16, /* maximum bit length of any code (16 for explode) */
  52. N_MAX = 288, /* maximum number of codes in any set */
  53. };
  54. /* This is somewhat complex-looking arrangement, but it allows
  55. * to place decompressor state either in bss or in
  56. * malloc'ed space simply by changing #defines below.
  57. * Sizes on i386:
  58. * text data bss dec hex
  59. * 5256 0 108 5364 14f4 - bss
  60. * 4915 0 0 4915 1333 - malloc
  61. */
  62. #define STATE_IN_BSS 0
  63. #define STATE_IN_MALLOC 1
  64. typedef struct state_t {
  65. off_t gunzip_bytes_out; /* number of output bytes */
  66. uint32_t gunzip_crc;
  67. int gunzip_src_fd;
  68. unsigned gunzip_outbuf_count; /* bytes in output buffer */
  69. unsigned char *gunzip_window;
  70. uint32_t *gunzip_crc_table;
  71. /* bitbuffer */
  72. unsigned gunzip_bb; /* bit buffer */
  73. unsigned char gunzip_bk; /* bits in bit buffer */
  74. /* input (compressed) data */
  75. unsigned char *bytebuffer; /* buffer itself */
  76. off_t to_read; /* compressed bytes to read (unzip only, -1 for gunzip) */
  77. // unsigned bytebuffer_max; /* buffer size */
  78. unsigned bytebuffer_offset; /* buffer position */
  79. unsigned bytebuffer_size; /* how much data is there (size <= max) */
  80. /* private data of inflate_codes() */
  81. unsigned inflate_codes_ml; /* masks for bl and bd bits */
  82. unsigned inflate_codes_md; /* masks for bl and bd bits */
  83. unsigned inflate_codes_bb; /* bit buffer */
  84. unsigned inflate_codes_k; /* number of bits in bit buffer */
  85. unsigned inflate_codes_w; /* current gunzip_window position */
  86. huft_t *inflate_codes_tl;
  87. huft_t *inflate_codes_td;
  88. unsigned inflate_codes_bl;
  89. unsigned inflate_codes_bd;
  90. unsigned inflate_codes_nn; /* length and index for copy */
  91. unsigned inflate_codes_dd;
  92. smallint resume_copy;
  93. /* private data of inflate_get_next_window() */
  94. smallint method; /* method == -1 for stored, -2 for codes */
  95. smallint need_another_block;
  96. smallint end_reached;
  97. /* private data of inflate_stored() */
  98. unsigned inflate_stored_n;
  99. unsigned inflate_stored_b;
  100. unsigned inflate_stored_k;
  101. unsigned inflate_stored_w;
  102. const char *error_msg;
  103. jmp_buf error_jmp;
  104. } state_t;
  105. #define gunzip_bytes_out (S()gunzip_bytes_out )
  106. #define gunzip_crc (S()gunzip_crc )
  107. #define gunzip_src_fd (S()gunzip_src_fd )
  108. #define gunzip_outbuf_count (S()gunzip_outbuf_count)
  109. #define gunzip_window (S()gunzip_window )
  110. #define gunzip_crc_table (S()gunzip_crc_table )
  111. #define gunzip_bb (S()gunzip_bb )
  112. #define gunzip_bk (S()gunzip_bk )
  113. #define to_read (S()to_read )
  114. // #define bytebuffer_max (S()bytebuffer_max )
  115. // Both gunzip and unzip can use constant buffer size now (16k):
  116. #define bytebuffer_max 0x4000
  117. #define bytebuffer (S()bytebuffer )
  118. #define bytebuffer_offset (S()bytebuffer_offset )
  119. #define bytebuffer_size (S()bytebuffer_size )
  120. #define inflate_codes_ml (S()inflate_codes_ml )
  121. #define inflate_codes_md (S()inflate_codes_md )
  122. #define inflate_codes_bb (S()inflate_codes_bb )
  123. #define inflate_codes_k (S()inflate_codes_k )
  124. #define inflate_codes_w (S()inflate_codes_w )
  125. #define inflate_codes_tl (S()inflate_codes_tl )
  126. #define inflate_codes_td (S()inflate_codes_td )
  127. #define inflate_codes_bl (S()inflate_codes_bl )
  128. #define inflate_codes_bd (S()inflate_codes_bd )
  129. #define inflate_codes_nn (S()inflate_codes_nn )
  130. #define inflate_codes_dd (S()inflate_codes_dd )
  131. #define resume_copy (S()resume_copy )
  132. #define method (S()method )
  133. #define need_another_block (S()need_another_block )
  134. #define end_reached (S()end_reached )
  135. #define inflate_stored_n (S()inflate_stored_n )
  136. #define inflate_stored_b (S()inflate_stored_b )
  137. #define inflate_stored_k (S()inflate_stored_k )
  138. #define inflate_stored_w (S()inflate_stored_w )
  139. #define error_msg (S()error_msg )
  140. #define error_jmp (S()error_jmp )
  141. /* This is a generic part */
  142. #if STATE_IN_BSS /* Use global data segment */
  143. #define DECLARE_STATE /*nothing*/
  144. #define ALLOC_STATE /*nothing*/
  145. #define DEALLOC_STATE ((void)0)
  146. #define S() state.
  147. #define PASS_STATE /*nothing*/
  148. #define PASS_STATE_ONLY /*nothing*/
  149. #define STATE_PARAM /*nothing*/
  150. #define STATE_PARAM_ONLY void
  151. static state_t state;
  152. #endif
  153. #if STATE_IN_MALLOC /* Use malloc space */
  154. #define DECLARE_STATE state_t *state
  155. #define ALLOC_STATE (state = xzalloc(sizeof(*state)))
  156. #define DEALLOC_STATE free(state)
  157. #define S() state->
  158. #define PASS_STATE state,
  159. #define PASS_STATE_ONLY state
  160. #define STATE_PARAM state_t *state,
  161. #define STATE_PARAM_ONLY state_t *state
  162. #endif
  163. static const uint16_t mask_bits[] ALIGN2 = {
  164. 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  165. 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
  166. };
  167. /* Copy lengths for literal codes 257..285 */
  168. static const uint16_t cplens[] ALIGN2 = {
  169. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
  170. 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
  171. };
  172. /* note: see note #13 above about the 258 in this list. */
  173. /* Extra bits for literal codes 257..285 */
  174. static const uint8_t cplext[] ALIGN1 = {
  175. 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,
  176. 5, 5, 5, 0, 99, 99
  177. }; /* 99 == invalid */
  178. /* Copy offsets for distance codes 0..29 */
  179. static const uint16_t cpdist[] ALIGN2 = {
  180. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
  181. 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
  182. };
  183. /* Extra bits for distance codes */
  184. static const uint8_t cpdext[] ALIGN1 = {
  185. 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
  186. 11, 11, 12, 12, 13, 13
  187. };
  188. /* Tables for deflate from PKZIP's appnote.txt. */
  189. /* Order of the bit length code lengths */
  190. static const uint8_t border[] ALIGN1 = {
  191. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
  192. };
  193. /*
  194. * Free the malloc'ed tables built by huft_build(), which makes a linked
  195. * list of the tables it made, with the links in a dummy first entry of
  196. * each table.
  197. * t: table to free
  198. */
  199. static void huft_free(huft_t *p)
  200. {
  201. huft_t *q;
  202. /* Go through linked list, freeing from the malloced (t[-1]) address. */
  203. while (p) {
  204. q = (--p)->v.t;
  205. free(p);
  206. p = q;
  207. }
  208. }
  209. static void huft_free_all(STATE_PARAM_ONLY)
  210. {
  211. huft_free(inflate_codes_tl);
  212. huft_free(inflate_codes_td);
  213. inflate_codes_tl = NULL;
  214. inflate_codes_td = NULL;
  215. }
  216. static void abort_unzip(STATE_PARAM_ONLY) NORETURN;
  217. static void abort_unzip(STATE_PARAM_ONLY)
  218. {
  219. huft_free_all(PASS_STATE_ONLY);
  220. longjmp(error_jmp, 1);
  221. }
  222. static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current, const unsigned required)
  223. {
  224. while (*current < required) {
  225. if (bytebuffer_offset >= bytebuffer_size) {
  226. unsigned sz = bytebuffer_max - 4;
  227. if (to_read >= 0 && to_read < sz) /* unzip only */
  228. sz = to_read;
  229. /* Leave the first 4 bytes empty so we can always unwind the bitbuffer
  230. * to the front of the bytebuffer */
  231. bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], sz);
  232. if ((int)bytebuffer_size < 1) {
  233. error_msg = "unexpected end of file";
  234. abort_unzip(PASS_STATE_ONLY);
  235. }
  236. if (to_read >= 0) /* unzip only */
  237. to_read -= bytebuffer_size;
  238. bytebuffer_size += 4;
  239. bytebuffer_offset = 4;
  240. }
  241. bitbuffer |= ((unsigned) bytebuffer[bytebuffer_offset]) << *current;
  242. bytebuffer_offset++;
  243. *current += 8;
  244. }
  245. return bitbuffer;
  246. }
  247. /* Given a list of code lengths and a maximum table size, make a set of
  248. * tables to decode that set of codes. Return zero on success, one if
  249. * the given code set is incomplete (the tables are still built in this
  250. * case), two if the input is invalid (all zero length codes or an
  251. * oversubscribed set of lengths) - in this case stores NULL in *t.
  252. *
  253. * b: code lengths in bits (all assumed <= BMAX)
  254. * n: number of codes (assumed <= N_MAX)
  255. * s: number of simple-valued codes (0..s-1)
  256. * d: list of base values for non-simple codes
  257. * e: list of extra bits for non-simple codes
  258. * t: result: starting table
  259. * m: maximum lookup bits, returns actual
  260. */
  261. static int huft_build(const unsigned *b, const unsigned n,
  262. const unsigned s, const unsigned short *d,
  263. const unsigned char *e, huft_t **t, unsigned *m)
  264. {
  265. unsigned a; /* counter for codes of length k */
  266. unsigned c[BMAX + 1]; /* bit length count table */
  267. unsigned eob_len; /* length of end-of-block code (value 256) */
  268. unsigned f; /* i repeats in table every f entries */
  269. int g; /* maximum code length */
  270. int htl; /* table level */
  271. unsigned i; /* counter, current code */
  272. unsigned j; /* counter */
  273. int k; /* number of bits in current code */
  274. unsigned *p; /* pointer into c[], b[], or v[] */
  275. huft_t *q; /* points to current table */
  276. huft_t r; /* table entry for structure assignment */
  277. huft_t *u[BMAX]; /* table stack */
  278. unsigned v[N_MAX]; /* values in order of bit length */
  279. int ws[BMAX + 1]; /* bits decoded stack */
  280. int w; /* bits decoded */
  281. unsigned x[BMAX + 1]; /* bit offsets, then code stack */
  282. unsigned *xp; /* pointer into x */
  283. int y; /* number of dummy codes added */
  284. unsigned z; /* number of entries in current table */
  285. /* Length of EOB code, if any */
  286. eob_len = n > 256 ? b[256] : BMAX;
  287. *t = NULL;
  288. /* Generate counts for each bit length */
  289. memset(c, 0, sizeof(c));
  290. p = (unsigned *) b; /* cast allows us to reuse p for pointing to b */
  291. i = n;
  292. do {
  293. c[*p]++; /* assume all entries <= BMAX */
  294. p++; /* can't combine with above line (Solaris bug) */
  295. } while (--i);
  296. if (c[0] == n) { /* null input - all zero length codes */
  297. *m = 0;
  298. return 2;
  299. }
  300. /* Find minimum and maximum length, bound *m by those */
  301. for (j = 1; (c[j] == 0) && (j <= BMAX); j++)
  302. continue;
  303. k = j; /* minimum code length */
  304. for (i = BMAX; (c[i] == 0) && i; i--)
  305. continue;
  306. g = i; /* maximum code length */
  307. *m = (*m < j) ? j : ((*m > i) ? i : *m);
  308. /* Adjust last length count to fill out codes, if needed */
  309. for (y = 1 << j; j < i; j++, y <<= 1) {
  310. y -= c[j];
  311. if (y < 0)
  312. return 2; /* bad input: more codes than bits */
  313. }
  314. y -= c[i];
  315. if (y < 0)
  316. return 2;
  317. c[i] += y;
  318. /* Generate starting offsets into the value table for each length */
  319. x[1] = j = 0;
  320. p = c + 1;
  321. xp = x + 2;
  322. while (--i) { /* note that i == g from above */
  323. j += *p++;
  324. *xp++ = j;
  325. }
  326. /* Make a table of values in order of bit lengths */
  327. p = (unsigned *) b;
  328. i = 0;
  329. do {
  330. j = *p++;
  331. if (j != 0) {
  332. v[x[j]++] = i;
  333. }
  334. } while (++i < n);
  335. /* Generate the Huffman codes and for each, make the table entries */
  336. x[0] = i = 0; /* first Huffman code is zero */
  337. p = v; /* grab values in bit order */
  338. htl = -1; /* no tables yet--level -1 */
  339. w = ws[0] = 0; /* bits decoded */
  340. u[0] = NULL; /* just to keep compilers happy */
  341. q = NULL; /* ditto */
  342. z = 0; /* ditto */
  343. /* go through the bit lengths (k already is bits in shortest code) */
  344. for (; k <= g; k++) {
  345. a = c[k];
  346. while (a--) {
  347. /* here i is the Huffman code of length k bits for value *p */
  348. /* make tables up to required level */
  349. while (k > ws[htl + 1]) {
  350. w = ws[++htl];
  351. /* compute minimum size table less than or equal to *m bits */
  352. z = g - w;
  353. z = z > *m ? *m : z; /* upper limit on table size */
  354. j = k - w;
  355. f = 1 << j;
  356. if (f > a + 1) { /* try a k-w bit table */
  357. /* too few codes for k-w bit table */
  358. f -= a + 1; /* deduct codes from patterns left */
  359. xp = c + k;
  360. while (++j < z) { /* try smaller tables up to z bits */
  361. f <<= 1;
  362. if (f <= *++xp) {
  363. break; /* enough codes to use up j bits */
  364. }
  365. f -= *xp; /* else deduct codes from patterns */
  366. }
  367. }
  368. j = (w + j > eob_len && w < eob_len) ? eob_len - w : j; /* make EOB code end at table */
  369. z = 1 << j; /* table entries for j-bit table */
  370. ws[htl+1] = w + j; /* set bits decoded in stack */
  371. /* allocate and link in new table */
  372. q = xzalloc((z + 1) * sizeof(huft_t));
  373. *t = q + 1; /* link to list for huft_free() */
  374. t = &(q->v.t);
  375. u[htl] = ++q; /* table starts after link */
  376. /* connect to last table, if there is one */
  377. if (htl) {
  378. x[htl] = i; /* save pattern for backing up */
  379. r.b = (unsigned char) (w - ws[htl - 1]); /* bits to dump before this table */
  380. r.e = (unsigned char) (16 + j); /* bits in this table */
  381. r.v.t = q; /* pointer to this table */
  382. j = (i & ((1 << w) - 1)) >> ws[htl - 1];
  383. u[htl - 1][j] = r; /* connect to last table */
  384. }
  385. }
  386. /* set up table entry in r */
  387. r.b = (unsigned char) (k - w);
  388. if (p >= v + n) {
  389. r.e = 99; /* out of values--invalid code */
  390. } else if (*p < s) {
  391. r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */
  392. r.v.n = (unsigned short) (*p++); /* simple code is just the value */
  393. } else {
  394. r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */
  395. r.v.n = d[*p++ - s];
  396. }
  397. /* fill code-like entries with r */
  398. f = 1 << (k - w);
  399. for (j = i >> w; j < z; j += f) {
  400. q[j] = r;
  401. }
  402. /* backwards increment the k-bit code i */
  403. for (j = 1 << (k - 1); i & j; j >>= 1) {
  404. i ^= j;
  405. }
  406. i ^= j;
  407. /* backup over finished tables */
  408. while ((i & ((1 << w) - 1)) != x[htl]) {
  409. w = ws[--htl];
  410. }
  411. }
  412. }
  413. /* return actual size of base table */
  414. *m = ws[1];
  415. /* Return 1 if we were given an incomplete table */
  416. return y != 0 && g != 1;
  417. }
  418. /*
  419. * inflate (decompress) the codes in a deflated (compressed) block.
  420. * Return an error code or zero if it all goes ok.
  421. *
  422. * tl, td: literal/length and distance decoder tables
  423. * bl, bd: number of bits decoded by tl[] and td[]
  424. */
  425. /* called once from inflate_block */
  426. /* map formerly local static variables to globals */
  427. #define ml inflate_codes_ml
  428. #define md inflate_codes_md
  429. #define bb inflate_codes_bb
  430. #define k inflate_codes_k
  431. #define w inflate_codes_w
  432. #define tl inflate_codes_tl
  433. #define td inflate_codes_td
  434. #define bl inflate_codes_bl
  435. #define bd inflate_codes_bd
  436. #define nn inflate_codes_nn
  437. #define dd inflate_codes_dd
  438. static void inflate_codes_setup(STATE_PARAM unsigned my_bl, unsigned my_bd)
  439. {
  440. bl = my_bl;
  441. bd = my_bd;
  442. /* make local copies of globals */
  443. bb = gunzip_bb; /* initialize bit buffer */
  444. k = gunzip_bk;
  445. w = gunzip_outbuf_count; /* initialize gunzip_window position */
  446. /* inflate the coded data */
  447. ml = mask_bits[bl]; /* precompute masks for speed */
  448. md = mask_bits[bd];
  449. }
  450. /* called once from inflate_get_next_window */
  451. static NOINLINE int inflate_codes(STATE_PARAM_ONLY)
  452. {
  453. unsigned e; /* table entry flag/number of extra bits */
  454. huft_t *t; /* pointer to table entry */
  455. if (resume_copy)
  456. goto do_copy;
  457. while (1) { /* do until end of block */
  458. bb = fill_bitbuffer(PASS_STATE bb, &k, bl);
  459. t = tl + ((unsigned) bb & ml);
  460. e = t->e;
  461. if (e > 16)
  462. do {
  463. if (e == 99)
  464. abort_unzip(PASS_STATE_ONLY);;
  465. bb >>= t->b;
  466. k -= t->b;
  467. e -= 16;
  468. bb = fill_bitbuffer(PASS_STATE bb, &k, e);
  469. t = t->v.t + ((unsigned) bb & mask_bits[e]);
  470. e = t->e;
  471. } while (e > 16);
  472. bb >>= t->b;
  473. k -= t->b;
  474. if (e == 16) { /* then it's a literal */
  475. gunzip_window[w++] = (unsigned char) t->v.n;
  476. if (w == GUNZIP_WSIZE) {
  477. gunzip_outbuf_count = w;
  478. //flush_gunzip_window();
  479. w = 0;
  480. return 1; // We have a block to read
  481. }
  482. } else { /* it's an EOB or a length */
  483. /* exit if end of block */
  484. if (e == 15) {
  485. break;
  486. }
  487. /* get length of block to copy */
  488. bb = fill_bitbuffer(PASS_STATE bb, &k, e);
  489. nn = t->v.n + ((unsigned) bb & mask_bits[e]);
  490. bb >>= e;
  491. k -= e;
  492. /* decode distance of block to copy */
  493. bb = fill_bitbuffer(PASS_STATE bb, &k, bd);
  494. t = td + ((unsigned) bb & md);
  495. e = t->e;
  496. if (e > 16)
  497. do {
  498. if (e == 99)
  499. abort_unzip(PASS_STATE_ONLY);
  500. bb >>= t->b;
  501. k -= t->b;
  502. e -= 16;
  503. bb = fill_bitbuffer(PASS_STATE bb, &k, e);
  504. t = t->v.t + ((unsigned) bb & mask_bits[e]);
  505. e = t->e;
  506. } while (e > 16);
  507. bb >>= t->b;
  508. k -= t->b;
  509. bb = fill_bitbuffer(PASS_STATE bb, &k, e);
  510. dd = w - t->v.n - ((unsigned) bb & mask_bits[e]);
  511. bb >>= e;
  512. k -= e;
  513. /* do the copy */
  514. do_copy:
  515. do {
  516. /* Was: nn -= (e = (e = GUNZIP_WSIZE - ((dd &= GUNZIP_WSIZE - 1) > w ? dd : w)) > nn ? nn : e); */
  517. /* Who wrote THAT?? rewritten as: */
  518. dd &= GUNZIP_WSIZE - 1;
  519. e = GUNZIP_WSIZE - (dd > w ? dd : w);
  520. if (e > nn) e = nn;
  521. nn -= e;
  522. /* copy to new buffer to prevent possible overwrite */
  523. if (w - dd >= e) { /* (this test assumes unsigned comparison) */
  524. memcpy(gunzip_window + w, gunzip_window + dd, e);
  525. w += e;
  526. dd += e;
  527. } else {
  528. /* do it slow to avoid memcpy() overlap */
  529. /* !NOMEMCPY */
  530. do {
  531. gunzip_window[w++] = gunzip_window[dd++];
  532. } while (--e);
  533. }
  534. if (w == GUNZIP_WSIZE) {
  535. gunzip_outbuf_count = w;
  536. resume_copy = (nn != 0);
  537. //flush_gunzip_window();
  538. w = 0;
  539. return 1;
  540. }
  541. } while (nn);
  542. resume_copy = 0;
  543. }
  544. }
  545. /* restore the globals from the locals */
  546. gunzip_outbuf_count = w; /* restore global gunzip_window pointer */
  547. gunzip_bb = bb; /* restore global bit buffer */
  548. gunzip_bk = k;
  549. /* normally just after call to inflate_codes, but save code by putting it here */
  550. /* free the decoding tables (tl and td), return */
  551. huft_free_all(PASS_STATE_ONLY);
  552. /* done */
  553. return 0;
  554. }
  555. #undef ml
  556. #undef md
  557. #undef bb
  558. #undef k
  559. #undef w
  560. #undef tl
  561. #undef td
  562. #undef bl
  563. #undef bd
  564. #undef nn
  565. #undef dd
  566. /* called once from inflate_block */
  567. static void inflate_stored_setup(STATE_PARAM int my_n, int my_b, int my_k)
  568. {
  569. inflate_stored_n = my_n;
  570. inflate_stored_b = my_b;
  571. inflate_stored_k = my_k;
  572. /* initialize gunzip_window position */
  573. inflate_stored_w = gunzip_outbuf_count;
  574. }
  575. /* called once from inflate_get_next_window */
  576. static int inflate_stored(STATE_PARAM_ONLY)
  577. {
  578. /* read and output the compressed data */
  579. while (inflate_stored_n--) {
  580. inflate_stored_b = fill_bitbuffer(PASS_STATE inflate_stored_b, &inflate_stored_k, 8);
  581. gunzip_window[inflate_stored_w++] = (unsigned char) inflate_stored_b;
  582. if (inflate_stored_w == GUNZIP_WSIZE) {
  583. gunzip_outbuf_count = inflate_stored_w;
  584. //flush_gunzip_window();
  585. inflate_stored_w = 0;
  586. inflate_stored_b >>= 8;
  587. inflate_stored_k -= 8;
  588. return 1; /* We have a block */
  589. }
  590. inflate_stored_b >>= 8;
  591. inflate_stored_k -= 8;
  592. }
  593. /* restore the globals from the locals */
  594. gunzip_outbuf_count = inflate_stored_w; /* restore global gunzip_window pointer */
  595. gunzip_bb = inflate_stored_b; /* restore global bit buffer */
  596. gunzip_bk = inflate_stored_k;
  597. return 0; /* Finished */
  598. }
  599. /*
  600. * decompress an inflated block
  601. * e: last block flag
  602. *
  603. * GLOBAL VARIABLES: bb, kk,
  604. */
  605. /* Return values: -1 = inflate_stored, -2 = inflate_codes */
  606. /* One callsite in inflate_get_next_window */
  607. static int inflate_block(STATE_PARAM smallint *e)
  608. {
  609. unsigned ll[286 + 30]; /* literal/length and distance code lengths */
  610. unsigned t; /* block type */
  611. unsigned b; /* bit buffer */
  612. unsigned k; /* number of bits in bit buffer */
  613. /* make local bit buffer */
  614. b = gunzip_bb;
  615. k = gunzip_bk;
  616. /* read in last block bit */
  617. b = fill_bitbuffer(PASS_STATE b, &k, 1);
  618. *e = b & 1;
  619. b >>= 1;
  620. k -= 1;
  621. /* read in block type */
  622. b = fill_bitbuffer(PASS_STATE b, &k, 2);
  623. t = (unsigned) b & 3;
  624. b >>= 2;
  625. k -= 2;
  626. /* restore the global bit buffer */
  627. gunzip_bb = b;
  628. gunzip_bk = k;
  629. /* Do we see block type 1 often? Yes!
  630. * TODO: fix performance problem (see below) */
  631. //bb_error_msg("blktype %d", t);
  632. /* inflate that block type */
  633. switch (t) {
  634. case 0: /* Inflate stored */
  635. {
  636. unsigned n; /* number of bytes in block */
  637. unsigned b_stored; /* bit buffer */
  638. unsigned k_stored; /* number of bits in bit buffer */
  639. /* make local copies of globals */
  640. b_stored = gunzip_bb; /* initialize bit buffer */
  641. k_stored = gunzip_bk;
  642. /* go to byte boundary */
  643. n = k_stored & 7;
  644. b_stored >>= n;
  645. k_stored -= n;
  646. /* get the length and its complement */
  647. b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
  648. n = ((unsigned) b_stored & 0xffff);
  649. b_stored >>= 16;
  650. k_stored -= 16;
  651. b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
  652. if (n != (unsigned) ((~b_stored) & 0xffff)) {
  653. abort_unzip(PASS_STATE_ONLY); /* error in compressed data */
  654. }
  655. b_stored >>= 16;
  656. k_stored -= 16;
  657. inflate_stored_setup(PASS_STATE n, b_stored, k_stored);
  658. return -1;
  659. }
  660. case 1:
  661. /* Inflate fixed
  662. * decompress an inflated type 1 (fixed Huffman codes) block. We should
  663. * either replace this with a custom decoder, or at least precompute the
  664. * Huffman tables. TODO */
  665. {
  666. int i; /* temporary variable */
  667. unsigned bl; /* lookup bits for tl */
  668. unsigned bd; /* lookup bits for td */
  669. /* gcc 4.2.1 is too dumb to reuse stackspace. Moved up... */
  670. //unsigned ll[288]; /* length list for huft_build */
  671. /* set up literal table */
  672. for (i = 0; i < 144; i++)
  673. ll[i] = 8;
  674. for (; i < 256; i++)
  675. ll[i] = 9;
  676. for (; i < 280; i++)
  677. ll[i] = 7;
  678. for (; i < 288; i++) /* make a complete, but wrong code set */
  679. ll[i] = 8;
  680. bl = 7;
  681. huft_build(ll, 288, 257, cplens, cplext, &inflate_codes_tl, &bl);
  682. /* huft_build() never return nonzero - we use known data */
  683. /* set up distance table */
  684. for (i = 0; i < 30; i++) /* make an incomplete code set */
  685. ll[i] = 5;
  686. bd = 5;
  687. huft_build(ll, 30, 0, cpdist, cpdext, &inflate_codes_td, &bd);
  688. /* set up data for inflate_codes() */
  689. inflate_codes_setup(PASS_STATE bl, bd);
  690. /* huft_free code moved into inflate_codes */
  691. return -2;
  692. }
  693. case 2: /* Inflate dynamic */
  694. {
  695. enum { dbits = 6 }; /* bits in base distance lookup table */
  696. enum { lbits = 9 }; /* bits in base literal/length lookup table */
  697. huft_t *td; /* distance code table */
  698. unsigned i; /* temporary variables */
  699. unsigned j;
  700. unsigned l; /* last length */
  701. unsigned m; /* mask for bit lengths table */
  702. unsigned n; /* number of lengths to get */
  703. unsigned bl; /* lookup bits for tl */
  704. unsigned bd; /* lookup bits for td */
  705. unsigned nb; /* number of bit length codes */
  706. unsigned nl; /* number of literal/length codes */
  707. unsigned nd; /* number of distance codes */
  708. //unsigned ll[286 + 30];/* literal/length and distance code lengths */
  709. unsigned b_dynamic; /* bit buffer */
  710. unsigned k_dynamic; /* number of bits in bit buffer */
  711. /* make local bit buffer */
  712. b_dynamic = gunzip_bb;
  713. k_dynamic = gunzip_bk;
  714. /* read in table lengths */
  715. b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5);
  716. nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */
  717. b_dynamic >>= 5;
  718. k_dynamic -= 5;
  719. b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5);
  720. nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */
  721. b_dynamic >>= 5;
  722. k_dynamic -= 5;
  723. b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 4);
  724. nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */
  725. b_dynamic >>= 4;
  726. k_dynamic -= 4;
  727. if (nl > 286 || nd > 30)
  728. abort_unzip(PASS_STATE_ONLY); /* bad lengths */
  729. /* read in bit-length-code lengths */
  730. for (j = 0; j < nb; j++) {
  731. b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3);
  732. ll[border[j]] = (unsigned) b_dynamic & 7;
  733. b_dynamic >>= 3;
  734. k_dynamic -= 3;
  735. }
  736. for (; j < 19; j++)
  737. ll[border[j]] = 0;
  738. /* build decoding table for trees - single level, 7 bit lookup */
  739. bl = 7;
  740. i = huft_build(ll, 19, 19, NULL, NULL, &inflate_codes_tl, &bl);
  741. if (i != 0) {
  742. abort_unzip(PASS_STATE_ONLY); //return i; /* incomplete code set */
  743. }
  744. /* read in literal and distance code lengths */
  745. n = nl + nd;
  746. m = mask_bits[bl];
  747. i = l = 0;
  748. while ((unsigned) i < n) {
  749. b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, (unsigned)bl);
  750. td = inflate_codes_tl + ((unsigned) b_dynamic & m);
  751. j = td->b;
  752. b_dynamic >>= j;
  753. k_dynamic -= j;
  754. j = td->v.n;
  755. if (j < 16) { /* length of code in bits (0..15) */
  756. ll[i++] = l = j; /* save last length in l */
  757. } else if (j == 16) { /* repeat last length 3 to 6 times */
  758. b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 2);
  759. j = 3 + ((unsigned) b_dynamic & 3);
  760. b_dynamic >>= 2;
  761. k_dynamic -= 2;
  762. if ((unsigned) i + j > n) {
  763. abort_unzip(PASS_STATE_ONLY); //return 1;
  764. }
  765. while (j--) {
  766. ll[i++] = l;
  767. }
  768. } else if (j == 17) { /* 3 to 10 zero length codes */
  769. b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3);
  770. j = 3 + ((unsigned) b_dynamic & 7);
  771. b_dynamic >>= 3;
  772. k_dynamic -= 3;
  773. if ((unsigned) i + j > n) {
  774. abort_unzip(PASS_STATE_ONLY); //return 1;
  775. }
  776. while (j--) {
  777. ll[i++] = 0;
  778. }
  779. l = 0;
  780. } else { /* j == 18: 11 to 138 zero length codes */
  781. b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 7);
  782. j = 11 + ((unsigned) b_dynamic & 0x7f);
  783. b_dynamic >>= 7;
  784. k_dynamic -= 7;
  785. if ((unsigned) i + j > n) {
  786. abort_unzip(PASS_STATE_ONLY); //return 1;
  787. }
  788. while (j--) {
  789. ll[i++] = 0;
  790. }
  791. l = 0;
  792. }
  793. }
  794. /* free decoding table for trees */
  795. huft_free(inflate_codes_tl);
  796. /* restore the global bit buffer */
  797. gunzip_bb = b_dynamic;
  798. gunzip_bk = k_dynamic;
  799. /* build the decoding tables for literal/length and distance codes */
  800. bl = lbits;
  801. i = huft_build(ll, nl, 257, cplens, cplext, &inflate_codes_tl, &bl);
  802. if (i != 0)
  803. abort_unzip(PASS_STATE_ONLY);
  804. bd = dbits;
  805. i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &inflate_codes_td, &bd);
  806. if (i != 0)
  807. abort_unzip(PASS_STATE_ONLY);
  808. /* set up data for inflate_codes() */
  809. inflate_codes_setup(PASS_STATE bl, bd);
  810. /* huft_free code moved into inflate_codes */
  811. return -2;
  812. }
  813. default:
  814. abort_unzip(PASS_STATE_ONLY);
  815. }
  816. }
  817. /* Two callsites, both in inflate_get_next_window */
  818. static void calculate_gunzip_crc(STATE_PARAM_ONLY)
  819. {
  820. unsigned n;
  821. for (n = 0; n < gunzip_outbuf_count; n++) {
  822. gunzip_crc = gunzip_crc_table[((int) gunzip_crc ^ (gunzip_window[n])) & 0xff] ^ (gunzip_crc >> 8);
  823. }
  824. gunzip_bytes_out += gunzip_outbuf_count;
  825. }
  826. /* One callsite in inflate_unzip_internal */
  827. static int inflate_get_next_window(STATE_PARAM_ONLY)
  828. {
  829. gunzip_outbuf_count = 0;
  830. while (1) {
  831. int ret;
  832. if (need_another_block) {
  833. if (end_reached) {
  834. calculate_gunzip_crc(PASS_STATE_ONLY);
  835. end_reached = 0;
  836. /* NB: need_another_block is still set */
  837. return 0; /* Last block */
  838. }
  839. method = inflate_block(PASS_STATE &end_reached);
  840. need_another_block = 0;
  841. }
  842. switch (method) {
  843. case -1:
  844. ret = inflate_stored(PASS_STATE_ONLY);
  845. break;
  846. case -2:
  847. ret = inflate_codes(PASS_STATE_ONLY);
  848. break;
  849. default: /* cannot happen */
  850. abort_unzip(PASS_STATE_ONLY);
  851. }
  852. if (ret == 1) {
  853. calculate_gunzip_crc(PASS_STATE_ONLY);
  854. return 1; /* more data left */
  855. }
  856. need_another_block = 1; /* end of that block */
  857. }
  858. /* Doesnt get here */
  859. }
  860. /* Called from unpack_gz_stream() and inflate_unzip() */
  861. static IF_DESKTOP(long long) int
  862. inflate_unzip_internal(STATE_PARAM int in, int out)
  863. {
  864. IF_DESKTOP(long long) int n = 0;
  865. ssize_t nwrote;
  866. /* Allocate all global buffers (for DYN_ALLOC option) */
  867. gunzip_window = xmalloc(GUNZIP_WSIZE);
  868. gunzip_outbuf_count = 0;
  869. gunzip_bytes_out = 0;
  870. gunzip_src_fd = in;
  871. /* (re) initialize state */
  872. method = -1;
  873. need_another_block = 1;
  874. resume_copy = 0;
  875. gunzip_bk = 0;
  876. gunzip_bb = 0;
  877. /* Create the crc table */
  878. gunzip_crc_table = crc32_filltable(NULL, 0);
  879. gunzip_crc = ~0;
  880. error_msg = "corrupted data";
  881. if (setjmp(error_jmp)) {
  882. /* Error from deep inside zip machinery */
  883. n = -1;
  884. goto ret;
  885. }
  886. while (1) {
  887. int r = inflate_get_next_window(PASS_STATE_ONLY);
  888. nwrote = full_write(out, gunzip_window, gunzip_outbuf_count);
  889. if (nwrote != (ssize_t)gunzip_outbuf_count) {
  890. bb_perror_msg("write");
  891. n = -1;
  892. goto ret;
  893. }
  894. IF_DESKTOP(n += nwrote;)
  895. if (r == 0) break;
  896. }
  897. /* Store unused bytes in a global buffer so calling applets can access it */
  898. if (gunzip_bk >= 8) {
  899. /* Undo too much lookahead. The next read will be byte aligned
  900. * so we can discard unused bits in the last meaningful byte. */
  901. bytebuffer_offset--;
  902. bytebuffer[bytebuffer_offset] = gunzip_bb & 0xff;
  903. gunzip_bb >>= 8;
  904. gunzip_bk -= 8;
  905. }
  906. ret:
  907. /* Cleanup */
  908. free(gunzip_window);
  909. free(gunzip_crc_table);
  910. return n;
  911. }
  912. /* External entry points */
  913. /* For unzip */
  914. IF_DESKTOP(long long) int FAST_FUNC
  915. inflate_unzip(inflate_unzip_result *res, off_t compr_size, int in, int out)
  916. {
  917. IF_DESKTOP(long long) int n;
  918. DECLARE_STATE;
  919. ALLOC_STATE;
  920. to_read = compr_size;
  921. // bytebuffer_max = 0x8000;
  922. bytebuffer_offset = 4;
  923. bytebuffer = xmalloc(bytebuffer_max);
  924. n = inflate_unzip_internal(PASS_STATE in, out);
  925. free(bytebuffer);
  926. res->crc = gunzip_crc;
  927. res->bytes_out = gunzip_bytes_out;
  928. DEALLOC_STATE;
  929. return n;
  930. }
  931. /* For gunzip */
  932. /* helpers first */
  933. /* Top up the input buffer with at least n bytes. */
  934. static int top_up(STATE_PARAM unsigned n)
  935. {
  936. int count = bytebuffer_size - bytebuffer_offset;
  937. if (count < (int)n) {
  938. memmove(bytebuffer, &bytebuffer[bytebuffer_offset], count);
  939. bytebuffer_offset = 0;
  940. bytebuffer_size = full_read(gunzip_src_fd, &bytebuffer[count], bytebuffer_max - count);
  941. if ((int)bytebuffer_size < 0) {
  942. bb_error_msg("read error");
  943. return 0;
  944. }
  945. bytebuffer_size += count;
  946. if (bytebuffer_size < n)
  947. return 0;
  948. }
  949. return 1;
  950. }
  951. static uint16_t buffer_read_le_u16(STATE_PARAM_ONLY)
  952. {
  953. uint16_t res;
  954. #if BB_LITTLE_ENDIAN
  955. move_from_unaligned16(res, &bytebuffer[bytebuffer_offset]);
  956. #else
  957. res = bytebuffer[bytebuffer_offset];
  958. res |= bytebuffer[bytebuffer_offset + 1] << 8;
  959. #endif
  960. bytebuffer_offset += 2;
  961. return res;
  962. }
  963. static uint32_t buffer_read_le_u32(STATE_PARAM_ONLY)
  964. {
  965. uint32_t res;
  966. #if BB_LITTLE_ENDIAN
  967. move_from_unaligned32(res, &bytebuffer[bytebuffer_offset]);
  968. #else
  969. res = bytebuffer[bytebuffer_offset];
  970. res |= bytebuffer[bytebuffer_offset + 1] << 8;
  971. res |= bytebuffer[bytebuffer_offset + 2] << 16;
  972. res |= bytebuffer[bytebuffer_offset + 3] << 24;
  973. #endif
  974. bytebuffer_offset += 4;
  975. return res;
  976. }
  977. static int check_header_gzip(STATE_PARAM unpack_info_t *info)
  978. {
  979. union {
  980. unsigned char raw[8];
  981. struct {
  982. uint8_t gz_method;
  983. uint8_t flags;
  984. uint32_t mtime;
  985. uint8_t xtra_flags_UNUSED;
  986. uint8_t os_flags_UNUSED;
  987. } PACKED formatted;
  988. } header;
  989. struct BUG_header {
  990. char BUG_header[sizeof(header) == 8 ? 1 : -1];
  991. };
  992. /*
  993. * Rewind bytebuffer. We use the beginning because the header has 8
  994. * bytes, leaving enough for unwinding afterwards.
  995. */
  996. bytebuffer_size -= bytebuffer_offset;
  997. memmove(bytebuffer, &bytebuffer[bytebuffer_offset], bytebuffer_size);
  998. bytebuffer_offset = 0;
  999. if (!top_up(PASS_STATE 8))
  1000. return 0;
  1001. memcpy(header.raw, &bytebuffer[bytebuffer_offset], 8);
  1002. bytebuffer_offset += 8;
  1003. /* Check the compression method */
  1004. if (header.formatted.gz_method != 8) {
  1005. return 0;
  1006. }
  1007. if (header.formatted.flags & 0x04) {
  1008. /* bit 2 set: extra field present */
  1009. unsigned extra_short;
  1010. if (!top_up(PASS_STATE 2))
  1011. return 0;
  1012. extra_short = buffer_read_le_u16(PASS_STATE_ONLY);
  1013. if (!top_up(PASS_STATE extra_short))
  1014. return 0;
  1015. /* Ignore extra field */
  1016. bytebuffer_offset += extra_short;
  1017. }
  1018. /* Discard original name and file comment if any */
  1019. /* bit 3 set: original file name present */
  1020. /* bit 4 set: file comment present */
  1021. if (header.formatted.flags & 0x18) {
  1022. while (1) {
  1023. do {
  1024. if (!top_up(PASS_STATE 1))
  1025. return 0;
  1026. } while (bytebuffer[bytebuffer_offset++] != 0);
  1027. if ((header.formatted.flags & 0x18) != 0x18)
  1028. break;
  1029. header.formatted.flags &= ~0x18;
  1030. }
  1031. }
  1032. if (info)
  1033. info->mtime = SWAP_LE32(header.formatted.mtime);
  1034. /* Read the header checksum */
  1035. if (header.formatted.flags & 0x02) {
  1036. if (!top_up(PASS_STATE 2))
  1037. return 0;
  1038. bytebuffer_offset += 2;
  1039. }
  1040. return 1;
  1041. }
  1042. IF_DESKTOP(long long) int FAST_FUNC
  1043. unpack_gz_stream_with_info(int in, int out, unpack_info_t *info)
  1044. {
  1045. uint32_t v32;
  1046. IF_DESKTOP(long long) int n;
  1047. DECLARE_STATE;
  1048. n = 0;
  1049. ALLOC_STATE;
  1050. to_read = -1;
  1051. // bytebuffer_max = 0x8000;
  1052. bytebuffer = xmalloc(bytebuffer_max);
  1053. gunzip_src_fd = in;
  1054. again:
  1055. if (!check_header_gzip(PASS_STATE info)) {
  1056. bb_error_msg("corrupted data");
  1057. n = -1;
  1058. goto ret;
  1059. }
  1060. n += inflate_unzip_internal(PASS_STATE in, out);
  1061. if (n < 0)
  1062. goto ret;
  1063. if (!top_up(PASS_STATE 8)) {
  1064. bb_error_msg("corrupted data");
  1065. n = -1;
  1066. goto ret;
  1067. }
  1068. /* Validate decompression - crc */
  1069. v32 = buffer_read_le_u32(PASS_STATE_ONLY);
  1070. if ((~gunzip_crc) != v32) {
  1071. bb_error_msg("crc error");
  1072. n = -1;
  1073. goto ret;
  1074. }
  1075. /* Validate decompression - size */
  1076. v32 = buffer_read_le_u32(PASS_STATE_ONLY);
  1077. if ((uint32_t)gunzip_bytes_out != v32) {
  1078. bb_error_msg("incorrect length");
  1079. n = -1;
  1080. }
  1081. if (!top_up(PASS_STATE 2))
  1082. goto ret; /* EOF */
  1083. if (bytebuffer[bytebuffer_offset] == 0x1f
  1084. && bytebuffer[bytebuffer_offset + 1] == 0x8b
  1085. ) {
  1086. bytebuffer_offset += 2;
  1087. goto again;
  1088. }
  1089. /* GNU gzip says: */
  1090. /*bb_error_msg("decompression OK, trailing garbage ignored");*/
  1091. ret:
  1092. free(bytebuffer);
  1093. DEALLOC_STATE;
  1094. return n;
  1095. }
  1096. IF_DESKTOP(long long) int FAST_FUNC
  1097. unpack_gz_stream(int in, int out)
  1098. {
  1099. return unpack_gz_stream_with_info(in, out, NULL);
  1100. }