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.2 1994/11/28 01:41:29 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 *
58 * PPP "BSD compress" compression
59 * The differences between this compression and the classic BSD LZW
60 * source are obvious from the requirement that the classic code worked
61 * with files while this handles arbitrarily long streams that
62 * are broken into packets. They are:
64 * When the code size expands, a block of junk is not emitted by
65 * the compressor and not expected by the decompressor.
67 * New codes are not necessarily assigned every time an old
68 * code is output by the compressor. This is because a packet
69 * end forces a code to be emitted, but does not imply that a
70 * new sequence has been seen.
72 * The compression ratio is checked at the first end of a packet
73 * after the appropriate gap. Besides simplifying and speeding
74 * things up, this makes it more likely that the transmitter
75 * and receiver will agree when the dictionary is cleared when
76 * compression is not going well.
80 * Macros to extract protocol version and number of bits
81 * from the third byte of the BSD Compress CCP configuration option.
83 #define BSD_VERSION(x) ((x) >> 5)
84 #define BSD_NBITS(x) ((x) & 0x1F)
86 #define BSD_CURRENT_VERSION 1
89 * A dictionary for doing BSD compress.
92 int totlen; /* length of this structure */
93 u_int hsize; /* size of the hash table */
94 u_char hshift; /* used in hash function */
95 u_char n_bits; /* current bits/code */
99 u_short seqno; /* sequence # of next packet */
101 u_int maxmaxcode; /* largest valid code */
102 u_int max_ent; /* largest code in use */
103 u_int in_count; /* uncompressed bytes, aged */
104 u_int bytes_out; /* compressed bytes, aged */
105 u_int ratio; /* recent compression ratio */
106 u_int checkpoint; /* when to next check the ratio */
107 u_int clear_count; /* times dictionary cleared */
108 u_int incomp_count; /* incompressible packets */
109 u_int incomp_bytes; /* incompressible bytes */
110 u_int uncomp_count; /* uncompressed packets */
111 u_int uncomp_bytes; /* uncompressed bytes */
112 u_int comp_count; /* compressed packets */
113 u_int comp_bytes; /* compressed bytes */
114 u_short *lens; /* array of lengths of codes */
116 union { /* hash value */
119 #if BYTE_ORDER == LITTLE_ENDIAN
120 u_short prefix; /* preceding code */
121 u_char suffix; /* last character of new code */
125 u_char suffix; /* last character of new code */
126 u_short prefix; /* preceding code */
130 u_short codem1; /* output of hash table -1 */
131 u_short cptr; /* map code to hash table entry */
135 #define BSD_OVHD 2 /* BSD compress overhead/packet */
136 #define MIN_BSD_BITS 9
137 #define BSD_INIT_BITS MIN_BSD_BITS
138 #define MAX_BSD_BITS 15
140 static void *bsd_comp_alloc __P((u_char *options, int opt_len));
141 static void *bsd_decomp_alloc __P((u_char *options, int opt_len));
142 static void bsd_free __P((void *state));
143 static int bsd_comp_init __P((void *state, u_char *options, int opt_len,
144 int unit, int debug));
145 static int bsd_decomp_init __P((void *state, u_char *options, int opt_len,
146 int unit, int mru, int debug));
147 static int bsd_compress __P((void *state, struct mbuf **mret,
148 struct mbuf *mp, int slen, int maxolen));
149 static void bsd_incomp __P((void *state, struct mbuf *dmsg));
150 static int bsd_decompress __P((void *state, struct mbuf *cmp,
151 struct mbuf **dmpp));
152 static void bsd_reset __P((void *state));
153 static void bsd_comp_stats __P((void *state, struct compstat *stats));
156 * Procedures exported to if_ppp.c.
158 struct compressor ppp_bsd_compress = {
159 0x21, /* compress_proto */
160 bsd_comp_alloc, /* comp_alloc */
161 bsd_free, /* comp_free */
162 bsd_comp_init, /* comp_init */
163 bsd_reset, /* comp_reset */
164 bsd_compress, /* compress */
165 bsd_comp_stats, /* comp_stat */
166 bsd_decomp_alloc, /* decomp_alloc */
167 bsd_free, /* decomp_free */
168 bsd_decomp_init, /* decomp_init */
169 bsd_reset, /* decomp_reset */
170 bsd_decompress, /* decompress */
171 bsd_incomp, /* incomp */
172 bsd_comp_stats, /* decomp_stat */
176 * Some useful mbuf macros not in mbuf.h.
178 #define M_IS_CLUSTER(m) ((m)->m_off > MMAXOFF)
180 #define M_TRAILINGSPACE(m) \
181 ((M_IS_CLUSTER(m) ? (u_int)(m)->m_clptr + M_CLUSTERSZ : MSIZE) \
182 - ((m)->m_off + (m)->m_len))
185 * the next two codes should not be changed lightly, as they must not
186 * lie within the contiguous general code space.
188 #define CLEAR 256 /* table clear output code */
189 #define FIRST 257 /* first free entry */
192 #define MAXCODE(b) ((1 << (b)) - 1)
193 #define BADCODEM1 MAXCODE(MAX_BSD_BITS);
195 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
196 ^ (u_int32_t)(prefix))
197 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
198 + (u_int32_t)(prefix))
200 #define CHECK_GAP 10000 /* Ratio check interval */
202 #define RATIO_SCALE_LOG 8
203 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
204 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
207 * clear the dictionary
214 db->max_ent = FIRST-1;
215 db->n_bits = BSD_INIT_BITS;
219 db->incomp_count = 0;
220 db->checkpoint = CHECK_GAP;
224 * If the dictionary is full, then see if it is time to reset it.
226 * Compute the compression ratio using fixed-point arithmetic
227 * with 8 fractional bits.
229 * Since we have an infinite stream instead of a single file,
230 * watch only the local compression ratio.
232 * Since both peers must reset the dictionary at the same time even in
233 * the absence of CLEAR codes (while packets are incompressible), they
234 * must compute the same ratio.
236 static int /* 1=output CLEAR */
242 if (db->in_count >= db->checkpoint) {
243 /* age the ratio by limiting the size of the counts */
244 if (db->in_count >= RATIO_MAX
245 || db->bytes_out >= RATIO_MAX) {
246 db->in_count -= db->in_count/4;
247 db->bytes_out -= db->bytes_out/4;
250 db->checkpoint = db->in_count + CHECK_GAP;
252 if (db->max_ent >= db->maxmaxcode) {
253 /* Reset the dictionary only if the ratio is worse,
254 * or if it looks as if it has been poisoned
255 * by incompressible data.
257 * This does not overflow, because
258 * db->in_count <= RATIO_MAX.
260 new_ratio = db->in_count << RATIO_SCALE_LOG;
261 if (db->bytes_out != 0)
262 new_ratio /= db->bytes_out;
264 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
268 db->ratio = new_ratio;
278 bsd_comp_stats(state, stats)
280 struct compstat *stats;
282 struct bsd_db *db = (struct bsd_db *) state;
285 stats->unc_bytes = db->uncomp_bytes;
286 stats->unc_packets = db->uncomp_count;
287 stats->comp_bytes = db->comp_bytes;
288 stats->comp_packets = db->comp_count;
289 stats->inc_bytes = db->incomp_bytes;
290 stats->inc_packets = db->incomp_count;
291 stats->ratio = db->in_count;
293 if (stats->ratio <= 0x7fffff)
302 * Reset state, as on a CCP ResetReq.
308 struct bsd_db *db = (struct bsd_db *) state;
316 * Allocate space for a (de) compressor.
319 bsd_alloc(options, opt_len, decomp)
324 u_int newlen, hsize, hshift, maxmaxcode;
327 if (opt_len != 3 || options[0] != 0x21 || options[1] != 3
328 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
330 bits = BSD_NBITS(options[2]);
332 case 9: /* needs 82152 for both directions */
333 case 10: /* needs 84144 */
334 case 11: /* needs 88240 */
335 case 12: /* needs 96432 */
339 case 13: /* needs 176784 */
343 case 14: /* needs 353744 */
347 case 15: /* needs 691440 */
351 case 16: /* needs 1366160--far too much, */
352 /* hsize = 69001; */ /* and 69001 is too big for cptr */
353 /* hshift = 8; */ /* in struct bsd_db */
359 maxmaxcode = MAXCODE(bits);
360 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
361 KM_ALLOC(db, struct bsd_db *, newlen, KM_DEVBUF, KM_NOARG);
364 bzero(db, sizeof(*db) - sizeof(db->dict));
369 KM_ALLOC(db->lens, u_short *, (maxmaxcode+1) * sizeof(db->lens[0]),
370 KM_DEVBUF, KM_NOARG);
372 KM_FREE(db, KM_DEVBUF);
380 db->maxmaxcode = maxmaxcode;
390 struct bsd_db *db = (struct bsd_db *) state;
393 KM_FREE(db->lens, KM_DEVBUF);
394 KM_FREE(db, KM_DEVBUF);
398 bsd_comp_alloc(options, opt_len)
402 return bsd_alloc(options, opt_len, 0);
406 bsd_decomp_alloc(options, opt_len)
410 return bsd_alloc(options, opt_len, 1);
414 * Initialize the database.
417 bsd_init(db, options, opt_len, unit, mru, debug, decomp)
420 int opt_len, unit, mru, debug, decomp;
424 if (opt_len != 3 || options[0] != 0x21 || options[1] != 3
425 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
426 || BSD_NBITS(options[2]) != db->maxbits
427 || decomp && db->lens == NULL)
437 db->dict[--i].codem1 = BADCODEM1;
438 db->dict[i].cptr = 0;
454 bsd_comp_init(state, options, opt_len, unit, debug)
457 int opt_len, unit, debug;
459 return bsd_init((struct bsd_db *) state, options, opt_len,
464 bsd_decomp_init(state, options, opt_len, unit, mru, debug)
467 int opt_len, unit, mru, debug;
469 return bsd_init((struct bsd_db *) state, options, opt_len,
470 unit, mru, debug, 1);
476 * One change from the BSD compress command is that when the
477 * code size expands, we do not output a bunch of padding.
479 static int /* new slen */
480 bsd_compress(state, mret, mp, slen, maxolen)
482 struct mbuf **mret; /* return compressed mbuf chain here */
483 struct mbuf *mp; /* from here */
484 int slen; /* uncompressed length */
485 int maxolen; /* max compressed length */
487 struct bsd_db *db = (struct bsd_db *) state;
488 int hshift = db->hshift;
489 u_int max_ent = db->max_ent;
490 u_int n_bits = db->n_bits;
492 u_int32_t accm = 0, fcode;
493 struct bsd_dict *dictp;
495 int hval, disp, ent, ilen;
500 struct mbuf *m, **mnp, *clp;
502 #define PUTBYTE(v) { \
506 if (wptr >= cp_end) { \
507 m->m_len = wptr - mtod(m, u_char *); \
508 MGET(m->m_next, M_DONTWAIT, MT_DATA); \
511 if (maxolen - olen > MLEN) { \
515 wptr = mtod(m, u_char *); \
516 cp_end = wptr + M_TRAILINGSPACE(m); \
523 #define OUTPUT(ent) { \
525 accm |= ((ent) << bitno); \
527 PUTBYTE(accm >> 24); \
530 } while (bitno <= 24); \
534 * If the protocol is not in the range we're interested in,
535 * just return without compressing the packet. If it is,
536 * the protocol becomes the first byte to compress.
538 rptr = mtod(mp, u_char *);
539 ent = PPP_PROTOCOL(rptr);
540 if (ent < 0x21 || ent > 0xf9) {
545 /* Don't generate compressed packets which are larger than
546 the uncompressed packet. */
550 /* Allocate one mbuf to start with. */
551 MGET(m, M_DONTWAIT, MT_DATA);
554 if (maxolen > MLEN) {
558 wptr = mtod(m, u_char *);
559 cp_end = wptr + M_TRAILINGSPACE(m);
561 wptr = cp_end = NULL;
564 * Copy the PPP header over, changing the protocol,
565 * and install the 2-byte packet sequence number.
568 *wptr++ = PPP_ADDRESS(rptr); /* assumes the ppp header is */
569 *wptr++ = PPP_CONTROL(rptr); /* all in one mbuf */
570 *wptr++ = 0; /* change the protocol */
572 *wptr++ = db->seqno >> 8;
579 slen = mp->m_len - PPP_HDRLEN;
586 rptr = mtod(mp, u_char *);
589 continue; /* handle 0-length buffers */
595 fcode = BSD_KEY(ent, c);
596 hval = BSD_HASH(ent, c, hshift);
597 dictp = &db->dict[hval];
599 /* Validate and then check the entry. */
600 if (dictp->codem1 >= max_ent)
602 if (dictp->f.fcode == fcode) {
603 ent = dictp->codem1+1;
604 continue; /* found (prefix,suffix) */
607 /* continue probing until a match or invalid entry */
608 disp = (hval == 0) ? 1 : hval;
611 if (hval >= db->hsize)
613 dictp = &db->dict[hval];
614 if (dictp->codem1 >= max_ent)
616 } while (dictp->f.fcode != fcode);
617 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
621 OUTPUT(ent); /* output the prefix */
623 /* code -> hashtable */
624 if (max_ent < db->maxmaxcode) {
625 struct bsd_dict *dictp2;
626 /* expand code size if needed */
627 if (max_ent >= MAXCODE(n_bits))
628 db->n_bits = ++n_bits;
630 /* Invalidate old hash table entry using
631 * this code, and then take it over.
633 dictp2 = &db->dict[max_ent+1];
634 if (db->dict[dictp2->cptr].codem1 == max_ent)
635 db->dict[dictp2->cptr].codem1 = BADCODEM1;
637 dictp->codem1 = max_ent;
638 dictp->f.fcode = fcode;
640 db->max_ent = ++max_ent;
645 OUTPUT(ent); /* output the last code */
646 db->bytes_out += olen;
647 db->in_count += ilen;
649 ++db->bytes_out; /* count complete bytes */
652 OUTPUT(CLEAR); /* do not count the CLEAR */
655 * Pad dribble bits of last code with ones.
656 * Do not emit a completely useless byte of ones.
659 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
662 m->m_len = wptr - mtod(m, u_char *);
667 * Increase code size if we would have without the packet
668 * boundary and as the decompressor will.
670 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
673 db->uncomp_bytes += ilen;
675 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen) {
676 /* throw away the compressed stuff if it is longer than uncompressed */
682 db->incomp_bytes += ilen;
685 db->comp_bytes += olen + BSD_OVHD;
688 return olen + PPP_HDRLEN + BSD_OVHD;
695 * Update the "BSD Compress" dictionary on the receiver for
696 * incompressible data by pretending to compress the incoming data.
699 bsd_incomp(state, dmsg)
703 struct bsd_db *db = (struct bsd_db *) state;
704 u_int hshift = db->hshift;
705 u_int max_ent = db->max_ent;
706 u_int n_bits = db->n_bits;
707 struct bsd_dict *dictp;
717 * If the protocol is not in the range we're interested in,
718 * just return without looking at the packet. If it is,
719 * the protocol becomes the first byte to "compress".
721 rptr = mtod(dmsg, u_char *);
722 ent = PPP_PROTOCOL(rptr);
723 if (ent < 0x21 || ent > 0xf9)
728 ilen = 1; /* count the protocol as 1 byte */
730 slen = dmsg->m_len - PPP_HDRLEN;
736 rptr = mtod(dmsg, u_char *);
744 fcode = BSD_KEY(ent, c);
745 hval = BSD_HASH(ent, c, hshift);
746 dictp = &db->dict[hval];
748 /* validate and then check the entry */
749 if (dictp->codem1 >= max_ent)
751 if (dictp->f.fcode == fcode) {
752 ent = dictp->codem1+1;
753 continue; /* found (prefix,suffix) */
756 /* continue probing until a match or invalid entry */
757 disp = (hval == 0) ? 1 : hval;
760 if (hval >= db->hsize)
762 dictp = &db->dict[hval];
763 if (dictp->codem1 >= max_ent)
765 } while (dictp->f.fcode != fcode);
766 ent = dictp->codem1+1;
767 continue; /* finally found (prefix,suffix) */
769 nomatch: /* output (count) the prefix */
772 /* code -> hashtable */
773 if (max_ent < db->maxmaxcode) {
774 struct bsd_dict *dictp2;
775 /* expand code size if needed */
776 if (max_ent >= MAXCODE(n_bits))
777 db->n_bits = ++n_bits;
779 /* Invalidate previous hash table entry
780 * assigned this code, and then take it over.
782 dictp2 = &db->dict[max_ent+1];
783 if (db->dict[dictp2->cptr].codem1 == max_ent)
784 db->dict[dictp2->cptr].codem1 = BADCODEM1;
786 dictp->codem1 = max_ent;
787 dictp->f.fcode = fcode;
789 db->max_ent = ++max_ent;
790 db->lens[max_ent] = db->lens[ent]+1;
793 } while (--slen != 0);
795 bitno += n_bits; /* output (count) the last code */
796 db->bytes_out += bitno/8;
797 db->in_count += ilen;
801 db->incomp_bytes += ilen;
803 db->uncomp_bytes += ilen;
805 /* Increase code size if we would have without the packet
806 * boundary and as the decompressor will.
808 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
814 * Decompress "BSD Compress".
816 * Because of patent problems, we return DECOMP_ERROR for errors
817 * found by inspecting the input data and for system problems, but
818 * DECOMP_FATALERROR for any errors which could possibly be said to
819 * be being detected "after" decompression. For DECOMP_ERROR,
820 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
821 * infringing a patent of Motorola's if we do, so we take CCP down
824 * Given that the frame has the correct sequence number and a good FCS,
825 * errors such as invalid codes in the input most likely indicate a
826 * bug, so we return DECOMP_FATALERROR for them in order to turn off
827 * compression, even though they are detected by inspecting the input.
830 bsd_decompress(state, cmp, dmpp)
832 struct mbuf *cmp, **dmpp;
834 struct bsd_db *db = (struct bsd_db *) state;
835 u_int max_ent = db->max_ent;
837 u_int bitno = 32; /* 1st valid bit in accm */
838 u_int n_bits = db->n_bits;
839 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
840 struct bsd_dict *dictp;
841 int explen, i, seq, len;
842 u_int incode, oldcode, finchar;
843 u_char *p, *rptr, *wptr;
844 struct mbuf *m, *dmp, *mret;
845 int adrs, ctrl, ilen;
846 int space, codelen, extra;
847 struct mbuf *last, *clp;
850 * Save the address/control from the PPP header
851 * and then get the sequence number.
854 rptr = mtod(cmp, u_char *);
855 adrs = PPP_ADDRESS(rptr);
856 ctrl = PPP_CONTROL(rptr);
858 len = cmp->m_len - PPP_HDRLEN;
860 for (i = 0; i < 2; ++i) {
865 rptr = mtod(cmp, u_char *);
868 seq = (seq << 8) + *rptr++;
873 * Check the sequence number and give up if it differs from
874 * the value we're expecting.
876 if (seq != db->seqno++) {
878 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
879 db->unit, seq, db->seqno - 1);
884 * Allocate one mbuf to start with.
886 MGET(dmp, M_DONTWAIT, MT_DATA);
893 wptr = mtod(dmp, u_char *);
894 space = M_TRAILINGSPACE(dmp) - PPP_HDRLEN + 1;
897 * Fill in the ppp header, but not the last byte of the protocol
898 * (that comes from the decompressed data).
903 wptr += PPP_HDRLEN - 1;
911 if (!cmp) /* quit at end of message */
913 rptr = mtod(cmp, u_char *);
916 continue; /* handle 0-length buffers */
920 * Accumulate bytes until we have a complete code.
921 * Then get the next code, relying on the 32-bit,
922 * unsigned accm to mask the result.
925 accm |= *rptr++ << bitno;
927 if (tgtbitno < bitno)
929 incode = accm >> tgtbitno;
933 if (incode == CLEAR) {
935 * The dictionary must only be cleared at
936 * the end of a packet. But there could be an
937 * empty mbuf at the end.
939 if (len > 0 || cmp->m_next != NULL) {
940 while ((cmp = cmp->m_next) != NULL)
945 printf("bsd_decomp%d: bad CLEAR\n", db->unit);
946 return DECOMP_FATALERROR; /* probably a bug */
954 if (incode > max_ent + 2 || incode > db->maxmaxcode
955 || incode > max_ent && oldcode == CLEAR) {
958 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
959 db->unit, incode, oldcode);
960 printf("max_ent=0x%x explen=%d seqno=%d\n",
961 max_ent, explen, db->seqno);
963 return DECOMP_FATALERROR; /* probably a bug */
966 /* Special case for KwKwK string. */
967 if (incode > max_ent) {
975 codelen = db->lens[finchar];
976 explen += codelen + extra;
977 if (explen > db->mru + 1) {
980 printf("bsd_decomp%d: ran out of mru\n", db->unit);
982 while ((cmp = cmp->m_next) != NULL)
984 printf(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
985 len, finchar, codelen, explen);
988 return DECOMP_FATALERROR;
992 * For simplicity, the decoded characters go in a single mbuf,
993 * so we allocate a single extra cluster mbuf if necessary.
995 if ((space -= codelen + extra) < 0) {
996 dmp->m_len = wptr - mtod(dmp, u_char *);
997 MGET(m, M_DONTWAIT, MT_DATA);
1000 return DECOMP_ERROR;
1006 space = M_TRAILINGSPACE(m) - (codelen + extra);
1008 /* now that's what I call *compression*. */
1010 return DECOMP_ERROR;
1013 wptr = mtod(dmp, u_char *);
1017 * Decode this code and install it in the decompressed buffer.
1019 p = (wptr += codelen);
1020 while (finchar > LAST) {
1021 dictp = &db->dict[db->dict[finchar].cptr];
1023 if (--codelen <= 0 || dictp->codem1 != finchar-1)
1026 *--p = dictp->f.hs.suffix;
1027 finchar = dictp->f.hs.prefix;
1033 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1034 db->unit, codelen, incode, max_ent);
1037 if (extra) /* the KwKwK case again */
1041 * If not first code in a packet, and
1042 * if not out of code space, then allocate a new code.
1044 * Keep the hash table correct so it can be used
1045 * with uncompressed packets.
1047 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1048 struct bsd_dict *dictp2;
1052 fcode = BSD_KEY(oldcode,finchar);
1053 hval = BSD_HASH(oldcode,finchar,db->hshift);
1054 dictp = &db->dict[hval];
1056 /* look for a free hash table entry */
1057 if (dictp->codem1 < max_ent) {
1058 disp = (hval == 0) ? 1 : hval;
1061 if (hval >= db->hsize)
1063 dictp = &db->dict[hval];
1064 } while (dictp->codem1 < max_ent);
1068 * Invalidate previous hash table entry
1069 * assigned this code, and then take it over
1071 dictp2 = &db->dict[max_ent+1];
1072 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1073 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1075 dictp2->cptr = hval;
1076 dictp->codem1 = max_ent;
1077 dictp->f.fcode = fcode;
1079 db->max_ent = ++max_ent;
1080 db->lens[max_ent] = db->lens[oldcode]+1;
1082 /* Expand code size if needed. */
1083 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1084 db->n_bits = ++n_bits;
1085 tgtbitno = 32-n_bits;
1090 dmp->m_len = wptr - mtod(dmp, u_char *);
1093 * Keep the checkpoint right so that incompressible packets
1094 * clear the dictionary at the right times.
1096 db->bytes_out += ilen;
1097 db->in_count += explen;
1098 if (bsd_check(db) && db->debug) {
1099 printf("bsd_decomp%d: peer should have cleared dictionary\n",
1104 db->comp_bytes += ilen + BSD_OVHD;
1106 db->uncomp_bytes += explen;
1114 printf("bsd_decomp%d: fell off end of chain ", db->unit);
1115 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1116 incode, finchar, db->dict[finchar].cptr, max_ent);
1117 } else if (dictp->codem1 != finchar-1) {
1118 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1119 db->unit, incode, finchar);
1120 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
1121 db->dict[finchar].cptr, dictp->codem1);
1124 return DECOMP_FATALERROR;