123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619 |
- #define __EXPLODE_C
- #define UNZIP_INTERNAL
- #include "unzip.h"
- #ifndef WSIZE
- # define WSIZE 0x8000
- #endif
- #if (defined(DLL) && !defined(NO_SLIDE_REDIR))
- # define wszimpl (unsigned)(G._wsize)
- #else
- # if defined(USE_DEFLATE64) && defined(INT_16BIT)
- # define wszimpl (unsigned)(WSIZE>>1)
- # else
- # define wszimpl WSIZE
- # endif
- #endif
- static int get_tree OF((__GPRO__ unsigned *l, unsigned n));
- static int explode_lit OF((__GPRO__ struct huft *tb, struct huft *tl,
- struct huft *td, unsigned bb, unsigned bl,
- unsigned bd, unsigned bdl));
- static int explode_nolit OF((__GPRO__ struct huft *tl, struct huft *td,
- unsigned bl, unsigned bd, unsigned bdl));
- int explode OF((__GPRO));
- #define INVALID_CODE 99
- #define IS_INVALID_CODE(c) ((c) == INVALID_CODE)
- static ZCONST ush cplen2[] =
- {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, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
- 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65};
- static ZCONST ush cplen3[] =
- {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,
- 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
- 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66};
- static ZCONST uch extra[] =
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 8};
- static ZCONST ush cpdist4[] =
- {1, 65, 129, 193, 257, 321, 385, 449, 513, 577, 641, 705,
- 769, 833, 897, 961, 1025, 1089, 1153, 1217, 1281, 1345, 1409, 1473,
- 1537, 1601, 1665, 1729, 1793, 1857, 1921, 1985, 2049, 2113, 2177,
- 2241, 2305, 2369, 2433, 2497, 2561, 2625, 2689, 2753, 2817, 2881,
- 2945, 3009, 3073, 3137, 3201, 3265, 3329, 3393, 3457, 3521, 3585,
- 3649, 3713, 3777, 3841, 3905, 3969, 4033};
- static ZCONST ush cpdist8[] =
- {1, 129, 257, 385, 513, 641, 769, 897, 1025, 1153, 1281,
- 1409, 1537, 1665, 1793, 1921, 2049, 2177, 2305, 2433, 2561, 2689,
- 2817, 2945, 3073, 3201, 3329, 3457, 3585, 3713, 3841, 3969, 4097,
- 4225, 4353, 4481, 4609, 4737, 4865, 4993, 5121, 5249, 5377, 5505,
- 5633, 5761, 5889, 6017, 6145, 6273, 6401, 6529, 6657, 6785, 6913,
- 7041, 7169, 7297, 7425, 7553, 7681, 7809, 7937, 8065};
- #define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE)<<k;k+=8;}}
- #define DUMPBITS(n) {b>>=(n);k-=(n);}
- #define DECODEHUFT(htab, bits, mask) {\
- NEEDBITS((unsigned)(bits))\
- t = (htab) + ((~(unsigned)b)&(mask));\
- while (1) {\
- DUMPBITS(t->b)\
- if ((e=t->e) <= 32) break;\
- if (IS_INVALID_CODE(e)) return 1;\
- e &= 31;\
- NEEDBITS(e)\
- t = t->v.t + ((~(unsigned)b)&mask_bits[e]);\
- }\
- }
- static int get_tree(__G__ l, n)
- __GDEF
- unsigned *l;
- unsigned n;
- {
- unsigned i;
- unsigned k;
- unsigned j;
- unsigned b;
-
- i = NEXTBYTE + 1;
- k = 0;
- do {
- b = ((j = NEXTBYTE) & 0xf) + 1;
- j = ((j & 0xf0) >> 4) + 1;
- if (k + j > n)
- return 4;
- do {
- l[k++] = b;
- } while (--j);
- } while (--i);
- return k != n ? 4 : 0;
- }
- static int explode_lit(__G__ tb, tl, td, bb, bl, bd, bdl)
- __GDEF
- struct huft *tb, *tl, *td;
- unsigned bb, bl, bd;
- unsigned bdl;
- {
- zusz_t s;
- register unsigned e;
- unsigned n, d;
- unsigned w;
- struct huft *t;
- unsigned mb, ml, md;
- unsigned mdl;
- register ulg b;
- register unsigned k;
- unsigned u;
- int retval = 0;
-
- b = k = w = 0;
- u = 1;
- mb = mask_bits[bb];
- ml = mask_bits[bl];
- md = mask_bits[bd];
- mdl = mask_bits[bdl];
- s = G.lrec.ucsize;
- while (s > 0)
- {
- NEEDBITS(1)
- if (b & 1)
- {
- DUMPBITS(1)
- s--;
- DECODEHUFT(tb, bb, mb)
- redirSlide[w++] = (uch)t->v.n;
- if (w == wszimpl)
- {
- if ((retval = flush(__G__ redirSlide, (ulg)w, 0)) != 0)
- return retval;
- w = u = 0;
- }
- }
- else
- {
- DUMPBITS(1)
- NEEDBITS(bdl)
- d = (unsigned)b & mdl;
- DUMPBITS(bdl)
- DECODEHUFT(td, bd, md)
- d = w - d - t->v.n;
- DECODEHUFT(tl, bl, ml)
- n = t->v.n;
- if (e)
- {
- NEEDBITS(8)
- n += (unsigned)b & 0xff;
- DUMPBITS(8)
- }
-
- s = (s > (zusz_t)n ? s - (zusz_t)n : 0);
- do {
- #if (defined(DLL) && !defined(NO_SLIDE_REDIR))
- if (G.redirect_slide) {
-
- if (d >= wszimpl)
- return 1;
- e = wszimpl - (d > w ? d : w);
- } else
- #endif
- e = wszimpl - ((d &= wszimpl-1) > w ? d : w);
- if (e > n) e = n;
- n -= e;
- if (u && w <= d)
- {
- memzero(redirSlide + w, e);
- w += e;
- d += e;
- }
- else
- #ifndef NOMEMCPY
- if (w - d >= e)
- {
- memcpy(redirSlide + w, redirSlide + d, e);
- w += e;
- d += e;
- }
- else
- #endif
- do {
- redirSlide[w++] = redirSlide[d++];
- } while (--e);
- if (w == wszimpl)
- {
- if ((retval = flush(__G__ redirSlide, (ulg)w, 0)) != 0)
- return retval;
- w = u = 0;
- }
- } while (n);
- }
- }
-
- if ((retval = flush(__G__ redirSlide, (ulg)w, 0)) != 0)
- return retval;
- if (G.csize + G.incnt + (k >> 3))
- {
- G.used_csize = G.lrec.csize - G.csize - G.incnt - (k >> 3);
- return 5;
- }
- return 0;
- }
- static int explode_nolit(__G__ tl, td, bl, bd, bdl)
- __GDEF
- struct huft *tl, *td;
- unsigned bl, bd;
- unsigned bdl;
- {
- zusz_t s;
- register unsigned e;
- unsigned n, d;
- unsigned w;
- struct huft *t;
- unsigned ml, md;
- unsigned mdl;
- register ulg b;
- register unsigned k;
- unsigned u;
- int retval = 0;
-
- b = k = w = 0;
- u = 1;
- ml = mask_bits[bl];
- md = mask_bits[bd];
- mdl = mask_bits[bdl];
- s = G.lrec.ucsize;
- while (s > 0)
- {
- NEEDBITS(1)
- if (b & 1)
- {
- DUMPBITS(1)
- s--;
- NEEDBITS(8)
- redirSlide[w++] = (uch)b;
- if (w == wszimpl)
- {
- if ((retval = flush(__G__ redirSlide, (ulg)w, 0)) != 0)
- return retval;
- w = u = 0;
- }
- DUMPBITS(8)
- }
- else
- {
- DUMPBITS(1)
- NEEDBITS(bdl)
- d = (unsigned)b & mdl;
- DUMPBITS(bdl)
- DECODEHUFT(td, bd, md)
- d = w - d - t->v.n;
- DECODEHUFT(tl, bl, ml)
- n = t->v.n;
- if (e)
- {
- NEEDBITS(8)
- n += (unsigned)b & 0xff;
- DUMPBITS(8)
- }
-
- s = (s > (zusz_t)n ? s - (zusz_t)n : 0);
- do {
- #if (defined(DLL) && !defined(NO_SLIDE_REDIR))
- if (G.redirect_slide) {
-
- if (d >= wszimpl)
- return 1;
- e = wszimpl - (d > w ? d : w);
- } else
- #endif
- e = wszimpl - ((d &= wszimpl-1) > w ? d : w);
- if (e > n) e = n;
- n -= e;
- if (u && w <= d)
- {
- memzero(redirSlide + w, e);
- w += e;
- d += e;
- }
- else
- #ifndef NOMEMCPY
- if (w - d >= e)
- {
- memcpy(redirSlide + w, redirSlide + d, e);
- w += e;
- d += e;
- }
- else
- #endif
- do {
- redirSlide[w++] = redirSlide[d++];
- } while (--e);
- if (w == wszimpl)
- {
- if ((retval = flush(__G__ redirSlide, (ulg)w, 0)) != 0)
- return retval;
- w = u = 0;
- }
- } while (n);
- }
- }
-
- if ((retval = flush(__G__ redirSlide, (ulg)w, 0)) != 0)
- return retval;
- if (G.csize + G.incnt + (k >> 3))
- {
- G.used_csize = G.lrec.csize - G.csize - G.incnt - (k >> 3);
- return 5;
- }
- return 0;
- }
- int explode(__G)
- __GDEF
- {
- unsigned r;
- struct huft *tb;
- struct huft *tl;
- struct huft *td;
- unsigned bb;
- unsigned bl;
- unsigned bd;
- unsigned bdl;
- unsigned l[256];
- #if (defined(DLL) && !defined(NO_SLIDE_REDIR))
- if (G.redirect_slide)
-
- G._wsize = G.redirect_size, redirSlide = G.redirect_buffer;
- else
- #if defined(USE_DEFLATE64) && defined(INT_16BIT)
-
- G._wsize = WSIZE>>1, redirSlide = slide;
- #else
- G._wsize = WSIZE, redirSlide = slide;
- #endif
- #endif
-
- bl = 7;
- bd = (G.csize + G.incnt) > 200000L ? 8 : 7;
- #ifdef DEBUG
- G.hufts = 0;
- #endif
- if (G.lrec.general_purpose_bit_flag & 4)
-
- {
- bb = 9;
- if ((r = get_tree(__G__ l, 256)) != 0)
- return (int)r;
- if ((r = huft_build(__G__ l, 256, 256, NULL, NULL, &tb, &bb)) != 0)
- {
- if (r == 1)
- huft_free(tb);
- return (int)r;
- }
- if ((r = get_tree(__G__ l, 64)) != 0) {
- huft_free(tb);
- return (int)r;
- }
- if ((r = huft_build(__G__ l, 64, 0, cplen3, extra, &tl, &bl)) != 0)
- {
- if (r == 1)
- huft_free(tl);
- huft_free(tb);
- return (int)r;
- }
- }
- else
-
- {
- tb = (struct huft *)NULL;
- if ((r = get_tree(__G__ l, 64)) != 0)
- return (int)r;
- if ((r = huft_build(__G__ l, 64, 0, cplen2, extra, &tl, &bl)) != 0)
- {
- if (r == 1)
- huft_free(tl);
- return (int)r;
- }
- }
- if ((r = get_tree(__G__ l, 64)) != 0) {
- huft_free(tl);
- if (tb != (struct huft *)NULL) huft_free(tb);
- return (int)r;
- }
- if (G.lrec.general_purpose_bit_flag & 2)
- {
- bdl = 7;
- r = huft_build(__G__ l, 64, 0, cpdist8, extra, &td, &bd);
- }
- else
- {
- bdl = 6;
- r = huft_build(__G__ l, 64, 0, cpdist4, extra, &td, &bd);
- }
- if (r != 0)
- {
- if (r == 1)
- huft_free(td);
- huft_free(tl);
- if (tb != (struct huft *)NULL) huft_free(tb);
- return (int)r;
- }
- if (tb != NULL) {
- r = explode_lit(__G__ tb, tl, td, bb, bl, bd, bdl);
- huft_free(tb);
- } else {
- r = explode_nolit(__G__ tl, td, bl, bd, bdl);
- }
- huft_free(td);
- huft_free(tl);
- Trace((stderr, "<%u > ", G.hufts));
- return (int)r;
- }
- #undef DECODEHUFT
- #undef NEEDBITS
- #undef DUMPBITS
- #undef wszimpl
|