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 mbufs on BSD-derived systems.
43 * $FreeBSD: src/sys/net/bsd_comp.c,v 1.11.2.1 2002/04/14 21:41:48 luigi Exp $
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
50 #include <net/ppp_layer/ppp_defs.h>
52 #define PACKETPTR struct mbuf *
53 #include <net/ppp_layer/ppp_comp.h>
57 * PPP "BSD compress" compression
58 * The differences between this compression and the classic BSD LZW
59 * source are obvious from the requirement that the classic code worked
60 * with files while this handles arbitrarily long streams that
61 * are broken into packets. They are:
63 * When the code size expands, a block of junk is not emitted by
64 * the compressor and not expected by the decompressor.
66 * New codes are not necessarily assigned every time an old
67 * code is output by the compressor. This is because a packet
68 * end forces a code to be emitted, but does not imply that a
69 * new sequence has been seen.
71 * The compression ratio is checked at the first end of a packet
72 * after the appropriate gap. Besides simplifying and speeding
73 * things up, this makes it more likely that the transmitter
74 * and receiver will agree when the dictionary is cleared when
75 * compression is not going well.
79 * A dictionary for doing BSD compress.
82 int totlen; /* length of this structure */
83 u_int hsize; /* size of the hash table */
84 u_char hshift; /* used in hash function */
85 u_char n_bits; /* current bits/code */
89 u_int16_t seqno; /* sequence # of next packet */
90 u_int hdrlen; /* header length to preallocate */
92 u_int maxmaxcode; /* largest valid code */
93 u_int max_ent; /* largest code in use */
94 u_int in_count; /* uncompressed bytes, aged */
95 u_int bytes_out; /* compressed bytes, aged */
96 u_int ratio; /* recent compression ratio */
97 u_int checkpoint; /* when to next check the ratio */
98 u_int clear_count; /* times dictionary cleared */
99 u_int incomp_count; /* incompressible packets */
100 u_int incomp_bytes; /* incompressible bytes */
101 u_int uncomp_count; /* uncompressed packets */
102 u_int uncomp_bytes; /* uncompressed bytes */
103 u_int comp_count; /* compressed packets */
104 u_int comp_bytes; /* compressed bytes */
105 u_int16_t *lens; /* array of lengths of codes */
107 union { /* hash value */
110 #if BYTE_ORDER == LITTLE_ENDIAN
111 u_int16_t prefix; /* preceding code */
112 u_char suffix; /* last character of new code */
116 u_char suffix; /* last character of new code */
117 u_int16_t prefix; /* preceding code */
121 u_int16_t codem1; /* output of hash table -1 */
122 u_int16_t cptr; /* map code to hash table entry */
126 #define BSD_OVHD 2 /* BSD compress overhead/packet */
127 #define BSD_INIT_BITS BSD_MIN_BITS
129 static void bsd_clear (struct bsd_db *db);
130 static int bsd_check (struct bsd_db *db);
131 static void *bsd_alloc (u_char *options, int opt_len, int decomp);
132 static int bsd_init (struct bsd_db *db, u_char *options, int opt_len,
133 int unit, int hdrlen, int mru, int debug,
135 static void *bsd_comp_alloc (u_char *options, int opt_len);
136 static void *bsd_decomp_alloc (u_char *options, int opt_len);
137 static void bsd_free (void *state);
138 static int bsd_comp_init (void *state, u_char *options, int opt_len,
139 int unit, int hdrlen, int debug);
140 static int bsd_decomp_init (void *state, u_char *options, int opt_len,
141 int unit, int hdrlen, int mru, int debug);
142 static int bsd_compress (void *state, struct mbuf **mret,
143 struct mbuf *mp, int slen, int maxolen);
144 static void bsd_incomp (void *state, struct mbuf *dmsg);
145 static int bsd_decompress (void *state, struct mbuf *cmp,
147 static void bsd_reset (void *state);
148 static void bsd_comp_stats (void *state, struct compstat *stats);
151 * Procedures exported to if_ppp.c.
153 struct compressor ppp_bsd_compress = {
154 CI_BSD_COMPRESS, /* compress_proto */
155 bsd_comp_alloc, /* comp_alloc */
156 bsd_free, /* comp_free */
157 bsd_comp_init, /* comp_init */
158 bsd_reset, /* comp_reset */
159 bsd_compress, /* compress */
160 bsd_comp_stats, /* comp_stat */
161 bsd_decomp_alloc, /* decomp_alloc */
162 bsd_free, /* decomp_free */
163 bsd_decomp_init, /* decomp_init */
164 bsd_reset, /* decomp_reset */
165 bsd_decompress, /* decompress */
166 bsd_incomp, /* incomp */
167 bsd_comp_stats, /* decomp_stat */
171 * the next two codes should not be changed lightly, as they must not
172 * lie within the contiguous general code space.
174 #define CLEAR 256 /* table clear output code */
175 #define FIRST 257 /* first free entry */
178 #define MAXCODE(b) ((1 << (b)) - 1)
179 #define BADCODEM1 MAXCODE(BSD_MAX_BITS)
181 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
182 ^ (u_int32_t)(prefix))
183 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
184 + (u_int32_t)(prefix))
186 #define CHECK_GAP 10000 /* Ratio check interval */
188 #define RATIO_SCALE_LOG 8
189 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
190 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
193 * clear the dictionary
196 bsd_clear(struct bsd_db *db)
199 db->max_ent = FIRST-1;
200 db->n_bits = BSD_INIT_BITS;
204 db->checkpoint = CHECK_GAP;
208 * If the dictionary is full, then see if it is time to reset it.
210 * Compute the compression ratio using fixed-point arithmetic
211 * with 8 fractional bits.
213 * Since we have an infinite stream instead of a single file,
214 * watch only the local compression ratio.
216 * Since both peers must reset the dictionary at the same time even in
217 * the absence of CLEAR codes (while packets are incompressible), they
218 * must compute the same ratio.
220 * Return 1=output CLEAR
223 bsd_check(struct bsd_db *db)
227 if (db->in_count >= db->checkpoint) {
228 /* age the ratio by limiting the size of the counts */
229 if (db->in_count >= RATIO_MAX
230 || db->bytes_out >= RATIO_MAX) {
231 db->in_count -= db->in_count/4;
232 db->bytes_out -= db->bytes_out/4;
235 db->checkpoint = db->in_count + CHECK_GAP;
237 if (db->max_ent >= db->maxmaxcode) {
238 /* Reset the dictionary only if the ratio is worse,
239 * or if it looks as if it has been poisoned
240 * by incompressible data.
242 * This does not overflow, because
243 * db->in_count <= RATIO_MAX.
245 new_ratio = db->in_count << RATIO_SCALE_LOG;
246 if (db->bytes_out != 0)
247 new_ratio /= db->bytes_out;
249 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
253 db->ratio = new_ratio;
263 bsd_comp_stats(void *state, struct compstat *stats)
265 struct bsd_db *db = (struct bsd_db *) state;
268 stats->unc_bytes = db->uncomp_bytes;
269 stats->unc_packets = db->uncomp_count;
270 stats->comp_bytes = db->comp_bytes;
271 stats->comp_packets = db->comp_count;
272 stats->inc_bytes = db->incomp_bytes;
273 stats->inc_packets = db->incomp_count;
274 stats->ratio = db->in_count;
276 if (stats->ratio <= 0x7fffff)
285 * Reset state, as on a CCP ResetReq.
288 bsd_reset(void *state)
290 struct bsd_db *db = (struct bsd_db *) state;
298 * Allocate space for a (de) compressor.
301 bsd_alloc(u_char *options, int opt_len, int decomp)
304 u_int newlen, hsize, hshift, maxmaxcode;
307 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
308 || options[1] != CILEN_BSD_COMPRESS
309 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
311 bits = BSD_NBITS(options[2]);
313 case 9: /* needs 82152 for both directions */
314 case 10: /* needs 84144 */
315 case 11: /* needs 88240 */
316 case 12: /* needs 96432 */
320 case 13: /* needs 176784 */
324 case 14: /* needs 353744 */
328 case 15: /* needs 691440 */
332 case 16: /* needs 1366160--far too much, */
333 /* hsize = 69001; */ /* and 69001 is too big for cptr */
334 /* hshift = 8; */ /* in struct bsd_db */
340 maxmaxcode = MAXCODE(bits);
341 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
342 db = kmalloc(newlen, M_DEVBUF, M_WAITOK);
343 bzero(db, sizeof(*db) - sizeof(db->dict));
348 db->lens = kmalloc((maxmaxcode + 1) * sizeof(db->lens[0]), M_DEVBUF,
355 db->maxmaxcode = maxmaxcode;
362 bsd_free(void *state)
364 struct bsd_db *db = (struct bsd_db *) state;
367 kfree(db->lens, M_DEVBUF);
372 bsd_comp_alloc(u_char *options, int opt_len)
374 return bsd_alloc(options, opt_len, 0);
378 bsd_decomp_alloc(u_char *options, int opt_len)
380 return bsd_alloc(options, opt_len, 1);
384 * Initialize the database.
387 bsd_init(struct bsd_db *db, u_char *options, int opt_len, int unit,
388 int hdrlen, int mru, int debug, int decomp)
392 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
393 || options[1] != CILEN_BSD_COMPRESS
394 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
395 || BSD_NBITS(options[2]) != db->maxbits
396 || (decomp && db->lens == NULL))
406 db->dict[--i].codem1 = BADCODEM1;
407 db->dict[i].cptr = 0;
424 bsd_comp_init(void *state, u_char *options, int opt_len, int unit,
425 int hdrlen, int debug)
427 return bsd_init((struct bsd_db *) state, options, opt_len,
428 unit, hdrlen, 0, debug, 0);
432 bsd_decomp_init(void *state, u_char *options, int opt_len,
433 int unit, int hdrlen, int mru, int debug)
435 return bsd_init((struct bsd_db *) state, options, opt_len,
436 unit, hdrlen, mru, debug, 1);
442 * One change from the BSD compress command is that when the
443 * code size expands, we do not output a bunch of padding.
444 * **mret - return compressed mbuf chain here
446 * slen - uncompressed length
447 * maxolen - max compressed length
452 bsd_compress(void *state, struct mbuf **mret, struct mbuf *mp,
453 int slen, int maxolen)
455 struct bsd_db *db = (struct bsd_db *) state;
456 int hshift = db->hshift;
457 u_int max_ent = db->max_ent;
458 u_int n_bits = db->n_bits;
460 u_int32_t accm = 0, fcode;
461 struct bsd_dict *dictp;
463 int hval, disp, ent, ilen;
469 #define PUTBYTE(v) { \
473 if (wptr >= cp_end) { \
474 m->m_len = wptr - mtod(m, u_char *); \
475 MGET(m->m_next, MB_DONTWAIT, MT_DATA); \
479 if (maxolen - olen > MLEN) \
480 MCLGET(m, MB_DONTWAIT); \
481 wptr = mtod(m, u_char *); \
482 cp_end = wptr + M_TRAILINGSPACE(m); \
489 #define OUTPUT(ent) { \
491 accm |= ((ent) << bitno); \
493 PUTBYTE(accm >> 24); \
496 } while (bitno <= 24); \
500 * If the protocol is not in the range we're interested in,
501 * just return without compressing the packet. If it is,
502 * the protocol becomes the first byte to compress.
504 rptr = mtod(mp, u_char *);
505 ent = PPP_PROTOCOL(rptr);
506 if (ent < 0x21 || ent > 0xf9) {
511 /* Don't generate compressed packets which are larger than
512 the uncompressed packet. */
516 /* Allocate one mbuf to start with. */
517 MGET(m, MB_DONTWAIT, MT_DATA);
521 if (maxolen + db->hdrlen > MLEN)
522 MCLGET(m, MB_DONTWAIT);
523 m->m_data += db->hdrlen;
524 wptr = mtod(m, u_char *);
525 cp_end = wptr + M_TRAILINGSPACE(m);
527 wptr = cp_end = NULL;
530 * Copy the PPP header over, changing the protocol,
531 * and install the 2-byte packet sequence number.
534 *wptr++ = PPP_ADDRESS(rptr); /* assumes the ppp header is */
535 *wptr++ = PPP_CONTROL(rptr); /* all in one mbuf */
536 *wptr++ = 0; /* change the protocol */
538 *wptr++ = db->seqno >> 8;
545 slen = mp->m_len - PPP_HDRLEN;
552 rptr = mtod(mp, u_char *);
555 continue; /* handle 0-length buffers */
561 fcode = BSD_KEY(ent, c);
562 hval = BSD_HASH(ent, c, hshift);
563 dictp = &db->dict[hval];
565 /* Validate and then check the entry. */
566 if (dictp->codem1 >= max_ent)
568 if (dictp->f.fcode == fcode) {
569 ent = dictp->codem1+1;
570 continue; /* found (prefix,suffix) */
573 /* continue probing until a match or invalid entry */
574 disp = (hval == 0) ? 1 : hval;
577 if (hval >= db->hsize)
579 dictp = &db->dict[hval];
580 if (dictp->codem1 >= max_ent)
582 } while (dictp->f.fcode != fcode);
583 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
587 OUTPUT(ent); /* output the prefix */
589 /* code -> hashtable */
590 if (max_ent < db->maxmaxcode) {
591 struct bsd_dict *dictp2;
592 /* expand code size if needed */
593 if (max_ent >= MAXCODE(n_bits))
594 db->n_bits = ++n_bits;
596 /* Invalidate old hash table entry using
597 * this code, and then take it over.
599 dictp2 = &db->dict[max_ent+1];
600 if (db->dict[dictp2->cptr].codem1 == max_ent)
601 db->dict[dictp2->cptr].codem1 = BADCODEM1;
603 dictp->codem1 = max_ent;
604 dictp->f.fcode = fcode;
606 db->max_ent = ++max_ent;
611 OUTPUT(ent); /* output the last code */
612 db->bytes_out += olen;
613 db->in_count += ilen;
615 ++db->bytes_out; /* count complete bytes */
618 OUTPUT(CLEAR); /* do not count the CLEAR */
621 * Pad dribble bits of last code with ones.
622 * Do not emit a completely useless byte of ones.
625 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
628 m->m_len = wptr - mtod(m, u_char *);
633 * Increase code size if we would have without the packet
634 * boundary and as the decompressor will.
636 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
639 db->uncomp_bytes += ilen;
641 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen) {
642 /* throw away the compressed stuff if it is longer than uncompressed */
648 db->incomp_bytes += ilen;
651 db->comp_bytes += olen + BSD_OVHD;
654 return olen + PPP_HDRLEN + BSD_OVHD;
661 * Update the "BSD Compress" dictionary on the receiver for
662 * incompressible data by pretending to compress the incoming data.
665 bsd_incomp(void *state, struct mbuf *dmsg)
667 struct bsd_db *db = (struct bsd_db *) state;
668 u_int hshift = db->hshift;
669 u_int max_ent = db->max_ent;
670 u_int n_bits = db->n_bits;
671 struct bsd_dict *dictp;
674 u_int32_t hval, disp;
681 * If the protocol is not in the range we're interested in,
682 * just return without looking at the packet. If it is,
683 * the protocol becomes the first byte to "compress".
685 rptr = mtod(dmsg, u_char *);
686 ent = PPP_PROTOCOL(rptr);
687 if (ent < 0x21 || ent > 0xf9)
691 ilen = 1; /* count the protocol as 1 byte */
693 slen = dmsg->m_len - PPP_HDRLEN;
699 rptr = mtod(dmsg, u_char *);
707 fcode = BSD_KEY(ent, c);
708 hval = BSD_HASH(ent, c, hshift);
709 dictp = &db->dict[hval];
711 /* validate and then check the entry */
712 if (dictp->codem1 >= max_ent)
714 if (dictp->f.fcode == fcode) {
715 ent = dictp->codem1+1;
716 continue; /* found (prefix,suffix) */
719 /* continue probing until a match or invalid entry */
720 disp = (hval == 0) ? 1 : hval;
723 if (hval >= db->hsize)
725 dictp = &db->dict[hval];
726 if (dictp->codem1 >= max_ent)
728 } while (dictp->f.fcode != fcode);
729 ent = dictp->codem1+1;
730 continue; /* finally found (prefix,suffix) */
732 nomatch: /* output (count) the prefix */
735 /* code -> hashtable */
736 if (max_ent < db->maxmaxcode) {
737 struct bsd_dict *dictp2;
738 /* expand code size if needed */
739 if (max_ent >= MAXCODE(n_bits))
740 db->n_bits = ++n_bits;
742 /* Invalidate previous hash table entry
743 * assigned this code, and then take it over.
745 dictp2 = &db->dict[max_ent+1];
746 if (db->dict[dictp2->cptr].codem1 == max_ent)
747 db->dict[dictp2->cptr].codem1 = BADCODEM1;
749 dictp->codem1 = max_ent;
750 dictp->f.fcode = fcode;
752 db->max_ent = ++max_ent;
753 db->lens[max_ent] = db->lens[ent]+1;
756 } while (--slen != 0);
758 bitno += n_bits; /* output (count) the last code */
759 db->bytes_out += bitno/8;
760 db->in_count += ilen;
764 db->incomp_bytes += ilen;
766 db->uncomp_bytes += ilen;
768 /* Increase code size if we would have without the packet
769 * boundary and as the decompressor will.
771 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
777 * Decompress "BSD Compress".
779 * Because of patent problems, we return DECOMP_ERROR for errors
780 * found by inspecting the input data and for system problems, but
781 * DECOMP_FATALERROR for any errors which could possibly be said to
782 * be being detected "after" decompression. For DECOMP_ERROR,
783 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
784 * infringing a patent of Motorola's if we do, so we take CCP down
787 * Given that the frame has the correct sequence number and a good FCS,
788 * errors such as invalid codes in the input most likely indicate a
789 * bug, so we return DECOMP_FATALERROR for them in order to turn off
790 * compression, even though they are detected by inspecting the input.
793 bsd_decompress(void *state, struct mbuf *cmp, struct mbuf **dmpp)
795 struct bsd_db *db = (struct bsd_db *) state;
796 u_int max_ent = db->max_ent;
798 u_int bitno = 32; /* 1st valid bit in accm */
799 u_int n_bits = db->n_bits;
800 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
801 struct bsd_dict *dictp;
802 int explen, i, seq, len;
803 u_int incode, oldcode, finchar;
804 u_char *p, *rptr, *wptr;
805 struct mbuf *m, *dmp, *mret;
806 int adrs, ctrl, ilen;
807 int space, codelen, extra;
810 * Save the address/control from the PPP header
811 * and then get the sequence number.
814 rptr = mtod(cmp, u_char *);
815 adrs = PPP_ADDRESS(rptr);
816 ctrl = PPP_CONTROL(rptr);
818 len = cmp->m_len - PPP_HDRLEN;
820 for (i = 0; i < 2; ++i) {
825 rptr = mtod(cmp, u_char *);
828 seq = (seq << 8) + *rptr++;
833 * Check the sequence number and give up if it differs from
834 * the value we're expecting.
836 if (seq != db->seqno) {
838 kprintf("bsd_decomp%d: bad sequence # %d, expected %d\n",
839 db->unit, seq, db->seqno - 1);
845 * Allocate one mbuf to start with.
847 MGETHDR(dmp, MB_DONTWAIT, MT_DATA);
853 MCLGET(dmp, MB_DONTWAIT);
854 dmp->m_data += db->hdrlen;
855 wptr = mtod(dmp, u_char *);
856 space = M_TRAILINGSPACE(dmp) - PPP_HDRLEN + 1;
859 * Fill in the ppp header, but not the last byte of the protocol
860 * (that comes from the decompressed data).
865 wptr += PPP_HDRLEN - 1;
873 if (!cmp) /* quit at end of message */
875 rptr = mtod(cmp, u_char *);
878 continue; /* handle 0-length buffers */
882 * Accumulate bytes until we have a complete code.
883 * Then get the next code, relying on the 32-bit,
884 * unsigned accm to mask the result.
887 accm |= *rptr++ << bitno;
889 if (tgtbitno < bitno)
891 incode = accm >> tgtbitno;
895 if (incode == CLEAR) {
897 * The dictionary must only be cleared at
898 * the end of a packet. But there could be an
899 * empty mbuf at the end.
901 if (len > 0 || cmp->m_next != NULL) {
902 while ((cmp = cmp->m_next) != NULL)
907 kprintf("bsd_decomp%d: bad CLEAR\n", db->unit);
908 return DECOMP_FATALERROR; /* probably a bug */
916 if (incode > max_ent + 2 || incode > db->maxmaxcode
917 || (incode > max_ent && oldcode == CLEAR)) {
920 kprintf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
921 db->unit, incode, oldcode);
922 kprintf("max_ent=0x%x explen=%d seqno=%d\n",
923 max_ent, explen, db->seqno);
925 return DECOMP_FATALERROR; /* probably a bug */
928 /* Special case for KwKwK string. */
929 if (incode > max_ent) {
937 codelen = db->lens[finchar];
938 explen += codelen + extra;
939 if (explen > db->mru + 1) {
942 kprintf("bsd_decomp%d: ran out of mru\n", db->unit);
944 while ((cmp = cmp->m_next) != NULL)
946 kprintf(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
947 len, finchar, codelen, explen);
950 return DECOMP_FATALERROR;
954 * For simplicity, the decoded characters go in a single mbuf,
955 * so we allocate a single extra cluster mbuf if necessary.
957 if ((space -= codelen + extra) < 0) {
958 dmp->m_len = wptr - mtod(dmp, u_char *);
959 MGET(m, MB_DONTWAIT, MT_DATA);
967 MCLGET(m, MB_DONTWAIT);
968 space = M_TRAILINGSPACE(m) - (codelen + extra);
970 /* now that's what I call *compression*. */
975 wptr = mtod(dmp, u_char *);
979 * Decode this code and install it in the decompressed buffer.
981 p = (wptr += codelen);
982 while (finchar > LAST) {
983 dictp = &db->dict[db->dict[finchar].cptr];
985 if (--codelen <= 0 || dictp->codem1 != finchar-1)
988 *--p = dictp->f.hs.suffix;
989 finchar = dictp->f.hs.prefix;
995 kprintf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
996 db->unit, codelen, incode, max_ent);
999 if (extra) /* the KwKwK case again */
1003 * If not first code in a packet, and
1004 * if not out of code space, then allocate a new code.
1006 * Keep the hash table correct so it can be used
1007 * with uncompressed packets.
1009 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1010 struct bsd_dict *dictp2;
1012 u_int32_t hval, disp;
1014 fcode = BSD_KEY(oldcode,finchar);
1015 hval = BSD_HASH(oldcode,finchar,db->hshift);
1016 dictp = &db->dict[hval];
1018 /* look for a free hash table entry */
1019 if (dictp->codem1 < max_ent) {
1020 disp = (hval == 0) ? 1 : hval;
1023 if (hval >= db->hsize)
1025 dictp = &db->dict[hval];
1026 } while (dictp->codem1 < max_ent);
1030 * Invalidate previous hash table entry
1031 * assigned this code, and then take it over
1033 dictp2 = &db->dict[max_ent+1];
1034 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1035 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1037 dictp2->cptr = hval;
1038 dictp->codem1 = max_ent;
1039 dictp->f.fcode = fcode;
1041 db->max_ent = ++max_ent;
1042 db->lens[max_ent] = db->lens[oldcode]+1;
1044 /* Expand code size if needed. */
1045 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1046 db->n_bits = ++n_bits;
1047 tgtbitno = 32-n_bits;
1052 dmp->m_len = wptr - mtod(dmp, u_char *);
1055 * Keep the checkpoint right so that incompressible packets
1056 * clear the dictionary at the right times.
1058 db->bytes_out += ilen;
1059 db->in_count += explen;
1060 if (bsd_check(db) && db->debug) {
1061 kprintf("bsd_decomp%d: peer should have cleared dictionary\n",
1066 db->comp_bytes += ilen + BSD_OVHD;
1068 db->uncomp_bytes += explen;
1076 kprintf("bsd_decomp%d: fell off end of chain ", db->unit);
1077 kprintf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1078 incode, finchar, db->dict[finchar].cptr, max_ent);
1079 } else if (dictp->codem1 != finchar-1) {
1080 kprintf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1081 db->unit, incode, finchar);
1082 kprintf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
1083 db->dict[finchar].cptr, dictp->codem1);
1086 return DECOMP_FATALERROR;
1089 #endif /* DO_BSD_COMPRESS */