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, AIX 4.x, and SVR4 systems including Solaris 2.
44 * $Id: bsd-comp.c,v 1.16 1995/10/27 03:35:49 paulus Exp $
47 #if defined(aix4) || defined(__aix4__)
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>
58 #if defined(svr4) || defined(__svr4__) /* SVR4, including SunOS 5.x */
59 # include <sys/kmem.h>
60 # define ALLOCATE(n) kmem_alloc((n), KM_NOSLEEP)
61 # define FREE(p, n) kmem_free((p), (n))
63 # if defined(sun) || defined(__sun__)
64 # include <sys/kmem_alloc.h>
65 # define ALLOCATE(n) kmem_alloc((n), KMEM_NOSLEEP)
66 # define FREE(p, n) kmem_free((p), (n))
70 #if defined(osf) || defined(__osf__)
71 #include <kern/kalloc.h>
79 #define ALLOCATE(n) kalloc((n))
80 #define FREE(p, n) kfree((p), (n))
81 #define BSD_LITTLE_ENDIAN
84 #if defined(aix4) || defined(__aix4__)
85 #define ALLOCATE(n) xmalloc((n), 0, pinned_heap)
86 #define FREE(p, n) xmfree((p), pinned_heap)
89 #define PACKETPTR mblk_t *
90 #include <net/ppp-comp.h>
95 * PPP "BSD compress" compression
96 * The differences between this compression and the classic BSD LZW
97 * source are obvious from the requirement that the classic code worked
98 * with files while this handles arbitrarily long streams that
99 * are broken into packets. They are:
101 * When the code size expands, a block of junk is not emitted by
102 * the compressor and not expected by the decompressor.
104 * New codes are not necessarily assigned every time an old
105 * code is output by the compressor. This is because a packet
106 * end forces a code to be emitted, but does not imply that a
107 * new sequence has been seen.
109 * The compression ratio is checked at the first end of a packet
110 * after the appropriate gap. Besides simplifying and speeding
111 * things up, this makes it more likely that the transmitter
112 * and receiver will agree when the dictionary is cleared when
113 * compression is not going well.
117 * A dictionary for doing BSD compress.
120 int totlen; /* length of this structure */
121 u_int hsize; /* size of the hash table */
122 u_char hshift; /* used in hash function */
123 u_char n_bits; /* current bits/code */
127 u_short seqno; /* sequence number of next packet */
128 u_int hdrlen; /* header length to preallocate */
130 u_int maxmaxcode; /* largest valid code */
131 u_int max_ent; /* largest code in use */
132 u_int in_count; /* uncompressed bytes, aged */
133 u_int bytes_out; /* compressed bytes, aged */
134 u_int ratio; /* recent compression ratio */
135 u_int checkpoint; /* when to next check the ratio */
136 u_int clear_count; /* times dictionary cleared */
137 u_int incomp_count; /* incompressible packets */
138 u_int incomp_bytes; /* incompressible bytes */
139 u_int uncomp_count; /* uncompressed packets */
140 u_int uncomp_bytes; /* uncompressed bytes */
141 u_int comp_count; /* compressed packets */
142 u_int comp_bytes; /* compressed bytes */
143 u_short *lens; /* array of lengths of codes */
145 union { /* hash value */
148 #ifdef BSD_LITTLE_ENDIAN
149 u_short prefix; /* preceding code */
150 u_char suffix; /* last character of new code */
154 u_char suffix; /* last character of new code */
155 u_short prefix; /* preceding code */
159 u_short codem1; /* output of hash table -1 */
160 u_short cptr; /* map code to hash table entry */
164 #define BSD_OVHD 2 /* BSD compress overhead/packet */
165 #define BSD_INIT_BITS BSD_MIN_BITS
167 static void *bsd_comp_alloc __P((u_char *options, int opt_len));
168 static void *bsd_decomp_alloc __P((u_char *options, int opt_len));
169 static void bsd_free __P((void *state));
170 static int bsd_comp_init __P((void *state, u_char *options, int opt_len,
171 int unit, int hdrlen, int debug));
172 static int bsd_decomp_init __P((void *state, u_char *options, int opt_len,
173 int unit, int hdrlen, int mru, int debug));
174 static int bsd_compress __P((void *state, mblk_t **mret,
175 mblk_t *mp, int slen, int maxolen));
176 static void bsd_incomp __P((void *state, mblk_t *dmsg));
177 static int bsd_decompress __P((void *state, mblk_t *cmp, mblk_t **dmpp));
178 static void bsd_reset __P((void *state));
179 static void bsd_comp_stats __P((void *state, struct compstat *stats));
182 * Procedures exported to ppp_comp.c.
184 struct compressor ppp_bsd_compress = {
185 CI_BSD_COMPRESS, /* compress_proto */
186 bsd_comp_alloc, /* comp_alloc */
187 bsd_free, /* comp_free */
188 bsd_comp_init, /* comp_init */
189 bsd_reset, /* comp_reset */
190 bsd_compress, /* compress */
191 bsd_comp_stats, /* comp_stat */
192 bsd_decomp_alloc, /* decomp_alloc */
193 bsd_free, /* decomp_free */
194 bsd_decomp_init, /* decomp_init */
195 bsd_reset, /* decomp_reset */
196 bsd_decompress, /* decompress */
197 bsd_incomp, /* incomp */
198 bsd_comp_stats, /* decomp_stat */
202 * the next two codes should not be changed lightly, as they must not
203 * lie within the contiguous general code space.
205 #define CLEAR 256 /* table clear output code */
206 #define FIRST 257 /* first free entry */
209 #define MAXCODE(b) ((1 << (b)) - 1)
210 #define BADCODEM1 MAXCODE(BSD_MAX_BITS)
212 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
213 ^ (u_int32_t)(prefix))
214 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
215 + (u_int32_t)(prefix))
217 #define CHECK_GAP 10000 /* Ratio check interval */
219 #define RATIO_SCALE_LOG 8
220 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
221 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
223 #define DECOMP_CHUNK 256
226 * clear the dictionary
233 db->max_ent = FIRST-1;
234 db->n_bits = BSD_INIT_BITS;
238 db->incomp_count = 0;
239 db->checkpoint = CHECK_GAP;
243 * If the dictionary is full, then see if it is time to reset it.
245 * Compute the compression ratio using fixed-point arithmetic
246 * with 8 fractional bits.
248 * Since we have an infinite stream instead of a single file,
249 * watch only the local compression ratio.
251 * Since both peers must reset the dictionary at the same time even in
252 * the absence of CLEAR codes (while packets are incompressible), they
253 * must compute the same ratio.
255 static int /* 1=output CLEAR */
261 if (db->in_count >= db->checkpoint) {
262 /* age the ratio by limiting the size of the counts */
263 if (db->in_count >= RATIO_MAX
264 || db->bytes_out >= RATIO_MAX) {
265 db->in_count -= db->in_count/4;
266 db->bytes_out -= db->bytes_out/4;
269 db->checkpoint = db->in_count + CHECK_GAP;
271 if (db->max_ent >= db->maxmaxcode) {
272 /* Reset the dictionary only if the ratio is worse,
273 * or if it looks as if it has been poisoned
274 * by incompressible data.
276 * This does not overflow, because
277 * db->in_count <= RATIO_MAX.
279 new_ratio = db->in_count << RATIO_SCALE_LOG;
280 if (db->bytes_out != 0)
281 new_ratio /= db->bytes_out;
283 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
287 db->ratio = new_ratio;
297 bsd_comp_stats(state, stats)
299 struct compstat *stats;
301 struct bsd_db *db = (struct bsd_db *) state;
304 stats->unc_bytes = db->uncomp_bytes;
305 stats->unc_packets = db->uncomp_count;
306 stats->comp_bytes = db->comp_bytes;
307 stats->comp_packets = db->comp_count;
308 stats->inc_bytes = db->incomp_bytes;
309 stats->inc_packets = db->incomp_count;
310 stats->ratio = db->in_count;
312 if (stats->ratio <= 0x7fffff)
321 * Reset state, as on a CCP ResetReq.
327 struct bsd_db *db = (struct bsd_db *) state;
335 * Allocate space for a (de) compressor.
338 bsd_alloc(options, opt_len, decomp)
343 u_int newlen, hsize, hshift, maxmaxcode;
346 if (opt_len != 3 || options[0] != CI_BSD_COMPRESS || options[1] != 3
347 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
349 bits = BSD_NBITS(options[2]);
351 case 9: /* needs 82152 for both directions */
352 case 10: /* needs 84144 */
353 case 11: /* needs 88240 */
354 case 12: /* needs 96432 */
358 case 13: /* needs 176784 */
362 case 14: /* needs 353744 */
366 case 15: /* needs 691440 */
370 case 16: /* needs 1366160--far too much, */
371 /* hsize = 69001; */ /* and 69001 is too big for cptr */
372 /* hshift = 8; */ /* in struct bsd_db */
378 maxmaxcode = MAXCODE(bits);
379 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
380 db = (struct bsd_db *) ALLOCATE(newlen);
383 bzero(db, sizeof(*db) - sizeof(db->dict));
388 db->lens = (u_short *) ALLOCATE((maxmaxcode+1) * sizeof(db->lens[0]));
398 db->maxmaxcode = maxmaxcode;
408 struct bsd_db *db = (struct bsd_db *) state;
411 FREE(db->lens, (db->maxmaxcode+1) * sizeof(db->lens[0]));
412 FREE(db, db->totlen);
416 bsd_comp_alloc(options, opt_len)
420 return bsd_alloc(options, opt_len, 0);
424 bsd_decomp_alloc(options, opt_len)
428 return bsd_alloc(options, opt_len, 1);
432 * Initialize the database.
435 bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp)
438 int opt_len, unit, hdrlen, mru, debug, decomp;
442 if (opt_len != 3 || options[0] != CI_BSD_COMPRESS || options[1] != 3
443 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
444 || BSD_NBITS(options[2]) != db->maxbits
445 || decomp && db->lens == NULL)
455 db->dict[--i].codem1 = BADCODEM1;
456 db->dict[i].cptr = 0;
471 bsd_comp_init(state, options, opt_len, unit, hdrlen, debug)
474 int opt_len, unit, hdrlen, debug;
476 return bsd_init((struct bsd_db *) state, options, opt_len,
477 unit, hdrlen, 0, debug, 0);
481 bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
484 int opt_len, unit, hdrlen, mru, debug;
486 return bsd_init((struct bsd_db *) state, options, opt_len,
487 unit, hdrlen, mru, debug, 1);
493 * One change from the BSD compress command is that when the
494 * code size expands, we do not output a bunch of padding.
496 * N.B. at present, we ignore the hdrlen specified in the comp_init call.
498 static int /* new slen */
499 bsd_compress(state, mretp, mp, slen, maxolen)
501 mblk_t **mretp; /* return compressed mbuf chain here */
502 mblk_t *mp; /* from here */
503 int slen; /* uncompressed length */
504 int maxolen; /* max compressed length */
506 struct bsd_db *db = (struct bsd_db *) state;
507 int hshift = db->hshift;
508 u_int max_ent = db->max_ent;
509 u_int n_bits = db->n_bits;
511 u_int32_t accm = 0, fcode;
512 struct bsd_dict *dictp;
514 int hval, disp, ent, ilen;
521 #define PUTBYTE(v) { \
524 if (wptr >= cp_end) { \
529 cp_end = m->b_datap->db_lim; \
537 #define OUTPUT(ent) { \
539 accm |= ((ent) << bitno); \
541 PUTBYTE(accm >> 24); \
544 } while (bitno <= 24); \
548 * First get the protocol and check that we're
549 * interested in this packet.
553 if (rptr + PPP_HDRLEN > mp->b_wptr) {
554 if (!pullupmsg(mp, PPP_HDRLEN))
558 ent = PPP_PROTOCOL(rptr); /* get the protocol */
559 if (ent < 0x21 || ent > 0xf9)
562 /* Don't generate compressed packets which are larger than
563 the uncompressed packet. */
567 /* Allocate enough message blocks to give maxolen total space. */
569 for (olen = maxolen; olen > 0; ) {
570 m = allocb((olen < 4096? olen: 4096), BPRI_MED);
580 olen -= m->b_datap->db_lim - m->b_wptr;
584 if ((m = mret) != NULL) {
586 cp_end = m->b_datap->db_lim;
588 wptr = cp_end = NULL;
592 * Copy the PPP header over, changing the protocol,
593 * and install the 2-byte sequence number.
596 wptr[0] = PPP_ADDRESS(rptr);
597 wptr[1] = PPP_CONTROL(rptr);
598 wptr[2] = 0; /* change the protocol */
600 wptr[4] = db->seqno >> 8;
602 wptr += PPP_HDRLEN + BSD_OVHD;
607 slen = mp->b_wptr - rptr;
615 slen = np->b_wptr - rptr;
618 continue; /* handle 0-length buffers */
624 fcode = BSD_KEY(ent, c);
625 hval = BSD_HASH(ent, c, hshift);
626 dictp = &db->dict[hval];
628 /* Validate and then check the entry. */
629 if (dictp->codem1 >= max_ent)
631 if (dictp->f.fcode == fcode) {
632 ent = dictp->codem1+1;
633 continue; /* found (prefix,suffix) */
636 /* continue probing until a match or invalid entry */
637 disp = (hval == 0) ? 1 : hval;
640 if (hval >= db->hsize)
642 dictp = &db->dict[hval];
643 if (dictp->codem1 >= max_ent)
645 } while (dictp->f.fcode != fcode);
646 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
650 OUTPUT(ent); /* output the prefix */
652 /* code -> hashtable */
653 if (max_ent < db->maxmaxcode) {
654 struct bsd_dict *dictp2;
655 /* expand code size if needed */
656 if (max_ent >= MAXCODE(n_bits))
657 db->n_bits = ++n_bits;
659 /* Invalidate old hash table entry using
660 * this code, and then take it over.
662 dictp2 = &db->dict[max_ent+1];
663 if (db->dict[dictp2->cptr].codem1 == max_ent)
664 db->dict[dictp2->cptr].codem1 = BADCODEM1;
666 dictp->codem1 = max_ent;
667 dictp->f.fcode = fcode;
669 db->max_ent = ++max_ent;
674 OUTPUT(ent); /* output the last code */
675 db->bytes_out += olen;
676 db->in_count += ilen;
678 ++db->bytes_out; /* count complete bytes */
681 OUTPUT(CLEAR); /* do not count the CLEAR */
684 * Pad dribble bits of last code with ones.
685 * Do not emit a completely useless byte of ones.
688 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
691 * Increase code size if we would have without the packet
692 * boundary and as the decompressor will.
694 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
697 db->uncomp_bytes += ilen;
699 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen && mret != NULL) {
700 /* throw away the compressed stuff if it is longer than uncompressed */
704 db->incomp_bytes += ilen;
705 } else if (wptr != NULL) {
712 db->comp_bytes += olen + BSD_OVHD;
716 return olen + PPP_HDRLEN + BSD_OVHD;
723 * Update the "BSD Compress" dictionary on the receiver for
724 * incompressible data by pretending to compress the incoming data.
727 bsd_incomp(state, dmsg)
731 struct bsd_db *db = (struct bsd_db *) state;
732 u_int hshift = db->hshift;
733 u_int max_ent = db->max_ent;
734 u_int n_bits = db->n_bits;
735 struct bsd_dict *dictp;
745 if (rptr + PPP_HDRLEN > dmsg->b_wptr) {
746 if (!pullupmsg(dmsg, PPP_HDRLEN))
750 ent = PPP_PROTOCOL(rptr); /* get the protocol */
751 if (ent < 0x21 || ent > 0xf9)
756 ilen = 1; /* count the protocol as 1 byte */
759 slen = dmsg->b_wptr - rptr;
765 continue; /* skip zero-length buffers */
771 fcode = BSD_KEY(ent, c);
772 hval = BSD_HASH(ent, c, hshift);
773 dictp = &db->dict[hval];
775 /* validate and then check the entry */
776 if (dictp->codem1 >= max_ent)
778 if (dictp->f.fcode == fcode) {
779 ent = dictp->codem1+1;
780 continue; /* found (prefix,suffix) */
783 /* continue probing until a match or invalid entry */
784 disp = (hval == 0) ? 1 : hval;
787 if (hval >= db->hsize)
789 dictp = &db->dict[hval];
790 if (dictp->codem1 >= max_ent)
792 } while (dictp->f.fcode != fcode);
793 ent = dictp->codem1+1;
794 continue; /* finally found (prefix,suffix) */
796 nomatch: /* output (count) the prefix */
799 /* code -> hashtable */
800 if (max_ent < db->maxmaxcode) {
801 struct bsd_dict *dictp2;
802 /* expand code size if needed */
803 if (max_ent >= MAXCODE(n_bits))
804 db->n_bits = ++n_bits;
806 /* Invalidate previous hash table entry
807 * assigned this code, and then take it over.
809 dictp2 = &db->dict[max_ent+1];
810 if (db->dict[dictp2->cptr].codem1 == max_ent)
811 db->dict[dictp2->cptr].codem1 = BADCODEM1;
813 dictp->codem1 = max_ent;
814 dictp->f.fcode = fcode;
816 db->max_ent = ++max_ent;
817 db->lens[max_ent] = db->lens[ent]+1;
820 } while (--slen != 0);
822 bitno += n_bits; /* output (count) the last code */
823 db->bytes_out += bitno/8;
824 db->in_count += ilen;
828 db->incomp_bytes += ilen;
830 db->uncomp_bytes += ilen;
832 /* Increase code size if we would have without the packet
833 * boundary and as the decompressor will.
835 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
841 * Decompress "BSD Compress"
843 * Because of patent problems, we return DECOMP_ERROR for errors
844 * found by inspecting the input data and for system problems, but
845 * DECOMP_FATALERROR for any errors which could possibly be said to
846 * be being detected "after" decompression. For DECOMP_ERROR,
847 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
848 * infringing a patent of Motorola's if we do, so we take CCP down
851 * Given that the frame has the correct sequence number and a good FCS,
852 * errors such as invalid codes in the input most likely indicate a
853 * bug, so we return DECOMP_FATALERROR for them in order to turn off
854 * compression, even though they are detected by inspecting the input.
857 bsd_decompress(state, cmsg, dmpp)
859 mblk_t *cmsg, **dmpp;
861 struct bsd_db *db = (struct bsd_db *) state;
862 u_int max_ent = db->max_ent;
864 u_int bitno = 32; /* 1st valid bit in accm */
865 u_int n_bits = db->n_bits;
866 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
867 struct bsd_dict *dictp;
868 int explen, i, seq, len;
869 u_int incode, oldcode, finchar;
870 u_char *p, *rptr, *wptr;
872 int adrs, ctrl, ilen;
873 int dlen, space, codelen, extra;
876 * Get at least the BSD Compress header in the first buffer
879 if (rptr + PPP_HDRLEN + BSD_OVHD >= cmsg->b_wptr) {
880 if (!pullupmsg(cmsg, PPP_HDRLEN + BSD_OVHD + 1)) {
882 printf("bsd_decomp%d: failed to pullup\n", db->unit);
889 * Save the address/control from the PPP header
890 * and then get the sequence number.
892 adrs = PPP_ADDRESS(rptr);
893 ctrl = PPP_CONTROL(rptr);
895 seq = (rptr[0] << 8) + rptr[1];
897 ilen = len = cmsg->b_wptr - rptr;
900 * Check the sequence number and give up if it is not what we expect.
902 if (seq != db->seqno++) {
904 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
905 db->unit, seq, db->seqno - 1);
910 * Allocate one message block to start with.
912 if ((dmsg = allocb(DECOMP_CHUNK + db->hdrlen, BPRI_MED)) == NULL)
915 dmsg->b_wptr += db->hdrlen;
916 dmsg->b_rptr = wptr = dmsg->b_wptr;
918 /* Fill in the ppp header, but not the last byte of the protocol
919 (that comes from the decompressed data). */
923 wptr += PPP_HDRLEN - 1;
924 space = dmsg->b_datap->db_lim - wptr;
931 if (!cmsg) /* quit at end of message */
934 len = cmsg->b_wptr - rptr;
936 continue; /* handle 0-length buffers */
940 * Accumulate bytes until we have a complete code.
941 * Then get the next code, relying on the 32-bit,
942 * unsigned accm to mask the result.
945 accm |= *rptr++ << bitno;
947 if (tgtbitno < bitno)
949 incode = accm >> tgtbitno;
953 if (incode == CLEAR) {
955 * The dictionary must only be cleared at
956 * the end of a packet. But there could be an
957 * empty message block at the end.
959 if (len > 0 || cmsg->b_cont != 0) {
961 len += msgdsize(cmsg->b_cont);
965 printf("bsd_decomp%d: bad CLEAR\n", db->unit);
966 return DECOMP_FATALERROR;
974 if (incode > max_ent + 2 || incode > db->maxmaxcode
975 || incode > max_ent && oldcode == CLEAR) {
978 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
979 db->unit, incode, oldcode);
980 printf("max_ent=0x%x dlen=%d seqno=%d\n",
981 max_ent, dlen, db->seqno);
983 return DECOMP_FATALERROR; /* probably a bug */
986 /* Special case for KwKwK string. */
987 if (incode > max_ent) {
995 codelen = db->lens[finchar];
996 explen += codelen + extra;
997 if (explen > db->mru + 1) {
1000 printf("bsd_decomp%d: ran out of mru\n", db->unit);
1001 return DECOMP_FATALERROR;
1005 * Decode this code and install it in the decompressed buffer.
1007 space -= codelen + extra;
1009 /* Allocate another message block. */
1010 dmsg->b_wptr = wptr;
1011 dlen = codelen + extra;
1012 if (dlen < DECOMP_CHUNK)
1013 dlen = DECOMP_CHUNK;
1014 if ((dmsg->b_cont = allocb(dlen, BPRI_MED)) == NULL) {
1016 return DECOMP_ERROR;
1018 dmsg = dmsg->b_cont;
1019 wptr = dmsg->b_wptr;
1020 space = dmsg->b_datap->db_lim - wptr - codelen - extra;
1022 p = (wptr += codelen);
1023 while (finchar > LAST) {
1024 dictp = &db->dict[db->dict[finchar].cptr];
1029 printf("bsd_decomp%d: fell off end of chain ", db->unit);
1030 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1031 incode, finchar, db->dict[finchar].cptr, max_ent);
1032 return DECOMP_FATALERROR;
1034 if (dictp->codem1 != finchar-1) {
1036 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1037 db->unit, incode, finchar);
1038 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
1039 db->dict[finchar].cptr, dictp->codem1);
1040 return DECOMP_FATALERROR;
1043 *--p = dictp->f.hs.suffix;
1044 finchar = dictp->f.hs.prefix;
1050 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1051 db->unit, codelen, incode, max_ent);
1054 if (extra) /* the KwKwK case again */
1058 * If not first code in a packet, and
1059 * if not out of code space, then allocate a new code.
1061 * Keep the hash table correct so it can be used
1062 * with uncompressed packets.
1064 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1065 struct bsd_dict *dictp2;
1069 fcode = BSD_KEY(oldcode,finchar);
1070 hval = BSD_HASH(oldcode,finchar,db->hshift);
1071 dictp = &db->dict[hval];
1073 /* look for a free hash table entry */
1074 if (dictp->codem1 < max_ent) {
1075 disp = (hval == 0) ? 1 : hval;
1078 if (hval >= db->hsize)
1080 dictp = &db->dict[hval];
1081 } while (dictp->codem1 < max_ent);
1085 * Invalidate previous hash table entry
1086 * assigned this code, and then take it over
1088 dictp2 = &db->dict[max_ent+1];
1089 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1090 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1092 dictp2->cptr = hval;
1093 dictp->codem1 = max_ent;
1094 dictp->f.fcode = fcode;
1096 db->max_ent = ++max_ent;
1097 db->lens[max_ent] = db->lens[oldcode]+1;
1099 /* Expand code size if needed. */
1100 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1101 db->n_bits = ++n_bits;
1102 tgtbitno = 32-n_bits;
1107 dmsg->b_wptr = wptr;
1110 * Keep the checkpoint right so that incompressible packets
1111 * clear the dictionary at the right times.
1113 db->bytes_out += ilen;
1114 db->in_count += explen;
1115 if (bsd_check(db) && db->debug) {
1116 printf("bsd_decomp%d: peer should have cleared dictionary\n",
1121 db->comp_bytes += ilen + BSD_OVHD;
1123 db->uncomp_bytes += explen;
1128 #endif /* DO_BSD_COMPRESS */