Update deflate.c to include nearly all the changes I've been making
[sgt/halibut] / deflate.c
1 /*
2 * Reimplementation of Deflate (RFC1951) compression. Adapted from
3 * the version in PuTTY, and extended to write dynamic Huffman
4 * trees and choose block boundaries usefully.
5 */
6
7 /*
8 * TODO:
9 *
10 * - Feature: could do with forms of flush other than SYNC_FLUSH.
11 * I'm not sure exactly how those work when you don't know in
12 * advance that your next block will be static (as we did in
13 * PuTTY). And remember the 9-bit limitation of zlib.
14 * + also, zlib has FULL_FLUSH which clears the LZ77 state as
15 * well, for random access.
16 *
17 * - Compression quality: chooseblock() appears to be computing
18 * wildly inaccurate block size estimates. Possible resolutions:
19 * + find and fix some trivial bug I haven't spotted yet
20 * + abandon the entropic approximation and go with trial
21 * Huffman runs
22 *
23 * - Compression quality: see if increasing SYMLIMIT causes
24 * dynamic blocks to start being consistently smaller than it.
25 * + actually we seem to be there already, but check on a
26 * larger corpus.
27 *
28 * - Compression quality: we ought to be able to fall right back
29 * to actual uncompressed blocks if really necessary, though
30 * it's not clear what the criterion for doing so would be.
31 */
32
33 /*
34 * This software is copyright 2000-2006 Simon Tatham.
35 *
36 * Permission is hereby granted, free of charge, to any person
37 * obtaining a copy of this software and associated documentation
38 * files (the "Software"), to deal in the Software without
39 * restriction, including without limitation the rights to use,
40 * copy, modify, merge, publish, distribute, sublicense, and/or
41 * sell copies of the Software, and to permit persons to whom the
42 * Software is furnished to do so, subject to the following
43 * conditions:
44 *
45 * The above copyright notice and this permission notice shall be
46 * included in all copies or substantial portions of the Software.
47 *
48 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
49 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
50 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
51 * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE
52 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
53 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
54 * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
55 * THE SOFTWARE.
56 */
57
58 #include <stdio.h>
59 #include <stddef.h>
60 #include <string.h>
61 #include <stdlib.h>
62 #include <assert.h>
63
64 #include "deflate.h"
65
66 #define snew(type) ( (type *) malloc(sizeof(type)) )
67 #define snewn(n, type) ( (type *) malloc((n) * sizeof(type)) )
68 #define sresize(x, n, type) ( (type *) realloc((x), (n) * sizeof(type)) )
69 #define sfree(x) ( free((x)) )
70
71 #define lenof(x) (sizeof((x)) / sizeof(*(x)))
72
73 #ifndef FALSE
74 #define FALSE 0
75 #define TRUE (!FALSE)
76 #endif
77
78 /* ----------------------------------------------------------------------
79 * This file can be compiled in a number of modes.
80 *
81 * With -DSTANDALONE, it builds a self-contained deflate tool which
82 * can compress, decompress, and also analyse a deflated file to
83 * print out the sequence of literals and copy commands it
84 * contains.
85 *
86 * With -DTESTMODE, it builds a test application which is given a
87 * file on standard input, both compresses and decompresses it, and
88 * outputs the re-decompressed result so it can be conveniently
89 * diffed against the original. Define -DTESTDBG as well for lots
90 * of diagnostics.
91 */
92
93 #if defined TESTDBG
94 /* gcc-specific diagnostic macro */
95 #define debug_int(x...) ( fprintf(stderr, x) )
96 #define debug(x) ( debug_int x )
97 #else
98 #define debug(x)
99 #endif
100
101 #ifdef STANDALONE
102 #define ANALYSIS
103 #endif
104
105 #ifdef ANALYSIS
106 int analyse_level = 0;
107 #endif
108
109 /* ----------------------------------------------------------------------
110 * Basic LZ77 code. This bit is designed modularly, so it could be
111 * ripped out and used in a different LZ77 compressor. Go to it,
112 * and good luck :-)
113 */
114
115 struct LZ77InternalContext;
116 struct LZ77Context {
117 struct LZ77InternalContext *ictx;
118 void *userdata;
119 void (*literal) (struct LZ77Context * ctx, unsigned char c);
120 void (*match) (struct LZ77Context * ctx, int distance, int len);
121 };
122
123 /*
124 * Initialise the private fields of an LZ77Context. It's up to the
125 * user to initialise the public fields.
126 */
127 static int lz77_init(struct LZ77Context *ctx);
128
129 /*
130 * Supply data to be compressed. Will update the private fields of
131 * the LZ77Context, and will call literal() and match() to output.
132 * If `compress' is FALSE, it will never emit a match, but will
133 * instead call literal() for everything.
134 */
135 static void lz77_compress(struct LZ77Context *ctx,
136 const unsigned char *data, int len, int compress);
137
138 /*
139 * Modifiable parameters.
140 */
141 #define WINSIZE 32768 /* window size. Must be power of 2! */
142 #define HASHMAX 2039 /* one more than max hash value */
143 #define MAXMATCH 32 /* how many matches we track */
144 #define HASHCHARS 3 /* how many chars make a hash */
145
146 /*
147 * This compressor takes a less slapdash approach than the
148 * gzip/zlib one. Rather than allowing our hash chains to fall into
149 * disuse near the far end, we keep them doubly linked so we can
150 * _find_ the far end, and then every time we add a new byte to the
151 * window (thus rolling round by one and removing the previous
152 * byte), we can carefully remove the hash chain entry.
153 */
154
155 #define INVALID -1 /* invalid hash _and_ invalid offset */
156 struct WindowEntry {
157 short next, prev; /* array indices within the window */
158 short hashval;
159 };
160
161 struct HashEntry {
162 short first; /* window index of first in chain */
163 };
164
165 struct Match {
166 int distance, len;
167 };
168
169 struct LZ77InternalContext {
170 struct WindowEntry win[WINSIZE];
171 unsigned char data[WINSIZE];
172 int winpos;
173 struct HashEntry hashtab[HASHMAX];
174 unsigned char pending[HASHCHARS];
175 int npending;
176 };
177
178 static int lz77_hash(const unsigned char *data)
179 {
180 return (257 * data[0] + 263 * data[1] + 269 * data[2]) % HASHMAX;
181 }
182
183 static int lz77_init(struct LZ77Context *ctx)
184 {
185 struct LZ77InternalContext *st;
186 int i;
187
188 st = snew(struct LZ77InternalContext);
189 if (!st)
190 return 0;
191
192 ctx->ictx = st;
193
194 for (i = 0; i < WINSIZE; i++)
195 st->win[i].next = st->win[i].prev = st->win[i].hashval = INVALID;
196 for (i = 0; i < HASHMAX; i++)
197 st->hashtab[i].first = INVALID;
198 st->winpos = 0;
199
200 st->npending = 0;
201
202 return 1;
203 }
204
205 static void lz77_advance(struct LZ77InternalContext *st,
206 unsigned char c, int hash)
207 {
208 int off;
209
210 /*
211 * Remove the hash entry at winpos from the tail of its chain,
212 * or empty the chain if it's the only thing on the chain.
213 */
214 if (st->win[st->winpos].prev != INVALID) {
215 st->win[st->win[st->winpos].prev].next = INVALID;
216 } else if (st->win[st->winpos].hashval != INVALID) {
217 st->hashtab[st->win[st->winpos].hashval].first = INVALID;
218 }
219
220 /*
221 * Create a new entry at winpos and add it to the head of its
222 * hash chain.
223 */
224 st->win[st->winpos].hashval = hash;
225 st->win[st->winpos].prev = INVALID;
226 off = st->win[st->winpos].next = st->hashtab[hash].first;
227 st->hashtab[hash].first = st->winpos;
228 if (off != INVALID)
229 st->win[off].prev = st->winpos;
230 st->data[st->winpos] = c;
231
232 /*
233 * Advance the window pointer.
234 */
235 st->winpos = (st->winpos + 1) & (WINSIZE - 1);
236 }
237
238 #define CHARAT(k) ( (k)<0 ? st->data[(st->winpos+k)&(WINSIZE-1)] : data[k] )
239
240 static void lz77_compress(struct LZ77Context *ctx,
241 const unsigned char *data, int len, int compress)
242 {
243 struct LZ77InternalContext *st = ctx->ictx;
244 int i, hash, distance, off, nmatch, matchlen, advance;
245 struct Match defermatch, matches[MAXMATCH];
246 int deferchr;
247
248 /*
249 * Add any pending characters from last time to the window. (We
250 * might not be able to.)
251 */
252 for (i = 0; i < st->npending; i++) {
253 unsigned char foo[HASHCHARS];
254 int j;
255 if (len + st->npending - i < HASHCHARS) {
256 /* Update the pending array. */
257 for (j = i; j < st->npending; j++)
258 st->pending[j - i] = st->pending[j];
259 break;
260 }
261 for (j = 0; j < HASHCHARS; j++)
262 foo[j] = (i + j < st->npending ? st->pending[i + j] :
263 data[i + j - st->npending]);
264 lz77_advance(st, foo[0], lz77_hash(foo));
265 }
266 st->npending -= i;
267
268 defermatch.len = 0;
269 deferchr = '\0';
270 while (len > 0) {
271
272 /* Don't even look for a match, if we're not compressing. */
273 if (compress && len >= HASHCHARS) {
274 /*
275 * Hash the next few characters.
276 */
277 hash = lz77_hash(data);
278
279 /*
280 * Look the hash up in the corresponding hash chain and see
281 * what we can find.
282 */
283 nmatch = 0;
284 for (off = st->hashtab[hash].first;
285 off != INVALID; off = st->win[off].next) {
286 /* distance = 1 if off == st->winpos-1 */
287 /* distance = WINSIZE if off == st->winpos */
288 distance =
289 WINSIZE - (off + WINSIZE - st->winpos) % WINSIZE;
290 for (i = 0; i < HASHCHARS; i++)
291 if (CHARAT(i) != CHARAT(i - distance))
292 break;
293 if (i == HASHCHARS) {
294 matches[nmatch].distance = distance;
295 matches[nmatch].len = 3;
296 if (++nmatch >= MAXMATCH)
297 break;
298 }
299 }
300 } else {
301 nmatch = 0;
302 hash = INVALID;
303 }
304
305 if (nmatch > 0) {
306 /*
307 * We've now filled up matches[] with nmatch potential
308 * matches. Follow them down to find the longest. (We
309 * assume here that it's always worth favouring a
310 * longer match over a shorter one.)
311 */
312 matchlen = HASHCHARS;
313 while (matchlen < len) {
314 int j;
315 for (i = j = 0; i < nmatch; i++) {
316 if (CHARAT(matchlen) ==
317 CHARAT(matchlen - matches[i].distance)) {
318 matches[j++] = matches[i];
319 }
320 }
321 if (j == 0)
322 break;
323 matchlen++;
324 nmatch = j;
325 }
326
327 /*
328 * We've now got all the longest matches. We favour the
329 * shorter distances, which means we go with matches[0].
330 * So see if we want to defer it or throw it away.
331 */
332 matches[0].len = matchlen;
333 if (defermatch.len > 0) {
334 if (matches[0].len > defermatch.len + 1) {
335 /* We have a better match. Emit the deferred char,
336 * and defer this match. */
337 ctx->literal(ctx, (unsigned char) deferchr);
338 defermatch = matches[0];
339 deferchr = data[0];
340 advance = 1;
341 } else {
342 /* We don't have a better match. Do the deferred one. */
343 ctx->match(ctx, defermatch.distance, defermatch.len);
344 advance = defermatch.len - 1;
345 defermatch.len = 0;
346 }
347 } else {
348 /* There was no deferred match. Defer this one. */
349 defermatch = matches[0];
350 deferchr = data[0];
351 advance = 1;
352 }
353 } else {
354 /*
355 * We found no matches. Emit the deferred match, if
356 * any; otherwise emit a literal.
357 */
358 if (defermatch.len > 0) {
359 ctx->match(ctx, defermatch.distance, defermatch.len);
360 advance = defermatch.len - 1;
361 defermatch.len = 0;
362 } else {
363 ctx->literal(ctx, data[0]);
364 advance = 1;
365 }
366 }
367
368 /*
369 * Now advance the position by `advance' characters,
370 * keeping the window and hash chains consistent.
371 */
372 while (advance > 0) {
373 if (len >= HASHCHARS) {
374 lz77_advance(st, *data, lz77_hash(data));
375 } else {
376 st->pending[st->npending++] = *data;
377 }
378 data++;
379 len--;
380 advance--;
381 }
382 }
383 }
384
385 /* ----------------------------------------------------------------------
386 * Deflate functionality common to both compression and decompression.
387 */
388
389 static const unsigned char lenlenmap[] = {
390 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
391 };
392
393 #define MAXCODELEN 16
394
395 /*
396 * Given a sequence of Huffman code lengths, compute the actual
397 * codes, in the final form suitable for feeding to outbits (i.e.
398 * already bit-mirrored).
399 *
400 * Returns the maximum code length found. Can also return -1 to
401 * indicate the table was overcommitted (too many or too short
402 * codes to exactly cover the possible space), or -2 to indicate it
403 * was undercommitted (too few or too long codes).
404 */
405 static int hufcodes(const unsigned char *lengths, int *codes, int nsyms)
406 {
407 int count[MAXCODELEN], startcode[MAXCODELEN];
408 int code, maxlen;
409 int i, j;
410
411 /* Count the codes of each length. */
412 maxlen = 0;
413 for (i = 1; i < MAXCODELEN; i++)
414 count[i] = 0;
415 for (i = 0; i < nsyms; i++) {
416 count[lengths[i]]++;
417 if (maxlen < lengths[i])
418 maxlen = lengths[i];
419 }
420 /* Determine the starting code for each length block. */
421 code = 0;
422 for (i = 1; i < MAXCODELEN; i++) {
423 startcode[i] = code;
424 code += count[i];
425 if (code > (1 << i))
426 maxlen = -1; /* overcommitted */
427 code <<= 1;
428 }
429 if (code < (1 << MAXCODELEN))
430 maxlen = -2; /* undercommitted */
431 /* Determine the code for each symbol. Mirrored, of course. */
432 for (i = 0; i < nsyms; i++) {
433 code = startcode[lengths[i]]++;
434 codes[i] = 0;
435 for (j = 0; j < lengths[i]; j++) {
436 codes[i] = (codes[i] << 1) | (code & 1);
437 code >>= 1;
438 }
439 }
440
441 return maxlen;
442 }
443
444 /*
445 * Adler32 checksum function.
446 */
447 static unsigned long adler32_update(unsigned long s,
448 const unsigned char *data, int len)
449 {
450 unsigned s1 = s & 0xFFFF, s2 = (s >> 16) & 0xFFFF;
451 int i;
452
453 for (i = 0; i < len; i++) {
454 s1 += data[i];
455 s2 += s1;
456 if (!(i & 0xFFF)) {
457 s1 %= 65521;
458 s2 %= 65521;
459 }
460 }
461
462 return ((s2 % 65521) << 16) | (s1 % 65521);
463 }
464
465 /*
466 * CRC32 checksum function.
467 */
468
469 static unsigned long crc32_update(unsigned long crcword,
470 const unsigned char *data, int len)
471 {
472 static const unsigned long crc32_table[256] = {
473 0x00000000L, 0x77073096L, 0xEE0E612CL, 0x990951BAL,
474 0x076DC419L, 0x706AF48FL, 0xE963A535L, 0x9E6495A3L,
475 0x0EDB8832L, 0x79DCB8A4L, 0xE0D5E91EL, 0x97D2D988L,
476 0x09B64C2BL, 0x7EB17CBDL, 0xE7B82D07L, 0x90BF1D91L,
477 0x1DB71064L, 0x6AB020F2L, 0xF3B97148L, 0x84BE41DEL,
478 0x1ADAD47DL, 0x6DDDE4EBL, 0xF4D4B551L, 0x83D385C7L,
479 0x136C9856L, 0x646BA8C0L, 0xFD62F97AL, 0x8A65C9ECL,
480 0x14015C4FL, 0x63066CD9L, 0xFA0F3D63L, 0x8D080DF5L,
481 0x3B6E20C8L, 0x4C69105EL, 0xD56041E4L, 0xA2677172L,
482 0x3C03E4D1L, 0x4B04D447L, 0xD20D85FDL, 0xA50AB56BL,
483 0x35B5A8FAL, 0x42B2986CL, 0xDBBBC9D6L, 0xACBCF940L,
484 0x32D86CE3L, 0x45DF5C75L, 0xDCD60DCFL, 0xABD13D59L,
485 0x26D930ACL, 0x51DE003AL, 0xC8D75180L, 0xBFD06116L,
486 0x21B4F4B5L, 0x56B3C423L, 0xCFBA9599L, 0xB8BDA50FL,
487 0x2802B89EL, 0x5F058808L, 0xC60CD9B2L, 0xB10BE924L,
488 0x2F6F7C87L, 0x58684C11L, 0xC1611DABL, 0xB6662D3DL,
489 0x76DC4190L, 0x01DB7106L, 0x98D220BCL, 0xEFD5102AL,
490 0x71B18589L, 0x06B6B51FL, 0x9FBFE4A5L, 0xE8B8D433L,
491 0x7807C9A2L, 0x0F00F934L, 0x9609A88EL, 0xE10E9818L,
492 0x7F6A0DBBL, 0x086D3D2DL, 0x91646C97L, 0xE6635C01L,
493 0x6B6B51F4L, 0x1C6C6162L, 0x856530D8L, 0xF262004EL,
494 0x6C0695EDL, 0x1B01A57BL, 0x8208F4C1L, 0xF50FC457L,
495 0x65B0D9C6L, 0x12B7E950L, 0x8BBEB8EAL, 0xFCB9887CL,
496 0x62DD1DDFL, 0x15DA2D49L, 0x8CD37CF3L, 0xFBD44C65L,
497 0x4DB26158L, 0x3AB551CEL, 0xA3BC0074L, 0xD4BB30E2L,
498 0x4ADFA541L, 0x3DD895D7L, 0xA4D1C46DL, 0xD3D6F4FBL,
499 0x4369E96AL, 0x346ED9FCL, 0xAD678846L, 0xDA60B8D0L,
500 0x44042D73L, 0x33031DE5L, 0xAA0A4C5FL, 0xDD0D7CC9L,
501 0x5005713CL, 0x270241AAL, 0xBE0B1010L, 0xC90C2086L,
502 0x5768B525L, 0x206F85B3L, 0xB966D409L, 0xCE61E49FL,
503 0x5EDEF90EL, 0x29D9C998L, 0xB0D09822L, 0xC7D7A8B4L,
504 0x59B33D17L, 0x2EB40D81L, 0xB7BD5C3BL, 0xC0BA6CADL,
505 0xEDB88320L, 0x9ABFB3B6L, 0x03B6E20CL, 0x74B1D29AL,
506 0xEAD54739L, 0x9DD277AFL, 0x04DB2615L, 0x73DC1683L,
507 0xE3630B12L, 0x94643B84L, 0x0D6D6A3EL, 0x7A6A5AA8L,
508 0xE40ECF0BL, 0x9309FF9DL, 0x0A00AE27L, 0x7D079EB1L,
509 0xF00F9344L, 0x8708A3D2L, 0x1E01F268L, 0x6906C2FEL,
510 0xF762575DL, 0x806567CBL, 0x196C3671L, 0x6E6B06E7L,
511 0xFED41B76L, 0x89D32BE0L, 0x10DA7A5AL, 0x67DD4ACCL,
512 0xF9B9DF6FL, 0x8EBEEFF9L, 0x17B7BE43L, 0x60B08ED5L,
513 0xD6D6A3E8L, 0xA1D1937EL, 0x38D8C2C4L, 0x4FDFF252L,
514 0xD1BB67F1L, 0xA6BC5767L, 0x3FB506DDL, 0x48B2364BL,
515 0xD80D2BDAL, 0xAF0A1B4CL, 0x36034AF6L, 0x41047A60L,
516 0xDF60EFC3L, 0xA867DF55L, 0x316E8EEFL, 0x4669BE79L,
517 0xCB61B38CL, 0xBC66831AL, 0x256FD2A0L, 0x5268E236L,
518 0xCC0C7795L, 0xBB0B4703L, 0x220216B9L, 0x5505262FL,
519 0xC5BA3BBEL, 0xB2BD0B28L, 0x2BB45A92L, 0x5CB36A04L,
520 0xC2D7FFA7L, 0xB5D0CF31L, 0x2CD99E8BL, 0x5BDEAE1DL,
521 0x9B64C2B0L, 0xEC63F226L, 0x756AA39CL, 0x026D930AL,
522 0x9C0906A9L, 0xEB0E363FL, 0x72076785L, 0x05005713L,
523 0x95BF4A82L, 0xE2B87A14L, 0x7BB12BAEL, 0x0CB61B38L,
524 0x92D28E9BL, 0xE5D5BE0DL, 0x7CDCEFB7L, 0x0BDBDF21L,
525 0x86D3D2D4L, 0xF1D4E242L, 0x68DDB3F8L, 0x1FDA836EL,
526 0x81BE16CDL, 0xF6B9265BL, 0x6FB077E1L, 0x18B74777L,
527 0x88085AE6L, 0xFF0F6A70L, 0x66063BCAL, 0x11010B5CL,
528 0x8F659EFFL, 0xF862AE69L, 0x616BFFD3L, 0x166CCF45L,
529 0xA00AE278L, 0xD70DD2EEL, 0x4E048354L, 0x3903B3C2L,
530 0xA7672661L, 0xD06016F7L, 0x4969474DL, 0x3E6E77DBL,
531 0xAED16A4AL, 0xD9D65ADCL, 0x40DF0B66L, 0x37D83BF0L,
532 0xA9BCAE53L, 0xDEBB9EC5L, 0x47B2CF7FL, 0x30B5FFE9L,
533 0xBDBDF21CL, 0xCABAC28AL, 0x53B39330L, 0x24B4A3A6L,
534 0xBAD03605L, 0xCDD70693L, 0x54DE5729L, 0x23D967BFL,
535 0xB3667A2EL, 0xC4614AB8L, 0x5D681B02L, 0x2A6F2B94L,
536 0xB40BBE37L, 0xC30C8EA1L, 0x5A05DF1BL, 0x2D02EF8DL
537 };
538 crcword ^= 0xFFFFFFFFL;
539 while (len--) {
540 unsigned long newbyte = *data++;
541 newbyte ^= crcword & 0xFFL;
542 crcword = (crcword >> 8) ^ crc32_table[newbyte];
543 }
544 return crcword ^ 0xFFFFFFFFL;
545 }
546
547 typedef struct {
548 short code, extrabits;
549 int min, max;
550 } coderecord;
551
552 static const coderecord lencodes[] = {
553 {257, 0, 3, 3},
554 {258, 0, 4, 4},
555 {259, 0, 5, 5},
556 {260, 0, 6, 6},
557 {261, 0, 7, 7},
558 {262, 0, 8, 8},
559 {263, 0, 9, 9},
560 {264, 0, 10, 10},
561 {265, 1, 11, 12},
562 {266, 1, 13, 14},
563 {267, 1, 15, 16},
564 {268, 1, 17, 18},
565 {269, 2, 19, 22},
566 {270, 2, 23, 26},
567 {271, 2, 27, 30},
568 {272, 2, 31, 34},
569 {273, 3, 35, 42},
570 {274, 3, 43, 50},
571 {275, 3, 51, 58},
572 {276, 3, 59, 66},
573 {277, 4, 67, 82},
574 {278, 4, 83, 98},
575 {279, 4, 99, 114},
576 {280, 4, 115, 130},
577 {281, 5, 131, 162},
578 {282, 5, 163, 194},
579 {283, 5, 195, 226},
580 {284, 5, 227, 257},
581 {285, 0, 258, 258},
582 };
583
584 static const coderecord distcodes[] = {
585 {0, 0, 1, 1},
586 {1, 0, 2, 2},
587 {2, 0, 3, 3},
588 {3, 0, 4, 4},
589 {4, 1, 5, 6},
590 {5, 1, 7, 8},
591 {6, 2, 9, 12},
592 {7, 2, 13, 16},
593 {8, 3, 17, 24},
594 {9, 3, 25, 32},
595 {10, 4, 33, 48},
596 {11, 4, 49, 64},
597 {12, 5, 65, 96},
598 {13, 5, 97, 128},
599 {14, 6, 129, 192},
600 {15, 6, 193, 256},
601 {16, 7, 257, 384},
602 {17, 7, 385, 512},
603 {18, 8, 513, 768},
604 {19, 8, 769, 1024},
605 {20, 9, 1025, 1536},
606 {21, 9, 1537, 2048},
607 {22, 10, 2049, 3072},
608 {23, 10, 3073, 4096},
609 {24, 11, 4097, 6144},
610 {25, 11, 6145, 8192},
611 {26, 12, 8193, 12288},
612 {27, 12, 12289, 16384},
613 {28, 13, 16385, 24576},
614 {29, 13, 24577, 32768},
615 };
616
617 /* ----------------------------------------------------------------------
618 * Deflate compression.
619 */
620
621 #define SYMLIMIT 65536
622 #define SYMPFX_LITLEN 0x00000000U
623 #define SYMPFX_DIST 0x40000000U
624 #define SYMPFX_EXTRABITS 0x80000000U
625 #define SYMPFX_CODELEN 0xC0000000U
626 #define SYMPFX_MASK 0xC0000000U
627
628 #define SYM_EXTRABITS_MASK 0x3C000000U
629 #define SYM_EXTRABITS_SHIFT 26
630
631 struct huftrees {
632 unsigned char *len_litlen;
633 int *code_litlen;
634 unsigned char *len_dist;
635 int *code_dist;
636 unsigned char *len_codelen;
637 int *code_codelen;
638 };
639
640 struct deflate_compress_ctx {
641 struct LZ77Context *lzc;
642 unsigned char *outbuf;
643 int outlen, outsize;
644 unsigned long outbits;
645 int noutbits;
646 int firstblock;
647 unsigned long *syms;
648 int symstart, nsyms;
649 int type;
650 unsigned long checksum;
651 unsigned long datasize;
652 int lastblock;
653 int finished;
654 unsigned char static_len1[286], static_len2[30];
655 int static_code1[286], static_code2[30];
656 struct huftrees sht;
657 #ifdef STATISTICS
658 unsigned long bitcount;
659 #endif
660 };
661
662 static void outbits(deflate_compress_ctx *out,
663 unsigned long bits, int nbits)
664 {
665 assert(out->noutbits + nbits <= 32);
666 out->outbits |= bits << out->noutbits;
667 out->noutbits += nbits;
668 while (out->noutbits >= 8) {
669 if (out->outlen >= out->outsize) {
670 out->outsize = out->outlen + 64;
671 out->outbuf = sresize(out->outbuf, out->outsize, unsigned char);
672 }
673 out->outbuf[out->outlen++] = (unsigned char) (out->outbits & 0xFF);
674 out->outbits >>= 8;
675 out->noutbits -= 8;
676 }
677 #ifdef STATISTICS
678 out->bitcount += nbits;
679 #endif
680 }
681
682 /*
683 * Binary heap functions used by buildhuf(). Each one assumes the
684 * heap to be stored in an array of ints, with two ints per node
685 * (user data and key). They take in the old heap length, and
686 * return the new one.
687 */
688 #define HEAPPARENT(x) (((x)-2)/4*2)
689 #define HEAPLEFT(x) ((x)*2+2)
690 #define HEAPRIGHT(x) ((x)*2+4)
691 static int addheap(int *heap, int len, int userdata, int key)
692 {
693 int me, dad, tmp;
694
695 me = len;
696 heap[len++] = userdata;
697 heap[len++] = key;
698
699 while (me > 0) {
700 dad = HEAPPARENT(me);
701 if (heap[me+1] < heap[dad+1]) {
702 tmp = heap[me]; heap[me] = heap[dad]; heap[dad] = tmp;
703 tmp = heap[me+1]; heap[me+1] = heap[dad+1]; heap[dad+1] = tmp;
704 me = dad;
705 } else
706 break;
707 }
708
709 return len;
710 }
711 static int rmheap(int *heap, int len, int *userdata, int *key)
712 {
713 int me, lc, rc, c, tmp;
714
715 len -= 2;
716 *userdata = heap[0];
717 *key = heap[1];
718 heap[0] = heap[len];
719 heap[1] = heap[len+1];
720
721 me = 0;
722
723 while (1) {
724 lc = HEAPLEFT(me);
725 rc = HEAPRIGHT(me);
726 if (lc >= len)
727 break;
728 else if (rc >= len || heap[lc+1] < heap[rc+1])
729 c = lc;
730 else
731 c = rc;
732 if (heap[me+1] > heap[c+1]) {
733 tmp = heap[me]; heap[me] = heap[c]; heap[c] = tmp;
734 tmp = heap[me+1]; heap[me+1] = heap[c+1]; heap[c+1] = tmp;
735 } else
736 break;
737 me = c;
738 }
739
740 return len;
741 }
742
743 /*
744 * The core of the Huffman algorithm: takes an input array of
745 * symbol frequencies, and produces an output array of code
746 * lengths.
747 *
748 * This is basically a generic Huffman implementation, but it has
749 * one zlib-related quirk which is that it caps the output code
750 * lengths to fit in an unsigned char (which is safe since Deflate
751 * will reject anything longer than 15 anyway). Anyone wanting to
752 * rip it out and use it in another context should find that easy
753 * to remove.
754 */
755 #define HUFMAX 286
756 static void buildhuf(const int *freqs, unsigned char *lengths, int nsyms)
757 {
758 int parent[2*HUFMAX-1];
759 int length[2*HUFMAX-1];
760 int heap[2*HUFMAX];
761 int heapsize;
762 int i, j, n;
763 int si, sj;
764
765 assert(nsyms <= HUFMAX);
766
767 memset(parent, 0, sizeof(parent));
768
769 /*
770 * Begin by building the heap.
771 */
772 heapsize = 0;
773 for (i = 0; i < nsyms; i++)
774 if (freqs[i] > 0) /* leave unused symbols out totally */
775 heapsize = addheap(heap, heapsize, i, freqs[i]);
776
777 /*
778 * Now repeatedly take two elements off the heap and merge
779 * them.
780 */
781 n = HUFMAX;
782 while (heapsize > 2) {
783 heapsize = rmheap(heap, heapsize, &i, &si);
784 heapsize = rmheap(heap, heapsize, &j, &sj);
785 parent[i] = n;
786 parent[j] = n;
787 heapsize = addheap(heap, heapsize, n, si + sj);
788 n++;
789 }
790
791 /*
792 * Now we have our tree, in the form of a link from each node
793 * to the index of its parent. Count back down the tree to
794 * determine the code lengths.
795 */
796 memset(length, 0, sizeof(length));
797 /* The tree root has length 0 after that, which is correct. */
798 for (i = n-1; i-- ;)
799 if (parent[i] > 0)
800 length[i] = 1 + length[parent[i]];
801
802 /*
803 * And that's it. (Simple, wasn't it?) Copy the lengths into
804 * the output array and leave.
805 *
806 * Here we cap lengths to fit in unsigned char.
807 */
808 for (i = 0; i < nsyms; i++)
809 lengths[i] = (length[i] > 255 ? 255 : length[i]);
810 }
811
812 /*
813 * Wrapper around buildhuf() which enforces the Deflate restriction
814 * that no code length may exceed 15 bits, or 7 for the auxiliary
815 * code length alphabet. This function has the same calling
816 * semantics as buildhuf(), except that it might modify the freqs
817 * array.
818 */
819 static void deflate_buildhuf(int *freqs, unsigned char *lengths,
820 int nsyms, int limit)
821 {
822 int smallestfreq, totalfreq, nactivesyms;
823 int num, denom, adjust;
824 int i;
825 int maxprob;
826
827 /*
828 * Nasty special case: if the frequency table has fewer than
829 * two non-zero elements, we must invent some, because we can't
830 * have fewer than one bit encoding a symbol.
831 */
832 assert(nsyms >= 2);
833 {
834 int count = 0;
835 for (i = 0; i < nsyms; i++)
836 if (freqs[i] > 0)
837 count++;
838 if (count < 2) {
839 for (i = 0; i < nsyms && count > 0; i++)
840 if (freqs[i] == 0) {
841 freqs[i] = 1;
842 count--;
843 }
844 }
845 }
846
847 /*
848 * First, try building the Huffman table the normal way. If
849 * this works, it's optimal, so we don't want to mess with it.
850 */
851 buildhuf(freqs, lengths, nsyms);
852
853 for (i = 0; i < nsyms; i++)
854 if (lengths[i] > limit)
855 break;
856
857 if (i == nsyms)
858 return; /* OK */
859
860 /*
861 * The Huffman algorithm can only ever generate a code length
862 * of N bits or more if there is a symbol whose probability is
863 * less than the reciprocal of the (N+2)th Fibonacci number
864 * (counting from F_0=0 and F_1=1), i.e. 1/2584 for N=16, or
865 * 1/55 for N=8. (This is a necessary though not sufficient
866 * condition.)
867 *
868 * Why is this? Well, consider the input symbol with the
869 * smallest probability. Let that probability be x. In order
870 * for this symbol to have a code length of at least 1, the
871 * Huffman algorithm will have to merge it with some other
872 * node; and since x is the smallest probability, the node it
873 * gets merged with must be at least x. Thus, the probability
874 * of the resulting combined node will be at least 2x. Now in
875 * order for our node to reach depth 2, this 2x-node must be
876 * merged again. But what with? We can't assume the node it
877 * merges with is at least 2x, because this one might only be
878 * the _second_ smallest remaining node. But we do know the
879 * node it merges with must be at least x, so our order-2
880 * internal node is at least 3x.
881 *
882 * How small a node can merge with _that_ to get an order-3
883 * internal node? Well, it must be at least 2x, because if it
884 * was smaller than that then it would have been one of the two
885 * smallest nodes in the previous step and been merged at that
886 * point. So at least 3x, plus at least 2x, comes to at least
887 * 5x for an order-3 node.
888 *
889 * And so it goes on: at every stage we must merge our current
890 * node with a node at least as big as the bigger of this one's
891 * two parents, and from this starting point that gives rise to
892 * the Fibonacci sequence. So we find that in order to have a
893 * node n levels deep (i.e. a maximum code length of n), the
894 * overall probability of the root of the entire tree must be
895 * at least F_{n+2} times the probability of the rarest symbol.
896 * In other words, since the overall probability is 1, it is a
897 * necessary condition for a code length of 16 or more that
898 * there must be at least one symbol with probability <=
899 * 1/F_18.
900 *
901 * (To demonstrate that a probability this big really can give
902 * rise to a code length of 16, consider the set of input
903 * frequencies { 1-epsilon, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,
904 * 89, 144, 233, 377, 610, 987 }, for arbitrarily small
905 * epsilon.)
906 *
907 * So here buildhuf() has returned us an overlong code. So to
908 * ensure it doesn't do it again, we add a constant to all the
909 * (non-zero) symbol frequencies, causing them to become more
910 * balanced and removing the danger. We can then feed the
911 * results back to the standard buildhuf() and be
912 * assert()-level confident that the resulting code lengths
913 * contain nothing outside the permitted range.
914 */
915 maxprob = (limit == 16 ? 2584 : 55); /* no point in computing full F_n */
916 totalfreq = nactivesyms = 0;
917 smallestfreq = -1;
918 for (i = 0; i < nsyms; i++) {
919 if (freqs[i] == 0)
920 continue;
921 if (smallestfreq < 0 || smallestfreq > freqs[i])
922 smallestfreq = freqs[i];
923 totalfreq += freqs[i];
924 nactivesyms++;
925 }
926 assert(smallestfreq <= totalfreq / maxprob);
927
928 /*
929 * We want to find the smallest integer `adjust' such that
930 * (totalfreq + nactivesyms * adjust) / (smallestfreq +
931 * adjust) is less than maxprob. A bit of algebra tells us
932 * that the threshold value is equal to
933 *
934 * totalfreq - maxprob * smallestfreq
935 * ----------------------------------
936 * maxprob - nactivesyms
937 *
938 * rounded up, of course. And we'll only even be trying
939 * this if
940 */
941 num = totalfreq - smallestfreq * maxprob;
942 denom = maxprob - nactivesyms;
943 adjust = (num + denom - 1) / denom;
944
945 /*
946 * Now add `adjust' to all the input symbol frequencies.
947 */
948 for (i = 0; i < nsyms; i++)
949 if (freqs[i] != 0)
950 freqs[i] += adjust;
951
952 /*
953 * Rebuild the Huffman tree...
954 */
955 buildhuf(freqs, lengths, nsyms);
956
957 /*
958 * ... and this time it ought to be OK.
959 */
960 for (i = 0; i < nsyms; i++)
961 assert(lengths[i] <= limit);
962 }
963
964 /*
965 * Compute the bit length of a symbol, given the three Huffman
966 * trees.
967 */
968 static int symsize(unsigned sym, const struct huftrees *trees)
969 {
970 unsigned basesym = sym &~ SYMPFX_MASK;
971
972 switch (sym & SYMPFX_MASK) {
973 case SYMPFX_LITLEN:
974 return trees->len_litlen[basesym];
975 case SYMPFX_DIST:
976 return trees->len_dist[basesym];
977 case SYMPFX_CODELEN:
978 return trees->len_codelen[basesym];
979 default /*case SYMPFX_EXTRABITS*/:
980 return basesym >> SYM_EXTRABITS_SHIFT;
981 }
982 }
983
984 /*
985 * Write out a single symbol, given the three Huffman trees.
986 */
987 static void writesym(deflate_compress_ctx *out,
988 unsigned sym, const struct huftrees *trees)
989 {
990 unsigned basesym = sym &~ SYMPFX_MASK;
991 int i;
992
993 switch (sym & SYMPFX_MASK) {
994 case SYMPFX_LITLEN:
995 debug(("send: litlen %d\n", basesym));
996 outbits(out, trees->code_litlen[basesym], trees->len_litlen[basesym]);
997 break;
998 case SYMPFX_DIST:
999 debug(("send: dist %d\n", basesym));
1000 outbits(out, trees->code_dist[basesym], trees->len_dist[basesym]);
1001 break;
1002 case SYMPFX_CODELEN:
1003 debug(("send: codelen %d\n", basesym));
1004 outbits(out, trees->code_codelen[basesym],trees->len_codelen[basesym]);
1005 break;
1006 case SYMPFX_EXTRABITS:
1007 i = basesym >> SYM_EXTRABITS_SHIFT;
1008 basesym &= ~SYM_EXTRABITS_MASK;
1009 debug(("send: extrabits %d/%d\n", basesym, i));
1010 outbits(out, basesym, i);
1011 break;
1012 }
1013 }
1014
1015 /*
1016 * outblock() must output _either_ a dynamic block of length
1017 * `dynamic_len', _or_ a static block of length `static_len', but
1018 * it gets to choose which.
1019 */
1020 static void outblock(deflate_compress_ctx *out,
1021 int dynamic_len, int static_len)
1022 {
1023 int freqs1[286], freqs2[30], freqs3[19];
1024 unsigned char len1[286], len2[30], len3[19];
1025 int code1[286], code2[30], code3[19];
1026 int hlit, hdist, hclen, bfinal, btype;
1027 int treesrc[286 + 30];
1028 int treesyms[286 + 30];
1029 int codelen[19];
1030 int i, ntreesrc, ntreesyms;
1031 int dynamic, blklen;
1032 struct huftrees dht;
1033 const struct huftrees *ht;
1034 #ifdef STATISTICS
1035 unsigned long bitcount_before;
1036 #endif
1037
1038 dht.len_litlen = len1;
1039 dht.len_dist = len2;
1040 dht.len_codelen = len3;
1041 dht.code_litlen = code1;
1042 dht.code_dist = code2;
1043 dht.code_codelen = code3;
1044
1045 /*
1046 * We make our choice of block to output by doing all the
1047 * detailed work to determine the exact length of each possible
1048 * block. Then we choose the one which has fewest output bits
1049 * per symbol.
1050 */
1051
1052 /*
1053 * First build the two main Huffman trees for the dynamic
1054 * block.
1055 */
1056
1057 /*
1058 * Count up the frequency tables.
1059 */
1060 memset(freqs1, 0, sizeof(freqs1));
1061 memset(freqs2, 0, sizeof(freqs2));
1062 freqs1[256] = 1; /* we're bound to need one EOB */
1063 for (i = 0; i < dynamic_len; i++) {
1064 unsigned sym = out->syms[(out->symstart + i) % SYMLIMIT];
1065
1066 /*
1067 * Increment the occurrence counter for this symbol, if
1068 * it's in one of the Huffman alphabets and isn't extra
1069 * bits.
1070 */
1071 if ((sym & SYMPFX_MASK) == SYMPFX_LITLEN) {
1072 sym &= ~SYMPFX_MASK;
1073 assert(sym < lenof(freqs1));
1074 freqs1[sym]++;
1075 } else if ((sym & SYMPFX_MASK) == SYMPFX_DIST) {
1076 sym &= ~SYMPFX_MASK;
1077 assert(sym < lenof(freqs2));
1078 freqs2[sym]++;
1079 }
1080 }
1081 deflate_buildhuf(freqs1, len1, lenof(freqs1), 15);
1082 deflate_buildhuf(freqs2, len2, lenof(freqs2), 15);
1083 hufcodes(len1, code1, lenof(freqs1));
1084 hufcodes(len2, code2, lenof(freqs2));
1085
1086 /*
1087 * Determine HLIT and HDIST.
1088 */
1089 for (hlit = 286; hlit > 257 && len1[hlit-1] == 0; hlit--);
1090 for (hdist = 30; hdist > 1 && len2[hdist-1] == 0; hdist--);
1091
1092 /*
1093 * Write out the list of symbols used to transmit the
1094 * trees.
1095 */
1096 ntreesrc = 0;
1097 for (i = 0; i < hlit; i++)
1098 treesrc[ntreesrc++] = len1[i];
1099 for (i = 0; i < hdist; i++)
1100 treesrc[ntreesrc++] = len2[i];
1101 ntreesyms = 0;
1102 for (i = 0; i < ntreesrc ;) {
1103 int j = 1;
1104 int k;
1105
1106 /* Find length of run of the same length code. */
1107 while (i+j < ntreesrc && treesrc[i+j] == treesrc[i])
1108 j++;
1109
1110 /* Encode that run as economically as we can. */
1111 k = j;
1112 if (treesrc[i] == 0) {
1113 /*
1114 * Zero code length: we can output run codes for
1115 * 3-138 zeroes. So if we have fewer than 3 zeroes,
1116 * we just output literals. Otherwise, we output
1117 * nothing but run codes, and tweak their lengths
1118 * to make sure we aren't left with under 3 at the
1119 * end.
1120 */
1121 if (k < 3) {
1122 while (k--)
1123 treesyms[ntreesyms++] = 0 | SYMPFX_CODELEN;
1124 } else {
1125 while (k > 0) {
1126 int rpt = (k < 138 ? k : 138);
1127 if (rpt > k-3 && rpt < k)
1128 rpt = k-3;
1129 assert(rpt >= 3 && rpt <= 138);
1130 if (rpt < 11) {
1131 treesyms[ntreesyms++] = 17 | SYMPFX_CODELEN;
1132 treesyms[ntreesyms++] =
1133 (SYMPFX_EXTRABITS | (rpt - 3) |
1134 (3 << SYM_EXTRABITS_SHIFT));
1135 } else {
1136 treesyms[ntreesyms++] = 18 | SYMPFX_CODELEN;
1137 treesyms[ntreesyms++] =
1138 (SYMPFX_EXTRABITS | (rpt - 11) |
1139 (7 << SYM_EXTRABITS_SHIFT));
1140 }
1141 k -= rpt;
1142 }
1143 }
1144 } else {
1145 /*
1146 * Non-zero code length: we must output the first
1147 * one explicitly, then we can output a copy code
1148 * for 3-6 repeats. So if we have fewer than 4
1149 * repeats, we _just_ output literals. Otherwise,
1150 * we output one literal plus at least one copy
1151 * code, and tweak the copy codes to make sure we
1152 * aren't left with under 3 at the end.
1153 */
1154 assert(treesrc[i] < 16);
1155 treesyms[ntreesyms++] = treesrc[i] | SYMPFX_CODELEN;
1156 k--;
1157 if (k < 3) {
1158 while (k--)
1159 treesyms[ntreesyms++] = treesrc[i] | SYMPFX_CODELEN;
1160 } else {
1161 while (k > 0) {
1162 int rpt = (k < 6 ? k : 6);
1163 if (rpt > k-3 && rpt < k)
1164 rpt = k-3;
1165 assert(rpt >= 3 && rpt <= 6);
1166 treesyms[ntreesyms++] = 16 | SYMPFX_CODELEN;
1167 treesyms[ntreesyms++] = (SYMPFX_EXTRABITS | (rpt - 3) |
1168 (2 << SYM_EXTRABITS_SHIFT));
1169 k -= rpt;
1170 }
1171 }
1172 }
1173
1174 i += j;
1175 }
1176 assert((unsigned)ntreesyms < lenof(treesyms));
1177
1178 /*
1179 * Count up the frequency table for the tree-transmission
1180 * symbols, and build the auxiliary Huffman tree for that.
1181 */
1182 memset(freqs3, 0, sizeof(freqs3));
1183 for (i = 0; i < ntreesyms; i++) {
1184 unsigned sym = treesyms[i];
1185
1186 /*
1187 * Increment the occurrence counter for this symbol, if
1188 * it's the Huffman alphabet and isn't extra bits.
1189 */
1190 if ((sym & SYMPFX_MASK) == SYMPFX_CODELEN) {
1191 sym &= ~SYMPFX_MASK;
1192 assert(sym < lenof(freqs3));
1193 freqs3[sym]++;
1194 }
1195 }
1196 deflate_buildhuf(freqs3, len3, lenof(freqs3), 7);
1197 hufcodes(len3, code3, lenof(freqs3));
1198
1199 /*
1200 * Reorder the code length codes into transmission order, and
1201 * determine HCLEN.
1202 */
1203 for (i = 0; i < 19; i++)
1204 codelen[i] = len3[lenlenmap[i]];
1205 for (hclen = 19; hclen > 4 && codelen[hclen-1] == 0; hclen--);
1206
1207 /*
1208 * Now work out the exact size of both the dynamic and the
1209 * static block, in bits.
1210 */
1211 {
1212 int ssize, dsize;
1213
1214 /*
1215 * First the dynamic block.
1216 */
1217 dsize = 3 + 5 + 5 + 4; /* 3-bit header, HLIT, HDIST, HCLEN */
1218 dsize += 3 * hclen; /* code-length-alphabet code lengths */
1219 /* Code lengths */
1220 for (i = 0; i < ntreesyms; i++)
1221 dsize += symsize(treesyms[i], &dht);
1222 /* The actual block data */
1223 for (i = 0; i < dynamic_len; i++) {
1224 unsigned sym = out->syms[(out->symstart + i) % SYMLIMIT];
1225 dsize += symsize(sym, &dht);
1226 }
1227 /* And the end-of-data symbol. */
1228 dsize += symsize(SYMPFX_LITLEN | 256, &dht);
1229
1230 /*
1231 * Now the static block.
1232 */
1233 ssize = 3; /* 3-bit block header */
1234 /* The actual block data */
1235 for (i = 0; i < static_len; i++) {
1236 unsigned sym = out->syms[(out->symstart + i) % SYMLIMIT];
1237 ssize += symsize(sym, &out->sht);
1238 }
1239 /* And the end-of-data symbol. */
1240 ssize += symsize(SYMPFX_LITLEN | 256, &out->sht);
1241
1242 /*
1243 * Compare the two and decide which to output. We break
1244 * exact ties in favour of the static block, because of the
1245 * special case in which that block has zero length.
1246 */
1247 dynamic = ((double)ssize * dynamic_len > (double)dsize * static_len);
1248 ht = dynamic ? &dht : &out->sht;
1249 blklen = dynamic ? dynamic_len : static_len;
1250 }
1251
1252 /*
1253 * Actually transmit the block.
1254 */
1255
1256 /* 3-bit block header */
1257 bfinal = (out->lastblock ? 1 : 0);
1258 btype = dynamic ? 2 : 1;
1259 debug(("send: bfinal=%d btype=%d\n", bfinal, btype));
1260 outbits(out, bfinal, 1);
1261 outbits(out, btype, 2);
1262
1263 #ifdef STATISTICS
1264 bitcount_before = out->bitcount;
1265 #endif
1266
1267 if (dynamic) {
1268 /* HLIT, HDIST and HCLEN */
1269 debug(("send: hlit=%d hdist=%d hclen=%d\n", hlit, hdist, hclen));
1270 outbits(out, hlit - 257, 5);
1271 outbits(out, hdist - 1, 5);
1272 outbits(out, hclen - 4, 4);
1273
1274 /* Code lengths for the auxiliary tree */
1275 for (i = 0; i < hclen; i++) {
1276 debug(("send: lenlen %d\n", codelen[i]));
1277 outbits(out, codelen[i], 3);
1278 }
1279
1280 /* Code lengths for the literal/length and distance trees */
1281 for (i = 0; i < ntreesyms; i++)
1282 writesym(out, treesyms[i], ht);
1283 #ifdef STATISTICS
1284 fprintf(stderr, "total tree size %lu bits\n",
1285 out->bitcount - bitcount_before);
1286 #endif
1287 }
1288
1289 /* Output the actual symbols from the buffer */
1290 for (i = 0; i < blklen; i++) {
1291 unsigned sym = out->syms[(out->symstart + i) % SYMLIMIT];
1292 writesym(out, sym, ht);
1293 }
1294
1295 /* Output the end-of-data symbol */
1296 writesym(out, SYMPFX_LITLEN | 256, ht);
1297
1298 /*
1299 * Remove all the just-output symbols from the symbol buffer by
1300 * adjusting symstart and nsyms.
1301 */
1302 out->symstart = (out->symstart + blklen) % SYMLIMIT;
1303 out->nsyms -= blklen;
1304 }
1305
1306 /*
1307 * Give the approximate log-base-2 of an input integer, measured in
1308 * 8ths of a bit. (I.e. this computes an integer approximation to
1309 * 8*logbase2(x).)
1310 */
1311 static int approxlog2(unsigned x)
1312 {
1313 int ret = 31*8;
1314
1315 /*
1316 * Binary-search to get the top bit of x up to bit 31.
1317 */
1318 if (x < 0x00010000U) x <<= 16, ret -= 16*8;
1319 if (x < 0x01000000U) x <<= 8, ret -= 8*8;
1320 if (x < 0x10000000U) x <<= 4, ret -= 4*8;
1321 if (x < 0x40000000U) x <<= 2, ret -= 2*8;
1322 if (x < 0x80000000U) x <<= 1, ret -= 1*8;
1323
1324 /*
1325 * Now we know the logarithm we want is in [ret,ret+1).
1326 * Determine the bottom three bits by checking against
1327 * threshold values.
1328 *
1329 * (Each of these threshold values is 0x80000000 times an odd
1330 * power of 2^(1/16). Therefore, this function rounds to
1331 * nearest.)
1332 */
1333 if (x <= 0xAD583EEAU) {
1334 if (x <= 0x91C3D373U)
1335 ret += (x <= 0x85AAC367U ? 0 : 1);
1336 else
1337 ret += (x <= 0x9EF53260U ? 2 : 3);
1338 } else {
1339 if (x <= 0xCE248C15U)
1340 ret += (x <= 0xBD08A39FU ? 4 : 5);
1341 else
1342 ret += (x <= 0xE0CCDEECU ? 6 : x <= 0xF5257D15L ? 7 : 8);
1343 }
1344
1345 return ret;
1346 }
1347
1348 static void chooseblock(deflate_compress_ctx *out)
1349 {
1350 int freqs1[286], freqs2[30];
1351 int i, len, bestlen, longestlen = 0;
1352 int total1, total2;
1353 int bestvfm;
1354
1355 memset(freqs1, 0, sizeof(freqs1));
1356 memset(freqs2, 0, sizeof(freqs2));
1357 freqs1[256] = 1; /* we're bound to need one EOB */
1358 total1 = 1;
1359 total2 = 0;
1360
1361 /*
1362 * Iterate over all possible block lengths, computing the
1363 * entropic coding approximation to the final length at every
1364 * stage. We divide the result by the number of symbols
1365 * encoded, to determine the `value for money' (overall
1366 * bits-per-symbol count) of a block of that length.
1367 */
1368 bestlen = -1;
1369 bestvfm = 0;
1370
1371 len = 300 * 8; /* very approximate size of the Huffman trees */
1372
1373 for (i = 0; i < out->nsyms; i++) {
1374 unsigned sym = out->syms[(out->symstart + i) % SYMLIMIT];
1375
1376 if (i > 0 && (sym & SYMPFX_MASK) == SYMPFX_LITLEN) {
1377 /*
1378 * This is a viable point at which to end the block.
1379 * Compute the value for money.
1380 */
1381 int vfm = i * 32768 / len; /* symbols encoded per bit */
1382
1383 if (bestlen < 0 || vfm > bestvfm) {
1384 bestlen = i;
1385 bestvfm = vfm;
1386 }
1387
1388 longestlen = i;
1389 }
1390
1391 /*
1392 * Increment the occurrence counter for this symbol, if
1393 * it's in one of the Huffman alphabets and isn't extra
1394 * bits.
1395 */
1396 if ((sym & SYMPFX_MASK) == SYMPFX_LITLEN) {
1397 sym &= ~SYMPFX_MASK;
1398 assert(sym < lenof(freqs1));
1399 len += freqs1[sym] * approxlog2(freqs1[sym]);
1400 len -= total1 * approxlog2(total1);
1401 freqs1[sym]++;
1402 total1++;
1403 len -= freqs1[sym] * approxlog2(freqs1[sym]);
1404 len += total1 * approxlog2(total1);
1405 } else if ((sym & SYMPFX_MASK) == SYMPFX_DIST) {
1406 sym &= ~SYMPFX_MASK;
1407 assert(sym < lenof(freqs2));
1408 len += freqs2[sym] * approxlog2(freqs2[sym]);
1409 len -= total2 * approxlog2(total2);
1410 freqs2[sym]++;
1411 total2++;
1412 len -= freqs2[sym] * approxlog2(freqs2[sym]);
1413 len += total2 * approxlog2(total2);
1414 } else if ((sym & SYMPFX_MASK) == SYMPFX_EXTRABITS) {
1415 len += 8 * ((sym &~ SYMPFX_MASK) >> SYM_EXTRABITS_SHIFT);
1416 }
1417 }
1418
1419 assert(bestlen > 0);
1420
1421 outblock(out, bestlen, longestlen);
1422 }
1423
1424 /*
1425 * Force the current symbol buffer to be flushed out as a single
1426 * block.
1427 */
1428 static void flushblock(deflate_compress_ctx *out)
1429 {
1430 /*
1431 * No need to check that out->nsyms is a valid block length: we
1432 * know it has to be, because flushblock() is called in between
1433 * two matches/literals.
1434 */
1435 outblock(out, out->nsyms, out->nsyms);
1436 assert(out->nsyms == 0);
1437 }
1438
1439 /*
1440 * Place a symbol into the symbols buffer.
1441 */
1442 static void outsym(deflate_compress_ctx *out, unsigned long sym)
1443 {
1444 assert(out->nsyms < SYMLIMIT);
1445 out->syms[(out->symstart + out->nsyms++) % SYMLIMIT] = sym;
1446
1447 if (out->nsyms == SYMLIMIT)
1448 chooseblock(out);
1449 }
1450
1451 static void literal(struct LZ77Context *ectx, unsigned char c)
1452 {
1453 deflate_compress_ctx *out = (deflate_compress_ctx *) ectx->userdata;
1454
1455 outsym(out, SYMPFX_LITLEN | c);
1456 }
1457
1458 static void match(struct LZ77Context *ectx, int distance, int len)
1459 {
1460 const coderecord *d, *l;
1461 int i, j, k;
1462 deflate_compress_ctx *out = (deflate_compress_ctx *) ectx->userdata;
1463
1464 while (len > 0) {
1465 int thislen;
1466
1467 /*
1468 * We can transmit matches of lengths 3 through 258
1469 * inclusive. So if len exceeds 258, we must transmit in
1470 * several steps, with 258 or less in each step.
1471 *
1472 * Specifically: if len >= 261, we can transmit 258 and be
1473 * sure of having at least 3 left for the next step. And if
1474 * len <= 258, we can just transmit len. But if len == 259
1475 * or 260, we must transmit len-3.
1476 */
1477 thislen = (len > 260 ? 258 : len <= 258 ? len : len - 3);
1478 len -= thislen;
1479
1480 /*
1481 * Binary-search to find which length code we're
1482 * transmitting.
1483 */
1484 i = -1;
1485 j = sizeof(lencodes) / sizeof(*lencodes);
1486 while (1) {
1487 assert(j - i >= 2);
1488 k = (j + i) / 2;
1489 if (thislen < lencodes[k].min)
1490 j = k;
1491 else if (thislen > lencodes[k].max)
1492 i = k;
1493 else {
1494 l = &lencodes[k];
1495 break; /* found it! */
1496 }
1497 }
1498
1499 /*
1500 * Transmit the length code.
1501 */
1502 outsym(out, SYMPFX_LITLEN | l->code);
1503
1504 /*
1505 * Transmit the extra bits.
1506 */
1507 if (l->extrabits) {
1508 outsym(out, (SYMPFX_EXTRABITS | (thislen - l->min) |
1509 (l->extrabits << SYM_EXTRABITS_SHIFT)));
1510 }
1511
1512 /*
1513 * Binary-search to find which distance code we're
1514 * transmitting.
1515 */
1516 i = -1;
1517 j = sizeof(distcodes) / sizeof(*distcodes);
1518 while (1) {
1519 assert(j - i >= 2);
1520 k = (j + i) / 2;
1521 if (distance < distcodes[k].min)
1522 j = k;
1523 else if (distance > distcodes[k].max)
1524 i = k;
1525 else {
1526 d = &distcodes[k];
1527 break; /* found it! */
1528 }
1529 }
1530
1531 /*
1532 * Write the distance code.
1533 */
1534 outsym(out, SYMPFX_DIST | d->code);
1535
1536 /*
1537 * Transmit the extra bits.
1538 */
1539 if (d->extrabits) {
1540 outsym(out, (SYMPFX_EXTRABITS | (distance - d->min) |
1541 (d->extrabits << SYM_EXTRABITS_SHIFT)));
1542 }
1543 }
1544 }
1545
1546 deflate_compress_ctx *deflate_compress_new(int type)
1547 {
1548 deflate_compress_ctx *out;
1549 struct LZ77Context *ectx = snew(struct LZ77Context);
1550
1551 lz77_init(ectx);
1552 ectx->literal = literal;
1553 ectx->match = match;
1554
1555 out = snew(deflate_compress_ctx);
1556 out->type = type;
1557 out->outbits = out->noutbits = 0;
1558 out->firstblock = TRUE;
1559 #ifdef STATISTICS
1560 out->bitcount = 0;
1561 #endif
1562
1563 out->syms = snewn(SYMLIMIT, unsigned long);
1564 out->symstart = out->nsyms = 0;
1565
1566 out->checksum = (type == DEFLATE_TYPE_ZLIB ? 1 : 0);
1567 out->datasize = 0;
1568 out->lastblock = FALSE;
1569 out->finished = FALSE;
1570
1571 /*
1572 * Build the static Huffman tables now, so we'll have them
1573 * available every time outblock() is called.
1574 */
1575 {
1576 int i;
1577
1578 for (i = 0; i < lenof(out->static_len1); i++)
1579 out->static_len1[i] = (i < 144 ? 8 :
1580 i < 256 ? 9 :
1581 i < 280 ? 7 : 8);
1582 for (i = 0; i < lenof(out->static_len2); i++)
1583 out->static_len2[i] = 5;
1584 }
1585 hufcodes(out->static_len1, out->static_code1, lenof(out->static_code1));
1586 hufcodes(out->static_len2, out->static_code2, lenof(out->static_code2));
1587 out->sht.len_litlen = out->static_len1;
1588 out->sht.len_dist = out->static_len2;
1589 out->sht.len_codelen = NULL;
1590 out->sht.code_litlen = out->static_code1;
1591 out->sht.code_dist = out->static_code2;
1592 out->sht.code_codelen = NULL;
1593
1594 ectx->userdata = out;
1595 out->lzc = ectx;
1596
1597 return out;
1598 }
1599
1600 void deflate_compress_free(deflate_compress_ctx *out)
1601 {
1602 struct LZ77Context *ectx = out->lzc;
1603
1604 sfree(out->syms);
1605 sfree(ectx->ictx);
1606 sfree(ectx);
1607 sfree(out);
1608 }
1609
1610 void deflate_compress_data(deflate_compress_ctx *out,
1611 const void *vblock, int len, int flushtype,
1612 void **outblock, int *outlen)
1613 {
1614 struct LZ77Context *ectx = out->lzc;
1615 const unsigned char *block = (const unsigned char *)vblock;
1616
1617 assert(!out->finished);
1618
1619 out->outbuf = NULL;
1620 out->outlen = out->outsize = 0;
1621
1622 /*
1623 * If this is the first block, output the header.
1624 */
1625 if (out->firstblock) {
1626 switch (out->type) {
1627 case DEFLATE_TYPE_BARE:
1628 break; /* no header */
1629 case DEFLATE_TYPE_ZLIB:
1630 /*
1631 * zlib (RFC1950) header bytes: 78 9C. (Deflate
1632 * compression, 32K window size, default algorithm.)
1633 */
1634 outbits(out, 0x9C78, 16);
1635 break;
1636 case DEFLATE_TYPE_GZIP:
1637 /*
1638 * Minimal gzip (RFC1952) header:
1639 *
1640 * - basic header of 1F 8B
1641 * - compression method byte (8 = deflate)
1642 * - flags byte (zero: we use no optional features)
1643 * - modification time (zero: no time stamp available)
1644 * - extra flags byte (2: we use maximum compression
1645 * always)
1646 * - operating system byte (255: we do not specify)
1647 */
1648 outbits(out, 0x00088B1F, 32); /* header, CM, flags */
1649 outbits(out, 0, 32); /* mtime */
1650 outbits(out, 0xFF02, 16); /* xflags, OS */
1651 break;
1652 }
1653 out->firstblock = FALSE;
1654 }
1655
1656 /*
1657 * Feed our data to the LZ77 compression phase.
1658 */
1659 lz77_compress(ectx, block, len, TRUE);
1660
1661 /*
1662 * Update checksums and counters.
1663 */
1664 switch (out->type) {
1665 case DEFLATE_TYPE_ZLIB:
1666 out->checksum = adler32_update(out->checksum, block, len);
1667 break;
1668 case DEFLATE_TYPE_GZIP:
1669 out->checksum = crc32_update(out->checksum, block, len);
1670 break;
1671 }
1672 out->datasize += len;
1673
1674 switch (flushtype) {
1675 /*
1676 * FIXME: what other flush types are available and useful?
1677 * In PuTTY, it was clear that we generally wanted to be in
1678 * a static block so it was safe to open one. Here, we
1679 * probably prefer to be _outside_ a block if we can. Think
1680 * about this.
1681 */
1682 case DEFLATE_NO_FLUSH:
1683 break; /* don't flush any data at all (duh) */
1684 case DEFLATE_SYNC_FLUSH:
1685 /*
1686 * Close the current block.
1687 */
1688 flushblock(out);
1689
1690 /*
1691 * Then output an empty _uncompressed_ block: send 000,
1692 * then sync to byte boundary, then send bytes 00 00 FF
1693 * FF.
1694 */
1695 outbits(out, 0, 3);
1696 if (out->noutbits)
1697 outbits(out, 0, 8 - out->noutbits);
1698 outbits(out, 0, 16);
1699 outbits(out, 0xFFFF, 16);
1700 break;
1701 case DEFLATE_END_OF_DATA:
1702 /*
1703 * Output a block with BFINAL set.
1704 */
1705 out->lastblock = TRUE;
1706 flushblock(out);
1707
1708 /*
1709 * Sync to byte boundary, flushing out the final byte.
1710 */
1711 if (out->noutbits)
1712 outbits(out, 0, 8 - out->noutbits);
1713
1714 /*
1715 * Format-specific trailer data.
1716 */
1717 switch (out->type) {
1718 case DEFLATE_TYPE_ZLIB:
1719 /*
1720 * Just write out the Adler32 checksum.
1721 */
1722 outbits(out, (out->checksum >> 24) & 0xFF, 8);
1723 outbits(out, (out->checksum >> 16) & 0xFF, 8);
1724 outbits(out, (out->checksum >> 8) & 0xFF, 8);
1725 outbits(out, (out->checksum >> 0) & 0xFF, 8);
1726 break;
1727 case DEFLATE_TYPE_GZIP:
1728 /*
1729 * Write out the CRC32 checksum and the data length.
1730 */
1731 outbits(out, out->checksum, 32);
1732 outbits(out, out->datasize, 32);
1733 break;
1734 }
1735
1736 out->finished = TRUE;
1737 break;
1738 }
1739
1740 /*
1741 * Return any data that we've generated.
1742 */
1743 *outblock = (void *)out->outbuf;
1744 *outlen = out->outlen;
1745 }
1746
1747 /* ----------------------------------------------------------------------
1748 * Deflate decompression.
1749 */
1750
1751 /*
1752 * The way we work the Huffman decode is to have a table lookup on
1753 * the first N bits of the input stream (in the order they arrive,
1754 * of course, i.e. the first bit of the Huffman code is in bit 0).
1755 * Each table entry lists the number of bits to consume, plus
1756 * either an output code or a pointer to a secondary table.
1757 */
1758 struct table;
1759 struct tableentry;
1760
1761 struct tableentry {
1762 unsigned char nbits;
1763 short code;
1764 struct table *nexttable;
1765 };
1766
1767 struct table {
1768 int mask; /* mask applied to input bit stream */
1769 struct tableentry *table;
1770 };
1771
1772 #define MAXSYMS 288
1773
1774 #define DWINSIZE 32768
1775
1776 /*
1777 * Build a single-level decode table for elements
1778 * [minlength,maxlength) of the provided code/length tables, and
1779 * recurse to build subtables.
1780 */
1781 static struct table *mkonetab(int *codes, unsigned char *lengths, int nsyms,
1782 int pfx, int pfxbits, int bits)
1783 {
1784 struct table *tab = snew(struct table);
1785 int pfxmask = (1 << pfxbits) - 1;
1786 int nbits, i, j, code;
1787
1788 tab->table = snewn(1 << bits, struct tableentry);
1789 tab->mask = (1 << bits) - 1;
1790
1791 for (code = 0; code <= tab->mask; code++) {
1792 tab->table[code].code = -1;
1793 tab->table[code].nbits = 0;
1794 tab->table[code].nexttable = NULL;
1795 }
1796
1797 for (i = 0; i < nsyms; i++) {
1798 if (lengths[i] <= pfxbits || (codes[i] & pfxmask) != pfx)
1799 continue;
1800 code = (codes[i] >> pfxbits) & tab->mask;
1801 for (j = code; j <= tab->mask; j += 1 << (lengths[i] - pfxbits)) {
1802 tab->table[j].code = i;
1803 nbits = lengths[i] - pfxbits;
1804 if (tab->table[j].nbits < nbits)
1805 tab->table[j].nbits = nbits;
1806 }
1807 }
1808 for (code = 0; code <= tab->mask; code++) {
1809 if (tab->table[code].nbits <= bits)
1810 continue;
1811 /* Generate a subtable. */
1812 tab->table[code].code = -1;
1813 nbits = tab->table[code].nbits - bits;
1814 if (nbits > 7)
1815 nbits = 7;
1816 tab->table[code].nbits = bits;
1817 tab->table[code].nexttable = mkonetab(codes, lengths, nsyms,
1818 pfx | (code << pfxbits),
1819 pfxbits + bits, nbits);
1820 }
1821
1822 return tab;
1823 }
1824
1825 /*
1826 * Build a decode table, given a set of Huffman tree lengths.
1827 */
1828 static struct table *mktable(unsigned char *lengths, int nlengths,
1829 #ifdef ANALYSIS
1830 const char *alphabet,
1831 #endif
1832 int *error)
1833 {
1834 int codes[MAXSYMS];
1835 int maxlen;
1836
1837 #ifdef ANALYSIS
1838 if (alphabet && analyse_level > 1) {
1839 int i, col = 0;
1840 printf("code lengths for %s alphabet:\n", alphabet);
1841 for (i = 0; i < nlengths; i++) {
1842 col += printf("%3d", lengths[i]);
1843 if (col > 72) {
1844 putchar('\n');
1845 col = 0;
1846 }
1847 }
1848 if (col > 0)
1849 putchar('\n');
1850 }
1851 #endif
1852
1853 maxlen = hufcodes(lengths, codes, nlengths);
1854
1855 if (maxlen < 0) {
1856 *error = (maxlen == -1 ? DEFLATE_ERR_LARGE_HUFTABLE :
1857 DEFLATE_ERR_SMALL_HUFTABLE);
1858 return NULL;
1859 }
1860
1861 /*
1862 * Now we have the complete list of Huffman codes. Build a
1863 * table.
1864 */
1865 return mkonetab(codes, lengths, nlengths, 0, 0, maxlen < 9 ? maxlen : 9);
1866 }
1867
1868 static int freetable(struct table **ztab)
1869 {
1870 struct table *tab;
1871 int code;
1872
1873 if (ztab == NULL)
1874 return -1;
1875
1876 if (*ztab == NULL)
1877 return 0;
1878
1879 tab = *ztab;
1880
1881 for (code = 0; code <= tab->mask; code++)
1882 if (tab->table[code].nexttable != NULL)
1883 freetable(&tab->table[code].nexttable);
1884
1885 sfree(tab->table);
1886 tab->table = NULL;
1887
1888 sfree(tab);
1889 *ztab = NULL;
1890
1891 return (0);
1892 }
1893
1894 struct deflate_decompress_ctx {
1895 struct table *staticlentable, *staticdisttable;
1896 struct table *currlentable, *currdisttable, *lenlentable;
1897 enum {
1898 ZLIBSTART,
1899 GZIPSTART, GZIPMETHFLAGS, GZIPIGNORE1, GZIPIGNORE2, GZIPIGNORE3,
1900 GZIPEXTRA, GZIPFNAME, GZIPCOMMENT,
1901 OUTSIDEBLK, TREES_HDR, TREES_LENLEN, TREES_LEN, TREES_LENREP,
1902 INBLK, GOTLENSYM, GOTLEN, GOTDISTSYM,
1903 UNCOMP_LEN, UNCOMP_NLEN, UNCOMP_DATA,
1904 END,
1905 ADLER1, ADLER2,
1906 CRC1, CRC2, ILEN1, ILEN2,
1907 FINALSPIN
1908 } state;
1909 int sym, hlit, hdist, hclen, lenptr, lenextrabits, lenaddon, len,
1910 lenrep, lastblock;
1911 int uncomplen;
1912 unsigned char lenlen[19];
1913 unsigned char lengths[286 + 32];
1914 unsigned long bits;
1915 int nbits;
1916 unsigned char window[DWINSIZE];
1917 int winpos;
1918 unsigned char *outblk;
1919 int outlen, outsize;
1920 int type;
1921 unsigned long checksum;
1922 unsigned long bytesout;
1923 int gzflags, gzextralen;
1924 #ifdef ANALYSIS
1925 int bytesread;
1926 int bitcount_before;
1927 #define BITCOUNT(dctx) ( (dctx)->bytesread * 8 - (dctx)->nbits )
1928 #endif
1929 };
1930
1931 deflate_decompress_ctx *deflate_decompress_new(int type)
1932 {
1933 deflate_decompress_ctx *dctx = snew(deflate_decompress_ctx);
1934 unsigned char lengths[288];
1935
1936 memset(lengths, 8, 144);
1937 memset(lengths + 144, 9, 256 - 144);
1938 memset(lengths + 256, 7, 280 - 256);
1939 memset(lengths + 280, 8, 288 - 280);
1940 dctx->staticlentable = mktable(lengths, 288,
1941 #ifdef ANALYSIS
1942 NULL,
1943 #endif
1944 NULL);
1945 assert(dctx->staticlentable);
1946 memset(lengths, 5, 32);
1947 dctx->staticdisttable = mktable(lengths, 32,
1948 #ifdef ANALYSIS
1949 NULL,
1950 #endif
1951 NULL);
1952 assert(dctx->staticdisttable);
1953 dctx->state = (type == DEFLATE_TYPE_ZLIB ? ZLIBSTART :
1954 type == DEFLATE_TYPE_GZIP ? GZIPSTART :
1955 OUTSIDEBLK);
1956 dctx->currlentable = dctx->currdisttable = dctx->lenlentable = NULL;
1957 dctx->bits = 0;
1958 dctx->nbits = 0;
1959 dctx->winpos = 0;
1960 dctx->type = type;
1961 dctx->lastblock = FALSE;
1962 dctx->checksum = (type == DEFLATE_TYPE_ZLIB ? 1 : 0);
1963 dctx->bytesout = 0;
1964 dctx->gzflags = dctx->gzextralen = 0;
1965 #ifdef ANALYSIS
1966 dctx->bytesread = dctx->bitcount_before = 0;
1967 #endif
1968
1969 return dctx;
1970 }
1971
1972 void deflate_decompress_free(deflate_decompress_ctx *dctx)
1973 {
1974 if (dctx->currlentable && dctx->currlentable != dctx->staticlentable)
1975 freetable(&dctx->currlentable);
1976 if (dctx->currdisttable && dctx->currdisttable != dctx->staticdisttable)
1977 freetable(&dctx->currdisttable);
1978 if (dctx->lenlentable)
1979 freetable(&dctx->lenlentable);
1980 freetable(&dctx->staticlentable);
1981 freetable(&dctx->staticdisttable);
1982 sfree(dctx);
1983 }
1984
1985 static int huflookup(unsigned long *bitsp, int *nbitsp, struct table *tab)
1986 {
1987 unsigned long bits = *bitsp;
1988 int nbits = *nbitsp;
1989 while (1) {
1990 struct tableentry *ent;
1991 ent = &tab->table[bits & tab->mask];
1992 if (ent->nbits > nbits)
1993 return -1; /* not enough data */
1994 bits >>= ent->nbits;
1995 nbits -= ent->nbits;
1996 if (ent->code == -1)
1997 tab = ent->nexttable;
1998 else {
1999 *bitsp = bits;
2000 *nbitsp = nbits;
2001 return ent->code;
2002 }
2003
2004 /*
2005 * If we reach here with `tab' null, it can only be because
2006 * there was a missing entry in the Huffman table. This
2007 * should never occur even with invalid input data, because
2008 * we enforce at mktable time that the Huffman codes should
2009 * precisely cover the code space; so we can enforce this
2010 * by assertion.
2011 */
2012 assert(tab);
2013 }
2014 }
2015
2016 static void emit_char(deflate_decompress_ctx *dctx, int c)
2017 {
2018 dctx->window[dctx->winpos] = c;
2019 dctx->winpos = (dctx->winpos + 1) & (DWINSIZE - 1);
2020 if (dctx->outlen >= dctx->outsize) {
2021 dctx->outsize = dctx->outlen * 3 / 2 + 512;
2022 dctx->outblk = sresize(dctx->outblk, dctx->outsize, unsigned char);
2023 }
2024 if (dctx->type == DEFLATE_TYPE_ZLIB) {
2025 unsigned char uc = c;
2026 dctx->checksum = adler32_update(dctx->checksum, &uc, 1);
2027 } else if (dctx->type == DEFLATE_TYPE_GZIP) {
2028 unsigned char uc = c;
2029 dctx->checksum = crc32_update(dctx->checksum, &uc, 1);
2030 }
2031 dctx->outblk[dctx->outlen++] = c;
2032 dctx->bytesout++;
2033 }
2034
2035 #define EATBITS(n) ( dctx->nbits -= (n), dctx->bits >>= (n) )
2036
2037 int deflate_decompress_data(deflate_decompress_ctx *dctx,
2038 const void *vblock, int len,
2039 void **outblock, int *outlen)
2040 {
2041 const coderecord *rec;
2042 const unsigned char *block = (const unsigned char *)vblock;
2043 int code, bfinal, btype, rep, dist, nlen, header, cksum;
2044 int error = 0;
2045
2046 if (len == 0) {
2047 *outblock = NULL;
2048 *outlen = 0;
2049 if (dctx->state != FINALSPIN)
2050 return DEFLATE_ERR_UNEXPECTED_EOF;
2051 else
2052 return 0;
2053 }
2054
2055 dctx->outblk = NULL;
2056 dctx->outsize = 0;
2057 dctx->outlen = 0;
2058
2059 while (len > 0 || dctx->nbits > 0) {
2060 while (dctx->nbits < 24 && len > 0) {
2061 dctx->bits |= (*block++) << dctx->nbits;
2062 dctx->nbits += 8;
2063 len--;
2064 #ifdef ANALYSIS
2065 dctx->bytesread++;
2066 #endif
2067 }
2068 switch (dctx->state) {
2069 case ZLIBSTART:
2070 /* Expect 16-bit zlib header. */
2071 if (dctx->nbits < 16)
2072 goto finished; /* done all we can */
2073
2074 /*
2075 * The header is stored as a big-endian 16-bit integer,
2076 * in contrast to the general little-endian policy in
2077 * the rest of the format :-(
2078 */
2079 header = (((dctx->bits & 0xFF00) >> 8) |
2080 ((dctx->bits & 0x00FF) << 8));
2081 EATBITS(16);
2082
2083 /*
2084 * Check the header:
2085 *
2086 * - bits 8-11 should be 1000 (Deflate/RFC1951)
2087 * - bits 12-15 should be at most 0111 (window size)
2088 * - bit 5 should be zero (no dictionary present)
2089 * - we don't care about bits 6-7 (compression rate)
2090 * - bits 0-4 should be set up to make the whole thing
2091 * a multiple of 31 (checksum).
2092 */
2093 if ((header & 0xF000) > 0x7000 ||
2094 (header & 0x0020) != 0x0000 ||
2095 (header % 31) != 0) {
2096 error = DEFLATE_ERR_ZLIB_HEADER;
2097 goto finished;
2098 }
2099 if ((header & 0x0F00) != 0x0800) {
2100 error = DEFLATE_ERR_ZLIB_WRONGCOMP;
2101 goto finished;
2102 }
2103 dctx->state = OUTSIDEBLK;
2104 break;
2105 case GZIPSTART:
2106 /* Expect 16-bit gzip header. */
2107 if (dctx->nbits < 16)
2108 goto finished;
2109 header = dctx->bits & 0xFFFF;
2110 EATBITS(16);
2111 if (header != 0x8B1F) {
2112 error = DEFLATE_ERR_GZIP_HEADER;
2113 goto finished;
2114 }
2115 dctx->state = GZIPMETHFLAGS;
2116 break;
2117 case GZIPMETHFLAGS:
2118 /* Expect gzip compression method and flags bytes. */
2119 if (dctx->nbits < 16)
2120 goto finished;
2121 header = dctx->bits & 0xFF;
2122 EATBITS(8);
2123 if (header != 8) {
2124 error = DEFLATE_ERR_GZIP_WRONGCOMP;
2125 goto finished;
2126 }
2127 dctx->gzflags = dctx->bits & 0xFF;
2128 if (dctx->gzflags & 2) {
2129 /*
2130 * The FHCRC flag is slightly confusing. RFC1952
2131 * documents it as indicating the presence of a
2132 * two-byte CRC16 of the gzip header, occurring
2133 * just before the beginning of the Deflate stream.
2134 * However, gzip itself (as of 1.3.5) appears to
2135 * believe it indicates that the current gzip
2136 * `member' is not the final one, i.e. that the
2137 * stream is composed of multiple gzip members
2138 * concatenated together, and furthermore gzip will
2139 * refuse to decode any file that has it set.
2140 *
2141 * For this reason, I label it as `disputed' and
2142 * also refuse to decode anything that has it set.
2143 * I don't expect this to be a problem in practice.
2144 */
2145 error = DEFLATE_ERR_GZIP_FHCRC;
2146 goto finished;
2147 }
2148 EATBITS(8);
2149 dctx->state = GZIPIGNORE1;
2150 break;
2151 case GZIPIGNORE1:
2152 case GZIPIGNORE2:
2153 case GZIPIGNORE3:
2154 /* Expect two bytes of gzip timestamp/XFL/OS, which we ignore. */
2155 if (dctx->nbits < 16)
2156 goto finished;
2157 EATBITS(16);
2158 if (dctx->state == GZIPIGNORE3) {
2159 dctx->state = GZIPEXTRA;
2160 } else
2161 dctx->state++; /* maps IGNORE1 -> IGNORE2 -> IGNORE3 */
2162 break;
2163 case GZIPEXTRA:
2164 if (dctx->gzflags & 4) {
2165 /* Expect two bytes of extra-length count, then that many
2166 * extra bytes of header data, all of which we ignore. */
2167 if (!dctx->gzextralen) {
2168 if (dctx->nbits < 16)
2169 goto finished;
2170 dctx->gzextralen = dctx->bits & 0xFFFF;
2171 EATBITS(16);
2172 break;
2173 } else if (dctx->gzextralen > 0) {
2174 if (dctx->nbits < 8)
2175 goto finished;
2176 EATBITS(8);
2177 if (--dctx->gzextralen > 0)
2178 break;
2179 }
2180 }
2181 dctx->state = GZIPFNAME;
2182 break;
2183 case GZIPFNAME:
2184 if (dctx->gzflags & 8) {
2185 /*
2186 * Expect a NUL-terminated filename.
2187 */
2188 if (dctx->nbits < 8)
2189 goto finished;
2190 code = dctx->bits & 0xFF;
2191 EATBITS(8);
2192 } else
2193 code = 0;
2194 if (code == 0)
2195 dctx->state = GZIPCOMMENT;
2196 break;
2197 case GZIPCOMMENT:
2198 if (dctx->gzflags & 16) {
2199 /*
2200 * Expect a NUL-terminated filename.
2201 */
2202 if (dctx->nbits < 8)
2203 goto finished;
2204 code = dctx->bits & 0xFF;
2205 EATBITS(8);
2206 } else
2207 code = 0;
2208 if (code == 0)
2209 dctx->state = OUTSIDEBLK;
2210 break;
2211 case OUTSIDEBLK:
2212 /* Expect 3-bit block header. */
2213 if (dctx->nbits < 3)
2214 goto finished; /* done all we can */
2215 bfinal = dctx->bits & 1;
2216 if (bfinal)
2217 dctx->lastblock = TRUE;
2218 EATBITS(1);
2219 btype = dctx->bits & 3;
2220 EATBITS(2);
2221 if (btype == 0) {
2222 int to_eat = dctx->nbits & 7;
2223 dctx->state = UNCOMP_LEN;
2224 EATBITS(to_eat); /* align to byte boundary */
2225 } else if (btype == 1) {
2226 dctx->currlentable = dctx->staticlentable;
2227 dctx->currdisttable = dctx->staticdisttable;
2228 dctx->state = INBLK;
2229 } else if (btype == 2) {
2230 dctx->state = TREES_HDR;
2231 }
2232 debug(("recv: bfinal=%d btype=%d\n", bfinal, btype));
2233 #ifdef ANALYSIS
2234 if (analyse_level > 1) {
2235 static const char *const btypes[] = {
2236 "uncompressed", "static", "dynamic", "type 3 (unknown)"
2237 };
2238 printf("new block, %sfinal, %s\n",
2239 bfinal ? "" : "not ",
2240 btypes[btype]);
2241 }
2242 #endif
2243 break;
2244 case TREES_HDR:
2245 /*
2246 * Dynamic block header. Five bits of HLIT, five of
2247 * HDIST, four of HCLEN.
2248 */
2249 if (dctx->nbits < 5 + 5 + 4)
2250 goto finished; /* done all we can */
2251 dctx->hlit = 257 + (dctx->bits & 31);
2252 EATBITS(5);
2253 dctx->hdist = 1 + (dctx->bits & 31);
2254 EATBITS(5);
2255 dctx->hclen = 4 + (dctx->bits & 15);
2256 EATBITS(4);
2257 debug(("recv: hlit=%d hdist=%d hclen=%d\n", dctx->hlit,
2258 dctx->hdist, dctx->hclen));
2259 #ifdef ANALYSIS
2260 if (analyse_level > 1)
2261 printf("hlit=%d, hdist=%d, hclen=%d\n",
2262 dctx->hlit, dctx->hdist, dctx->hclen);
2263 #endif
2264 dctx->lenptr = 0;
2265 dctx->state = TREES_LENLEN;
2266 memset(dctx->lenlen, 0, sizeof(dctx->lenlen));
2267 break;
2268 case TREES_LENLEN:
2269 if (dctx->nbits < 3)
2270 goto finished;
2271 while (dctx->lenptr < dctx->hclen && dctx->nbits >= 3) {
2272 dctx->lenlen[lenlenmap[dctx->lenptr++]] =
2273 (unsigned char) (dctx->bits & 7);
2274 debug(("recv: lenlen %d\n", (unsigned char) (dctx->bits & 7)));
2275 EATBITS(3);
2276 }
2277 if (dctx->lenptr == dctx->hclen) {
2278 dctx->lenlentable = mktable(dctx->lenlen, 19,
2279 #ifdef ANALYSIS
2280 "code length",
2281 #endif
2282 &error);
2283 if (!dctx->lenlentable)
2284 goto finished; /* error code set up by mktable */
2285 dctx->state = TREES_LEN;
2286 dctx->lenptr = 0;
2287 }
2288 break;
2289 case TREES_LEN:
2290 if (dctx->lenptr >= dctx->hlit + dctx->hdist) {
2291 dctx->currlentable = mktable(dctx->lengths, dctx->hlit,
2292 #ifdef ANALYSIS
2293 "literal/length",
2294 #endif
2295 &error);
2296 if (!dctx->currlentable)
2297 goto finished; /* error code set up by mktable */
2298 dctx->currdisttable = mktable(dctx->lengths + dctx->hlit,
2299 dctx->hdist,
2300 #ifdef ANALYSIS
2301 "distance",
2302 #endif
2303 &error);
2304 if (!dctx->currdisttable)
2305 goto finished; /* error code set up by mktable */
2306 freetable(&dctx->lenlentable);
2307 dctx->lenlentable = NULL;
2308 dctx->state = INBLK;
2309 break;
2310 }
2311 code = huflookup(&dctx->bits, &dctx->nbits, dctx->lenlentable);
2312 debug(("recv: codelen %d\n", code));
2313 if (code == -1)
2314 goto finished;
2315 if (code < 16) {
2316 #ifdef ANALYSIS
2317 if (analyse_level > 1)
2318 printf("code-length %d\n", code);
2319 #endif
2320 dctx->lengths[dctx->lenptr++] = code;
2321 } else {
2322 dctx->lenextrabits = (code == 16 ? 2 : code == 17 ? 3 : 7);
2323 dctx->lenaddon = (code == 18 ? 11 : 3);
2324 dctx->lenrep = (code == 16 && dctx->lenptr > 0 ?
2325 dctx->lengths[dctx->lenptr - 1] : 0);
2326 dctx->state = TREES_LENREP;
2327 }
2328 break;
2329 case TREES_LENREP:
2330 if (dctx->nbits < dctx->lenextrabits)
2331 goto finished;
2332 rep =
2333 dctx->lenaddon +
2334 (dctx->bits & ((1 << dctx->lenextrabits) - 1));
2335 EATBITS(dctx->lenextrabits);
2336 if (dctx->lenextrabits)
2337 debug(("recv: codelen-extrabits %d/%d\n", rep - dctx->lenaddon,
2338 dctx->lenextrabits));
2339 #ifdef ANALYSIS
2340 if (analyse_level > 1)
2341 printf("code-length-repeat: %d copies of %d\n", rep,
2342 dctx->lenrep);
2343 #endif
2344 while (rep > 0 && dctx->lenptr < dctx->hlit + dctx->hdist) {
2345 dctx->lengths[dctx->lenptr] = dctx->lenrep;
2346 dctx->lenptr++;
2347 rep--;
2348 }
2349 dctx->state = TREES_LEN;
2350 break;
2351 case INBLK:
2352 #ifdef ANALYSIS
2353 dctx->bitcount_before = BITCOUNT(dctx);
2354 #endif
2355 code = huflookup(&dctx->bits, &dctx->nbits, dctx->currlentable);
2356 debug(("recv: litlen %d\n", code));
2357 if (code == -1)
2358 goto finished;
2359 if (code < 256) {
2360 #ifdef ANALYSIS
2361 if (analyse_level > 0)
2362 printf("%lu: literal %d [%d]\n", dctx->bytesout, code,
2363 BITCOUNT(dctx) - dctx->bitcount_before);
2364 #endif
2365 emit_char(dctx, code);
2366 } else if (code == 256) {
2367 if (dctx->lastblock)
2368 dctx->state = END;
2369 else
2370 dctx->state = OUTSIDEBLK;
2371 if (dctx->currlentable != dctx->staticlentable) {
2372 freetable(&dctx->currlentable);
2373 dctx->currlentable = NULL;
2374 }
2375 if (dctx->currdisttable != dctx->staticdisttable) {
2376 freetable(&dctx->currdisttable);
2377 dctx->currdisttable = NULL;
2378 }
2379 } else if (code < 286) { /* static tree can give >285; ignore */
2380 dctx->state = GOTLENSYM;
2381 dctx->sym = code;
2382 }
2383 break;
2384 case GOTLENSYM:
2385 rec = &lencodes[dctx->sym - 257];
2386 if (dctx->nbits < rec->extrabits)
2387 goto finished;
2388 dctx->len =
2389 rec->min + (dctx->bits & ((1 << rec->extrabits) - 1));
2390 if (rec->extrabits)
2391 debug(("recv: litlen-extrabits %d/%d\n",
2392 dctx->len - rec->min, rec->extrabits));
2393 EATBITS(rec->extrabits);
2394 dctx->state = GOTLEN;
2395 break;
2396 case GOTLEN:
2397 code = huflookup(&dctx->bits, &dctx->nbits, dctx->currdisttable);
2398 debug(("recv: dist %d\n", code));
2399 if (code == -1)
2400 goto finished;
2401 dctx->state = GOTDISTSYM;
2402 dctx->sym = code;
2403 break;
2404 case GOTDISTSYM:
2405 rec = &distcodes[dctx->sym];
2406 if (dctx->nbits < rec->extrabits)
2407 goto finished;
2408 dist = rec->min + (dctx->bits & ((1 << rec->extrabits) - 1));
2409 if (rec->extrabits)
2410 debug(("recv: dist-extrabits %d/%d\n",
2411 dist - rec->min, rec->extrabits));
2412 EATBITS(rec->extrabits);
2413 dctx->state = INBLK;
2414 #ifdef ANALYSIS
2415 if (analyse_level > 0)
2416 printf("%lu: copy len=%d dist=%d [%d]\n", dctx->bytesout,
2417 dctx->len, dist,
2418 BITCOUNT(dctx) - dctx->bitcount_before);
2419 #endif
2420 while (dctx->len--)
2421 emit_char(dctx, dctx->window[(dctx->winpos - dist) &
2422 (DWINSIZE - 1)]);
2423 break;
2424 case UNCOMP_LEN:
2425 /*
2426 * Uncompressed block. We expect to see a 16-bit LEN.
2427 */
2428 if (dctx->nbits < 16)
2429 goto finished;
2430 dctx->uncomplen = dctx->bits & 0xFFFF;
2431 EATBITS(16);
2432 dctx->state = UNCOMP_NLEN;
2433 break;
2434 case UNCOMP_NLEN:
2435 /*
2436 * Uncompressed block. We expect to see a 16-bit NLEN,
2437 * which should be the one's complement of the previous
2438 * LEN.
2439 */
2440 if (dctx->nbits < 16)
2441 goto finished;
2442 nlen = dctx->bits & 0xFFFF;
2443 EATBITS(16);
2444 if (dctx->uncomplen == 0)
2445 dctx->state = OUTSIDEBLK; /* block is empty */
2446 else
2447 dctx->state = UNCOMP_DATA;
2448 break;
2449 case UNCOMP_DATA:
2450 if (dctx->nbits < 8)
2451 goto finished;
2452 #ifdef ANALYSIS
2453 if (analyse_level > 0)
2454 printf("%lu: uncompressed %d [8]\n", dctx->bytesout,
2455 (int)(dctx->bits & 0xFF));
2456 #endif
2457 emit_char(dctx, dctx->bits & 0xFF);
2458 EATBITS(8);
2459 if (--dctx->uncomplen == 0)
2460 dctx->state = OUTSIDEBLK; /* end of uncompressed block */
2461 break;
2462 case END:
2463 /*
2464 * End of compressed data. We align to a byte boundary,
2465 * and then look for format-specific trailer data.
2466 */
2467 {
2468 int to_eat = dctx->nbits & 7;
2469 EATBITS(to_eat);
2470 }
2471 if (dctx->type == DEFLATE_TYPE_ZLIB)
2472 dctx->state = ADLER1;
2473 else if (dctx->type == DEFLATE_TYPE_GZIP)
2474 dctx->state = CRC1;
2475 else
2476 dctx->state = FINALSPIN;
2477 break;
2478 case ADLER1:
2479 if (dctx->nbits < 16)
2480 goto finished;
2481 cksum = (dctx->bits & 0xFF) << 8;
2482 EATBITS(8);
2483 cksum |= (dctx->bits & 0xFF);
2484 EATBITS(8);
2485 if (cksum != ((dctx->checksum >> 16) & 0xFFFF)) {
2486 error = DEFLATE_ERR_CHECKSUM;
2487 goto finished;
2488 }
2489 dctx->state = ADLER2;
2490 break;
2491 case ADLER2:
2492 if (dctx->nbits < 16)
2493 goto finished;
2494 cksum = (dctx->bits & 0xFF) << 8;
2495 EATBITS(8);
2496 cksum |= (dctx->bits & 0xFF);
2497 EATBITS(8);
2498 if (cksum != (dctx->checksum & 0xFFFF)) {
2499 error = DEFLATE_ERR_CHECKSUM;
2500 goto finished;
2501 }
2502 dctx->state = FINALSPIN;
2503 break;
2504 case CRC1:
2505 if (dctx->nbits < 16)
2506 goto finished;
2507 cksum = dctx->bits & 0xFFFF;
2508 EATBITS(16);
2509 if (cksum != (dctx->checksum & 0xFFFF)) {
2510 error = DEFLATE_ERR_CHECKSUM;
2511 goto finished;
2512 }
2513 dctx->state = CRC2;
2514 break;
2515 case CRC2:
2516 if (dctx->nbits < 16)
2517 goto finished;
2518 cksum = dctx->bits & 0xFFFF;
2519 EATBITS(16);
2520 if (cksum != ((dctx->checksum >> 16) & 0xFFFF)) {
2521 error = DEFLATE_ERR_CHECKSUM;
2522 goto finished;
2523 }
2524 dctx->state = ILEN1;
2525 break;
2526 case ILEN1:
2527 if (dctx->nbits < 16)
2528 goto finished;
2529 cksum = dctx->bits & 0xFFFF;
2530 EATBITS(16);
2531 if (cksum != (dctx->bytesout & 0xFFFF)) {
2532 error = DEFLATE_ERR_INLEN;
2533 goto finished;
2534 }
2535 dctx->state = ILEN2;
2536 break;
2537 case ILEN2:
2538 if (dctx->nbits < 16)
2539 goto finished;
2540 cksum = dctx->bits & 0xFFFF;
2541 EATBITS(16);
2542 if (cksum != ((dctx->bytesout >> 16) & 0xFFFF)) {
2543 error = DEFLATE_ERR_INLEN;
2544 goto finished;
2545 }
2546 dctx->state = FINALSPIN;
2547 break;
2548 case FINALSPIN:
2549 /* Just ignore any trailing garbage on the data stream. */
2550 /* (We could alternatively throw an error here, if we wanted
2551 * to detect and complain about trailing garbage.) */
2552 EATBITS(dctx->nbits);
2553 break;
2554 }
2555 }
2556
2557 finished:
2558 *outblock = dctx->outblk;
2559 *outlen = dctx->outlen;
2560 return error;
2561 }
2562
2563 #define A(code,str) str
2564 const char *const deflate_error_msg[DEFLATE_NUM_ERRORS] = {
2565 DEFLATE_ERRORLIST(A)
2566 };
2567 #undef A
2568
2569 #define A(code,str) #code
2570 const char *const deflate_error_sym[DEFLATE_NUM_ERRORS] = {
2571 DEFLATE_ERRORLIST(A)
2572 };
2573 #undef A
2574
2575 #ifdef STANDALONE
2576
2577 int main(int argc, char **argv)
2578 {
2579 unsigned char buf[65536];
2580 void *outbuf;
2581 int ret, err, outlen;
2582 deflate_decompress_ctx *dhandle;
2583 deflate_compress_ctx *chandle;
2584 int type = DEFLATE_TYPE_ZLIB, opts = TRUE;
2585 int compress = FALSE, decompress = FALSE;
2586 int got_arg = FALSE;
2587 char *filename = NULL;
2588 FILE *fp;
2589
2590 while (--argc) {
2591 char *p = *++argv;
2592
2593 got_arg = TRUE;
2594
2595 if (p[0] == '-' && opts) {
2596 if (!strcmp(p, "-b"))
2597 type = DEFLATE_TYPE_BARE;
2598 else if (!strcmp(p, "-g"))
2599 type = DEFLATE_TYPE_GZIP;
2600 else if (!strcmp(p, "-c"))
2601 compress = TRUE;
2602 else if (!strcmp(p, "-d"))
2603 decompress = TRUE;
2604 else if (!strcmp(p, "-a"))
2605 analyse_level++, decompress = TRUE;
2606 else if (!strcmp(p, "--"))
2607 opts = FALSE; /* next thing is filename */
2608 else {
2609 fprintf(stderr, "unknown command line option '%s'\n", p);
2610 return 1;
2611 }
2612 } else if (!filename) {
2613 filename = p;
2614 } else {
2615 fprintf(stderr, "can only handle one filename\n");
2616 return 1;
2617 }
2618 }
2619
2620 if (!compress && !decompress) {
2621 fprintf(stderr, "usage: deflate [ -c | -d | -a ] [ -b | -g ]"
2622 " [filename]\n");
2623 return (got_arg ? 1 : 0);
2624 }
2625
2626 if (compress && decompress) {
2627 fprintf(stderr, "please do not specify both compression and"
2628 " decompression\n");
2629 return (got_arg ? 1 : 0);
2630 }
2631
2632 if (compress) {
2633 chandle = deflate_compress_new(type);
2634 dhandle = NULL;
2635 } else {
2636 dhandle = deflate_decompress_new(type);
2637 chandle = NULL;
2638 }
2639
2640 if (filename)
2641 fp = fopen(filename, "rb");
2642 else
2643 fp = stdin;
2644
2645 if (!fp) {
2646 assert(filename);
2647 fprintf(stderr, "unable to open '%s'\n", filename);
2648 return 1;
2649 }
2650
2651 do {
2652 ret = fread(buf, 1, sizeof(buf), fp);
2653 outbuf = NULL;
2654 if (dhandle) {
2655 if (ret > 0)
2656 err = deflate_decompress_data(dhandle, buf, ret,
2657 (void **)&outbuf, &outlen);
2658 else
2659 err = deflate_decompress_data(dhandle, NULL, 0,
2660 (void **)&outbuf, &outlen);
2661 } else {
2662 if (ret > 0)
2663 deflate_compress_data(chandle, buf, ret, DEFLATE_NO_FLUSH,
2664 (void **)&outbuf, &outlen);
2665 else
2666 deflate_compress_data(chandle, buf, ret, DEFLATE_END_OF_DATA,
2667 (void **)&outbuf, &outlen);
2668 err = 0;
2669 }
2670 if (outbuf) {
2671 if (!analyse_level && outlen)
2672 fwrite(outbuf, 1, outlen, stdout);
2673 sfree(outbuf);
2674 }
2675 if (err > 0) {
2676 fprintf(stderr, "decoding error: %s\n", deflate_error_msg[err]);
2677 return 1;
2678 }
2679 } while (ret > 0);
2680
2681 if (dhandle)
2682 deflate_decompress_free(dhandle);
2683 if (chandle)
2684 deflate_compress_free(chandle);
2685
2686 if (filename)
2687 fclose(fp);
2688
2689 return 0;
2690 }
2691
2692 #endif
2693
2694 #ifdef TESTMODE
2695
2696 int main(int argc, char **argv)
2697 {
2698 char *filename = NULL;
2699 FILE *fp;
2700 deflate_compress_ctx *chandle;
2701 deflate_decompress_ctx *dhandle;
2702 unsigned char buf[65536], *outbuf, *outbuf2;
2703 int ret, err, outlen, outlen2;
2704 int dlen = 0, clen = 0;
2705 int opts = TRUE;
2706
2707 while (--argc) {
2708 char *p = *++argv;
2709
2710 if (p[0] == '-' && opts) {
2711 if (!strcmp(p, "--"))
2712 opts = FALSE; /* next thing is filename */
2713 else {
2714 fprintf(stderr, "unknown command line option '%s'\n", p);
2715 return 1;
2716 }
2717 } else if (!filename) {
2718 filename = p;
2719 } else {
2720 fprintf(stderr, "can only handle one filename\n");
2721 return 1;
2722 }
2723 }
2724
2725 if (filename)
2726 fp = fopen(filename, "rb");
2727 else
2728 fp = stdin;
2729
2730 if (!fp) {
2731 assert(filename);
2732 fprintf(stderr, "unable to open '%s'\n", filename);
2733 return 1;
2734 }
2735
2736 chandle = deflate_compress_new(DEFLATE_TYPE_ZLIB);
2737 dhandle = deflate_decompress_new(DEFLATE_TYPE_ZLIB);
2738
2739 do {
2740 ret = fread(buf, 1, sizeof(buf), fp);
2741 if (ret <= 0) {
2742 deflate_compress_data(chandle, NULL, 0, DEFLATE_END_OF_DATA,
2743 (void **)&outbuf, &outlen);
2744 } else {
2745 dlen += ret;
2746 deflate_compress_data(chandle, buf, ret, DEFLATE_NO_FLUSH,
2747 (void **)&outbuf, &outlen);
2748 }
2749 if (outbuf) {
2750 clen += outlen;
2751 err = deflate_decompress_data(dhandle, outbuf, outlen,
2752 (void **)&outbuf2, &outlen2);
2753 sfree(outbuf);
2754 if (outbuf2) {
2755 if (outlen2)
2756 fwrite(outbuf2, 1, outlen2, stdout);
2757 sfree(outbuf2);
2758 }
2759 if (!err && ret <= 0) {
2760 /*
2761 * signal EOF
2762 */
2763 err = deflate_decompress_data(dhandle, NULL, 0,
2764 (void **)&outbuf2, &outlen2);
2765 assert(outbuf2 == NULL);
2766 }
2767 if (err) {
2768 fprintf(stderr, "decoding error: %s\n",
2769 deflate_error_msg[err]);
2770 return 1;
2771 }
2772 }
2773 } while (ret > 0);
2774
2775 fprintf(stderr, "%d plaintext -> %d compressed\n", dlen, clen);
2776
2777 return 0;
2778 }
2779
2780 #endif