3
0

decompress_gunzip.c 35 KB

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