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
31
32
33
34
35#include "libbb.h"
36#include "bb_archive.h"
37
38typedef struct huft_t {
39 unsigned char e;
40 unsigned char b;
41 union {
42 unsigned short n;
43 struct huft_t *t;
44 } v;
45} huft_t;
46
47enum {
48
49
50 GUNZIP_WSIZE = 0x8000,
51
52 BMAX = 16,
53 N_MAX = 288,
54};
55
56
57
58
59
60
61
62
63
64
65#define STATE_IN_BSS 0
66#define STATE_IN_MALLOC 1
67
68
69typedef struct state_t {
70 off_t gunzip_bytes_out;
71 uint32_t gunzip_crc;
72
73 int gunzip_src_fd;
74 unsigned gunzip_outbuf_count;
75
76 unsigned char *gunzip_window;
77
78 uint32_t *gunzip_crc_table;
79
80
81 unsigned gunzip_bb;
82 unsigned char gunzip_bk;
83
84
85 unsigned char *bytebuffer;
86 off_t to_read;
87
88 unsigned bytebuffer_offset;
89 unsigned bytebuffer_size;
90
91
92 unsigned inflate_codes_ml;
93 unsigned inflate_codes_md;
94 unsigned inflate_codes_bb;
95 unsigned inflate_codes_k;
96 unsigned inflate_codes_w;
97 huft_t *inflate_codes_tl;
98 huft_t *inflate_codes_td;
99 unsigned inflate_codes_bl;
100 unsigned inflate_codes_bd;
101 unsigned inflate_codes_nn;
102 unsigned inflate_codes_dd;
103
104 smallint resume_copy;
105
106
107 smallint method;
108 smallint need_another_block;
109 smallint end_reached;
110
111
112 unsigned inflate_stored_n;
113 unsigned inflate_stored_b;
114 unsigned inflate_stored_k;
115 unsigned inflate_stored_w;
116
117 const char *error_msg;
118 jmp_buf error_jmp;
119} state_t;
120#define gunzip_bytes_out (S()gunzip_bytes_out )
121#define gunzip_crc (S()gunzip_crc )
122#define gunzip_src_fd (S()gunzip_src_fd )
123#define gunzip_outbuf_count (S()gunzip_outbuf_count)
124#define gunzip_window (S()gunzip_window )
125#define gunzip_crc_table (S()gunzip_crc_table )
126#define gunzip_bb (S()gunzip_bb )
127#define gunzip_bk (S()gunzip_bk )
128#define to_read (S()to_read )
129
130
131#define bytebuffer_max 0x4000
132#define bytebuffer (S()bytebuffer )
133#define bytebuffer_offset (S()bytebuffer_offset )
134#define bytebuffer_size (S()bytebuffer_size )
135#define inflate_codes_ml (S()inflate_codes_ml )
136#define inflate_codes_md (S()inflate_codes_md )
137#define inflate_codes_bb (S()inflate_codes_bb )
138#define inflate_codes_k (S()inflate_codes_k )
139#define inflate_codes_w (S()inflate_codes_w )
140#define inflate_codes_tl (S()inflate_codes_tl )
141#define inflate_codes_td (S()inflate_codes_td )
142#define inflate_codes_bl (S()inflate_codes_bl )
143#define inflate_codes_bd (S()inflate_codes_bd )
144#define inflate_codes_nn (S()inflate_codes_nn )
145#define inflate_codes_dd (S()inflate_codes_dd )
146#define resume_copy (S()resume_copy )
147#define method (S()method )
148#define need_another_block (S()need_another_block )
149#define end_reached (S()end_reached )
150#define inflate_stored_n (S()inflate_stored_n )
151#define inflate_stored_b (S()inflate_stored_b )
152#define inflate_stored_k (S()inflate_stored_k )
153#define inflate_stored_w (S()inflate_stored_w )
154#define error_msg (S()error_msg )
155#define error_jmp (S()error_jmp )
156
157
158#if STATE_IN_BSS
159#define DECLARE_STATE
160#define ALLOC_STATE
161#define DEALLOC_STATE ((void)0)
162#define S() state.
163#define PASS_STATE
164#define PASS_STATE_ONLY
165#define STATE_PARAM
166#define STATE_PARAM_ONLY void
167static state_t state;
168#endif
169
170#if STATE_IN_MALLOC
171#define DECLARE_STATE state_t *state
172#define ALLOC_STATE (state = xzalloc(sizeof(*state)))
173#define DEALLOC_STATE free(state)
174#define S() state->
175#define PASS_STATE state,
176#define PASS_STATE_ONLY state
177#define STATE_PARAM state_t *state,
178#define STATE_PARAM_ONLY state_t *state
179#endif
180
181
182static const uint16_t mask_bits[] ALIGN2 = {
183 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
184 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
185};
186
187
188static const uint16_t cplens[] ALIGN2 = {
189 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
190 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
191};
192
193
194
195static const uint8_t cplext[] ALIGN1 = {
196 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,
197 5, 5, 5, 0, 99, 99
198};
199
200
201static const uint16_t cpdist[] ALIGN2 = {
202 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
203 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
204};
205
206
207static const uint8_t cpdext[] ALIGN1 = {
208 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
209 11, 11, 12, 12, 13, 13
210};
211
212
213
214static const uint8_t border[] ALIGN1 = {
215 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
216};
217
218
219
220
221
222
223
224
225static void huft_free(huft_t *p)
226{
227 huft_t *q;
228
229
230 while (p) {
231 q = (--p)->v.t;
232 free(p);
233 p = q;
234 }
235}
236
237static void huft_free_all(STATE_PARAM_ONLY)
238{
239 huft_free(inflate_codes_tl);
240 huft_free(inflate_codes_td);
241 inflate_codes_tl = NULL;
242 inflate_codes_td = NULL;
243}
244
245static void abort_unzip(STATE_PARAM_ONLY) NORETURN;
246static void abort_unzip(STATE_PARAM_ONLY)
247{
248 huft_free_all(PASS_STATE_ONLY);
249 longjmp(error_jmp, 1);
250}
251
252static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current, const unsigned required)
253{
254 while (*current < required) {
255 if (bytebuffer_offset >= bytebuffer_size) {
256 unsigned sz = bytebuffer_max - 4;
257 if (to_read >= 0 && to_read < sz)
258 sz = to_read;
259
260
261 bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], sz);
262 if ((int)bytebuffer_size < 1) {
263 error_msg = "unexpected end of file";
264 abort_unzip(PASS_STATE_ONLY);
265 }
266 if (to_read >= 0)
267 to_read -= bytebuffer_size;
268 bytebuffer_size += 4;
269 bytebuffer_offset = 4;
270 }
271 bitbuffer |= ((unsigned) bytebuffer[bytebuffer_offset]) << *current;
272 bytebuffer_offset++;
273 *current += 8;
274 }
275 return bitbuffer;
276}
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293static int huft_build(const unsigned *b, const unsigned n,
294 const unsigned s, const unsigned short *d,
295 const unsigned char *e, huft_t **t, unsigned *m)
296{
297 unsigned a;
298 unsigned c[BMAX + 1];
299 unsigned eob_len;
300 unsigned f;
301 int g;
302 int htl;
303 unsigned i;
304 unsigned j;
305 int k;
306 const unsigned *p;
307 huft_t *q;
308 huft_t r;
309 huft_t *u[BMAX];
310 unsigned v[N_MAX + 1];
311 int ws[BMAX + 1];
312 int w;
313 unsigned x[BMAX + 1];
314 unsigned *xp;
315 int y;
316 unsigned z;
317
318
319 eob_len = n > 256 ? b[256] : BMAX;
320
321 *t = NULL;
322
323
324 memset(c, 0, sizeof(c));
325 p = b;
326 i = n;
327 do {
328 c[*p]++;
329 p++;
330 } while (--i);
331 if (c[0] == n) {
332 q = xzalloc(3 * sizeof(*q));
333
334 q[1].e = 99;
335 q[1].b = 1;
336 q[2].e = 99;
337 q[2].b = 1;
338 *t = q + 1;
339 *m = 1;
340 return 0;
341 }
342
343
344 for (j = 1; (j <= BMAX) && (c[j] == 0); j++)
345 continue;
346 k = j;
347 for (i = BMAX; (c[i] == 0) && i; i--)
348 continue;
349 g = i;
350 *m = (*m < j) ? j : ((*m > i) ? i : *m);
351
352
353 for (y = 1 << j; j < i; j++, y <<= 1) {
354 y -= c[j];
355 if (y < 0)
356 return 2;
357 }
358 y -= c[i];
359 if (y < 0)
360 return 2;
361 c[i] += y;
362
363
364 x[1] = j = 0;
365 p = c + 1;
366 xp = x + 2;
367 while (--i) {
368 j += *p++;
369 *xp++ = j;
370 }
371
372
373
374
375
376 memset(v, 0xff, sizeof(v));
377 p = b;
378 i = 0;
379 do {
380 j = *p++;
381 if (j != 0) {
382 v[x[j]++] = i;
383 }
384 } while (++i < n);
385
386
387 x[0] = i = 0;
388 p = v;
389 htl = -1;
390 w = ws[0] = 0;
391 u[0] = NULL;
392 q = NULL;
393 z = 0;
394
395
396 for (; k <= g; k++) {
397 a = c[k];
398 while (a--) {
399
400
401 while (k > ws[htl + 1]) {
402 w = ws[++htl];
403
404
405 z = g - w;
406 z = z > *m ? *m : z;
407 j = k - w;
408 f = 1 << j;
409 if (f > a + 1) {
410
411 f -= a + 1;
412 xp = c + k;
413 while (++j < z) {
414 f <<= 1;
415 if (f <= *++xp) {
416 break;
417 }
418 f -= *xp;
419 }
420 }
421 j = (w + j > eob_len && w < eob_len) ? eob_len - w : j;
422 z = 1 << j;
423 ws[htl+1] = w + j;
424
425
426 q = xzalloc((z + 1) * sizeof(huft_t));
427 *t = q + 1;
428 t = &(q->v.t);
429 u[htl] = ++q;
430
431
432 if (htl) {
433 x[htl] = i;
434 r.b = (unsigned char) (w - ws[htl - 1]);
435 r.e = (unsigned char) (16 + j);
436 r.v.t = q;
437 j = (i & ((1 << w) - 1)) >> ws[htl - 1];
438 u[htl - 1][j] = r;
439 }
440 }
441
442
443 r.b = (unsigned char) (k - w);
444 if (
445 *p == UINT_MAX
446 ) {
447 r.e = 99;
448 } else if (*p < s) {
449 r.e = (unsigned char) (*p < 256 ? 16 : 15);
450 r.v.n = (unsigned short) (*p++);
451 } else {
452 r.e = (unsigned char) e[*p - s];
453 r.v.n = d[*p++ - s];
454 }
455
456
457 f = 1 << (k - w);
458 for (j = i >> w; j < z; j += f) {
459 q[j] = r;
460 }
461
462
463 for (j = 1 << (k - 1); i & j; j >>= 1) {
464 i ^= j;
465 }
466 i ^= j;
467
468
469 while ((i & ((1 << w) - 1)) != x[htl]) {
470 w = ws[--htl];
471 }
472 }
473 }
474
475
476 *m = ws[1];
477
478
479 return y != 0 && g != 1;
480}
481
482
483
484
485
486
487
488
489
490
491
492
493#define ml inflate_codes_ml
494#define md inflate_codes_md
495#define bb inflate_codes_bb
496#define k inflate_codes_k
497#define w inflate_codes_w
498#define tl inflate_codes_tl
499#define td inflate_codes_td
500#define bl inflate_codes_bl
501#define bd inflate_codes_bd
502#define nn inflate_codes_nn
503#define dd inflate_codes_dd
504static void inflate_codes_setup(STATE_PARAM unsigned my_bl, unsigned my_bd)
505{
506 bl = my_bl;
507 bd = my_bd;
508
509 bb = gunzip_bb;
510 k = gunzip_bk;
511 w = gunzip_outbuf_count;
512
513 ml = mask_bits[bl];
514 md = mask_bits[bd];
515}
516
517static NOINLINE int inflate_codes(STATE_PARAM_ONLY)
518{
519 unsigned e;
520 huft_t *t;
521
522 if (resume_copy)
523 goto do_copy;
524
525 while (1) {
526 bb = fill_bitbuffer(PASS_STATE bb, &k, bl);
527 t = tl + ((unsigned) bb & ml);
528 e = t->e;
529 if (e > 16)
530 do {
531 if (e == 99) {
532 abort_unzip(PASS_STATE_ONLY);
533 }
534 bb >>= t->b;
535 k -= t->b;
536 e -= 16;
537 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
538 t = t->v.t + ((unsigned) bb & mask_bits[e]);
539 e = t->e;
540 } while (e > 16);
541 bb >>= t->b;
542 k -= t->b;
543 if (e == 16) {
544 gunzip_window[w++] = (unsigned char) t->v.n;
545 if (w == GUNZIP_WSIZE) {
546 gunzip_outbuf_count = w;
547
548 w = 0;
549 return 1;
550 }
551 } else {
552
553 if (e == 15) {
554 break;
555 }
556
557
558 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
559 nn = t->v.n + ((unsigned) bb & mask_bits[e]);
560 bb >>= e;
561 k -= e;
562
563
564 bb = fill_bitbuffer(PASS_STATE bb, &k, bd);
565 t = td + ((unsigned) bb & md);
566 e = t->e;
567 if (e > 16)
568 do {
569 if (e == 99) {
570 abort_unzip(PASS_STATE_ONLY);
571 }
572 bb >>= t->b;
573 k -= t->b;
574 e -= 16;
575 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
576 t = t->v.t + ((unsigned) bb & mask_bits[e]);
577 e = t->e;
578 } while (e > 16);
579 bb >>= t->b;
580 k -= t->b;
581 bb = fill_bitbuffer(PASS_STATE bb, &k, e);
582 dd = w - t->v.n - ((unsigned) bb & mask_bits[e]);
583 bb >>= e;
584 k -= e;
585
586
587 do_copy:
588 do {
589
590
591 unsigned delta;
592
593 dd &= GUNZIP_WSIZE - 1;
594 e = GUNZIP_WSIZE - (dd > w ? dd : w);
595 delta = w > dd ? w - dd : dd - w;
596 if (e > nn) e = nn;
597 nn -= e;
598
599
600 if (delta >= e) {
601 memcpy(gunzip_window + w, gunzip_window + dd, e);
602 w += e;
603 dd += e;
604 } else {
605
606
607 do {
608 gunzip_window[w++] = gunzip_window[dd++];
609 } while (--e);
610 }
611 if (w == GUNZIP_WSIZE) {
612 gunzip_outbuf_count = w;
613 resume_copy = (nn != 0);
614
615 w = 0;
616 return 1;
617 }
618 } while (nn);
619 resume_copy = 0;
620 }
621 }
622
623
624 gunzip_outbuf_count = w;
625 gunzip_bb = bb;
626 gunzip_bk = k;
627
628
629
630 huft_free_all(PASS_STATE_ONLY);
631
632
633 return 0;
634}
635#undef ml
636#undef md
637#undef bb
638#undef k
639#undef w
640#undef tl
641#undef td
642#undef bl
643#undef bd
644#undef nn
645#undef dd
646
647
648
649static void inflate_stored_setup(STATE_PARAM int my_n, int my_b, int my_k)
650{
651 inflate_stored_n = my_n;
652 inflate_stored_b = my_b;
653 inflate_stored_k = my_k;
654
655 inflate_stored_w = gunzip_outbuf_count;
656}
657
658static int inflate_stored(STATE_PARAM_ONLY)
659{
660
661 while (inflate_stored_n--) {
662 inflate_stored_b = fill_bitbuffer(PASS_STATE inflate_stored_b, &inflate_stored_k, 8);
663 gunzip_window[inflate_stored_w++] = (unsigned char) inflate_stored_b;
664 if (inflate_stored_w == GUNZIP_WSIZE) {
665 gunzip_outbuf_count = inflate_stored_w;
666
667 inflate_stored_w = 0;
668 inflate_stored_b >>= 8;
669 inflate_stored_k -= 8;
670 return 1;
671 }
672 inflate_stored_b >>= 8;
673 inflate_stored_k -= 8;
674 }
675
676
677 gunzip_outbuf_count = inflate_stored_w;
678 gunzip_bb = inflate_stored_b;
679 gunzip_bk = inflate_stored_k;
680 return 0;
681}
682
683
684
685
686
687
688
689
690
691
692static int inflate_block(STATE_PARAM smallint *e)
693{
694 unsigned ll[286 + 30];
695 unsigned t;
696 unsigned b;
697 unsigned k;
698
699
700
701 b = gunzip_bb;
702 k = gunzip_bk;
703
704
705 b = fill_bitbuffer(PASS_STATE b, &k, 1);
706 *e = b & 1;
707 b >>= 1;
708 k -= 1;
709
710
711 b = fill_bitbuffer(PASS_STATE b, &k, 2);
712 t = (unsigned) b & 3;
713 b >>= 2;
714 k -= 2;
715
716
717 gunzip_bb = b;
718 gunzip_bk = k;
719
720
721
722
723
724
725 switch (t) {
726 case 0:
727 {
728 unsigned n;
729 unsigned b_stored;
730 unsigned k_stored;
731
732
733 b_stored = gunzip_bb;
734 k_stored = gunzip_bk;
735
736
737 n = k_stored & 7;
738 b_stored >>= n;
739 k_stored -= n;
740
741
742 b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
743 n = ((unsigned) b_stored & 0xffff);
744 b_stored >>= 16;
745 k_stored -= 16;
746
747 b_stored = fill_bitbuffer(PASS_STATE b_stored, &k_stored, 16);
748 if (n != (unsigned) ((~b_stored) & 0xffff)) {
749 abort_unzip(PASS_STATE_ONLY);
750 }
751 b_stored >>= 16;
752 k_stored -= 16;
753
754 inflate_stored_setup(PASS_STATE n, b_stored, k_stored);
755
756 return -1;
757 }
758 case 1:
759
760
761
762
763 {
764 int i;
765 unsigned bl;
766 unsigned bd;
767
768
769
770
771 for (i = 0; i < 144; i++)
772 ll[i] = 8;
773 for (; i < 256; i++)
774 ll[i] = 9;
775 for (; i < 280; i++)
776 ll[i] = 7;
777 for (; i < 288; i++)
778 ll[i] = 8;
779 bl = 7;
780 huft_build(ll, 288, 257, cplens, cplext, &inflate_codes_tl, &bl);
781
782
783
784 for (i = 0; i < 30; i++)
785 ll[i] = 5;
786 bd = 5;
787 huft_build(ll, 30, 0, cpdist, cpdext, &inflate_codes_td, &bd);
788
789
790 inflate_codes_setup(PASS_STATE bl, bd);
791
792
793
794 return -2;
795 }
796 case 2:
797 {
798 enum { dbits = 6 };
799 enum { lbits = 9 };
800
801 huft_t *td;
802 unsigned i;
803 unsigned j;
804 unsigned l;
805 unsigned m;
806 unsigned n;
807 unsigned bl;
808 unsigned bd;
809 unsigned nb;
810 unsigned nl;
811 unsigned nd;
812
813
814 unsigned b_dynamic;
815 unsigned k_dynamic;
816
817
818 b_dynamic = gunzip_bb;
819 k_dynamic = gunzip_bk;
820
821
822 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5);
823 nl = 257 + ((unsigned) b_dynamic & 0x1f);
824
825 b_dynamic >>= 5;
826 k_dynamic -= 5;
827 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 5);
828 nd = 1 + ((unsigned) b_dynamic & 0x1f);
829
830 b_dynamic >>= 5;
831 k_dynamic -= 5;
832 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 4);
833 nb = 4 + ((unsigned) b_dynamic & 0xf);
834
835 b_dynamic >>= 4;
836 k_dynamic -= 4;
837 if (nl > 286 || nd > 30) {
838 abort_unzip(PASS_STATE_ONLY);
839 }
840
841
842 for (j = 0; j < nb; j++) {
843 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3);
844 ll[border[j]] = (unsigned) b_dynamic & 7;
845 b_dynamic >>= 3;
846 k_dynamic -= 3;
847 }
848 for (; j < 19; j++)
849 ll[border[j]] = 0;
850
851
852 bl = 7;
853 i = huft_build(ll, 19, 19, NULL, NULL, &inflate_codes_tl, &bl);
854 if (i != 0) {
855 abort_unzip(PASS_STATE_ONLY);
856 }
857
858
859 n = nl + nd;
860 m = mask_bits[bl];
861 i = l = 0;
862 while ((unsigned) i < n) {
863 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, (unsigned)bl);
864 td = inflate_codes_tl + ((unsigned) b_dynamic & m);
865 j = td->b;
866 b_dynamic >>= j;
867 k_dynamic -= j;
868 j = td->v.n;
869 if (j < 16) {
870 ll[i++] = l = j;
871 } else if (j == 16) {
872 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 2);
873 j = 3 + ((unsigned) b_dynamic & 3);
874 b_dynamic >>= 2;
875 k_dynamic -= 2;
876 if ((unsigned) i + j > n) {
877 abort_unzip(PASS_STATE_ONLY);
878 }
879 while (j--) {
880 ll[i++] = l;
881 }
882 } else if (j == 17) {
883 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 3);
884 j = 3 + ((unsigned) b_dynamic & 7);
885 b_dynamic >>= 3;
886 k_dynamic -= 3;
887 if ((unsigned) i + j > n) {
888 abort_unzip(PASS_STATE_ONLY);
889 }
890 while (j--) {
891 ll[i++] = 0;
892 }
893 l = 0;
894 } else {
895 b_dynamic = fill_bitbuffer(PASS_STATE b_dynamic, &k_dynamic, 7);
896 j = 11 + ((unsigned) b_dynamic & 0x7f);
897 b_dynamic >>= 7;
898 k_dynamic -= 7;
899 if ((unsigned) i + j > n) {
900 abort_unzip(PASS_STATE_ONLY);
901 }
902 while (j--) {
903 ll[i++] = 0;
904 }
905 l = 0;
906 }
907 }
908
909
910 huft_free(inflate_codes_tl);
911
912
913 gunzip_bb = b_dynamic;
914 gunzip_bk = k_dynamic;
915
916
917 bl = lbits;
918
919 i = huft_build(ll, nl, 257, cplens, cplext, &inflate_codes_tl, &bl);
920 if (i != 0) {
921 abort_unzip(PASS_STATE_ONLY);
922 }
923 bd = dbits;
924 i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &inflate_codes_td, &bd);
925 if (i != 0) {
926 abort_unzip(PASS_STATE_ONLY);
927 }
928
929
930 inflate_codes_setup(PASS_STATE bl, bd);
931
932
933
934 return -2;
935 }
936 default:
937 abort_unzip(PASS_STATE_ONLY);
938 }
939}
940
941
942static void calculate_gunzip_crc(STATE_PARAM_ONLY)
943{
944 gunzip_crc = crc32_block_endian0(gunzip_crc, gunzip_window, gunzip_outbuf_count, gunzip_crc_table);
945 gunzip_bytes_out += gunzip_outbuf_count;
946}
947
948
949static int inflate_get_next_window(STATE_PARAM_ONLY)
950{
951 gunzip_outbuf_count = 0;
952
953 while (1) {
954 int ret;
955
956 if (need_another_block) {
957 if (end_reached) {
958 calculate_gunzip_crc(PASS_STATE_ONLY);
959 end_reached = 0;
960
961 return 0;
962 }
963 method = inflate_block(PASS_STATE &end_reached);
964 need_another_block = 0;
965 }
966
967 switch (method) {
968 case -1:
969 ret = inflate_stored(PASS_STATE_ONLY);
970 break;
971 case -2:
972 ret = inflate_codes(PASS_STATE_ONLY);
973 break;
974 default:
975 abort_unzip(PASS_STATE_ONLY);
976 }
977
978 if (ret == 1) {
979 calculate_gunzip_crc(PASS_STATE_ONLY);
980 return 1;
981 }
982 need_another_block = 1;
983 }
984
985}
986
987
988
989static IF_DESKTOP(long long) int
990inflate_unzip_internal(STATE_PARAM transformer_state_t *xstate)
991{
992 IF_DESKTOP(long long) int n = 0;
993 ssize_t nwrote;
994
995
996 gunzip_window = xmalloc(GUNZIP_WSIZE);
997 gunzip_outbuf_count = 0;
998 gunzip_bytes_out = 0;
999 gunzip_src_fd = xstate->src_fd;
1000
1001
1002 method = -1;
1003 need_another_block = 1;
1004 resume_copy = 0;
1005 gunzip_bk = 0;
1006 gunzip_bb = 0;
1007
1008
1009 gunzip_crc_table = crc32_new_table_le();
1010 gunzip_crc = ~0;
1011
1012 error_msg = "corrupted data";
1013 if (setjmp(error_jmp)) {
1014
1015 bb_error_msg(error_msg);
1016 n = -1;
1017 goto ret;
1018 }
1019
1020 while (1) {
1021 int r = inflate_get_next_window(PASS_STATE_ONLY);
1022 nwrote = transformer_write(xstate, gunzip_window, gunzip_outbuf_count);
1023 if (nwrote == (ssize_t)-1) {
1024 n = -1;
1025 goto ret;
1026 }
1027 IF_DESKTOP(n += nwrote;)
1028 if (r == 0) break;
1029 }
1030
1031
1032 if (gunzip_bk >= 8) {
1033
1034
1035 bytebuffer_offset--;
1036 bytebuffer[bytebuffer_offset] = gunzip_bb & 0xff;
1037 gunzip_bb >>= 8;
1038 gunzip_bk -= 8;
1039 }
1040 ret:
1041
1042 free(gunzip_window);
1043 free(gunzip_crc_table);
1044 return n;
1045}
1046
1047
1048
1049
1050
1051
1052IF_DESKTOP(long long) int FAST_FUNC
1053inflate_unzip(transformer_state_t *xstate)
1054{
1055 IF_DESKTOP(long long) int n;
1056 DECLARE_STATE;
1057
1058 ALLOC_STATE;
1059
1060 to_read = xstate->bytes_in;
1061
1062 bytebuffer_offset = 4;
1063 bytebuffer = xmalloc(bytebuffer_max);
1064 n = inflate_unzip_internal(PASS_STATE xstate);
1065 free(bytebuffer);
1066
1067 xstate->crc32 = gunzip_crc;
1068 xstate->bytes_out = gunzip_bytes_out;
1069 DEALLOC_STATE;
1070 return n;
1071}
1072
1073
1074
1075
1076
1077
1078
1079static int top_up(STATE_PARAM unsigned n)
1080{
1081 int count = bytebuffer_size - bytebuffer_offset;
1082
1083 if (count < (int)n) {
1084 memmove(bytebuffer, &bytebuffer[bytebuffer_offset], count);
1085 bytebuffer_offset = 0;
1086 bytebuffer_size = full_read(gunzip_src_fd, &bytebuffer[count], bytebuffer_max - count);
1087 if ((int)bytebuffer_size < 0) {
1088 bb_error_msg(bb_msg_read_error);
1089 return 0;
1090 }
1091 bytebuffer_size += count;
1092 if (bytebuffer_size < n)
1093 return 0;
1094 }
1095 return 1;
1096}
1097
1098static uint16_t buffer_read_le_u16(STATE_PARAM_ONLY)
1099{
1100 uint16_t res;
1101#if BB_LITTLE_ENDIAN
1102 move_from_unaligned16(res, &bytebuffer[bytebuffer_offset]);
1103#else
1104 res = bytebuffer[bytebuffer_offset];
1105 res |= bytebuffer[bytebuffer_offset + 1] << 8;
1106#endif
1107 bytebuffer_offset += 2;
1108 return res;
1109}
1110
1111static uint32_t buffer_read_le_u32(STATE_PARAM_ONLY)
1112{
1113 uint32_t res;
1114#if BB_LITTLE_ENDIAN
1115 move_from_unaligned32(res, &bytebuffer[bytebuffer_offset]);
1116#else
1117 res = bytebuffer[bytebuffer_offset];
1118 res |= bytebuffer[bytebuffer_offset + 1] << 8;
1119 res |= bytebuffer[bytebuffer_offset + 2] << 16;
1120 res |= bytebuffer[bytebuffer_offset + 3] << 24;
1121#endif
1122 bytebuffer_offset += 4;
1123 return res;
1124}
1125
1126static int check_header_gzip(STATE_PARAM transformer_state_t *xstate)
1127{
1128 union {
1129 unsigned char raw[8];
1130 struct {
1131 uint8_t gz_method;
1132 uint8_t flags;
1133 uint32_t mtime;
1134 uint8_t xtra_flags_UNUSED;
1135 uint8_t os_flags_UNUSED;
1136 } PACKED formatted;
1137 } header;
1138
1139 BUILD_BUG_ON(sizeof(header) != 8);
1140
1141
1142
1143
1144
1145 bytebuffer_size -= bytebuffer_offset;
1146 memmove(bytebuffer, &bytebuffer[bytebuffer_offset], bytebuffer_size);
1147 bytebuffer_offset = 0;
1148
1149 if (!top_up(PASS_STATE 8))
1150 return 0;
1151 memcpy(header.raw, &bytebuffer[bytebuffer_offset], 8);
1152 bytebuffer_offset += 8;
1153
1154
1155 if (header.formatted.gz_method != 8) {
1156 return 0;
1157 }
1158
1159 if (header.formatted.flags & 0x04) {
1160
1161 unsigned extra_short;
1162
1163 if (!top_up(PASS_STATE 2))
1164 return 0;
1165 extra_short = buffer_read_le_u16(PASS_STATE_ONLY);
1166 if (!top_up(PASS_STATE extra_short))
1167 return 0;
1168
1169 bytebuffer_offset += extra_short;
1170 }
1171
1172
1173
1174
1175 if (header.formatted.flags & 0x18) {
1176 while (1) {
1177 do {
1178 if (!top_up(PASS_STATE 1))
1179 return 0;
1180 } while (bytebuffer[bytebuffer_offset++] != 0);
1181 if ((header.formatted.flags & 0x18) != 0x18)
1182 break;
1183 header.formatted.flags &= ~0x18;
1184 }
1185 }
1186
1187 xstate->mtime = SWAP_LE32(header.formatted.mtime);
1188
1189
1190 if (header.formatted.flags & 0x02) {
1191 if (!top_up(PASS_STATE 2))
1192 return 0;
1193 bytebuffer_offset += 2;
1194 }
1195 return 1;
1196}
1197
1198IF_DESKTOP(long long) int FAST_FUNC
1199unpack_gz_stream(transformer_state_t *xstate)
1200{
1201 uint32_t v32;
1202 IF_DESKTOP(long long) int total, n;
1203 DECLARE_STATE;
1204
1205#if !ENABLE_FEATURE_SEAMLESS_Z
1206 if (check_signature16(xstate, GZIP_MAGIC))
1207 return -1;
1208#else
1209 if (!xstate->signature_skipped) {
1210 uint16_t magic2;
1211
1212 if (full_read(xstate->src_fd, &magic2, 2) != 2) {
1213 bad_magic:
1214 bb_error_msg("invalid magic");
1215 return -1;
1216 }
1217 if (magic2 == COMPRESS_MAGIC) {
1218 xstate->signature_skipped = 2;
1219 return unpack_Z_stream(xstate);
1220 }
1221 if (magic2 != GZIP_MAGIC)
1222 goto bad_magic;
1223 }
1224#endif
1225
1226 total = 0;
1227
1228 ALLOC_STATE;
1229 to_read = -1;
1230
1231 bytebuffer = xmalloc(bytebuffer_max);
1232 gunzip_src_fd = xstate->src_fd;
1233
1234 again:
1235 if (!check_header_gzip(PASS_STATE xstate)) {
1236 bb_error_msg("corrupted data");
1237 total = -1;
1238 goto ret;
1239 }
1240
1241 n = inflate_unzip_internal(PASS_STATE xstate);
1242 if (n < 0) {
1243 total = -1;
1244 goto ret;
1245 }
1246 total += n;
1247
1248 if (!top_up(PASS_STATE 8)) {
1249 bb_error_msg("corrupted data");
1250 total = -1;
1251 goto ret;
1252 }
1253
1254
1255 v32 = buffer_read_le_u32(PASS_STATE_ONLY);
1256 if ((~gunzip_crc) != v32) {
1257 bb_error_msg("crc error");
1258 total = -1;
1259 goto ret;
1260 }
1261
1262
1263 v32 = buffer_read_le_u32(PASS_STATE_ONLY);
1264 if ((uint32_t)gunzip_bytes_out != v32) {
1265 bb_error_msg("incorrect length");
1266 total = -1;
1267 }
1268
1269 if (!top_up(PASS_STATE 2))
1270 goto ret;
1271
1272 if (bytebuffer[bytebuffer_offset] == 0x1f
1273 && bytebuffer[bytebuffer_offset + 1] == 0x8b
1274 ) {
1275 bytebuffer_offset += 2;
1276 goto again;
1277 }
1278
1279
1280
1281 ret:
1282 free(bytebuffer);
1283 DEALLOC_STATE;
1284 return total;
1285}
1286