1 /* $Id: bsd-comp.c,v 1.3 1996/04/04 02:48:56 paulus Exp $ */
3 /* Because this code is derived from the 4.3BSD compress source:
6 * Copyright (c) 1985, 1986 The Regents of the University of California.
9 * This code is derived from software contributed to Berkeley by
10 * James A. Woods, derived from original work by Spencer Thomas
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * This version is for use with mbufs on BSD-derived systems.
46 #include <sys/param.h>
47 #include <sys/types.h>
48 #include <sys/systm.h>
50 #include <sys/socket.h>
52 #include <net/if_types.h>
53 #include <net/ppp_defs.h>
54 #include <net/if_ppp.h>
56 #define PACKETPTR struct mbuf *
57 #include <net/ppp-comp.h>
61 * PPP "BSD compress" compression
62 * The differences between this compression and the classic BSD LZW
63 * source are obvious from the requirement that the classic code worked
64 * with files while this handles arbitrarily long streams that
65 * are broken into packets. They are:
67 * When the code size expands, a block of junk is not emitted by
68 * the compressor and not expected by the decompressor.
70 * New codes are not necessarily assigned every time an old
71 * code is output by the compressor. This is because a packet
72 * end forces a code to be emitted, but does not imply that a
73 * new sequence has been seen.
75 * The compression ratio is checked at the first end of a packet
76 * after the appropriate gap. Besides simplifying and speeding
77 * things up, this makes it more likely that the transmitter
78 * and receiver will agree when the dictionary is cleared when
79 * compression is not going well.
83 * A dictionary for doing BSD compress.
86 int totlen; /* length of this structure */
87 u_int hsize; /* size of the hash table */
88 u_char hshift; /* used in hash function */
89 u_char n_bits; /* current bits/code */
93 u_int16_t seqno; /* sequence # of next packet */
94 u_int hdrlen; /* header length to preallocate */
96 u_int maxmaxcode; /* largest valid code */
97 u_int max_ent; /* largest code in use */
98 u_int in_count; /* uncompressed bytes, aged */
99 u_int bytes_out; /* compressed bytes, aged */
100 u_int ratio; /* recent compression ratio */
101 u_int checkpoint; /* when to next check the ratio */
102 u_int clear_count; /* times dictionary cleared */
103 u_int incomp_count; /* incompressible packets */
104 u_int incomp_bytes; /* incompressible bytes */
105 u_int uncomp_count; /* uncompressed packets */
106 u_int uncomp_bytes; /* uncompressed bytes */
107 u_int comp_count; /* compressed packets */
108 u_int comp_bytes; /* compressed bytes */
109 u_int16_t *lens; /* array of lengths of codes */
111 union { /* hash value */
114 #if BYTE_ORDER == LITTLE_ENDIAN
115 u_int16_t prefix; /* preceding code */
116 u_char suffix; /* last character of new code */
120 u_char suffix; /* last character of new code */
121 u_int16_t prefix; /* preceding code */
125 u_int16_t codem1; /* output of hash table -1 */
126 u_int16_t cptr; /* map code to hash table entry */
130 #define BSD_OVHD 2 /* BSD compress overhead/packet */
131 #define BSD_INIT_BITS BSD_MIN_BITS
133 static void *bsd_comp_alloc __P((u_char *options, int opt_len));
134 static void *bsd_decomp_alloc __P((u_char *options, int opt_len));
135 static void bsd_free __P((void *state));
136 static int bsd_comp_init __P((void *state, u_char *options, int opt_len,
137 int unit, int hdrlen, int debug));
138 static int bsd_decomp_init __P((void *state, u_char *options, int opt_len,
139 int unit, int hdrlen, int mru, int debug));
140 static int bsd_compress __P((void *state, struct mbuf **mret,
141 struct mbuf *mp, int slen, int maxolen));
142 static void bsd_incomp __P((void *state, struct mbuf *dmsg));
143 static int bsd_decompress __P((void *state, struct mbuf *cmp,
144 struct mbuf **dmpp));
145 static void bsd_reset __P((void *state));
146 static void bsd_comp_stats __P((void *state, struct compstat *stats));
149 * Procedures exported to if_ppp.c.
151 struct compressor ppp_bsd_compress = {
152 CI_BSD_COMPRESS, /* compress_proto */
153 bsd_comp_alloc, /* comp_alloc */
154 bsd_free, /* comp_free */
155 bsd_comp_init, /* comp_init */
156 bsd_reset, /* comp_reset */
157 bsd_compress, /* compress */
158 bsd_comp_stats, /* comp_stat */
159 bsd_decomp_alloc, /* decomp_alloc */
160 bsd_free, /* decomp_free */
161 bsd_decomp_init, /* decomp_init */
162 bsd_reset, /* decomp_reset */
163 bsd_decompress, /* decompress */
164 bsd_incomp, /* incomp */
165 bsd_comp_stats, /* decomp_stat */
169 * the next two codes should not be changed lightly, as they must not
170 * lie within the contiguous general code space.
172 #define CLEAR 256 /* table clear output code */
173 #define FIRST 257 /* first free entry */
176 #define MAXCODE(b) ((1 << (b)) - 1)
177 #define BADCODEM1 MAXCODE(BSD_MAX_BITS)
179 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
180 ^ (u_int32_t)(prefix))
181 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
182 + (u_int32_t)(prefix))
184 #define CHECK_GAP 10000 /* Ratio check interval */
186 #define RATIO_SCALE_LOG 8
187 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
188 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
190 static void bsd_clear __P((struct bsd_db *));
191 static int bsd_check __P((struct bsd_db *));
192 static void *bsd_alloc __P((u_char *, int, int));
193 static int bsd_init __P((struct bsd_db *, u_char *, int, int, int, int,
197 * clear the dictionary
204 db->max_ent = FIRST-1;
205 db->n_bits = BSD_INIT_BITS;
209 db->incomp_count = 0;
210 db->checkpoint = CHECK_GAP;
214 * If the dictionary is full, then see if it is time to reset it.
216 * Compute the compression ratio using fixed-point arithmetic
217 * with 8 fractional bits.
219 * Since we have an infinite stream instead of a single file,
220 * watch only the local compression ratio.
222 * Since both peers must reset the dictionary at the same time even in
223 * the absence of CLEAR codes (while packets are incompressible), they
224 * must compute the same ratio.
226 static int /* 1=output CLEAR */
232 if (db->in_count >= db->checkpoint) {
233 /* age the ratio by limiting the size of the counts */
234 if (db->in_count >= RATIO_MAX
235 || db->bytes_out >= RATIO_MAX) {
236 db->in_count -= db->in_count/4;
237 db->bytes_out -= db->bytes_out/4;
240 db->checkpoint = db->in_count + CHECK_GAP;
242 if (db->max_ent >= db->maxmaxcode) {
243 /* Reset the dictionary only if the ratio is worse,
244 * or if it looks as if it has been poisoned
245 * by incompressible data.
247 * This does not overflow, because
248 * db->in_count <= RATIO_MAX.
250 new_ratio = db->in_count << RATIO_SCALE_LOG;
251 if (db->bytes_out != 0)
252 new_ratio /= db->bytes_out;
254 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
258 db->ratio = new_ratio;
268 bsd_comp_stats(state, stats)
270 struct compstat *stats;
272 struct bsd_db *db = (struct bsd_db *) state;
275 stats->unc_bytes = db->uncomp_bytes;
276 stats->unc_packets = db->uncomp_count;
277 stats->comp_bytes = db->comp_bytes;
278 stats->comp_packets = db->comp_count;
279 stats->inc_bytes = db->incomp_bytes;
280 stats->inc_packets = db->incomp_count;
281 stats->ratio = db->in_count;
283 if (stats->ratio <= 0x7fffff)
292 * Reset state, as on a CCP ResetReq.
298 struct bsd_db *db = (struct bsd_db *) state;
306 * Allocate space for a (de) compressor.
309 bsd_alloc(options, opt_len, decomp)
314 u_int newlen, hsize, hshift, maxmaxcode;
317 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
318 || options[1] != CILEN_BSD_COMPRESS
319 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
321 bits = BSD_NBITS(options[2]);
323 case 9: /* needs 82152 for both directions */
324 case 10: /* needs 84144 */
325 case 11: /* needs 88240 */
326 case 12: /* needs 96432 */
330 case 13: /* needs 176784 */
334 case 14: /* needs 353744 */
338 case 15: /* needs 691440 */
342 case 16: /* needs 1366160--far too much, */
343 /* hsize = 69001; */ /* and 69001 is too big for cptr */
344 /* hshift = 8; */ /* in struct bsd_db */
350 maxmaxcode = MAXCODE(bits);
351 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
352 MALLOC(db, struct bsd_db *, newlen, M_DEVBUF, M_NOWAIT);
355 bzero(db, sizeof(*db) - sizeof(db->dict));
360 MALLOC(db->lens, u_int16_t *, (maxmaxcode+1) * sizeof(db->lens[0]),
371 db->maxmaxcode = maxmaxcode;
381 struct bsd_db *db = (struct bsd_db *) state;
384 FREE(db->lens, M_DEVBUF);
389 bsd_comp_alloc(options, opt_len)
393 return bsd_alloc(options, opt_len, 0);
397 bsd_decomp_alloc(options, opt_len)
401 return bsd_alloc(options, opt_len, 1);
405 * Initialize the database.
408 bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp)
411 int opt_len, unit, hdrlen, mru, debug, decomp;
415 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
416 || options[1] != CILEN_BSD_COMPRESS
417 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
418 || BSD_NBITS(options[2]) != db->maxbits
419 || decomp && db->lens == NULL)
429 db->dict[--i].codem1 = BADCODEM1;
430 db->dict[i].cptr = 0;
447 bsd_comp_init(state, options, opt_len, unit, hdrlen, debug)
450 int opt_len, unit, hdrlen, debug;
452 return bsd_init((struct bsd_db *) state, options, opt_len,
453 unit, hdrlen, 0, debug, 0);
457 bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
460 int opt_len, unit, hdrlen, mru, debug;
462 return bsd_init((struct bsd_db *) state, options, opt_len,
463 unit, hdrlen, mru, debug, 1);
469 * One change from the BSD compress command is that when the
470 * code size expands, we do not output a bunch of padding.
473 bsd_compress(state, mret, mp, slen, maxolen)
475 struct mbuf **mret; /* return compressed mbuf chain here */
476 struct mbuf *mp; /* from here */
477 int slen; /* uncompressed length */
478 int maxolen; /* max compressed length */
480 struct bsd_db *db = (struct bsd_db *) state;
481 int hshift = db->hshift;
482 u_int max_ent = db->max_ent;
483 u_int n_bits = db->n_bits;
485 u_int32_t accm = 0, fcode;
486 struct bsd_dict *dictp;
488 int hval, disp, ent, ilen;
494 #define PUTBYTE(v) { \
498 if (wptr >= cp_end) { \
499 m->m_len = wptr - mtod(m, u_char *); \
500 MGET(m->m_next, M_DONTWAIT, MT_DATA); \
504 if (maxolen - olen > MLEN) \
505 MCLGET(m, M_DONTWAIT); \
506 wptr = mtod(m, u_char *); \
507 cp_end = wptr + M_TRAILINGSPACE(m); \
514 #define OUTPUT(ent) { \
516 accm |= ((ent) << bitno); \
518 PUTBYTE(accm >> 24); \
521 } while (bitno <= 24); \
525 * If the protocol is not in the range we're interested in,
526 * just return without compressing the packet. If it is,
527 * the protocol becomes the first byte to compress.
529 rptr = mtod(mp, u_char *);
530 ent = PPP_PROTOCOL(rptr);
531 if (ent < 0x21 || ent > 0xf9) {
536 /* Don't generate compressed packets which are larger than
537 the uncompressed packet. */
541 /* Allocate one mbuf to start with. */
542 MGET(m, M_DONTWAIT, MT_DATA);
546 if (maxolen + db->hdrlen > MLEN)
547 MCLGET(m, M_DONTWAIT);
548 m->m_data += db->hdrlen;
549 wptr = mtod(m, u_char *);
550 cp_end = wptr + M_TRAILINGSPACE(m);
552 wptr = cp_end = NULL;
555 * Copy the PPP header over, changing the protocol,
556 * and install the 2-byte packet sequence number.
559 *wptr++ = PPP_ADDRESS(rptr); /* assumes the ppp header is */
560 *wptr++ = PPP_CONTROL(rptr); /* all in one mbuf */
561 *wptr++ = 0; /* change the protocol */
563 *wptr++ = db->seqno >> 8;
570 slen = mp->m_len - PPP_HDRLEN;
577 rptr = mtod(mp, u_char *);
580 continue; /* handle 0-length buffers */
586 fcode = BSD_KEY(ent, c);
587 hval = BSD_HASH(ent, c, hshift);
588 dictp = &db->dict[hval];
590 /* Validate and then check the entry. */
591 if (dictp->codem1 >= max_ent)
593 if (dictp->f.fcode == fcode) {
594 ent = dictp->codem1+1;
595 continue; /* found (prefix,suffix) */
598 /* continue probing until a match or invalid entry */
599 disp = (hval == 0) ? 1 : hval;
602 if (hval >= db->hsize)
604 dictp = &db->dict[hval];
605 if (dictp->codem1 >= max_ent)
607 } while (dictp->f.fcode != fcode);
608 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
612 OUTPUT(ent); /* output the prefix */
614 /* code -> hashtable */
615 if (max_ent < db->maxmaxcode) {
616 struct bsd_dict *dictp2;
617 /* expand code size if needed */
618 if (max_ent >= MAXCODE(n_bits))
619 db->n_bits = ++n_bits;
621 /* Invalidate old hash table entry using
622 * this code, and then take it over.
624 dictp2 = &db->dict[max_ent+1];
625 if (db->dict[dictp2->cptr].codem1 == max_ent)
626 db->dict[dictp2->cptr].codem1 = BADCODEM1;
628 dictp->codem1 = max_ent;
629 dictp->f.fcode = fcode;
631 db->max_ent = ++max_ent;
636 OUTPUT(ent); /* output the last code */
637 db->bytes_out += olen;
638 db->in_count += ilen;
640 ++db->bytes_out; /* count complete bytes */
643 OUTPUT(CLEAR); /* do not count the CLEAR */
646 * Pad dribble bits of last code with ones.
647 * Do not emit a completely useless byte of ones.
650 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
653 m->m_len = wptr - mtod(m, u_char *);
658 * Increase code size if we would have without the packet
659 * boundary and as the decompressor will.
661 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
664 db->uncomp_bytes += ilen;
666 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen) {
667 /* throw away the compressed stuff if it is longer than uncompressed */
673 db->incomp_bytes += ilen;
676 db->comp_bytes += olen + BSD_OVHD;
679 return olen + PPP_HDRLEN + BSD_OVHD;
686 * Update the "BSD Compress" dictionary on the receiver for
687 * incompressible data by pretending to compress the incoming data.
690 bsd_incomp(state, dmsg)
694 struct bsd_db *db = (struct bsd_db *) state;
695 u_int hshift = db->hshift;
696 u_int max_ent = db->max_ent;
697 u_int n_bits = db->n_bits;
698 struct bsd_dict *dictp;
701 u_int32_t hval, disp;
708 * If the protocol is not in the range we're interested in,
709 * just return without looking at the packet. If it is,
710 * the protocol becomes the first byte to "compress".
712 rptr = mtod(dmsg, u_char *);
713 ent = PPP_PROTOCOL(rptr);
714 if (ent < 0x21 || ent > 0xf9)
719 ilen = 1; /* count the protocol as 1 byte */
721 slen = dmsg->m_len - PPP_HDRLEN;
727 rptr = mtod(dmsg, u_char *);
735 fcode = BSD_KEY(ent, c);
736 hval = BSD_HASH(ent, c, hshift);
737 dictp = &db->dict[hval];
739 /* validate and then check the entry */
740 if (dictp->codem1 >= max_ent)
742 if (dictp->f.fcode == fcode) {
743 ent = dictp->codem1+1;
744 continue; /* found (prefix,suffix) */
747 /* continue probing until a match or invalid entry */
748 disp = (hval == 0) ? 1 : hval;
751 if (hval >= db->hsize)
753 dictp = &db->dict[hval];
754 if (dictp->codem1 >= max_ent)
756 } while (dictp->f.fcode != fcode);
757 ent = dictp->codem1+1;
758 continue; /* finally found (prefix,suffix) */
760 nomatch: /* output (count) the prefix */
763 /* code -> hashtable */
764 if (max_ent < db->maxmaxcode) {
765 struct bsd_dict *dictp2;
766 /* expand code size if needed */
767 if (max_ent >= MAXCODE(n_bits))
768 db->n_bits = ++n_bits;
770 /* Invalidate previous hash table entry
771 * assigned this code, and then take it over.
773 dictp2 = &db->dict[max_ent+1];
774 if (db->dict[dictp2->cptr].codem1 == max_ent)
775 db->dict[dictp2->cptr].codem1 = BADCODEM1;
777 dictp->codem1 = max_ent;
778 dictp->f.fcode = fcode;
780 db->max_ent = ++max_ent;
781 db->lens[max_ent] = db->lens[ent]+1;
784 } while (--slen != 0);
786 bitno += n_bits; /* output (count) the last code */
787 db->bytes_out += bitno/8;
788 db->in_count += ilen;
792 db->incomp_bytes += ilen;
794 db->uncomp_bytes += ilen;
796 /* Increase code size if we would have without the packet
797 * boundary and as the decompressor will.
799 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
805 * Decompress "BSD Compress".
807 * Because of patent problems, we return DECOMP_ERROR for errors
808 * found by inspecting the input data and for system problems, but
809 * DECOMP_FATALERROR for any errors which could possibly be said to
810 * be being detected "after" decompression. For DECOMP_ERROR,
811 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
812 * infringing a patent of Motorola's if we do, so we take CCP down
815 * Given that the frame has the correct sequence number and a good FCS,
816 * errors such as invalid codes in the input most likely indicate a
817 * bug, so we return DECOMP_FATALERROR for them in order to turn off
818 * compression, even though they are detected by inspecting the input.
821 bsd_decompress(state, cmp, dmpp)
823 struct mbuf *cmp, **dmpp;
825 struct bsd_db *db = (struct bsd_db *) state;
826 u_int max_ent = db->max_ent;
828 u_int bitno = 32; /* 1st valid bit in accm */
829 u_int n_bits = db->n_bits;
830 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
831 struct bsd_dict *dictp;
832 int explen, i, seq, len;
833 u_int incode, oldcode, finchar;
834 u_char *p, *rptr, *wptr;
835 struct mbuf *m, *dmp, *mret;
836 int adrs, ctrl, ilen;
837 int space, codelen, extra;
840 * Save the address/control from the PPP header
841 * and then get the sequence number.
844 rptr = mtod(cmp, u_char *);
845 adrs = PPP_ADDRESS(rptr);
846 ctrl = PPP_CONTROL(rptr);
848 len = cmp->m_len - PPP_HDRLEN;
850 for (i = 0; i < 2; ++i) {
855 rptr = mtod(cmp, u_char *);
858 seq = (seq << 8) + *rptr++;
863 * Check the sequence number and give up if it differs from
864 * the value we're expecting.
866 if (seq != db->seqno) {
868 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
869 db->unit, seq, db->seqno - 1);
875 * Allocate one mbuf to start with.
877 MGETHDR(dmp, M_DONTWAIT, MT_DATA);
883 MCLGET(dmp, M_DONTWAIT);
884 dmp->m_data += db->hdrlen;
885 wptr = mtod(dmp, u_char *);
886 space = M_TRAILINGSPACE(dmp) - PPP_HDRLEN + 1;
889 * Fill in the ppp header, but not the last byte of the protocol
890 * (that comes from the decompressed data).
895 wptr += PPP_HDRLEN - 1;
903 if (!cmp) /* quit at end of message */
905 rptr = mtod(cmp, u_char *);
908 continue; /* handle 0-length buffers */
912 * Accumulate bytes until we have a complete code.
913 * Then get the next code, relying on the 32-bit,
914 * unsigned accm to mask the result.
917 accm |= *rptr++ << bitno;
919 if (tgtbitno < bitno)
921 incode = accm >> tgtbitno;
925 if (incode == CLEAR) {
927 * The dictionary must only be cleared at
928 * the end of a packet. But there could be an
929 * empty mbuf at the end.
931 if (len > 0 || cmp->m_next != NULL) {
932 while ((cmp = cmp->m_next) != NULL)
937 printf("bsd_decomp%d: bad CLEAR\n", db->unit);
938 return DECOMP_FATALERROR; /* probably a bug */
946 if (incode > max_ent + 2 || incode > db->maxmaxcode
947 || incode > max_ent && oldcode == CLEAR) {
950 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
951 db->unit, incode, oldcode);
952 printf("max_ent=0x%x explen=%d seqno=%d\n",
953 max_ent, explen, db->seqno);
955 return DECOMP_FATALERROR; /* probably a bug */
958 /* Special case for KwKwK string. */
959 if (incode > max_ent) {
967 codelen = db->lens[finchar];
968 explen += codelen + extra;
969 if (explen > db->mru + 1) {
972 printf("bsd_decomp%d: ran out of mru\n", db->unit);
974 while ((cmp = cmp->m_next) != NULL)
976 printf(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
977 len, finchar, codelen, explen);
980 return DECOMP_FATALERROR;
984 * For simplicity, the decoded characters go in a single mbuf,
985 * so we allocate a single extra cluster mbuf if necessary.
987 if ((space -= codelen + extra) < 0) {
988 dmp->m_len = wptr - mtod(dmp, u_char *);
989 MGET(m, M_DONTWAIT, MT_DATA);
997 MCLGET(m, M_DONTWAIT);
998 space = M_TRAILINGSPACE(m) - (codelen + extra);
1000 /* now that's what I call *compression*. */
1002 return DECOMP_ERROR;
1005 wptr = mtod(dmp, u_char *);
1009 * Decode this code and install it in the decompressed buffer.
1011 p = (wptr += codelen);
1012 while (finchar > LAST) {
1013 dictp = &db->dict[db->dict[finchar].cptr];
1015 if (--codelen <= 0 || dictp->codem1 != finchar-1)
1018 *--p = dictp->f.hs.suffix;
1019 finchar = dictp->f.hs.prefix;
1025 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1026 db->unit, codelen, incode, max_ent);
1029 if (extra) /* the KwKwK case again */
1033 * If not first code in a packet, and
1034 * if not out of code space, then allocate a new code.
1036 * Keep the hash table correct so it can be used
1037 * with uncompressed packets.
1039 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1040 struct bsd_dict *dictp2;
1042 u_int32_t hval, disp;
1044 fcode = BSD_KEY(oldcode,finchar);
1045 hval = BSD_HASH(oldcode,finchar,db->hshift);
1046 dictp = &db->dict[hval];
1048 /* look for a free hash table entry */
1049 if (dictp->codem1 < max_ent) {
1050 disp = (hval == 0) ? 1 : hval;
1053 if (hval >= db->hsize)
1055 dictp = &db->dict[hval];
1056 } while (dictp->codem1 < max_ent);
1060 * Invalidate previous hash table entry
1061 * assigned this code, and then take it over
1063 dictp2 = &db->dict[max_ent+1];
1064 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1065 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1067 dictp2->cptr = hval;
1068 dictp->codem1 = max_ent;
1069 dictp->f.fcode = fcode;
1071 db->max_ent = ++max_ent;
1072 db->lens[max_ent] = db->lens[oldcode]+1;
1074 /* Expand code size if needed. */
1075 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1076 db->n_bits = ++n_bits;
1077 tgtbitno = 32-n_bits;
1082 dmp->m_len = wptr - mtod(dmp, u_char *);
1085 * Keep the checkpoint right so that incompressible packets
1086 * clear the dictionary at the right times.
1088 db->bytes_out += ilen;
1089 db->in_count += explen;
1090 if (bsd_check(db) && db->debug) {
1091 printf("bsd_decomp%d: peer should have cleared dictionary\n",
1096 db->comp_bytes += ilen + BSD_OVHD;
1098 db->uncomp_bytes += explen;
1106 printf("bsd_decomp%d: fell off end of chain ", db->unit);
1107 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1108 incode, finchar, db->dict[finchar].cptr, max_ent);
1109 } else if (dictp->codem1 != finchar-1) {
1110 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1111 db->unit, incode, finchar);
1112 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
1113 db->dict[finchar].cptr, dictp->codem1);
1116 return DECOMP_FATALERROR;
1119 #endif /* DO_BSD_COMPRESS */