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.8 1996/08/28 06:32:00 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 *
59 #define BSD_LITTLE_ENDIAN /* all Ultrix machines are little-endian */
62 * PPP "BSD compress" compression
63 * The differences between this compression and the classic BSD LZW
64 * source are obvious from the requirement that the classic code worked
65 * with files while this handles arbitrarily long streams that
66 * are broken into packets. They are:
68 * When the code size expands, a block of junk is not emitted by
69 * the compressor and not expected by the decompressor.
71 * New codes are not necessarily assigned every time an old
72 * code is output by the compressor. This is because a packet
73 * end forces a code to be emitted, but does not imply that a
74 * new sequence has been seen.
76 * The compression ratio is checked at the first end of a packet
77 * after the appropriate gap. Besides simplifying and speeding
78 * things up, this makes it more likely that the transmitter
79 * and receiver will agree when the dictionary is cleared when
80 * compression is not going well.
84 * A dictionary for doing BSD compress.
87 int totlen; /* length of this structure */
88 u_int hsize; /* size of the hash table */
89 u_char hshift; /* used in hash function */
90 u_char n_bits; /* current bits/code */
94 u_short seqno; /* sequence # of next packet */
95 u_int hdrlen; /* header length to preallocate */
97 u_int maxmaxcode; /* largest valid code */
98 u_int max_ent; /* largest code in use */
99 u_int in_count; /* uncompressed bytes, aged */
100 u_int bytes_out; /* compressed bytes, aged */
101 u_int ratio; /* recent compression ratio */
102 u_int checkpoint; /* when to next check the ratio */
103 u_int clear_count; /* times dictionary cleared */
104 u_int incomp_count; /* incompressible packets */
105 u_int incomp_bytes; /* incompressible bytes */
106 u_int uncomp_count; /* uncompressed packets */
107 u_int uncomp_bytes; /* uncompressed bytes */
108 u_int comp_count; /* compressed packets */
109 u_int comp_bytes; /* compressed bytes */
110 u_short *lens; /* array of lengths of codes */
112 union { /* hash value */
115 #ifdef BSD_LITTLE_ENDIAN
116 u_short prefix; /* preceding code */
117 u_char suffix; /* last character of new code */
121 u_char suffix; /* last character of new code */
122 u_short prefix; /* preceding code */
126 u_short codem1; /* output of hash table -1 */
127 u_short cptr; /* map code to hash table entry */
131 #define BSD_OVHD 2 /* BSD compress overhead/packet */
132 #define BSD_INIT_BITS BSD_MIN_BITS
134 static void *bsd_comp_alloc __P((u_char *options, int opt_len));
135 static void *bsd_decomp_alloc __P((u_char *options, int opt_len));
136 static void bsd_free __P((void *state));
137 static int bsd_comp_init __P((void *state, u_char *options, int opt_len,
138 int unit, int hdrlen, int debug));
139 static int bsd_decomp_init __P((void *state, u_char *options, int opt_len,
140 int unit, int hdrlen, int mru, int debug));
141 static int bsd_compress __P((void *state, struct mbuf **mret,
142 struct mbuf *mp, int slen, int maxolen));
143 static void bsd_incomp __P((void *state, struct mbuf *dmsg));
144 static int bsd_decompress __P((void *state, struct mbuf *cmp,
145 struct mbuf **dmpp));
146 static void bsd_reset __P((void *state));
147 static void bsd_comp_stats __P((void *state, struct compstat *stats));
150 * Procedures exported to if_ppp.c.
152 struct compressor ppp_bsd_compress = {
153 CI_BSD_COMPRESS, /* compress_proto */
154 bsd_comp_alloc, /* comp_alloc */
155 bsd_free, /* comp_free */
156 bsd_comp_init, /* comp_init */
157 bsd_reset, /* comp_reset */
158 bsd_compress, /* compress */
159 bsd_comp_stats, /* comp_stat */
160 bsd_decomp_alloc, /* decomp_alloc */
161 bsd_free, /* decomp_free */
162 bsd_decomp_init, /* decomp_init */
163 bsd_reset, /* decomp_reset */
164 bsd_decompress, /* decompress */
165 bsd_incomp, /* incomp */
166 bsd_comp_stats, /* decomp_stat */
170 * Some useful mbuf macros not in mbuf.h.
172 #define M_IS_CLUSTER(m) ((m)->m_off > MMAXOFF)
174 #define M_TRAILINGSPACE(m) \
175 ((M_IS_CLUSTER(m) ? (u_int)(m)->m_clptr + M_CLUSTERSZ : MSIZE) \
176 - ((m)->m_off + (m)->m_len))
179 * the next two codes should not be changed lightly, as they must not
180 * lie within the contiguous general code space.
182 #define CLEAR 256 /* table clear output code */
183 #define FIRST 257 /* first free entry */
186 #define MAXCODE(b) ((1 << (b)) - 1)
187 #define BADCODEM1 MAXCODE(BSD_MAX_BITS)
189 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
190 ^ (u_int32_t)(prefix))
191 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
192 + (u_int32_t)(prefix))
194 #define CHECK_GAP 10000 /* Ratio check interval */
196 #define RATIO_SCALE_LOG 8
197 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
198 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
200 static void bsd_clear __P((struct bsd_db *));
201 static int bsd_check __P((struct bsd_db *));
202 static void *bsd_alloc __P((u_char *, int, int));
203 static int bsd_init __P((struct bsd_db *, u_char *, int, int, int, int,
207 * clear the dictionary
214 db->max_ent = FIRST-1;
215 db->n_bits = BSD_INIT_BITS;
219 db->checkpoint = CHECK_GAP;
223 * If the dictionary is full, then see if it is time to reset it.
225 * Compute the compression ratio using fixed-point arithmetic
226 * with 8 fractional bits.
228 * Since we have an infinite stream instead of a single file,
229 * watch only the local compression ratio.
231 * Since both peers must reset the dictionary at the same time even in
232 * the absence of CLEAR codes (while packets are incompressible), they
233 * must compute the same ratio.
235 static int /* 1=output CLEAR */
241 if (db->in_count >= db->checkpoint) {
242 /* age the ratio by limiting the size of the counts */
243 if (db->in_count >= RATIO_MAX
244 || db->bytes_out >= RATIO_MAX) {
245 db->in_count -= db->in_count/4;
246 db->bytes_out -= db->bytes_out/4;
249 db->checkpoint = db->in_count + CHECK_GAP;
251 if (db->max_ent >= db->maxmaxcode) {
252 /* Reset the dictionary only if the ratio is worse,
253 * or if it looks as if it has been poisoned
254 * by incompressible data.
256 * This does not overflow, because
257 * db->in_count <= RATIO_MAX.
259 new_ratio = db->in_count << RATIO_SCALE_LOG;
260 if (db->bytes_out != 0)
261 new_ratio /= db->bytes_out;
263 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
267 db->ratio = new_ratio;
277 bsd_comp_stats(state, stats)
279 struct compstat *stats;
281 struct bsd_db *db = (struct bsd_db *) state;
284 stats->unc_bytes = db->uncomp_bytes;
285 stats->unc_packets = db->uncomp_count;
286 stats->comp_bytes = db->comp_bytes;
287 stats->comp_packets = db->comp_count;
288 stats->inc_bytes = db->incomp_bytes;
289 stats->inc_packets = db->incomp_count;
290 stats->ratio = db->in_count;
292 if (stats->ratio <= 0x7fffff)
301 * Reset state, as on a CCP ResetReq.
307 struct bsd_db *db = (struct bsd_db *) state;
315 * Allocate space for a (de) compressor.
318 bsd_alloc(options, opt_len, decomp)
323 u_int newlen, hsize, hshift, maxmaxcode;
326 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
327 || options[1] != CILEN_BSD_COMPRESS
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, hdrlen, mru, debug, decomp)
420 int opt_len, unit, hdrlen, mru, debug, decomp;
424 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
425 || options[1] != CILEN_BSD_COMPRESS
426 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
427 || BSD_NBITS(options[2]) != db->maxbits
428 || decomp && db->lens == NULL)
438 db->dict[--i].codem1 = BADCODEM1;
439 db->dict[i].cptr = 0;
456 bsd_comp_init(state, options, opt_len, unit, hdrlen, debug)
459 int opt_len, unit, hdrlen, debug;
461 return bsd_init((struct bsd_db *) state, options, opt_len,
462 unit, hdrlen, 0, debug, 0);
466 bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
469 int opt_len, unit, hdrlen, mru, debug;
471 return bsd_init((struct bsd_db *) state, options, opt_len,
472 unit, hdrlen, mru, debug, 1);
478 * One change from the BSD compress command is that when the
479 * code size expands, we do not output a bunch of padding.
481 static int /* new slen */
482 bsd_compress(state, mret, mp, slen, maxolen)
484 struct mbuf **mret; /* return compressed mbuf chain here */
485 struct mbuf *mp; /* from here */
486 int slen; /* uncompressed length */
487 int maxolen; /* max compressed length */
489 struct bsd_db *db = (struct bsd_db *) state;
490 int hshift = db->hshift;
491 u_int max_ent = db->max_ent;
492 u_int n_bits = db->n_bits;
494 u_int32_t accm = 0, fcode;
495 struct bsd_dict *dictp;
497 int hval, disp, ent, ilen;
502 struct mbuf *m, *clp;
504 #define PUTBYTE(v) { \
508 if (wptr >= cp_end) { \
509 m->m_len = wptr - mtod(m, u_char *); \
510 MGET(m->m_next, M_DONTWAIT, MT_DATA); \
513 if (maxolen - olen > MLEN) { \
517 wptr = mtod(m, u_char *); \
518 cp_end = wptr + M_TRAILINGSPACE(m); \
525 #define OUTPUT(ent) { \
527 accm |= ((ent) << bitno); \
529 PUTBYTE(accm >> 24); \
532 } while (bitno <= 24); \
536 * If the protocol is not in the range we're interested in,
537 * just return without compressing the packet. If it is,
538 * the protocol becomes the first byte to compress.
540 rptr = mtod(mp, u_char *);
541 ent = PPP_PROTOCOL(rptr);
542 if (ent < 0x21 || ent > 0xf9) {
547 /* Don't generate compressed packets which are larger than
548 the uncompressed packet. */
552 /* Allocate one mbuf to start with. */
553 MGET(m, M_DONTWAIT, MT_DATA);
556 if (maxolen + db->hdrlen > MLEN) {
559 m->m_off += db->hdrlen;
561 wptr = mtod(m, u_char *);
562 cp_end = wptr + M_TRAILINGSPACE(m);
564 wptr = cp_end = NULL;
567 * Copy the PPP header over, changing the protocol,
568 * and install the 2-byte packet sequence number.
571 *wptr++ = PPP_ADDRESS(rptr); /* assumes the ppp header is */
572 *wptr++ = PPP_CONTROL(rptr); /* all in one mbuf */
573 *wptr++ = 0; /* change the protocol */
575 *wptr++ = db->seqno >> 8;
582 slen = mp->m_len - PPP_HDRLEN;
589 rptr = mtod(mp, u_char *);
592 continue; /* handle 0-length buffers */
598 fcode = BSD_KEY(ent, c);
599 hval = BSD_HASH(ent, c, hshift);
600 dictp = &db->dict[hval];
602 /* Validate and then check the entry. */
603 if (dictp->codem1 >= max_ent)
605 if (dictp->f.fcode == fcode) {
606 ent = dictp->codem1+1;
607 continue; /* found (prefix,suffix) */
610 /* continue probing until a match or invalid entry */
611 disp = (hval == 0) ? 1 : hval;
614 if (hval >= db->hsize)
616 dictp = &db->dict[hval];
617 if (dictp->codem1 >= max_ent)
619 } while (dictp->f.fcode != fcode);
620 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
624 OUTPUT(ent); /* output the prefix */
626 /* code -> hashtable */
627 if (max_ent < db->maxmaxcode) {
628 struct bsd_dict *dictp2;
629 /* expand code size if needed */
630 if (max_ent >= MAXCODE(n_bits))
631 db->n_bits = ++n_bits;
633 /* Invalidate old hash table entry using
634 * this code, and then take it over.
636 dictp2 = &db->dict[max_ent+1];
637 if (db->dict[dictp2->cptr].codem1 == max_ent)
638 db->dict[dictp2->cptr].codem1 = BADCODEM1;
640 dictp->codem1 = max_ent;
641 dictp->f.fcode = fcode;
643 db->max_ent = ++max_ent;
648 OUTPUT(ent); /* output the last code */
649 db->bytes_out += olen;
650 db->in_count += ilen;
652 ++db->bytes_out; /* count complete bytes */
655 OUTPUT(CLEAR); /* do not count the CLEAR */
658 * Pad dribble bits of last code with ones.
659 * Do not emit a completely useless byte of ones.
662 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
665 m->m_len = wptr - mtod(m, u_char *);
670 * Increase code size if we would have without the packet
671 * boundary and as the decompressor will.
673 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
676 db->uncomp_bytes += ilen;
678 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen) {
679 /* throw away the compressed stuff if it is longer than uncompressed */
685 db->incomp_bytes += ilen;
688 db->comp_bytes += olen + BSD_OVHD;
691 return olen + PPP_HDRLEN + BSD_OVHD;
698 * Update the "BSD Compress" dictionary on the receiver for
699 * incompressible data by pretending to compress the incoming data.
702 bsd_incomp(state, dmsg)
706 struct bsd_db *db = (struct bsd_db *) state;
707 u_int hshift = db->hshift;
708 u_int max_ent = db->max_ent;
709 u_int n_bits = db->n_bits;
710 struct bsd_dict *dictp;
720 * If the protocol is not in the range we're interested in,
721 * just return without looking at the packet. If it is,
722 * the protocol becomes the first byte to "compress".
724 rptr = mtod(dmsg, u_char *);
725 ent = PPP_PROTOCOL(rptr);
726 if (ent < 0x21 || ent > 0xf9)
730 ilen = 1; /* count the protocol as 1 byte */
732 slen = dmsg->m_len - PPP_HDRLEN;
738 rptr = mtod(dmsg, u_char *);
746 fcode = BSD_KEY(ent, c);
747 hval = BSD_HASH(ent, c, hshift);
748 dictp = &db->dict[hval];
750 /* validate and then check the entry */
751 if (dictp->codem1 >= max_ent)
753 if (dictp->f.fcode == fcode) {
754 ent = dictp->codem1+1;
755 continue; /* found (prefix,suffix) */
758 /* continue probing until a match or invalid entry */
759 disp = (hval == 0) ? 1 : hval;
762 if (hval >= db->hsize)
764 dictp = &db->dict[hval];
765 if (dictp->codem1 >= max_ent)
767 } while (dictp->f.fcode != fcode);
768 ent = dictp->codem1+1;
769 continue; /* finally found (prefix,suffix) */
771 nomatch: /* output (count) the prefix */
774 /* code -> hashtable */
775 if (max_ent < db->maxmaxcode) {
776 struct bsd_dict *dictp2;
777 /* expand code size if needed */
778 if (max_ent >= MAXCODE(n_bits))
779 db->n_bits = ++n_bits;
781 /* Invalidate previous hash table entry
782 * assigned this code, and then take it over.
784 dictp2 = &db->dict[max_ent+1];
785 if (db->dict[dictp2->cptr].codem1 == max_ent)
786 db->dict[dictp2->cptr].codem1 = BADCODEM1;
788 dictp->codem1 = max_ent;
789 dictp->f.fcode = fcode;
791 db->max_ent = ++max_ent;
792 db->lens[max_ent] = db->lens[ent]+1;
795 } while (--slen != 0);
797 bitno += n_bits; /* output (count) the last code */
798 db->bytes_out += bitno/8;
799 db->in_count += ilen;
803 db->incomp_bytes += ilen;
805 db->uncomp_bytes += ilen;
807 /* Increase code size if we would have without the packet
808 * boundary and as the decompressor will.
810 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
816 * Decompress "BSD Compress".
818 * Because of patent problems, we return DECOMP_ERROR for errors
819 * found by inspecting the input data and for system problems, but
820 * DECOMP_FATALERROR for any errors which could possibly be said to
821 * be being detected "after" decompression. For DECOMP_ERROR,
822 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
823 * infringing a patent of Motorola's if we do, so we take CCP down
826 * Given that the frame has the correct sequence number and a good FCS,
827 * errors such as invalid codes in the input most likely indicate a
828 * bug, so we return DECOMP_FATALERROR for them in order to turn off
829 * compression, even though they are detected by inspecting the input.
832 bsd_decompress(state, cmp, dmpp)
834 struct mbuf *cmp, **dmpp;
836 struct bsd_db *db = (struct bsd_db *) state;
837 u_int max_ent = db->max_ent;
839 u_int bitno = 32; /* 1st valid bit in accm */
840 u_int n_bits = db->n_bits;
841 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
842 struct bsd_dict *dictp;
843 int explen, i, seq, len;
844 u_int incode, oldcode, finchar;
845 u_char *p, *rptr, *wptr;
846 struct mbuf *m, *dmp, *mret;
847 int adrs, ctrl, ilen;
848 int space, codelen, extra;
852 * Save the address/control from the PPP header
853 * and then get the sequence number.
856 rptr = mtod(cmp, u_char *);
857 adrs = PPP_ADDRESS(rptr);
858 ctrl = PPP_CONTROL(rptr);
860 len = cmp->m_len - PPP_HDRLEN;
862 for (i = 0; i < 2; ++i) {
867 rptr = mtod(cmp, u_char *);
870 seq = (seq << 8) + *rptr++;
875 * Check the sequence number and give up if it differs from
876 * the value we're expecting.
878 if (seq != db->seqno) {
880 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
881 db->unit, seq, db->seqno - 1);
887 * Allocate one mbuf to start with.
889 MGET(dmp, M_DONTWAIT, MT_DATA);
896 dmp->m_off += db->hdrlen;
897 wptr = mtod(dmp, u_char *);
898 space = M_TRAILINGSPACE(dmp) - PPP_HDRLEN + 1;
901 * Fill in the ppp header, but not the last byte of the protocol
902 * (that comes from the decompressed data).
907 wptr += PPP_HDRLEN - 1;
915 if (!cmp) /* quit at end of message */
917 rptr = mtod(cmp, u_char *);
920 continue; /* handle 0-length buffers */
924 * Accumulate bytes until we have a complete code.
925 * Then get the next code, relying on the 32-bit,
926 * unsigned accm to mask the result.
929 accm |= *rptr++ << bitno;
931 if (tgtbitno < bitno)
933 incode = accm >> tgtbitno;
937 if (incode == CLEAR) {
939 * The dictionary must only be cleared at
940 * the end of a packet. But there could be an
941 * empty mbuf at the end.
943 if (len > 0 || cmp->m_next != NULL) {
944 while ((cmp = cmp->m_next) != NULL)
949 printf("bsd_decomp%d: bad CLEAR\n", db->unit);
950 return DECOMP_FATALERROR; /* probably a bug */
958 if (incode > max_ent + 2 || incode > db->maxmaxcode
959 || incode > max_ent && oldcode == CLEAR) {
962 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
963 db->unit, incode, oldcode);
964 printf("max_ent=0x%x explen=%d seqno=%d\n",
965 max_ent, explen, db->seqno);
967 return DECOMP_FATALERROR; /* probably a bug */
970 /* Special case for KwKwK string. */
971 if (incode > max_ent) {
979 codelen = db->lens[finchar];
980 explen += codelen + extra;
981 if (explen > db->mru + 1) {
984 printf("bsd_decomp%d: ran out of mru\n", db->unit);
986 while ((cmp = cmp->m_next) != NULL)
988 printf(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
989 len, finchar, codelen, explen);
992 return DECOMP_FATALERROR;
996 * For simplicity, the decoded characters go in a single mbuf,
997 * so we allocate a single extra cluster mbuf if necessary.
999 if ((space -= codelen + extra) < 0) {
1000 dmp->m_len = wptr - mtod(dmp, u_char *);
1001 MGET(m, M_DONTWAIT, MT_DATA);
1004 return DECOMP_ERROR;
1010 space = M_TRAILINGSPACE(m) - (codelen + extra);
1012 /* now that's what I call *compression*. */
1014 return DECOMP_ERROR;
1017 wptr = mtod(dmp, u_char *);
1021 * Decode this code and install it in the decompressed buffer.
1023 p = (wptr += codelen);
1024 while (finchar > LAST) {
1025 dictp = &db->dict[db->dict[finchar].cptr];
1027 if (--codelen <= 0 || dictp->codem1 != finchar-1)
1030 *--p = dictp->f.hs.suffix;
1031 finchar = dictp->f.hs.prefix;
1037 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1038 db->unit, codelen, incode, max_ent);
1041 if (extra) /* the KwKwK case again */
1045 * If not first code in a packet, and
1046 * if not out of code space, then allocate a new code.
1048 * Keep the hash table correct so it can be used
1049 * with uncompressed packets.
1051 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1052 struct bsd_dict *dictp2;
1056 fcode = BSD_KEY(oldcode,finchar);
1057 hval = BSD_HASH(oldcode,finchar,db->hshift);
1058 dictp = &db->dict[hval];
1060 /* look for a free hash table entry */
1061 if (dictp->codem1 < max_ent) {
1062 disp = (hval == 0) ? 1 : hval;
1065 if (hval >= db->hsize)
1067 dictp = &db->dict[hval];
1068 } while (dictp->codem1 < max_ent);
1072 * Invalidate previous hash table entry
1073 * assigned this code, and then take it over
1075 dictp2 = &db->dict[max_ent+1];
1076 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1077 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1079 dictp2->cptr = hval;
1080 dictp->codem1 = max_ent;
1081 dictp->f.fcode = fcode;
1083 db->max_ent = ++max_ent;
1084 db->lens[max_ent] = db->lens[oldcode]+1;
1086 /* Expand code size if needed. */
1087 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1088 db->n_bits = ++n_bits;
1089 tgtbitno = 32-n_bits;
1094 dmp->m_len = wptr - mtod(dmp, u_char *);
1097 * Keep the checkpoint right so that incompressible packets
1098 * clear the dictionary at the right times.
1100 db->bytes_out += ilen;
1101 db->in_count += explen;
1102 if (bsd_check(db) && db->debug) {
1103 printf("bsd_decomp%d: peer should have cleared dictionary\n",
1108 db->comp_bytes += ilen + BSD_OVHD;
1110 db->uncomp_bytes += explen;
1118 printf("bsd_decomp%d: fell off end of chain ", db->unit);
1119 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1120 incode, finchar, db->dict[finchar].cptr, max_ent);
1121 } else if (dictp->codem1 != finchar-1) {
1122 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1123 db->unit, incode, finchar);
1124 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
1125 db->dict[finchar].cptr, dictp->codem1);
1128 return DECOMP_FATALERROR;
1131 #endif /* DO_BSD_COMPRESS */