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.5 1998/03/26 02:51:45 paulus Exp $
55 #include <sys/types.h>
56 #include <sys/param.h>
57 #include <sys/socket.h>
60 #include <net/netbuf.h>
63 #include <net/ppp_defs.h>
65 #include <net/if_ppp.h>
69 #define PACKETPTR NETBUF_T
70 #include <net/ppp-comp.h>
74 * We align with this number of bits zero. The code makes the somewhat
75 * suspect assumption that an address can be held in an unsigned long.
76 * Sadly this is necessary to do bit operations on it.
79 #define Z_ALIGN 3 /* 8 byte boudary */
80 #define Z_EXTRA ((unsigned long)((1<<Z_ALIGN)-1))
81 #define ALIGN(x) ((x+Z_EXTRA) & ~Z_EXTRA)
86 * The following includes are necessary to correctly
87 * support BYTE_ORDER. -SJP
90 #include <netinet/in.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/in_var.h>
93 #include <netinet/ip.h>
95 #define mtod(m,type) ((type)NB_MAP(m))
98 * PPP "BSD compress" compression
99 * The differences between this compression and the classic BSD LZW
100 * source are obvious from the requirement that the classic code worked
101 * with files while this handles arbitrarily long streams that
102 * are broken into packets. They are:
104 * When the code size expands, a block of junk is not emitted by
105 * the compressor and not expected by the decompressor.
107 * New codes are not necessarily assigned every time an old
108 * code is output by the compressor. This is because a packet
109 * end forces a code to be emitted, but does not imply that a
110 * new sequence has been seen.
112 * The compression ratio is checked at the first end of a packet
113 * after the appropriate gap. Besides simplifying and speeding
114 * things up, this makes it more likely that the transmitter
115 * and receiver will agree when the dictionary is cleared when
116 * compression is not going well.
120 * A dictionary for doing BSD compress.
123 void *kbase; /* actual kalloc'd address for struct */
124 void *klens; /* actual kalloc'd address for lens */
125 int totlen; /* length of this structure */
126 u_int hsize; /* size of the hash table */
127 u_char hshift; /* used in hash function */
128 u_char n_bits; /* current bits/code */
132 u_int16_t seqno; /* sequence # of next packet */
133 u_int hdrlen; /* header length to preallocate */
135 u_int maxmaxcode; /* largest valid code */
136 u_int max_ent; /* largest code in use */
137 u_int in_count; /* uncompressed bytes, aged */
138 u_int bytes_out; /* compressed bytes, aged */
139 u_int ratio; /* recent compression ratio */
140 u_int checkpoint; /* when to next check the ratio */
141 u_int clear_count; /* times dictionary cleared */
142 u_int incomp_count; /* incompressible packets */
143 u_int incomp_bytes; /* incompressible bytes */
144 u_int uncomp_count; /* uncompressed packets */
145 u_int uncomp_bytes; /* uncompressed bytes */
146 u_int comp_count; /* compressed packets */
147 u_int comp_bytes; /* compressed bytes */
148 u_int16_t *lens; /* array of lengths of codes */
150 union { /* hash value */
153 #if BYTE_ORDER == LITTLE_ENDIAN
154 u_int16_t prefix; /* preceding code */
155 u_char suffix; /* last character of new code */
159 u_char suffix; /* last character of new code */
160 u_int16_t prefix; /* preceding code */
164 u_int16_t codem1; /* output of hash table -1 */
165 u_int16_t cptr; /* map code to hash table entry */
169 #define BSD_OVHD 2 /* BSD compress overhead/packet */
170 #define BSD_INIT_BITS BSD_MIN_BITS
172 static void *bsd_comp_alloc __P((u_char *options, int opt_len));
173 static void *bsd_decomp_alloc __P((u_char *options, int opt_len));
174 static void bsd_free __P((void *state));
175 static int bsd_comp_init __P((void *state, u_char *options, int opt_len,
176 int unit, int hdrlen, int debug));
177 static int bsd_decomp_init __P((void *state, u_char *options, int opt_len,
178 int unit, int hdrlen, int mru, int debug));
179 static int bsd_compress __P((void *state, NETBUF_T *mret,
180 NETBUF_T mp, int slen, int maxolen));
181 static void bsd_incomp __P((void *state, NETBUF_T dmsg));
182 static int bsd_decompress __P((void *state, NETBUF_T cmp, NETBUF_T *dmpp));
183 static void bsd_reset __P((void *state));
184 static void bsd_comp_stats __P((void *state, struct compstat *stats));
187 * Procedures exported to if_ppp.c.
189 struct compressor ppp_bsd_compress = {
190 CI_BSD_COMPRESS, /* compress_proto */
191 bsd_comp_alloc, /* comp_alloc */
192 bsd_free, /* comp_free */
193 bsd_comp_init, /* comp_init */
194 bsd_reset, /* comp_reset */
195 bsd_compress, /* compress */
196 bsd_comp_stats, /* comp_stat */
197 bsd_decomp_alloc, /* decomp_alloc */
198 bsd_free, /* decomp_free */
199 bsd_decomp_init, /* decomp_init */
200 bsd_reset, /* decomp_reset */
201 bsd_decompress, /* decompress */
202 bsd_incomp, /* incomp */
203 bsd_comp_stats, /* decomp_stat */
207 * the next two codes should not be changed lightly, as they must not
208 * lie within the contiguous general code space.
210 #define CLEAR 256 /* table clear output code */
211 #define FIRST 257 /* first free entry */
214 #define MAXCODE(b) ((1 << (b)) - 1)
215 #define BADCODEM1 MAXCODE(BSD_MAX_BITS)
217 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
218 ^ (u_int32_t)(prefix))
219 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
220 + (u_int32_t)(prefix))
222 #define CHECK_GAP 10000 /* Ratio check interval */
224 #define RATIO_SCALE_LOG 8
225 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
226 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
228 /* Could include inlines.h */
231 #define IOLogDbg if (db->debug) printf
233 #define IOLogDbg if (db->debug) IOLog
237 * clear the dictionary
244 db->max_ent = FIRST-1;
245 db->n_bits = BSD_INIT_BITS;
249 db->incomp_count = 0;
250 db->checkpoint = CHECK_GAP;
254 * If the dictionary is full, then see if it is time to reset it.
256 * Compute the compression ratio using fixed-point arithmetic
257 * with 8 fractional bits.
259 * Since we have an infinite stream instead of a single file,
260 * watch only the local compression ratio.
262 * Since both peers must reset the dictionary at the same time even in
263 * the absence of CLEAR codes (while packets are incompressible), they
264 * must compute the same ratio.
266 static int /* 1=output CLEAR */
272 if (db->in_count >= db->checkpoint)
275 /* age the ratio by limiting the size of the counts */
276 if (db->in_count >= RATIO_MAX
277 || db->bytes_out >= RATIO_MAX) {
278 db->in_count -= db->in_count/4;
279 db->bytes_out -= db->bytes_out/4;
282 db->checkpoint = db->in_count + CHECK_GAP;
284 if (db->max_ent >= db->maxmaxcode) {
285 /* Reset the dictionary only if the ratio is worse,
286 * or if it looks as if it has been poisoned
287 * by incompressible data.
289 * This does not overflow, because
290 * db->in_count <= RATIO_MAX.
292 new_ratio = db->in_count << RATIO_SCALE_LOG;
293 if (db->bytes_out != 0)
294 new_ratio /= db->bytes_out;
296 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE)
301 db->ratio = new_ratio;
311 bsd_comp_stats(state, stats)
313 struct compstat *stats;
315 struct bsd_db *db = (struct bsd_db *) state;
318 stats->unc_bytes = db->uncomp_bytes;
319 stats->unc_packets = db->uncomp_count;
320 stats->comp_bytes = db->comp_bytes;
321 stats->comp_packets = db->comp_count;
322 stats->inc_bytes = db->incomp_bytes;
323 stats->inc_packets = db->incomp_count;
324 stats->ratio = db->in_count;
326 if (stats->ratio <= 0x7fffff)
335 * Reset state, as on a CCP ResetReq.
341 struct bsd_db *db = (struct bsd_db *) state;
349 * Allocate space for a (de) compressor.
352 bsd_alloc(options, opt_len, decomp)
357 u_int newlen, hsize, hshift, maxmaxcode;
360 if (opt_len != CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
361 || options[1] != CILEN_BSD_COMPRESS
362 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
364 bits = BSD_NBITS(options[2]);
366 case 9: /* needs 82152 for both directions */
367 case 10: /* needs 84144 */
368 case 11: /* needs 88240 */
369 case 12: /* needs 96432 */
373 case 13: /* needs 176784 */
377 case 14: /* needs 353744 */
381 case 15: /* needs 691440 */
385 case 16: /* needs 1366160--far too much, */
386 /* hsize = 69001; */ /* and 69001 is too big for cptr */
387 /* hshift = 8; */ /* in struct bsd_db */
393 maxmaxcode = MAXCODE(bits);
394 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
397 kret = (unsigned long) kalloc(Z_EXTRA + newlen);
400 db = (struct bsd_db *) ALIGN(kret);
401 bzero(db, sizeof(*db) - sizeof(db->dict));
402 db->kbase = (void *)kret;
409 kret = (unsigned long) kalloc(Z_EXTRA +
410 ((maxmaxcode+1) * sizeof(db->lens[0])));
412 kfree(db->kbase, newlen + Z_EXTRA);
415 db->lens = (u_int16_t *) ALIGN(kret);
416 db->klens = (void *) kret;
422 db->maxmaxcode = maxmaxcode;
432 struct bsd_db *db = (struct bsd_db *) state;
435 kfree(db->klens, ((db->maxmaxcode+1) * sizeof(db->lens[0])) + Z_EXTRA);
436 kfree(db->kbase, db->totlen + Z_EXTRA);
440 bsd_comp_alloc(options, opt_len)
444 return bsd_alloc(options, opt_len, 0);
448 bsd_decomp_alloc(options, opt_len)
452 return bsd_alloc(options, opt_len, 1);
456 * Initialize the database.
459 bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp)
462 int opt_len, unit, hdrlen, mru, debug, decomp;
466 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
467 || options[1] != CILEN_BSD_COMPRESS
468 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
469 || BSD_NBITS(options[2]) != db->maxbits
470 || decomp && db->lens == NULL)
480 db->dict[--i].codem1 = BADCODEM1;
481 db->dict[i].cptr = 0;
498 bsd_comp_init(state, options, opt_len, unit, hdrlen, debug)
501 int opt_len, unit, hdrlen, debug;
503 return bsd_init((struct bsd_db *) state, options, opt_len,
504 unit, hdrlen, 0, debug, 0);
508 bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
511 int opt_len, unit, hdrlen, mru, debug;
513 return bsd_init((struct bsd_db *) state, options, opt_len,
514 unit, hdrlen, mru, debug, 1);
520 * One change from the BSD compress command is that when the
521 * code size expands, we do not output a bunch of padding.
524 bsd_compress(state, mret, mp, slen, maxolen)
526 NETBUF_T *mret; /* return compressed netbuf here */
527 NETBUF_T mp; /* from here */
528 int slen; /* uncompressed length */
529 int maxolen; /* max compressed length */
531 struct bsd_db *db = (struct bsd_db *) state;
532 int hshift = db->hshift;
533 u_int max_ent = db->max_ent;
534 u_int n_bits = db->n_bits;
536 u_int32_t accm = 0, fcode;
537 struct bsd_dict *dictp;
539 int hval, disp, ent, ilen;
545 #define PUTBYTE(v) { \
549 if (wptr >= cp_end) \
554 #define OUTPUT(ent) { \
556 accm |= ((ent) << bitno); \
558 PUTBYTE(accm >> 24); \
561 } while (bitno <= 24); \
565 * If the protocol is not in the range we're interested in,
566 * just return without compressing the packet. If it is,
567 * the protocol becomes the first byte to compress.
569 rptr = mtod(mp, u_char *);
570 ent = PPP_PROTOCOL(rptr);
571 if (ent < CI_BSD_COMPRESS || ent > 0xf9) {
576 /* Don't generate compressed packets which are larger than
577 the uncompressed packet. */
581 /* Allocate one mbuf to start with. (don't forget space for the FCS!) */
582 m = NB_ALLOC(maxolen + db->hdrlen + PPP_FCSLEN);
586 NB_SHRINK_TOP(m, db->hdrlen);
587 NB_SHRINK_BOT(m, PPP_FCSLEN); /* grown by pppstart() */
588 wptr = mtod(m, u_char *);
589 cp_end = wptr + maxolen;
591 wptr = cp_end = NULL;
594 * Copy the PPP header over, changing the protocol,
595 * and install the 2-byte packet sequence number.
598 *wptr++ = PPP_ADDRESS(rptr); /* assumes the ppp header is */
599 *wptr++ = PPP_CONTROL(rptr); /* all in one mbuf */
600 *wptr++ = 0; /* change the protocol */
602 *wptr++ = db->seqno >> 8;
609 slen = NB_SIZE(mp) - PPP_HDRLEN;
614 fcode = BSD_KEY(ent, c);
615 hval = BSD_HASH(ent, c, hshift);
616 dictp = &db->dict[hval];
618 /* Validate and then check the entry. */
619 if (dictp->codem1 >= max_ent)
621 if (dictp->f.fcode == fcode) {
622 ent = dictp->codem1+1;
623 continue; /* found (prefix,suffix) */
626 /* continue probing until a match or invalid entry */
627 disp = (hval == 0) ? 1 : hval;
630 if (hval >= db->hsize)
632 dictp = &db->dict[hval];
633 if (dictp->codem1 >= max_ent)
635 } while (dictp->f.fcode != fcode);
636 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
640 OUTPUT(ent); /* output the prefix */
642 /* code -> hashtable */
643 if (max_ent < db->maxmaxcode) {
644 struct bsd_dict *dictp2;
645 /* expand code size if needed */
646 if (max_ent >= MAXCODE(n_bits))
647 db->n_bits = ++n_bits;
649 /* Invalidate old hash table entry using
650 * this code, and then take it over.
652 dictp2 = &db->dict[max_ent+1];
653 if (db->dict[dictp2->cptr].codem1 == max_ent)
654 db->dict[dictp2->cptr].codem1 = BADCODEM1;
656 dictp->codem1 = max_ent;
657 dictp->f.fcode = fcode;
659 db->max_ent = ++max_ent;
664 OUTPUT(ent); /* output the last code */
665 db->bytes_out += olen;
666 db->in_count += ilen;
668 ++db->bytes_out; /* count complete bytes */
671 OUTPUT(CLEAR); /* do not count the CLEAR */
674 * Pad dribble bits of last code with ones.
675 * Do not emit a completely useless byte of ones.
678 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
681 * Increase code size if we would have without the packet
682 * boundary and as the decompressor will.
684 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
687 db->uncomp_bytes += ilen;
689 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen || wptr == NULL) {
690 /* throw away the compressed stuff if it is longer than uncompressed */
696 db->incomp_bytes += ilen;
698 NB_SHRINK_BOT(m, NB_SIZE(m) - (wptr - mtod(m, u_char *)));
700 db->comp_bytes += olen + BSD_OVHD;
703 return olen + PPP_HDRLEN + BSD_OVHD;
710 * Update the "BSD Compress" dictionary on the receiver for
711 * incompressible data by pretending to compress the incoming data.
714 bsd_incomp(state, dmsg)
718 struct bsd_db *db = (struct bsd_db *) state;
719 u_int hshift = db->hshift;
720 u_int max_ent = db->max_ent;
721 u_int n_bits = db->n_bits;
722 struct bsd_dict *dictp;
725 u_int32_t hval, disp;
732 * If the protocol is not in the range we're interested in,
733 * just return without looking at the packet. If it is,
734 * the protocol becomes the first byte to "compress".
736 rptr = mtod(dmsg, u_char *);
737 ent = PPP_PROTOCOL(rptr);
738 if (ent < CI_BSD_COMPRESS || ent > 0xf9)
743 ilen = 1; /* count the protocol as 1 byte */
745 slen = NB_SIZE(dmsg) - PPP_HDRLEN;
750 fcode = BSD_KEY(ent, c);
751 hval = BSD_HASH(ent, c, hshift);
752 dictp = &db->dict[hval];
754 /* validate and then check the entry */
755 if (dictp->codem1 >= max_ent)
757 if (dictp->f.fcode == fcode) {
758 ent = dictp->codem1+1;
759 continue; /* found (prefix,suffix) */
762 /* continue probing until a match or invalid entry */
763 disp = (hval == 0) ? 1 : hval;
766 if (hval >= db->hsize)
768 dictp = &db->dict[hval];
769 if (dictp->codem1 >= max_ent)
771 } while (dictp->f.fcode != fcode);
772 ent = dictp->codem1+1;
773 continue; /* finally found (prefix,suffix) */
775 nomatch: /* output (count) the prefix */
778 /* code -> hashtable */
779 if (max_ent < db->maxmaxcode) {
780 struct bsd_dict *dictp2;
781 /* expand code size if needed */
782 if (max_ent >= MAXCODE(n_bits))
783 db->n_bits = ++n_bits;
785 /* Invalidate previous hash table entry
786 * assigned this code, and then take it over.
788 dictp2 = &db->dict[max_ent+1];
789 if (db->dict[dictp2->cptr].codem1 == max_ent)
790 db->dict[dictp2->cptr].codem1 = BADCODEM1;
792 dictp->codem1 = max_ent;
793 dictp->f.fcode = fcode;
795 db->max_ent = ++max_ent;
796 db->lens[max_ent] = db->lens[ent]+1;
799 } while (--slen != 0);
800 bitno += n_bits; /* output (count) the last code */
801 db->bytes_out += bitno/8;
802 db->in_count += ilen;
806 db->incomp_bytes += ilen;
808 db->uncomp_bytes += ilen;
810 /* Increase code size if we would have without the packet
811 * boundary and as the decompressor will.
813 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
819 * Decompress "BSD Compress".
821 * Because of patent problems, we return DECOMP_ERROR for errors
822 * found by inspecting the input data and for system problems, but
823 * DECOMP_FATALERROR for any errors which could possibly be said to
824 * be being detected "after" decompression. For DECOMP_ERROR,
825 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
826 * infringing a patent of Motorola's if we do, so we take CCP down
829 * Given that the frame has the correct sequence number and a good FCS,
830 * errors such as invalid codes in the input most likely indicate a
831 * bug, so we return DECOMP_FATALERROR for them in order to turn off
832 * compression, even though they are detected by inspecting the input.
835 bsd_decompress(state, cmp, dmpp)
839 struct bsd_db *db = (struct bsd_db *) state;
840 u_int max_ent = db->max_ent;
842 u_int bitno = 32; /* 1st valid bit in accm */
843 u_int n_bits = db->n_bits;
844 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
845 struct bsd_dict *dictp;
846 int explen, seq, len;
847 u_int incode, oldcode, finchar;
848 u_char *p, *rptr, *wptr;
850 int adrs, ctrl, ilen;
851 int space, codelen, extra, maxilen;
854 * Save the address/control from the PPP header
855 * and then get the sequence number.
858 rptr = mtod(cmp, u_char *);
859 adrs = PPP_ADDRESS(rptr);
860 ctrl = PPP_CONTROL(rptr);
862 len = NB_SIZE(cmp) - PPP_HDRLEN;
863 seq = (rptr[0] << 8) + rptr[1];
868 * Check the sequence number and give up if it differs from
869 * the value we're expecting.
871 if (seq != db->seqno) {
872 IOLogDbg("bsd_decomp%d: bad sequence # %d, expected %d\n",
873 db->unit, seq, db->seqno - 1);
879 * Allocate an netbuf large enough for all the data.
881 maxilen = db->mru + db->hdrlen + PPP_HDRLEN; /* no FCS */
882 dmp = NB_ALLOC(maxilen); /* XXX */
886 NB_SHRINK_TOP(dmp, db->hdrlen);
888 wptr = mtod(dmp, u_char *);
889 space = NB_SIZE(dmp) - PPP_HDRLEN + 1;
892 * Fill in the ppp header, but not the last byte of the protocol
893 * (that comes from the decompressed data).
898 wptr += PPP_HDRLEN - 1;
905 * Accumulate bytes until we have a complete code.
906 * Then get the next code, relying on the 32-bit,
907 * unsigned accm to mask the result.
910 accm |= *rptr++ << bitno;
912 if (tgtbitno < bitno)
914 incode = accm >> tgtbitno;
918 if (incode == CLEAR) {
920 * The dictionary must only be cleared at
921 * the end of a packet. But there could be an
922 * empty mbuf at the end.
926 IOLogDbg("bsd_decomp%d: bad CLEAR\n", db->unit);
927 return DECOMP_FATALERROR; /* probably a bug */
934 if (incode > max_ent + 2 || incode > db->maxmaxcode
935 || incode > max_ent && oldcode == CLEAR) {
937 IOLogDbg("bsd_decomp%d: bad code 0x%x oldcode=0x%x max_ent=0x%x explen=%d seqno=%d\n",
938 db->unit, incode, oldcode, max_ent, explen, db->seqno);
939 return DECOMP_FATALERROR; /* probably a bug */
942 /* Special case for KwKwK string. */
943 if (incode > max_ent) {
951 codelen = db->lens[finchar];
952 explen += codelen + extra;
953 if (explen > db->mru + 1) {
955 IOLogDbg("bsd_decomp%d: ran out of mru\n len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
956 db->unit, len, finchar, codelen, explen);
957 return DECOMP_FATALERROR;
961 * If we have no space left, then we've overflowed...
963 if ((space -= codelen + extra) < 0) {
964 IOLog("bsd_decompress%d: no space left in netbuf (need %d bytes)\n",
965 db->unit, (codelen + extra) - space);
971 * Decode this code and install it in the decompressed buffer.
973 p = (wptr += codelen);
974 while (finchar > LAST) {
975 dictp = &db->dict[db->dict[finchar].cptr];
977 if (--codelen <= 0 || dictp->codem1 != finchar-1)
980 *--p = dictp->f.hs.suffix;
981 finchar = dictp->f.hs.prefix;
987 IOLog("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
988 db->unit, codelen, incode, max_ent);
991 if (extra) /* the KwKwK case again */
995 * If not first code in a packet, and
996 * if not out of code space, then allocate a new code.
998 * Keep the hash table correct so it can be used
999 * with uncompressed packets.
1001 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1002 struct bsd_dict *dictp2;
1004 u_int32_t hval, disp;
1006 fcode = BSD_KEY(oldcode,finchar);
1007 hval = BSD_HASH(oldcode,finchar,db->hshift);
1008 dictp = &db->dict[hval];
1010 /* look for a free hash table entry */
1011 if (dictp->codem1 < max_ent) {
1012 disp = (hval == 0) ? 1 : hval;
1015 if (hval >= db->hsize)
1017 dictp = &db->dict[hval];
1018 } while (dictp->codem1 < max_ent);
1022 * Invalidate previous hash table entry
1023 * assigned this code, and then take it over
1025 dictp2 = &db->dict[max_ent+1];
1026 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1027 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1029 dictp2->cptr = hval;
1030 dictp->codem1 = max_ent;
1031 dictp->f.fcode = fcode;
1033 db->max_ent = ++max_ent;
1034 db->lens[max_ent] = db->lens[oldcode]+1;
1036 /* Expand code size if needed. */
1037 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1038 db->n_bits = ++n_bits;
1039 tgtbitno = 32-n_bits;
1044 NB_SHRINK_BOT(dmp, NB_SIZE(dmp) - (wptr - mtod(dmp, u_char *)));
1047 * Keep the checkpoint right so that incompressible packets
1048 * clear the dictionary at the right times.
1050 db->bytes_out += ilen;
1051 db->in_count += explen;
1052 if (bsd_check(db)) {
1053 IOLogDbg("bsd_decomp%d: peer should have cleared dictionary\n",
1058 db->comp_bytes += ilen + BSD_OVHD;
1060 db->uncomp_bytes += explen;
1068 IOLog("bsd_decomp%d: fell off end of chain 0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1069 db->unit, incode, finchar, db->dict[finchar].cptr, max_ent);
1070 } else if (dictp->codem1 != finchar-1) {
1071 IOLog("bsd_decomp%d: bad code chain 0x%x finchar=0x%x oldcode=0x%x cptr=0x%x codem1=0x%x\n",
1072 db->unit, incode, finchar, oldcode, db->dict[finchar].cptr,
1076 return DECOMP_FATALERROR;
1079 #endif /* DO_BSD_COMPRESS */