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.3 1996/09/14 05:08:26 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->checkpoint = CHECK_GAP;
252 * If the dictionary is full, then see if it is time to reset it.
254 * Compute the compression ratio using fixed-point arithmetic
255 * with 8 fractional bits.
257 * Since we have an infinite stream instead of a single file,
258 * watch only the local compression ratio.
260 * Since both peers must reset the dictionary at the same time even in
261 * the absence of CLEAR codes (while packets are incompressible), they
262 * must compute the same ratio.
264 static int /* 1=output CLEAR */
270 if (db->in_count >= db->checkpoint) {
271 /* age the ratio by limiting the size of the counts */
272 if (db->in_count >= RATIO_MAX
273 || db->bytes_out >= RATIO_MAX) {
274 db->in_count -= db->in_count/4;
275 db->bytes_out -= db->bytes_out/4;
278 db->checkpoint = db->in_count + CHECK_GAP;
280 if (db->max_ent >= db->maxmaxcode) {
281 /* Reset the dictionary only if the ratio is worse,
282 * or if it looks as if it has been poisoned
283 * by incompressible data.
285 * This does not overflow, because
286 * db->in_count <= RATIO_MAX.
288 new_ratio = db->in_count << RATIO_SCALE_LOG;
289 if (db->bytes_out != 0)
290 new_ratio /= db->bytes_out;
292 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
296 db->ratio = new_ratio;
306 bsd_comp_stats(state, stats)
308 struct compstat *stats;
310 struct bsd_db *db = (struct bsd_db *) state;
313 stats->unc_bytes = db->uncomp_bytes;
314 stats->unc_packets = db->uncomp_count;
315 stats->comp_bytes = db->comp_bytes;
316 stats->comp_packets = db->comp_count;
317 stats->inc_bytes = db->incomp_bytes;
318 stats->inc_packets = db->incomp_count;
319 stats->ratio = db->in_count;
321 if (stats->ratio <= 0x7fffff)
330 * Reset state, as on a CCP ResetReq.
336 struct bsd_db *db = (struct bsd_db *) state;
344 * Allocate space for a (de) compressor.
347 bsd_alloc(options, opt_len, decomp)
352 u_int newlen, hsize, hshift, maxmaxcode;
355 if (opt_len != CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
356 || options[1] != CILEN_BSD_COMPRESS
357 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
359 bits = BSD_NBITS(options[2]);
361 case 9: /* needs 82152 for both directions */
362 case 10: /* needs 84144 */
363 case 11: /* needs 88240 */
364 case 12: /* needs 96432 */
368 case 13: /* needs 176784 */
372 case 14: /* needs 353744 */
376 case 15: /* needs 691440 */
380 case 16: /* needs 1366160--far too much, */
381 /* hsize = 69001; */ /* and 69001 is too big for cptr */
382 /* hshift = 8; */ /* in struct bsd_db */
388 maxmaxcode = MAXCODE(bits);
389 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
392 kret = (unsigned long) kalloc(Z_EXTRA + newlen);
395 db = (struct bsd_db *) ALIGN(kret);
396 bzero(db, sizeof(*db) - sizeof(db->dict));
397 db->kbase = (void *)kret;
404 kret = (unsigned long) kalloc(Z_EXTRA +
405 ((maxmaxcode+1) * sizeof(db->lens[0])));
407 kfree(db->kbase, newlen + Z_EXTRA);
410 db->lens = (u_int16_t *) ALIGN(kret);
411 db->klens = (void *) kret;
417 db->maxmaxcode = maxmaxcode;
427 struct bsd_db *db = (struct bsd_db *) state;
430 kfree(db->klens, ((db->maxmaxcode+1) * sizeof(db->lens[0])) + Z_EXTRA);
431 kfree(db->kbase, db->totlen + Z_EXTRA);
435 bsd_comp_alloc(options, opt_len)
439 return bsd_alloc(options, opt_len, 0);
443 bsd_decomp_alloc(options, opt_len)
447 return bsd_alloc(options, opt_len, 1);
451 * Initialize the database.
454 bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp)
457 int opt_len, unit, hdrlen, mru, debug, decomp;
461 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
462 || options[1] != CILEN_BSD_COMPRESS
463 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
464 || BSD_NBITS(options[2]) != db->maxbits
465 || decomp && db->lens == NULL)
475 db->dict[--i].codem1 = BADCODEM1;
476 db->dict[i].cptr = 0;
493 bsd_comp_init(state, options, opt_len, unit, hdrlen, debug)
496 int opt_len, unit, hdrlen, debug;
498 return bsd_init((struct bsd_db *) state, options, opt_len,
499 unit, hdrlen, 0, debug, 0);
503 bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
506 int opt_len, unit, hdrlen, mru, debug;
508 return bsd_init((struct bsd_db *) state, options, opt_len,
509 unit, hdrlen, mru, debug, 1);
515 * One change from the BSD compress command is that when the
516 * code size expands, we do not output a bunch of padding.
519 bsd_compress(state, mret, mp, slen, maxolen)
521 netbuf_t *mret; /* return compressed netbuf here */
522 netbuf_t mp; /* from here */
523 int slen; /* uncompressed length */
524 int maxolen; /* max compressed length */
526 struct bsd_db *db = (struct bsd_db *) state;
527 int hshift = db->hshift;
528 u_int max_ent = db->max_ent;
529 u_int n_bits = db->n_bits;
531 u_int32_t accm = 0, fcode;
532 struct bsd_dict *dictp;
534 int hval, disp, ent, ilen;
540 #define PUTBYTE(v) { \
544 if (wptr >= cp_end) \
549 #define OUTPUT(ent) { \
551 accm |= ((ent) << bitno); \
553 PUTBYTE(accm >> 24); \
556 } while (bitno <= 24); \
560 * If the protocol is not in the range we're interested in,
561 * just return without compressing the packet. If it is,
562 * the protocol becomes the first byte to compress.
564 rptr = mtod(mp, u_char *);
565 ent = PPP_PROTOCOL(rptr);
566 if (ent < CI_BSD_COMPRESS || ent > 0xf9) {
571 /* Don't generate compressed packets which are larger than
572 the uncompressed packet. */
576 /* Allocate one mbuf to start with. (don't forget space for the FCS!) */
577 m = ppp_nb_alloc(maxolen + db->hdrlen + PPP_FCSLEN);
581 ppp_nb_shrink_top(m, db->hdrlen);
582 nb_shrink_bot(m, PPP_FCSLEN); /* grown by pppstart() */
583 wptr = mtod(m, u_char *);
584 cp_end = wptr + maxolen;
586 wptr = cp_end = NULL;
589 * Copy the PPP header over, changing the protocol,
590 * and install the 2-byte packet sequence number.
593 *wptr++ = PPP_ADDRESS(rptr); /* assumes the ppp header is */
594 *wptr++ = PPP_CONTROL(rptr); /* all in one mbuf */
595 *wptr++ = 0; /* change the protocol */
597 *wptr++ = db->seqno >> 8;
604 slen = nb_size(mp) - PPP_HDRLEN;
609 fcode = BSD_KEY(ent, c);
610 hval = BSD_HASH(ent, c, hshift);
611 dictp = &db->dict[hval];
613 /* Validate and then check the entry. */
614 if (dictp->codem1 >= max_ent)
616 if (dictp->f.fcode == fcode) {
617 ent = dictp->codem1+1;
618 continue; /* found (prefix,suffix) */
621 /* continue probing until a match or invalid entry */
622 disp = (hval == 0) ? 1 : hval;
625 if (hval >= db->hsize)
627 dictp = &db->dict[hval];
628 if (dictp->codem1 >= max_ent)
630 } while (dictp->f.fcode != fcode);
631 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
635 OUTPUT(ent); /* output the prefix */
637 /* code -> hashtable */
638 if (max_ent < db->maxmaxcode) {
639 struct bsd_dict *dictp2;
640 /* expand code size if needed */
641 if (max_ent >= MAXCODE(n_bits))
642 db->n_bits = ++n_bits;
644 /* Invalidate old hash table entry using
645 * this code, and then take it over.
647 dictp2 = &db->dict[max_ent+1];
648 if (db->dict[dictp2->cptr].codem1 == max_ent)
649 db->dict[dictp2->cptr].codem1 = BADCODEM1;
651 dictp->codem1 = max_ent;
652 dictp->f.fcode = fcode;
654 db->max_ent = ++max_ent;
659 OUTPUT(ent); /* output the last code */
660 db->bytes_out += olen;
661 db->in_count += ilen;
663 ++db->bytes_out; /* count complete bytes */
666 OUTPUT(CLEAR); /* do not count the CLEAR */
669 * Pad dribble bits of last code with ones.
670 * Do not emit a completely useless byte of ones.
673 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
676 * Increase code size if we would have without the packet
677 * boundary and as the decompressor will.
679 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
682 db->uncomp_bytes += ilen;
684 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen || wptr == NULL) {
685 /* throw away the compressed stuff if it is longer than uncompressed */
691 db->incomp_bytes += ilen;
693 nb_shrink_bot(m, nb_size(m) - (wptr - mtod(m, u_char *)));
695 db->comp_bytes += olen + BSD_OVHD;
698 return olen + PPP_HDRLEN + BSD_OVHD;
705 * Update the "BSD Compress" dictionary on the receiver for
706 * incompressible data by pretending to compress the incoming data.
709 bsd_incomp(state, dmsg)
713 struct bsd_db *db = (struct bsd_db *) state;
714 u_int hshift = db->hshift;
715 u_int max_ent = db->max_ent;
716 u_int n_bits = db->n_bits;
717 struct bsd_dict *dictp;
720 u_int32_t hval, disp;
727 * If the protocol is not in the range we're interested in,
728 * just return without looking at the packet. If it is,
729 * the protocol becomes the first byte to "compress".
731 rptr = mtod(dmsg, u_char *);
732 ent = PPP_PROTOCOL(rptr);
733 if (ent < CI_BSD_COMPRESS || ent > 0xf9)
737 ilen = 1; /* count the protocol as 1 byte */
739 slen = nb_size(dmsg) - PPP_HDRLEN;
744 fcode = BSD_KEY(ent, c);
745 hval = BSD_HASH(ent, c, hshift);
746 dictp = &db->dict[hval];
748 /* validate and then check the entry */
749 if (dictp->codem1 >= max_ent)
751 if (dictp->f.fcode == fcode) {
752 ent = dictp->codem1+1;
753 continue; /* found (prefix,suffix) */
756 /* continue probing until a match or invalid entry */
757 disp = (hval == 0) ? 1 : hval;
760 if (hval >= db->hsize)
762 dictp = &db->dict[hval];
763 if (dictp->codem1 >= max_ent)
765 } while (dictp->f.fcode != fcode);
766 ent = dictp->codem1+1;
767 continue; /* finally found (prefix,suffix) */
769 nomatch: /* output (count) the prefix */
772 /* code -> hashtable */
773 if (max_ent < db->maxmaxcode) {
774 struct bsd_dict *dictp2;
775 /* expand code size if needed */
776 if (max_ent >= MAXCODE(n_bits))
777 db->n_bits = ++n_bits;
779 /* Invalidate previous hash table entry
780 * assigned this code, and then take it over.
782 dictp2 = &db->dict[max_ent+1];
783 if (db->dict[dictp2->cptr].codem1 == max_ent)
784 db->dict[dictp2->cptr].codem1 = BADCODEM1;
786 dictp->codem1 = max_ent;
787 dictp->f.fcode = fcode;
789 db->max_ent = ++max_ent;
790 db->lens[max_ent] = db->lens[ent]+1;
793 } while (--slen != 0);
794 bitno += n_bits; /* output (count) the last code */
795 db->bytes_out += bitno/8;
796 db->in_count += ilen;
800 db->incomp_bytes += ilen;
802 db->uncomp_bytes += ilen;
804 /* Increase code size if we would have without the packet
805 * boundary and as the decompressor will.
807 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
813 * Decompress "BSD Compress".
815 * Because of patent problems, we return DECOMP_ERROR for errors
816 * found by inspecting the input data and for system problems, but
817 * DECOMP_FATALERROR for any errors which could possibly be said to
818 * be being detected "after" decompression. For DECOMP_ERROR,
819 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
820 * infringing a patent of Motorola's if we do, so we take CCP down
823 * Given that the frame has the correct sequence number and a good FCS,
824 * errors such as invalid codes in the input most likely indicate a
825 * bug, so we return DECOMP_FATALERROR for them in order to turn off
826 * compression, even though they are detected by inspecting the input.
829 bsd_decompress(state, cmp, dmpp)
833 struct bsd_db *db = (struct bsd_db *) state;
834 u_int max_ent = db->max_ent;
836 u_int bitno = 32; /* 1st valid bit in accm */
837 u_int n_bits = db->n_bits;
838 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
839 struct bsd_dict *dictp;
840 int explen, seq, len;
841 u_int incode, oldcode, finchar;
842 u_char *p, *rptr, *wptr;
844 int adrs, ctrl, ilen;
845 int space, codelen, extra, maxilen;
848 * Save the address/control from the PPP header
849 * and then get the sequence number.
852 rptr = mtod(cmp, u_char *);
853 adrs = PPP_ADDRESS(rptr);
854 ctrl = PPP_CONTROL(rptr);
856 len = nb_size(cmp) - PPP_HDRLEN;
857 seq = (rptr[0] << 8) + rptr[1];
862 * Check the sequence number and give up if it differs from
863 * the value we're expecting.
865 if (seq != db->seqno) {
866 IOLogDbg("bsd_decomp%d: bad sequence # %d, expected %d\n",
867 db->unit, seq, db->seqno - 1);
873 * Allocate an netbuf large enough for all the data.
875 maxilen = db->mru + db->hdrlen + PPP_HDRLEN; /* no FCS */
876 dmp = ppp_nb_alloc(maxilen); /* XXX */
880 ppp_nb_shrink_top(dmp, db->hdrlen);
882 wptr = mtod(dmp, u_char *);
883 space = nb_size(dmp) - PPP_HDRLEN + 1;
886 * Fill in the ppp header, but not the last byte of the protocol
887 * (that comes from the decompressed data).
892 wptr += PPP_HDRLEN - 1;
899 * Accumulate bytes until we have a complete code.
900 * Then get the next code, relying on the 32-bit,
901 * unsigned accm to mask the result.
904 accm |= *rptr++ << bitno;
906 if (tgtbitno < bitno)
908 incode = accm >> tgtbitno;
912 if (incode == CLEAR) {
914 * The dictionary must only be cleared at
915 * the end of a packet. But there could be an
916 * empty mbuf at the end.
920 IOLogDbg("bsd_decomp%d: bad CLEAR\n", db->unit);
921 return DECOMP_FATALERROR; /* probably a bug */
928 if (incode > max_ent + 2 || incode > db->maxmaxcode
929 || incode > max_ent && oldcode == CLEAR) {
931 IOLogDbg("bsd_decomp%d: bad code 0x%x oldcode=0x%x max_ent=0x%x explen=%d seqno=%d\n",
932 db->unit, incode, oldcode, max_ent, explen, db->seqno);
933 return DECOMP_FATALERROR; /* probably a bug */
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) {
949 IOLogDbg("bsd_decomp%d: ran out of mru\n len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
950 db->unit, len, finchar, codelen, explen);
951 return DECOMP_FATALERROR;
955 * If we have no space left, then we've overflowed...
957 if ((space -= codelen + extra) < 0) {
958 IOLog("bsd_decompress%d: no space left in netbuf (need %d bytes)\n",
959 db->unit, (codelen + extra) - space);
965 * Decode this code and install it in the decompressed buffer.
967 p = (wptr += codelen);
968 while (finchar > LAST) {
969 dictp = &db->dict[db->dict[finchar].cptr];
971 if (--codelen <= 0 || dictp->codem1 != finchar-1)
974 *--p = dictp->f.hs.suffix;
975 finchar = dictp->f.hs.prefix;
981 IOLog("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
982 db->unit, codelen, incode, max_ent);
985 if (extra) /* the KwKwK case again */
989 * If not first code in a packet, and
990 * if not out of code space, then allocate a new code.
992 * Keep the hash table correct so it can be used
993 * with uncompressed packets.
995 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
996 struct bsd_dict *dictp2;
998 u_int32_t hval, disp;
1000 fcode = BSD_KEY(oldcode,finchar);
1001 hval = BSD_HASH(oldcode,finchar,db->hshift);
1002 dictp = &db->dict[hval];
1004 /* look for a free hash table entry */
1005 if (dictp->codem1 < max_ent) {
1006 disp = (hval == 0) ? 1 : hval;
1009 if (hval >= db->hsize)
1011 dictp = &db->dict[hval];
1012 } while (dictp->codem1 < max_ent);
1016 * Invalidate previous hash table entry
1017 * assigned this code, and then take it over
1019 dictp2 = &db->dict[max_ent+1];
1020 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1021 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1023 dictp2->cptr = hval;
1024 dictp->codem1 = max_ent;
1025 dictp->f.fcode = fcode;
1027 db->max_ent = ++max_ent;
1028 db->lens[max_ent] = db->lens[oldcode]+1;
1030 /* Expand code size if needed. */
1031 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1032 db->n_bits = ++n_bits;
1033 tgtbitno = 32-n_bits;
1038 nb_shrink_bot(dmp, nb_size(dmp) - (wptr - mtod(dmp, u_char *)));
1041 * Keep the checkpoint right so that incompressible packets
1042 * clear the dictionary at the right times.
1044 db->bytes_out += ilen;
1045 db->in_count += explen;
1046 if (bsd_check(db)) {
1047 IOLogDbg("bsd_decomp%d: peer should have cleared dictionary\n",
1052 db->comp_bytes += ilen + BSD_OVHD;
1054 db->uncomp_bytes += explen;
1062 IOLog("bsd_decomp%d: fell off end of chain 0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1063 db->unit, incode, finchar, db->dict[finchar].cptr, max_ent);
1064 } else if (dictp->codem1 != finchar-1) {
1065 IOLog("bsd_decomp%d: bad code chain 0x%x finchar=0x%x oldcode=0x%x cptr=0x%x codem1=0x%x\n",
1066 db->unit, incode, finchar, oldcode, db->dict[finchar].cptr,
1070 return DECOMP_FATALERROR;
1073 #endif /* DO_BSD_COMPRESS */