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,
42 * DEC Alpha OSF/1, AIX 4.x, and SVR4 systems including Solaris 2.
44 * $Id: bsd-comp.c,v 1.18 1996/01/18 03:12:53 paulus Exp $
48 #include <net/net_globals.h>
50 #include <sys/param.h>
51 #include <sys/types.h>
52 #include <sys/stream.h>
53 #include <net/ppp_defs.h>
57 #include <sys/byteorder.h>
59 #define BSD_LITTLE_ENDIAN
66 #define BSD_LITTLE_ENDIAN
69 #define PACKETPTR mblk_t *
70 #include <net/ppp-comp.h>
75 * PPP "BSD compress" compression
76 * The differences between this compression and the classic BSD LZW
77 * source are obvious from the requirement that the classic code worked
78 * with files while this handles arbitrarily long streams that
79 * are broken into packets. They are:
81 * When the code size expands, a block of junk is not emitted by
82 * the compressor and not expected by the decompressor.
84 * New codes are not necessarily assigned every time an old
85 * code is output by the compressor. This is because a packet
86 * end forces a code to be emitted, but does not imply that a
87 * new sequence has been seen.
89 * The compression ratio is checked at the first end of a packet
90 * after the appropriate gap. Besides simplifying and speeding
91 * things up, this makes it more likely that the transmitter
92 * and receiver will agree when the dictionary is cleared when
93 * compression is not going well.
97 * A dictionary for doing BSD compress.
100 int totlen; /* length of this structure */
101 u_int hsize; /* size of the hash table */
102 u_char hshift; /* used in hash function */
103 u_char n_bits; /* current bits/code */
107 u_short seqno; /* sequence number of next packet */
108 u_int hdrlen; /* header length to preallocate */
110 u_int maxmaxcode; /* largest valid code */
111 u_int max_ent; /* largest code in use */
112 u_int in_count; /* uncompressed bytes, aged */
113 u_int bytes_out; /* compressed bytes, aged */
114 u_int ratio; /* recent compression ratio */
115 u_int checkpoint; /* when to next check the ratio */
116 u_int clear_count; /* times dictionary cleared */
117 u_int incomp_count; /* incompressible packets */
118 u_int incomp_bytes; /* incompressible bytes */
119 u_int uncomp_count; /* uncompressed packets */
120 u_int uncomp_bytes; /* uncompressed bytes */
121 u_int comp_count; /* compressed packets */
122 u_int comp_bytes; /* compressed bytes */
123 u_short *lens; /* array of lengths of codes */
125 union { /* hash value */
128 #ifdef BSD_LITTLE_ENDIAN
129 u_short prefix; /* preceding code */
130 u_char suffix; /* last character of new code */
134 u_char suffix; /* last character of new code */
135 u_short prefix; /* preceding code */
139 u_short codem1; /* output of hash table -1 */
140 u_short cptr; /* map code to hash table entry */
144 #define BSD_OVHD 2 /* BSD compress overhead/packet */
145 #define BSD_INIT_BITS BSD_MIN_BITS
147 static void *bsd_comp_alloc __P((u_char *options, int opt_len));
148 static void *bsd_decomp_alloc __P((u_char *options, int opt_len));
149 static void bsd_free __P((void *state));
150 static int bsd_comp_init __P((void *state, u_char *options, int opt_len,
151 int unit, int hdrlen, int debug));
152 static int bsd_decomp_init __P((void *state, u_char *options, int opt_len,
153 int unit, int hdrlen, int mru, int debug));
154 static int bsd_compress __P((void *state, mblk_t **mret,
155 mblk_t *mp, int slen, int maxolen));
156 static void bsd_incomp __P((void *state, mblk_t *dmsg));
157 static int bsd_decompress __P((void *state, mblk_t *cmp, mblk_t **dmpp));
158 static void bsd_reset __P((void *state));
159 static void bsd_comp_stats __P((void *state, struct compstat *stats));
162 * Procedures exported to ppp_comp.c.
164 struct compressor ppp_bsd_compress = {
165 CI_BSD_COMPRESS, /* compress_proto */
166 bsd_comp_alloc, /* comp_alloc */
167 bsd_free, /* comp_free */
168 bsd_comp_init, /* comp_init */
169 bsd_reset, /* comp_reset */
170 bsd_compress, /* compress */
171 bsd_comp_stats, /* comp_stat */
172 bsd_decomp_alloc, /* decomp_alloc */
173 bsd_free, /* decomp_free */
174 bsd_decomp_init, /* decomp_init */
175 bsd_reset, /* decomp_reset */
176 bsd_decompress, /* decompress */
177 bsd_incomp, /* incomp */
178 bsd_comp_stats, /* decomp_stat */
182 * the next two codes should not be changed lightly, as they must not
183 * lie within the contiguous general code space.
185 #define CLEAR 256 /* table clear output code */
186 #define FIRST 257 /* first free entry */
189 #define MAXCODE(b) ((1 << (b)) - 1)
190 #define BADCODEM1 MAXCODE(BSD_MAX_BITS)
192 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
193 ^ (u_int32_t)(prefix))
194 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
195 + (u_int32_t)(prefix))
197 #define CHECK_GAP 10000 /* Ratio check interval */
199 #define RATIO_SCALE_LOG 8
200 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
201 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
203 #define DECOMP_CHUNK 256
206 * clear the dictionary
213 db->max_ent = FIRST-1;
214 db->n_bits = BSD_INIT_BITS;
218 db->incomp_count = 0;
219 db->checkpoint = CHECK_GAP;
223 * If the dictionary is full, then see if it is time to reset it.
225 * Compute the compression ratio using fixed-point arithmetic
226 * with 8 fractional bits.
228 * Since we have an infinite stream instead of a single file,
229 * watch only the local compression ratio.
231 * Since both peers must reset the dictionary at the same time even in
232 * the absence of CLEAR codes (while packets are incompressible), they
233 * must compute the same ratio.
235 static int /* 1=output CLEAR */
241 if (db->in_count >= db->checkpoint) {
242 /* age the ratio by limiting the size of the counts */
243 if (db->in_count >= RATIO_MAX
244 || db->bytes_out >= RATIO_MAX) {
245 db->in_count -= db->in_count/4;
246 db->bytes_out -= db->bytes_out/4;
249 db->checkpoint = db->in_count + CHECK_GAP;
251 if (db->max_ent >= db->maxmaxcode) {
252 /* Reset the dictionary only if the ratio is worse,
253 * or if it looks as if it has been poisoned
254 * by incompressible data.
256 * This does not overflow, because
257 * db->in_count <= RATIO_MAX.
259 new_ratio = db->in_count << RATIO_SCALE_LOG;
260 if (db->bytes_out != 0)
261 new_ratio /= db->bytes_out;
263 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
267 db->ratio = new_ratio;
277 bsd_comp_stats(state, stats)
279 struct compstat *stats;
281 struct bsd_db *db = (struct bsd_db *) state;
284 stats->unc_bytes = db->uncomp_bytes;
285 stats->unc_packets = db->uncomp_count;
286 stats->comp_bytes = db->comp_bytes;
287 stats->comp_packets = db->comp_count;
288 stats->inc_bytes = db->incomp_bytes;
289 stats->inc_packets = db->incomp_count;
290 stats->ratio = db->in_count;
292 if (stats->ratio <= 0x7fffff)
301 * Reset state, as on a CCP ResetReq.
307 struct bsd_db *db = (struct bsd_db *) state;
315 * Allocate space for a (de) compressor.
318 bsd_alloc(options, opt_len, decomp)
323 u_int newlen, hsize, hshift, maxmaxcode;
326 if (opt_len != 3 || options[0] != CI_BSD_COMPRESS || options[1] != 3
327 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
329 bits = BSD_NBITS(options[2]);
331 case 9: /* needs 82152 for both directions */
332 case 10: /* needs 84144 */
333 case 11: /* needs 88240 */
334 case 12: /* needs 96432 */
338 case 13: /* needs 176784 */
342 case 14: /* needs 353744 */
346 case 15: /* needs 691440 */
350 case 16: /* needs 1366160--far too much, */
351 /* hsize = 69001; */ /* and 69001 is too big for cptr */
352 /* hshift = 8; */ /* in struct bsd_db */
358 maxmaxcode = MAXCODE(bits);
359 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
360 db = (struct bsd_db *) ALLOC_NOSLEEP(newlen);
363 bzero(db, sizeof(*db) - sizeof(db->dict));
368 db->lens = (u_short *) ALLOC_NOSLEEP((maxmaxcode+1) * sizeof(db->lens[0]));
378 db->maxmaxcode = maxmaxcode;
388 struct bsd_db *db = (struct bsd_db *) state;
391 FREE(db->lens, (db->maxmaxcode+1) * sizeof(db->lens[0]));
392 FREE(db, db->totlen);
396 bsd_comp_alloc(options, opt_len)
400 return bsd_alloc(options, opt_len, 0);
404 bsd_decomp_alloc(options, opt_len)
408 return bsd_alloc(options, opt_len, 1);
412 * Initialize the database.
415 bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp)
418 int opt_len, unit, hdrlen, mru, debug, decomp;
422 if (opt_len < CILEN_BSD_COMPRESS
423 || options[0] != CI_BSD_COMPRESS || options[1] != CILEN_BSD_COMPRESS
424 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
425 || BSD_NBITS(options[2]) != db->maxbits
426 || decomp && db->lens == NULL)
436 db->dict[--i].codem1 = BADCODEM1;
437 db->dict[i].cptr = 0;
452 bsd_comp_init(state, options, opt_len, unit, hdrlen, debug)
455 int opt_len, unit, hdrlen, debug;
457 return bsd_init((struct bsd_db *) state, options, opt_len,
458 unit, hdrlen, 0, debug, 0);
462 bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
465 int opt_len, unit, hdrlen, mru, debug;
467 return bsd_init((struct bsd_db *) state, options, opt_len,
468 unit, hdrlen, mru, debug, 1);
474 * One change from the BSD compress command is that when the
475 * code size expands, we do not output a bunch of padding.
477 * N.B. at present, we ignore the hdrlen specified in the comp_init call.
479 static int /* new slen */
480 bsd_compress(state, mretp, mp, slen, maxolen)
482 mblk_t **mretp; /* return compressed mbuf chain here */
483 mblk_t *mp; /* from here */
484 int slen; /* uncompressed length */
485 int maxolen; /* max compressed length */
487 struct bsd_db *db = (struct bsd_db *) state;
488 int hshift = db->hshift;
489 u_int max_ent = db->max_ent;
490 u_int n_bits = db->n_bits;
492 u_int32_t accm = 0, fcode;
493 struct bsd_dict *dictp;
495 int hval, disp, ent, ilen;
502 #define PUTBYTE(v) { \
505 if (wptr >= cp_end) { \
510 cp_end = m->b_datap->db_lim; \
518 #define OUTPUT(ent) { \
520 accm |= ((ent) << bitno); \
522 PUTBYTE(accm >> 24); \
525 } while (bitno <= 24); \
529 * First get the protocol and check that we're
530 * interested in this packet.
534 if (rptr + PPP_HDRLEN > mp->b_wptr) {
535 if (!pullupmsg(mp, PPP_HDRLEN))
539 ent = PPP_PROTOCOL(rptr); /* get the protocol */
540 if (ent < 0x21 || ent > 0xf9)
543 /* Don't generate compressed packets which are larger than
544 the uncompressed packet. */
548 /* Allocate enough message blocks to give maxolen total space. */
550 for (olen = maxolen; olen > 0; ) {
551 m = allocb((olen < 4096? olen: 4096), BPRI_MED);
561 olen -= m->b_datap->db_lim - m->b_wptr;
565 if ((m = mret) != NULL) {
567 cp_end = m->b_datap->db_lim;
569 wptr = cp_end = NULL;
573 * Copy the PPP header over, changing the protocol,
574 * and install the 2-byte sequence number.
577 wptr[0] = PPP_ADDRESS(rptr);
578 wptr[1] = PPP_CONTROL(rptr);
579 wptr[2] = 0; /* change the protocol */
581 wptr[4] = db->seqno >> 8;
583 wptr += PPP_HDRLEN + BSD_OVHD;
588 slen = mp->b_wptr - rptr;
596 slen = np->b_wptr - rptr;
599 continue; /* handle 0-length buffers */
605 fcode = BSD_KEY(ent, c);
606 hval = BSD_HASH(ent, c, hshift);
607 dictp = &db->dict[hval];
609 /* Validate and then check the entry. */
610 if (dictp->codem1 >= max_ent)
612 if (dictp->f.fcode == fcode) {
613 ent = dictp->codem1+1;
614 continue; /* found (prefix,suffix) */
617 /* continue probing until a match or invalid entry */
618 disp = (hval == 0) ? 1 : hval;
621 if (hval >= db->hsize)
623 dictp = &db->dict[hval];
624 if (dictp->codem1 >= max_ent)
626 } while (dictp->f.fcode != fcode);
627 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
631 OUTPUT(ent); /* output the prefix */
633 /* code -> hashtable */
634 if (max_ent < db->maxmaxcode) {
635 struct bsd_dict *dictp2;
636 /* expand code size if needed */
637 if (max_ent >= MAXCODE(n_bits))
638 db->n_bits = ++n_bits;
640 /* Invalidate old hash table entry using
641 * this code, and then take it over.
643 dictp2 = &db->dict[max_ent+1];
644 if (db->dict[dictp2->cptr].codem1 == max_ent)
645 db->dict[dictp2->cptr].codem1 = BADCODEM1;
647 dictp->codem1 = max_ent;
648 dictp->f.fcode = fcode;
650 db->max_ent = ++max_ent;
655 OUTPUT(ent); /* output the last code */
656 db->bytes_out += olen;
657 db->in_count += ilen;
659 ++db->bytes_out; /* count complete bytes */
662 OUTPUT(CLEAR); /* do not count the CLEAR */
665 * Pad dribble bits of last code with ones.
666 * Do not emit a completely useless byte of ones.
669 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
672 * Increase code size if we would have without the packet
673 * boundary and as the decompressor will.
675 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
678 db->uncomp_bytes += ilen;
680 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen && mret != NULL) {
681 /* throw away the compressed stuff if it is longer than uncompressed */
685 db->incomp_bytes += ilen;
686 } else if (wptr != NULL) {
693 db->comp_bytes += olen + BSD_OVHD;
697 return olen + PPP_HDRLEN + BSD_OVHD;
704 * Update the "BSD Compress" dictionary on the receiver for
705 * incompressible data by pretending to compress the incoming data.
708 bsd_incomp(state, dmsg)
712 struct bsd_db *db = (struct bsd_db *) state;
713 u_int hshift = db->hshift;
714 u_int max_ent = db->max_ent;
715 u_int n_bits = db->n_bits;
716 struct bsd_dict *dictp;
726 if (rptr + PPP_HDRLEN > dmsg->b_wptr) {
727 if (!pullupmsg(dmsg, PPP_HDRLEN))
731 ent = PPP_PROTOCOL(rptr); /* get the protocol */
732 if (ent < 0x21 || ent > 0xf9)
737 ilen = 1; /* count the protocol as 1 byte */
740 slen = dmsg->b_wptr - rptr;
746 continue; /* skip zero-length buffers */
752 fcode = BSD_KEY(ent, c);
753 hval = BSD_HASH(ent, c, hshift);
754 dictp = &db->dict[hval];
756 /* validate and then check the entry */
757 if (dictp->codem1 >= max_ent)
759 if (dictp->f.fcode == fcode) {
760 ent = dictp->codem1+1;
761 continue; /* found (prefix,suffix) */
764 /* continue probing until a match or invalid entry */
765 disp = (hval == 0) ? 1 : hval;
768 if (hval >= db->hsize)
770 dictp = &db->dict[hval];
771 if (dictp->codem1 >= max_ent)
773 } while (dictp->f.fcode != fcode);
774 ent = dictp->codem1+1;
775 continue; /* finally found (prefix,suffix) */
777 nomatch: /* output (count) the prefix */
780 /* code -> hashtable */
781 if (max_ent < db->maxmaxcode) {
782 struct bsd_dict *dictp2;
783 /* expand code size if needed */
784 if (max_ent >= MAXCODE(n_bits))
785 db->n_bits = ++n_bits;
787 /* Invalidate previous hash table entry
788 * assigned this code, and then take it over.
790 dictp2 = &db->dict[max_ent+1];
791 if (db->dict[dictp2->cptr].codem1 == max_ent)
792 db->dict[dictp2->cptr].codem1 = BADCODEM1;
794 dictp->codem1 = max_ent;
795 dictp->f.fcode = fcode;
797 db->max_ent = ++max_ent;
798 db->lens[max_ent] = db->lens[ent]+1;
801 } while (--slen != 0);
803 bitno += n_bits; /* output (count) the last code */
804 db->bytes_out += bitno/8;
805 db->in_count += ilen;
809 db->incomp_bytes += ilen;
811 db->uncomp_bytes += ilen;
813 /* Increase code size if we would have without the packet
814 * boundary and as the decompressor will.
816 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
822 * Decompress "BSD Compress"
824 * Because of patent problems, we return DECOMP_ERROR for errors
825 * found by inspecting the input data and for system problems, but
826 * DECOMP_FATALERROR for any errors which could possibly be said to
827 * be being detected "after" decompression. For DECOMP_ERROR,
828 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
829 * infringing a patent of Motorola's if we do, so we take CCP down
832 * Given that the frame has the correct sequence number and a good FCS,
833 * errors such as invalid codes in the input most likely indicate a
834 * bug, so we return DECOMP_FATALERROR for them in order to turn off
835 * compression, even though they are detected by inspecting the input.
838 bsd_decompress(state, cmsg, dmpp)
840 mblk_t *cmsg, **dmpp;
842 struct bsd_db *db = (struct bsd_db *) state;
843 u_int max_ent = db->max_ent;
845 u_int bitno = 32; /* 1st valid bit in accm */
846 u_int n_bits = db->n_bits;
847 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
848 struct bsd_dict *dictp;
849 int explen, i, seq, len;
850 u_int incode, oldcode, finchar;
851 u_char *p, *rptr, *wptr;
853 int adrs, ctrl, ilen;
854 int dlen, space, codelen, extra;
857 * Get at least the BSD Compress header in the first buffer
860 if (rptr + PPP_HDRLEN + BSD_OVHD >= cmsg->b_wptr) {
861 if (!pullupmsg(cmsg, PPP_HDRLEN + BSD_OVHD + 1)) {
863 printf("bsd_decomp%d: failed to pullup\n", db->unit);
870 * Save the address/control from the PPP header
871 * and then get the sequence number.
873 adrs = PPP_ADDRESS(rptr);
874 ctrl = PPP_CONTROL(rptr);
876 seq = (rptr[0] << 8) + rptr[1];
878 ilen = len = cmsg->b_wptr - rptr;
881 * Check the sequence number and give up if it is not what we expect.
883 if (seq != db->seqno++) {
885 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
886 db->unit, seq, db->seqno - 1);
891 * Allocate one message block to start with.
893 if ((dmsg = allocb(DECOMP_CHUNK + db->hdrlen, BPRI_MED)) == NULL)
896 dmsg->b_wptr += db->hdrlen;
897 dmsg->b_rptr = wptr = dmsg->b_wptr;
899 /* Fill in the ppp header, but not the last byte of the protocol
900 (that comes from the decompressed data). */
904 wptr += PPP_HDRLEN - 1;
905 space = dmsg->b_datap->db_lim - wptr;
912 if (!cmsg) /* quit at end of message */
915 len = cmsg->b_wptr - rptr;
917 continue; /* handle 0-length buffers */
921 * Accumulate bytes until we have a complete code.
922 * Then get the next code, relying on the 32-bit,
923 * unsigned accm to mask the result.
926 accm |= *rptr++ << bitno;
928 if (tgtbitno < bitno)
930 incode = accm >> tgtbitno;
934 if (incode == CLEAR) {
936 * The dictionary must only be cleared at
937 * the end of a packet. But there could be an
938 * empty message block at the end.
940 if (len > 0 || cmsg->b_cont != 0) {
942 len += msgdsize(cmsg->b_cont);
946 printf("bsd_decomp%d: bad CLEAR\n", db->unit);
947 return DECOMP_FATALERROR;
955 if (incode > max_ent + 2 || incode > db->maxmaxcode
956 || incode > max_ent && oldcode == CLEAR) {
959 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
960 db->unit, incode, oldcode);
961 printf("max_ent=0x%x dlen=%d seqno=%d\n",
962 max_ent, dlen, db->seqno);
964 return DECOMP_FATALERROR; /* probably a bug */
967 /* Special case for KwKwK string. */
968 if (incode > max_ent) {
976 codelen = db->lens[finchar];
977 explen += codelen + extra;
978 if (explen > db->mru + 1) {
981 printf("bsd_decomp%d: ran out of mru\n", db->unit);
982 return DECOMP_FATALERROR;
986 * Decode this code and install it in the decompressed buffer.
988 space -= codelen + extra;
990 /* Allocate another message block. */
992 dlen = codelen + extra;
993 if (dlen < DECOMP_CHUNK)
995 if ((dmsg->b_cont = allocb(dlen, BPRI_MED)) == NULL) {
1000 wptr = dmsg->b_wptr;
1001 space = dmsg->b_datap->db_lim - wptr - codelen - extra;
1003 p = (wptr += codelen);
1004 while (finchar > LAST) {
1005 dictp = &db->dict[db->dict[finchar].cptr];
1010 printf("bsd_decomp%d: fell off end of chain ", db->unit);
1011 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1012 incode, finchar, db->dict[finchar].cptr, max_ent);
1013 return DECOMP_FATALERROR;
1015 if (dictp->codem1 != finchar-1) {
1017 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1018 db->unit, incode, finchar);
1019 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
1020 db->dict[finchar].cptr, dictp->codem1);
1021 return DECOMP_FATALERROR;
1024 *--p = dictp->f.hs.suffix;
1025 finchar = dictp->f.hs.prefix;
1031 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1032 db->unit, codelen, incode, max_ent);
1035 if (extra) /* the KwKwK case again */
1039 * If not first code in a packet, and
1040 * if not out of code space, then allocate a new code.
1042 * Keep the hash table correct so it can be used
1043 * with uncompressed packets.
1045 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1046 struct bsd_dict *dictp2;
1050 fcode = BSD_KEY(oldcode,finchar);
1051 hval = BSD_HASH(oldcode,finchar,db->hshift);
1052 dictp = &db->dict[hval];
1054 /* look for a free hash table entry */
1055 if (dictp->codem1 < max_ent) {
1056 disp = (hval == 0) ? 1 : hval;
1059 if (hval >= db->hsize)
1061 dictp = &db->dict[hval];
1062 } while (dictp->codem1 < max_ent);
1066 * Invalidate previous hash table entry
1067 * assigned this code, and then take it over
1069 dictp2 = &db->dict[max_ent+1];
1070 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1071 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1073 dictp2->cptr = hval;
1074 dictp->codem1 = max_ent;
1075 dictp->f.fcode = fcode;
1077 db->max_ent = ++max_ent;
1078 db->lens[max_ent] = db->lens[oldcode]+1;
1080 /* Expand code size if needed. */
1081 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1082 db->n_bits = ++n_bits;
1083 tgtbitno = 32-n_bits;
1088 dmsg->b_wptr = wptr;
1091 * Keep the checkpoint right so that incompressible packets
1092 * clear the dictionary at the right times.
1094 db->bytes_out += ilen;
1095 db->in_count += explen;
1096 if (bsd_check(db) && db->debug) {
1097 printf("bsd_decomp%d: peer should have cleared dictionary\n",
1102 db->comp_bytes += ilen + BSD_OVHD;
1104 db->uncomp_bytes += explen;
1109 #endif /* DO_BSD_COMPRESS */