Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/codexlib/zip/implode.c
1810 views
1
#pragma prototyped
2
3
/*
4
* zip implode decoder
5
*/
6
7
#include "zip.h"
8
#include "huff.h"
9
10
#define EXPLODE_BIG 01
11
#define EXPLODE_LIT 02
12
13
#define WSIZE 32768
14
15
/* explode.c -- put in the public domain by Mark Adler
16
version c15, 6 July 1996 */
17
18
19
/* You can do whatever you like with this source file, though I would
20
prefer that if you modify it and redistribute it that you include
21
comments to that effect with your name and the date. Thank you.
22
23
History:
24
vers date who what
25
---- --------- -------------- ------------------------------------
26
c1 30 Mar 92 M. Adler explode that uses huff() from inflate
27
(this gives over a 70% speed improvement
28
over the original unimplode.c, which
29
decoded a bit at a time)
30
c2 4 Apr 92 M. Adler fixed bug for file sizes a multiple of 32k.
31
c3 10 Apr 92 M. Adler added a little memory tracking if DEBUG
32
c4 11 Apr 92 M. Adler added NOMEMCPY do kill use of memcpy()
33
c5 21 Apr 92 M. Adler added the WSIZE #define to allow reducing
34
the 32K window size for specialized
35
applications.
36
c6 31 May 92 M. Adler added typecasts to eliminate some warnings
37
c7 27 Jun 92 G. Roelofs added more typecasts.
38
c8 17 Oct 92 G. Roelofs changed ULONG/UWORD/byte to ulg/ush/uch.
39
c9 19 Jul 93 J. Bush added more typecasts (to return values);
40
made l[256] array static for Amiga.
41
c10 8 Oct 93 G. Roelofs added used_csize for diagnostics; added
42
buf and unshrink arguments to flush();
43
undef'd various macros at end for Turbo C;
44
removed NEXTBYTE macro (now in unzip.h)
45
and bytebuf variable (not used); changed
46
memset() to memzero().
47
c11 9 Jan 94 M. Adler fixed incorrect used_csize calculation.
48
c12 9 Apr 94 G. Roelofs fixed split comments on preprocessor lines
49
to avoid bug in Encore compiler.
50
c13 25 Aug 94 M. Adler fixed distance-length comment (orig c9 fix)
51
c14 22 Nov 95 S. Maxwell removed unnecessary "static" on auto array
52
c15 6 Jul 96 W. Haidinger added ulg typecasts to flush() calls
53
*/
54
55
56
/*
57
Explode imploded (PKZIP method 6 compressed) data. This compression
58
method searches for as much of the current string of bytes (up to a length
59
of ~320) in the previous 4K or 8K bytes. If it doesn't find any matches
60
(of at least length 2 or 3), it codes the next byte. Otherwise, it codes
61
the length of the matched string and its distance backwards from the
62
current position. Single bytes ("literals") are preceded by a one (a
63
single bit) and are either uncoded (the eight bits go directly into the
64
compressed stream for a total of nine bits) or Huffman coded with a
65
supplied literal code tree. If literals are coded, then the minimum match
66
length is three, otherwise it is two.
67
68
There are therefore four kinds of imploded streams: 8K search with coded
69
literals (min match = 3), 4K search with coded literals (min match = 3),
70
8K with uncoded literals (min match = 2), and 4K with uncoded literals
71
(min match = 2). The kind of stream is identified in two bits of a
72
general purpose bit flag that is outside of the compressed stream.
73
74
Distance-length pairs for matched strings are preceded by a zero bit (to
75
distinguish them from literals) and are always coded. The distance comes
76
first and is either the low six (4K) or low seven (8K) bits of the
77
distance (uncoded), followed by the high six bits of the distance coded.
78
Then the length is six bits coded (0..63 + min match length), and if the
79
maximum such length is coded, then it's followed by another eight bits
80
(uncoded) to be added to the coded length. This gives a match length
81
range of 2..320 or 3..321 bytes.
82
83
The literal, length, and distance codes are all represented in a slightly
84
compressed form themselves. What is sent are the lengths of the codes for
85
each value, which is sufficient to construct the codes. Each byte of the
86
code representation is the code length (the low four bits representing
87
1..16), and the number of values sequentially with that length (the high
88
four bits also representing 1..16). There are 256 literal code values (if
89
literals are coded), 64 length code values, and 64 distance code values,
90
in that order at the beginning of the compressed stream. Each set of code
91
values is preceded (redundantly) with a byte indicating how many bytes are
92
in the code description that follows, in the range 1..256.
93
94
The codes themselves are decoded using tables made by huff() from
95
the bit lengths. That routine and its comments are in the inflate.c
96
module.
97
*/
98
99
struct State_s; typedef struct State_s State_t;
100
101
struct State_s
102
{
103
Codex_t* codex;
104
int method;
105
ssize_t (*explodef)(State_t*, char*, size_t);
106
107
uch buf[SF_BUFSIZE];
108
uch* ip;
109
uch* ie;
110
111
ulg bit_buf; /* bit buffer */
112
ulg bit_len; /* bits in bit buffer */
113
114
uch slide[WSIZE];
115
Huff_t *tb; /* literal, length, and distance tables */
116
Huff_t *tl;
117
Huff_t *td;
118
int bb; /* number of bits decoded by those */
119
int bl;
120
int bd;
121
122
ulg u, n, d, w;
123
size_t s; /* original size */
124
ulg l[256]; /* bit lengths for codes */
125
126
Vmalloc_t* vm;
127
128
int eof;
129
};
130
131
/* The implode algorithm uses a sliding 4K or 8K byte window on the
132
uncompressed stream to find repeated byte strings. This is implemented
133
here as a circular buffer. The index is updated simply by incrementing
134
and then and'ing with 0x0fff (4K-1) or 0x1fff (8K-1). Here, the 32K
135
buffer of inflate is used, and it works just as well to always have
136
a 32K circular buffer, so the index is anded with 0x7fff. This is
137
done to allow the window to also be used as the output buffer. */
138
/* This must be supplied in an external module useable like "uch slide[8192];"
139
or "uch *slide;", where the latter would be malloc'ed. In unzip, slide[]
140
is actually a 32K area for use by inflate, which uses a 32K sliding window.
141
*/
142
143
144
/* Tables for length and distance */
145
static ush cplen2[] =
146
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
147
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
148
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
149
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65};
150
static ush cplen3[] =
151
{3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
152
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
153
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
154
53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66};
155
static ush extra[] =
156
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
157
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
158
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159
8};
160
static ush cpdist4[] =
161
{1, 65, 129, 193, 257, 321, 385, 449, 513, 577, 641, 705,
162
769, 833, 897, 961, 1025, 1089, 1153, 1217, 1281, 1345, 1409, 1473,
163
1537, 1601, 1665, 1729, 1793, 1857, 1921, 1985, 2049, 2113, 2177,
164
2241, 2305, 2369, 2433, 2497, 2561, 2625, 2689, 2753, 2817, 2881,
165
2945, 3009, 3073, 3137, 3201, 3265, 3329, 3393, 3457, 3521, 3585,
166
3649, 3713, 3777, 3841, 3905, 3969, 4033};
167
static ush cpdist8[] =
168
{1, 129, 257, 385, 513, 641, 769, 897, 1025, 1153, 1281,
169
1409, 1537, 1665, 1793, 1921, 2049, 2177, 2305, 2433, 2561, 2689,
170
2817, 2945, 3073, 3201, 3329, 3457, 3585, 3713, 3841, 3969, 4097,
171
4225, 4353, 4481, 4609, 4737, 4865, 4993, 5121, 5249, 5377, 5505,
172
5633, 5761, 5889, 6017, 6145, 6273, 6401, 6529, 6657, 6785, 6913,
173
7041, 7169, 7297, 7425, 7553, 7681, 7809, 7937, 8065};
174
175
176
/* Macros for inflate() bit peeking and grabbing.
177
The usage is:
178
179
NEEDBITS(j)
180
x = GETBITS(j)
181
DUMPBITS(j)
182
183
where NEEDBITS makes sure that b has at least j bits in it, and
184
DUMPBITS removes the bits from b. The macros use the variable k
185
for the number of bits in b. Normally, b and k are register
186
variables for speed.
187
*/
188
189
#define NEXTBYTE(p) ((p)->ip < (p)->ie ? (int)*(p)->ip++ : fill(p))
190
#define MASK_BITS(n) ((((ulg)1)<<(n))-1)
191
#define NEEDBITS(p,n) {while((p)->bit_len<(n)){(p)->bit_buf|=((ulg)NEXTBYTE(p))<<(p)->bit_len;(p)->bit_len+=8;}}
192
#define GETBITS(p,n) ((ulg)(p)->bit_buf & MASK_BITS(n))
193
#define IGETBITS(p,n) ((~((ulg)(p)->bit_buf)) & MASK_BITS(n))
194
#define DUMPBITS(p,n) {(p)->bit_buf>>=(n);(p)->bit_len-=(n);}
195
196
static int
197
fill(State_t* state)
198
{
199
ssize_t r;
200
201
if (state->eof)
202
return 0;
203
if ((r = sfrd(state->codex->sp, state->buf, sizeof(state->buf), &state->codex->sfdisc)) <= 0)
204
{
205
state->eof = 1;
206
return 0;
207
}
208
state->ie = (state->ip = state->buf) + r;
209
return *state->ip++;
210
}
211
212
static int get_tree(
213
State_t* state,
214
ulg *l, /* bit lengths */
215
ulg n) /* number expected */
216
/* Get the bit lengths for a code representation from the compressed
217
stream. If get_tree() returns 4, then there is an error in the data.
218
Otherwise zero is returned. */
219
{
220
ulg i; /* bytes remaining in list */
221
ulg k; /* lengths entered */
222
ulg j; /* number of codes */
223
ulg b; /* bit length for those codes */
224
225
/* get bit lengths */
226
i = NEXTBYTE(state) + 1; /* length/count pairs to read */
227
k = 0; /* next code */
228
do {
229
b = ((j = NEXTBYTE(state)) & 0xf) + 1; /* bits in code (1..16) */
230
j = ((j & 0xf0) >> 4) + 1; /* codes with those bits (1..16) */
231
if (k + j > n)
232
return 4; /* don't overflow l[] */
233
do {
234
l[k++] = b;
235
} while (--j);
236
} while (--i);
237
238
return k != n ? 4 : 0; /* should have read n of them */
239
}
240
241
static ssize_t explode_lit8(State_t* state, char *buff, size_t size)
242
/* Decompress the imploded data using coded literals and an 8K sliding
243
window. */
244
{
245
size_t s; /* bytes to decompress */
246
register ulg e; /* table entry flag/number of extra bits */
247
ulg n, d; /* length and index for copy */
248
ulg w; /* current window position */
249
Huff_t *t; /* pointer to table entry */
250
ulg u; /* true if unflushed */
251
size_t j;
252
Huff_t *tb, *tl, *td; /* literal, length, and distance tables */
253
int bb, bl, bd; /* number of bits decoded by those */
254
255
tb = state->tb;
256
tl = state->tl;
257
td = state->td;
258
bb = state->bb;
259
bl = state->bl;
260
bd = state->bd;
261
262
/* explode the coded data */
263
s = state->s;
264
w = state->w;
265
u = state->u;
266
j = 0;
267
268
while(s > 0) /* do until ucsize bytes uncompressed */
269
{
270
NEEDBITS(state, 1);
271
if(state->bit_buf & 1) /* then literal--decode it */
272
{
273
DUMPBITS(state, 1);
274
s--;
275
NEEDBITS(state, (ulg)bb); /* get coded literal */
276
t = tb + IGETBITS(state, bb);
277
e = t->e;
278
while(e > 16)
279
{
280
if(e == 99)
281
return -1;
282
DUMPBITS(state, t->b);
283
e -= 16;
284
NEEDBITS(state, e);
285
t = t->v.t + IGETBITS(state, e);
286
e = t->e;
287
}
288
DUMPBITS(state, t->b);
289
buff[j++] = state->slide[w++] = (uch)t->v.n;
290
if(w == WSIZE)
291
w = u = 0;
292
if(j == size)
293
{
294
state->u = u;
295
state->w = w;
296
state->s = s;
297
return size;
298
}
299
}
300
else /* else distance/length */
301
{
302
DUMPBITS(state, 1);
303
NEEDBITS(state, 7); /* get distance low bits */
304
d = GETBITS(state, 7);
305
DUMPBITS(state, 7);
306
NEEDBITS(state, (ulg)bd); /* get coded distance high bits */
307
t = td + IGETBITS(state, bd);
308
e = t->e;
309
while(e > 16)
310
{
311
if(e == 99)
312
return -1;
313
DUMPBITS(state, t->b);
314
e -= 16;
315
NEEDBITS(state, e);
316
t = t->v.t + IGETBITS(state, e);
317
e = t->e;
318
}
319
DUMPBITS(state, t->b);
320
d = w - d - t->v.n; /* construct offset */
321
NEEDBITS(state, (ulg)bl); /* get coded length */
322
t = tl + IGETBITS(state, bl);
323
e = t->e;
324
while(e > 16)
325
{
326
if(e == 99)
327
return -1;
328
DUMPBITS(state, t->b);
329
e -= 16;
330
NEEDBITS(state, e);
331
t = t->v.t + IGETBITS(state, e);
332
e = t->e;
333
}
334
DUMPBITS(state, t->b);
335
n = t->v.n;
336
if(e) /* get length extra bits */
337
{
338
NEEDBITS(state, 8);
339
n += GETBITS(state, 8);
340
DUMPBITS(state, 8);
341
}
342
343
/* do the copy */
344
s -= n;
345
while(n > 0 && j < size)
346
{
347
n--;
348
d &= WSIZE - 1;
349
w &= WSIZE - 1;
350
if(u && w <= d)
351
{
352
buff[j++] = 0;
353
w++;
354
d++;
355
}
356
else
357
buff[j++] = state->slide[w++] = state->slide[d++];
358
if(w == WSIZE)
359
w = u = 0;
360
}
361
if(j == size)
362
{
363
state->u = u;
364
state->n = n;
365
state->d = d;
366
state->w = w;
367
state->s = s;
368
return size;
369
}
370
state->n = 0;
371
}
372
}
373
374
state->n = 0;
375
state->w = 0;
376
state->eof = 1;
377
return j;
378
}
379
380
381
382
static ssize_t explode_lit4(State_t* state, char *buff, size_t size)
383
/* Decompress the imploded data using coded literals and a 4K sliding
384
window. */
385
{
386
size_t s; /* bytes to decompress */
387
register ulg e; /* table entry flag/number of extra bits */
388
ulg n, d; /* length and index for copy */
389
ulg w; /* current window position */
390
Huff_t *t; /* pointer to table entry */
391
ulg u; /* true if unflushed */
392
size_t j;
393
Huff_t *tb, *tl, *td; /* literal, length, and distance tables */
394
int bb, bl, bd; /* number of bits decoded by those */
395
396
tb = state->tb;
397
tl = state->tl;
398
td = state->td;
399
bb = state->bb;
400
bl = state->bl;
401
bd = state->bd;
402
403
/* explode the coded data */
404
s = state->s;
405
w = state->w;
406
u = state->u;
407
j = 0;
408
409
while(s > 0) /* do until ucsize bytes uncompressed */
410
{
411
NEEDBITS(state, 1);
412
if(state->bit_buf & 1) /* then literal--decode it */
413
{
414
DUMPBITS(state, 1);
415
s--;
416
NEEDBITS(state, (ulg)bb); /* get coded literal */
417
t = tb + IGETBITS(state, bb);
418
e = t->e;
419
while(e > 16)
420
{
421
if(e == 99)
422
return -1;
423
DUMPBITS(state, t->b);
424
e -= 16;
425
NEEDBITS(state, e);
426
t = t->v.t + IGETBITS(state, e);
427
}
428
DUMPBITS(state, t->b);
429
buff[j++] = state->slide[w++] = (uch)t->v.n;
430
if(w == WSIZE)
431
w = u = 0;
432
if(j == size)
433
{
434
state->u = u;
435
state->w = w;
436
state->s = s;
437
return size;
438
}
439
}
440
else /* else distance/length */
441
{
442
DUMPBITS(state, 1);
443
NEEDBITS(state, 6); /* get distance low bits */
444
d = GETBITS(state, 6);
445
DUMPBITS(state, 6);
446
NEEDBITS(state, (ulg)bd); /* get coded distance high bits */
447
t = td + IGETBITS(state, bd);
448
e = t->e;
449
while(e > 16)
450
{
451
if(e == 99)
452
return -1;
453
DUMPBITS(state, t->b);
454
e -= 16;
455
NEEDBITS(state, e);
456
t = t->v.t + IGETBITS(state, e);
457
e = t->e;
458
}
459
DUMPBITS(state, t->b);
460
d = w - d - t->v.n; /* construct offset */
461
NEEDBITS(state, (ulg)bl); /* get coded length */
462
t = tl + IGETBITS(state, bl);
463
e = t->e;
464
while(e > 16)
465
{
466
if(e == 99)
467
return -1;
468
DUMPBITS(state, t->b);
469
e -= 16;
470
NEEDBITS(state, e);
471
t = t->v.t + IGETBITS(state, e);
472
e = t->e;
473
}
474
DUMPBITS(state, t->b);
475
n = t->v.n;
476
if(e) /* get length extra bits */
477
{
478
NEEDBITS(state, 8);
479
n += GETBITS(state, 8);
480
DUMPBITS(state, 8);
481
}
482
483
/* do the copy */
484
s -= n;
485
while(n > 0 && j < size)
486
{
487
n--;
488
d &= WSIZE - 1;
489
w &= WSIZE - 1;
490
if(u && w <= d)
491
{
492
buff[j++] = 0;
493
w++;
494
d++;
495
}
496
else
497
buff[j++] = state->slide[w++] = state->slide[d++];
498
if(w == WSIZE)
499
w = u = 0;
500
}
501
if(j == size)
502
{
503
state->u = u;
504
state->n = n;
505
state->d = d;
506
state->w = w;
507
state->s = s;
508
return size;
509
}
510
state->n = 0;
511
}
512
}
513
514
state->n = 0;
515
state->w = 0;
516
state->eof = 1;
517
return j;
518
}
519
520
521
522
static ssize_t explode_nolit8(State_t* state, char *buff, size_t size)
523
/* Decompress the imploded data using uncoded literals and an 8K sliding
524
window. */
525
{
526
size_t s; /* bytes to decompress */
527
register ulg e; /* table entry flag/number of extra bits */
528
ulg n, d; /* length and index for copy */
529
ulg w; /* current window position */
530
Huff_t *t; /* pointer to table entry */
531
ulg u; /* true if unflushed */
532
size_t j;
533
Huff_t *tl, *td; /* length and distance state tables */
534
int bl, bd; /* number of bits decoded by tl[] and td[] */
535
536
tl = state->tl;
537
td = state->td;
538
bl = state->bl;
539
bd = state->bd;
540
541
/* explode the coded data */
542
543
s = state->s;
544
w = state->w;
545
u = state->u;
546
j = 0;
547
548
while(s > 0) /* do until ucsize bytes uncompressed */
549
{
550
NEEDBITS(state, 1);
551
if(state->bit_buf & 1) /* then literal--get eight bits */
552
{
553
DUMPBITS(state, 1);
554
s--;
555
NEEDBITS(state, 8);
556
buff[j++] = state->slide[w++] = (uch)state->bit_buf;;
557
DUMPBITS(state, 8);
558
if(w == WSIZE)
559
w = u = 0;
560
if(j == size)
561
{
562
state->u = u;
563
state->w = w;
564
state->s = s;
565
return size;
566
}
567
}
568
else /* else distance/length */
569
{
570
DUMPBITS(state, 1);
571
NEEDBITS(state, 7); /* get distance low bits */
572
d = GETBITS(state, 7);
573
DUMPBITS(state, 7);
574
NEEDBITS(state, (ulg)bd); /* get coded distance high bits */
575
t = td + IGETBITS(state, bd);
576
e = t->e;
577
while(e > 16)
578
{
579
if(e == 99)
580
return -1;
581
DUMPBITS(state, t->b);
582
e -= 16;
583
NEEDBITS(state, e);
584
t = t->v.t + IGETBITS(state, e);
585
e = t->e;
586
}
587
DUMPBITS(state, t->b);
588
d = w - d - t->v.n; /* construct offset */
589
NEEDBITS(state, (ulg)bl); /* get coded length */
590
t = tl + IGETBITS(state, bl);
591
e = t->e;
592
while(e > 16)
593
{
594
if(e == 99)
595
return -1;
596
DUMPBITS(state, t->b);
597
e -= 16;
598
NEEDBITS(state, e);
599
t = t->v.t + IGETBITS(state, e);
600
e = t->e;
601
}
602
DUMPBITS(state, t->b);
603
n = t->v.n;
604
if(e) /* get length extra bits */
605
{
606
NEEDBITS(state, 8);
607
n += GETBITS(state, 8);
608
DUMPBITS(state, 8);
609
}
610
611
/* do the copy */
612
s -= n;
613
while(n > 0 && j < size)
614
{
615
n--;
616
d &= WSIZE - 1;
617
w &= WSIZE - 1;
618
if(u && w <= d)
619
{
620
buff[j++] = 0;
621
w++;
622
d++;
623
}
624
else
625
buff[j++] = state->slide[w++] = state->slide[d++];
626
if(w == WSIZE)
627
w = u = 0;
628
}
629
if(j == size)
630
{
631
state->u = u;
632
state->n = n;
633
state->d = d;
634
state->w = w;
635
state->s = s;
636
return size;
637
}
638
state->n = 0;
639
}
640
}
641
642
state->n = 0;
643
state->w = 0;
644
state->eof = 1;
645
return j;
646
}
647
648
649
650
static ssize_t explode_nolit4(State_t* state, char *buff, size_t size)
651
/* Decompress the imploded data using uncoded literals and a 4K sliding
652
window. */
653
{
654
size_t s; /* bytes to decompress */
655
register ulg e; /* table entry flag/number of extra bits */
656
ulg n, d; /* length and index for copy */
657
ulg w; /* current window position */
658
Huff_t *t; /* pointer to table entry */
659
ulg u; /* true if unflushed */
660
size_t j;
661
Huff_t *tl, *td; /* length and distance state tables */
662
int bl, bd; /* number of bits decoded by tl[] and td[] */
663
664
tl = state->tl;
665
td = state->td;
666
bl = state->bl;
667
bd = state->bd;
668
669
/* explode the coded data */
670
s = state->s;
671
w = state->w;
672
u = state->u;
673
j = 0;
674
675
while(s > 0) /* do until ucsize bytes uncompressed */
676
{
677
NEEDBITS(state, 1);
678
if(state->bit_buf & 1) /* then literal--get eight bits */
679
{
680
DUMPBITS(state, 1);
681
s--;
682
NEEDBITS(state, 8);
683
buff[j++] = state->slide[w++] = (uch)state->bit_buf;
684
DUMPBITS(state, 8);
685
if(w == WSIZE)
686
w = u = 0;
687
if(j == size)
688
{
689
state->u = u;
690
state->w = w;
691
state->s = s;
692
return size;
693
}
694
}
695
else /* else distance/length */
696
{
697
DUMPBITS(state, 1);
698
NEEDBITS(state, 6); /* get distance low bits */
699
d = GETBITS(state, 6);
700
DUMPBITS(state, 6);
701
NEEDBITS(state, (ulg)bd); /* get coded distance high bits */
702
t = td + IGETBITS(state, bd);
703
e = t->e;
704
while(e > 16)
705
{
706
if(e == 99)
707
return -1;
708
DUMPBITS(state, t->b);
709
e -= 16;
710
NEEDBITS(state, e);
711
t = t->v.t + IGETBITS(state, e);
712
e = t->e;
713
}
714
DUMPBITS(state, t->b);
715
d = w - d - t->v.n; /* construct offset */
716
NEEDBITS(state, (ulg)bl); /* get coded length */
717
t = tl + IGETBITS(state, bl);
718
e = t->e;
719
while(e > 16)
720
{
721
if(e == 99)
722
return -1;
723
DUMPBITS(state, t->b);
724
e -= 16;
725
NEEDBITS(state, e);
726
t = t->v.t + IGETBITS(state, e);
727
e = t->e;
728
}
729
DUMPBITS(state, t->b);
730
n = t->v.n;
731
if(e) /* get length extra bits */
732
{
733
NEEDBITS(state, 8);
734
n += GETBITS(state, 8);
735
DUMPBITS(state, 8);
736
}
737
738
/* do the copy */
739
s -= n;
740
while(n > 0 && j < size)
741
{
742
n--;
743
d &= WSIZE - 1;
744
w &= WSIZE - 1;
745
if(u && w <= d)
746
{
747
buff[j++] = 0;
748
w++;
749
d++;
750
}
751
else
752
buff[j++] = state->slide[w++] = state->slide[d++];
753
if(w == WSIZE)
754
w = u = 0;
755
}
756
if(j == size)
757
{
758
state->u = u;
759
state->n = n;
760
state->d = d;
761
state->w = w;
762
state->s = s;
763
return size;
764
}
765
state->n = 0;
766
}
767
}
768
769
state->n = 0;
770
state->w = 0;
771
state->eof = 1;
772
return j;
773
}
774
775
static int
776
implode_open(Codex_t* p, char* const args[], Codexnum_t flags)
777
{
778
State_t* state;
779
Vmalloc_t* vm;
780
char* const* a;
781
782
if (!(vm = vmopen(Vmdcheap, Vmbest, 0)))
783
return -1;
784
if (!(state = newof(0, State_t, 1, 0)))
785
{
786
vmclose(vm);
787
return -1;
788
}
789
for (a = args; *a; a++)
790
switch (**a)
791
{
792
case 'l':
793
state->method |= EXPLODE_LIT;
794
break;
795
case '8':
796
state->method |= EXPLODE_BIG;
797
break;
798
}
799
switch (state->method)
800
{
801
case 0:
802
state->explodef = explode_nolit4;
803
break;
804
case EXPLODE_BIG:
805
state->explodef = explode_nolit8;
806
break;
807
case EXPLODE_LIT:
808
state->explodef = explode_lit4;
809
break;
810
case EXPLODE_LIT|EXPLODE_BIG:
811
state->explodef = explode_lit8;
812
break;
813
}
814
state->codex = p;
815
p->data = state;
816
return 0;
817
}
818
819
static int
820
implode_close(Codex_t* p)
821
{
822
State_t* state = (State_t*)p->data;
823
824
if (!state)
825
return -1;
826
if (state->vm)
827
vmclose(state->vm);
828
free(state);
829
return 0;
830
}
831
832
static int
833
implode_init(Codex_t* p)
834
{
835
register State_t* state = (State_t*)p->data;
836
837
vmclear(state->vm);
838
state->ip = state->ie = 0;
839
state->eof = 0;
840
state->u = 1;
841
state->s = p->size;
842
843
/* Tune base table sizes. Note: I thought that to truly optimize speed,
844
I would have to select different bl, bd, and bb values for different
845
compressed file sizes. I was suprised to find out the the values of
846
7, 7, and 9 worked best over a very wide range of sizes, except that
847
bd = 8 worked marginally better for large compressed sizes. */
848
state->bl = 7;
849
state->bd = (state->s > 200000L ? 8 : 7);
850
851
/* With literal tree--minimum match length is 3 */
852
if(state->method & EXPLODE_LIT)
853
{
854
state->bb = 9; /* base table size for literals */
855
if(get_tree(state, state->l, 256) != 0)
856
return -1;
857
858
if(huff(state->l, 256, 256, NULL, NULL,
859
&state->tb, &state->bb, state->vm) != 0)
860
return -1;
861
862
if(get_tree(state, state->l, 64) != 0)
863
return -1;
864
865
if(huff(state->l, 64, 0, cplen3, extra,
866
&state->tl, &state->bl, state->vm) != 0)
867
return -1;
868
869
if(get_tree(state, state->l, 64) != 0)
870
return -1;
871
872
if(state->method & EXPLODE_BIG)
873
{
874
if(huff(state->l, 64, 0, cpdist8, extra,
875
&state->td, &state->bd, state->vm) != 0)
876
return -1;
877
}
878
else
879
{
880
if(huff(state->l, 64, 0, cpdist4, extra,
881
&state->td, &state->bd, state->vm) != 0)
882
return -1;
883
}
884
}
885
else
886
{
887
if(get_tree(state, state->l, 64) != 0)
888
return -1;
889
890
if(huff(state->l, 64, 0, cplen2, extra,
891
&state->tl, &state->bl, state->vm) != 0)
892
return -1;
893
894
if(get_tree(state, state->l, 64) != 0)
895
return -1;
896
897
if(state->method & EXPLODE_BIG)
898
{
899
if(huff(state->l, 64, 0, cpdist8, extra,
900
&state->td, &state->bd, state->vm) != 0)
901
return -1;
902
}
903
else
904
{
905
if(huff(state->l, 64, 0, cpdist4, extra,
906
&state->td, &state->bd, state->vm) != 0)
907
return -1;
908
}
909
}
910
return 0;
911
}
912
913
/* Explode an imploded compressed stream. Based on the general purpose
914
bit flag, decide on coded or uncoded literals, and an 8K or 4K sliding
915
window. Construct the literal (if any), length, and distance codes and
916
the tables needed to decode them (using huff() from inflate.c),
917
and call the appropriate routine for the type of data in the remainder
918
of the stream. The four routines are nearly identical, differing only
919
in whether the literal is decoded or simply read in, and in how many
920
bits are read in, uncoded, for the low distance bits. */
921
922
static ssize_t
923
implode_read(Sfio_t* sp, void* buff, size_t size, Sfdisc_t* disc)
924
{
925
register State_t* state = (State_t*)CODEX(disc)->data;
926
size_t j, i;
927
928
if(size <= 0)
929
return size;
930
931
j = 0;
932
while(j < size)
933
{
934
if(state->n > 0) /* do the copy */
935
{
936
ulg u, n, w, d;
937
938
u = state->u;
939
n = state->n;
940
d = state->d;
941
w = state->w;
942
while(n > 0 && j < size)
943
{
944
n--;
945
d &= WSIZE - 1;
946
w &= WSIZE - 1;
947
if(u && w <= d)
948
{
949
((char*)buff)[j++] = 0;
950
w++;
951
d++;
952
}
953
else
954
((char*)buff)[j++] = state->slide[w++] = state->slide[d++];
955
if(w == WSIZE)
956
w = u = 0;
957
}
958
959
state->u = u;
960
state->n = n;
961
state->d = d;
962
state->w = w;
963
if(j == size)
964
return size;
965
}
966
967
/* state->n == 0 */
968
if(state->eof)
969
return j;
970
971
i = (*state->explodef)(state, (char*)buff + j, size - j);
972
if(i == -1)
973
return -1;
974
j += i;
975
}
976
return j;
977
}
978
979
Codexmeth_t codex_zip_implode =
980
{
981
"implode",
982
"zip implode compression (PKZIP method 6). Options are window size"
983
" { 4K 8K } and \bliteral\b for literal coding.",
984
0,
985
CODEX_DECODE|CODEX_COMPRESS,
986
0,
987
0,
988
implode_open,
989
implode_close,
990
implode_init,
991
0,
992
implode_read,
993
0,
994
0,
995
0,
996
0,
997
0,
998
0,
999
0
1000
};
1001
1002