decompress_gunzip.c 37 KB

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