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