I've just discovered that using the saved sessions menu from Unix
[u/mdw/putty] / sshzlib.c
CommitLineData
4ba9b64b 1/*
2 * Zlib (RFC1950 / RFC1951) compression for PuTTY.
3 *
4 * There will no doubt be criticism of my decision to reimplement
5 * Zlib compression from scratch instead of using the existing zlib
6 * code. People will cry `reinventing the wheel'; they'll claim
7 * that the `fundamental basis of OSS' is code reuse; they'll want
8 * to see a really good reason for me having chosen not to use the
9 * existing code.
10 *
11 * Well, here are my reasons. Firstly, I don't want to link the
12 * whole of zlib into the PuTTY binary; PuTTY is justifiably proud
13 * of its small size and I think zlib contains a lot of unnecessary
14 * baggage for the kind of compression that SSH requires.
15 *
16 * Secondly, I also don't like the alternative of using zlib.dll.
17 * Another thing PuTTY is justifiably proud of is its ease of
18 * installation, and the last thing I want to do is to start
19 * mandating DLLs. Not only that, but there are two _kinds_ of
20 * zlib.dll kicking around, one with C calling conventions on the
21 * exported functions and another with WINAPI conventions, and
22 * there would be a significant danger of getting the wrong one.
23 *
24 * Thirdly, there seems to be a difference of opinion on the IETF
25 * secsh mailing list about the correct way to round off a
26 * compressed packet and start the next. In particular, there's
27 * some talk of switching to a mechanism zlib isn't currently
28 * capable of supporting (see below for an explanation). Given that
29 * sort of uncertainty, I thought it might be better to have code
30 * that will support even the zlib-incompatible worst case.
31 *
32 * Fourthly, it's a _second implementation_. Second implementations
33 * are fundamentally a Good Thing in standardisation efforts. The
34 * difference of opinion mentioned above has arisen _precisely_
35 * because there has been only one zlib implementation and
36 * everybody has used it. I don't intend that this should happen
37 * again.
38 */
39
40#include <stdlib.h>
41#include <assert.h>
42
52912ff3 43#ifdef ZLIB_STANDALONE
44
45/*
46 * This module also makes a handy zlib decoding tool for when
47 * you're picking apart Zip files or PDFs or PNGs. If you compile
48 * it with ZLIB_STANDALONE defined, it builds on its own and
49 * becomes a command-line utility.
50 *
51 * Therefore, here I provide a self-contained implementation of the
52 * macros required from the rest of the PuTTY sources.
53 */
54#define snew(type) ( (type *) malloc(sizeof(type)) )
55#define snewn(n, type) ( (type *) malloc((n) * sizeof(type)) )
56#define sresize(x, n, type) ( (type *) realloc((x), (n) * sizeof(type)) )
57#define sfree(x) ( free((x)) )
58
59#else
4ba9b64b 60#include "ssh.h"
52912ff3 61#endif
4ba9b64b 62
dc3c8261 63#ifndef FALSE
64#define FALSE 0
65#define TRUE (!FALSE)
66#endif
67
4ba9b64b 68/* ----------------------------------------------------------------------
69 * Basic LZ77 code. This bit is designed modularly, so it could be
70 * ripped out and used in a different LZ77 compressor. Go to it,
71 * and good luck :-)
72 */
73
74struct LZ77InternalContext;
75struct LZ77Context {
76 struct LZ77InternalContext *ictx;
77 void *userdata;
32874aea 78 void (*literal) (struct LZ77Context * ctx, unsigned char c);
79 void (*match) (struct LZ77Context * ctx, int distance, int len);
4ba9b64b 80};
81
82/*
83 * Initialise the private fields of an LZ77Context. It's up to the
84 * user to initialise the public fields.
85 */
86static int lz77_init(struct LZ77Context *ctx);
87
88/*
89 * Supply data to be compressed. Will update the private fields of
90 * the LZ77Context, and will call literal() and match() to output.
6e9e9520 91 * If `compress' is FALSE, it will never emit a match, but will
92 * instead call literal() for everything.
4ba9b64b 93 */
94static void lz77_compress(struct LZ77Context *ctx,
32874aea 95 unsigned char *data, int len, int compress);
4ba9b64b 96
97/*
98 * Modifiable parameters.
99 */
100#define WINSIZE 32768 /* window size. Must be power of 2! */
101#define HASHMAX 2039 /* one more than max hash value */
102#define MAXMATCH 32 /* how many matches we track */
103#define HASHCHARS 3 /* how many chars make a hash */
104
105/*
106 * This compressor takes a less slapdash approach than the
107 * gzip/zlib one. Rather than allowing our hash chains to fall into
108 * disuse near the far end, we keep them doubly linked so we can
109 * _find_ the far end, and then every time we add a new byte to the
110 * window (thus rolling round by one and removing the previous
111 * byte), we can carefully remove the hash chain entry.
112 */
113
114#define INVALID -1 /* invalid hash _and_ invalid offset */
115struct WindowEntry {
d2371c81 116 short next, prev; /* array indices within the window */
117 short hashval;
4ba9b64b 118};
119
120struct HashEntry {
d2371c81 121 short first; /* window index of first in chain */
4ba9b64b 122};
123
124struct Match {
125 int distance, len;
126};
127
128struct LZ77InternalContext {
129 struct WindowEntry win[WINSIZE];
130 unsigned char data[WINSIZE];
131 int winpos;
132 struct HashEntry hashtab[HASHMAX];
133 unsigned char pending[HASHCHARS];
134 int npending;
135};
136
32874aea 137static int lz77_hash(unsigned char *data)
138{
139 return (257 * data[0] + 263 * data[1] + 269 * data[2]) % HASHMAX;
4ba9b64b 140}
141
32874aea 142static int lz77_init(struct LZ77Context *ctx)
143{
4ba9b64b 144 struct LZ77InternalContext *st;
145 int i;
146
3d88e64d 147 st = snew(struct LZ77InternalContext);
4ba9b64b 148 if (!st)
149 return 0;
150
151 ctx->ictx = st;
152
153 for (i = 0; i < WINSIZE; i++)
154 st->win[i].next = st->win[i].prev = st->win[i].hashval = INVALID;
155 for (i = 0; i < HASHMAX; i++)
156 st->hashtab[i].first = INVALID;
157 st->winpos = 0;
158
159 st->npending = 0;
160
161 return 1;
162}
163
164static void lz77_advance(struct LZ77InternalContext *st,
32874aea 165 unsigned char c, int hash)
166{
4ba9b64b 167 int off;
168
169 /*
170 * Remove the hash entry at winpos from the tail of its chain,
171 * or empty the chain if it's the only thing on the chain.
172 */
173 if (st->win[st->winpos].prev != INVALID) {
174 st->win[st->win[st->winpos].prev].next = INVALID;
175 } else if (st->win[st->winpos].hashval != INVALID) {
176 st->hashtab[st->win[st->winpos].hashval].first = INVALID;
177 }
178
179 /*
180 * Create a new entry at winpos and add it to the head of its
181 * hash chain.
182 */
183 st->win[st->winpos].hashval = hash;
184 st->win[st->winpos].prev = INVALID;
185 off = st->win[st->winpos].next = st->hashtab[hash].first;
186 st->hashtab[hash].first = st->winpos;
187 if (off != INVALID)
188 st->win[off].prev = st->winpos;
189 st->data[st->winpos] = c;
190
191 /*
192 * Advance the window pointer.
193 */
32874aea 194 st->winpos = (st->winpos + 1) & (WINSIZE - 1);
4ba9b64b 195}
196
197#define CHARAT(k) ( (k)<0 ? st->data[(st->winpos+k)&(WINSIZE-1)] : data[k] )
198
199static void lz77_compress(struct LZ77Context *ctx,
32874aea 200 unsigned char *data, int len, int compress)
201{
4ba9b64b 202 struct LZ77InternalContext *st = ctx->ictx;
203 int i, hash, distance, off, nmatch, matchlen, advance;
204 struct Match defermatch, matches[MAXMATCH];
205 int deferchr;
206
207 /*
208 * Add any pending characters from last time to the window. (We
209 * might not be able to.)
210 */
211 for (i = 0; i < st->npending; i++) {
212 unsigned char foo[HASHCHARS];
213 int j;
214 if (len + st->npending - i < HASHCHARS) {
215 /* Update the pending array. */
216 for (j = i; j < st->npending; j++)
32874aea 217 st->pending[j - i] = st->pending[j];
4ba9b64b 218 break;
219 }
220 for (j = 0; j < HASHCHARS; j++)
32874aea 221 foo[j] = (i + j < st->npending ? st->pending[i + j] :
4ba9b64b 222 data[i + j - st->npending]);
223 lz77_advance(st, foo[0], lz77_hash(foo));
224 }
225 st->npending -= i;
226
227 defermatch.len = 0;
2d466ffd 228 deferchr = '\0';
4ba9b64b 229 while (len > 0) {
230
32874aea 231 /* Don't even look for a match, if we're not compressing. */
232 if (compress && len >= HASHCHARS) {
233 /*
234 * Hash the next few characters.
235 */
236 hash = lz77_hash(data);
237
238 /*
239 * Look the hash up in the corresponding hash chain and see
240 * what we can find.
241 */
242 nmatch = 0;
243 for (off = st->hashtab[hash].first;
244 off != INVALID; off = st->win[off].next) {
245 /* distance = 1 if off == st->winpos-1 */
246 /* distance = WINSIZE if off == st->winpos */
247 distance =
248 WINSIZE - (off + WINSIZE - st->winpos) % WINSIZE;
249 for (i = 0; i < HASHCHARS; i++)
250 if (CHARAT(i) != CHARAT(i - distance))
251 break;
252 if (i == HASHCHARS) {
253 matches[nmatch].distance = distance;
254 matches[nmatch].len = 3;
255 if (++nmatch >= MAXMATCH)
256 break;
257 }
258 }
259 } else {
260 nmatch = 0;
4ba9b64b 261 hash = INVALID;
262 }
263
264 if (nmatch > 0) {
265 /*
266 * We've now filled up matches[] with nmatch potential
267 * matches. Follow them down to find the longest. (We
268 * assume here that it's always worth favouring a
269 * longer match over a shorter one.)
270 */
271 matchlen = HASHCHARS;
272 while (matchlen < len) {
273 int j;
274 for (i = j = 0; i < nmatch; i++) {
275 if (CHARAT(matchlen) ==
276 CHARAT(matchlen - matches[i].distance)) {
277 matches[j++] = matches[i];
278 }
279 }
280 if (j == 0)
281 break;
282 matchlen++;
283 nmatch = j;
284 }
285
286 /*
287 * We've now got all the longest matches. We favour the
288 * shorter distances, which means we go with matches[0].
289 * So see if we want to defer it or throw it away.
290 */
291 matches[0].len = matchlen;
292 if (defermatch.len > 0) {
293 if (matches[0].len > defermatch.len + 1) {
294 /* We have a better match. Emit the deferred char,
295 * and defer this match. */
32874aea 296 ctx->literal(ctx, (unsigned char) deferchr);
4ba9b64b 297 defermatch = matches[0];
298 deferchr = data[0];
299 advance = 1;
300 } else {
301 /* We don't have a better match. Do the deferred one. */
302 ctx->match(ctx, defermatch.distance, defermatch.len);
303 advance = defermatch.len - 1;
304 defermatch.len = 0;
305 }
306 } else {
307 /* There was no deferred match. Defer this one. */
308 defermatch = matches[0];
309 deferchr = data[0];
310 advance = 1;
32874aea 311 }
4ba9b64b 312 } else {
313 /*
314 * We found no matches. Emit the deferred match, if
315 * any; otherwise emit a literal.
316 */
317 if (defermatch.len > 0) {
318 ctx->match(ctx, defermatch.distance, defermatch.len);
319 advance = defermatch.len - 1;
320 defermatch.len = 0;
321 } else {
322 ctx->literal(ctx, data[0]);
323 advance = 1;
324 }
325 }
326
327 /*
328 * Now advance the position by `advance' characters,
329 * keeping the window and hash chains consistent.
330 */
331 while (advance > 0) {
332 if (len >= HASHCHARS) {
333 lz77_advance(st, *data, lz77_hash(data));
334 } else {
335 st->pending[st->npending++] = *data;
336 }
337 data++;
338 len--;
339 advance--;
340 }
341 }
342}
343
344/* ----------------------------------------------------------------------
345 * Zlib compression. We always use the static Huffman tree option.
346 * Mostly this is because it's hard to scan a block in advance to
347 * work out better trees; dynamic trees are great when you're
348 * compressing a large file under no significant time constraint,
349 * but when you're compressing little bits in real time, things get
350 * hairier.
351 *
352 * I suppose it's possible that I could compute Huffman trees based
353 * on the frequencies in the _previous_ block, as a sort of
354 * heuristic, but I'm not confident that the gain would balance out
355 * having to transmit the trees.
356 */
357
4ba9b64b 358struct Outbuf {
359 unsigned char *outbuf;
360 int outlen, outsize;
361 unsigned long outbits;
362 int noutbits;
363 int firstblock;
6e9e9520 364 int comp_disabled;
4ba9b64b 365};
366
32874aea 367static void outbits(struct Outbuf *out, unsigned long bits, int nbits)
368{
4ba9b64b 369 assert(out->noutbits + nbits <= 32);
370 out->outbits |= bits << out->noutbits;
371 out->noutbits += nbits;
372 while (out->noutbits >= 8) {
32874aea 373 if (out->outlen >= out->outsize) {
374 out->outsize = out->outlen + 64;
3d88e64d 375 out->outbuf = sresize(out->outbuf, out->outsize, unsigned char);
32874aea 376 }
377 out->outbuf[out->outlen++] = (unsigned char) (out->outbits & 0xFF);
378 out->outbits >>= 8;
379 out->noutbits -= 8;
4ba9b64b 380 }
381}
382
383static const unsigned char mirrorbytes[256] = {
384 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
385 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
386 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
387 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
388 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
389 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
390 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
391 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
392 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
393 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
394 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
395 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
396 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
397 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
398 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
399 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
400 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
401 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
402 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
403 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
404 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
405 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
406 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
407 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
408 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
409 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
410 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
411 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
412 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
413 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
414 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
415 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
416};
417
418typedef struct {
d2371c81 419 short code, extrabits;
420 int min, max;
4ba9b64b 421} coderecord;
422
423static const coderecord lencodes[] = {
32874aea 424 {257, 0, 3, 3},
425 {258, 0, 4, 4},
426 {259, 0, 5, 5},
427 {260, 0, 6, 6},
428 {261, 0, 7, 7},
429 {262, 0, 8, 8},
430 {263, 0, 9, 9},
431 {264, 0, 10, 10},
432 {265, 1, 11, 12},
433 {266, 1, 13, 14},
434 {267, 1, 15, 16},
435 {268, 1, 17, 18},
436 {269, 2, 19, 22},
437 {270, 2, 23, 26},
438 {271, 2, 27, 30},
439 {272, 2, 31, 34},
440 {273, 3, 35, 42},
441 {274, 3, 43, 50},
442 {275, 3, 51, 58},
443 {276, 3, 59, 66},
444 {277, 4, 67, 82},
445 {278, 4, 83, 98},
446 {279, 4, 99, 114},
447 {280, 4, 115, 130},
448 {281, 5, 131, 162},
449 {282, 5, 163, 194},
450 {283, 5, 195, 226},
451 {284, 5, 227, 257},
452 {285, 0, 258, 258},
4ba9b64b 453};
454
455static const coderecord distcodes[] = {
32874aea 456 {0, 0, 1, 1},
457 {1, 0, 2, 2},
458 {2, 0, 3, 3},
459 {3, 0, 4, 4},
460 {4, 1, 5, 6},
461 {5, 1, 7, 8},
462 {6, 2, 9, 12},
463 {7, 2, 13, 16},
464 {8, 3, 17, 24},
465 {9, 3, 25, 32},
466 {10, 4, 33, 48},
467 {11, 4, 49, 64},
468 {12, 5, 65, 96},
469 {13, 5, 97, 128},
470 {14, 6, 129, 192},
471 {15, 6, 193, 256},
472 {16, 7, 257, 384},
473 {17, 7, 385, 512},
474 {18, 8, 513, 768},
475 {19, 8, 769, 1024},
476 {20, 9, 1025, 1536},
477 {21, 9, 1537, 2048},
478 {22, 10, 2049, 3072},
479 {23, 10, 3073, 4096},
480 {24, 11, 4097, 6144},
481 {25, 11, 6145, 8192},
482 {26, 12, 8193, 12288},
483 {27, 12, 12289, 16384},
484 {28, 13, 16385, 24576},
485 {29, 13, 24577, 32768},
4ba9b64b 486};
487
32874aea 488static void zlib_literal(struct LZ77Context *ectx, unsigned char c)
489{
490 struct Outbuf *out = (struct Outbuf *) ectx->userdata;
4ba9b64b 491
6e9e9520 492 if (out->comp_disabled) {
32874aea 493 /*
494 * We're in an uncompressed block, so just output the byte.
495 */
496 outbits(out, c, 8);
497 return;
6e9e9520 498 }
499
4ba9b64b 500 if (c <= 143) {
32874aea 501 /* 0 through 143 are 8 bits long starting at 00110000. */
502 outbits(out, mirrorbytes[0x30 + c], 8);
4ba9b64b 503 } else {
32874aea 504 /* 144 through 255 are 9 bits long starting at 110010000. */
505 outbits(out, 1 + 2 * mirrorbytes[0x90 - 144 + c], 9);
4ba9b64b 506 }
507}
508
32874aea 509static void zlib_match(struct LZ77Context *ectx, int distance, int len)
510{
4ba9b64b 511 const coderecord *d, *l;
512 int i, j, k;
32874aea 513 struct Outbuf *out = (struct Outbuf *) ectx->userdata;
6e9e9520 514
515 assert(!out->comp_disabled);
516
4ba9b64b 517 while (len > 0) {
32874aea 518 int thislen;
519
4ba9b64b 520 /*
521 * We can transmit matches of lengths 3 through 258
522 * inclusive. So if len exceeds 258, we must transmit in
523 * several steps, with 258 or less in each step.
524 *
525 * Specifically: if len >= 261, we can transmit 258 and be
526 * sure of having at least 3 left for the next step. And if
527 * len <= 258, we can just transmit len. But if len == 259
528 * or 260, we must transmit len-3.
529 */
32874aea 530 thislen = (len > 260 ? 258 : len <= 258 ? len : len - 3);
531 len -= thislen;
532
533 /*
534 * Binary-search to find which length code we're
535 * transmitting.
536 */
537 i = -1;
538 j = sizeof(lencodes) / sizeof(*lencodes);
2d466ffd 539 while (1) {
540 assert(j - i >= 2);
32874aea 541 k = (j + i) / 2;
542 if (thislen < lencodes[k].min)
543 j = k;
544 else if (thislen > lencodes[k].max)
545 i = k;
546 else {
547 l = &lencodes[k];
548 break; /* found it! */
549 }
550 }
551
552 /*
553 * Transmit the length code. 256-279 are seven bits
554 * starting at 0000000; 280-287 are eight bits starting at
555 * 11000000.
556 */
557 if (l->code <= 279) {
558 outbits(out, mirrorbytes[(l->code - 256) * 2], 7);
559 } else {
560 outbits(out, mirrorbytes[0xc0 - 280 + l->code], 8);
561 }
562
563 /*
564 * Transmit the extra bits.
565 */
566 if (l->extrabits)
567 outbits(out, thislen - l->min, l->extrabits);
568
569 /*
570 * Binary-search to find which distance code we're
571 * transmitting.
572 */
573 i = -1;
574 j = sizeof(distcodes) / sizeof(*distcodes);
2d466ffd 575 while (1) {
576 assert(j - i >= 2);
32874aea 577 k = (j + i) / 2;
578 if (distance < distcodes[k].min)
579 j = k;
580 else if (distance > distcodes[k].max)
581 i = k;
582 else {
583 d = &distcodes[k];
584 break; /* found it! */
585 }
586 }
587
588 /*
589 * Transmit the distance code. Five bits starting at 00000.
590 */
591 outbits(out, mirrorbytes[d->code * 8], 5);
592
593 /*
594 * Transmit the extra bits.
595 */
596 if (d->extrabits)
597 outbits(out, distance - d->min, d->extrabits);
4ba9b64b 598 }
599}
600
5366aed8 601void *zlib_compress_init(void)
32874aea 602{
4ba9b64b 603 struct Outbuf *out;
3d88e64d 604 struct LZ77Context *ectx = snew(struct LZ77Context);
4ba9b64b 605
5366aed8 606 lz77_init(ectx);
607 ectx->literal = zlib_literal;
608 ectx->match = zlib_match;
4ba9b64b 609
3d88e64d 610 out = snew(struct Outbuf);
4ba9b64b 611 out->outbits = out->noutbits = 0;
612 out->firstblock = 1;
6e9e9520 613 out->comp_disabled = FALSE;
5366aed8 614 ectx->userdata = out;
615
616 return ectx;
617}
4ba9b64b 618
5366aed8 619void zlib_compress_cleanup(void *handle)
620{
621 struct LZ77Context *ectx = (struct LZ77Context *)handle;
622 sfree(ectx->userdata);
29b1d0b3 623 sfree(ectx->ictx);
624 sfree(ectx);
4ba9b64b 625}
626
6e9e9520 627/*
628 * Turn off actual LZ77 analysis for one block, to facilitate
629 * construction of a precise-length IGNORE packet. Returns the
630 * length adjustment (which is only valid for packets < 65536
631 * bytes, but that seems reasonable enough).
632 */
6958ec09 633static int zlib_disable_compression(void *handle)
32874aea 634{
5366aed8 635 struct LZ77Context *ectx = (struct LZ77Context *)handle;
636 struct Outbuf *out = (struct Outbuf *) ectx->userdata;
dae2b91f 637 int n;
6e9e9520 638
639 out->comp_disabled = TRUE;
640
641 n = 0;
642 /*
643 * If this is the first block, we will start by outputting two
644 * header bytes, and then three bits to begin an uncompressed
645 * block. This will cost three bytes (because we will start on
646 * a byte boundary, this is certain).
647 */
648 if (out->firstblock) {
32874aea 649 n = 3;
6e9e9520 650 } else {
32874aea 651 /*
652 * Otherwise, we will output seven bits to close the
653 * previous static block, and _then_ three bits to begin an
654 * uncompressed block, and then flush the current byte.
655 * This may cost two bytes or three, depending on noutbits.
656 */
657 n += (out->noutbits + 10) / 8;
6e9e9520 658 }
659
660 /*
661 * Now we output four bytes for the length / ~length pair in
662 * the uncompressed block.
663 */
664 n += 4;
665
666 return n;
667}
668
5366aed8 669int zlib_compress_block(void *handle, unsigned char *block, int len,
32874aea 670 unsigned char **outblock, int *outlen)
671{
5366aed8 672 struct LZ77Context *ectx = (struct LZ77Context *)handle;
673 struct Outbuf *out = (struct Outbuf *) ectx->userdata;
6e9e9520 674 int in_block;
4ba9b64b 675
676 out->outbuf = NULL;
677 out->outlen = out->outsize = 0;
678
679 /*
680 * If this is the first block, output the Zlib (RFC1950) header
681 * bytes 78 9C. (Deflate compression, 32K window size, default
682 * algorithm.)
683 */
684 if (out->firstblock) {
32874aea 685 outbits(out, 0x9C78, 16);
686 out->firstblock = 0;
6e9e9520 687
32874aea 688 in_block = FALSE;
2d466ffd 689 } else
690 in_block = TRUE;
4ba9b64b 691
6e9e9520 692 if (out->comp_disabled) {
32874aea 693 if (in_block)
694 outbits(out, 0, 7); /* close static block */
695
696 while (len > 0) {
697 int blen = (len < 65535 ? len : 65535);
698
699 /*
700 * Start a Deflate (RFC1951) uncompressed block. We
701 * transmit a zero bit (BFINAL=0), followed by a zero
702 * bit and a one bit (BTYPE=00). Of course these are in
703 * the wrong order (00 0).
704 */
705 outbits(out, 0, 3);
706
707 /*
708 * Output zero bits to align to a byte boundary.
709 */
710 if (out->noutbits)
711 outbits(out, 0, 8 - out->noutbits);
712
713 /*
714 * Output the block length, and then its one's
715 * complement. They're little-endian, so all we need to
716 * do is pass them straight to outbits() with bit count
717 * 16.
718 */
719 outbits(out, blen, 16);
720 outbits(out, blen ^ 0xFFFF, 16);
721
722 /*
723 * Do the `compression': we need to pass the data to
724 * lz77_compress so that it will be taken into account
725 * for subsequent (distance,length) pairs. But
726 * lz77_compress is passed FALSE, which means it won't
727 * actually find (or even look for) any matches; so
728 * every character will be passed straight to
729 * zlib_literal which will spot out->comp_disabled and
730 * emit in the uncompressed format.
731 */
5366aed8 732 lz77_compress(ectx, block, blen, FALSE);
32874aea 733
734 len -= blen;
735 block += blen;
736 }
737 outbits(out, 2, 3); /* open new block */
6e9e9520 738 } else {
32874aea 739 if (!in_block) {
740 /*
741 * Start a Deflate (RFC1951) fixed-trees block. We
742 * transmit a zero bit (BFINAL=0), followed by a zero
743 * bit and a one bit (BTYPE=01). Of course these are in
744 * the wrong order (01 0).
745 */
746 outbits(out, 2, 3);
747 }
748
749 /*
750 * Do the compression.
751 */
5366aed8 752 lz77_compress(ectx, block, len, TRUE);
32874aea 753
754 /*
755 * End the block (by transmitting code 256, which is
756 * 0000000 in fixed-tree mode), and transmit some empty
757 * blocks to ensure we have emitted the byte containing the
758 * last piece of genuine data. There are three ways we can
759 * do this:
760 *
761 * - Minimal flush. Output end-of-block and then open a
762 * new static block. This takes 9 bits, which is
763 * guaranteed to flush out the last genuine code in the
764 * closed block; but allegedly zlib can't handle it.
765 *
766 * - Zlib partial flush. Output EOB, open and close an
767 * empty static block, and _then_ open the new block.
768 * This is the best zlib can handle.
769 *
770 * - Zlib sync flush. Output EOB, then an empty
771 * _uncompressed_ block (000, then sync to byte
772 * boundary, then send bytes 00 00 FF FF). Then open the
773 * new block.
774 *
775 * For the moment, we will use Zlib partial flush.
776 */
777 outbits(out, 0, 7); /* close block */
778 outbits(out, 2, 3 + 7); /* empty static block */
779 outbits(out, 2, 3); /* open new block */
6e9e9520 780 }
781
782 out->comp_disabled = FALSE;
4ba9b64b 783
784 *outblock = out->outbuf;
785 *outlen = out->outlen;
786
787 return 1;
788}
789
790/* ----------------------------------------------------------------------
791 * Zlib decompression. Of course, even though our compressor always
792 * uses static trees, our _decompressor_ has to be capable of
793 * handling dynamic trees if it sees them.
794 */
795
796/*
797 * The way we work the Huffman decode is to have a table lookup on
798 * the first N bits of the input stream (in the order they arrive,
799 * of course, i.e. the first bit of the Huffman code is in bit 0).
800 * Each table entry lists the number of bits to consume, plus
801 * either an output code or a pointer to a secondary table.
802 */
803struct zlib_table;
804struct zlib_tableentry;
805
806struct zlib_tableentry {
807 unsigned char nbits;
d2371c81 808 short code;
4ba9b64b 809 struct zlib_table *nexttable;
810};
811
812struct zlib_table {
32874aea 813 int mask; /* mask applied to input bit stream */
4ba9b64b 814 struct zlib_tableentry *table;
815};
816
817#define MAXCODELEN 16
818#define MAXSYMS 288
819
820/*
821 * Build a single-level decode table for elements
822 * [minlength,maxlength) of the provided code/length tables, and
823 * recurse to build subtables.
824 */
825static struct zlib_table *zlib_mkonetab(int *codes, unsigned char *lengths,
32874aea 826 int nsyms,
827 int pfx, int pfxbits, int bits)
828{
3d88e64d 829 struct zlib_table *tab = snew(struct zlib_table);
4ba9b64b 830 int pfxmask = (1 << pfxbits) - 1;
831 int nbits, i, j, code;
832
3d88e64d 833 tab->table = snewn(1 << bits, struct zlib_tableentry);
4ba9b64b 834 tab->mask = (1 << bits) - 1;
835
836 for (code = 0; code <= tab->mask; code++) {
32874aea 837 tab->table[code].code = -1;
838 tab->table[code].nbits = 0;
839 tab->table[code].nexttable = NULL;
4ba9b64b 840 }
841
842 for (i = 0; i < nsyms; i++) {
32874aea 843 if (lengths[i] <= pfxbits || (codes[i] & pfxmask) != pfx)
844 continue;
845 code = (codes[i] >> pfxbits) & tab->mask;
846 for (j = code; j <= tab->mask; j += 1 << (lengths[i] - pfxbits)) {
847 tab->table[j].code = i;
848 nbits = lengths[i] - pfxbits;
849 if (tab->table[j].nbits < nbits)
850 tab->table[j].nbits = nbits;
851 }
4ba9b64b 852 }
853 for (code = 0; code <= tab->mask; code++) {
32874aea 854 if (tab->table[code].nbits <= bits)
855 continue;
856 /* Generate a subtable. */
857 tab->table[code].code = -1;
858 nbits = tab->table[code].nbits - bits;
859 if (nbits > 7)
860 nbits = 7;
861 tab->table[code].nbits = bits;
862 tab->table[code].nexttable = zlib_mkonetab(codes, lengths, nsyms,
863 pfx | (code << pfxbits),
864 pfxbits + bits, nbits);
4ba9b64b 865 }
866
867 return tab;
868}
869
870/*
871 * Build a decode table, given a set of Huffman tree lengths.
872 */
32874aea 873static struct zlib_table *zlib_mktable(unsigned char *lengths,
874 int nlengths)
875{
4ba9b64b 876 int count[MAXCODELEN], startcode[MAXCODELEN], codes[MAXSYMS];
877 int code, maxlen;
878 int i, j;
879
880 /* Count the codes of each length. */
881 maxlen = 0;
32874aea 882 for (i = 1; i < MAXCODELEN; i++)
883 count[i] = 0;
4ba9b64b 884 for (i = 0; i < nlengths; i++) {
32874aea 885 count[lengths[i]]++;
886 if (maxlen < lengths[i])
887 maxlen = lengths[i];
4ba9b64b 888 }
889 /* Determine the starting code for each length block. */
890 code = 0;
891 for (i = 1; i < MAXCODELEN; i++) {
32874aea 892 startcode[i] = code;
893 code += count[i];
894 code <<= 1;
4ba9b64b 895 }
896 /* Determine the code for each symbol. Mirrored, of course. */
897 for (i = 0; i < nlengths; i++) {
32874aea 898 code = startcode[lengths[i]]++;
899 codes[i] = 0;
900 for (j = 0; j < lengths[i]; j++) {
901 codes[i] = (codes[i] << 1) | (code & 1);
902 code >>= 1;
903 }
4ba9b64b 904 }
905
906 /*
907 * Now we have the complete list of Huffman codes. Build a
908 * table.
909 */
910 return zlib_mkonetab(codes, lengths, nlengths, 0, 0,
32874aea 911 maxlen < 9 ? maxlen : 9);
4ba9b64b 912}
913
32874aea 914static int zlib_freetable(struct zlib_table **ztab)
915{
dda46d54 916 struct zlib_table *tab;
917 int code;
918
919 if (ztab == NULL)
920 return -1;
921
922 if (*ztab == NULL)
923 return 0;
924
925 tab = *ztab;
926
927 for (code = 0; code <= tab->mask; code++)
928 if (tab->table[code].nexttable != NULL)
929 zlib_freetable(&tab->table[code].nexttable);
930
931 sfree(tab->table);
932 tab->table = NULL;
933
934 sfree(tab);
935 *ztab = NULL;
936
32874aea 937 return (0);
dda46d54 938}
939
5366aed8 940struct zlib_decompress_ctx {
4ba9b64b 941 struct zlib_table *staticlentable, *staticdisttable;
942 struct zlib_table *currlentable, *currdisttable, *lenlentable;
943 enum {
32874aea 944 START, OUTSIDEBLK,
945 TREES_HDR, TREES_LENLEN, TREES_LEN, TREES_LENREP,
946 INBLK, GOTLENSYM, GOTLEN, GOTDISTSYM,
947 UNCOMP_LEN, UNCOMP_NLEN, UNCOMP_DATA
4ba9b64b 948 } state;
32874aea 949 int sym, hlit, hdist, hclen, lenptr, lenextrabits, lenaddon, len,
950 lenrep;
4ba9b64b 951 int uncomplen;
952 unsigned char lenlen[19];
32874aea 953 unsigned char lengths[286 + 32];
4ba9b64b 954 unsigned long bits;
955 int nbits;
956 unsigned char window[WINSIZE];
957 int winpos;
958 unsigned char *outblk;
959 int outlen, outsize;
5366aed8 960};
4ba9b64b 961
5366aed8 962void *zlib_decompress_init(void)
32874aea 963{
3d88e64d 964 struct zlib_decompress_ctx *dctx = snew(struct zlib_decompress_ctx);
4ba9b64b 965 unsigned char lengths[288];
5366aed8 966
4ba9b64b 967 memset(lengths, 8, 144);
32874aea 968 memset(lengths + 144, 9, 256 - 144);
969 memset(lengths + 256, 7, 280 - 256);
970 memset(lengths + 280, 8, 288 - 280);
5366aed8 971 dctx->staticlentable = zlib_mktable(lengths, 288);
4ba9b64b 972 memset(lengths, 5, 32);
5366aed8 973 dctx->staticdisttable = zlib_mktable(lengths, 32);
974 dctx->state = START; /* even before header */
975 dctx->currlentable = dctx->currdisttable = dctx->lenlentable = NULL;
976 dctx->bits = 0;
977 dctx->nbits = 0;
a8327734 978 dctx->winpos = 0;
5366aed8 979
980 return dctx;
981}
982
983void zlib_decompress_cleanup(void *handle)
984{
985 struct zlib_decompress_ctx *dctx = (struct zlib_decompress_ctx *)handle;
29b1d0b3 986
5366aed8 987 if (dctx->currlentable && dctx->currlentable != dctx->staticlentable)
988 zlib_freetable(&dctx->currlentable);
989 if (dctx->currdisttable && dctx->currdisttable != dctx->staticdisttable)
990 zlib_freetable(&dctx->currdisttable);
991 if (dctx->lenlentable)
992 zlib_freetable(&dctx->lenlentable);
29b1d0b3 993 zlib_freetable(&dctx->staticlentable);
994 zlib_freetable(&dctx->staticdisttable);
5366aed8 995 sfree(dctx);
4ba9b64b 996}
997
6958ec09 998static int zlib_huflookup(unsigned long *bitsp, int *nbitsp,
32874aea 999 struct zlib_table *tab)
1000{
4ba9b64b 1001 unsigned long bits = *bitsp;
1002 int nbits = *nbitsp;
1003 while (1) {
32874aea 1004 struct zlib_tableentry *ent;
1005 ent = &tab->table[bits & tab->mask];
1006 if (ent->nbits > nbits)
1007 return -1; /* not enough data */
1008 bits >>= ent->nbits;
1009 nbits -= ent->nbits;
1010 if (ent->code == -1)
1011 tab = ent->nexttable;
1012 else {
1013 *bitsp = bits;
1014 *nbitsp = nbits;
1015 return ent->code;
1016 }
36b8d9bb 1017
1018 if (!tab) {
1019 /*
1020 * There was a missing entry in the table, presumably
1021 * due to an invalid Huffman table description, and the
1022 * subsequent data has attempted to use the missing
1023 * entry. Return a decoding failure.
1024 */
1025 return -2;
1026 }
4ba9b64b 1027 }
1028}
1029
5366aed8 1030static void zlib_emit_char(struct zlib_decompress_ctx *dctx, int c)
32874aea 1031{
5366aed8 1032 dctx->window[dctx->winpos] = c;
1033 dctx->winpos = (dctx->winpos + 1) & (WINSIZE - 1);
1034 if (dctx->outlen >= dctx->outsize) {
1035 dctx->outsize = dctx->outlen + 512;
3d88e64d 1036 dctx->outblk = sresize(dctx->outblk, dctx->outsize, unsigned char);
4ba9b64b 1037 }
5366aed8 1038 dctx->outblk[dctx->outlen++] = c;
4ba9b64b 1039}
1040
5366aed8 1041#define EATBITS(n) ( dctx->nbits -= (n), dctx->bits >>= (n) )
4ba9b64b 1042
5366aed8 1043int zlib_decompress_block(void *handle, unsigned char *block, int len,
32874aea 1044 unsigned char **outblock, int *outlen)
1045{
5366aed8 1046 struct zlib_decompress_ctx *dctx = (struct zlib_decompress_ctx *)handle;
4ba9b64b 1047 const coderecord *rec;
52912ff3 1048 int code, blktype, rep, dist, nlen, header;
4ba9b64b 1049 static const unsigned char lenlenmap[] = {
32874aea 1050 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
4ba9b64b 1051 };
1052
52912ff3 1053 dctx->outblk = snewn(256, unsigned char);
1054 dctx->outsize = 256;
1055 dctx->outlen = 0;
4ba9b64b 1056
5366aed8 1057 while (len > 0 || dctx->nbits > 0) {
1058 while (dctx->nbits < 24 && len > 0) {
1059 dctx->bits |= (*block++) << dctx->nbits;
1060 dctx->nbits += 8;
32874aea 1061 len--;
1062 }
5366aed8 1063 switch (dctx->state) {
32874aea 1064 case START:
52912ff3 1065 /* Expect 16-bit zlib header. */
5366aed8 1066 if (dctx->nbits < 16)
32874aea 1067 goto finished; /* done all we can */
52912ff3 1068
1069 /*
1070 * The header is stored as a big-endian 16-bit integer,
1071 * in contrast to the general little-endian policy in
1072 * the rest of the format :-(
1073 */
1074 header = (((dctx->bits & 0xFF00) >> 8) |
1075 ((dctx->bits & 0x00FF) << 8));
1076 EATBITS(16);
1077
1078 /*
1079 * Check the header:
1080 *
1081 * - bits 8-11 should be 1000 (Deflate/RFC1951)
1082 * - bits 12-15 should be at most 0111 (window size)
1083 * - bit 5 should be zero (no dictionary present)
1084 * - we don't care about bits 6-7 (compression rate)
1085 * - bits 0-4 should be set up to make the whole thing
1086 * a multiple of 31 (checksum).
1087 */
1088 if ((header & 0x0F00) != 0x0800 ||
1089 (header & 0xF000) > 0x7000 ||
1090 (header & 0x0020) != 0x0000 ||
1091 (header % 31) != 0)
1092 goto decode_error;
1093
5366aed8 1094 dctx->state = OUTSIDEBLK;
32874aea 1095 break;
1096 case OUTSIDEBLK:
1097 /* Expect 3-bit block header. */
5366aed8 1098 if (dctx->nbits < 3)
32874aea 1099 goto finished; /* done all we can */
1100 EATBITS(1);
5366aed8 1101 blktype = dctx->bits & 3;
32874aea 1102 EATBITS(2);
1103 if (blktype == 0) {
5366aed8 1104 int to_eat = dctx->nbits & 7;
1105 dctx->state = UNCOMP_LEN;
4ba9b64b 1106 EATBITS(to_eat); /* align to byte boundary */
32874aea 1107 } else if (blktype == 1) {
5366aed8 1108 dctx->currlentable = dctx->staticlentable;
1109 dctx->currdisttable = dctx->staticdisttable;
1110 dctx->state = INBLK;
32874aea 1111 } else if (blktype == 2) {
5366aed8 1112 dctx->state = TREES_HDR;
32874aea 1113 }
1114 break;
1115 case TREES_HDR:
1116 /*
1117 * Dynamic block header. Five bits of HLIT, five of
1118 * HDIST, four of HCLEN.
1119 */
5366aed8 1120 if (dctx->nbits < 5 + 5 + 4)
32874aea 1121 goto finished; /* done all we can */
5366aed8 1122 dctx->hlit = 257 + (dctx->bits & 31);
32874aea 1123 EATBITS(5);
5366aed8 1124 dctx->hdist = 1 + (dctx->bits & 31);
32874aea 1125 EATBITS(5);
5366aed8 1126 dctx->hclen = 4 + (dctx->bits & 15);
32874aea 1127 EATBITS(4);
5366aed8 1128 dctx->lenptr = 0;
1129 dctx->state = TREES_LENLEN;
1130 memset(dctx->lenlen, 0, sizeof(dctx->lenlen));
32874aea 1131 break;
1132 case TREES_LENLEN:
5366aed8 1133 if (dctx->nbits < 3)
32874aea 1134 goto finished;
5366aed8 1135 while (dctx->lenptr < dctx->hclen && dctx->nbits >= 3) {
1136 dctx->lenlen[lenlenmap[dctx->lenptr++]] =
1137 (unsigned char) (dctx->bits & 7);
32874aea 1138 EATBITS(3);
1139 }
5366aed8 1140 if (dctx->lenptr == dctx->hclen) {
1141 dctx->lenlentable = zlib_mktable(dctx->lenlen, 19);
1142 dctx->state = TREES_LEN;
1143 dctx->lenptr = 0;
32874aea 1144 }
1145 break;
1146 case TREES_LEN:
5366aed8 1147 if (dctx->lenptr >= dctx->hlit + dctx->hdist) {
1148 dctx->currlentable = zlib_mktable(dctx->lengths, dctx->hlit);
1149 dctx->currdisttable = zlib_mktable(dctx->lengths + dctx->hlit,
1150 dctx->hdist);
1151 zlib_freetable(&dctx->lenlentable);
1152 dctx->lenlentable = NULL;
1153 dctx->state = INBLK;
32874aea 1154 break;
1155 }
1156 code =
5366aed8 1157 zlib_huflookup(&dctx->bits, &dctx->nbits, dctx->lenlentable);
32874aea 1158 if (code == -1)
1159 goto finished;
36b8d9bb 1160 if (code == -2)
1161 goto decode_error;
32874aea 1162 if (code < 16)
5366aed8 1163 dctx->lengths[dctx->lenptr++] = code;
32874aea 1164 else {
5366aed8 1165 dctx->lenextrabits = (code == 16 ? 2 : code == 17 ? 3 : 7);
1166 dctx->lenaddon = (code == 18 ? 11 : 3);
1167 dctx->lenrep = (code == 16 && dctx->lenptr > 0 ?
1168 dctx->lengths[dctx->lenptr - 1] : 0);
1169 dctx->state = TREES_LENREP;
32874aea 1170 }
1171 break;
1172 case TREES_LENREP:
5366aed8 1173 if (dctx->nbits < dctx->lenextrabits)
32874aea 1174 goto finished;
1175 rep =
5366aed8 1176 dctx->lenaddon +
1177 (dctx->bits & ((1 << dctx->lenextrabits) - 1));
1178 EATBITS(dctx->lenextrabits);
1179 while (rep > 0 && dctx->lenptr < dctx->hlit + dctx->hdist) {
1180 dctx->lengths[dctx->lenptr] = dctx->lenrep;
1181 dctx->lenptr++;
32874aea 1182 rep--;
1183 }
5366aed8 1184 dctx->state = TREES_LEN;
32874aea 1185 break;
1186 case INBLK:
1187 code =
5366aed8 1188 zlib_huflookup(&dctx->bits, &dctx->nbits, dctx->currlentable);
32874aea 1189 if (code == -1)
1190 goto finished;
36b8d9bb 1191 if (code == -2)
1192 goto decode_error;
32874aea 1193 if (code < 256)
5366aed8 1194 zlib_emit_char(dctx, code);
32874aea 1195 else if (code == 256) {
5366aed8 1196 dctx->state = OUTSIDEBLK;
1197 if (dctx->currlentable != dctx->staticlentable) {
1198 zlib_freetable(&dctx->currlentable);
1199 dctx->currlentable = NULL;
1200 }
1201 if (dctx->currdisttable != dctx->staticdisttable) {
1202 zlib_freetable(&dctx->currdisttable);
1203 dctx->currdisttable = NULL;
1204 }
32874aea 1205 } else if (code < 286) { /* static tree can give >285; ignore */
5366aed8 1206 dctx->state = GOTLENSYM;
1207 dctx->sym = code;
32874aea 1208 }
1209 break;
1210 case GOTLENSYM:
5366aed8 1211 rec = &lencodes[dctx->sym - 257];
1212 if (dctx->nbits < rec->extrabits)
32874aea 1213 goto finished;
5366aed8 1214 dctx->len =
1215 rec->min + (dctx->bits & ((1 << rec->extrabits) - 1));
32874aea 1216 EATBITS(rec->extrabits);
5366aed8 1217 dctx->state = GOTLEN;
32874aea 1218 break;
1219 case GOTLEN:
1220 code =
5366aed8 1221 zlib_huflookup(&dctx->bits, &dctx->nbits,
1222 dctx->currdisttable);
32874aea 1223 if (code == -1)
1224 goto finished;
36b8d9bb 1225 if (code == -2)
1226 goto decode_error;
5366aed8 1227 dctx->state = GOTDISTSYM;
1228 dctx->sym = code;
32874aea 1229 break;
1230 case GOTDISTSYM:
5366aed8 1231 rec = &distcodes[dctx->sym];
1232 if (dctx->nbits < rec->extrabits)
32874aea 1233 goto finished;
5366aed8 1234 dist = rec->min + (dctx->bits & ((1 << rec->extrabits) - 1));
32874aea 1235 EATBITS(rec->extrabits);
5366aed8 1236 dctx->state = INBLK;
1237 while (dctx->len--)
1238 zlib_emit_char(dctx, dctx->window[(dctx->winpos - dist) &
1239 (WINSIZE - 1)]);
4ba9b64b 1240 break;
1241 case UNCOMP_LEN:
1242 /*
1243 * Uncompressed block. We expect to see a 16-bit LEN.
1244 */
5366aed8 1245 if (dctx->nbits < 16)
4ba9b64b 1246 goto finished;
5366aed8 1247 dctx->uncomplen = dctx->bits & 0xFFFF;
4ba9b64b 1248 EATBITS(16);
5366aed8 1249 dctx->state = UNCOMP_NLEN;
4ba9b64b 1250 break;
1251 case UNCOMP_NLEN:
1252 /*
1253 * Uncompressed block. We expect to see a 16-bit NLEN,
1254 * which should be the one's complement of the previous
1255 * LEN.
1256 */
5366aed8 1257 if (dctx->nbits < 16)
4ba9b64b 1258 goto finished;
5366aed8 1259 nlen = dctx->bits & 0xFFFF;
4ba9b64b 1260 EATBITS(16);
5366aed8 1261 if (dctx->uncomplen == 0)
1262 dctx->state = OUTSIDEBLK; /* block is empty */
7b4fa1b4 1263 else
5366aed8 1264 dctx->state = UNCOMP_DATA;
4ba9b64b 1265 break;
1266 case UNCOMP_DATA:
5366aed8 1267 if (dctx->nbits < 8)
4ba9b64b 1268 goto finished;
5366aed8 1269 zlib_emit_char(dctx, dctx->bits & 0xFF);
4ba9b64b 1270 EATBITS(8);
5366aed8 1271 if (--dctx->uncomplen == 0)
1272 dctx->state = OUTSIDEBLK; /* end of uncompressed block */
4ba9b64b 1273 break;
32874aea 1274 }
4ba9b64b 1275 }
1276
32874aea 1277 finished:
5366aed8 1278 *outblock = dctx->outblk;
1279 *outlen = dctx->outlen;
4ba9b64b 1280 return 1;
36b8d9bb 1281
1282 decode_error:
1283 sfree(dctx->outblk);
1284 *outblock = dctx->outblk = NULL;
1285 *outlen = 0;
1286 return 0;
4ba9b64b 1287}
1288
52912ff3 1289#ifdef ZLIB_STANDALONE
1290
1291#include <stdio.h>
1292#include <string.h>
1293
1294int main(int argc, char **argv)
1295{
1296 unsigned char buf[16], *outbuf;
1297 int ret, outlen;
1298 void *handle;
1299 int noheader = FALSE, opts = TRUE;
1300 char *filename = NULL;
1301 FILE *fp;
1302
1303 while (--argc) {
1304 char *p = *++argv;
1305
1306 if (p[0] == '-' && opts) {
1307 if (!strcmp(p, "-d"))
1308 noheader = TRUE;
1309 else if (!strcmp(p, "--"))
1310 opts = FALSE; /* next thing is filename */
1311 else {
1312 fprintf(stderr, "unknown command line option '%s'\n", p);
1313 return 1;
1314 }
1315 } else if (!filename) {
1316 filename = p;
1317 } else {
1318 fprintf(stderr, "can only handle one filename\n");
1319 return 1;
1320 }
1321 }
1322
1323 handle = zlib_decompress_init();
1324
1325 if (noheader) {
1326 /*
1327 * Provide missing zlib header if -d was specified.
1328 */
1329 zlib_decompress_block(handle, "\x78\x9C", 2, &outbuf, &outlen);
1330 assert(outlen == 0);
1331 }
1332
1333 if (filename)
1334 fp = fopen(filename, "rb");
1335 else
1336 fp = stdin;
1337
1338 if (!fp) {
1339 assert(filename);
1340 fprintf(stderr, "unable to open '%s'\n", filename);
1341 return 1;
1342 }
1343
1344 while (1) {
1345 ret = fread(buf, 1, sizeof(buf), fp);
1346 if (ret <= 0)
1347 break;
1348 zlib_decompress_block(handle, buf, ret, &outbuf, &outlen);
1349 if (outbuf) {
1350 if (outlen)
1351 fwrite(outbuf, 1, outlen, stdout);
1352 sfree(outbuf);
1353 } else {
1354 fprintf(stderr, "decoding error\n");
1355 return 1;
1356 }
1357 }
1358
1359 zlib_decompress_cleanup(handle);
1360
1361 if (filename)
1362 fclose(fp);
1363
1364 return 0;
1365}
1366
1367#else
1368
4ba9b64b 1369const struct ssh_compress ssh_zlib = {
1370 "zlib",
1371 zlib_compress_init,
5366aed8 1372 zlib_compress_cleanup,
4ba9b64b 1373 zlib_compress_block,
1374 zlib_decompress_init,
5366aed8 1375 zlib_decompress_cleanup,
6e9e9520 1376 zlib_decompress_block,
5366aed8 1377 zlib_disable_compression,
1378 "zlib (RFC1950)"
4ba9b64b 1379};
52912ff3 1380
1381#endif