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 BSD-derived systems.
43 * $FreeBSD: src/sys/net/bsd_comp.c,v 1.11.2.1 2002/04/14 21:41:48 luigi Exp $
44 * $DragonFly: src/sys/net/bsd_comp.c,v 1.10 2006/09/05 00:55:46 dillon Exp $
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
51 #include <net/ppp_layer/ppp_defs.h>
53 #define PACKETPTR struct mbuf *
54 #include <net/ppp_layer/ppp_comp.h>
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 * A dictionary for doing BSD compress.
83 int totlen; /* length of this structure */
84 u_int hsize; /* size of the hash table */
85 u_char hshift; /* used in hash function */
86 u_char n_bits; /* current bits/code */
90 u_int16_t seqno; /* sequence # of next packet */
91 u_int hdrlen; /* header length to preallocate */
93 u_int maxmaxcode; /* largest valid code */
94 u_int max_ent; /* largest code in use */
95 u_int in_count; /* uncompressed bytes, aged */
96 u_int bytes_out; /* compressed bytes, aged */
97 u_int ratio; /* recent compression ratio */
98 u_int checkpoint; /* when to next check the ratio */
99 u_int clear_count; /* times dictionary cleared */
100 u_int incomp_count; /* incompressible packets */
101 u_int incomp_bytes; /* incompressible bytes */
102 u_int uncomp_count; /* uncompressed packets */
103 u_int uncomp_bytes; /* uncompressed bytes */
104 u_int comp_count; /* compressed packets */
105 u_int comp_bytes; /* compressed bytes */
106 u_int16_t *lens; /* array of lengths of codes */
108 union { /* hash value */
111 #if BYTE_ORDER == LITTLE_ENDIAN
112 u_int16_t prefix; /* preceding code */
113 u_char suffix; /* last character of new code */
117 u_char suffix; /* last character of new code */
118 u_int16_t prefix; /* preceding code */
122 u_int16_t codem1; /* output of hash table -1 */
123 u_int16_t cptr; /* map code to hash table entry */
127 #define BSD_OVHD 2 /* BSD compress overhead/packet */
128 #define BSD_INIT_BITS BSD_MIN_BITS
130 static void bsd_clear (struct bsd_db *db);
131 static int bsd_check (struct bsd_db *db);
132 static void *bsd_alloc (u_char *options, int opt_len, int decomp);
133 static int bsd_init (struct bsd_db *db, u_char *options, int opt_len,
134 int unit, int hdrlen, int mru, int debug,
136 static void *bsd_comp_alloc (u_char *options, int opt_len);
137 static void *bsd_decomp_alloc (u_char *options, int opt_len);
138 static void bsd_free (void *state);
139 static int bsd_comp_init (void *state, u_char *options, int opt_len,
140 int unit, int hdrlen, int debug);
141 static int bsd_decomp_init (void *state, u_char *options, int opt_len,
142 int unit, int hdrlen, int mru, int debug);
143 static int bsd_compress (void *state, struct mbuf **mret,
144 struct mbuf *mp, int slen, int maxolen);
145 static void bsd_incomp (void *state, struct mbuf *dmsg);
146 static int bsd_decompress (void *state, struct mbuf *cmp,
148 static void bsd_reset (void *state);
149 static void bsd_comp_stats (void *state, struct compstat *stats);
152 * Procedures exported to if_ppp.c.
154 struct compressor ppp_bsd_compress = {
155 CI_BSD_COMPRESS, /* compress_proto */
156 bsd_comp_alloc, /* comp_alloc */
157 bsd_free, /* comp_free */
158 bsd_comp_init, /* comp_init */
159 bsd_reset, /* comp_reset */
160 bsd_compress, /* compress */
161 bsd_comp_stats, /* comp_stat */
162 bsd_decomp_alloc, /* decomp_alloc */
163 bsd_free, /* decomp_free */
164 bsd_decomp_init, /* decomp_init */
165 bsd_reset, /* decomp_reset */
166 bsd_decompress, /* decompress */
167 bsd_incomp, /* incomp */
168 bsd_comp_stats, /* decomp_stat */
172 * the next two codes should not be changed lightly, as they must not
173 * lie within the contiguous general code space.
175 #define CLEAR 256 /* table clear output code */
176 #define FIRST 257 /* first free entry */
179 #define MAXCODE(b) ((1 << (b)) - 1)
180 #define BADCODEM1 MAXCODE(BSD_MAX_BITS)
182 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
183 ^ (u_int32_t)(prefix))
184 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
185 + (u_int32_t)(prefix))
187 #define CHECK_GAP 10000 /* Ratio check interval */
189 #define RATIO_SCALE_LOG 8
190 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
191 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
194 * clear the dictionary
197 bsd_clear(struct bsd_db *db)
200 db->max_ent = FIRST-1;
201 db->n_bits = BSD_INIT_BITS;
205 db->checkpoint = CHECK_GAP;
209 * If the dictionary is full, then see if it is time to reset it.
211 * Compute the compression ratio using fixed-point arithmetic
212 * with 8 fractional bits.
214 * Since we have an infinite stream instead of a single file,
215 * watch only the local compression ratio.
217 * Since both peers must reset the dictionary at the same time even in
218 * the absence of CLEAR codes (while packets are incompressible), they
219 * must compute the same ratio.
221 * Return 1=output CLEAR
224 bsd_check(struct bsd_db *db)
228 if (db->in_count >= db->checkpoint) {
229 /* age the ratio by limiting the size of the counts */
230 if (db->in_count >= RATIO_MAX
231 || db->bytes_out >= RATIO_MAX) {
232 db->in_count -= db->in_count/4;
233 db->bytes_out -= db->bytes_out/4;
236 db->checkpoint = db->in_count + CHECK_GAP;
238 if (db->max_ent >= db->maxmaxcode) {
239 /* Reset the dictionary only if the ratio is worse,
240 * or if it looks as if it has been poisoned
241 * by incompressible data.
243 * This does not overflow, because
244 * db->in_count <= RATIO_MAX.
246 new_ratio = db->in_count << RATIO_SCALE_LOG;
247 if (db->bytes_out != 0)
248 new_ratio /= db->bytes_out;
250 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
254 db->ratio = new_ratio;
264 bsd_comp_stats(void *state, struct compstat *stats)
266 struct bsd_db *db = (struct bsd_db *) state;
269 stats->unc_bytes = db->uncomp_bytes;
270 stats->unc_packets = db->uncomp_count;
271 stats->comp_bytes = db->comp_bytes;
272 stats->comp_packets = db->comp_count;
273 stats->inc_bytes = db->incomp_bytes;
274 stats->inc_packets = db->incomp_count;
275 stats->ratio = db->in_count;
277 if (stats->ratio <= 0x7fffff)
286 * Reset state, as on a CCP ResetReq.
289 bsd_reset(void *state)
291 struct bsd_db *db = (struct bsd_db *) state;
299 * Allocate space for a (de) compressor.
302 bsd_alloc(u_char *options, int opt_len, int decomp)
305 u_int newlen, hsize, hshift, maxmaxcode;
308 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
309 || options[1] != CILEN_BSD_COMPRESS
310 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
312 bits = BSD_NBITS(options[2]);
314 case 9: /* needs 82152 for both directions */
315 case 10: /* needs 84144 */
316 case 11: /* needs 88240 */
317 case 12: /* needs 96432 */
321 case 13: /* needs 176784 */
325 case 14: /* needs 353744 */
329 case 15: /* needs 691440 */
333 case 16: /* needs 1366160--far too much, */
334 /* hsize = 69001; */ /* and 69001 is too big for cptr */
335 /* hshift = 8; */ /* in struct bsd_db */
341 maxmaxcode = MAXCODE(bits);
342 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
343 MALLOC(db, struct bsd_db *, newlen, M_DEVBUF, M_WAITOK);
344 bzero(db, sizeof(*db) - sizeof(db->dict));
349 MALLOC(db->lens, u_int16_t *, (maxmaxcode+1) * sizeof(db->lens[0]),
356 db->maxmaxcode = maxmaxcode;
363 bsd_free(void *state)
365 struct bsd_db *db = (struct bsd_db *) state;
368 kfree(db->lens, M_DEVBUF);
373 bsd_comp_alloc(u_char *options, int opt_len)
375 return bsd_alloc(options, opt_len, 0);
379 bsd_decomp_alloc(u_char *options, int opt_len)
381 return bsd_alloc(options, opt_len, 1);
385 * Initialize the database.
388 bsd_init(struct bsd_db *db, u_char *options, int opt_len, int unit,
389 int hdrlen, int mru, int debug, int decomp)
393 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
394 || options[1] != CILEN_BSD_COMPRESS
395 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
396 || BSD_NBITS(options[2]) != db->maxbits
397 || (decomp && db->lens == NULL))
407 db->dict[--i].codem1 = BADCODEM1;
408 db->dict[i].cptr = 0;
425 bsd_comp_init(void *state, u_char *options, int opt_len, int unit,
426 int hdrlen, int debug)
428 return bsd_init((struct bsd_db *) state, options, opt_len,
429 unit, hdrlen, 0, debug, 0);
433 bsd_decomp_init(void *state, u_char *options, int opt_len,
434 int unit, int hdrlen, int mru, int debug)
436 return bsd_init((struct bsd_db *) state, options, opt_len,
437 unit, hdrlen, mru, debug, 1);
443 * One change from the BSD compress command is that when the
444 * code size expands, we do not output a bunch of padding.
445 * **mret - return compressed mbuf chain here
447 * slen - uncompressed length
448 * maxolen - max compressed length
453 bsd_compress(void *state, struct mbuf **mret, struct mbuf *mp,
454 int slen, int maxolen)
456 struct bsd_db *db = (struct bsd_db *) state;
457 int hshift = db->hshift;
458 u_int max_ent = db->max_ent;
459 u_int n_bits = db->n_bits;
461 u_int32_t accm = 0, fcode;
462 struct bsd_dict *dictp;
464 int hval, disp, ent, ilen;
470 #define PUTBYTE(v) { \
474 if (wptr >= cp_end) { \
475 m->m_len = wptr - mtod(m, u_char *); \
476 MGET(m->m_next, MB_DONTWAIT, MT_DATA); \
480 if (maxolen - olen > MLEN) \
481 MCLGET(m, MB_DONTWAIT); \
482 wptr = mtod(m, u_char *); \
483 cp_end = wptr + M_TRAILINGSPACE(m); \
490 #define OUTPUT(ent) { \
492 accm |= ((ent) << bitno); \
494 PUTBYTE(accm >> 24); \
497 } while (bitno <= 24); \
501 * If the protocol is not in the range we're interested in,
502 * just return without compressing the packet. If it is,
503 * the protocol becomes the first byte to compress.
505 rptr = mtod(mp, u_char *);
506 ent = PPP_PROTOCOL(rptr);
507 if (ent < 0x21 || ent > 0xf9) {
512 /* Don't generate compressed packets which are larger than
513 the uncompressed packet. */
517 /* Allocate one mbuf to start with. */
518 MGET(m, MB_DONTWAIT, MT_DATA);
522 if (maxolen + db->hdrlen > MLEN)
523 MCLGET(m, MB_DONTWAIT);
524 m->m_data += db->hdrlen;
525 wptr = mtod(m, u_char *);
526 cp_end = wptr + M_TRAILINGSPACE(m);
528 wptr = cp_end = NULL;
531 * Copy the PPP header over, changing the protocol,
532 * and install the 2-byte packet sequence number.
535 *wptr++ = PPP_ADDRESS(rptr); /* assumes the ppp header is */
536 *wptr++ = PPP_CONTROL(rptr); /* all in one mbuf */
537 *wptr++ = 0; /* change the protocol */
539 *wptr++ = db->seqno >> 8;
546 slen = mp->m_len - PPP_HDRLEN;
553 rptr = mtod(mp, u_char *);
556 continue; /* handle 0-length buffers */
562 fcode = BSD_KEY(ent, c);
563 hval = BSD_HASH(ent, c, hshift);
564 dictp = &db->dict[hval];
566 /* Validate and then check the entry. */
567 if (dictp->codem1 >= max_ent)
569 if (dictp->f.fcode == fcode) {
570 ent = dictp->codem1+1;
571 continue; /* found (prefix,suffix) */
574 /* continue probing until a match or invalid entry */
575 disp = (hval == 0) ? 1 : hval;
578 if (hval >= db->hsize)
580 dictp = &db->dict[hval];
581 if (dictp->codem1 >= max_ent)
583 } while (dictp->f.fcode != fcode);
584 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
588 OUTPUT(ent); /* output the prefix */
590 /* code -> hashtable */
591 if (max_ent < db->maxmaxcode) {
592 struct bsd_dict *dictp2;
593 /* expand code size if needed */
594 if (max_ent >= MAXCODE(n_bits))
595 db->n_bits = ++n_bits;
597 /* Invalidate old hash table entry using
598 * this code, and then take it over.
600 dictp2 = &db->dict[max_ent+1];
601 if (db->dict[dictp2->cptr].codem1 == max_ent)
602 db->dict[dictp2->cptr].codem1 = BADCODEM1;
604 dictp->codem1 = max_ent;
605 dictp->f.fcode = fcode;
607 db->max_ent = ++max_ent;
612 OUTPUT(ent); /* output the last code */
613 db->bytes_out += olen;
614 db->in_count += ilen;
616 ++db->bytes_out; /* count complete bytes */
619 OUTPUT(CLEAR); /* do not count the CLEAR */
622 * Pad dribble bits of last code with ones.
623 * Do not emit a completely useless byte of ones.
626 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
629 m->m_len = wptr - mtod(m, u_char *);
634 * Increase code size if we would have without the packet
635 * boundary and as the decompressor will.
637 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
640 db->uncomp_bytes += ilen;
642 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen) {
643 /* throw away the compressed stuff if it is longer than uncompressed */
649 db->incomp_bytes += ilen;
652 db->comp_bytes += olen + BSD_OVHD;
655 return olen + PPP_HDRLEN + BSD_OVHD;
662 * Update the "BSD Compress" dictionary on the receiver for
663 * incompressible data by pretending to compress the incoming data.
666 bsd_incomp(void *state, struct mbuf *dmsg)
668 struct bsd_db *db = (struct bsd_db *) state;
669 u_int hshift = db->hshift;
670 u_int max_ent = db->max_ent;
671 u_int n_bits = db->n_bits;
672 struct bsd_dict *dictp;
675 u_int32_t hval, disp;
682 * If the protocol is not in the range we're interested in,
683 * just return without looking at the packet. If it is,
684 * the protocol becomes the first byte to "compress".
686 rptr = mtod(dmsg, u_char *);
687 ent = PPP_PROTOCOL(rptr);
688 if (ent < 0x21 || ent > 0xf9)
692 ilen = 1; /* count the protocol as 1 byte */
694 slen = dmsg->m_len - PPP_HDRLEN;
700 rptr = mtod(dmsg, u_char *);
708 fcode = BSD_KEY(ent, c);
709 hval = BSD_HASH(ent, c, hshift);
710 dictp = &db->dict[hval];
712 /* validate and then check the entry */
713 if (dictp->codem1 >= max_ent)
715 if (dictp->f.fcode == fcode) {
716 ent = dictp->codem1+1;
717 continue; /* found (prefix,suffix) */
720 /* continue probing until a match or invalid entry */
721 disp = (hval == 0) ? 1 : hval;
724 if (hval >= db->hsize)
726 dictp = &db->dict[hval];
727 if (dictp->codem1 >= max_ent)
729 } while (dictp->f.fcode != fcode);
730 ent = dictp->codem1+1;
731 continue; /* finally found (prefix,suffix) */
733 nomatch: /* output (count) the prefix */
736 /* code -> hashtable */
737 if (max_ent < db->maxmaxcode) {
738 struct bsd_dict *dictp2;
739 /* expand code size if needed */
740 if (max_ent >= MAXCODE(n_bits))
741 db->n_bits = ++n_bits;
743 /* Invalidate previous hash table entry
744 * assigned this code, and then take it over.
746 dictp2 = &db->dict[max_ent+1];
747 if (db->dict[dictp2->cptr].codem1 == max_ent)
748 db->dict[dictp2->cptr].codem1 = BADCODEM1;
750 dictp->codem1 = max_ent;
751 dictp->f.fcode = fcode;
753 db->max_ent = ++max_ent;
754 db->lens[max_ent] = db->lens[ent]+1;
757 } while (--slen != 0);
759 bitno += n_bits; /* output (count) the last code */
760 db->bytes_out += bitno/8;
761 db->in_count += ilen;
765 db->incomp_bytes += ilen;
767 db->uncomp_bytes += ilen;
769 /* Increase code size if we would have without the packet
770 * boundary and as the decompressor will.
772 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
778 * Decompress "BSD Compress".
780 * Because of patent problems, we return DECOMP_ERROR for errors
781 * found by inspecting the input data and for system problems, but
782 * DECOMP_FATALERROR for any errors which could possibly be said to
783 * be being detected "after" decompression. For DECOMP_ERROR,
784 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
785 * infringing a patent of Motorola's if we do, so we take CCP down
788 * Given that the frame has the correct sequence number and a good FCS,
789 * errors such as invalid codes in the input most likely indicate a
790 * bug, so we return DECOMP_FATALERROR for them in order to turn off
791 * compression, even though they are detected by inspecting the input.
794 bsd_decompress(void *state, struct mbuf *cmp, struct mbuf **dmpp)
796 struct bsd_db *db = (struct bsd_db *) state;
797 u_int max_ent = db->max_ent;
799 u_int bitno = 32; /* 1st valid bit in accm */
800 u_int n_bits = db->n_bits;
801 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
802 struct bsd_dict *dictp;
803 int explen, i, seq, len;
804 u_int incode, oldcode, finchar;
805 u_char *p, *rptr, *wptr;
806 struct mbuf *m, *dmp, *mret;
807 int adrs, ctrl, ilen;
808 int space, codelen, extra;
811 * Save the address/control from the PPP header
812 * and then get the sequence number.
815 rptr = mtod(cmp, u_char *);
816 adrs = PPP_ADDRESS(rptr);
817 ctrl = PPP_CONTROL(rptr);
819 len = cmp->m_len - PPP_HDRLEN;
821 for (i = 0; i < 2; ++i) {
826 rptr = mtod(cmp, u_char *);
829 seq = (seq << 8) + *rptr++;
834 * Check the sequence number and give up if it differs from
835 * the value we're expecting.
837 if (seq != db->seqno) {
839 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
840 db->unit, seq, db->seqno - 1);
846 * Allocate one mbuf to start with.
848 MGETHDR(dmp, MB_DONTWAIT, MT_DATA);
854 MCLGET(dmp, MB_DONTWAIT);
855 dmp->m_data += db->hdrlen;
856 wptr = mtod(dmp, u_char *);
857 space = M_TRAILINGSPACE(dmp) - PPP_HDRLEN + 1;
860 * Fill in the ppp header, but not the last byte of the protocol
861 * (that comes from the decompressed data).
866 wptr += PPP_HDRLEN - 1;
874 if (!cmp) /* quit at end of message */
876 rptr = mtod(cmp, u_char *);
879 continue; /* handle 0-length buffers */
883 * Accumulate bytes until we have a complete code.
884 * Then get the next code, relying on the 32-bit,
885 * unsigned accm to mask the result.
888 accm |= *rptr++ << bitno;
890 if (tgtbitno < bitno)
892 incode = accm >> tgtbitno;
896 if (incode == CLEAR) {
898 * The dictionary must only be cleared at
899 * the end of a packet. But there could be an
900 * empty mbuf at the end.
902 if (len > 0 || cmp->m_next != NULL) {
903 while ((cmp = cmp->m_next) != NULL)
908 printf("bsd_decomp%d: bad CLEAR\n", db->unit);
909 return DECOMP_FATALERROR; /* probably a bug */
917 if (incode > max_ent + 2 || incode > db->maxmaxcode
918 || (incode > max_ent && oldcode == CLEAR)) {
921 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
922 db->unit, incode, oldcode);
923 printf("max_ent=0x%x explen=%d seqno=%d\n",
924 max_ent, explen, db->seqno);
926 return DECOMP_FATALERROR; /* probably a bug */
929 /* Special case for KwKwK string. */
930 if (incode > max_ent) {
938 codelen = db->lens[finchar];
939 explen += codelen + extra;
940 if (explen > db->mru + 1) {
943 printf("bsd_decomp%d: ran out of mru\n", db->unit);
945 while ((cmp = cmp->m_next) != NULL)
947 printf(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
948 len, finchar, codelen, explen);
951 return DECOMP_FATALERROR;
955 * For simplicity, the decoded characters go in a single mbuf,
956 * so we allocate a single extra cluster mbuf if necessary.
958 if ((space -= codelen + extra) < 0) {
959 dmp->m_len = wptr - mtod(dmp, u_char *);
960 MGET(m, MB_DONTWAIT, MT_DATA);
968 MCLGET(m, MB_DONTWAIT);
969 space = M_TRAILINGSPACE(m) - (codelen + extra);
971 /* now that's what I call *compression*. */
976 wptr = mtod(dmp, u_char *);
980 * Decode this code and install it in the decompressed buffer.
982 p = (wptr += codelen);
983 while (finchar > LAST) {
984 dictp = &db->dict[db->dict[finchar].cptr];
986 if (--codelen <= 0 || dictp->codem1 != finchar-1)
989 *--p = dictp->f.hs.suffix;
990 finchar = dictp->f.hs.prefix;
996 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
997 db->unit, codelen, incode, max_ent);
1000 if (extra) /* the KwKwK case again */
1004 * If not first code in a packet, and
1005 * if not out of code space, then allocate a new code.
1007 * Keep the hash table correct so it can be used
1008 * with uncompressed packets.
1010 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1011 struct bsd_dict *dictp2;
1013 u_int32_t hval, disp;
1015 fcode = BSD_KEY(oldcode,finchar);
1016 hval = BSD_HASH(oldcode,finchar,db->hshift);
1017 dictp = &db->dict[hval];
1019 /* look for a free hash table entry */
1020 if (dictp->codem1 < max_ent) {
1021 disp = (hval == 0) ? 1 : hval;
1024 if (hval >= db->hsize)
1026 dictp = &db->dict[hval];
1027 } while (dictp->codem1 < max_ent);
1031 * Invalidate previous hash table entry
1032 * assigned this code, and then take it over
1034 dictp2 = &db->dict[max_ent+1];
1035 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1036 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1038 dictp2->cptr = hval;
1039 dictp->codem1 = max_ent;
1040 dictp->f.fcode = fcode;
1042 db->max_ent = ++max_ent;
1043 db->lens[max_ent] = db->lens[oldcode]+1;
1045 /* Expand code size if needed. */
1046 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1047 db->n_bits = ++n_bits;
1048 tgtbitno = 32-n_bits;
1053 dmp->m_len = wptr - mtod(dmp, u_char *);
1056 * Keep the checkpoint right so that incompressible packets
1057 * clear the dictionary at the right times.
1059 db->bytes_out += ilen;
1060 db->in_count += explen;
1061 if (bsd_check(db) && db->debug) {
1062 printf("bsd_decomp%d: peer should have cleared dictionary\n",
1067 db->comp_bytes += ilen + BSD_OVHD;
1069 db->uncomp_bytes += explen;
1077 printf("bsd_decomp%d: fell off end of chain ", db->unit);
1078 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1079 incode, finchar, db->dict[finchar].cptr, max_ent);
1080 } else if (dictp->codem1 != finchar-1) {
1081 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1082 db->unit, incode, finchar);
1083 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
1084 db->dict[finchar].cptr, dictp->codem1);
1087 return DECOMP_FATALERROR;
1090 #endif /* DO_BSD_COMPRESS */