2 * Because this code is derived from the 4.3BSD compress source:
5 * Copyright (c) 1985, 1986 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
9 * James A. Woods, derived from original work by Spencer Thomas
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * This version is for use with mbufs on BSD-derived systems.
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/malloc.h>
50 #include <sys/mutex.h>
52 #include <net/ppp_defs.h>
54 #define PACKETPTR struct mbuf *
55 #include <net/ppp_comp.h>
57 #include <security/mac/mac_framework.h>
60 * PPP "BSD compress" compression
61 * The differences between this compression and the classic BSD LZW
62 * source are obvious from the requirement that the classic code worked
63 * with files while this handles arbitrarily long streams that
64 * are broken into packets. They are:
66 * When the code size expands, a block of junk is not emitted by
67 * the compressor and not expected by the decompressor.
69 * New codes are not necessarily assigned every time an old
70 * code is output by the compressor. This is because a packet
71 * end forces a code to be emitted, but does not imply that a
72 * new sequence has been seen.
74 * The compression ratio is checked at the first end of a packet
75 * after the appropriate gap. Besides simplifying and speeding
76 * things up, this makes it more likely that the transmitter
77 * and receiver will agree when the dictionary is cleared when
78 * compression is not going well.
82 * A dictionary for doing BSD compress.
85 int totlen; /* length of this structure */
86 u_int hsize; /* size of the hash table */
87 u_char hshift; /* used in hash function */
88 u_char n_bits; /* current bits/code */
92 u_int16_t seqno; /* sequence # of next packet */
93 u_int hdrlen; /* header length to preallocate */
95 u_int maxmaxcode; /* largest valid code */
96 u_int max_ent; /* largest code in use */
97 u_int in_count; /* uncompressed bytes, aged */
98 u_int bytes_out; /* compressed bytes, aged */
99 u_int ratio; /* recent compression ratio */
100 u_int checkpoint; /* when to next check the ratio */
101 u_int clear_count; /* times dictionary cleared */
102 u_int incomp_count; /* incompressible packets */
103 u_int incomp_bytes; /* incompressible bytes */
104 u_int uncomp_count; /* uncompressed packets */
105 u_int uncomp_bytes; /* uncompressed bytes */
106 u_int comp_count; /* compressed packets */
107 u_int comp_bytes; /* compressed bytes */
108 u_int16_t *lens; /* array of lengths of codes */
110 union { /* hash value */
113 #if BYTE_ORDER == LITTLE_ENDIAN
114 u_int16_t prefix; /* preceding code */
115 u_char suffix; /* last character of new code */
119 u_char suffix; /* last character of new code */
120 u_int16_t prefix; /* preceding code */
124 u_int16_t codem1; /* output of hash table -1 */
125 u_int16_t cptr; /* map code to hash table entry */
129 #define BSD_OVHD 2 /* BSD compress overhead/packet */
130 #define BSD_INIT_BITS BSD_MIN_BITS
132 static void bsd_clear(struct bsd_db *db);
133 static int bsd_check(struct bsd_db *db);
134 static void *bsd_alloc(u_char *options, int opt_len, int decomp);
135 static int bsd_init(struct bsd_db *db, u_char *options, int opt_len,
136 int unit, int hdrlen, int mru, int debug, int decomp);
137 static void *bsd_comp_alloc(u_char *options, int opt_len);
138 static void *bsd_decomp_alloc(u_char *options, int opt_len);
139 static void bsd_free(void *state);
140 static int bsd_comp_init(void *state, u_char *options, int opt_len,
141 int unit, int hdrlen, int debug);
142 static int bsd_decomp_init(void *state, u_char *options, int opt_len,
143 int unit, int hdrlen, int mru, int debug);
144 static int bsd_compress(void *state, struct mbuf **mret, struct mbuf *mp,
145 int slen, int maxolen);
146 static void bsd_incomp(void *state, struct mbuf *dmsg);
147 static int bsd_decompress(void *state, struct mbuf *cmp,
149 static void bsd_reset(void *state);
150 static void bsd_comp_stats(void *state, struct compstat *stats);
153 * Procedures exported to if_ppp.c.
155 struct compressor ppp_bsd_compress = {
156 CI_BSD_COMPRESS, /* compress_proto */
157 bsd_comp_alloc, /* comp_alloc */
158 bsd_free, /* comp_free */
159 bsd_comp_init, /* comp_init */
160 bsd_reset, /* comp_reset */
161 bsd_compress, /* compress */
162 bsd_comp_stats, /* comp_stat */
163 bsd_decomp_alloc, /* decomp_alloc */
164 bsd_free, /* decomp_free */
165 bsd_decomp_init, /* decomp_init */
166 bsd_reset, /* decomp_reset */
167 bsd_decompress, /* decompress */
168 bsd_incomp, /* incomp */
169 bsd_comp_stats, /* decomp_stat */
173 * the next two codes should not be changed lightly, as they must not
174 * lie within the contiguous general code space.
176 #define CLEAR 256 /* table clear output code */
177 #define FIRST 257 /* first free entry */
180 #define MAXCODE(b) ((1 << (b)) - 1)
181 #define BADCODEM1 MAXCODE(BSD_MAX_BITS)
183 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
184 ^ (u_int32_t)(prefix))
185 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
186 + (u_int32_t)(prefix))
188 #define CHECK_GAP 10000 /* Ratio check interval */
190 #define RATIO_SCALE_LOG 8
191 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
192 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
195 * clear the dictionary
202 db->max_ent = FIRST-1;
203 db->n_bits = BSD_INIT_BITS;
207 db->checkpoint = CHECK_GAP;
211 * If the dictionary is full, then see if it is time to reset it.
213 * Compute the compression ratio using fixed-point arithmetic
214 * with 8 fractional bits.
216 * Since we have an infinite stream instead of a single file,
217 * watch only the local compression ratio.
219 * Since both peers must reset the dictionary at the same time even in
220 * the absence of CLEAR codes (while packets are incompressible), they
221 * must compute the same ratio.
223 static int /* 1=output CLEAR */
229 if (db->in_count >= db->checkpoint) {
230 /* age the ratio by limiting the size of the counts */
231 if (db->in_count >= RATIO_MAX
232 || db->bytes_out >= RATIO_MAX) {
233 db->in_count -= db->in_count/4;
234 db->bytes_out -= db->bytes_out/4;
237 db->checkpoint = db->in_count + CHECK_GAP;
239 if (db->max_ent >= db->maxmaxcode) {
240 /* Reset the dictionary only if the ratio is worse,
241 * or if it looks as if it has been poisoned
242 * by incompressible data.
244 * This does not overflow, because
245 * db->in_count <= RATIO_MAX.
247 new_ratio = db->in_count << RATIO_SCALE_LOG;
248 if (db->bytes_out != 0)
249 new_ratio /= db->bytes_out;
251 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
255 db->ratio = new_ratio;
265 bsd_comp_stats(state, stats)
267 struct compstat *stats;
269 struct bsd_db *db = (struct bsd_db *) state;
272 stats->unc_bytes = db->uncomp_bytes;
273 stats->unc_packets = db->uncomp_count;
274 stats->comp_bytes = db->comp_bytes;
275 stats->comp_packets = db->comp_count;
276 stats->inc_bytes = db->incomp_bytes;
277 stats->inc_packets = db->incomp_count;
278 stats->ratio = db->in_count;
280 if (stats->ratio <= 0x7fffff)
289 * Reset state, as on a CCP ResetReq.
295 struct bsd_db *db = (struct bsd_db *) state;
303 * Allocate space for a (de) compressor.
306 bsd_alloc(options, opt_len, decomp)
311 u_int newlen, hsize, hshift, maxmaxcode;
314 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
315 || options[1] != CILEN_BSD_COMPRESS
316 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
318 bits = BSD_NBITS(options[2]);
320 case 9: /* needs 82152 for both directions */
321 case 10: /* needs 84144 */
322 case 11: /* needs 88240 */
323 case 12: /* needs 96432 */
327 case 13: /* needs 176784 */
331 case 14: /* needs 353744 */
335 case 15: /* needs 691440 */
339 case 16: /* needs 1366160--far too much, */
340 /* hsize = 69001; */ /* and 69001 is too big for cptr */
341 /* hshift = 8; */ /* in struct bsd_db */
347 maxmaxcode = MAXCODE(bits);
348 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
349 MALLOC(db, struct bsd_db *, newlen, M_DEVBUF, M_NOWAIT);
352 bzero(db, sizeof(*db) - sizeof(db->dict));
357 MALLOC(db->lens, u_int16_t *, (maxmaxcode+1) * sizeof(db->lens[0]),
368 db->maxmaxcode = maxmaxcode;
378 struct bsd_db *db = (struct bsd_db *) state;
381 free(db->lens, M_DEVBUF);
386 bsd_comp_alloc(options, opt_len)
390 return bsd_alloc(options, opt_len, 0);
394 bsd_decomp_alloc(options, opt_len)
398 return bsd_alloc(options, opt_len, 1);
402 * Initialize the database.
405 bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp)
408 int opt_len, unit, hdrlen, mru, debug, decomp;
412 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
413 || options[1] != CILEN_BSD_COMPRESS
414 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
415 || BSD_NBITS(options[2]) != db->maxbits
416 || (decomp && db->lens == NULL))
426 db->dict[--i].codem1 = BADCODEM1;
427 db->dict[i].cptr = 0;
444 bsd_comp_init(state, options, opt_len, unit, hdrlen, debug)
447 int opt_len, unit, hdrlen, debug;
449 return bsd_init((struct bsd_db *) state, options, opt_len,
450 unit, hdrlen, 0, debug, 0);
454 bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
457 int opt_len, unit, hdrlen, mru, debug;
459 return bsd_init((struct bsd_db *) state, options, opt_len,
460 unit, hdrlen, mru, debug, 1);
466 * One change from the BSD compress command is that when the
467 * code size expands, we do not output a bunch of padding.
469 static int /* new slen */
470 bsd_compress(state, mret, mp, slen, maxolen)
472 struct mbuf **mret; /* return compressed mbuf chain here */
473 struct mbuf *mp; /* from here */
474 int slen; /* uncompressed length */
475 int maxolen; /* max compressed length */
477 struct bsd_db *db = (struct bsd_db *) state;
478 int hshift = db->hshift;
479 u_int max_ent = db->max_ent;
480 u_int n_bits = db->n_bits;
482 u_int32_t accm = 0, fcode;
483 struct bsd_dict *dictp;
485 int hval, disp, ent, ilen;
491 #define PUTBYTE(v) { \
495 if (wptr >= cp_end) { \
496 m->m_len = wptr - mtod(m, u_char *); \
497 MGET(m->m_next, M_DONTWAIT, MT_DATA); \
501 if (maxolen - olen > MLEN) \
502 MCLGET(m, M_DONTWAIT); \
503 wptr = mtod(m, u_char *); \
504 cp_end = wptr + M_TRAILINGSPACE(m); \
511 #define OUTPUT(ent) { \
513 accm |= ((ent) << bitno); \
515 PUTBYTE(accm >> 24); \
518 } while (bitno <= 24); \
522 * If the protocol is not in the range we're interested in,
523 * just return without compressing the packet. If it is,
524 * the protocol becomes the first byte to compress.
526 rptr = mtod(mp, u_char *);
527 ent = PPP_PROTOCOL(rptr);
528 if (ent < 0x21 || ent > 0xf9) {
533 /* Don't generate compressed packets which are larger than
534 the uncompressed packet. */
538 /* Allocate one mbuf to start with. */
539 MGET(m, M_DONTWAIT, MT_DATA);
543 if (maxolen + db->hdrlen > MLEN)
544 MCLGET(m, M_DONTWAIT);
545 m->m_data += db->hdrlen;
546 wptr = mtod(m, u_char *);
547 cp_end = wptr + M_TRAILINGSPACE(m);
549 wptr = cp_end = NULL;
552 * Copy the PPP header over, changing the protocol,
553 * and install the 2-byte packet sequence number.
556 *wptr++ = PPP_ADDRESS(rptr); /* assumes the ppp header is */
557 *wptr++ = PPP_CONTROL(rptr); /* all in one mbuf */
558 *wptr++ = 0; /* change the protocol */
560 *wptr++ = db->seqno >> 8;
567 slen = mp->m_len - PPP_HDRLEN;
574 rptr = mtod(mp, u_char *);
577 continue; /* handle 0-length buffers */
583 fcode = BSD_KEY(ent, c);
584 hval = BSD_HASH(ent, c, hshift);
585 dictp = &db->dict[hval];
587 /* Validate and then check the entry. */
588 if (dictp->codem1 >= max_ent)
590 if (dictp->f.fcode == fcode) {
591 ent = dictp->codem1+1;
592 continue; /* found (prefix,suffix) */
595 /* continue probing until a match or invalid entry */
596 disp = (hval == 0) ? 1 : hval;
599 if (hval >= db->hsize)
601 dictp = &db->dict[hval];
602 if (dictp->codem1 >= max_ent)
604 } while (dictp->f.fcode != fcode);
605 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
609 OUTPUT(ent); /* output the prefix */
611 /* code -> hashtable */
612 if (max_ent < db->maxmaxcode) {
613 struct bsd_dict *dictp2;
614 /* expand code size if needed */
615 if (max_ent >= MAXCODE(n_bits))
616 db->n_bits = ++n_bits;
618 /* Invalidate old hash table entry using
619 * this code, and then take it over.
621 dictp2 = &db->dict[max_ent+1];
622 if (db->dict[dictp2->cptr].codem1 == max_ent)
623 db->dict[dictp2->cptr].codem1 = BADCODEM1;
625 dictp->codem1 = max_ent;
626 dictp->f.fcode = fcode;
628 db->max_ent = ++max_ent;
633 OUTPUT(ent); /* output the last code */
634 db->bytes_out += olen;
635 db->in_count += ilen;
637 ++db->bytes_out; /* count complete bytes */
640 OUTPUT(CLEAR); /* do not count the CLEAR */
643 * Pad dribble bits of last code with ones.
644 * Do not emit a completely useless byte of ones.
647 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
650 m->m_len = wptr - mtod(m, u_char *);
655 * Increase code size if we would have without the packet
656 * boundary and as the decompressor will.
658 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
661 db->uncomp_bytes += ilen;
663 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen) {
664 /* throw away the compressed stuff if it is longer than uncompressed */
670 db->incomp_bytes += ilen;
673 db->comp_bytes += olen + BSD_OVHD;
676 return olen + PPP_HDRLEN + BSD_OVHD;
683 * Update the "BSD Compress" dictionary on the receiver for
684 * incompressible data by pretending to compress the incoming data.
687 bsd_incomp(state, dmsg)
691 struct bsd_db *db = (struct bsd_db *) state;
692 u_int hshift = db->hshift;
693 u_int max_ent = db->max_ent;
694 u_int n_bits = db->n_bits;
695 struct bsd_dict *dictp;
698 u_int32_t hval, disp;
705 * If the protocol is not in the range we're interested in,
706 * just return without looking at the packet. If it is,
707 * the protocol becomes the first byte to "compress".
709 rptr = mtod(dmsg, u_char *);
710 ent = PPP_PROTOCOL(rptr);
711 if (ent < 0x21 || ent > 0xf9)
715 ilen = 1; /* count the protocol as 1 byte */
717 slen = dmsg->m_len - PPP_HDRLEN;
723 rptr = mtod(dmsg, u_char *);
731 fcode = BSD_KEY(ent, c);
732 hval = BSD_HASH(ent, c, hshift);
733 dictp = &db->dict[hval];
735 /* validate and then check the entry */
736 if (dictp->codem1 >= max_ent)
738 if (dictp->f.fcode == fcode) {
739 ent = dictp->codem1+1;
740 continue; /* found (prefix,suffix) */
743 /* continue probing until a match or invalid entry */
744 disp = (hval == 0) ? 1 : hval;
747 if (hval >= db->hsize)
749 dictp = &db->dict[hval];
750 if (dictp->codem1 >= max_ent)
752 } while (dictp->f.fcode != fcode);
753 ent = dictp->codem1+1;
754 continue; /* finally found (prefix,suffix) */
756 nomatch: /* output (count) the prefix */
759 /* code -> hashtable */
760 if (max_ent < db->maxmaxcode) {
761 struct bsd_dict *dictp2;
762 /* expand code size if needed */
763 if (max_ent >= MAXCODE(n_bits))
764 db->n_bits = ++n_bits;
766 /* Invalidate previous hash table entry
767 * assigned this code, and then take it over.
769 dictp2 = &db->dict[max_ent+1];
770 if (db->dict[dictp2->cptr].codem1 == max_ent)
771 db->dict[dictp2->cptr].codem1 = BADCODEM1;
773 dictp->codem1 = max_ent;
774 dictp->f.fcode = fcode;
776 db->max_ent = ++max_ent;
777 db->lens[max_ent] = db->lens[ent]+1;
780 } while (--slen != 0);
782 bitno += n_bits; /* output (count) the last code */
783 db->bytes_out += bitno/8;
784 db->in_count += ilen;
788 db->incomp_bytes += ilen;
790 db->uncomp_bytes += ilen;
792 /* Increase code size if we would have without the packet
793 * boundary and as the decompressor will.
795 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
801 * Decompress "BSD Compress".
803 * Because of patent problems, we return DECOMP_ERROR for errors
804 * found by inspecting the input data and for system problems, but
805 * DECOMP_FATALERROR for any errors which could possibly be said to
806 * be being detected "after" decompression. For DECOMP_ERROR,
807 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
808 * infringing a patent of Motorola's if we do, so we take CCP down
811 * Given that the frame has the correct sequence number and a good FCS,
812 * errors such as invalid codes in the input most likely indicate a
813 * bug, so we return DECOMP_FATALERROR for them in order to turn off
814 * compression, even though they are detected by inspecting the input.
817 bsd_decompress(state, cmp, dmpp)
819 struct mbuf *cmp, **dmpp;
821 struct bsd_db *db = (struct bsd_db *) state;
822 u_int max_ent = db->max_ent;
824 u_int bitno = 32; /* 1st valid bit in accm */
825 u_int n_bits = db->n_bits;
826 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
827 struct bsd_dict *dictp;
828 int explen, i, seq, len;
829 u_int incode, oldcode, finchar;
830 u_char *p, *rptr, *wptr;
831 struct mbuf *m, *dmp, *mret;
832 int adrs, ctrl, ilen;
833 int space, codelen, extra;
836 * Save the address/control from the PPP header
837 * and then get the sequence number.
840 rptr = mtod(cmp, u_char *);
841 adrs = PPP_ADDRESS(rptr);
842 ctrl = PPP_CONTROL(rptr);
844 len = cmp->m_len - PPP_HDRLEN;
846 for (i = 0; i < 2; ++i) {
851 rptr = mtod(cmp, u_char *);
854 seq = (seq << 8) + *rptr++;
859 * Check the sequence number and give up if it differs from
860 * the value we're expecting.
862 if (seq != db->seqno) {
864 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
865 db->unit, seq, db->seqno - 1);
871 * Allocate one mbuf to start with.
873 MGETHDR(dmp, M_DONTWAIT, MT_DATA);
879 MCLGET(dmp, M_DONTWAIT);
880 dmp->m_data += db->hdrlen;
881 wptr = mtod(dmp, u_char *);
882 space = M_TRAILINGSPACE(dmp) - PPP_HDRLEN + 1;
884 mac_copy_mbuf(cmp, dmp);
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;
902 if (!cmp) /* quit at end of message */
904 rptr = mtod(cmp, u_char *);
907 continue; /* handle 0-length buffers */
911 * Accumulate bytes until we have a complete code.
912 * Then get the next code, relying on the 32-bit,
913 * unsigned accm to mask the result.
916 accm |= *rptr++ << bitno;
918 if (tgtbitno < bitno)
920 incode = accm >> tgtbitno;
924 if (incode == CLEAR) {
926 * The dictionary must only be cleared at
927 * the end of a packet. But there could be an
928 * empty mbuf at the end.
930 if (len > 0 || cmp->m_next != NULL) {
931 while ((cmp = cmp->m_next) != NULL)
936 printf("bsd_decomp%d: bad CLEAR\n", db->unit);
937 return DECOMP_FATALERROR; /* probably a bug */
945 if (incode > max_ent + 2 || incode > db->maxmaxcode
946 || (incode > max_ent && oldcode == CLEAR)) {
949 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
950 db->unit, incode, oldcode);
951 printf("max_ent=0x%x explen=%d seqno=%d\n",
952 max_ent, explen, db->seqno);
954 return DECOMP_FATALERROR; /* probably a bug */
957 /* Special case for KwKwK string. */
958 if (incode > max_ent) {
966 codelen = db->lens[finchar];
967 explen += codelen + extra;
968 if (explen > db->mru + 1) {
971 printf("bsd_decomp%d: ran out of mru\n", db->unit);
973 while ((cmp = cmp->m_next) != NULL)
975 printf(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
976 len, finchar, codelen, explen);
979 return DECOMP_FATALERROR;
983 * For simplicity, the decoded characters go in a single mbuf,
984 * so we allocate a single extra cluster mbuf if necessary.
986 if ((space -= codelen + extra) < 0) {
987 dmp->m_len = wptr - mtod(dmp, u_char *);
988 MGET(m, M_DONTWAIT, MT_DATA);
996 MCLGET(m, M_DONTWAIT);
997 space = M_TRAILINGSPACE(m) - (codelen + extra);
999 /* now that's what I call *compression*. */
1001 return DECOMP_ERROR;
1004 wptr = mtod(dmp, u_char *);
1008 * Decode this code and install it in the decompressed buffer.
1010 p = (wptr += codelen);
1011 while (finchar > LAST) {
1012 dictp = &db->dict[db->dict[finchar].cptr];
1014 if (--codelen <= 0 || dictp->codem1 != finchar-1)
1017 *--p = dictp->f.hs.suffix;
1018 finchar = dictp->f.hs.prefix;
1024 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1025 db->unit, codelen, incode, max_ent);
1028 if (extra) /* the KwKwK case again */
1032 * If not first code in a packet, and
1033 * if not out of code space, then allocate a new code.
1035 * Keep the hash table correct so it can be used
1036 * with uncompressed packets.
1038 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1039 struct bsd_dict *dictp2;
1041 u_int32_t hval, disp;
1043 fcode = BSD_KEY(oldcode,finchar);
1044 hval = BSD_HASH(oldcode,finchar,db->hshift);
1045 dictp = &db->dict[hval];
1047 /* look for a free hash table entry */
1048 if (dictp->codem1 < max_ent) {
1049 disp = (hval == 0) ? 1 : hval;
1052 if (hval >= db->hsize)
1054 dictp = &db->dict[hval];
1055 } while (dictp->codem1 < max_ent);
1059 * Invalidate previous hash table entry
1060 * assigned this code, and then take it over
1062 dictp2 = &db->dict[max_ent+1];
1063 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1064 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1066 dictp2->cptr = hval;
1067 dictp->codem1 = max_ent;
1068 dictp->f.fcode = fcode;
1070 db->max_ent = ++max_ent;
1071 db->lens[max_ent] = db->lens[oldcode]+1;
1073 /* Expand code size if needed. */
1074 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1075 db->n_bits = ++n_bits;
1076 tgtbitno = 32-n_bits;
1081 dmp->m_len = wptr - mtod(dmp, u_char *);
1084 * Keep the checkpoint right so that incompressible packets
1085 * clear the dictionary at the right times.
1087 db->bytes_out += ilen;
1088 db->in_count += explen;
1089 if (bsd_check(db) && db->debug) {
1090 printf("bsd_decomp%d: peer should have cleared dictionary\n",
1095 db->comp_bytes += ilen + BSD_OVHD;
1097 db->uncomp_bytes += explen;
1105 printf("bsd_decomp%d: fell off end of chain ", db->unit);
1106 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1107 incode, finchar, db->dict[finchar].cptr, max_ent);
1108 } else if (dictp->codem1 != finchar-1) {
1109 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1110 db->unit, incode, finchar);
1111 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
1112 db->dict[finchar].cptr, dictp->codem1);
1115 return DECOMP_FATALERROR;