decompress_unzip.c 34 KB

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