3
0

decompress_gunzip.c 35 KB

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