decompress_gunzip.c 37 KB

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