#pragma prototyped12/*3* zip implode decoder4*/56#include "zip.h"7#include "huff.h"89#define EXPLODE_BIG 0110#define EXPLODE_LIT 021112#define WSIZE 327681314/* explode.c -- put in the public domain by Mark Adler15version c15, 6 July 1996 */161718/* You can do whatever you like with this source file, though I would19prefer that if you modify it and redistribute it that you include20comments to that effect with your name and the date. Thank you.2122History:23vers date who what24---- --------- -------------- ------------------------------------25c1 30 Mar 92 M. Adler explode that uses huff() from inflate26(this gives over a 70% speed improvement27over the original unimplode.c, which28decoded a bit at a time)29c2 4 Apr 92 M. Adler fixed bug for file sizes a multiple of 32k.30c3 10 Apr 92 M. Adler added a little memory tracking if DEBUG31c4 11 Apr 92 M. Adler added NOMEMCPY do kill use of memcpy()32c5 21 Apr 92 M. Adler added the WSIZE #define to allow reducing33the 32K window size for specialized34applications.35c6 31 May 92 M. Adler added typecasts to eliminate some warnings36c7 27 Jun 92 G. Roelofs added more typecasts.37c8 17 Oct 92 G. Roelofs changed ULONG/UWORD/byte to ulg/ush/uch.38c9 19 Jul 93 J. Bush added more typecasts (to return values);39made l[256] array static for Amiga.40c10 8 Oct 93 G. Roelofs added used_csize for diagnostics; added41buf and unshrink arguments to flush();42undef'd various macros at end for Turbo C;43removed NEXTBYTE macro (now in unzip.h)44and bytebuf variable (not used); changed45memset() to memzero().46c11 9 Jan 94 M. Adler fixed incorrect used_csize calculation.47c12 9 Apr 94 G. Roelofs fixed split comments on preprocessor lines48to avoid bug in Encore compiler.49c13 25 Aug 94 M. Adler fixed distance-length comment (orig c9 fix)50c14 22 Nov 95 S. Maxwell removed unnecessary "static" on auto array51c15 6 Jul 96 W. Haidinger added ulg typecasts to flush() calls52*/535455/*56Explode imploded (PKZIP method 6 compressed) data. This compression57method searches for as much of the current string of bytes (up to a length58of ~320) in the previous 4K or 8K bytes. If it doesn't find any matches59(of at least length 2 or 3), it codes the next byte. Otherwise, it codes60the length of the matched string and its distance backwards from the61current position. Single bytes ("literals") are preceded by a one (a62single bit) and are either uncoded (the eight bits go directly into the63compressed stream for a total of nine bits) or Huffman coded with a64supplied literal code tree. If literals are coded, then the minimum match65length is three, otherwise it is two.6667There are therefore four kinds of imploded streams: 8K search with coded68literals (min match = 3), 4K search with coded literals (min match = 3),698K with uncoded literals (min match = 2), and 4K with uncoded literals70(min match = 2). The kind of stream is identified in two bits of a71general purpose bit flag that is outside of the compressed stream.7273Distance-length pairs for matched strings are preceded by a zero bit (to74distinguish them from literals) and are always coded. The distance comes75first and is either the low six (4K) or low seven (8K) bits of the76distance (uncoded), followed by the high six bits of the distance coded.77Then the length is six bits coded (0..63 + min match length), and if the78maximum such length is coded, then it's followed by another eight bits79(uncoded) to be added to the coded length. This gives a match length80range of 2..320 or 3..321 bytes.8182The literal, length, and distance codes are all represented in a slightly83compressed form themselves. What is sent are the lengths of the codes for84each value, which is sufficient to construct the codes. Each byte of the85code representation is the code length (the low four bits representing861..16), and the number of values sequentially with that length (the high87four bits also representing 1..16). There are 256 literal code values (if88literals are coded), 64 length code values, and 64 distance code values,89in that order at the beginning of the compressed stream. Each set of code90values is preceded (redundantly) with a byte indicating how many bytes are91in the code description that follows, in the range 1..256.9293The codes themselves are decoded using tables made by huff() from94the bit lengths. That routine and its comments are in the inflate.c95module.96*/9798struct State_s; typedef struct State_s State_t;99100struct State_s101{102Codex_t* codex;103int method;104ssize_t (*explodef)(State_t*, char*, size_t);105106uch buf[SF_BUFSIZE];107uch* ip;108uch* ie;109110ulg bit_buf; /* bit buffer */111ulg bit_len; /* bits in bit buffer */112113uch slide[WSIZE];114Huff_t *tb; /* literal, length, and distance tables */115Huff_t *tl;116Huff_t *td;117int bb; /* number of bits decoded by those */118int bl;119int bd;120121ulg u, n, d, w;122size_t s; /* original size */123ulg l[256]; /* bit lengths for codes */124125Vmalloc_t* vm;126127int eof;128};129130/* The implode algorithm uses a sliding 4K or 8K byte window on the131uncompressed stream to find repeated byte strings. This is implemented132here as a circular buffer. The index is updated simply by incrementing133and then and'ing with 0x0fff (4K-1) or 0x1fff (8K-1). Here, the 32K134buffer of inflate is used, and it works just as well to always have135a 32K circular buffer, so the index is anded with 0x7fff. This is136done to allow the window to also be used as the output buffer. */137/* This must be supplied in an external module useable like "uch slide[8192];"138or "uch *slide;", where the latter would be malloc'ed. In unzip, slide[]139is actually a 32K area for use by inflate, which uses a 32K sliding window.140*/141142143/* Tables for length and distance */144static ush cplen2[] =145{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,14618, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,14735, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,14852, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65};149static ush cplen3[] =150{3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,15119, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,15236, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,15353, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66};154static ush extra[] =155{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,1560, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,1570, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,1588};159static ush cpdist4[] =160{1, 65, 129, 193, 257, 321, 385, 449, 513, 577, 641, 705,161769, 833, 897, 961, 1025, 1089, 1153, 1217, 1281, 1345, 1409, 1473,1621537, 1601, 1665, 1729, 1793, 1857, 1921, 1985, 2049, 2113, 2177,1632241, 2305, 2369, 2433, 2497, 2561, 2625, 2689, 2753, 2817, 2881,1642945, 3009, 3073, 3137, 3201, 3265, 3329, 3393, 3457, 3521, 3585,1653649, 3713, 3777, 3841, 3905, 3969, 4033};166static ush cpdist8[] =167{1, 129, 257, 385, 513, 641, 769, 897, 1025, 1153, 1281,1681409, 1537, 1665, 1793, 1921, 2049, 2177, 2305, 2433, 2561, 2689,1692817, 2945, 3073, 3201, 3329, 3457, 3585, 3713, 3841, 3969, 4097,1704225, 4353, 4481, 4609, 4737, 4865, 4993, 5121, 5249, 5377, 5505,1715633, 5761, 5889, 6017, 6145, 6273, 6401, 6529, 6657, 6785, 6913,1727041, 7169, 7297, 7425, 7553, 7681, 7809, 7937, 8065};173174175/* Macros for inflate() bit peeking and grabbing.176The usage is:177178NEEDBITS(j)179x = GETBITS(j)180DUMPBITS(j)181182where NEEDBITS makes sure that b has at least j bits in it, and183DUMPBITS removes the bits from b. The macros use the variable k184for the number of bits in b. Normally, b and k are register185variables for speed.186*/187188#define NEXTBYTE(p) ((p)->ip < (p)->ie ? (int)*(p)->ip++ : fill(p))189#define MASK_BITS(n) ((((ulg)1)<<(n))-1)190#define NEEDBITS(p,n) {while((p)->bit_len<(n)){(p)->bit_buf|=((ulg)NEXTBYTE(p))<<(p)->bit_len;(p)->bit_len+=8;}}191#define GETBITS(p,n) ((ulg)(p)->bit_buf & MASK_BITS(n))192#define IGETBITS(p,n) ((~((ulg)(p)->bit_buf)) & MASK_BITS(n))193#define DUMPBITS(p,n) {(p)->bit_buf>>=(n);(p)->bit_len-=(n);}194195static int196fill(State_t* state)197{198ssize_t r;199200if (state->eof)201return 0;202if ((r = sfrd(state->codex->sp, state->buf, sizeof(state->buf), &state->codex->sfdisc)) <= 0)203{204state->eof = 1;205return 0;206}207state->ie = (state->ip = state->buf) + r;208return *state->ip++;209}210211static int get_tree(212State_t* state,213ulg *l, /* bit lengths */214ulg n) /* number expected */215/* Get the bit lengths for a code representation from the compressed216stream. If get_tree() returns 4, then there is an error in the data.217Otherwise zero is returned. */218{219ulg i; /* bytes remaining in list */220ulg k; /* lengths entered */221ulg j; /* number of codes */222ulg b; /* bit length for those codes */223224/* get bit lengths */225i = NEXTBYTE(state) + 1; /* length/count pairs to read */226k = 0; /* next code */227do {228b = ((j = NEXTBYTE(state)) & 0xf) + 1; /* bits in code (1..16) */229j = ((j & 0xf0) >> 4) + 1; /* codes with those bits (1..16) */230if (k + j > n)231return 4; /* don't overflow l[] */232do {233l[k++] = b;234} while (--j);235} while (--i);236237return k != n ? 4 : 0; /* should have read n of them */238}239240static ssize_t explode_lit8(State_t* state, char *buff, size_t size)241/* Decompress the imploded data using coded literals and an 8K sliding242window. */243{244size_t s; /* bytes to decompress */245register ulg e; /* table entry flag/number of extra bits */246ulg n, d; /* length and index for copy */247ulg w; /* current window position */248Huff_t *t; /* pointer to table entry */249ulg u; /* true if unflushed */250size_t j;251Huff_t *tb, *tl, *td; /* literal, length, and distance tables */252int bb, bl, bd; /* number of bits decoded by those */253254tb = state->tb;255tl = state->tl;256td = state->td;257bb = state->bb;258bl = state->bl;259bd = state->bd;260261/* explode the coded data */262s = state->s;263w = state->w;264u = state->u;265j = 0;266267while(s > 0) /* do until ucsize bytes uncompressed */268{269NEEDBITS(state, 1);270if(state->bit_buf & 1) /* then literal--decode it */271{272DUMPBITS(state, 1);273s--;274NEEDBITS(state, (ulg)bb); /* get coded literal */275t = tb + IGETBITS(state, bb);276e = t->e;277while(e > 16)278{279if(e == 99)280return -1;281DUMPBITS(state, t->b);282e -= 16;283NEEDBITS(state, e);284t = t->v.t + IGETBITS(state, e);285e = t->e;286}287DUMPBITS(state, t->b);288buff[j++] = state->slide[w++] = (uch)t->v.n;289if(w == WSIZE)290w = u = 0;291if(j == size)292{293state->u = u;294state->w = w;295state->s = s;296return size;297}298}299else /* else distance/length */300{301DUMPBITS(state, 1);302NEEDBITS(state, 7); /* get distance low bits */303d = GETBITS(state, 7);304DUMPBITS(state, 7);305NEEDBITS(state, (ulg)bd); /* get coded distance high bits */306t = td + IGETBITS(state, bd);307e = t->e;308while(e > 16)309{310if(e == 99)311return -1;312DUMPBITS(state, t->b);313e -= 16;314NEEDBITS(state, e);315t = t->v.t + IGETBITS(state, e);316e = t->e;317}318DUMPBITS(state, t->b);319d = w - d - t->v.n; /* construct offset */320NEEDBITS(state, (ulg)bl); /* get coded length */321t = tl + IGETBITS(state, bl);322e = t->e;323while(e > 16)324{325if(e == 99)326return -1;327DUMPBITS(state, t->b);328e -= 16;329NEEDBITS(state, e);330t = t->v.t + IGETBITS(state, e);331e = t->e;332}333DUMPBITS(state, t->b);334n = t->v.n;335if(e) /* get length extra bits */336{337NEEDBITS(state, 8);338n += GETBITS(state, 8);339DUMPBITS(state, 8);340}341342/* do the copy */343s -= n;344while(n > 0 && j < size)345{346n--;347d &= WSIZE - 1;348w &= WSIZE - 1;349if(u && w <= d)350{351buff[j++] = 0;352w++;353d++;354}355else356buff[j++] = state->slide[w++] = state->slide[d++];357if(w == WSIZE)358w = u = 0;359}360if(j == size)361{362state->u = u;363state->n = n;364state->d = d;365state->w = w;366state->s = s;367return size;368}369state->n = 0;370}371}372373state->n = 0;374state->w = 0;375state->eof = 1;376return j;377}378379380381static ssize_t explode_lit4(State_t* state, char *buff, size_t size)382/* Decompress the imploded data using coded literals and a 4K sliding383window. */384{385size_t s; /* bytes to decompress */386register ulg e; /* table entry flag/number of extra bits */387ulg n, d; /* length and index for copy */388ulg w; /* current window position */389Huff_t *t; /* pointer to table entry */390ulg u; /* true if unflushed */391size_t j;392Huff_t *tb, *tl, *td; /* literal, length, and distance tables */393int bb, bl, bd; /* number of bits decoded by those */394395tb = state->tb;396tl = state->tl;397td = state->td;398bb = state->bb;399bl = state->bl;400bd = state->bd;401402/* explode the coded data */403s = state->s;404w = state->w;405u = state->u;406j = 0;407408while(s > 0) /* do until ucsize bytes uncompressed */409{410NEEDBITS(state, 1);411if(state->bit_buf & 1) /* then literal--decode it */412{413DUMPBITS(state, 1);414s--;415NEEDBITS(state, (ulg)bb); /* get coded literal */416t = tb + IGETBITS(state, bb);417e = t->e;418while(e > 16)419{420if(e == 99)421return -1;422DUMPBITS(state, t->b);423e -= 16;424NEEDBITS(state, e);425t = t->v.t + IGETBITS(state, e);426}427DUMPBITS(state, t->b);428buff[j++] = state->slide[w++] = (uch)t->v.n;429if(w == WSIZE)430w = u = 0;431if(j == size)432{433state->u = u;434state->w = w;435state->s = s;436return size;437}438}439else /* else distance/length */440{441DUMPBITS(state, 1);442NEEDBITS(state, 6); /* get distance low bits */443d = GETBITS(state, 6);444DUMPBITS(state, 6);445NEEDBITS(state, (ulg)bd); /* get coded distance high bits */446t = td + IGETBITS(state, bd);447e = t->e;448while(e > 16)449{450if(e == 99)451return -1;452DUMPBITS(state, t->b);453e -= 16;454NEEDBITS(state, e);455t = t->v.t + IGETBITS(state, e);456e = t->e;457}458DUMPBITS(state, t->b);459d = w - d - t->v.n; /* construct offset */460NEEDBITS(state, (ulg)bl); /* get coded length */461t = tl + IGETBITS(state, bl);462e = t->e;463while(e > 16)464{465if(e == 99)466return -1;467DUMPBITS(state, t->b);468e -= 16;469NEEDBITS(state, e);470t = t->v.t + IGETBITS(state, e);471e = t->e;472}473DUMPBITS(state, t->b);474n = t->v.n;475if(e) /* get length extra bits */476{477NEEDBITS(state, 8);478n += GETBITS(state, 8);479DUMPBITS(state, 8);480}481482/* do the copy */483s -= n;484while(n > 0 && j < size)485{486n--;487d &= WSIZE - 1;488w &= WSIZE - 1;489if(u && w <= d)490{491buff[j++] = 0;492w++;493d++;494}495else496buff[j++] = state->slide[w++] = state->slide[d++];497if(w == WSIZE)498w = u = 0;499}500if(j == size)501{502state->u = u;503state->n = n;504state->d = d;505state->w = w;506state->s = s;507return size;508}509state->n = 0;510}511}512513state->n = 0;514state->w = 0;515state->eof = 1;516return j;517}518519520521static ssize_t explode_nolit8(State_t* state, char *buff, size_t size)522/* Decompress the imploded data using uncoded literals and an 8K sliding523window. */524{525size_t s; /* bytes to decompress */526register ulg e; /* table entry flag/number of extra bits */527ulg n, d; /* length and index for copy */528ulg w; /* current window position */529Huff_t *t; /* pointer to table entry */530ulg u; /* true if unflushed */531size_t j;532Huff_t *tl, *td; /* length and distance state tables */533int bl, bd; /* number of bits decoded by tl[] and td[] */534535tl = state->tl;536td = state->td;537bl = state->bl;538bd = state->bd;539540/* explode the coded data */541542s = state->s;543w = state->w;544u = state->u;545j = 0;546547while(s > 0) /* do until ucsize bytes uncompressed */548{549NEEDBITS(state, 1);550if(state->bit_buf & 1) /* then literal--get eight bits */551{552DUMPBITS(state, 1);553s--;554NEEDBITS(state, 8);555buff[j++] = state->slide[w++] = (uch)state->bit_buf;;556DUMPBITS(state, 8);557if(w == WSIZE)558w = u = 0;559if(j == size)560{561state->u = u;562state->w = w;563state->s = s;564return size;565}566}567else /* else distance/length */568{569DUMPBITS(state, 1);570NEEDBITS(state, 7); /* get distance low bits */571d = GETBITS(state, 7);572DUMPBITS(state, 7);573NEEDBITS(state, (ulg)bd); /* get coded distance high bits */574t = td + IGETBITS(state, bd);575e = t->e;576while(e > 16)577{578if(e == 99)579return -1;580DUMPBITS(state, t->b);581e -= 16;582NEEDBITS(state, e);583t = t->v.t + IGETBITS(state, e);584e = t->e;585}586DUMPBITS(state, t->b);587d = w - d - t->v.n; /* construct offset */588NEEDBITS(state, (ulg)bl); /* get coded length */589t = tl + IGETBITS(state, bl);590e = t->e;591while(e > 16)592{593if(e == 99)594return -1;595DUMPBITS(state, t->b);596e -= 16;597NEEDBITS(state, e);598t = t->v.t + IGETBITS(state, e);599e = t->e;600}601DUMPBITS(state, t->b);602n = t->v.n;603if(e) /* get length extra bits */604{605NEEDBITS(state, 8);606n += GETBITS(state, 8);607DUMPBITS(state, 8);608}609610/* do the copy */611s -= n;612while(n > 0 && j < size)613{614n--;615d &= WSIZE - 1;616w &= WSIZE - 1;617if(u && w <= d)618{619buff[j++] = 0;620w++;621d++;622}623else624buff[j++] = state->slide[w++] = state->slide[d++];625if(w == WSIZE)626w = u = 0;627}628if(j == size)629{630state->u = u;631state->n = n;632state->d = d;633state->w = w;634state->s = s;635return size;636}637state->n = 0;638}639}640641state->n = 0;642state->w = 0;643state->eof = 1;644return j;645}646647648649static ssize_t explode_nolit4(State_t* state, char *buff, size_t size)650/* Decompress the imploded data using uncoded literals and a 4K sliding651window. */652{653size_t s; /* bytes to decompress */654register ulg e; /* table entry flag/number of extra bits */655ulg n, d; /* length and index for copy */656ulg w; /* current window position */657Huff_t *t; /* pointer to table entry */658ulg u; /* true if unflushed */659size_t j;660Huff_t *tl, *td; /* length and distance state tables */661int bl, bd; /* number of bits decoded by tl[] and td[] */662663tl = state->tl;664td = state->td;665bl = state->bl;666bd = state->bd;667668/* explode the coded data */669s = state->s;670w = state->w;671u = state->u;672j = 0;673674while(s > 0) /* do until ucsize bytes uncompressed */675{676NEEDBITS(state, 1);677if(state->bit_buf & 1) /* then literal--get eight bits */678{679DUMPBITS(state, 1);680s--;681NEEDBITS(state, 8);682buff[j++] = state->slide[w++] = (uch)state->bit_buf;683DUMPBITS(state, 8);684if(w == WSIZE)685w = u = 0;686if(j == size)687{688state->u = u;689state->w = w;690state->s = s;691return size;692}693}694else /* else distance/length */695{696DUMPBITS(state, 1);697NEEDBITS(state, 6); /* get distance low bits */698d = GETBITS(state, 6);699DUMPBITS(state, 6);700NEEDBITS(state, (ulg)bd); /* get coded distance high bits */701t = td + IGETBITS(state, bd);702e = t->e;703while(e > 16)704{705if(e == 99)706return -1;707DUMPBITS(state, t->b);708e -= 16;709NEEDBITS(state, e);710t = t->v.t + IGETBITS(state, e);711e = t->e;712}713DUMPBITS(state, t->b);714d = w - d - t->v.n; /* construct offset */715NEEDBITS(state, (ulg)bl); /* get coded length */716t = tl + IGETBITS(state, bl);717e = t->e;718while(e > 16)719{720if(e == 99)721return -1;722DUMPBITS(state, t->b);723e -= 16;724NEEDBITS(state, e);725t = t->v.t + IGETBITS(state, e);726e = t->e;727}728DUMPBITS(state, t->b);729n = t->v.n;730if(e) /* get length extra bits */731{732NEEDBITS(state, 8);733n += GETBITS(state, 8);734DUMPBITS(state, 8);735}736737/* do the copy */738s -= n;739while(n > 0 && j < size)740{741n--;742d &= WSIZE - 1;743w &= WSIZE - 1;744if(u && w <= d)745{746buff[j++] = 0;747w++;748d++;749}750else751buff[j++] = state->slide[w++] = state->slide[d++];752if(w == WSIZE)753w = u = 0;754}755if(j == size)756{757state->u = u;758state->n = n;759state->d = d;760state->w = w;761state->s = s;762return size;763}764state->n = 0;765}766}767768state->n = 0;769state->w = 0;770state->eof = 1;771return j;772}773774static int775implode_open(Codex_t* p, char* const args[], Codexnum_t flags)776{777State_t* state;778Vmalloc_t* vm;779char* const* a;780781if (!(vm = vmopen(Vmdcheap, Vmbest, 0)))782return -1;783if (!(state = newof(0, State_t, 1, 0)))784{785vmclose(vm);786return -1;787}788for (a = args; *a; a++)789switch (**a)790{791case 'l':792state->method |= EXPLODE_LIT;793break;794case '8':795state->method |= EXPLODE_BIG;796break;797}798switch (state->method)799{800case 0:801state->explodef = explode_nolit4;802break;803case EXPLODE_BIG:804state->explodef = explode_nolit8;805break;806case EXPLODE_LIT:807state->explodef = explode_lit4;808break;809case EXPLODE_LIT|EXPLODE_BIG:810state->explodef = explode_lit8;811break;812}813state->codex = p;814p->data = state;815return 0;816}817818static int819implode_close(Codex_t* p)820{821State_t* state = (State_t*)p->data;822823if (!state)824return -1;825if (state->vm)826vmclose(state->vm);827free(state);828return 0;829}830831static int832implode_init(Codex_t* p)833{834register State_t* state = (State_t*)p->data;835836vmclear(state->vm);837state->ip = state->ie = 0;838state->eof = 0;839state->u = 1;840state->s = p->size;841842/* Tune base table sizes. Note: I thought that to truly optimize speed,843I would have to select different bl, bd, and bb values for different844compressed file sizes. I was suprised to find out the the values of8457, 7, and 9 worked best over a very wide range of sizes, except that846bd = 8 worked marginally better for large compressed sizes. */847state->bl = 7;848state->bd = (state->s > 200000L ? 8 : 7);849850/* With literal tree--minimum match length is 3 */851if(state->method & EXPLODE_LIT)852{853state->bb = 9; /* base table size for literals */854if(get_tree(state, state->l, 256) != 0)855return -1;856857if(huff(state->l, 256, 256, NULL, NULL,858&state->tb, &state->bb, state->vm) != 0)859return -1;860861if(get_tree(state, state->l, 64) != 0)862return -1;863864if(huff(state->l, 64, 0, cplen3, extra,865&state->tl, &state->bl, state->vm) != 0)866return -1;867868if(get_tree(state, state->l, 64) != 0)869return -1;870871if(state->method & EXPLODE_BIG)872{873if(huff(state->l, 64, 0, cpdist8, extra,874&state->td, &state->bd, state->vm) != 0)875return -1;876}877else878{879if(huff(state->l, 64, 0, cpdist4, extra,880&state->td, &state->bd, state->vm) != 0)881return -1;882}883}884else885{886if(get_tree(state, state->l, 64) != 0)887return -1;888889if(huff(state->l, 64, 0, cplen2, extra,890&state->tl, &state->bl, state->vm) != 0)891return -1;892893if(get_tree(state, state->l, 64) != 0)894return -1;895896if(state->method & EXPLODE_BIG)897{898if(huff(state->l, 64, 0, cpdist8, extra,899&state->td, &state->bd, state->vm) != 0)900return -1;901}902else903{904if(huff(state->l, 64, 0, cpdist4, extra,905&state->td, &state->bd, state->vm) != 0)906return -1;907}908}909return 0;910}911912/* Explode an imploded compressed stream. Based on the general purpose913bit flag, decide on coded or uncoded literals, and an 8K or 4K sliding914window. Construct the literal (if any), length, and distance codes and915the tables needed to decode them (using huff() from inflate.c),916and call the appropriate routine for the type of data in the remainder917of the stream. The four routines are nearly identical, differing only918in whether the literal is decoded or simply read in, and in how many919bits are read in, uncoded, for the low distance bits. */920921static ssize_t922implode_read(Sfio_t* sp, void* buff, size_t size, Sfdisc_t* disc)923{924register State_t* state = (State_t*)CODEX(disc)->data;925size_t j, i;926927if(size <= 0)928return size;929930j = 0;931while(j < size)932{933if(state->n > 0) /* do the copy */934{935ulg u, n, w, d;936937u = state->u;938n = state->n;939d = state->d;940w = state->w;941while(n > 0 && j < size)942{943n--;944d &= WSIZE - 1;945w &= WSIZE - 1;946if(u && w <= d)947{948((char*)buff)[j++] = 0;949w++;950d++;951}952else953((char*)buff)[j++] = state->slide[w++] = state->slide[d++];954if(w == WSIZE)955w = u = 0;956}957958state->u = u;959state->n = n;960state->d = d;961state->w = w;962if(j == size)963return size;964}965966/* state->n == 0 */967if(state->eof)968return j;969970i = (*state->explodef)(state, (char*)buff + j, size - j);971if(i == -1)972return -1;973j += i;974}975return j;976}977978Codexmeth_t codex_zip_implode =979{980"implode",981"zip implode compression (PKZIP method 6). Options are window size"982" { 4K 8K } and \bliteral\b for literal coding.",9830,984CODEX_DECODE|CODEX_COMPRESS,9850,9860,987implode_open,988implode_close,989implode_init,9900,991implode_read,9920,9930,9940,9950,9960,9970,9980999};100010011002