get rid of warning
[mpls-ppp.git] / NeXT / bsd-comp.c
blob8ae940450513e17076aed67f08524ab07bcbcd66
1 /* Because this code is derived from the 4.3BSD compress source:
4 * Copyright (c) 1985, 1986 The Regents of the University of California.
5 * All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * James A. Woods, derived from original work by Spencer Thomas
9 * and Joseph Orost.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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
37 * SUCH DAMAGE.
39 * Rewritten for NextStep's funky kernel functions, I/O threads,
40 * and netbufs (instead of real mbufs). Also, ifnets don't install
41 * into the kernel under NS as they do under BSD. We have tried to
42 * make the code remain as similar to the NetBSD version without
43 * incurring too much hassle. This code is the merge of
44 * Philip Prindeville's <philipp@res.enst.fr>/Pete French's <pete@ohm.york.ac.uk>
45 * and Stephen Perkins' <perkins@cps.msu.edu> independent ports.
50 * This version is for use with mbufs on BSD-derived systems.
52 * $Id: bsd-comp.c,v 1.5 1998/03/26 02:51:45 paulus Exp $
55 #include <sys/types.h>
56 #include <sys/param.h>
57 #include <sys/socket.h>
59 #define KERNEL 1
60 #include <net/netbuf.h>
61 #include <net/if.h>
63 #include <net/ppp_defs.h>
65 #include <net/if_ppp.h>
67 #include "nbq.h"
69 #define PACKETPTR NETBUF_T
70 #include <net/ppp-comp.h>
74 * We align with this number of bits zero. The code makes the somewhat
75 * suspect assumption that an address can be held in an unsigned long.
76 * Sadly this is necessary to do bit operations on it.
79 #define Z_ALIGN 3 /* 8 byte boudary */
80 #define Z_EXTRA ((unsigned long)((1<<Z_ALIGN)-1))
81 #define ALIGN(x) ((x+Z_EXTRA) & ~Z_EXTRA)
83 #if DO_BSD_COMPRESS
86 * The following includes are necessary to correctly
87 * support BYTE_ORDER. -SJP
90 #include <netinet/in.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/in_var.h>
93 #include <netinet/ip.h>
95 #define mtod(m,type) ((type)NB_MAP(m))
98 * PPP "BSD compress" compression
99 * The differences between this compression and the classic BSD LZW
100 * source are obvious from the requirement that the classic code worked
101 * with files while this handles arbitrarily long streams that
102 * are broken into packets. They are:
104 * When the code size expands, a block of junk is not emitted by
105 * the compressor and not expected by the decompressor.
107 * New codes are not necessarily assigned every time an old
108 * code is output by the compressor. This is because a packet
109 * end forces a code to be emitted, but does not imply that a
110 * new sequence has been seen.
112 * The compression ratio is checked at the first end of a packet
113 * after the appropriate gap. Besides simplifying and speeding
114 * things up, this makes it more likely that the transmitter
115 * and receiver will agree when the dictionary is cleared when
116 * compression is not going well.
120 * A dictionary for doing BSD compress.
122 struct bsd_db {
123 void *kbase; /* actual kalloc'd address for struct */
124 void *klens; /* actual kalloc'd address for lens */
125 int totlen; /* length of this structure */
126 u_int hsize; /* size of the hash table */
127 u_char hshift; /* used in hash function */
128 u_char n_bits; /* current bits/code */
129 u_char maxbits;
130 u_char debug;
131 u_char unit;
132 u_int16_t seqno; /* sequence # of next packet */
133 u_int hdrlen; /* header length to preallocate */
134 u_int mru;
135 u_int maxmaxcode; /* largest valid code */
136 u_int max_ent; /* largest code in use */
137 u_int in_count; /* uncompressed bytes, aged */
138 u_int bytes_out; /* compressed bytes, aged */
139 u_int ratio; /* recent compression ratio */
140 u_int checkpoint; /* when to next check the ratio */
141 u_int clear_count; /* times dictionary cleared */
142 u_int incomp_count; /* incompressible packets */
143 u_int incomp_bytes; /* incompressible bytes */
144 u_int uncomp_count; /* uncompressed packets */
145 u_int uncomp_bytes; /* uncompressed bytes */
146 u_int comp_count; /* compressed packets */
147 u_int comp_bytes; /* compressed bytes */
148 u_int16_t *lens; /* array of lengths of codes */
149 struct bsd_dict {
150 union { /* hash value */
151 u_int32_t fcode;
152 struct {
153 #if BYTE_ORDER == LITTLE_ENDIAN
154 u_int16_t prefix; /* preceding code */
155 u_char suffix; /* last character of new code */
156 u_char pad;
157 #else
158 u_char pad;
159 u_char suffix; /* last character of new code */
160 u_int16_t prefix; /* preceding code */
161 #endif
162 } hs;
163 } f;
164 u_int16_t codem1; /* output of hash table -1 */
165 u_int16_t cptr; /* map code to hash table entry */
166 } dict[1];
169 #define BSD_OVHD 2 /* BSD compress overhead/packet */
170 #define BSD_INIT_BITS BSD_MIN_BITS
172 static void *bsd_comp_alloc __P((u_char *options, int opt_len));
173 static void *bsd_decomp_alloc __P((u_char *options, int opt_len));
174 static void bsd_free __P((void *state));
175 static int bsd_comp_init __P((void *state, u_char *options, int opt_len,
176 int unit, int hdrlen, int debug));
177 static int bsd_decomp_init __P((void *state, u_char *options, int opt_len,
178 int unit, int hdrlen, int mru, int debug));
179 static int bsd_compress __P((void *state, NETBUF_T *mret,
180 NETBUF_T mp, int slen, int maxolen));
181 static void bsd_incomp __P((void *state, NETBUF_T dmsg));
182 static int bsd_decompress __P((void *state, NETBUF_T cmp, NETBUF_T *dmpp));
183 static void bsd_reset __P((void *state));
184 static void bsd_comp_stats __P((void *state, struct compstat *stats));
187 * Procedures exported to if_ppp.c.
189 struct compressor ppp_bsd_compress = {
190 CI_BSD_COMPRESS, /* compress_proto */
191 bsd_comp_alloc, /* comp_alloc */
192 bsd_free, /* comp_free */
193 bsd_comp_init, /* comp_init */
194 bsd_reset, /* comp_reset */
195 bsd_compress, /* compress */
196 bsd_comp_stats, /* comp_stat */
197 bsd_decomp_alloc, /* decomp_alloc */
198 bsd_free, /* decomp_free */
199 bsd_decomp_init, /* decomp_init */
200 bsd_reset, /* decomp_reset */
201 bsd_decompress, /* decompress */
202 bsd_incomp, /* incomp */
203 bsd_comp_stats, /* decomp_stat */
207 * the next two codes should not be changed lightly, as they must not
208 * lie within the contiguous general code space.
210 #define CLEAR 256 /* table clear output code */
211 #define FIRST 257 /* first free entry */
212 #define LAST 255
214 #define MAXCODE(b) ((1 << (b)) - 1)
215 #define BADCODEM1 MAXCODE(BSD_MAX_BITS)
217 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
218 ^ (u_int32_t)(prefix))
219 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
220 + (u_int32_t)(prefix))
222 #define CHECK_GAP 10000 /* Ratio check interval */
224 #define RATIO_SCALE_LOG 8
225 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
226 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
228 /* Could include inlines.h */
229 #ifndef IOLog
230 #define IOLog printf
231 #define IOLogDbg if (db->debug) printf
232 #else
233 #define IOLogDbg if (db->debug) IOLog
234 #endif
237 * clear the dictionary
239 static void
240 bsd_clear(db)
241 struct bsd_db *db;
243 db->clear_count++;
244 db->max_ent = FIRST-1;
245 db->n_bits = BSD_INIT_BITS;
246 db->ratio = 0;
247 db->bytes_out = 0;
248 db->in_count = 0;
249 db->incomp_count = 0;
250 db->checkpoint = CHECK_GAP;
254 * If the dictionary is full, then see if it is time to reset it.
256 * Compute the compression ratio using fixed-point arithmetic
257 * with 8 fractional bits.
259 * Since we have an infinite stream instead of a single file,
260 * watch only the local compression ratio.
262 * Since both peers must reset the dictionary at the same time even in
263 * the absence of CLEAR codes (while packets are incompressible), they
264 * must compute the same ratio.
266 static int /* 1=output CLEAR */
267 bsd_check(db)
268 struct bsd_db *db;
270 u_int new_ratio;
272 if (db->in_count >= db->checkpoint)
275 /* age the ratio by limiting the size of the counts */
276 if (db->in_count >= RATIO_MAX
277 || db->bytes_out >= RATIO_MAX) {
278 db->in_count -= db->in_count/4;
279 db->bytes_out -= db->bytes_out/4;
282 db->checkpoint = db->in_count + CHECK_GAP;
284 if (db->max_ent >= db->maxmaxcode) {
285 /* Reset the dictionary only if the ratio is worse,
286 * or if it looks as if it has been poisoned
287 * by incompressible data.
289 * This does not overflow, because
290 * db->in_count <= RATIO_MAX.
292 new_ratio = db->in_count << RATIO_SCALE_LOG;
293 if (db->bytes_out != 0)
294 new_ratio /= db->bytes_out;
296 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE)
298 bsd_clear(db);
299 return 1;
301 db->ratio = new_ratio;
304 return 0;
308 * Return statistics.
310 static void
311 bsd_comp_stats(state, stats)
312 void *state;
313 struct compstat *stats;
315 struct bsd_db *db = (struct bsd_db *) state;
316 u_int out;
318 stats->unc_bytes = db->uncomp_bytes;
319 stats->unc_packets = db->uncomp_count;
320 stats->comp_bytes = db->comp_bytes;
321 stats->comp_packets = db->comp_count;
322 stats->inc_bytes = db->incomp_bytes;
323 stats->inc_packets = db->incomp_count;
324 stats->ratio = db->in_count;
325 out = db->bytes_out;
326 if (stats->ratio <= 0x7fffff)
327 stats->ratio <<= 8;
328 else
329 out >>= 8;
330 if (out != 0)
331 stats->ratio /= out;
335 * Reset state, as on a CCP ResetReq.
337 static void
338 bsd_reset(state)
339 void *state;
341 struct bsd_db *db = (struct bsd_db *) state;
343 db->seqno = 0;
344 bsd_clear(db);
345 db->clear_count = 0;
349 * Allocate space for a (de) compressor.
351 static void *
352 bsd_alloc(options, opt_len, decomp)
353 u_char *options;
354 int opt_len, decomp;
356 int bits;
357 u_int newlen, hsize, hshift, maxmaxcode;
358 struct bsd_db *db;
360 if (opt_len != CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
361 || options[1] != CILEN_BSD_COMPRESS
362 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
363 return NULL;
364 bits = BSD_NBITS(options[2]);
365 switch (bits) {
366 case 9: /* needs 82152 for both directions */
367 case 10: /* needs 84144 */
368 case 11: /* needs 88240 */
369 case 12: /* needs 96432 */
370 hsize = 5003;
371 hshift = 4;
372 break;
373 case 13: /* needs 176784 */
374 hsize = 9001;
375 hshift = 5;
376 break;
377 case 14: /* needs 353744 */
378 hsize = 18013;
379 hshift = 6;
380 break;
381 case 15: /* needs 691440 */
382 hsize = 35023;
383 hshift = 7;
384 break;
385 case 16: /* needs 1366160--far too much, */
386 /* hsize = 69001; */ /* and 69001 is too big for cptr */
387 /* hshift = 8; */ /* in struct bsd_db */
388 /* break; */
389 default:
390 return NULL;
393 maxmaxcode = MAXCODE(bits);
394 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
396 unsigned long kret;
397 kret = (unsigned long) kalloc(Z_EXTRA + newlen);
398 if (!kret)
399 return NULL;
400 db = (struct bsd_db *) ALIGN(kret);
401 bzero(db, sizeof(*db) - sizeof(db->dict));
402 db->kbase = (void *)kret;
405 if (!decomp) {
406 db->lens = NULL;
407 } else {
408 unsigned long kret;
409 kret = (unsigned long) kalloc(Z_EXTRA +
410 ((maxmaxcode+1) * sizeof(db->lens[0])));
411 if (!kret) {
412 kfree(db->kbase, newlen + Z_EXTRA);
413 return NULL;
415 db->lens = (u_int16_t *) ALIGN(kret);
416 db->klens = (void *) kret;
419 db->totlen = newlen;
420 db->hsize = hsize;
421 db->hshift = hshift;
422 db->maxmaxcode = maxmaxcode;
423 db->maxbits = bits;
425 return (void *) db;
428 static void
429 bsd_free(state)
430 void *state;
432 struct bsd_db *db = (struct bsd_db *) state;
434 if (db->lens)
435 kfree(db->klens, ((db->maxmaxcode+1) * sizeof(db->lens[0])) + Z_EXTRA);
436 kfree(db->kbase, db->totlen + Z_EXTRA);
439 static void *
440 bsd_comp_alloc(options, opt_len)
441 u_char *options;
442 int opt_len;
444 return bsd_alloc(options, opt_len, 0);
447 static void *
448 bsd_decomp_alloc(options, opt_len)
449 u_char *options;
450 int opt_len;
452 return bsd_alloc(options, opt_len, 1);
456 * Initialize the database.
458 static int
459 bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp)
460 struct bsd_db *db;
461 u_char *options;
462 int opt_len, unit, hdrlen, mru, debug, decomp;
464 int i;
466 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
467 || options[1] != CILEN_BSD_COMPRESS
468 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
469 || BSD_NBITS(options[2]) != db->maxbits
470 || decomp && db->lens == NULL)
471 return 0;
473 if (decomp) {
474 i = LAST+1;
475 while (i != 0)
476 db->lens[--i] = 1;
478 i = db->hsize;
479 while (i != 0) {
480 db->dict[--i].codem1 = BADCODEM1;
481 db->dict[i].cptr = 0;
484 db->unit = unit;
485 db->hdrlen = hdrlen;
486 db->mru = mru;
487 #ifndef DEBUG
488 if (debug)
489 #endif
490 db->debug = 1;
492 bsd_reset(db);
494 return 1;
497 static int
498 bsd_comp_init(state, options, opt_len, unit, hdrlen, debug)
499 void *state;
500 u_char *options;
501 int opt_len, unit, hdrlen, debug;
503 return bsd_init((struct bsd_db *) state, options, opt_len,
504 unit, hdrlen, 0, debug, 0);
507 static int
508 bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
509 void *state;
510 u_char *options;
511 int opt_len, unit, hdrlen, mru, debug;
513 return bsd_init((struct bsd_db *) state, options, opt_len,
514 unit, hdrlen, mru, debug, 1);
519 * compress a packet
520 * One change from the BSD compress command is that when the
521 * code size expands, we do not output a bunch of padding.
523 int /* new slen */
524 bsd_compress(state, mret, mp, slen, maxolen)
525 void *state;
526 NETBUF_T *mret; /* return compressed netbuf here */
527 NETBUF_T mp; /* from here */
528 int slen; /* uncompressed length */
529 int maxolen; /* max compressed length */
531 struct bsd_db *db = (struct bsd_db *) state;
532 int hshift = db->hshift;
533 u_int max_ent = db->max_ent;
534 u_int n_bits = db->n_bits;
535 u_int bitno = 32;
536 u_int32_t accm = 0, fcode;
537 struct bsd_dict *dictp;
538 u_char c;
539 int hval, disp, ent, ilen;
540 u_char *rptr, *wptr;
541 u_char *cp_end;
542 int olen;
543 NETBUF_T m;
545 #define PUTBYTE(v) { \
546 ++olen; \
547 if (wptr) { \
548 *wptr++ = (v); \
549 if (wptr >= cp_end) \
550 wptr = NULL; \
554 #define OUTPUT(ent) { \
555 bitno -= n_bits; \
556 accm |= ((ent) << bitno); \
557 do { \
558 PUTBYTE(accm >> 24); \
559 accm <<= 8; \
560 bitno += 8; \
561 } while (bitno <= 24); \
565 * If the protocol is not in the range we're interested in,
566 * just return without compressing the packet. If it is,
567 * the protocol becomes the first byte to compress.
569 rptr = mtod(mp, u_char *);
570 ent = PPP_PROTOCOL(rptr);
571 if (ent < CI_BSD_COMPRESS || ent > 0xf9) {
572 *mret = NULL;
573 return slen;
576 /* Don't generate compressed packets which are larger than
577 the uncompressed packet. */
578 if (maxolen > slen)
579 maxolen = slen;
581 /* Allocate one mbuf to start with. (don't forget space for the FCS!) */
582 m = NB_ALLOC(maxolen + db->hdrlen + PPP_FCSLEN);
583 *mret = m;
584 if (m != NULL) {
585 if (db->hdrlen > 0)
586 NB_SHRINK_TOP(m, db->hdrlen);
587 NB_SHRINK_BOT(m, PPP_FCSLEN); /* grown by pppstart() */
588 wptr = mtod(m, u_char *);
589 cp_end = wptr + maxolen;
590 } else
591 wptr = cp_end = NULL;
594 * Copy the PPP header over, changing the protocol,
595 * and install the 2-byte packet sequence number.
597 if (wptr) {
598 *wptr++ = PPP_ADDRESS(rptr); /* assumes the ppp header is */
599 *wptr++ = PPP_CONTROL(rptr); /* all in one mbuf */
600 *wptr++ = 0; /* change the protocol */
601 *wptr++ = PPP_COMP;
602 *wptr++ = db->seqno >> 8;
603 *wptr++ = db->seqno;
605 ++db->seqno;
607 olen = 0;
608 rptr += PPP_HDRLEN;
609 slen = NB_SIZE(mp) - PPP_HDRLEN;
610 ilen = slen + 1;
611 while (slen > 0) {
612 slen--;
613 c = *rptr++;
614 fcode = BSD_KEY(ent, c);
615 hval = BSD_HASH(ent, c, hshift);
616 dictp = &db->dict[hval];
618 /* Validate and then check the entry. */
619 if (dictp->codem1 >= max_ent)
620 goto nomatch;
621 if (dictp->f.fcode == fcode) {
622 ent = dictp->codem1+1;
623 continue; /* found (prefix,suffix) */
626 /* continue probing until a match or invalid entry */
627 disp = (hval == 0) ? 1 : hval;
628 do {
629 hval += disp;
630 if (hval >= db->hsize)
631 hval -= db->hsize;
632 dictp = &db->dict[hval];
633 if (dictp->codem1 >= max_ent)
634 goto nomatch;
635 } while (dictp->f.fcode != fcode);
636 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
637 continue;
639 nomatch:
640 OUTPUT(ent); /* output the prefix */
642 /* code -> hashtable */
643 if (max_ent < db->maxmaxcode) {
644 struct bsd_dict *dictp2;
645 /* expand code size if needed */
646 if (max_ent >= MAXCODE(n_bits))
647 db->n_bits = ++n_bits;
649 /* Invalidate old hash table entry using
650 * this code, and then take it over.
652 dictp2 = &db->dict[max_ent+1];
653 if (db->dict[dictp2->cptr].codem1 == max_ent)
654 db->dict[dictp2->cptr].codem1 = BADCODEM1;
655 dictp2->cptr = hval;
656 dictp->codem1 = max_ent;
657 dictp->f.fcode = fcode;
659 db->max_ent = ++max_ent;
661 ent = c;
664 OUTPUT(ent); /* output the last code */
665 db->bytes_out += olen;
666 db->in_count += ilen;
667 if (bitno < 32)
668 ++db->bytes_out; /* count complete bytes */
670 if (bsd_check(db))
671 OUTPUT(CLEAR); /* do not count the CLEAR */
674 * Pad dribble bits of last code with ones.
675 * Do not emit a completely useless byte of ones.
677 if (bitno != 32)
678 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
681 * Increase code size if we would have without the packet
682 * boundary and as the decompressor will.
684 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
685 db->n_bits++;
687 db->uncomp_bytes += ilen;
688 ++db->uncomp_count;
689 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen || wptr == NULL) {
690 /* throw away the compressed stuff if it is longer than uncompressed */
691 if (*mret != NULL) {
692 NB_FREE(*mret);
693 *mret = NULL;
695 ++db->incomp_count;
696 db->incomp_bytes += ilen;
697 } else {
698 NB_SHRINK_BOT(m, NB_SIZE(m) - (wptr - mtod(m, u_char *)));
699 ++db->comp_count;
700 db->comp_bytes += olen + BSD_OVHD;
703 return olen + PPP_HDRLEN + BSD_OVHD;
704 #undef OUTPUT
705 #undef PUTBYTE
710 * Update the "BSD Compress" dictionary on the receiver for
711 * incompressible data by pretending to compress the incoming data.
713 static void
714 bsd_incomp(state, dmsg)
715 void *state;
716 NETBUF_T dmsg;
718 struct bsd_db *db = (struct bsd_db *) state;
719 u_int hshift = db->hshift;
720 u_int max_ent = db->max_ent;
721 u_int n_bits = db->n_bits;
722 struct bsd_dict *dictp;
723 u_int32_t fcode;
724 u_char c;
725 u_int32_t hval, disp;
726 int slen, ilen;
727 u_int bitno = 7;
728 u_char *rptr;
729 u_int ent;
732 * If the protocol is not in the range we're interested in,
733 * just return without looking at the packet. If it is,
734 * the protocol becomes the first byte to "compress".
736 rptr = mtod(dmsg, u_char *);
737 ent = PPP_PROTOCOL(rptr);
738 if (ent < CI_BSD_COMPRESS || ent > 0xf9)
739 return;
741 db->incomp_count++;
742 db->seqno++;
743 ilen = 1; /* count the protocol as 1 byte */
744 rptr += PPP_HDRLEN;
745 slen = NB_SIZE(dmsg) - PPP_HDRLEN;
746 ilen += slen;
748 do {
749 c = *rptr++;
750 fcode = BSD_KEY(ent, c);
751 hval = BSD_HASH(ent, c, hshift);
752 dictp = &db->dict[hval];
754 /* validate and then check the entry */
755 if (dictp->codem1 >= max_ent)
756 goto nomatch;
757 if (dictp->f.fcode == fcode) {
758 ent = dictp->codem1+1;
759 continue; /* found (prefix,suffix) */
762 /* continue probing until a match or invalid entry */
763 disp = (hval == 0) ? 1 : hval;
764 do {
765 hval += disp;
766 if (hval >= db->hsize)
767 hval -= db->hsize;
768 dictp = &db->dict[hval];
769 if (dictp->codem1 >= max_ent)
770 goto nomatch;
771 } while (dictp->f.fcode != fcode);
772 ent = dictp->codem1+1;
773 continue; /* finally found (prefix,suffix) */
775 nomatch: /* output (count) the prefix */
776 bitno += n_bits;
778 /* code -> hashtable */
779 if (max_ent < db->maxmaxcode) {
780 struct bsd_dict *dictp2;
781 /* expand code size if needed */
782 if (max_ent >= MAXCODE(n_bits))
783 db->n_bits = ++n_bits;
785 /* Invalidate previous hash table entry
786 * assigned this code, and then take it over.
788 dictp2 = &db->dict[max_ent+1];
789 if (db->dict[dictp2->cptr].codem1 == max_ent)
790 db->dict[dictp2->cptr].codem1 = BADCODEM1;
791 dictp2->cptr = hval;
792 dictp->codem1 = max_ent;
793 dictp->f.fcode = fcode;
795 db->max_ent = ++max_ent;
796 db->lens[max_ent] = db->lens[ent]+1;
798 ent = c;
799 } while (--slen != 0);
800 bitno += n_bits; /* output (count) the last code */
801 db->bytes_out += bitno/8;
802 db->in_count += ilen;
803 (void)bsd_check(db);
805 ++db->incomp_count;
806 db->incomp_bytes += ilen;
807 ++db->uncomp_count;
808 db->uncomp_bytes += ilen;
810 /* Increase code size if we would have without the packet
811 * boundary and as the decompressor will.
813 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
814 db->n_bits++;
819 * Decompress "BSD Compress".
821 * Because of patent problems, we return DECOMP_ERROR for errors
822 * found by inspecting the input data and for system problems, but
823 * DECOMP_FATALERROR for any errors which could possibly be said to
824 * be being detected "after" decompression. For DECOMP_ERROR,
825 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
826 * infringing a patent of Motorola's if we do, so we take CCP down
827 * instead.
829 * Given that the frame has the correct sequence number and a good FCS,
830 * errors such as invalid codes in the input most likely indicate a
831 * bug, so we return DECOMP_FATALERROR for them in order to turn off
832 * compression, even though they are detected by inspecting the input.
835 bsd_decompress(state, cmp, dmpp)
836 void *state;
837 NETBUF_T cmp, *dmpp;
839 struct bsd_db *db = (struct bsd_db *) state;
840 u_int max_ent = db->max_ent;
841 u_int32_t accm = 0;
842 u_int bitno = 32; /* 1st valid bit in accm */
843 u_int n_bits = db->n_bits;
844 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
845 struct bsd_dict *dictp;
846 int explen, seq, len;
847 u_int incode, oldcode, finchar;
848 u_char *p, *rptr, *wptr;
849 NETBUF_T dmp, mret;
850 int adrs, ctrl, ilen;
851 int space, codelen, extra, maxilen;
854 * Save the address/control from the PPP header
855 * and then get the sequence number.
857 *dmpp = NULL;
858 rptr = mtod(cmp, u_char *);
859 adrs = PPP_ADDRESS(rptr);
860 ctrl = PPP_CONTROL(rptr);
861 rptr += PPP_HDRLEN;
862 len = NB_SIZE(cmp) - PPP_HDRLEN;
863 seq = (rptr[0] << 8) + rptr[1];
864 rptr += BSD_OVHD;
865 len -= BSD_OVHD;
868 * Check the sequence number and give up if it differs from
869 * the value we're expecting.
871 if (seq != db->seqno) {
872 IOLogDbg("bsd_decomp%d: bad sequence # %d, expected %d\n",
873 db->unit, seq, db->seqno - 1);
874 return DECOMP_ERROR;
876 ++db->seqno;
879 * Allocate an netbuf large enough for all the data.
881 maxilen = db->mru + db->hdrlen + PPP_HDRLEN; /* no FCS */
882 dmp = NB_ALLOC(maxilen); /* XXX */
883 if (dmp == NULL)
884 return DECOMP_ERROR;
885 if (db->hdrlen > 0)
886 NB_SHRINK_TOP(dmp, db->hdrlen);
887 mret = dmp;
888 wptr = mtod(dmp, u_char *);
889 space = NB_SIZE(dmp) - PPP_HDRLEN + 1;
892 * Fill in the ppp header, but not the last byte of the protocol
893 * (that comes from the decompressed data).
895 wptr[0] = adrs;
896 wptr[1] = ctrl;
897 wptr[2] = 0;
898 wptr += PPP_HDRLEN - 1;
900 ilen = len;
901 oldcode = CLEAR;
902 explen = 0;
903 while (len > 0) {
905 * Accumulate bytes until we have a complete code.
906 * Then get the next code, relying on the 32-bit,
907 * unsigned accm to mask the result.
909 bitno -= 8;
910 accm |= *rptr++ << bitno;
911 --len;
912 if (tgtbitno < bitno)
913 continue;
914 incode = accm >> tgtbitno;
915 accm <<= n_bits;
916 bitno += n_bits;
918 if (incode == CLEAR) {
920 * The dictionary must only be cleared at
921 * the end of a packet. But there could be an
922 * empty mbuf at the end.
924 if (len > 0) {
925 NB_FREE(mret);
926 IOLogDbg("bsd_decomp%d: bad CLEAR\n", db->unit);
927 return DECOMP_FATALERROR; /* probably a bug */
929 bsd_clear(db);
930 explen = ilen = 0;
931 break;
934 if (incode > max_ent + 2 || incode > db->maxmaxcode
935 || incode > max_ent && oldcode == CLEAR) {
936 NB_FREE(mret);
937 IOLogDbg("bsd_decomp%d: bad code 0x%x oldcode=0x%x max_ent=0x%x explen=%d seqno=%d\n",
938 db->unit, incode, oldcode, max_ent, explen, db->seqno);
939 return DECOMP_FATALERROR; /* probably a bug */
942 /* Special case for KwKwK string. */
943 if (incode > max_ent) {
944 finchar = oldcode;
945 extra = 1;
946 } else {
947 finchar = incode;
948 extra = 0;
951 codelen = db->lens[finchar];
952 explen += codelen + extra;
953 if (explen > db->mru + 1) {
954 NB_FREE(mret);
955 IOLogDbg("bsd_decomp%d: ran out of mru\n len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
956 db->unit, len, finchar, codelen, explen);
957 return DECOMP_FATALERROR;
961 * If we have no space left, then we've overflowed...
963 if ((space -= codelen + extra) < 0) {
964 IOLog("bsd_decompress%d: no space left in netbuf (need %d bytes)\n",
965 db->unit, (codelen + extra) - space);
966 NB_FREE(mret);
967 return DECOMP_ERROR;
971 * Decode this code and install it in the decompressed buffer.
973 p = (wptr += codelen);
974 while (finchar > LAST) {
975 dictp = &db->dict[db->dict[finchar].cptr];
976 #ifdef DEBUG
977 if (--codelen <= 0 || dictp->codem1 != finchar-1)
978 goto bad;
979 #endif
980 *--p = dictp->f.hs.suffix;
981 finchar = dictp->f.hs.prefix;
983 *--p = finchar;
985 #ifdef DEBUG
986 if (--codelen != 0)
987 IOLog("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
988 db->unit, codelen, incode, max_ent);
989 #endif
991 if (extra) /* the KwKwK case again */
992 *wptr++ = finchar;
995 * If not first code in a packet, and
996 * if not out of code space, then allocate a new code.
998 * Keep the hash table correct so it can be used
999 * with uncompressed packets.
1001 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1002 struct bsd_dict *dictp2;
1003 u_int32_t fcode;
1004 u_int32_t hval, disp;
1006 fcode = BSD_KEY(oldcode,finchar);
1007 hval = BSD_HASH(oldcode,finchar,db->hshift);
1008 dictp = &db->dict[hval];
1010 /* look for a free hash table entry */
1011 if (dictp->codem1 < max_ent) {
1012 disp = (hval == 0) ? 1 : hval;
1013 do {
1014 hval += disp;
1015 if (hval >= db->hsize)
1016 hval -= db->hsize;
1017 dictp = &db->dict[hval];
1018 } while (dictp->codem1 < max_ent);
1022 * Invalidate previous hash table entry
1023 * assigned this code, and then take it over
1025 dictp2 = &db->dict[max_ent+1];
1026 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1027 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1029 dictp2->cptr = hval;
1030 dictp->codem1 = max_ent;
1031 dictp->f.fcode = fcode;
1033 db->max_ent = ++max_ent;
1034 db->lens[max_ent] = db->lens[oldcode]+1;
1036 /* Expand code size if needed. */
1037 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1038 db->n_bits = ++n_bits;
1039 tgtbitno = 32-n_bits;
1042 oldcode = incode;
1044 NB_SHRINK_BOT(dmp, NB_SIZE(dmp) - (wptr - mtod(dmp, u_char *)));
1047 * Keep the checkpoint right so that incompressible packets
1048 * clear the dictionary at the right times.
1050 db->bytes_out += ilen;
1051 db->in_count += explen;
1052 if (bsd_check(db)) {
1053 IOLogDbg("bsd_decomp%d: peer should have cleared dictionary\n",
1054 db->unit);
1057 ++db->comp_count;
1058 db->comp_bytes += ilen + BSD_OVHD;
1059 ++db->uncomp_count;
1060 db->uncomp_bytes += explen;
1062 *dmpp = mret;
1063 return DECOMP_OK;
1065 #ifdef DEBUG
1066 bad:
1067 if (codelen <= 0) {
1068 IOLog("bsd_decomp%d: fell off end of chain 0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1069 db->unit, incode, finchar, db->dict[finchar].cptr, max_ent);
1070 } else if (dictp->codem1 != finchar-1) {
1071 IOLog("bsd_decomp%d: bad code chain 0x%x finchar=0x%x oldcode=0x%x cptr=0x%x codem1=0x%x\n",
1072 db->unit, incode, finchar, oldcode, db->dict[finchar].cptr,
1073 dictp->codem1);
1075 NB_FREE(mret);
1076 return DECOMP_FATALERROR;
1077 #endif /* DEBUG */
1079 #endif /* DO_BSD_COMPRESS */