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 STREAMS under SunOS 4.x,
42 * DEC Alpha OSF/1, and AIX 4.x.
44 * $Id: bsd-comp.c,v 1.11 1995/05/02 02:49:09 paulus Exp $
48 #include <net/net_globals.h>
50 #include <sys/param.h>
51 #include <sys/types.h>
52 #include <sys/stream.h>
53 #include <sys/socket.h>
55 #include <net/ppp_defs.h>
56 #include <net/ppp_str.h>
59 #include <sys/kmem_alloc.h>
60 #define ALLOCATE(n) kmem_alloc((n), KMEM_NOSLEEP)
61 #define FREE(p, n) kmem_free((p), (n))
65 #include <kern/kalloc.h>
66 #define ALLOCATE(n) kalloc((n))
67 #define FREE(p, n) kfree((p), (n))
71 #define ALLOCATE(n) xmalloc((n), 0, pinned_heap)
72 #define FREE(p, n) xmfree((p), pinned_heap)
75 #define PACKETPTR mblk_t *
76 #include <net/ppp-comp.h>
81 * PPP "BSD compress" compression
82 * The differences between this compression and the classic BSD LZW
83 * source are obvious from the requirement that the classic code worked
84 * with files while this handles arbitrarily long streams that
85 * are broken into packets. They are:
87 * When the code size expands, a block of junk is not emitted by
88 * the compressor and not expected by the decompressor.
90 * New codes are not necessarily assigned every time an old
91 * code is output by the compressor. This is because a packet
92 * end forces a code to be emitted, but does not imply that a
93 * new sequence has been seen.
95 * The compression ratio is checked at the first end of a packet
96 * after the appropriate gap. Besides simplifying and speeding
97 * things up, this makes it more likely that the transmitter
98 * and receiver will agree when the dictionary is cleared when
99 * compression is not going well.
103 * A dictionary for doing BSD compress.
106 int totlen; /* length of this structure */
107 u_int hsize; /* size of the hash table */
108 u_char hshift; /* used in hash function */
109 u_char n_bits; /* current bits/code */
113 u_short seqno; /* sequence number of next packet */
114 u_int hdrlen; /* header length to preallocate */
116 u_int maxmaxcode; /* largest valid code */
117 u_int max_ent; /* largest code in use */
118 u_int in_count; /* uncompressed bytes, aged */
119 u_int bytes_out; /* compressed bytes, aged */
120 u_int ratio; /* recent compression ratio */
121 u_int checkpoint; /* when to next check the ratio */
122 u_int clear_count; /* times dictionary cleared */
123 u_int incomp_count; /* incompressible packets */
124 u_int incomp_bytes; /* incompressible bytes */
125 u_int uncomp_count; /* uncompressed packets */
126 u_int uncomp_bytes; /* uncompressed bytes */
127 u_int comp_count; /* compressed packets */
128 u_int comp_bytes; /* compressed bytes */
129 u_short *lens; /* array of lengths of codes */
131 union { /* hash value */
134 #ifdef BSD_LITTLE_ENDIAN
135 u_short prefix; /* preceding code */
136 u_char suffix; /* last character of new code */
140 u_char suffix; /* last character of new code */
141 u_short prefix; /* preceding code */
145 u_short codem1; /* output of hash table -1 */
146 u_short cptr; /* map code to hash table entry */
150 #define BSD_OVHD 2 /* BSD compress overhead/packet */
151 #define BSD_INIT_BITS BSD_MIN_BITS
153 static void *bsd_comp_alloc __P((u_char *options, int opt_len));
154 static void *bsd_decomp_alloc __P((u_char *options, int opt_len));
155 static void bsd_free __P((void *state));
156 static int bsd_comp_init __P((void *state, u_char *options, int opt_len,
157 int unit, int hdrlen, int debug));
158 static int bsd_decomp_init __P((void *state, u_char *options, int opt_len,
159 int unit, int hdrlen, int mru, int debug));
160 static int bsd_compress __P((void *state, mblk_t **mret,
161 mblk_t *mp, int slen, int maxolen));
162 static void bsd_incomp __P((void *state, mblk_t *dmsg));
163 static int bsd_decompress __P((void *state, mblk_t *cmp, mblk_t **dmpp));
164 static void bsd_reset __P((void *state));
165 static void bsd_comp_stats __P((void *state, struct compstat *stats));
168 * Procedures exported to ppp_comp.c.
170 struct compressor ppp_bsd_compress = {
171 CI_BSD_COMPRESS, /* compress_proto */
172 bsd_comp_alloc, /* comp_alloc */
173 bsd_free, /* comp_free */
174 bsd_comp_init, /* comp_init */
175 bsd_reset, /* comp_reset */
176 bsd_compress, /* compress */
177 bsd_comp_stats, /* comp_stat */
178 bsd_decomp_alloc, /* decomp_alloc */
179 bsd_free, /* decomp_free */
180 bsd_decomp_init, /* decomp_init */
181 bsd_reset, /* decomp_reset */
182 bsd_decompress, /* decompress */
183 bsd_incomp, /* incomp */
184 bsd_comp_stats, /* decomp_stat */
188 * the next two codes should not be changed lightly, as they must not
189 * lie within the contiguous general code space.
191 #define CLEAR 256 /* table clear output code */
192 #define FIRST 257 /* first free entry */
195 #define MAXCODE(b) ((1 << (b)) - 1)
196 #define BADCODEM1 MAXCODE(BSD_MAX_BITS)
198 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
199 ^ (u_int32_t)(prefix))
200 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
201 + (u_int32_t)(prefix))
203 #define CHECK_GAP 10000 /* Ratio check interval */
205 #define RATIO_SCALE_LOG 8
206 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
207 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
209 #define DECOMP_CHUNK 256
212 * clear the dictionary
219 db->max_ent = FIRST-1;
220 db->n_bits = BSD_INIT_BITS;
224 db->incomp_count = 0;
225 db->checkpoint = CHECK_GAP;
229 * If the dictionary is full, then see if it is time to reset it.
231 * Compute the compression ratio using fixed-point arithmetic
232 * with 8 fractional bits.
234 * Since we have an infinite stream instead of a single file,
235 * watch only the local compression ratio.
237 * Since both peers must reset the dictionary at the same time even in
238 * the absence of CLEAR codes (while packets are incompressible), they
239 * must compute the same ratio.
241 static int /* 1=output CLEAR */
247 if (db->in_count >= db->checkpoint) {
248 /* age the ratio by limiting the size of the counts */
249 if (db->in_count >= RATIO_MAX
250 || db->bytes_out >= RATIO_MAX) {
251 db->in_count -= db->in_count/4;
252 db->bytes_out -= db->bytes_out/4;
255 db->checkpoint = db->in_count + CHECK_GAP;
257 if (db->max_ent >= db->maxmaxcode) {
258 /* Reset the dictionary only if the ratio is worse,
259 * or if it looks as if it has been poisoned
260 * by incompressible data.
262 * This does not overflow, because
263 * db->in_count <= RATIO_MAX.
265 new_ratio = db->in_count << RATIO_SCALE_LOG;
266 if (db->bytes_out != 0)
267 new_ratio /= db->bytes_out;
269 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
273 db->ratio = new_ratio;
283 bsd_comp_stats(state, stats)
285 struct compstat *stats;
287 struct bsd_db *db = (struct bsd_db *) state;
290 stats->unc_bytes = db->uncomp_bytes;
291 stats->unc_packets = db->uncomp_count;
292 stats->comp_bytes = db->comp_bytes;
293 stats->comp_packets = db->comp_count;
294 stats->inc_bytes = db->incomp_bytes;
295 stats->inc_packets = db->incomp_count;
296 stats->ratio = db->in_count;
298 if (stats->ratio <= 0x7fffff)
307 * Reset state, as on a CCP ResetReq.
313 struct bsd_db *db = (struct bsd_db *) state;
321 * Allocate space for a (de) compressor.
324 bsd_alloc(options, opt_len, decomp)
329 u_int newlen, hsize, hshift, maxmaxcode;
332 if (opt_len != 3 || options[0] != CI_BSD_COMPRESS || options[1] != 3
333 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
335 bits = BSD_NBITS(options[2]);
337 case 9: /* needs 82152 for both directions */
338 case 10: /* needs 84144 */
339 case 11: /* needs 88240 */
340 case 12: /* needs 96432 */
344 case 13: /* needs 176784 */
348 case 14: /* needs 353744 */
352 case 15: /* needs 691440 */
356 case 16: /* needs 1366160--far too much, */
357 /* hsize = 69001; */ /* and 69001 is too big for cptr */
358 /* hshift = 8; */ /* in struct bsd_db */
364 maxmaxcode = MAXCODE(bits);
365 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
366 db = (struct bsd_db *) ALLOCATE(newlen);
369 bzero(db, sizeof(*db) - sizeof(db->dict));
374 db->lens = (u_short *) ALLOCATE((maxmaxcode+1) * sizeof(db->lens[0]));
384 db->maxmaxcode = maxmaxcode;
394 struct bsd_db *db = (struct bsd_db *) state;
397 FREE(db->lens, (db->maxmaxcode+1) * sizeof(db->lens[0]));
398 FREE(db, db->totlen);
402 bsd_comp_alloc(options, opt_len)
406 return bsd_alloc(options, opt_len, 0);
410 bsd_decomp_alloc(options, opt_len)
414 return bsd_alloc(options, opt_len, 1);
418 * Initialize the database.
421 bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp)
424 int opt_len, unit, hdrlen, mru, debug, decomp;
428 if (opt_len != 3 || options[0] != CI_BSD_COMPRESS || options[1] != 3
429 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
430 || BSD_NBITS(options[2]) != db->maxbits
431 || decomp && db->lens == NULL)
441 db->dict[--i].codem1 = BADCODEM1;
442 db->dict[i].cptr = 0;
457 bsd_comp_init(state, options, opt_len, unit, hdrlen, debug)
460 int opt_len, unit, hdrlen, debug;
462 return bsd_init((struct bsd_db *) state, options, opt_len,
463 unit, hdrlen, 0, debug, 0);
467 bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
470 int opt_len, unit, hdrlen, mru, debug;
472 return bsd_init((struct bsd_db *) state, options, opt_len,
473 unit, hdrlen, mru, debug, 1);
479 * One change from the BSD compress command is that when the
480 * code size expands, we do not output a bunch of padding.
482 * N.B. at present, we ignore the hdrlen specified in the comp_init call.
484 static int /* new slen */
485 bsd_compress(state, mretp, mp, slen, maxolen)
487 mblk_t **mretp; /* return compressed mbuf chain here */
488 mblk_t *mp; /* from here */
489 int slen; /* uncompressed length */
490 int maxolen; /* max compressed length */
492 struct bsd_db *db = (struct bsd_db *) state;
493 int hshift = db->hshift;
494 u_int max_ent = db->max_ent;
495 u_int n_bits = db->n_bits;
497 u_int32_t accm = 0, fcode;
498 struct bsd_dict *dictp;
500 int hval, disp, ent, ilen;
507 #define PUTBYTE(v) { \
510 if (wptr >= cp_end) { \
515 cp_end = m->b_datap->db_lim; \
523 #define OUTPUT(ent) { \
525 accm |= ((ent) << bitno); \
527 PUTBYTE(accm >> 24); \
530 } while (bitno <= 24); \
534 * First get the protocol and check that we're
535 * interested in this packet.
539 if (rptr + PPP_HDRLEN > mp->b_wptr) {
540 if (!pullupmsg(mp, PPP_HDRLEN))
544 ent = PPP_PROTOCOL(rptr); /* get the protocol */
545 if (ent < 0x21 || ent > 0xf9)
548 /* Don't generate compressed packets which are larger than
549 the uncompressed packet. */
553 /* Allocate enough message blocks to give maxolen total space. */
555 for (olen = maxolen; olen > 0; ) {
556 m = allocb((olen < 4096? olen: 4096), BPRI_MED);
566 olen -= m->b_datap->db_lim - m->b_wptr;
570 if ((m = mret) != NULL) {
572 cp_end = m->b_datap->db_lim;
574 wptr = cp_end = NULL;
578 * Copy the PPP header over, changing the protocol,
579 * and install the 2-byte sequence number.
582 wptr[0] = PPP_ADDRESS(rptr);
583 wptr[1] = PPP_CONTROL(rptr);
584 wptr[2] = 0; /* change the protocol */
586 wptr[4] = db->seqno >> 8;
588 wptr += PPP_HDRLEN + BSD_OVHD;
593 slen = mp->b_wptr - rptr;
601 slen = np->b_wptr - rptr;
604 continue; /* handle 0-length buffers */
610 fcode = BSD_KEY(ent, c);
611 hval = BSD_HASH(ent, c, hshift);
612 dictp = &db->dict[hval];
614 /* Validate and then check the entry. */
615 if (dictp->codem1 >= max_ent)
617 if (dictp->f.fcode == fcode) {
618 ent = dictp->codem1+1;
619 continue; /* found (prefix,suffix) */
622 /* continue probing until a match or invalid entry */
623 disp = (hval == 0) ? 1 : hval;
626 if (hval >= db->hsize)
628 dictp = &db->dict[hval];
629 if (dictp->codem1 >= max_ent)
631 } while (dictp->f.fcode != fcode);
632 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
636 OUTPUT(ent); /* output the prefix */
638 /* code -> hashtable */
639 if (max_ent < db->maxmaxcode) {
640 struct bsd_dict *dictp2;
641 /* expand code size if needed */
642 if (max_ent >= MAXCODE(n_bits))
643 db->n_bits = ++n_bits;
645 /* Invalidate old hash table entry using
646 * this code, and then take it over.
648 dictp2 = &db->dict[max_ent+1];
649 if (db->dict[dictp2->cptr].codem1 == max_ent)
650 db->dict[dictp2->cptr].codem1 = BADCODEM1;
652 dictp->codem1 = max_ent;
653 dictp->f.fcode = fcode;
655 db->max_ent = ++max_ent;
660 OUTPUT(ent); /* output the last code */
661 db->bytes_out += olen;
662 db->in_count += ilen;
664 ++db->bytes_out; /* count complete bytes */
667 OUTPUT(CLEAR); /* do not count the CLEAR */
670 * Pad dribble bits of last code with ones.
671 * Do not emit a completely useless byte of ones.
674 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
677 * Increase code size if we would have without the packet
678 * boundary and as the decompressor will.
680 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
683 db->uncomp_bytes += ilen;
685 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen && mret != NULL) {
686 /* throw away the compressed stuff if it is longer than uncompressed */
690 db->incomp_bytes += ilen;
691 } else if (wptr != NULL) {
698 db->comp_bytes += olen + BSD_OVHD;
702 return olen + PPP_HDRLEN + BSD_OVHD;
709 * Update the "BSD Compress" dictionary on the receiver for
710 * incompressible data by pretending to compress the incoming data.
713 bsd_incomp(state, dmsg)
717 struct bsd_db *db = (struct bsd_db *) state;
718 u_int hshift = db->hshift;
719 u_int max_ent = db->max_ent;
720 u_int n_bits = db->n_bits;
721 struct bsd_dict *dictp;
731 if (rptr + PPP_HDRLEN > dmsg->b_wptr) {
732 if (!pullupmsg(dmsg, PPP_HDRLEN))
736 ent = PPP_PROTOCOL(rptr); /* get the protocol */
737 if (ent < 0x21 || ent > 0xf9)
742 ilen = 1; /* count the protocol as 1 byte */
745 slen = dmsg->b_wptr - rptr;
751 continue; /* skip zero-length buffers */
757 fcode = BSD_KEY(ent, c);
758 hval = BSD_HASH(ent, c, hshift);
759 dictp = &db->dict[hval];
761 /* validate and then check the entry */
762 if (dictp->codem1 >= max_ent)
764 if (dictp->f.fcode == fcode) {
765 ent = dictp->codem1+1;
766 continue; /* found (prefix,suffix) */
769 /* continue probing until a match or invalid entry */
770 disp = (hval == 0) ? 1 : hval;
773 if (hval >= db->hsize)
775 dictp = &db->dict[hval];
776 if (dictp->codem1 >= max_ent)
778 } while (dictp->f.fcode != fcode);
779 ent = dictp->codem1+1;
780 continue; /* finally found (prefix,suffix) */
782 nomatch: /* output (count) the prefix */
785 /* code -> hashtable */
786 if (max_ent < db->maxmaxcode) {
787 struct bsd_dict *dictp2;
788 /* expand code size if needed */
789 if (max_ent >= MAXCODE(n_bits))
790 db->n_bits = ++n_bits;
792 /* Invalidate previous hash table entry
793 * assigned this code, and then take it over.
795 dictp2 = &db->dict[max_ent+1];
796 if (db->dict[dictp2->cptr].codem1 == max_ent)
797 db->dict[dictp2->cptr].codem1 = BADCODEM1;
799 dictp->codem1 = max_ent;
800 dictp->f.fcode = fcode;
802 db->max_ent = ++max_ent;
803 db->lens[max_ent] = db->lens[ent]+1;
806 } while (--slen != 0);
808 bitno += n_bits; /* output (count) the last code */
809 db->bytes_out += bitno/8;
810 db->in_count += ilen;
814 db->incomp_bytes += ilen;
816 db->uncomp_bytes += ilen;
818 /* Increase code size if we would have without the packet
819 * boundary and as the decompressor will.
821 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
827 * Decompress "BSD Compress"
829 * Because of patent problems, we return DECOMP_ERROR for errors
830 * found by inspecting the input data and for system problems, but
831 * DECOMP_FATALERROR for any errors which could possibly be said to
832 * be being detected "after" decompression. For DECOMP_ERROR,
833 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
834 * infringing a patent of Motorola's if we do, so we take CCP down
837 * Given that the frame has the correct sequence number and a good FCS,
838 * errors such as invalid codes in the input most likely indicate a
839 * bug, so we return DECOMP_FATALERROR for them in order to turn off
840 * compression, even though they are detected by inspecting the input.
843 bsd_decompress(state, cmsg, dmpp)
845 mblk_t *cmsg, **dmpp;
847 struct bsd_db *db = (struct bsd_db *) state;
848 u_int max_ent = db->max_ent;
850 u_int bitno = 32; /* 1st valid bit in accm */
851 u_int n_bits = db->n_bits;
852 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
853 struct bsd_dict *dictp;
854 int explen, i, seq, len;
855 u_int incode, oldcode, finchar;
856 u_char *p, *rptr, *wptr;
858 int adrs, ctrl, ilen;
859 int dlen, space, codelen, extra;
862 * Get at least the BSD Compress header in the first buffer
865 if (rptr + PPP_HDRLEN + BSD_OVHD >= cmsg->b_wptr) {
866 if (!pullupmsg(cmsg, PPP_HDRLEN + BSD_OVHD + 1)) {
868 printf("bsd_decomp%d: failed to pullup\n", db->unit);
875 * Save the address/control from the PPP header
876 * and then get the sequence number.
878 adrs = PPP_ADDRESS(rptr);
879 ctrl = PPP_CONTROL(rptr);
881 seq = (rptr[0] << 8) + rptr[1];
883 ilen = len = cmsg->b_wptr - rptr;
886 * Check the sequence number and give up if it is not what we expect.
888 if (seq != db->seqno++) {
890 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
891 db->unit, seq, db->seqno - 1);
896 * Allocate one message block to start with.
898 if ((dmsg = allocb(DECOMP_CHUNK + db->hdrlen, BPRI_MED)) == NULL)
901 dmsg->b_wptr += db->hdrlen;
902 dmsg->b_rptr = wptr = dmsg->b_wptr;
904 /* Fill in the ppp header, but not the last byte of the protocol
905 (that comes from the decompressed data). */
909 wptr += PPP_HDRLEN - 1;
910 space = dmsg->b_datap->db_lim - wptr;
917 if (!cmsg) /* quit at end of message */
920 len = cmsg->b_wptr - rptr;
922 continue; /* handle 0-length buffers */
926 * Accumulate bytes until we have a complete code.
927 * Then get the next code, relying on the 32-bit,
928 * unsigned accm to mask the result.
931 accm |= *rptr++ << bitno;
933 if (tgtbitno < bitno)
935 incode = accm >> tgtbitno;
939 if (incode == CLEAR) {
941 * The dictionary must only be cleared at
942 * the end of a packet. But there could be an
943 * empty message block at the end.
945 if (len > 0 || cmsg->b_cont != 0) {
947 len += msgdsize(cmsg->b_cont);
951 printf("bsd_decomp%d: bad CLEAR\n", db->unit);
952 return DECOMP_FATALERROR;
960 if (incode > max_ent + 2 || incode > db->maxmaxcode
961 || incode > max_ent && oldcode == CLEAR) {
964 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
965 db->unit, incode, oldcode);
966 printf("max_ent=0x%x dlen=%d seqno=%d\n",
967 max_ent, dlen, db->seqno);
969 return DECOMP_FATALERROR; /* probably a bug */
972 /* Special case for KwKwK string. */
973 if (incode > max_ent) {
981 codelen = db->lens[finchar];
982 explen += codelen + extra;
983 if (explen > db->mru + 1) {
986 printf("bsd_decomp%d: ran out of mru\n", db->unit);
987 return DECOMP_FATALERROR;
991 * Decode this code and install it in the decompressed buffer.
993 space -= codelen + extra;
995 /* Allocate another message block. */
997 dlen = codelen + extra;
998 if (dlen < DECOMP_CHUNK)
1000 if ((dmsg->b_cont = allocb(dlen, BPRI_MED)) == NULL) {
1002 return DECOMP_ERROR;
1004 dmsg = dmsg->b_cont;
1005 wptr = dmsg->b_wptr;
1006 space = dmsg->b_datap->db_lim - wptr - codelen - extra;
1008 p = (wptr += codelen);
1009 while (finchar > LAST) {
1010 dictp = &db->dict[db->dict[finchar].cptr];
1015 printf("bsd_decomp%d: fell off end of chain ", db->unit);
1016 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1017 incode, finchar, db->dict[finchar].cptr, max_ent);
1018 return DECOMP_FATALERROR;
1020 if (dictp->codem1 != finchar-1) {
1022 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1023 db->unit, incode, finchar);
1024 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
1025 db->dict[finchar].cptr, dictp->codem1);
1026 return DECOMP_FATALERROR;
1029 *--p = dictp->f.hs.suffix;
1030 finchar = dictp->f.hs.prefix;
1036 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1037 db->unit, codelen, incode, max_ent);
1040 if (extra) /* the KwKwK case again */
1044 * If not first code in a packet, and
1045 * if not out of code space, then allocate a new code.
1047 * Keep the hash table correct so it can be used
1048 * with uncompressed packets.
1050 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1051 struct bsd_dict *dictp2;
1055 fcode = BSD_KEY(oldcode,finchar);
1056 hval = BSD_HASH(oldcode,finchar,db->hshift);
1057 dictp = &db->dict[hval];
1059 /* look for a free hash table entry */
1060 if (dictp->codem1 < max_ent) {
1061 disp = (hval == 0) ? 1 : hval;
1064 if (hval >= db->hsize)
1066 dictp = &db->dict[hval];
1067 } while (dictp->codem1 < max_ent);
1071 * Invalidate previous hash table entry
1072 * assigned this code, and then take it over
1074 dictp2 = &db->dict[max_ent+1];
1075 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1076 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1078 dictp2->cptr = hval;
1079 dictp->codem1 = max_ent;
1080 dictp->f.fcode = fcode;
1082 db->max_ent = ++max_ent;
1083 db->lens[max_ent] = db->lens[oldcode]+1;
1085 /* Expand code size if needed. */
1086 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1087 db->n_bits = ++n_bits;
1088 tgtbitno = 32-n_bits;
1093 dmsg->b_wptr = wptr;
1096 * Keep the checkpoint right so that incompressible packets
1097 * clear the dictionary at the right times.
1099 db->bytes_out += ilen;
1100 db->in_count += explen;
1101 if (bsd_check(db) && db->debug) {
1102 printf("bsd_decomp%d: peer should have cleared dictionary\n",
1107 db->comp_bytes += ilen + BSD_OVHD;
1109 db->uncomp_bytes += explen;
1114 #endif /* DO_BSD_COMPRESS */