xref: /titanic_50/usr/src/cmd/cmd-inet/usr.bin/pppdump/bsd-comp.c (revision 24db46411fd54f70c35b94bb952eb7ba040e43b4)
1 /* 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 /*
41  * $Id: bsd-comp.c,v 1.3 1999/04/16 11:35:59 paulus Exp $
42  */
43 
44 #include <sys/types.h>
45 #include <stddef.h>
46 #include <stdlib.h>
47 #ifdef PPP_DEFS_IN_NET
48 #include <net/ppp_defs.h>
49 #else
50 #include "ppp_defs.h"
51 #endif
52 #include "ppp-comp.h"
53 
54 #if DO_BSD_COMPRESS
55 
56 /*
57  * PPP "BSD compress" compression
58  *  The differences between this compression and the classic BSD LZW
59  *  source are obvious from the requirement that the classic code worked
60  *  with files while this handles arbitrarily long streams that
61  *  are broken into packets.  They are:
62  *
63  *	When the code size expands, a block of junk is not emitted by
64  *	    the compressor and not expected by the decompressor.
65  *
66  *	New codes are not necessarily assigned every time an old
67  *	    code is output by the compressor.  This is because a packet
68  *	    end forces a code to be emitted, but does not imply that a
69  *	    new sequence has been seen.
70  *
71  *	The compression ratio is checked at the first end of a packet
72  *	    after the appropriate gap.	Besides simplifying and speeding
73  *	    things up, this makes it more likely that the transmitter
74  *	    and receiver will agree when the dictionary is cleared when
75  *	    compression is not going well.
76  */
77 
78 /*
79  * A dictionary for doing BSD compress.
80  */
81 struct bsd_db {
82     int	    totlen;			/* length of this structure */
83     u_int   hsize;			/* size of the hash table */
84     u_char  hshift;			/* used in hash function */
85     u_char  n_bits;			/* current bits/code */
86     u_char  maxbits;
87     u_char  debug;
88     u_char  unit;
89     u_short seqno;			/* sequence number of next packet */
90     u_int   hdrlen;			/* header length to preallocate */
91     u_int   mru;
92     u_int   maxmaxcode;			/* largest valid code */
93     u_int   max_ent;			/* largest code in use */
94     u_int   in_count;			/* uncompressed bytes, aged */
95     u_int   bytes_out;			/* compressed bytes, aged */
96     u_int   ratio;			/* recent compression ratio */
97     u_int   checkpoint;			/* when to next check the ratio */
98     u_int   clear_count;		/* times dictionary cleared */
99     u_int   incomp_count;		/* incompressible packets */
100     u_int   incomp_bytes;		/* incompressible bytes */
101     u_int   uncomp_count;		/* uncompressed packets */
102     u_int   uncomp_bytes;		/* uncompressed bytes */
103     u_int   comp_count;			/* compressed packets */
104     u_int   comp_bytes;			/* compressed bytes */
105     u_short *lens;			/* array of lengths of codes */
106     struct bsd_dict {
107 	union {				/* hash value */
108 	    u_int32_t	fcode;
109 	    struct {
110 #ifdef BSD_LITTLE_ENDIAN
111 		u_short prefix;		/* preceding code */
112 		u_char	suffix;		/* last character of new code */
113 		u_char	pad;
114 #else
115 		u_char	pad;
116 		u_char	suffix;		/* last character of new code */
117 		u_short prefix;		/* preceding code */
118 #endif
119 	    } hs;
120 	} f;
121 	u_short codem1;			/* output of hash table -1 */
122 	u_short cptr;			/* map code to hash table entry */
123     } dict[1];
124 };
125 
126 #define BSD_OVHD	2		/* BSD compress overhead/packet */
127 #define BSD_INIT_BITS	BSD_MIN_BITS
128 
129 static void	*bsd_decomp_alloc __P((u_char *options, int opt_len));
130 static void	bsd_free __P((void *state));
131 static int	bsd_decomp_init __P((void *state, u_char *options, int opt_len,
132 				     int unit, int hdrlen, int mru, int debug));
133 static void	bsd_incomp __P((void *state, u_char *dmsg, int len));
134 static int	bsd_decompress __P((void *state, u_char *cmp, int inlen,
135 				    u_char *dmp, int *outlen));
136 static void	bsd_reset __P((void *state));
137 static void	bsd_comp_stats __P((void *state, struct compstat *stats));
138 
139 /*
140  * Exported procedures.
141  */
142 struct compressor ppp_bsd_compress = {
143     CI_BSD_COMPRESS,		/* compress_proto */
144     bsd_decomp_alloc,		/* decomp_alloc */
145     bsd_free,			/* decomp_free */
146     bsd_decomp_init,		/* decomp_init */
147     bsd_reset,			/* decomp_reset */
148     bsd_decompress,		/* decompress */
149     bsd_incomp,			/* incomp */
150     bsd_comp_stats,		/* decomp_stat */
151 };
152 
153 /*
154  * the next two codes should not be changed lightly, as they must not
155  * lie within the contiguous general code space.
156  */
157 #define CLEAR	256			/* table clear output code */
158 #define FIRST	257			/* first free entry */
159 #define LAST	255
160 
161 #define MAXCODE(b)	((1 << (b)) - 1)
162 #define BADCODEM1	MAXCODE(BSD_MAX_BITS)
163 
164 #define BSD_HASH(prefix,suffix,hshift)	((((u_int32_t)(suffix)) << (hshift)) \
165 					 ^ (u_int32_t)(prefix))
166 #define BSD_KEY(prefix,suffix)		((((u_int32_t)(suffix)) << 16) \
167 					 + (u_int32_t)(prefix))
168 
169 #define CHECK_GAP	10000		/* Ratio check interval */
170 
171 #define RATIO_SCALE_LOG	8
172 #define RATIO_SCALE	(1<<RATIO_SCALE_LOG)
173 #define RATIO_MAX	(0x7fffffff>>RATIO_SCALE_LOG)
174 
175 /*
176  * clear the dictionary
177  */
178 static void
179 bsd_clear(db)
180     struct bsd_db *db;
181 {
182     db->clear_count++;
183     db->max_ent = FIRST-1;
184     db->n_bits = BSD_INIT_BITS;
185     db->ratio = 0;
186     db->bytes_out = 0;
187     db->in_count = 0;
188     db->checkpoint = CHECK_GAP;
189 }
190 
191 /*
192  * If the dictionary is full, then see if it is time to reset it.
193  *
194  * Compute the compression ratio using fixed-point arithmetic
195  * with 8 fractional bits.
196  *
197  * Since we have an infinite stream instead of a single file,
198  * watch only the local compression ratio.
199  *
200  * Since both peers must reset the dictionary at the same time even in
201  * the absence of CLEAR codes (while packets are incompressible), they
202  * must compute the same ratio.
203  */
204 static int				/* 1=output CLEAR */
205 bsd_check(db)
206     struct bsd_db *db;
207 {
208     u_int new_ratio;
209 
210     if (db->in_count >= db->checkpoint) {
211 	/* age the ratio by limiting the size of the counts */
212 	if (db->in_count >= RATIO_MAX
213 	    || db->bytes_out >= RATIO_MAX) {
214 	    db->in_count -= db->in_count/4;
215 	    db->bytes_out -= db->bytes_out/4;
216 	}
217 
218 	db->checkpoint = db->in_count + CHECK_GAP;
219 
220 	if (db->max_ent >= db->maxmaxcode) {
221 	    /* Reset the dictionary only if the ratio is worse,
222 	     * or if it looks as if it has been poisoned
223 	     * by incompressible data.
224 	     *
225 	     * This does not overflow, because
226 	     *	db->in_count <= RATIO_MAX.
227 	     */
228 	    new_ratio = db->in_count << RATIO_SCALE_LOG;
229 	    if (db->bytes_out != 0)
230 		new_ratio /= db->bytes_out;
231 
232 	    if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
233 		bsd_clear(db);
234 		return 1;
235 	    }
236 	    db->ratio = new_ratio;
237 	}
238     }
239     return 0;
240 }
241 
242 /*
243  * Return statistics.
244  */
245 static void
246 bsd_comp_stats(state, stats)
247     void *state;
248     struct compstat *stats;
249 {
250     struct bsd_db *db = (struct bsd_db *) state;
251     u_int out;
252 
253     stats->unc_bytes = db->uncomp_bytes;
254     stats->unc_packets = db->uncomp_count;
255     stats->comp_bytes = db->comp_bytes;
256     stats->comp_packets = db->comp_count;
257     stats->inc_bytes = db->incomp_bytes;
258     stats->inc_packets = db->incomp_count;
259     stats->ratio = db->in_count;
260     out = db->bytes_out;
261     if (stats->ratio <= 0x7fffff)
262 	stats->ratio <<= 8;
263     else
264 	out >>= 8;
265     if (out != 0)
266 	stats->ratio /= out;
267 }
268 
269 /*
270  * Reset state, as on a CCP ResetReq.
271  */
272 static void
273 bsd_reset(state)
274     void *state;
275 {
276     struct bsd_db *db = (struct bsd_db *) state;
277 
278     db->seqno = 0;
279     bsd_clear(db);
280     db->clear_count = 0;
281 }
282 
283 /*
284  * Allocate space for a (de) compressor.
285  */
286 static void *
287 bsd_alloc(options, opt_len, decomp)
288     u_char *options;
289     int opt_len, decomp;
290 {
291     int bits;
292     u_int newlen, hsize, hshift, maxmaxcode;
293     struct bsd_db *db;
294 
295     if (opt_len != 3 || options[0] != CI_BSD_COMPRESS || options[1] != 3
296 	|| BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
297 	return NULL;
298 
299     bits = BSD_NBITS(options[2]);
300     switch (bits) {
301     case 9:			/* needs 82152 for both directions */
302     case 10:			/* needs 84144 */
303     case 11:			/* needs 88240 */
304     case 12:			/* needs 96432 */
305 	hsize = 5003;
306 	hshift = 4;
307 	break;
308     case 13:			/* needs 176784 */
309 	hsize = 9001;
310 	hshift = 5;
311 	break;
312     case 14:			/* needs 353744 */
313 	hsize = 18013;
314 	hshift = 6;
315 	break;
316     case 15:			/* needs 691440 */
317 	hsize = 35023;
318 	hshift = 7;
319 	break;
320     case 16:			/* needs 1366160--far too much, */
321 	/* hsize = 69001; */	/* and 69001 is too big for cptr */
322 	/* hshift = 8; */	/* in struct bsd_db */
323 	/* break; */
324     default:
325 	return NULL;
326     }
327 
328     maxmaxcode = MAXCODE(bits);
329     newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
330     db = (struct bsd_db *) malloc(newlen);
331     if (!db)
332 	return NULL;
333     memset(db, 0, sizeof(*db) - sizeof(db->dict));
334 
335     if (!decomp) {
336 	db->lens = NULL;
337     } else {
338 	db->lens = (u_short *) malloc((maxmaxcode+1) * sizeof(db->lens[0]));
339 	if (!db->lens) {
340 	    free(db);
341 	    return NULL;
342 	}
343     }
344 
345     db->totlen = newlen;
346     db->hsize = hsize;
347     db->hshift = hshift;
348     db->maxmaxcode = maxmaxcode;
349     db->maxbits = bits;
350 
351     return (void *) db;
352 }
353 
354 static void
355 bsd_free(state)
356     void *state;
357 {
358     struct bsd_db *db = (struct bsd_db *) state;
359 
360     if (db->lens)
361 	free(db->lens);
362     free(db);
363 }
364 
365 static void *
366 bsd_decomp_alloc(options, opt_len)
367     u_char *options;
368     int opt_len;
369 {
370     return bsd_alloc(options, opt_len, 1);
371 }
372 
373 /*
374  * Initialize the database.
375  */
376 static int
377 bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp)
378     struct bsd_db *db;
379     u_char *options;
380     int opt_len, unit, hdrlen, mru, debug, decomp;
381 {
382     int i;
383 
384     if (opt_len < CILEN_BSD_COMPRESS
385 	|| options[0] != CI_BSD_COMPRESS || options[1] != CILEN_BSD_COMPRESS
386 	|| BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
387 	|| BSD_NBITS(options[2]) != db->maxbits
388 	|| decomp && db->lens == NULL)
389 	return 0;
390 
391     if (decomp) {
392 	i = LAST+1;
393 	while (i != 0)
394 	    db->lens[--i] = 1;
395     }
396     i = db->hsize;
397     while (i != 0) {
398 	db->dict[--i].codem1 = BADCODEM1;
399 	db->dict[i].cptr = 0;
400     }
401 
402     db->unit = unit;
403     db->hdrlen = hdrlen;
404     db->mru = mru;
405     if (debug)
406 	db->debug = 1;
407 
408     bsd_reset(db);
409 
410     return 1;
411 }
412 
413 static int
414 bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
415     void *state;
416     u_char *options;
417     int opt_len, unit, hdrlen, mru, debug;
418 {
419     return bsd_init((struct bsd_db *) state, options, opt_len,
420 		    unit, hdrlen, mru, debug, 1);
421 }
422 
423 
424 /*
425  * Update the "BSD Compress" dictionary on the receiver for
426  * incompressible data by pretending to compress the incoming data.
427  */
428 static void
429 bsd_incomp(state, dmsg, mlen)
430     void *state;
431     u_char *dmsg;
432     int mlen;
433 {
434     struct bsd_db *db = (struct bsd_db *) state;
435     u_int hshift = db->hshift;
436     u_int max_ent = db->max_ent;
437     u_int n_bits = db->n_bits;
438     struct bsd_dict *dictp;
439     u_int32_t fcode;
440     u_char c;
441     long hval, disp;
442     int slen, ilen;
443     u_int bitno = 7;
444     u_char *rptr;
445     u_int ent;
446 
447     rptr = dmsg;
448     ent = rptr[0];		/* get the protocol */
449     if (ent == 0) {
450 	++rptr;
451 	--mlen;
452 	ent = rptr[0];
453     }
454     if ((ent & 1) == 0 || ent < 0x21 || ent > 0xf9)
455 	return;
456 
457     db->seqno++;
458     ilen = 1;		/* count the protocol as 1 byte */
459     ++rptr;
460     slen = dmsg + mlen - rptr;
461     ilen += slen;
462     for (; slen > 0; --slen) {
463 	c = *rptr++;
464 	fcode = BSD_KEY(ent, c);
465 	hval = BSD_HASH(ent, c, hshift);
466 	dictp = &db->dict[hval];
467 
468 	/* validate and then check the entry */
469 	if (dictp->codem1 >= max_ent)
470 	    goto nomatch;
471 	if (dictp->f.fcode == fcode) {
472 	    ent = dictp->codem1+1;
473 	    continue;   /* found (prefix,suffix) */
474 	}
475 
476 	/* continue probing until a match or invalid entry */
477 	disp = (hval == 0) ? 1 : hval;
478 	do {
479 	    hval += disp;
480 	    if (hval >= db->hsize)
481 		hval -= db->hsize;
482 	    dictp = &db->dict[hval];
483 	    if (dictp->codem1 >= max_ent)
484 		goto nomatch;
485 	} while (dictp->f.fcode != fcode);
486 	ent = dictp->codem1+1;
487 	continue;	/* finally found (prefix,suffix) */
488 
489     nomatch:		/* output (count) the prefix */
490 	bitno += n_bits;
491 
492 	/* code -> hashtable */
493 	if (max_ent < db->maxmaxcode) {
494 	    struct bsd_dict *dictp2;
495 	    /* expand code size if needed */
496 	    if (max_ent >= MAXCODE(n_bits))
497 		db->n_bits = ++n_bits;
498 
499 	    /* Invalidate previous hash table entry
500 	     * assigned this code, and then take it over.
501 	     */
502 	    dictp2 = &db->dict[max_ent+1];
503 	    if (db->dict[dictp2->cptr].codem1 == max_ent)
504 		db->dict[dictp2->cptr].codem1 = BADCODEM1;
505 	    dictp2->cptr = hval;
506 	    dictp->codem1 = max_ent;
507 	    dictp->f.fcode = fcode;
508 
509 	    db->max_ent = ++max_ent;
510 	    db->lens[max_ent] = db->lens[ent]+1;
511 	}
512 	ent = c;
513     }
514     bitno += n_bits;		/* output (count) the last code */
515     db->bytes_out += bitno/8;
516     db->in_count += ilen;
517     (void)bsd_check(db);
518 
519     ++db->incomp_count;
520     db->incomp_bytes += ilen;
521     ++db->uncomp_count;
522     db->uncomp_bytes += ilen;
523 
524     /* Increase code size if we would have without the packet
525      * boundary and as the decompressor will.
526      */
527     if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
528 	db->n_bits++;
529 }
530 
531 
532 /*
533  * Decompress "BSD Compress"
534  *
535  * Because of patent problems, we return DECOMP_ERROR for errors
536  * found by inspecting the input data and for system problems, but
537  * DECOMP_FATALERROR for any errors which could possibly be said to
538  * be being detected "after" decompression.  For DECOMP_ERROR,
539  * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
540  * infringing a patent of Motorola's if we do, so we take CCP down
541  * instead.
542  *
543  * Given that the frame has the correct sequence number and a good FCS,
544  * errors such as invalid codes in the input most likely indicate a
545  * bug, so we return DECOMP_FATALERROR for them in order to turn off
546  * compression, even though they are detected by inspecting the input.
547  */
548 static int
549 bsd_decompress(state, cmsg, inlen, dmp, outlenp)
550     void *state;
551     u_char *cmsg, *dmp;
552     int inlen, *outlenp;
553 {
554     struct bsd_db *db = (struct bsd_db *) state;
555     u_int max_ent = db->max_ent;
556     u_int32_t accm = 0;
557     u_int bitno = 32;		/* 1st valid bit in accm */
558     u_int n_bits = db->n_bits;
559     u_int tgtbitno = 32-n_bits;	/* bitno when we have a code */
560     struct bsd_dict *dictp;
561     int explen, i, seq, len;
562     u_int incode, oldcode, finchar;
563     u_char *p, *rptr, *wptr;
564     int ilen;
565     int dlen, space, codelen, extra;
566 
567     rptr = cmsg;
568     if (*rptr == 0)
569 	++rptr;
570     ++rptr;			/* skip protocol (assumed 0xfd) */
571     seq = (rptr[0] << 8) + rptr[1];
572     rptr += BSD_OVHD;
573     ilen = len = cmsg + inlen - rptr;
574 
575     /*
576      * Check the sequence number and give up if it is not what we expect.
577      */
578     if (seq != db->seqno++) {
579 	if (db->debug)
580 	    printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
581 		   db->unit, seq, db->seqno - 1);
582 	return DECOMP_ERROR;
583     }
584 
585     wptr = dmp + db->hdrlen;
586 
587     oldcode = CLEAR;
588     explen = 0;
589     while (len > 0) {
590 	/*
591 	 * Accumulate bytes until we have a complete code.
592 	 * Then get the next code, relying on the 32-bit,
593 	 * unsigned accm to mask the result.
594 	 */
595 	bitno -= 8;
596 	accm |= *rptr++ << bitno;
597 	--len;
598 	if (tgtbitno < bitno)
599 	    continue;
600 	incode = accm >> tgtbitno;
601 	accm <<= n_bits;
602 	bitno += n_bits;
603 
604 	if (incode == CLEAR) {
605 	    /*
606 	     * The dictionary must only be cleared at
607 	     * the end of a packet.  But there could be an
608 	     * empty message block at the end.
609 	     */
610 	    if (len > 0) {
611 		if (db->debug)
612 		    printf("bsd_decomp%d: bad CLEAR\n", db->unit);
613 		return DECOMP_FATALERROR;
614 	    }
615 	    bsd_clear(db);
616 	    explen = ilen = 0;
617 	    break;
618 	}
619 
620 	if (incode > max_ent + 2 || incode > db->maxmaxcode
621 	    || incode > max_ent && oldcode == CLEAR) {
622 	    if (db->debug) {
623 		printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
624 		       db->unit, incode, oldcode);
625 		printf("max_ent=0x%x dlen=%d seqno=%d\n",
626 		       max_ent, dlen, db->seqno);
627 	    }
628 	    return DECOMP_FATALERROR;	/* probably a bug */
629 	}
630 
631 	/* Special case for KwKwK string. */
632 	if (incode > max_ent) {
633 	    finchar = oldcode;
634 	    extra = 1;
635 	} else {
636 	    finchar = incode;
637 	    extra = 0;
638 	}
639 
640 	codelen = db->lens[finchar];
641 	explen += codelen + extra;
642 	if (explen > db->mru + 1) {
643 	    if (db->debug)
644 		printf("bsd_decomp%d: ran out of mru\n", db->unit);
645 	    return DECOMP_FATALERROR;
646 	}
647 
648 	/*
649 	 * Decode this code and install it in the decompressed buffer.
650 	 */
651 	p = (wptr += codelen);
652 	while (finchar > LAST) {
653 	    dictp = &db->dict[db->dict[finchar].cptr];
654 #ifdef DEBUG
655 	    --codelen;
656 	    if (codelen <= 0) {
657 		printf("bsd_decomp%d: fell off end of chain ", db->unit);
658 		printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
659 		       incode, finchar, db->dict[finchar].cptr, max_ent);
660 		return DECOMP_FATALERROR;
661 	    }
662 	    if (dictp->codem1 != finchar-1) {
663 		printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
664 		       db->unit, incode, finchar);
665 		printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
666 		       db->dict[finchar].cptr, dictp->codem1);
667 		return DECOMP_FATALERROR;
668 	    }
669 #endif
670 	    *--p = dictp->f.hs.suffix;
671 	    finchar = dictp->f.hs.prefix;
672 	}
673 	*--p = finchar;
674 
675 #ifdef DEBUG
676 	if (--codelen != 0)
677 	    printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
678 		   db->unit, codelen, incode, max_ent);
679 #endif
680 
681 	if (extra)		/* the KwKwK case again */
682 	    *wptr++ = finchar;
683 
684 	/*
685 	 * If not first code in a packet, and
686 	 * if not out of code space, then allocate a new code.
687 	 *
688 	 * Keep the hash table correct so it can be used
689 	 * with uncompressed packets.
690 	 */
691 	if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
692 	    struct bsd_dict *dictp2;
693 	    u_int32_t fcode;
694 	    int hval, disp;
695 
696 	    fcode = BSD_KEY(oldcode,finchar);
697 	    hval = BSD_HASH(oldcode,finchar,db->hshift);
698 	    dictp = &db->dict[hval];
699 
700 	    /* look for a free hash table entry */
701 	    if (dictp->codem1 < max_ent) {
702 		disp = (hval == 0) ? 1 : hval;
703 		do {
704 		    hval += disp;
705 		    if (hval >= db->hsize)
706 			hval -= db->hsize;
707 		    dictp = &db->dict[hval];
708 		} while (dictp->codem1 < max_ent);
709 	    }
710 
711 	    /*
712 	     * Invalidate previous hash table entry
713 	     * assigned this code, and then take it over
714 	     */
715 	    dictp2 = &db->dict[max_ent+1];
716 	    if (db->dict[dictp2->cptr].codem1 == max_ent) {
717 		db->dict[dictp2->cptr].codem1 = BADCODEM1;
718 	    }
719 	    dictp2->cptr = hval;
720 	    dictp->codem1 = max_ent;
721 	    dictp->f.fcode = fcode;
722 
723 	    db->max_ent = ++max_ent;
724 	    db->lens[max_ent] = db->lens[oldcode]+1;
725 
726 	    /* Expand code size if needed. */
727 	    if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
728 		db->n_bits = ++n_bits;
729 		tgtbitno = 32-n_bits;
730 	    }
731 	}
732 	oldcode = incode;
733     }
734     *outlenp = wptr - (dmp + db->hdrlen);
735 
736     /*
737      * Keep the checkpoint right so that incompressible packets
738      * clear the dictionary at the right times.
739      */
740     db->bytes_out += ilen;
741     db->in_count += explen;
742     if (bsd_check(db) && db->debug) {
743 	printf("bsd_decomp%d: peer should have cleared dictionary\n",
744 	       db->unit);
745     }
746 
747     ++db->comp_count;
748     db->comp_bytes += ilen + BSD_OVHD;
749     ++db->uncomp_count;
750     db->uncomp_bytes += explen;
751 
752     return DECOMP_OK;
753 }
754 #endif /* DO_BSD_COMPRESS */
755