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 Ultrix systems.
43 * $Id: bsd-comp.c,v 1.3 1994/12/08 00:33:31 paulus Exp $
46 #include "../h/param.h"
47 #include "../h/types.h"
48 #include "../h/mbuf.h"
49 #include "../h/socket.h"
50 #include "../net/net/if.h"
54 #define PACKETPTR struct mbuf *
57 #define BSD_LITTLE_ENDIAN /* all Ultrix machines are little-endian */
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 * Macros to extract protocol version and number of bits
83 * from the third byte of the BSD Compress CCP configuration option.
85 #define BSD_VERSION(x) ((x) >> 5)
86 #define BSD_NBITS(x) ((x) & 0x1F)
88 #define BSD_CURRENT_VERSION 1
91 * A dictionary for doing BSD compress.
94 int totlen; /* length of this structure */
95 u_int hsize; /* size of the hash table */
96 u_char hshift; /* used in hash function */
97 u_char n_bits; /* current bits/code */
101 u_short seqno; /* sequence # of next packet */
102 u_int hdrlen; /* header length to preallocate */
104 u_int maxmaxcode; /* largest valid code */
105 u_int max_ent; /* largest code in use */
106 u_int in_count; /* uncompressed bytes, aged */
107 u_int bytes_out; /* compressed bytes, aged */
108 u_int ratio; /* recent compression ratio */
109 u_int checkpoint; /* when to next check the ratio */
110 u_int clear_count; /* times dictionary cleared */
111 u_int incomp_count; /* incompressible packets */
112 u_int incomp_bytes; /* incompressible bytes */
113 u_int uncomp_count; /* uncompressed packets */
114 u_int uncomp_bytes; /* uncompressed bytes */
115 u_int comp_count; /* compressed packets */
116 u_int comp_bytes; /* compressed bytes */
117 u_short *lens; /* array of lengths of codes */
119 union { /* hash value */
122 #ifdef BSD_LITTLE_ENDIAN
123 u_short prefix; /* preceding code */
124 u_char suffix; /* last character of new code */
128 u_char suffix; /* last character of new code */
129 u_short prefix; /* preceding code */
133 u_short codem1; /* output of hash table -1 */
134 u_short cptr; /* map code to hash table entry */
138 #define BSD_OVHD 2 /* BSD compress overhead/packet */
139 #define MIN_BSD_BITS 9
140 #define BSD_INIT_BITS MIN_BSD_BITS
141 #define MAX_BSD_BITS 15
143 static void *bsd_comp_alloc __P((u_char *options, int opt_len));
144 static void *bsd_decomp_alloc __P((u_char *options, int opt_len));
145 static void bsd_free __P((void *state));
146 static int bsd_comp_init __P((void *state, u_char *options, int opt_len,
147 int unit, int debug));
148 static int bsd_decomp_init __P((void *state, u_char *options, int opt_len,
149 int unit, int hdrlen, int mru, int debug));
150 static int bsd_compress __P((void *state, struct mbuf **mret,
151 struct mbuf *mp, int slen, int maxolen));
152 static void bsd_incomp __P((void *state, struct mbuf *dmsg));
153 static int bsd_decompress __P((void *state, struct mbuf *cmp,
154 struct mbuf **dmpp));
155 static void bsd_reset __P((void *state));
156 static void bsd_comp_stats __P((void *state, struct compstat *stats));
159 * Procedures exported to if_ppp.c.
161 struct compressor ppp_bsd_compress = {
162 0x21, /* compress_proto */
163 bsd_comp_alloc, /* comp_alloc */
164 bsd_free, /* comp_free */
165 bsd_comp_init, /* comp_init */
166 bsd_reset, /* comp_reset */
167 bsd_compress, /* compress */
168 bsd_comp_stats, /* comp_stat */
169 bsd_decomp_alloc, /* decomp_alloc */
170 bsd_free, /* decomp_free */
171 bsd_decomp_init, /* decomp_init */
172 bsd_reset, /* decomp_reset */
173 bsd_decompress, /* decompress */
174 bsd_incomp, /* incomp */
175 bsd_comp_stats, /* decomp_stat */
179 * Some useful mbuf macros not in mbuf.h.
181 #define M_IS_CLUSTER(m) ((m)->m_off > MMAXOFF)
183 #define M_TRAILINGSPACE(m) \
184 ((M_IS_CLUSTER(m) ? (u_int)(m)->m_clptr + M_CLUSTERSZ : MSIZE) \
185 - ((m)->m_off + (m)->m_len))
188 * the next two codes should not be changed lightly, as they must not
189 * lie within the contiguous general code space.
191 #define CLEAR 256 /* table clear output code */
192 #define FIRST 257 /* first free entry */
195 #define MAXCODE(b) ((1 << (b)) - 1)
196 #define BADCODEM1 MAXCODE(MAX_BSD_BITS);
198 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
199 ^ (u_int32_t)(prefix))
200 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
201 + (u_int32_t)(prefix))
203 #define CHECK_GAP 10000 /* Ratio check interval */
205 #define RATIO_SCALE_LOG 8
206 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
207 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
210 * clear the dictionary
217 db->max_ent = FIRST-1;
218 db->n_bits = BSD_INIT_BITS;
222 db->incomp_count = 0;
223 db->checkpoint = CHECK_GAP;
227 * If the dictionary is full, then see if it is time to reset it.
229 * Compute the compression ratio using fixed-point arithmetic
230 * with 8 fractional bits.
232 * Since we have an infinite stream instead of a single file,
233 * watch only the local compression ratio.
235 * Since both peers must reset the dictionary at the same time even in
236 * the absence of CLEAR codes (while packets are incompressible), they
237 * must compute the same ratio.
239 static int /* 1=output CLEAR */
245 if (db->in_count >= db->checkpoint) {
246 /* age the ratio by limiting the size of the counts */
247 if (db->in_count >= RATIO_MAX
248 || db->bytes_out >= RATIO_MAX) {
249 db->in_count -= db->in_count/4;
250 db->bytes_out -= db->bytes_out/4;
253 db->checkpoint = db->in_count + CHECK_GAP;
255 if (db->max_ent >= db->maxmaxcode) {
256 /* Reset the dictionary only if the ratio is worse,
257 * or if it looks as if it has been poisoned
258 * by incompressible data.
260 * This does not overflow, because
261 * db->in_count <= RATIO_MAX.
263 new_ratio = db->in_count << RATIO_SCALE_LOG;
264 if (db->bytes_out != 0)
265 new_ratio /= db->bytes_out;
267 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
271 db->ratio = new_ratio;
281 bsd_comp_stats(state, stats)
283 struct compstat *stats;
285 struct bsd_db *db = (struct bsd_db *) state;
288 stats->unc_bytes = db->uncomp_bytes;
289 stats->unc_packets = db->uncomp_count;
290 stats->comp_bytes = db->comp_bytes;
291 stats->comp_packets = db->comp_count;
292 stats->inc_bytes = db->incomp_bytes;
293 stats->inc_packets = db->incomp_count;
294 stats->ratio = db->in_count;
296 if (stats->ratio <= 0x7fffff)
305 * Reset state, as on a CCP ResetReq.
311 struct bsd_db *db = (struct bsd_db *) state;
319 * Allocate space for a (de) compressor.
322 bsd_alloc(options, opt_len, decomp)
327 u_int newlen, hsize, hshift, maxmaxcode;
330 if (opt_len != 3 || options[0] != 0x21 || options[1] != 3
331 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
333 bits = BSD_NBITS(options[2]);
335 case 9: /* needs 82152 for both directions */
336 case 10: /* needs 84144 */
337 case 11: /* needs 88240 */
338 case 12: /* needs 96432 */
342 case 13: /* needs 176784 */
346 case 14: /* needs 353744 */
350 case 15: /* needs 691440 */
354 case 16: /* needs 1366160--far too much, */
355 /* hsize = 69001; */ /* and 69001 is too big for cptr */
356 /* hshift = 8; */ /* in struct bsd_db */
362 maxmaxcode = MAXCODE(bits);
363 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
364 KM_ALLOC(db, struct bsd_db *, newlen, KM_DEVBUF, KM_NOARG);
367 bzero(db, sizeof(*db) - sizeof(db->dict));
372 KM_ALLOC(db->lens, u_short *, (maxmaxcode+1) * sizeof(db->lens[0]),
373 KM_DEVBUF, KM_NOARG);
375 KM_FREE(db, KM_DEVBUF);
383 db->maxmaxcode = maxmaxcode;
393 struct bsd_db *db = (struct bsd_db *) state;
396 KM_FREE(db->lens, KM_DEVBUF);
397 KM_FREE(db, KM_DEVBUF);
401 bsd_comp_alloc(options, opt_len)
405 return bsd_alloc(options, opt_len, 0);
409 bsd_decomp_alloc(options, opt_len)
413 return bsd_alloc(options, opt_len, 1);
417 * Initialize the database.
420 bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp)
423 int opt_len, unit, hdrlen, mru, debug, decomp;
427 if (opt_len != 3 || options[0] != 0x21 || options[1] != 3
428 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
429 || BSD_NBITS(options[2]) != db->maxbits
430 || decomp && db->lens == NULL)
440 db->dict[--i].codem1 = BADCODEM1;
441 db->dict[i].cptr = 0;
458 bsd_comp_init(state, options, opt_len, unit, debug)
461 int opt_len, unit, debug;
463 return bsd_init((struct bsd_db *) state, options, opt_len,
464 unit, 0, 0, debug, 0);
468 bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
471 int opt_len, unit, hdrlen, mru, debug;
473 return bsd_init((struct bsd_db *) state, options, opt_len,
474 unit, hdrlen, mru, debug, 1);
480 * One change from the BSD compress command is that when the
481 * code size expands, we do not output a bunch of padding.
483 static int /* new slen */
484 bsd_compress(state, mret, mp, slen, maxolen)
486 struct mbuf **mret; /* return compressed mbuf chain here */
487 struct mbuf *mp; /* from here */
488 int slen; /* uncompressed length */
489 int maxolen; /* max compressed length */
491 struct bsd_db *db = (struct bsd_db *) state;
492 int hshift = db->hshift;
493 u_int max_ent = db->max_ent;
494 u_int n_bits = db->n_bits;
496 u_int32_t accm = 0, fcode;
497 struct bsd_dict *dictp;
499 int hval, disp, ent, ilen;
504 struct mbuf *m, **mnp, *clp;
506 #define PUTBYTE(v) { \
510 if (wptr >= cp_end) { \
511 m->m_len = wptr - mtod(m, u_char *); \
512 MGET(m->m_next, M_DONTWAIT, MT_DATA); \
515 if (maxolen - olen > MLEN) { \
519 wptr = mtod(m, u_char *); \
520 cp_end = wptr + M_TRAILINGSPACE(m); \
527 #define OUTPUT(ent) { \
529 accm |= ((ent) << bitno); \
531 PUTBYTE(accm >> 24); \
534 } while (bitno <= 24); \
538 * If the protocol is not in the range we're interested in,
539 * just return without compressing the packet. If it is,
540 * the protocol becomes the first byte to compress.
542 rptr = mtod(mp, u_char *);
543 ent = PPP_PROTOCOL(rptr);
544 if (ent < 0x21 || ent > 0xf9) {
549 /* Don't generate compressed packets which are larger than
550 the uncompressed packet. */
554 /* Allocate one mbuf to start with. */
555 MGET(m, M_DONTWAIT, MT_DATA);
558 if (maxolen > MLEN) {
562 wptr = mtod(m, u_char *);
563 cp_end = wptr + M_TRAILINGSPACE(m);
565 wptr = cp_end = NULL;
568 * Copy the PPP header over, changing the protocol,
569 * and install the 2-byte packet sequence number.
572 *wptr++ = PPP_ADDRESS(rptr); /* assumes the ppp header is */
573 *wptr++ = PPP_CONTROL(rptr); /* all in one mbuf */
574 *wptr++ = 0; /* change the protocol */
576 *wptr++ = db->seqno >> 8;
583 slen = mp->m_len - PPP_HDRLEN;
590 rptr = mtod(mp, u_char *);
593 continue; /* handle 0-length buffers */
599 fcode = BSD_KEY(ent, c);
600 hval = BSD_HASH(ent, c, hshift);
601 dictp = &db->dict[hval];
603 /* Validate and then check the entry. */
604 if (dictp->codem1 >= max_ent)
606 if (dictp->f.fcode == fcode) {
607 ent = dictp->codem1+1;
608 continue; /* found (prefix,suffix) */
611 /* continue probing until a match or invalid entry */
612 disp = (hval == 0) ? 1 : hval;
615 if (hval >= db->hsize)
617 dictp = &db->dict[hval];
618 if (dictp->codem1 >= max_ent)
620 } while (dictp->f.fcode != fcode);
621 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
625 OUTPUT(ent); /* output the prefix */
627 /* code -> hashtable */
628 if (max_ent < db->maxmaxcode) {
629 struct bsd_dict *dictp2;
630 /* expand code size if needed */
631 if (max_ent >= MAXCODE(n_bits))
632 db->n_bits = ++n_bits;
634 /* Invalidate old hash table entry using
635 * this code, and then take it over.
637 dictp2 = &db->dict[max_ent+1];
638 if (db->dict[dictp2->cptr].codem1 == max_ent)
639 db->dict[dictp2->cptr].codem1 = BADCODEM1;
641 dictp->codem1 = max_ent;
642 dictp->f.fcode = fcode;
644 db->max_ent = ++max_ent;
649 OUTPUT(ent); /* output the last code */
650 db->bytes_out += olen;
651 db->in_count += ilen;
653 ++db->bytes_out; /* count complete bytes */
656 OUTPUT(CLEAR); /* do not count the CLEAR */
659 * Pad dribble bits of last code with ones.
660 * Do not emit a completely useless byte of ones.
663 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
666 m->m_len = wptr - mtod(m, u_char *);
671 * Increase code size if we would have without the packet
672 * boundary and as the decompressor will.
674 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
677 db->uncomp_bytes += ilen;
679 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen) {
680 /* throw away the compressed stuff if it is longer than uncompressed */
686 db->incomp_bytes += ilen;
689 db->comp_bytes += olen + BSD_OVHD;
692 return olen + PPP_HDRLEN + BSD_OVHD;
699 * Update the "BSD Compress" dictionary on the receiver for
700 * incompressible data by pretending to compress the incoming data.
703 bsd_incomp(state, dmsg)
707 struct bsd_db *db = (struct bsd_db *) state;
708 u_int hshift = db->hshift;
709 u_int max_ent = db->max_ent;
710 u_int n_bits = db->n_bits;
711 struct bsd_dict *dictp;
721 * If the protocol is not in the range we're interested in,
722 * just return without looking at the packet. If it is,
723 * the protocol becomes the first byte to "compress".
725 rptr = mtod(dmsg, u_char *);
726 ent = PPP_PROTOCOL(rptr);
727 if (ent < 0x21 || ent > 0xf9)
732 ilen = 1; /* count the protocol as 1 byte */
734 slen = dmsg->m_len - PPP_HDRLEN;
740 rptr = mtod(dmsg, u_char *);
748 fcode = BSD_KEY(ent, c);
749 hval = BSD_HASH(ent, c, hshift);
750 dictp = &db->dict[hval];
752 /* validate and then check the entry */
753 if (dictp->codem1 >= max_ent)
755 if (dictp->f.fcode == fcode) {
756 ent = dictp->codem1+1;
757 continue; /* found (prefix,suffix) */
760 /* continue probing until a match or invalid entry */
761 disp = (hval == 0) ? 1 : hval;
764 if (hval >= db->hsize)
766 dictp = &db->dict[hval];
767 if (dictp->codem1 >= max_ent)
769 } while (dictp->f.fcode != fcode);
770 ent = dictp->codem1+1;
771 continue; /* finally found (prefix,suffix) */
773 nomatch: /* output (count) the prefix */
776 /* code -> hashtable */
777 if (max_ent < db->maxmaxcode) {
778 struct bsd_dict *dictp2;
779 /* expand code size if needed */
780 if (max_ent >= MAXCODE(n_bits))
781 db->n_bits = ++n_bits;
783 /* Invalidate previous hash table entry
784 * assigned this code, and then take it over.
786 dictp2 = &db->dict[max_ent+1];
787 if (db->dict[dictp2->cptr].codem1 == max_ent)
788 db->dict[dictp2->cptr].codem1 = BADCODEM1;
790 dictp->codem1 = max_ent;
791 dictp->f.fcode = fcode;
793 db->max_ent = ++max_ent;
794 db->lens[max_ent] = db->lens[ent]+1;
797 } while (--slen != 0);
799 bitno += n_bits; /* output (count) the last code */
800 db->bytes_out += bitno/8;
801 db->in_count += ilen;
805 db->incomp_bytes += ilen;
807 db->uncomp_bytes += ilen;
809 /* Increase code size if we would have without the packet
810 * boundary and as the decompressor will.
812 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
818 * Decompress "BSD Compress".
820 * Because of patent problems, we return DECOMP_ERROR for errors
821 * found by inspecting the input data and for system problems, but
822 * DECOMP_FATALERROR for any errors which could possibly be said to
823 * be being detected "after" decompression. For DECOMP_ERROR,
824 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
825 * infringing a patent of Motorola's if we do, so we take CCP down
828 * Given that the frame has the correct sequence number and a good FCS,
829 * errors such as invalid codes in the input most likely indicate a
830 * bug, so we return DECOMP_FATALERROR for them in order to turn off
831 * compression, even though they are detected by inspecting the input.
834 bsd_decompress(state, cmp, dmpp)
836 struct mbuf *cmp, **dmpp;
838 struct bsd_db *db = (struct bsd_db *) state;
839 u_int max_ent = db->max_ent;
841 u_int bitno = 32; /* 1st valid bit in accm */
842 u_int n_bits = db->n_bits;
843 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
844 struct bsd_dict *dictp;
845 int explen, i, seq, len;
846 u_int incode, oldcode, finchar;
847 u_char *p, *rptr, *wptr;
848 struct mbuf *m, *dmp, *mret;
849 int adrs, ctrl, ilen;
850 int space, codelen, extra;
851 struct mbuf *last, *clp;
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 = cmp->m_len - PPP_HDRLEN;
864 for (i = 0; i < 2; ++i) {
869 rptr = mtod(cmp, u_char *);
872 seq = (seq << 8) + *rptr++;
877 * Check the sequence number and give up if it differs from
878 * the value we're expecting.
880 if (seq != db->seqno++) {
882 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
883 db->unit, seq, db->seqno - 1);
888 * Allocate one mbuf to start with.
890 MGET(dmp, M_DONTWAIT, MT_DATA);
897 dmp->m_off += db->hdrlen;
898 wptr = mtod(dmp, u_char *);
899 space = M_TRAILINGSPACE(dmp) - PPP_HDRLEN + 1;
902 * Fill in the ppp header, but not the last byte of the protocol
903 * (that comes from the decompressed data).
908 wptr += PPP_HDRLEN - 1;
916 if (!cmp) /* quit at end of message */
918 rptr = mtod(cmp, u_char *);
921 continue; /* handle 0-length buffers */
925 * Accumulate bytes until we have a complete code.
926 * Then get the next code, relying on the 32-bit,
927 * unsigned accm to mask the result.
930 accm |= *rptr++ << bitno;
932 if (tgtbitno < bitno)
934 incode = accm >> tgtbitno;
938 if (incode == CLEAR) {
940 * The dictionary must only be cleared at
941 * the end of a packet. But there could be an
942 * empty mbuf at the end.
944 if (len > 0 || cmp->m_next != NULL) {
945 while ((cmp = cmp->m_next) != NULL)
950 printf("bsd_decomp%d: bad CLEAR\n", db->unit);
951 return DECOMP_FATALERROR; /* probably a bug */
959 if (incode > max_ent + 2 || incode > db->maxmaxcode
960 || incode > max_ent && oldcode == CLEAR) {
963 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
964 db->unit, incode, oldcode);
965 printf("max_ent=0x%x explen=%d seqno=%d\n",
966 max_ent, explen, db->seqno);
968 return DECOMP_FATALERROR; /* probably a bug */
971 /* Special case for KwKwK string. */
972 if (incode > max_ent) {
980 codelen = db->lens[finchar];
981 explen += codelen + extra;
982 if (explen > db->mru + 1) {
985 printf("bsd_decomp%d: ran out of mru\n", db->unit);
987 while ((cmp = cmp->m_next) != NULL)
989 printf(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
990 len, finchar, codelen, explen);
993 return DECOMP_FATALERROR;
997 * For simplicity, the decoded characters go in a single mbuf,
998 * so we allocate a single extra cluster mbuf if necessary.
1000 if ((space -= codelen + extra) < 0) {
1001 dmp->m_len = wptr - mtod(dmp, u_char *);
1002 MGET(m, M_DONTWAIT, MT_DATA);
1005 return DECOMP_ERROR;
1011 space = M_TRAILINGSPACE(m) - (codelen + extra);
1013 /* now that's what I call *compression*. */
1015 return DECOMP_ERROR;
1018 wptr = mtod(dmp, u_char *);
1022 * Decode this code and install it in the decompressed buffer.
1024 p = (wptr += codelen);
1025 while (finchar > LAST) {
1026 dictp = &db->dict[db->dict[finchar].cptr];
1028 if (--codelen <= 0 || dictp->codem1 != finchar-1)
1031 *--p = dictp->f.hs.suffix;
1032 finchar = dictp->f.hs.prefix;
1038 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1039 db->unit, codelen, incode, max_ent);
1042 if (extra) /* the KwKwK case again */
1046 * If not first code in a packet, and
1047 * if not out of code space, then allocate a new code.
1049 * Keep the hash table correct so it can be used
1050 * with uncompressed packets.
1052 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1053 struct bsd_dict *dictp2;
1057 fcode = BSD_KEY(oldcode,finchar);
1058 hval = BSD_HASH(oldcode,finchar,db->hshift);
1059 dictp = &db->dict[hval];
1061 /* look for a free hash table entry */
1062 if (dictp->codem1 < max_ent) {
1063 disp = (hval == 0) ? 1 : hval;
1066 if (hval >= db->hsize)
1068 dictp = &db->dict[hval];
1069 } while (dictp->codem1 < max_ent);
1073 * Invalidate previous hash table entry
1074 * assigned this code, and then take it over
1076 dictp2 = &db->dict[max_ent+1];
1077 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1078 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1080 dictp2->cptr = hval;
1081 dictp->codem1 = max_ent;
1082 dictp->f.fcode = fcode;
1084 db->max_ent = ++max_ent;
1085 db->lens[max_ent] = db->lens[oldcode]+1;
1087 /* Expand code size if needed. */
1088 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1089 db->n_bits = ++n_bits;
1090 tgtbitno = 32-n_bits;
1095 dmp->m_len = wptr - mtod(dmp, u_char *);
1098 * Keep the checkpoint right so that incompressible packets
1099 * clear the dictionary at the right times.
1101 db->bytes_out += ilen;
1102 db->in_count += explen;
1103 if (bsd_check(db) && db->debug) {
1104 printf("bsd_decomp%d: peer should have cleared dictionary\n",
1109 db->comp_bytes += ilen + BSD_OVHD;
1111 db->uncomp_bytes += explen;
1119 printf("bsd_decomp%d: fell off end of chain ", db->unit);
1120 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1121 incode, finchar, db->dict[finchar].cptr, max_ent);
1122 } else if (dictp->codem1 != finchar-1) {
1123 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1124 db->unit, incode, finchar);
1125 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
1126 db->dict[finchar].cptr, dictp->codem1);
1129 return DECOMP_FATALERROR;