1 /* Because this code is derived from the 4.3BSD compress source:
4 * Copyright (c) 1985, 1986 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
8 * James A. Woods, derived from original work by Spencer Thomas
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
41 * This version is for use with STREAMS under SunOS 4.x.
43 * $Id: bsd-comp.c,v 1.5 1994/10/21 06:30:15 paulus Exp $
46 #include <sys/param.h>
47 #include <sys/types.h>
48 #include <sys/stream.h>
49 #include <sys/kmem_alloc.h>
50 #include <sys/socket.h>
52 #include <net/ppp_defs.h>
53 #include <net/ppp_str.h>
55 #define PACKETPTR mblk_t *
56 #include <net/ppp-comp.h>
59 * PPP "BSD compress" compression
60 * The differences between this compression and the classic BSD LZW
61 * source are obvious from the requirement that the classic code worked
62 * with files while this handles arbitrarily long streams that
63 * are broken into packets. They are:
65 * When the code size expands, a block of junk is not emitted by
66 * the compressor and not expected by the decompressor.
68 * New codes are not necessarily assigned every time an old
69 * code is output by the compressor. This is because a packet
70 * end forces a code to be emitted, but does not imply that a
71 * new sequence has been seen.
73 * The compression ratio is checked at the first end of a packet
74 * after the appropriate gap. Besides simplifying and speeding
75 * things up, this makes it more likely that the transmitter
76 * and receiver will agree when the dictionary is cleared when
77 * compression is not going well.
81 * Macros to extract protocol version and number of bits
82 * from the third byte of the BSD Compress CCP configuration option.
84 #define BSD_VERSION(x) ((x) >> 5)
85 #define BSD_NBITS(x) ((x) & 0x1F)
87 #define BSD_CURRENT_VERSION 1
90 * A dictionary for doing BSD compress.
93 int totlen; /* length of this structure */
94 u_int hsize; /* size of the hash table */
95 u_char hshift; /* used in hash function */
96 u_char n_bits; /* current bits/code */
101 u_short seqno; /* sequence number of next packet */
102 u_int maxmaxcode; /* largest valid code */
103 u_int max_ent; /* largest code in use */
104 u_int in_count; /* uncompressed bytes, aged */
105 u_int bytes_out; /* compressed bytes, aged */
106 u_int ratio; /* recent compression ratio */
107 u_int checkpoint; /* when to next check the ratio */
108 u_int clear_count; /* times dictionary cleared */
109 u_int incomp_count; /* incompressible packets */
110 u_int incomp_bytes; /* incompressible bytes */
111 u_int uncomp_count; /* uncompressed packets */
112 u_int uncomp_bytes; /* uncompressed bytes */
113 u_int comp_count; /* compressed packets */
114 u_int comp_bytes; /* compressed bytes */
115 u_short *lens; /* array of lengths of codes */
117 union { /* hash value */
120 #ifdef BSD_LITTLE_ENDIAN
121 u_short prefix; /* preceding code */
122 u_char suffix; /* last character of new code */
126 u_char suffix; /* last character of new code */
127 u_short prefix; /* preceding code */
131 u_short codem1; /* output of hash table -1 */
132 u_short cptr; /* map code to hash table entry */
136 #define BSD_OVHD 2 /* BSD compress overhead/packet */
137 #define MIN_BSD_BITS 9
138 #define BSD_INIT_BITS MIN_BSD_BITS
139 #define MAX_BSD_BITS 15
141 static void *bsd_comp_alloc __P((u_char *options, int opt_len));
142 static void *bsd_decomp_alloc __P((u_char *options, int opt_len));
143 static void bsd_free __P((void *state));
144 static int bsd_comp_init __P((void *state, u_char *options, int opt_len,
145 int unit, int debug));
146 static int bsd_decomp_init __P((void *state, u_char *options, int opt_len,
147 int unit, int mru, int debug));
148 static int bsd_compress __P((void *state, mblk_t **mret,
149 mblk_t *mp, int slen, int maxolen));
150 static void bsd_incomp __P((void *state, mblk_t *dmsg));
151 static int bsd_decompress __P((void *state, mblk_t *cmp, mblk_t **dmpp));
152 static void bsd_reset __P((void *state));
153 static void bsd_comp_stats __P((void *state, struct compstat *stats));
156 * Procedures exported to ppp_comp.c.
158 struct compressor ppp_bsd_compress = {
159 0x21, /* compress_proto */
160 bsd_comp_alloc, /* comp_alloc */
161 bsd_free, /* comp_free */
162 bsd_comp_init, /* comp_init */
163 bsd_reset, /* comp_reset */
164 bsd_compress, /* compress */
165 bsd_comp_stats, /* comp_stat */
166 bsd_decomp_alloc, /* decomp_alloc */
167 bsd_free, /* decomp_free */
168 bsd_decomp_init, /* decomp_init */
169 bsd_reset, /* decomp_reset */
170 bsd_decompress, /* decompress */
171 bsd_incomp, /* incomp */
172 bsd_comp_stats, /* decomp_stat */
176 * the next two codes should not be changed lightly, as they must not
177 * lie within the contiguous general code space.
179 #define CLEAR 256 /* table clear output code */
180 #define FIRST 257 /* first free entry */
183 #define MAXCODE(b) ((1 << (b)) - 1)
184 #define BADCODEM1 MAXCODE(MAX_BSD_BITS);
186 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
187 ^ (u_int32_t)(prefix))
188 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
189 + (u_int32_t)(prefix))
191 #define CHECK_GAP 10000 /* Ratio check interval */
193 #define RATIO_SCALE_LOG 8
194 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
195 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
197 #define DECOMP_CHUNK 256
200 * clear the dictionary
207 db->max_ent = FIRST-1;
208 db->n_bits = BSD_INIT_BITS;
212 db->incomp_count = 0;
213 db->checkpoint = CHECK_GAP;
217 * If the dictionary is full, then see if it is time to reset it.
219 * Compute the compression ratio using fixed-point arithmetic
220 * with 8 fractional bits.
222 * Since we have an infinite stream instead of a single file,
223 * watch only the local compression ratio.
225 * Since both peers must reset the dictionary at the same time even in
226 * the absence of CLEAR codes (while packets are incompressible), they
227 * must compute the same ratio.
229 static int /* 1=output CLEAR */
235 if (db->in_count >= db->checkpoint) {
236 /* age the ratio by limiting the size of the counts */
237 if (db->in_count >= RATIO_MAX
238 || db->bytes_out >= RATIO_MAX) {
239 db->in_count -= db->in_count/4;
240 db->bytes_out -= db->bytes_out/4;
243 db->checkpoint = db->in_count + CHECK_GAP;
245 if (db->max_ent >= db->maxmaxcode) {
246 /* Reset the dictionary only if the ratio is worse,
247 * or if it looks as if it has been poisoned
248 * by incompressible data.
250 * This does not overflow, because
251 * db->in_count <= RATIO_MAX.
253 new_ratio = db->in_count << RATIO_SCALE_LOG;
254 if (db->bytes_out != 0)
255 new_ratio /= db->bytes_out;
257 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
261 db->ratio = new_ratio;
271 bsd_comp_stats(state, stats)
273 struct compstat *stats;
275 struct bsd_db *db = (struct bsd_db *) state;
277 stats->unc_bytes = db->uncomp_bytes;
278 stats->unc_packets = db->uncomp_count;
279 stats->comp_bytes = db->comp_bytes;
280 stats->comp_packets = db->comp_count;
281 stats->inc_bytes = db->incomp_bytes;
282 stats->inc_packets = db->incomp_count;
283 stats->ratio = (double) db->in_count;
284 if (db->bytes_out != 0)
285 stats->ratio /= db->bytes_out;
289 * Reset state, as on a CCP ResetReq.
295 struct bsd_db *db = (struct bsd_db *) state;
303 * Allocate space for a (de) compressor.
306 bsd_alloc(options, opt_len, decomp)
311 u_int newlen, hsize, hshift, maxmaxcode;
314 if (opt_len != 3 || options[0] != 0x21 || options[1] != 3
315 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
317 bits = BSD_NBITS(options[2]);
319 case 9: /* needs 82152 for both directions */
320 case 10: /* needs 84144 */
321 case 11: /* needs 88240 */
322 case 12: /* needs 96432 */
326 case 13: /* needs 176784 */
330 case 14: /* needs 353744 */
334 case 15: /* needs 691440 */
338 case 16: /* needs 1366160--far too much, */
339 /* hsize = 69001; */ /* and 69001 is too big for cptr */
340 /* hshift = 8; */ /* in struct bsd_db */
346 maxmaxcode = MAXCODE(bits);
347 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
348 db = (struct bsd_db *) kmem_alloc(newlen, KMEM_NOSLEEP);
351 bzero(db, sizeof(*db) - sizeof(db->dict));
356 db->lens = (u_short *) kmem_alloc((maxmaxcode+1) * sizeof(db->lens[0]),
359 kmem_free(db, newlen);
367 db->maxmaxcode = maxmaxcode;
377 struct bsd_db *db = (struct bsd_db *) state;
380 kmem_free(db->lens, (db->maxmaxcode+1) * sizeof(db->lens[0]));
381 kmem_free(db, db->totlen);
385 bsd_comp_alloc(options, opt_len)
389 return bsd_alloc(options, opt_len, 0);
393 bsd_decomp_alloc(options, opt_len)
397 return bsd_alloc(options, opt_len, 1);
401 * Initialize the database.
404 bsd_init(db, options, opt_len, unit, mru, debug, decomp)
407 int opt_len, unit, mru, debug, decomp;
411 if (opt_len != 3 || options[0] != 0x21 || options[1] != 3
412 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
413 || BSD_NBITS(options[2]) != db->maxbits
414 || decomp && db->lens == NULL)
424 db->dict[--i].codem1 = BADCODEM1;
425 db->dict[i].cptr = 0;
430 db->clear_count = -1;
440 bsd_comp_init(state, options, opt_len, unit, debug)
443 int opt_len, unit, debug;
445 return bsd_init((struct bsd_db *) state, options, opt_len,
450 bsd_decomp_init(state, options, opt_len, unit, mru, debug)
453 int opt_len, unit, mru, debug;
455 return bsd_init((struct bsd_db *) state, options, opt_len,
456 unit, mru, debug, 1);
463 * One change from the BSD compress command is that when the
464 * code size expands, we do not output a bunch of padding.
466 static int /* new slen */
467 bsd_compress(state, mretp, mp, slen, maxolen)
469 mblk_t **mretp; /* return compressed mbuf chain here */
470 mblk_t *mp; /* from here */
471 int slen; /* uncompressed length */
472 int maxolen; /* max compressed length */
474 struct bsd_db *db = (struct bsd_db *) state;
475 int hshift = db->hshift;
476 u_int max_ent = db->max_ent;
477 u_int n_bits = db->n_bits;
479 u_int32_t accm = 0, fcode;
480 struct bsd_dict *dictp;
482 int hval, disp, ent, ilen;
489 #define PUTBYTE(v) { \
492 if (wptr >= cp_end) { \
497 cp_end = m->b_datap->db_lim; \
505 #define OUTPUT(ent) { \
507 accm |= ((ent) << bitno); \
509 PUTBYTE(accm >> 24); \
512 } while (bitno <= 24); \
516 * First get the protocol and check that we're
517 * interested in this packet.
521 if (rptr + PPP_HDRLEN > mp->b_wptr) {
522 if (!pullupmsg(mp, PPP_HDRLEN))
526 ent = PPP_PROTOCOL(rptr); /* get the protocol */
527 if (ent < 0x21 || ent > 0xf9)
530 /* Don't generate compressed packets which are larger than
531 the uncompressed packet. */
535 /* Allocate enough message blocks to give maxolen total space. */
537 for (olen = maxolen; olen > 0; ) {
538 m = allocb((olen < 4096? olen: 4096), BPRI_MED);
548 olen -= m->b_datap->db_lim - m->b_wptr;
552 if ((m = mret) != NULL) {
554 cp_end = m->b_datap->db_lim;
556 wptr = cp_end = NULL;
560 * Copy the PPP header over, changing the protocol,
561 * and install the 2-byte sequence number.
564 wptr[0] = PPP_ADDRESS(rptr);
565 wptr[1] = PPP_CONTROL(rptr);
566 wptr[2] = 0; /* change the protocol */
568 wptr[4] = db->seqno >> 8;
570 wptr += PPP_HDRLEN + BSD_OVHD;
575 slen = mp->b_wptr - rptr;
583 slen = np->b_wptr - rptr;
586 continue; /* handle 0-length buffers */
592 fcode = BSD_KEY(ent, c);
593 hval = BSD_HASH(ent, c, hshift);
594 dictp = &db->dict[hval];
596 /* Validate and then check the entry. */
597 if (dictp->codem1 >= max_ent)
599 if (dictp->f.fcode == fcode) {
600 ent = dictp->codem1+1;
601 continue; /* found (prefix,suffix) */
604 /* continue probing until a match or invalid entry */
605 disp = (hval == 0) ? 1 : hval;
608 if (hval >= db->hsize)
610 dictp = &db->dict[hval];
611 if (dictp->codem1 >= max_ent)
613 } while (dictp->f.fcode != fcode);
614 ent = dictp->codem1+1; /* finally found (prefix,suffix) */
618 OUTPUT(ent); /* output the prefix */
620 /* code -> hashtable */
621 if (max_ent < db->maxmaxcode) {
622 struct bsd_dict *dictp2;
623 /* expand code size if needed */
624 if (max_ent >= MAXCODE(n_bits))
625 db->n_bits = ++n_bits;
627 /* Invalidate old hash table entry using
628 * this code, and then take it over.
630 dictp2 = &db->dict[max_ent+1];
631 if (db->dict[dictp2->cptr].codem1 == max_ent)
632 db->dict[dictp2->cptr].codem1 = BADCODEM1;
634 dictp->codem1 = max_ent;
635 dictp->f.fcode = fcode;
637 db->max_ent = ++max_ent;
641 OUTPUT(ent); /* output the last code */
643 db->bytes_out += olen;
644 db->in_count += ilen;
646 OUTPUT(CLEAR); /* do not count the CLEAR */
649 * Pad dribble bits of last code with ones.
650 * Do not emit a completely useless byte of ones.
653 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
656 * Increase code size if we would have without the packet
657 * boundary and as the decompressor will.
659 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
662 db->uncomp_bytes += ilen;
664 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen && mret != NULL) {
665 /* throw away the compressed stuff if it is longer than uncompressed */
669 db->incomp_bytes += ilen;
670 } else if (wptr != NULL) {
677 db->comp_bytes += olen + BSD_OVHD;
681 return olen + PPP_HDRLEN + BSD_OVHD;
688 * Update the "BSD Compress" dictionary on the receiver for
689 * incompressible data by pretending to compress the incoming data.
692 bsd_incomp(state, dmsg)
696 struct bsd_db *db = (struct bsd_db *) state;
697 u_int hshift = db->hshift;
698 u_int max_ent = db->max_ent;
699 u_int n_bits = db->n_bits;
700 struct bsd_dict *dictp;
710 if (rptr + PPP_HDRLEN > dmsg->b_wptr) {
711 if (!pullupmsg(dmsg, PPP_HDRLEN))
715 ent = PPP_PROTOCOL(rptr); /* get the protocol */
716 if (ent < 0x21 || ent > 0xf9)
721 ilen = 1; /* count the protocol as 1 byte */
724 slen = dmsg->b_wptr - rptr;
730 continue; /* skip zero-length buffers */
736 fcode = BSD_KEY(ent, c);
737 hval = BSD_HASH(ent, c, hshift);
738 dictp = &db->dict[hval];
740 /* validate and then check the entry */
741 if (dictp->codem1 >= max_ent)
743 if (dictp->f.fcode == fcode) {
744 ent = dictp->codem1+1;
745 continue; /* found (prefix,suffix) */
748 /* continue probing until a match or invalid entry */
749 disp = (hval == 0) ? 1 : hval;
752 if (hval >= db->hsize)
754 dictp = &db->dict[hval];
755 if (dictp->codem1 >= max_ent)
757 } while (dictp->f.fcode != fcode);
758 ent = dictp->codem1+1;
759 continue; /* finally found (prefix,suffix) */
761 nomatch: /* output (count) the prefix */
764 /* code -> hashtable */
765 if (max_ent < db->maxmaxcode) {
766 struct bsd_dict *dictp2;
767 /* expand code size if needed */
768 if (max_ent >= MAXCODE(n_bits))
769 db->n_bits = ++n_bits;
771 /* Invalidate previous hash table entry
772 * assigned this code, and then take it over.
774 dictp2 = &db->dict[max_ent+1];
775 if (db->dict[dictp2->cptr].codem1 == max_ent)
776 db->dict[dictp2->cptr].codem1 = BADCODEM1;
778 dictp->codem1 = max_ent;
779 dictp->f.fcode = fcode;
781 db->max_ent = ++max_ent;
782 db->lens[max_ent] = db->lens[ent]+1;
785 } while (--slen != 0);
787 bitno += n_bits; /* output (count) the last code */
788 db->bytes_out += bitno/8;
789 db->in_count += ilen;
793 db->comp_bytes += bitno / 8;
795 db->incomp_bytes += ilen;
797 /* Increase code size if we would have without the packet
798 * boundary and as the decompressor will.
800 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
806 * Decompress "BSD Compress"
809 bsd_decompress(state, cmsg, dmpp)
811 mblk_t *cmsg, **dmpp;
813 struct bsd_db *db = (struct bsd_db *) state;
814 u_int max_ent = db->max_ent;
816 u_int bitno = 32; /* 1st valid bit in accm */
817 u_int n_bits = db->n_bits;
818 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
819 struct bsd_dict *dictp;
820 int explen, i, seq, len;
821 u_int incode, oldcode, finchar;
822 u_char *p, *rptr, *wptr;
824 int adrs, ctrl, ilen;
825 int dlen, space, codelen, extra;
828 * Get at least the BSD Compress header in the first buffer
831 if (rptr + PPP_HDRLEN + BSD_OVHD >= cmsg->b_wptr) {
832 if (!pullupmsg(cmsg, PPP_HDRLEN + BSD_OVHD + 1)) {
834 printf("bsd_decomp%d: failed to pullup\n", db->unit);
841 * Save the address/control from the PPP header
842 * and then get the sequence number.
844 adrs = PPP_ADDRESS(rptr);
845 ctrl = PPP_CONTROL(rptr);
847 seq = (rptr[0] << 8) + rptr[1];
849 ilen = len = cmsg->b_wptr - rptr;
852 * Check the sequence number and give up if it is not what we expect.
854 if (seq != db->seqno++) {
856 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
857 db->unit, seq, db->seqno - 1);
862 * Allocate one message block to start with.
864 if ((dmsg = allocb(DECOMP_CHUNK, BPRI_MED)) == NULL)
867 dmsg->b_rptr = wptr = dmsg->b_wptr;
869 /* Fill in the ppp header, but not the last byte of the protocol
870 (that comes from the decompressed data). */
874 wptr += PPP_HDRLEN - 1;
875 space = dmsg->b_datap->db_lim - wptr;
882 if (!cmsg) /* quit at end of message */
885 len = cmsg->b_wptr - rptr;
886 continue; /* handle 0-length buffers */
890 * Accumulate bytes until we have a complete code.
891 * Then get the next code, relying on the 32-bit,
892 * unsigned accm to mask the result.
895 accm |= *rptr++ << bitno;
897 if (tgtbitno < bitno)
899 incode = accm >> tgtbitno;
903 if (incode == CLEAR) {
905 * The dictionary must only be cleared at
906 * the end of a packet. But there could be an
907 * empty message block at the end.
909 if (len > 0 || cmsg->b_cont != 0) {
911 len += msgdsize(cmsg->b_cont);
915 printf("bsd_decomp%d: bad CLEAR\n", db->unit);
916 return DECOMP_FATALERROR;
924 if (incode > max_ent+2 || incode > db->maxmaxcode
925 || incode > max_ent && oldcode == CLEAR) {
928 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
929 db->unit, incode, oldcode);
930 printf("max_ent=0x%x dlen=%d seqno=%d\n",
931 max_ent, dlen, db->seqno);
933 return DECOMP_FATALERROR;
936 /* Special case for KwKwK string. */
937 if (incode > max_ent) {
945 codelen = db->lens[finchar];
946 explen += codelen + extra;
947 if (explen > db->mru + 1) {
950 printf("bsd_decomp%d: ran out of mru\n", db->unit);
951 return DECOMP_FATALERROR;
955 * Decode this code and install it in the decompressed buffer.
957 space -= codelen + extra;
959 /* Allocate another message block. */
961 dlen = codelen + extra;
962 if (dlen < DECOMP_CHUNK)
964 if ((dmsg->b_cont = allocb(dlen, BPRI_MED)) == NULL) {
970 space = dmsg->b_datap->db_lim - wptr - codelen - extra;
972 p = (wptr += codelen);
973 while (finchar > LAST) {
974 dictp = &db->dict[db->dict[finchar].cptr];
979 printf("bsd_decomp%d: fell off end of chain ", db->unit);
980 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
981 incode, finchar, db->dict[finchar].cptr, max_ent);
982 return DECOMP_FATALERROR;
984 if (dictp->codem1 != finchar-1) {
986 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
987 db->unit, incode, finchar);
988 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
989 db->dict[finchar].cptr, dictp->codem1);
990 return DECOMP_FATALERROR;
993 *--p = dictp->f.hs.suffix;
994 finchar = dictp->f.hs.prefix;
1000 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1001 db->unit, codelen, incode, max_ent);
1004 if (extra) /* the KwKwK case again */
1008 * If not first code in a packet, and
1009 * if not out of code space, then allocate a new code.
1011 * Keep the hash table correct so it can be used
1012 * with uncompressed packets.
1014 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1015 struct bsd_dict *dictp2;
1019 fcode = BSD_KEY(oldcode,finchar);
1020 hval = BSD_HASH(oldcode,finchar,db->hshift);
1021 dictp = &db->dict[hval];
1023 /* look for a free hash table entry */
1024 if (dictp->codem1 < max_ent) {
1025 disp = (hval == 0) ? 1 : hval;
1028 if (hval >= db->hsize)
1030 dictp = &db->dict[hval];
1031 } while (dictp->codem1 < max_ent);
1035 * Invalidate previous hash table entry
1036 * assigned this code, and then take it over
1038 dictp2 = &db->dict[max_ent+1];
1039 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1040 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1042 dictp2->cptr = hval;
1043 dictp->codem1 = max_ent;
1044 dictp->f.fcode = fcode;
1046 db->max_ent = ++max_ent;
1047 db->lens[max_ent] = db->lens[oldcode]+1;
1049 /* Expand code size if needed. */
1050 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1051 db->n_bits = ++n_bits;
1052 tgtbitno = 32-n_bits;
1057 dmsg->b_wptr = wptr;
1060 * Keep the checkpoint right so that incompressible packets
1061 * clear the dictionary at the right times.
1063 db->bytes_out += ilen;
1064 db->in_count += explen;
1065 if (bsd_check(db) && db->debug) {
1066 printf("bsd_decomp%d: peer should have cleared dictionary\n",
1071 db->comp_bytes += ilen + BSD_OVHD;
1073 db->uncomp_bytes += explen;