blob: 57f936c5ed0004650f43a928eaf98f66aaa00041 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Because this code is derived from the 4.3BSD compress source:
2 *
3 *
4 * Copyright (c) 1985, 1986 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * James A. Woods, derived from original work by Spencer Thomas
9 * and Joseph Orost.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40/*
xf.li84027492024-04-09 00:17:51 -070041 * $Id: bsd-comp.c,v 1.4 2004/01/17 05:47:55 carlsonj Exp $
lh9ed821d2023-04-07 01:36:19 -070042 */
43
44#include <sys/types.h>
45#include <stdio.h>
46#include <stddef.h>
47#include <stdlib.h>
48#include <string.h>
xf.li84027492024-04-09 00:17:51 -070049
lh9ed821d2023-04-07 01:36:19 -070050#include "ppp-comp.h"
51
52#if DO_BSD_COMPRESS
53
54/*
55 * PPP "BSD compress" compression
56 * The differences between this compression and the classic BSD LZW
57 * source are obvious from the requirement that the classic code worked
58 * with files while this handles arbitrarily long streams that
59 * are broken into packets. They are:
60 *
61 * When the code size expands, a block of junk is not emitted by
62 * the compressor and not expected by the decompressor.
63 *
64 * New codes are not necessarily assigned every time an old
65 * code is output by the compressor. This is because a packet
66 * end forces a code to be emitted, but does not imply that a
67 * new sequence has been seen.
68 *
69 * The compression ratio is checked at the first end of a packet
70 * after the appropriate gap. Besides simplifying and speeding
71 * things up, this makes it more likely that the transmitter
72 * and receiver will agree when the dictionary is cleared when
73 * compression is not going well.
74 */
75
76/*
77 * A dictionary for doing BSD compress.
78 */
79struct bsd_db {
80 int totlen; /* length of this structure */
81 u_int hsize; /* size of the hash table */
82 u_char hshift; /* used in hash function */
83 u_char n_bits; /* current bits/code */
84 u_char maxbits;
85 u_char debug;
86 u_char unit;
87 u_short seqno; /* sequence number of next packet */
88 u_int hdrlen; /* header length to preallocate */
89 u_int mru;
90 u_int maxmaxcode; /* largest valid code */
91 u_int max_ent; /* largest code in use */
92 u_int in_count; /* uncompressed bytes, aged */
93 u_int bytes_out; /* compressed bytes, aged */
94 u_int ratio; /* recent compression ratio */
95 u_int checkpoint; /* when to next check the ratio */
96 u_int clear_count; /* times dictionary cleared */
97 u_int incomp_count; /* incompressible packets */
98 u_int incomp_bytes; /* incompressible bytes */
99 u_int uncomp_count; /* uncompressed packets */
100 u_int uncomp_bytes; /* uncompressed bytes */
101 u_int comp_count; /* compressed packets */
102 u_int comp_bytes; /* compressed bytes */
103 u_short *lens; /* array of lengths of codes */
104 struct bsd_dict {
105 union { /* hash value */
106 u_int32_t fcode;
107 struct {
108#ifdef BSD_LITTLE_ENDIAN
109 u_short prefix; /* preceding code */
110 u_char suffix; /* last character of new code */
111 u_char pad;
112#else
113 u_char pad;
114 u_char suffix; /* last character of new code */
115 u_short prefix; /* preceding code */
116#endif
117 } hs;
118 } f;
119 u_short codem1; /* output of hash table -1 */
120 u_short cptr; /* map code to hash table entry */
121 } dict[1];
122};
123
124#define BSD_OVHD 2 /* BSD compress overhead/packet */
125#define BSD_INIT_BITS BSD_MIN_BITS
126
xf.li84027492024-04-09 00:17:51 -0700127static void *bsd_decomp_alloc(u_char *options, int opt_len);
128static void bsd_free(void *state);
129static int bsd_decomp_init(void *state, u_char *options, int opt_len,
130 int unit, int hdrlen, int mru, int debug);
131static void bsd_incomp(void *state, u_char *dmsg, int len);
132static int bsd_decompress(void *state, u_char *cmp, int inlen,
133 u_char *dmp, int *outlen);
134static void bsd_reset(void *state);
135static void bsd_comp_stats(void *state, struct compstat *stats);
lh9ed821d2023-04-07 01:36:19 -0700136
137/*
138 * Exported procedures.
139 */
140struct compressor ppp_bsd_compress = {
141 CI_BSD_COMPRESS, /* compress_proto */
142 bsd_decomp_alloc, /* decomp_alloc */
143 bsd_free, /* decomp_free */
144 bsd_decomp_init, /* decomp_init */
145 bsd_reset, /* decomp_reset */
146 bsd_decompress, /* decompress */
147 bsd_incomp, /* incomp */
148 bsd_comp_stats, /* decomp_stat */
149};
150
151/*
152 * the next two codes should not be changed lightly, as they must not
153 * lie within the contiguous general code space.
154 */
155#define CLEAR 256 /* table clear output code */
156#define FIRST 257 /* first free entry */
157#define LAST 255
158
159#define MAXCODE(b) ((1 << (b)) - 1)
160#define BADCODEM1 MAXCODE(BSD_MAX_BITS)
161
162#define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
163 ^ (u_int32_t)(prefix))
164#define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
165 + (u_int32_t)(prefix))
166
167#define CHECK_GAP 10000 /* Ratio check interval */
168
169#define RATIO_SCALE_LOG 8
170#define RATIO_SCALE (1<<RATIO_SCALE_LOG)
171#define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
172
173/*
174 * clear the dictionary
175 */
176static void
xf.li84027492024-04-09 00:17:51 -0700177bsd_clear(struct bsd_db *db)
lh9ed821d2023-04-07 01:36:19 -0700178{
179 db->clear_count++;
180 db->max_ent = FIRST-1;
181 db->n_bits = BSD_INIT_BITS;
182 db->ratio = 0;
183 db->bytes_out = 0;
184 db->in_count = 0;
185 db->checkpoint = CHECK_GAP;
186}
187
188/*
189 * If the dictionary is full, then see if it is time to reset it.
190 *
191 * Compute the compression ratio using fixed-point arithmetic
192 * with 8 fractional bits.
193 *
194 * Since we have an infinite stream instead of a single file,
195 * watch only the local compression ratio.
196 *
197 * Since both peers must reset the dictionary at the same time even in
198 * the absence of CLEAR codes (while packets are incompressible), they
199 * must compute the same ratio.
200 */
201static int /* 1=output CLEAR */
xf.li84027492024-04-09 00:17:51 -0700202bsd_check(struct bsd_db *db)
lh9ed821d2023-04-07 01:36:19 -0700203{
204 u_int new_ratio;
205
206 if (db->in_count >= db->checkpoint) {
207 /* age the ratio by limiting the size of the counts */
208 if (db->in_count >= RATIO_MAX
209 || db->bytes_out >= RATIO_MAX) {
210 db->in_count -= db->in_count/4;
211 db->bytes_out -= db->bytes_out/4;
212 }
213
214 db->checkpoint = db->in_count + CHECK_GAP;
215
216 if (db->max_ent >= db->maxmaxcode) {
217 /* Reset the dictionary only if the ratio is worse,
218 * or if it looks as if it has been poisoned
219 * by incompressible data.
220 *
221 * This does not overflow, because
222 * db->in_count <= RATIO_MAX.
223 */
224 new_ratio = db->in_count << RATIO_SCALE_LOG;
225 if (db->bytes_out != 0)
226 new_ratio /= db->bytes_out;
227
228 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
229 bsd_clear(db);
230 return 1;
231 }
232 db->ratio = new_ratio;
233 }
234 }
235 return 0;
236}
237
238/*
239 * Return statistics.
240 */
241static void
xf.li84027492024-04-09 00:17:51 -0700242bsd_comp_stats(void *state, struct compstat *stats)
lh9ed821d2023-04-07 01:36:19 -0700243{
244 struct bsd_db *db = (struct bsd_db *) state;
245 u_int out;
246
247 stats->unc_bytes = db->uncomp_bytes;
248 stats->unc_packets = db->uncomp_count;
249 stats->comp_bytes = db->comp_bytes;
250 stats->comp_packets = db->comp_count;
251 stats->inc_bytes = db->incomp_bytes;
252 stats->inc_packets = db->incomp_count;
xf.li84027492024-04-09 00:17:51 -0700253
254 u_int ratio = db->in_count;
lh9ed821d2023-04-07 01:36:19 -0700255 out = db->bytes_out;
xf.li84027492024-04-09 00:17:51 -0700256 if (ratio <= 0x7fffff)
257 ratio <<= 8;
lh9ed821d2023-04-07 01:36:19 -0700258 else
259 out >>= 8;
260 if (out != 0)
xf.li84027492024-04-09 00:17:51 -0700261 stats->ratio = ratio / out;
lh9ed821d2023-04-07 01:36:19 -0700262}
263
264/*
265 * Reset state, as on a CCP ResetReq.
266 */
267static void
xf.li84027492024-04-09 00:17:51 -0700268bsd_reset(void *state)
lh9ed821d2023-04-07 01:36:19 -0700269{
270 struct bsd_db *db = (struct bsd_db *) state;
271
272 db->seqno = 0;
273 bsd_clear(db);
274 db->clear_count = 0;
275}
276
277/*
278 * Allocate space for a (de) compressor.
279 */
280static void *
xf.li84027492024-04-09 00:17:51 -0700281bsd_alloc(u_char *options, int opt_len, int decomp)
lh9ed821d2023-04-07 01:36:19 -0700282{
283 int bits;
284 u_int newlen, hsize, hshift, maxmaxcode;
285 struct bsd_db *db;
286
287 if (opt_len != 3 || options[0] != CI_BSD_COMPRESS || options[1] != 3
288 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
289 return NULL;
290
291 bits = BSD_NBITS(options[2]);
292 switch (bits) {
293 case 9: /* needs 82152 for both directions */
294 case 10: /* needs 84144 */
295 case 11: /* needs 88240 */
296 case 12: /* needs 96432 */
297 hsize = 5003;
298 hshift = 4;
299 break;
300 case 13: /* needs 176784 */
301 hsize = 9001;
302 hshift = 5;
303 break;
304 case 14: /* needs 353744 */
305 hsize = 18013;
306 hshift = 6;
307 break;
308 case 15: /* needs 691440 */
309 hsize = 35023;
310 hshift = 7;
311 break;
312 case 16: /* needs 1366160--far too much, */
313 /* hsize = 69001; */ /* and 69001 is too big for cptr */
314 /* hshift = 8; */ /* in struct bsd_db */
315 /* break; */
316 default:
317 return NULL;
318 }
319
320 maxmaxcode = MAXCODE(bits);
321 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
322 db = (struct bsd_db *) malloc(newlen);
323 if (!db)
324 return NULL;
325 memset(db, 0, sizeof(*db) - sizeof(db->dict));
326
327 if (!decomp) {
328 db->lens = NULL;
329 } else {
330 db->lens = (u_short *) malloc((maxmaxcode+1) * sizeof(db->lens[0]));
331 if (!db->lens) {
332 free(db);
333 return NULL;
334 }
335 }
336
337 db->totlen = newlen;
338 db->hsize = hsize;
339 db->hshift = hshift;
340 db->maxmaxcode = maxmaxcode;
341 db->maxbits = bits;
342
343 return (void *) db;
344}
345
346static void
xf.li84027492024-04-09 00:17:51 -0700347bsd_free(void *state)
lh9ed821d2023-04-07 01:36:19 -0700348{
349 struct bsd_db *db = (struct bsd_db *) state;
350
351 if (db->lens)
352 free(db->lens);
353 free(db);
354}
355
356static void *
xf.li84027492024-04-09 00:17:51 -0700357bsd_decomp_alloc(u_char *options, int opt_len)
lh9ed821d2023-04-07 01:36:19 -0700358{
359 return bsd_alloc(options, opt_len, 1);
360}
361
362/*
363 * Initialize the database.
364 */
365static int
xf.li84027492024-04-09 00:17:51 -0700366bsd_init(struct bsd_db *db, u_char *options, int opt_len, int unit,
367 int hdrlen, int mru, int debug, int decomp)
lh9ed821d2023-04-07 01:36:19 -0700368{
369 int i;
370
371 if (opt_len < CILEN_BSD_COMPRESS
372 || options[0] != CI_BSD_COMPRESS || options[1] != CILEN_BSD_COMPRESS
373 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
374 || BSD_NBITS(options[2]) != db->maxbits
xf.li84027492024-04-09 00:17:51 -0700375 || (decomp && db->lens == NULL))
lh9ed821d2023-04-07 01:36:19 -0700376 return 0;
377
378 if (decomp) {
379 i = LAST+1;
380 while (i != 0)
381 db->lens[--i] = 1;
382 }
383 i = db->hsize;
384 while (i != 0) {
385 db->dict[--i].codem1 = BADCODEM1;
386 db->dict[i].cptr = 0;
387 }
388
389 db->unit = unit;
390 db->hdrlen = hdrlen;
391 db->mru = mru;
392 if (debug)
393 db->debug = 1;
394
395 bsd_reset(db);
396
397 return 1;
398}
399
400static int
xf.li84027492024-04-09 00:17:51 -0700401bsd_decomp_init(void *state, u_char *options, int opt_len,
402 int unit, int hdrlen, int mru, int debug)
lh9ed821d2023-04-07 01:36:19 -0700403{
404 return bsd_init((struct bsd_db *) state, options, opt_len,
405 unit, hdrlen, mru, debug, 1);
406}
407
408
409/*
410 * Update the "BSD Compress" dictionary on the receiver for
411 * incompressible data by pretending to compress the incoming data.
412 */
413static void
xf.li84027492024-04-09 00:17:51 -0700414bsd_incomp(void *state, u_char *dmsg, int mlen)
lh9ed821d2023-04-07 01:36:19 -0700415{
416 struct bsd_db *db = (struct bsd_db *) state;
417 u_int hshift = db->hshift;
418 u_int max_ent = db->max_ent;
419 u_int n_bits = db->n_bits;
420 struct bsd_dict *dictp;
421 u_int32_t fcode;
422 u_char c;
423 long hval, disp;
424 int slen, ilen;
425 u_int bitno = 7;
426 u_char *rptr;
427 u_int ent;
428
429 rptr = dmsg;
430 ent = rptr[0]; /* get the protocol */
431 if (ent == 0) {
432 ++rptr;
433 --mlen;
434 ent = rptr[0];
435 }
436 if ((ent & 1) == 0 || ent < 0x21 || ent > 0xf9)
437 return;
438
439 db->seqno++;
440 ilen = 1; /* count the protocol as 1 byte */
441 ++rptr;
442 slen = dmsg + mlen - rptr;
443 ilen += slen;
444 for (; slen > 0; --slen) {
445 c = *rptr++;
446 fcode = BSD_KEY(ent, c);
447 hval = BSD_HASH(ent, c, hshift);
448 dictp = &db->dict[hval];
449
450 /* validate and then check the entry */
451 if (dictp->codem1 >= max_ent)
452 goto nomatch;
453 if (dictp->f.fcode == fcode) {
454 ent = dictp->codem1+1;
455 continue; /* found (prefix,suffix) */
456 }
457
458 /* continue probing until a match or invalid entry */
459 disp = (hval == 0) ? 1 : hval;
460 do {
461 hval += disp;
462 if (hval >= db->hsize)
463 hval -= db->hsize;
464 dictp = &db->dict[hval];
465 if (dictp->codem1 >= max_ent)
466 goto nomatch;
467 } while (dictp->f.fcode != fcode);
468 ent = dictp->codem1+1;
469 continue; /* finally found (prefix,suffix) */
470
471 nomatch: /* output (count) the prefix */
472 bitno += n_bits;
473
474 /* code -> hashtable */
475 if (max_ent < db->maxmaxcode) {
476 struct bsd_dict *dictp2;
477 /* expand code size if needed */
478 if (max_ent >= MAXCODE(n_bits))
479 db->n_bits = ++n_bits;
480
481 /* Invalidate previous hash table entry
482 * assigned this code, and then take it over.
483 */
484 dictp2 = &db->dict[max_ent+1];
485 if (db->dict[dictp2->cptr].codem1 == max_ent)
486 db->dict[dictp2->cptr].codem1 = BADCODEM1;
487 dictp2->cptr = hval;
488 dictp->codem1 = max_ent;
489 dictp->f.fcode = fcode;
490
491 db->max_ent = ++max_ent;
492 db->lens[max_ent] = db->lens[ent]+1;
493 }
494 ent = c;
495 }
496 bitno += n_bits; /* output (count) the last code */
497 db->bytes_out += bitno/8;
498 db->in_count += ilen;
499 (void)bsd_check(db);
500
501 ++db->incomp_count;
502 db->incomp_bytes += ilen;
503 ++db->uncomp_count;
504 db->uncomp_bytes += ilen;
505
506 /* Increase code size if we would have without the packet
507 * boundary and as the decompressor will.
508 */
509 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
510 db->n_bits++;
511}
512
513
514/*
515 * Decompress "BSD Compress"
516 *
517 * Because of patent problems, we return DECOMP_ERROR for errors
518 * found by inspecting the input data and for system problems, but
519 * DECOMP_FATALERROR for any errors which could possibly be said to
520 * be being detected "after" decompression. For DECOMP_ERROR,
521 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
522 * infringing a patent of Motorola's if we do, so we take CCP down
523 * instead.
524 *
525 * Given that the frame has the correct sequence number and a good FCS,
526 * errors such as invalid codes in the input most likely indicate a
527 * bug, so we return DECOMP_FATALERROR for them in order to turn off
528 * compression, even though they are detected by inspecting the input.
529 */
530static int
xf.li84027492024-04-09 00:17:51 -0700531bsd_decompress(void *state, u_char *cmsg, int inlen, u_char *dmp, int *outlenp)
lh9ed821d2023-04-07 01:36:19 -0700532{
533 struct bsd_db *db = (struct bsd_db *) state;
534 u_int max_ent = db->max_ent;
535 u_int32_t accm = 0;
536 u_int bitno = 32; /* 1st valid bit in accm */
537 u_int n_bits = db->n_bits;
538 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
539 struct bsd_dict *dictp;
xf.li84027492024-04-09 00:17:51 -0700540 int explen, seq, len;
lh9ed821d2023-04-07 01:36:19 -0700541 u_int incode, oldcode, finchar;
542 u_char *p, *rptr, *wptr;
543 int ilen;
xf.li84027492024-04-09 00:17:51 -0700544 int codelen, extra;
lh9ed821d2023-04-07 01:36:19 -0700545
546 rptr = cmsg;
547 if (*rptr == 0)
548 ++rptr;
549 ++rptr; /* skip protocol (assumed 0xfd) */
550 seq = (rptr[0] << 8) + rptr[1];
551 rptr += BSD_OVHD;
552 ilen = len = cmsg + inlen - rptr;
553
554 /*
555 * Check the sequence number and give up if it is not what we expect.
556 */
557 if (seq != db->seqno++) {
558 if (db->debug)
559 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
560 db->unit, seq, db->seqno - 1);
561 return DECOMP_ERROR;
562 }
563
564 wptr = dmp + db->hdrlen;
565
566 oldcode = CLEAR;
567 explen = 0;
568 while (len > 0) {
569 /*
570 * Accumulate bytes until we have a complete code.
571 * Then get the next code, relying on the 32-bit,
572 * unsigned accm to mask the result.
573 */
574 bitno -= 8;
575 accm |= *rptr++ << bitno;
576 --len;
577 if (tgtbitno < bitno)
578 continue;
579 incode = accm >> tgtbitno;
580 accm <<= n_bits;
581 bitno += n_bits;
582
583 if (incode == CLEAR) {
584 /*
585 * The dictionary must only be cleared at
586 * the end of a packet. But there could be an
587 * empty message block at the end.
588 */
589 if (len > 0) {
590 if (db->debug)
591 printf("bsd_decomp%d: bad CLEAR\n", db->unit);
592 return DECOMP_FATALERROR;
593 }
594 bsd_clear(db);
595 explen = ilen = 0;
596 break;
597 }
598
599 if (incode > max_ent + 2 || incode > db->maxmaxcode
xf.li84027492024-04-09 00:17:51 -0700600 || (incode > max_ent && oldcode == CLEAR)) {
lh9ed821d2023-04-07 01:36:19 -0700601 if (db->debug) {
602 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
603 db->unit, incode, oldcode);
xf.li84027492024-04-09 00:17:51 -0700604 printf("max_ent=0x%x seqno=%d\n",
605 max_ent, db->seqno);
lh9ed821d2023-04-07 01:36:19 -0700606 }
607 return DECOMP_FATALERROR; /* probably a bug */
608 }
609
610 /* Special case for KwKwK string. */
611 if (incode > max_ent) {
612 finchar = oldcode;
613 extra = 1;
614 } else {
615 finchar = incode;
616 extra = 0;
617 }
618
619 codelen = db->lens[finchar];
620 explen += codelen + extra;
621 if (explen > db->mru + 1) {
622 if (db->debug)
623 printf("bsd_decomp%d: ran out of mru\n", db->unit);
624 return DECOMP_FATALERROR;
625 }
626
627 /*
628 * Decode this code and install it in the decompressed buffer.
629 */
630 p = (wptr += codelen);
631 while (finchar > LAST) {
632 dictp = &db->dict[db->dict[finchar].cptr];
633#ifdef DEBUG
634 --codelen;
635 if (codelen <= 0) {
636 printf("bsd_decomp%d: fell off end of chain ", db->unit);
637 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
638 incode, finchar, db->dict[finchar].cptr, max_ent);
639 return DECOMP_FATALERROR;
640 }
641 if (dictp->codem1 != finchar-1) {
642 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
643 db->unit, incode, finchar);
644 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
645 db->dict[finchar].cptr, dictp->codem1);
646 return DECOMP_FATALERROR;
647 }
648#endif
649 *--p = dictp->f.hs.suffix;
650 finchar = dictp->f.hs.prefix;
651 }
652 *--p = finchar;
653
654#ifdef DEBUG
655 if (--codelen != 0)
656 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
657 db->unit, codelen, incode, max_ent);
658#endif
659
660 if (extra) /* the KwKwK case again */
661 *wptr++ = finchar;
662
663 /*
664 * If not first code in a packet, and
665 * if not out of code space, then allocate a new code.
666 *
667 * Keep the hash table correct so it can be used
668 * with uncompressed packets.
669 */
670 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
671 struct bsd_dict *dictp2;
672 u_int32_t fcode;
673 int hval, disp;
674
675 fcode = BSD_KEY(oldcode,finchar);
676 hval = BSD_HASH(oldcode,finchar,db->hshift);
677 dictp = &db->dict[hval];
678
679 /* look for a free hash table entry */
680 if (dictp->codem1 < max_ent) {
681 disp = (hval == 0) ? 1 : hval;
682 do {
683 hval += disp;
684 if (hval >= db->hsize)
685 hval -= db->hsize;
686 dictp = &db->dict[hval];
687 } while (dictp->codem1 < max_ent);
688 }
689
690 /*
691 * Invalidate previous hash table entry
692 * assigned this code, and then take it over
693 */
694 dictp2 = &db->dict[max_ent+1];
695 if (db->dict[dictp2->cptr].codem1 == max_ent) {
696 db->dict[dictp2->cptr].codem1 = BADCODEM1;
697 }
698 dictp2->cptr = hval;
699 dictp->codem1 = max_ent;
700 dictp->f.fcode = fcode;
701
702 db->max_ent = ++max_ent;
703 db->lens[max_ent] = db->lens[oldcode]+1;
704
705 /* Expand code size if needed. */
706 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
707 db->n_bits = ++n_bits;
708 tgtbitno = 32-n_bits;
709 }
710 }
711 oldcode = incode;
712 }
713 *outlenp = wptr - (dmp + db->hdrlen);
714
715 /*
716 * Keep the checkpoint right so that incompressible packets
717 * clear the dictionary at the right times.
718 */
719 db->bytes_out += ilen;
720 db->in_count += explen;
721 if (bsd_check(db) && db->debug) {
722 printf("bsd_decomp%d: peer should have cleared dictionary\n",
723 db->unit);
724 }
725
726 ++db->comp_count;
727 db->comp_bytes += ilen + BSD_OVHD;
728 ++db->uncomp_count;
729 db->uncomp_bytes += explen;
730
731 return DECOMP_OK;
732}
733#endif /* DO_BSD_COMPRESS */