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.8 2004/12/21 02:54:14 hsu 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 static int /* 1=output CLEAR */
222 bsd_check(struct bsd_db *db)
226 if (db->in_count >= db->checkpoint) {
227 /* age the ratio by limiting the size of the counts */
228 if (db->in_count >= RATIO_MAX
229 || db->bytes_out >= RATIO_MAX) {
230 db->in_count -= db->in_count/4;
231 db->bytes_out -= db->bytes_out/4;
234 db->checkpoint = db->in_count + CHECK_GAP;
236 if (db->max_ent >= db->maxmaxcode) {
237 /* Reset the dictionary only if the ratio is worse,
238 * or if it looks as if it has been poisoned
239 * by incompressible data.
241 * This does not overflow, because
242 * db->in_count <= RATIO_MAX.
244 new_ratio = db->in_count << RATIO_SCALE_LOG;
245 if (db->bytes_out != 0)
246 new_ratio /= db->bytes_out;
248 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
252 db->ratio = new_ratio;
262 bsd_comp_stats(void *state, struct compstat *stats)
264 struct bsd_db *db = (struct bsd_db *) state;
267 stats->unc_bytes = db->uncomp_bytes;
268 stats->unc_packets = db->uncomp_count;
269 stats->comp_bytes = db->comp_bytes;
270 stats->comp_packets = db->comp_count;
271 stats->inc_bytes = db->incomp_bytes;
272 stats->inc_packets = db->incomp_count;
273 stats->ratio = db->in_count;
275 if (stats->ratio <= 0x7fffff)
284 * Reset state, as on a CCP ResetReq.
287 bsd_reset(void *state)
289 struct bsd_db *db = (struct bsd_db *) state;
297 * Allocate space for a (de) compressor.
300 bsd_alloc(u_char *options, int opt_len, int decomp)
303 u_int newlen, hsize, hshift, maxmaxcode;
306 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
307 || options[1] != CILEN_BSD_COMPRESS
308 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
310 bits = BSD_NBITS(options[2]);
312 case 9: /* needs 82152 for both directions */
313 case 10: /* needs 84144 */
314 case 11: /* needs 88240 */
315 case 12: /* needs 96432 */
319 case 13: /* needs 176784 */
323 case 14: /* needs 353744 */
327 case 15: /* needs 691440 */
331 case 16: /* needs 1366160--far too much, */
332 /* hsize = 69001; */ /* and 69001 is too big for cptr */
333 /* hshift = 8; */ /* in struct bsd_db */
339 maxmaxcode = MAXCODE(bits);
340 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
341 MALLOC(db, struct bsd_db *, newlen, M_DEVBUF, M_WAITOK);
342 bzero(db, sizeof(*db) - sizeof(db->dict));
347 MALLOC(db->lens, u_int16_t *, (maxmaxcode+1) * sizeof(db->lens[0]),
354 db->maxmaxcode = maxmaxcode;
361 bsd_free(void *state)
363 struct bsd_db *db = (struct bsd_db *) state;
366 free(db->lens, M_DEVBUF);
371 bsd_comp_alloc(u_char *options, int opt_len)
373 return bsd_alloc(options, opt_len, 0);
377 bsd_decomp_alloc(u_char *options, int opt_len)
379 return bsd_alloc(options, opt_len, 1);
383 * Initialize the database.
386 bsd_init(struct bsd_db *db, u_char *options, int opt_len, int unit,
387 int hdrlen, int mru, int debug, int decomp)
391 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
392 || options[1] != CILEN_BSD_COMPRESS
393 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
394 || BSD_NBITS(options[2]) != db->maxbits
395 || (decomp && db->lens == NULL))
405 db->dict[--i].codem1 = BADCODEM1;
406 db->dict[i].cptr = 0;
423 bsd_comp_init(void *state, u_char *options, int opt_len, int unit,
424 int hdrlen, int debug)
426 return bsd_init((struct bsd_db *) state, options, opt_len,
427 unit, hdrlen, 0, debug, 0);
431 bsd_decomp_init(void *state, u_char *options, int opt_len,
432 int unit, int hdrlen, int mru, int debug)
434 return bsd_init((struct bsd_db *) state, options, opt_len,
435 unit, hdrlen, mru, debug, 1);
441 * One change from the BSD compress command is that when the
442 * code size expands, we do not output a bunch of padding.
443 * **mret - return compressed mbuf chain here
445 * slen - uncompressed length
446 * maxolen - max compressed length
449 bsd_compress(void *state, struct mbuf **mret, struct mbuf *mp,
450 int slen, int maxolen)
452 struct bsd_db *db = (struct bsd_db *) state;
453 int hshift = db->hshift;
454 u_int max_ent = db->max_ent;
455 u_int n_bits = db->n_bits;
457 u_int32_t accm = 0, fcode;
458 struct bsd_dict *dictp;
460 int hval, disp, ent, ilen;
466 #define PUTBYTE(v) { \
470 if (wptr >= cp_end) { \
471 m->m_len = wptr - mtod(m, u_char *); \
472 MGET(m->m_next, MB_DONTWAIT, MT_DATA); \
476 if (maxolen - olen > MLEN) \
477 MCLGET(m, MB_DONTWAIT); \
478 wptr = mtod(m, u_char *); \
479 cp_end = wptr + M_TRAILINGSPACE(m); \
486 #define OUTPUT(ent) { \
488 accm |= ((ent) << bitno); \
490 PUTBYTE(accm >> 24); \
493 } while (bitno <= 24); \
497 * If the protocol is not in the range we're interested in,
498 * just return without compressing the packet. If it is,
499 * the protocol becomes the first byte to compress.
501 rptr = mtod(mp, u_char *);
502 ent = PPP_PROTOCOL(rptr);
503 if (ent < 0x21 || ent > 0xf9) {
508 /* Don't generate compressed packets which are larger than
509 the uncompressed packet. */
513 /* Allocate one mbuf to start with. */
514 MGET(m, MB_DONTWAIT, MT_DATA);
518 if (maxolen + db->hdrlen > MLEN)
519 MCLGET(m, MB_DONTWAIT);
520 m->m_data += db->hdrlen;
521 wptr = mtod(m, u_char *);
522 cp_end = wptr + M_TRAILINGSPACE(m);
524 wptr = cp_end = NULL;
527 * Copy the PPP header over, changing the protocol,
528 * and install the 2-byte packet sequence number.
531 *wptr++ = PPP_ADDRESS(rptr); /* assumes the ppp header is */
532 *wptr++ = PPP_CONTROL(rptr); /* all in one mbuf */
533 *wptr++ = 0; /* change the protocol */
535 *wptr++ = db->seqno >> 8;
542 slen = mp->m_len - PPP_HDRLEN;
549 rptr = mtod(mp, u_char *);
552 continue; /* handle 0-length buffers */
558 fcode = BSD_KEY(ent, c);
559 hval = BSD_HASH(ent, c, hshift);
560 dictp = &db->dict[hval];
562 /* Validate and then check the entry. */
563 if (dictp->codem1 >= max_ent)
565 if (dictp->f.fcode == fcode) {
566 ent = dictp->codem1+1;
567 continue; /* found (prefix,suffix) */
570 /* continue probing until a match or invalid entry */
571 disp = (hval == 0) ? 1 : hval;
574 if (hval >= db->hsize)
576 dictp = &db->dict[hval];
577 if (dictp->codem1 >= max_ent)
579 } while (dictp->f.fcode != fcode);
580 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
584 OUTPUT(ent); /* output the prefix */
586 /* code -> hashtable */
587 if (max_ent < db->maxmaxcode) {
588 struct bsd_dict *dictp2;
589 /* expand code size if needed */
590 if (max_ent >= MAXCODE(n_bits))
591 db->n_bits = ++n_bits;
593 /* Invalidate old hash table entry using
594 * this code, and then take it over.
596 dictp2 = &db->dict[max_ent+1];
597 if (db->dict[dictp2->cptr].codem1 == max_ent)
598 db->dict[dictp2->cptr].codem1 = BADCODEM1;
600 dictp->codem1 = max_ent;
601 dictp->f.fcode = fcode;
603 db->max_ent = ++max_ent;
608 OUTPUT(ent); /* output the last code */
609 db->bytes_out += olen;
610 db->in_count += ilen;
612 ++db->bytes_out; /* count complete bytes */
615 OUTPUT(CLEAR); /* do not count the CLEAR */
618 * Pad dribble bits of last code with ones.
619 * Do not emit a completely useless byte of ones.
622 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
625 m->m_len = wptr - mtod(m, u_char *);
630 * Increase code size if we would have without the packet
631 * boundary and as the decompressor will.
633 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
636 db->uncomp_bytes += ilen;
638 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen) {
639 /* throw away the compressed stuff if it is longer than uncompressed */
645 db->incomp_bytes += ilen;
648 db->comp_bytes += olen + BSD_OVHD;
651 return olen + PPP_HDRLEN + BSD_OVHD;
658 * Update the "BSD Compress" dictionary on the receiver for
659 * incompressible data by pretending to compress the incoming data.
662 bsd_incomp(void *state, struct mbuf *dmsg)
664 struct bsd_db *db = (struct bsd_db *) state;
665 u_int hshift = db->hshift;
666 u_int max_ent = db->max_ent;
667 u_int n_bits = db->n_bits;
668 struct bsd_dict *dictp;
671 u_int32_t hval, disp;
678 * If the protocol is not in the range we're interested in,
679 * just return without looking at the packet. If it is,
680 * the protocol becomes the first byte to "compress".
682 rptr = mtod(dmsg, u_char *);
683 ent = PPP_PROTOCOL(rptr);
684 if (ent < 0x21 || ent > 0xf9)
688 ilen = 1; /* count the protocol as 1 byte */
690 slen = dmsg->m_len - PPP_HDRLEN;
696 rptr = mtod(dmsg, u_char *);
704 fcode = BSD_KEY(ent, c);
705 hval = BSD_HASH(ent, c, hshift);
706 dictp = &db->dict[hval];
708 /* validate and then check the entry */
709 if (dictp->codem1 >= max_ent)
711 if (dictp->f.fcode == fcode) {
712 ent = dictp->codem1+1;
713 continue; /* found (prefix,suffix) */
716 /* continue probing until a match or invalid entry */
717 disp = (hval == 0) ? 1 : hval;
720 if (hval >= db->hsize)
722 dictp = &db->dict[hval];
723 if (dictp->codem1 >= max_ent)
725 } while (dictp->f.fcode != fcode);
726 ent = dictp->codem1+1;
727 continue; /* finally found (prefix,suffix) */
729 nomatch: /* output (count) the prefix */
732 /* code -> hashtable */
733 if (max_ent < db->maxmaxcode) {
734 struct bsd_dict *dictp2;
735 /* expand code size if needed */
736 if (max_ent >= MAXCODE(n_bits))
737 db->n_bits = ++n_bits;
739 /* Invalidate previous hash table entry
740 * assigned this code, and then take it over.
742 dictp2 = &db->dict[max_ent+1];
743 if (db->dict[dictp2->cptr].codem1 == max_ent)
744 db->dict[dictp2->cptr].codem1 = BADCODEM1;
746 dictp->codem1 = max_ent;
747 dictp->f.fcode = fcode;
749 db->max_ent = ++max_ent;
750 db->lens[max_ent] = db->lens[ent]+1;
753 } while (--slen != 0);
755 bitno += n_bits; /* output (count) the last code */
756 db->bytes_out += bitno/8;
757 db->in_count += ilen;
761 db->incomp_bytes += ilen;
763 db->uncomp_bytes += ilen;
765 /* Increase code size if we would have without the packet
766 * boundary and as the decompressor will.
768 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
774 * Decompress "BSD Compress".
776 * Because of patent problems, we return DECOMP_ERROR for errors
777 * found by inspecting the input data and for system problems, but
778 * DECOMP_FATALERROR for any errors which could possibly be said to
779 * be being detected "after" decompression. For DECOMP_ERROR,
780 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
781 * infringing a patent of Motorola's if we do, so we take CCP down
784 * Given that the frame has the correct sequence number and a good FCS,
785 * errors such as invalid codes in the input most likely indicate a
786 * bug, so we return DECOMP_FATALERROR for them in order to turn off
787 * compression, even though they are detected by inspecting the input.
790 bsd_decompress(void *state, struct mbuf *cmp, struct mbuf **dmpp)
792 struct bsd_db *db = (struct bsd_db *) state;
793 u_int max_ent = db->max_ent;
795 u_int bitno = 32; /* 1st valid bit in accm */
796 u_int n_bits = db->n_bits;
797 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
798 struct bsd_dict *dictp;
799 int explen, i, seq, len;
800 u_int incode, oldcode, finchar;
801 u_char *p, *rptr, *wptr;
802 struct mbuf *m, *dmp, *mret;
803 int adrs, ctrl, ilen;
804 int space, codelen, extra;
807 * Save the address/control from the PPP header
808 * and then get the sequence number.
811 rptr = mtod(cmp, u_char *);
812 adrs = PPP_ADDRESS(rptr);
813 ctrl = PPP_CONTROL(rptr);
815 len = cmp->m_len - PPP_HDRLEN;
817 for (i = 0; i < 2; ++i) {
822 rptr = mtod(cmp, u_char *);
825 seq = (seq << 8) + *rptr++;
830 * Check the sequence number and give up if it differs from
831 * the value we're expecting.
833 if (seq != db->seqno) {
835 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
836 db->unit, seq, db->seqno - 1);
842 * Allocate one mbuf to start with.
844 MGETHDR(dmp, MB_DONTWAIT, MT_DATA);
850 MCLGET(dmp, MB_DONTWAIT);
851 dmp->m_data += db->hdrlen;
852 wptr = mtod(dmp, u_char *);
853 space = M_TRAILINGSPACE(dmp) - PPP_HDRLEN + 1;
856 * Fill in the ppp header, but not the last byte of the protocol
857 * (that comes from the decompressed data).
862 wptr += PPP_HDRLEN - 1;
870 if (!cmp) /* quit at end of message */
872 rptr = mtod(cmp, u_char *);
875 continue; /* handle 0-length buffers */
879 * Accumulate bytes until we have a complete code.
880 * Then get the next code, relying on the 32-bit,
881 * unsigned accm to mask the result.
884 accm |= *rptr++ << bitno;
886 if (tgtbitno < bitno)
888 incode = accm >> tgtbitno;
892 if (incode == CLEAR) {
894 * The dictionary must only be cleared at
895 * the end of a packet. But there could be an
896 * empty mbuf at the end.
898 if (len > 0 || cmp->m_next != NULL) {
899 while ((cmp = cmp->m_next) != NULL)
904 printf("bsd_decomp%d: bad CLEAR\n", db->unit);
905 return DECOMP_FATALERROR; /* probably a bug */
913 if (incode > max_ent + 2 || incode > db->maxmaxcode
914 || (incode > max_ent && oldcode == CLEAR)) {
917 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
918 db->unit, incode, oldcode);
919 printf("max_ent=0x%x explen=%d seqno=%d\n",
920 max_ent, explen, db->seqno);
922 return DECOMP_FATALERROR; /* probably a bug */
925 /* Special case for KwKwK string. */
926 if (incode > max_ent) {
934 codelen = db->lens[finchar];
935 explen += codelen + extra;
936 if (explen > db->mru + 1) {
939 printf("bsd_decomp%d: ran out of mru\n", db->unit);
941 while ((cmp = cmp->m_next) != NULL)
943 printf(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
944 len, finchar, codelen, explen);
947 return DECOMP_FATALERROR;
951 * For simplicity, the decoded characters go in a single mbuf,
952 * so we allocate a single extra cluster mbuf if necessary.
954 if ((space -= codelen + extra) < 0) {
955 dmp->m_len = wptr - mtod(dmp, u_char *);
956 MGET(m, MB_DONTWAIT, MT_DATA);
964 MCLGET(m, MB_DONTWAIT);
965 space = M_TRAILINGSPACE(m) - (codelen + extra);
967 /* now that's what I call *compression*. */
972 wptr = mtod(dmp, u_char *);
976 * Decode this code and install it in the decompressed buffer.
978 p = (wptr += codelen);
979 while (finchar > LAST) {
980 dictp = &db->dict[db->dict[finchar].cptr];
982 if (--codelen <= 0 || dictp->codem1 != finchar-1)
985 *--p = dictp->f.hs.suffix;
986 finchar = dictp->f.hs.prefix;
992 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
993 db->unit, codelen, incode, max_ent);
996 if (extra) /* the KwKwK case again */
1000 * If not first code in a packet, and
1001 * if not out of code space, then allocate a new code.
1003 * Keep the hash table correct so it can be used
1004 * with uncompressed packets.
1006 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1007 struct bsd_dict *dictp2;
1009 u_int32_t hval, disp;
1011 fcode = BSD_KEY(oldcode,finchar);
1012 hval = BSD_HASH(oldcode,finchar,db->hshift);
1013 dictp = &db->dict[hval];
1015 /* look for a free hash table entry */
1016 if (dictp->codem1 < max_ent) {
1017 disp = (hval == 0) ? 1 : hval;
1020 if (hval >= db->hsize)
1022 dictp = &db->dict[hval];
1023 } while (dictp->codem1 < max_ent);
1027 * Invalidate previous hash table entry
1028 * assigned this code, and then take it over
1030 dictp2 = &db->dict[max_ent+1];
1031 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1032 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1034 dictp2->cptr = hval;
1035 dictp->codem1 = max_ent;
1036 dictp->f.fcode = fcode;
1038 db->max_ent = ++max_ent;
1039 db->lens[max_ent] = db->lens[oldcode]+1;
1041 /* Expand code size if needed. */
1042 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1043 db->n_bits = ++n_bits;
1044 tgtbitno = 32-n_bits;
1049 dmp->m_len = wptr - mtod(dmp, u_char *);
1052 * Keep the checkpoint right so that incompressible packets
1053 * clear the dictionary at the right times.
1055 db->bytes_out += ilen;
1056 db->in_count += explen;
1057 if (bsd_check(db) && db->debug) {
1058 printf("bsd_decomp%d: peer should have cleared dictionary\n",
1063 db->comp_bytes += ilen + BSD_OVHD;
1065 db->uncomp_bytes += explen;
1073 printf("bsd_decomp%d: fell off end of chain ", db->unit);
1074 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1075 incode, finchar, db->dict[finchar].cptr, max_ent);
1076 } else if (dictp->codem1 != finchar-1) {
1077 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1078 db->unit, incode, finchar);
1079 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
1080 db->dict[finchar].cptr, dictp->codem1);
1083 return DECOMP_FATALERROR;
1086 #endif /* DO_BSD_COMPRESS */