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
39 * Rewritten for NextStep's funky kernel functions, I/O threads,
40 * and netbufs (instead of real mbufs). Also, ifnets don't install
41 * into the kernel under NS as they do under BSD. We have tried to
42 * make the code remain as similar to the NetBSD version without
43 * incurring too much hassle. This code is the merge of
44 * Philip Prindeville's <philipp@res.enst.fr>/Pete French's <pete@ohm.york.ac.uk>
45 * and Stephen Perkins' <perkins@cps.msu.edu> independent ports.
50 * This version is for use with mbufs on BSD-derived systems.
52 * $Id: bsd-comp.c,v 1.1 1995/12/18 03:30:00 paulus Exp $
55 #include <sys/param.h>
56 #include <sys/types.h>
57 #include <sys/socket.h>
60 #include <net/netbuf.h>
63 #include <net/ppp_defs.h>
64 #include <net/if_ppp.h>
66 #define PACKETPTR netbuf_t
67 #include <net/ppp-comp.h>
72 * We align with this number of bits zero. The code makes the somewhat
73 * suspect assumption that an address can be held in an unsigned long.
74 * Sadly this is necessary to do bit operations on it.
77 #define Z_ALIGN 3 /* 8 byte boudary */
78 #define Z_EXTRA ((unsigned long)((1<<Z_ALIGN)-1))
79 #define ALIGN(x) ((x+Z_EXTRA) & ~Z_EXTRA)
84 * The following includes are necessary to correctly
85 * support BYTE_ORDER. -SJP
88 #include <netinet/in.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/in_var.h>
91 #include <netinet/ip.h>
93 #define mtod(m,type) ((type)nb_map(m))
94 typedef unsigned short u_int16_t;
97 * PPP "BSD compress" compression
98 * The differences between this compression and the classic BSD LZW
99 * source are obvious from the requirement that the classic code worked
100 * with files while this handles arbitrarily long streams that
101 * are broken into packets. They are:
103 * When the code size expands, a block of junk is not emitted by
104 * the compressor and not expected by the decompressor.
106 * New codes are not necessarily assigned every time an old
107 * code is output by the compressor. This is because a packet
108 * end forces a code to be emitted, but does not imply that a
109 * new sequence has been seen.
111 * The compression ratio is checked at the first end of a packet
112 * after the appropriate gap. Besides simplifying and speeding
113 * things up, this makes it more likely that the transmitter
114 * and receiver will agree when the dictionary is cleared when
115 * compression is not going well.
119 * A dictionary for doing BSD compress.
122 void *kbase; /* actual kalloc'd address for struct */
123 void *klens; /* actual kalloc'd address for lens */
124 int totlen; /* length of this structure */
125 u_int hsize; /* size of the hash table */
126 u_char hshift; /* used in hash function */
127 u_char n_bits; /* current bits/code */
131 u_int16_t seqno; /* sequence # of next packet */
132 u_int hdrlen; /* header length to preallocate */
134 u_int maxmaxcode; /* largest valid code */
135 u_int max_ent; /* largest code in use */
136 u_int in_count; /* uncompressed bytes, aged */
137 u_int bytes_out; /* compressed bytes, aged */
138 u_int ratio; /* recent compression ratio */
139 u_int checkpoint; /* when to next check the ratio */
140 u_int clear_count; /* times dictionary cleared */
141 u_int incomp_count; /* incompressible packets */
142 u_int incomp_bytes; /* incompressible bytes */
143 u_int uncomp_count; /* uncompressed packets */
144 u_int uncomp_bytes; /* uncompressed bytes */
145 u_int comp_count; /* compressed packets */
146 u_int comp_bytes; /* compressed bytes */
147 u_int16_t *lens; /* array of lengths of codes */
149 union { /* hash value */
152 #if BYTE_ORDER == LITTLE_ENDIAN
153 u_int16_t prefix; /* preceding code */
154 u_char suffix; /* last character of new code */
158 u_char suffix; /* last character of new code */
159 u_int16_t prefix; /* preceding code */
163 u_int16_t codem1; /* output of hash table -1 */
164 u_int16_t cptr; /* map code to hash table entry */
168 #define BSD_OVHD 2 /* BSD compress overhead/packet */
169 #define BSD_INIT_BITS BSD_MIN_BITS
171 static void *bsd_comp_alloc __P((u_char *options, int opt_len));
172 static void *bsd_decomp_alloc __P((u_char *options, int opt_len));
173 static void bsd_free __P((void *state));
174 static int bsd_comp_init __P((void *state, u_char *options, int opt_len,
175 int unit, int hdrlen, int debug));
176 static int bsd_decomp_init __P((void *state, u_char *options, int opt_len,
177 int unit, int hdrlen, int mru, int debug));
178 static int bsd_compress __P((void *state, netbuf_t *mret,
179 netbuf_t mp, int slen, int maxolen));
180 static void bsd_incomp __P((void *state, netbuf_t dmsg));
181 static int bsd_decompress __P((void *state, netbuf_t cmp, netbuf_t *dmpp));
182 static void bsd_reset __P((void *state));
183 static void bsd_comp_stats __P((void *state, struct compstat *stats));
186 * Procedures exported to if_ppp.c.
188 struct compressor ppp_bsd_compress = {
189 CI_BSD_COMPRESS, /* compress_proto */
190 bsd_comp_alloc, /* comp_alloc */
191 bsd_free, /* comp_free */
192 bsd_comp_init, /* comp_init */
193 bsd_reset, /* comp_reset */
194 bsd_compress, /* compress */
195 bsd_comp_stats, /* comp_stat */
196 bsd_decomp_alloc, /* decomp_alloc */
197 bsd_free, /* decomp_free */
198 bsd_decomp_init, /* decomp_init */
199 bsd_reset, /* decomp_reset */
200 bsd_decompress, /* decompress */
201 bsd_incomp, /* incomp */
202 bsd_comp_stats, /* decomp_stat */
206 * the next two codes should not be changed lightly, as they must not
207 * lie within the contiguous general code space.
209 #define CLEAR 256 /* table clear output code */
210 #define FIRST 257 /* first free entry */
213 #define MAXCODE(b) ((1 << (b)) - 1)
214 #define BADCODEM1 MAXCODE(BSD_MAX_BITS)
216 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
217 ^ (u_int32_t)(prefix))
218 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
219 + (u_int32_t)(prefix))
221 #define CHECK_GAP 10000 /* Ratio check interval */
223 #define RATIO_SCALE_LOG 8
224 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
225 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
227 /* Could include inlines.h */
230 #define IOLogDbg if (db->debug) printf
232 #define IOLogDbg if (db->debug) IOLog
236 * clear the dictionary
243 db->max_ent = FIRST-1;
244 db->n_bits = BSD_INIT_BITS;
248 db->incomp_count = 0;
249 db->checkpoint = CHECK_GAP;
253 * If the dictionary is full, then see if it is time to reset it.
255 * Compute the compression ratio using fixed-point arithmetic
256 * with 8 fractional bits.
258 * Since we have an infinite stream instead of a single file,
259 * watch only the local compression ratio.
261 * Since both peers must reset the dictionary at the same time even in
262 * the absence of CLEAR codes (while packets are incompressible), they
263 * must compute the same ratio.
265 static int /* 1=output CLEAR */
271 if (db->in_count >= db->checkpoint) {
272 /* age the ratio by limiting the size of the counts */
273 if (db->in_count >= RATIO_MAX
274 || db->bytes_out >= RATIO_MAX) {
275 db->in_count -= db->in_count/4;
276 db->bytes_out -= db->bytes_out/4;
279 db->checkpoint = db->in_count + CHECK_GAP;
281 if (db->max_ent >= db->maxmaxcode) {
282 /* Reset the dictionary only if the ratio is worse,
283 * or if it looks as if it has been poisoned
284 * by incompressible data.
286 * This does not overflow, because
287 * db->in_count <= RATIO_MAX.
289 new_ratio = db->in_count << RATIO_SCALE_LOG;
290 if (db->bytes_out != 0)
291 new_ratio /= db->bytes_out;
293 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
297 db->ratio = new_ratio;
307 bsd_comp_stats(state, stats)
309 struct compstat *stats;
311 struct bsd_db *db = (struct bsd_db *) state;
314 stats->unc_bytes = db->uncomp_bytes;
315 stats->unc_packets = db->uncomp_count;
316 stats->comp_bytes = db->comp_bytes;
317 stats->comp_packets = db->comp_count;
318 stats->inc_bytes = db->incomp_bytes;
319 stats->inc_packets = db->incomp_count;
320 stats->ratio = db->in_count;
322 if (stats->ratio <= 0x7fffff)
331 * Reset state, as on a CCP ResetReq.
337 struct bsd_db *db = (struct bsd_db *) state;
345 * Allocate space for a (de) compressor.
348 bsd_alloc(options, opt_len, decomp)
353 u_int newlen, hsize, hshift, maxmaxcode;
356 if (opt_len != CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
357 || options[1] != CILEN_BSD_COMPRESS
358 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
360 bits = BSD_NBITS(options[2]);
362 case 9: /* needs 82152 for both directions */
363 case 10: /* needs 84144 */
364 case 11: /* needs 88240 */
365 case 12: /* needs 96432 */
369 case 13: /* needs 176784 */
373 case 14: /* needs 353744 */
377 case 15: /* needs 691440 */
381 case 16: /* needs 1366160--far too much, */
382 /* hsize = 69001; */ /* and 69001 is too big for cptr */
383 /* hshift = 8; */ /* in struct bsd_db */
389 maxmaxcode = MAXCODE(bits);
390 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
393 kret = (unsigned long) kalloc(Z_EXTRA + newlen);
396 db = (struct bsd_db *) ALIGN(kret);
397 bzero(db, sizeof(*db) - sizeof(db->dict));
398 db->kbase = (void *)kret;
405 kret = (unsigned long) kalloc(Z_EXTRA +
406 ((maxmaxcode+1) * sizeof(db->lens[0])));
408 kfree(db->kbase, newlen + Z_EXTRA);
411 db->lens = (u_int16_t *) ALIGN(kret);
412 db->klens = (void *) kret;
418 db->maxmaxcode = maxmaxcode;
428 struct bsd_db *db = (struct bsd_db *) state;
431 kfree(db->klens, ((db->maxmaxcode+1) * sizeof(db->lens[0])) + Z_EXTRA);
432 kfree(db->kbase, db->totlen + Z_EXTRA);
436 bsd_comp_alloc(options, opt_len)
440 return bsd_alloc(options, opt_len, 0);
444 bsd_decomp_alloc(options, opt_len)
448 return bsd_alloc(options, opt_len, 1);
452 * Initialize the database.
455 bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp)
458 int opt_len, unit, hdrlen, mru, debug, decomp;
462 if (opt_len != CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
463 || options[1] != CILEN_BSD_COMPRESS
464 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
465 || BSD_NBITS(options[2]) != db->maxbits
466 || decomp && db->lens == NULL)
476 db->dict[--i].codem1 = BADCODEM1;
477 db->dict[i].cptr = 0;
494 bsd_comp_init(state, options, opt_len, unit, hdrlen, debug)
497 int opt_len, unit, hdrlen, debug;
499 return bsd_init((struct bsd_db *) state, options, opt_len,
500 unit, hdrlen, 0, debug, 0);
504 bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
507 int opt_len, unit, hdrlen, mru, debug;
509 return bsd_init((struct bsd_db *) state, options, opt_len,
510 unit, hdrlen, mru, debug, 1);
516 * One change from the BSD compress command is that when the
517 * code size expands, we do not output a bunch of padding.
520 bsd_compress(state, mret, mp, slen, maxolen)
522 netbuf_t *mret; /* return compressed netbuf here */
523 netbuf_t mp; /* from here */
524 int slen; /* uncompressed length */
525 int maxolen; /* max compressed length */
527 struct bsd_db *db = (struct bsd_db *) state;
528 int hshift = db->hshift;
529 u_int max_ent = db->max_ent;
530 u_int n_bits = db->n_bits;
532 u_int32_t accm = 0, fcode;
533 struct bsd_dict *dictp;
535 int hval, disp, ent, ilen;
541 #define PUTBYTE(v) { \
545 if (wptr >= cp_end) \
550 #define OUTPUT(ent) { \
552 accm |= ((ent) << bitno); \
554 PUTBYTE(accm >> 24); \
557 } while (bitno <= 24); \
561 * If the protocol is not in the range we're interested in,
562 * just return without compressing the packet. If it is,
563 * the protocol becomes the first byte to compress.
565 rptr = mtod(mp, u_char *);
566 ent = PPP_PROTOCOL(rptr);
567 if (ent < CI_BSD_COMPRESS || ent > 0xf9) {
572 /* Don't generate compressed packets which are larger than
573 the uncompressed packet. */
577 /* Allocate one mbuf to start with. (don't forget space for the FCS!) */
578 m = ppp_nb_alloc(maxolen + db->hdrlen + PPP_FCSLEN);
582 ppp_nb_shrink_top(m, db->hdrlen);
583 nb_shrink_bot(m, PPP_FCSLEN); /* grown by pppstart() */
584 wptr = mtod(m, u_char *);
585 cp_end = wptr + maxolen;
587 wptr = cp_end = NULL;
590 * Copy the PPP header over, changing the protocol,
591 * and install the 2-byte packet sequence number.
594 *wptr++ = PPP_ADDRESS(rptr); /* assumes the ppp header is */
595 *wptr++ = PPP_CONTROL(rptr); /* all in one mbuf */
596 *wptr++ = 0; /* change the protocol */
598 *wptr++ = db->seqno >> 8;
605 slen = nb_size(mp) - PPP_HDRLEN;
610 fcode = BSD_KEY(ent, c);
611 hval = BSD_HASH(ent, c, hshift);
612 dictp = &db->dict[hval];
614 /* Validate and then check the entry. */
615 if (dictp->codem1 >= max_ent)
617 if (dictp->f.fcode == fcode) {
618 ent = dictp->codem1+1;
619 continue; /* found (prefix,suffix) */
622 /* continue probing until a match or invalid entry */
623 disp = (hval == 0) ? 1 : hval;
626 if (hval >= db->hsize)
628 dictp = &db->dict[hval];
629 if (dictp->codem1 >= max_ent)
631 } while (dictp->f.fcode != fcode);
632 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
636 OUTPUT(ent); /* output the prefix */
638 /* code -> hashtable */
639 if (max_ent < db->maxmaxcode) {
640 struct bsd_dict *dictp2;
641 /* expand code size if needed */
642 if (max_ent >= MAXCODE(n_bits))
643 db->n_bits = ++n_bits;
645 /* Invalidate old hash table entry using
646 * this code, and then take it over.
648 dictp2 = &db->dict[max_ent+1];
649 if (db->dict[dictp2->cptr].codem1 == max_ent)
650 db->dict[dictp2->cptr].codem1 = BADCODEM1;
652 dictp->codem1 = max_ent;
653 dictp->f.fcode = fcode;
655 db->max_ent = ++max_ent;
660 OUTPUT(ent); /* output the last code */
661 db->bytes_out += olen;
662 db->in_count += ilen;
664 ++db->bytes_out; /* count complete bytes */
667 OUTPUT(CLEAR); /* do not count the CLEAR */
670 * Pad dribble bits of last code with ones.
671 * Do not emit a completely useless byte of ones.
674 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
677 * Increase code size if we would have without the packet
678 * boundary and as the decompressor will.
680 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
683 db->uncomp_bytes += ilen;
685 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen || wptr == NULL) {
686 /* throw away the compressed stuff if it is longer than uncompressed */
692 db->incomp_bytes += ilen;
694 nb_shrink_bot(m, nb_size(m) - (wptr - mtod(m, u_char *)));
696 db->comp_bytes += olen + BSD_OVHD;
699 return olen + PPP_HDRLEN + BSD_OVHD;
706 * Update the "BSD Compress" dictionary on the receiver for
707 * incompressible data by pretending to compress the incoming data.
710 bsd_incomp(state, dmsg)
714 struct bsd_db *db = (struct bsd_db *) state;
715 u_int hshift = db->hshift;
716 u_int max_ent = db->max_ent;
717 u_int n_bits = db->n_bits;
718 struct bsd_dict *dictp;
721 u_int32_t hval, disp;
728 * If the protocol is not in the range we're interested in,
729 * just return without looking at the packet. If it is,
730 * the protocol becomes the first byte to "compress".
732 rptr = mtod(dmsg, u_char *);
733 ent = PPP_PROTOCOL(rptr);
734 if (ent < CI_BSD_COMPRESS || ent > 0xf9)
739 ilen = 1; /* count the protocol as 1 byte */
741 slen = nb_size(dmsg) - PPP_HDRLEN;
746 fcode = BSD_KEY(ent, c);
747 hval = BSD_HASH(ent, c, hshift);
748 dictp = &db->dict[hval];
750 /* validate and then check the entry */
751 if (dictp->codem1 >= max_ent)
753 if (dictp->f.fcode == fcode) {
754 ent = dictp->codem1+1;
755 continue; /* found (prefix,suffix) */
758 /* continue probing until a match or invalid entry */
759 disp = (hval == 0) ? 1 : hval;
762 if (hval >= db->hsize)
764 dictp = &db->dict[hval];
765 if (dictp->codem1 >= max_ent)
767 } while (dictp->f.fcode != fcode);
768 ent = dictp->codem1+1;
769 continue; /* finally found (prefix,suffix) */
771 nomatch: /* output (count) the prefix */
774 /* code -> hashtable */
775 if (max_ent < db->maxmaxcode) {
776 struct bsd_dict *dictp2;
777 /* expand code size if needed */
778 if (max_ent >= MAXCODE(n_bits))
779 db->n_bits = ++n_bits;
781 /* Invalidate previous hash table entry
782 * assigned this code, and then take it over.
784 dictp2 = &db->dict[max_ent+1];
785 if (db->dict[dictp2->cptr].codem1 == max_ent)
786 db->dict[dictp2->cptr].codem1 = BADCODEM1;
788 dictp->codem1 = max_ent;
789 dictp->f.fcode = fcode;
791 db->max_ent = ++max_ent;
792 db->lens[max_ent] = db->lens[ent]+1;
795 } while (--slen != 0);
796 bitno += n_bits; /* output (count) the last code */
797 db->bytes_out += bitno/8;
798 db->in_count += ilen;
802 db->incomp_bytes += ilen;
804 db->uncomp_bytes += ilen;
806 /* Increase code size if we would have without the packet
807 * boundary and as the decompressor will.
809 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
815 * Decompress "BSD Compress".
817 * Because of patent problems, we return DECOMP_ERROR for errors
818 * found by inspecting the input data and for system problems, but
819 * DECOMP_FATALERROR for any errors which could possibly be said to
820 * be being detected "after" decompression. For DECOMP_ERROR,
821 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
822 * infringing a patent of Motorola's if we do, so we take CCP down
825 * Given that the frame has the correct sequence number and a good FCS,
826 * errors such as invalid codes in the input most likely indicate a
827 * bug, so we return DECOMP_FATALERROR for them in order to turn off
828 * compression, even though they are detected by inspecting the input.
831 bsd_decompress(state, cmp, dmpp)
835 struct bsd_db *db = (struct bsd_db *) state;
836 u_int max_ent = db->max_ent;
838 u_int bitno = 32; /* 1st valid bit in accm */
839 u_int n_bits = db->n_bits;
840 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
841 struct bsd_dict *dictp;
842 int explen, seq, len;
843 u_int incode, oldcode, finchar;
844 u_char *p, *rptr, *wptr;
846 int adrs, ctrl, ilen;
847 int space, codelen, extra, maxilen;
850 * Save the address/control from the PPP header
851 * and then get the sequence number.
854 rptr = mtod(cmp, u_char *);
855 adrs = PPP_ADDRESS(rptr);
856 ctrl = PPP_CONTROL(rptr);
858 len = nb_size(cmp) - PPP_HDRLEN;
859 seq = (rptr[0] << 8) + rptr[1];
864 * Check the sequence number and give up if it differs from
865 * the value we're expecting.
867 if (seq != db->seqno) {
868 IOLogDbg("bsd_decomp%d: bad sequence # %d, expected %d\n",
869 db->unit, seq, db->seqno - 1);
875 * Allocate an netbuf large enough for all the data.
877 maxilen = db->mru + db->hdrlen + PPP_HDRLEN; /* no FCS */
878 dmp = ppp_nb_alloc(maxilen); /* XXX */
882 ppp_nb_shrink_top(dmp, db->hdrlen);
884 wptr = mtod(dmp, u_char *);
885 space = nb_size(dmp) - PPP_HDRLEN + 1;
888 * Fill in the ppp header, but not the last byte of the protocol
889 * (that comes from the decompressed data).
894 wptr += PPP_HDRLEN - 1;
901 * Accumulate bytes until we have a complete code.
902 * Then get the next code, relying on the 32-bit,
903 * unsigned accm to mask the result.
906 accm |= *rptr++ << bitno;
908 if (tgtbitno < bitno)
910 incode = accm >> tgtbitno;
914 if (incode == CLEAR) {
916 * The dictionary must only be cleared at
917 * the end of a packet. But there could be an
918 * empty mbuf at the end.
922 IOLogDbg("bsd_decomp%d: bad CLEAR\n", db->unit);
923 return DECOMP_FATALERROR; /* probably a bug */
930 if (incode > max_ent + 2 || incode > db->maxmaxcode
931 || incode > max_ent && oldcode == CLEAR) {
933 IOLogDbg("bsd_decomp%d: bad code 0x%x oldcode=0x%x max_ent=0x%x explen=%d seqno=%d\n",
934 db->unit, incode, oldcode, max_ent, explen, db->seqno);
935 return DECOMP_FATALERROR; /* probably a bug */
938 /* Special case for KwKwK string. */
939 if (incode > max_ent) {
947 codelen = db->lens[finchar];
948 explen += codelen + extra;
949 if (explen > db->mru + 1) {
951 IOLogDbg("bsd_decomp%d: ran out of mru\n len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
952 db->unit, len, finchar, codelen, explen);
953 return DECOMP_FATALERROR;
957 * If we have no space left, then we've overflowed...
959 if ((space -= codelen + extra) < 0) {
960 IOLog("bsd_decompress%d: no space left in netbuf (need %d bytes)\n",
961 db->unit, (codelen + extra) - space);
967 * Decode this code and install it in the decompressed buffer.
969 p = (wptr += codelen);
970 while (finchar > LAST) {
971 dictp = &db->dict[db->dict[finchar].cptr];
973 if (--codelen <= 0 || dictp->codem1 != finchar-1)
976 *--p = dictp->f.hs.suffix;
977 finchar = dictp->f.hs.prefix;
983 IOLog("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
984 db->unit, codelen, incode, max_ent);
987 if (extra) /* the KwKwK case again */
991 * If not first code in a packet, and
992 * if not out of code space, then allocate a new code.
994 * Keep the hash table correct so it can be used
995 * with uncompressed packets.
997 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
998 struct bsd_dict *dictp2;
1000 u_int32_t hval, disp;
1002 fcode = BSD_KEY(oldcode,finchar);
1003 hval = BSD_HASH(oldcode,finchar,db->hshift);
1004 dictp = &db->dict[hval];
1006 /* look for a free hash table entry */
1007 if (dictp->codem1 < max_ent) {
1008 disp = (hval == 0) ? 1 : hval;
1011 if (hval >= db->hsize)
1013 dictp = &db->dict[hval];
1014 } while (dictp->codem1 < max_ent);
1018 * Invalidate previous hash table entry
1019 * assigned this code, and then take it over
1021 dictp2 = &db->dict[max_ent+1];
1022 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1023 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1025 dictp2->cptr = hval;
1026 dictp->codem1 = max_ent;
1027 dictp->f.fcode = fcode;
1029 db->max_ent = ++max_ent;
1030 db->lens[max_ent] = db->lens[oldcode]+1;
1032 /* Expand code size if needed. */
1033 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1034 db->n_bits = ++n_bits;
1035 tgtbitno = 32-n_bits;
1040 nb_shrink_bot(dmp, nb_size(dmp) - (wptr - mtod(dmp, u_char *)));
1043 * Keep the checkpoint right so that incompressible packets
1044 * clear the dictionary at the right times.
1046 db->bytes_out += ilen;
1047 db->in_count += explen;
1048 if (bsd_check(db)) {
1049 IOLogDbg("bsd_decomp%d: peer should have cleared dictionary\n",
1054 db->comp_bytes += ilen + BSD_OVHD;
1056 db->uncomp_bytes += explen;
1064 IOLog("bsd_decomp%d: fell off end of chain 0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1065 db->unit, incode, finchar, db->dict[finchar].cptr, max_ent);
1066 } else if (dictp->codem1 != finchar-1) {
1067 IOLog("bsd_decomp%d: bad code chain 0x%x finchar=0x%x oldcode=0x%x cptr=0x%x codem1=0x%x\n",
1068 db->unit, incode, finchar, oldcode, db->dict[finchar].cptr,
1072 return DECOMP_FATALERROR;
1075 #endif /* DO_BSD_COMPRESS */