MFC r1.27:
[dragonfly.git] / sys / net / bsd_comp.c
blob9764ff202759e03c179e8a3590104480318e9bb3
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.
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.11 2006/12/22 23:44:54 swildner Exp $
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <net/ppp_layer/ppp_defs.h>
53 #define PACKETPTR struct mbuf *
54 #include <net/ppp_layer/ppp_comp.h>
56 #if DO_BSD_COMPRESS
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.
82 struct bsd_db {
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 */
87 u_char maxbits;
88 u_char debug;
89 u_char unit;
90 u_int16_t seqno; /* sequence # of next packet */
91 u_int hdrlen; /* header length to preallocate */
92 u_int mru;
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 */
107 struct bsd_dict {
108 union { /* hash value */
109 u_int32_t fcode;
110 struct {
111 #if BYTE_ORDER == LITTLE_ENDIAN
112 u_int16_t prefix; /* preceding code */
113 u_char suffix; /* last character of new code */
114 u_char pad;
115 #else
116 u_char pad;
117 u_char suffix; /* last character of new code */
118 u_int16_t prefix; /* preceding code */
119 #endif
120 } hs;
121 } f;
122 u_int16_t codem1; /* output of hash table -1 */
123 u_int16_t cptr; /* map code to hash table entry */
124 } dict[1];
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,
135 int decomp);
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,
147 struct mbuf **dmpp);
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 */
177 #define LAST 255
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
196 static void
197 bsd_clear(struct bsd_db *db)
199 db->clear_count++;
200 db->max_ent = FIRST-1;
201 db->n_bits = BSD_INIT_BITS;
202 db->ratio = 0;
203 db->bytes_out = 0;
204 db->in_count = 0;
205 db->checkpoint = CHECK_GAP;
209 * If the dictionary is full, then see if it is time to reset it.
211 * Compute the compression ratio using fixed-point arithmetic
212 * with 8 fractional bits.
214 * Since we have an infinite stream instead of a single file,
215 * watch only the local compression ratio.
217 * Since both peers must reset the dictionary at the same time even in
218 * the absence of CLEAR codes (while packets are incompressible), they
219 * must compute the same ratio.
221 * Return 1=output CLEAR
223 static int
224 bsd_check(struct bsd_db *db)
226 u_int new_ratio;
228 if (db->in_count >= db->checkpoint) {
229 /* age the ratio by limiting the size of the counts */
230 if (db->in_count >= RATIO_MAX
231 || db->bytes_out >= RATIO_MAX) {
232 db->in_count -= db->in_count/4;
233 db->bytes_out -= db->bytes_out/4;
236 db->checkpoint = db->in_count + CHECK_GAP;
238 if (db->max_ent >= db->maxmaxcode) {
239 /* Reset the dictionary only if the ratio is worse,
240 * or if it looks as if it has been poisoned
241 * by incompressible data.
243 * This does not overflow, because
244 * db->in_count <= RATIO_MAX.
246 new_ratio = db->in_count << RATIO_SCALE_LOG;
247 if (db->bytes_out != 0)
248 new_ratio /= db->bytes_out;
250 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
251 bsd_clear(db);
252 return 1;
254 db->ratio = new_ratio;
257 return 0;
261 * Return statistics.
263 static void
264 bsd_comp_stats(void *state, struct compstat *stats)
266 struct bsd_db *db = (struct bsd_db *) state;
267 u_int out;
269 stats->unc_bytes = db->uncomp_bytes;
270 stats->unc_packets = db->uncomp_count;
271 stats->comp_bytes = db->comp_bytes;
272 stats->comp_packets = db->comp_count;
273 stats->inc_bytes = db->incomp_bytes;
274 stats->inc_packets = db->incomp_count;
275 stats->ratio = db->in_count;
276 out = db->bytes_out;
277 if (stats->ratio <= 0x7fffff)
278 stats->ratio <<= 8;
279 else
280 out >>= 8;
281 if (out != 0)
282 stats->ratio /= out;
286 * Reset state, as on a CCP ResetReq.
288 static void
289 bsd_reset(void *state)
291 struct bsd_db *db = (struct bsd_db *) state;
293 db->seqno = 0;
294 bsd_clear(db);
295 db->clear_count = 0;
299 * Allocate space for a (de) compressor.
301 static void *
302 bsd_alloc(u_char *options, int opt_len, int decomp)
304 int bits;
305 u_int newlen, hsize, hshift, maxmaxcode;
306 struct bsd_db *db;
308 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
309 || options[1] != CILEN_BSD_COMPRESS
310 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
311 return NULL;
312 bits = BSD_NBITS(options[2]);
313 switch (bits) {
314 case 9: /* needs 82152 for both directions */
315 case 10: /* needs 84144 */
316 case 11: /* needs 88240 */
317 case 12: /* needs 96432 */
318 hsize = 5003;
319 hshift = 4;
320 break;
321 case 13: /* needs 176784 */
322 hsize = 9001;
323 hshift = 5;
324 break;
325 case 14: /* needs 353744 */
326 hsize = 18013;
327 hshift = 6;
328 break;
329 case 15: /* needs 691440 */
330 hsize = 35023;
331 hshift = 7;
332 break;
333 case 16: /* needs 1366160--far too much, */
334 /* hsize = 69001; */ /* and 69001 is too big for cptr */
335 /* hshift = 8; */ /* in struct bsd_db */
336 /* break; */
337 default:
338 return NULL;
341 maxmaxcode = MAXCODE(bits);
342 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
343 MALLOC(db, struct bsd_db *, newlen, M_DEVBUF, M_WAITOK);
344 bzero(db, sizeof(*db) - sizeof(db->dict));
346 if (!decomp) {
347 db->lens = NULL;
348 } else {
349 MALLOC(db->lens, u_int16_t *, (maxmaxcode+1) * sizeof(db->lens[0]),
350 M_DEVBUF, M_WAITOK);
353 db->totlen = newlen;
354 db->hsize = hsize;
355 db->hshift = hshift;
356 db->maxmaxcode = maxmaxcode;
357 db->maxbits = bits;
359 return (void *) db;
362 static void
363 bsd_free(void *state)
365 struct bsd_db *db = (struct bsd_db *) state;
367 if (db->lens)
368 kfree(db->lens, M_DEVBUF);
369 kfree(db, M_DEVBUF);
372 static void *
373 bsd_comp_alloc(u_char *options, int opt_len)
375 return bsd_alloc(options, opt_len, 0);
378 static void *
379 bsd_decomp_alloc(u_char *options, int opt_len)
381 return bsd_alloc(options, opt_len, 1);
385 * Initialize the database.
387 static int
388 bsd_init(struct bsd_db *db, u_char *options, int opt_len, int unit,
389 int hdrlen, int mru, int debug, int decomp)
391 int i;
393 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
394 || options[1] != CILEN_BSD_COMPRESS
395 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
396 || BSD_NBITS(options[2]) != db->maxbits
397 || (decomp && db->lens == NULL))
398 return 0;
400 if (decomp) {
401 i = LAST+1;
402 while (i != 0)
403 db->lens[--i] = 1;
405 i = db->hsize;
406 while (i != 0) {
407 db->dict[--i].codem1 = BADCODEM1;
408 db->dict[i].cptr = 0;
411 db->unit = unit;
412 db->hdrlen = hdrlen;
413 db->mru = mru;
414 #ifndef DEBUG
415 if (debug)
416 #endif
417 db->debug = 1;
419 bsd_reset(db);
421 return 1;
424 static int
425 bsd_comp_init(void *state, u_char *options, int opt_len, int unit,
426 int hdrlen, int debug)
428 return bsd_init((struct bsd_db *) state, options, opt_len,
429 unit, hdrlen, 0, debug, 0);
432 static int
433 bsd_decomp_init(void *state, u_char *options, int opt_len,
434 int unit, int hdrlen, int mru, int debug)
436 return bsd_init((struct bsd_db *) state, options, opt_len,
437 unit, hdrlen, mru, debug, 1);
442 * compress a packet
443 * One change from the BSD compress command is that when the
444 * code size expands, we do not output a bunch of padding.
445 * **mret - return compressed mbuf chain here
446 * *mp - from here
447 * slen - uncompressed length
448 * maxolen - max compressed length
450 * return new slen
453 bsd_compress(void *state, struct mbuf **mret, struct mbuf *mp,
454 int slen, int maxolen)
456 struct bsd_db *db = (struct bsd_db *) state;
457 int hshift = db->hshift;
458 u_int max_ent = db->max_ent;
459 u_int n_bits = db->n_bits;
460 u_int bitno = 32;
461 u_int32_t accm = 0, fcode;
462 struct bsd_dict *dictp;
463 u_char c;
464 int hval, disp, ent, ilen;
465 u_char *rptr, *wptr;
466 u_char *cp_end;
467 int olen;
468 struct mbuf *m;
470 #define PUTBYTE(v) { \
471 ++olen; \
472 if (wptr) { \
473 *wptr++ = (v); \
474 if (wptr >= cp_end) { \
475 m->m_len = wptr - mtod(m, u_char *); \
476 MGET(m->m_next, MB_DONTWAIT, MT_DATA); \
477 m = m->m_next; \
478 if (m) { \
479 m->m_len = 0; \
480 if (maxolen - olen > MLEN) \
481 MCLGET(m, MB_DONTWAIT); \
482 wptr = mtod(m, u_char *); \
483 cp_end = wptr + M_TRAILINGSPACE(m); \
484 } else \
485 wptr = NULL; \
490 #define OUTPUT(ent) { \
491 bitno -= n_bits; \
492 accm |= ((ent) << bitno); \
493 do { \
494 PUTBYTE(accm >> 24); \
495 accm <<= 8; \
496 bitno += 8; \
497 } while (bitno <= 24); \
501 * If the protocol is not in the range we're interested in,
502 * just return without compressing the packet. If it is,
503 * the protocol becomes the first byte to compress.
505 rptr = mtod(mp, u_char *);
506 ent = PPP_PROTOCOL(rptr);
507 if (ent < 0x21 || ent > 0xf9) {
508 *mret = NULL;
509 return slen;
512 /* Don't generate compressed packets which are larger than
513 the uncompressed packet. */
514 if (maxolen > slen)
515 maxolen = slen;
517 /* Allocate one mbuf to start with. */
518 MGET(m, MB_DONTWAIT, MT_DATA);
519 *mret = m;
520 if (m != NULL) {
521 m->m_len = 0;
522 if (maxolen + db->hdrlen > MLEN)
523 MCLGET(m, MB_DONTWAIT);
524 m->m_data += db->hdrlen;
525 wptr = mtod(m, u_char *);
526 cp_end = wptr + M_TRAILINGSPACE(m);
527 } else
528 wptr = cp_end = NULL;
531 * Copy the PPP header over, changing the protocol,
532 * and install the 2-byte packet sequence number.
534 if (wptr) {
535 *wptr++ = PPP_ADDRESS(rptr); /* assumes the ppp header is */
536 *wptr++ = PPP_CONTROL(rptr); /* all in one mbuf */
537 *wptr++ = 0; /* change the protocol */
538 *wptr++ = PPP_COMP;
539 *wptr++ = db->seqno >> 8;
540 *wptr++ = db->seqno;
542 ++db->seqno;
544 olen = 0;
545 rptr += PPP_HDRLEN;
546 slen = mp->m_len - PPP_HDRLEN;
547 ilen = slen + 1;
548 for (;;) {
549 if (slen <= 0) {
550 mp = mp->m_next;
551 if (!mp)
552 break;
553 rptr = mtod(mp, u_char *);
554 slen = mp->m_len;
555 if (!slen)
556 continue; /* handle 0-length buffers */
557 ilen += slen;
560 slen--;
561 c = *rptr++;
562 fcode = BSD_KEY(ent, c);
563 hval = BSD_HASH(ent, c, hshift);
564 dictp = &db->dict[hval];
566 /* Validate and then check the entry. */
567 if (dictp->codem1 >= max_ent)
568 goto nomatch;
569 if (dictp->f.fcode == fcode) {
570 ent = dictp->codem1+1;
571 continue; /* found (prefix,suffix) */
574 /* continue probing until a match or invalid entry */
575 disp = (hval == 0) ? 1 : hval;
576 do {
577 hval += disp;
578 if (hval >= db->hsize)
579 hval -= db->hsize;
580 dictp = &db->dict[hval];
581 if (dictp->codem1 >= max_ent)
582 goto nomatch;
583 } while (dictp->f.fcode != fcode);
584 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
585 continue;
587 nomatch:
588 OUTPUT(ent); /* output the prefix */
590 /* code -> hashtable */
591 if (max_ent < db->maxmaxcode) {
592 struct bsd_dict *dictp2;
593 /* expand code size if needed */
594 if (max_ent >= MAXCODE(n_bits))
595 db->n_bits = ++n_bits;
597 /* Invalidate old hash table entry using
598 * this code, and then take it over.
600 dictp2 = &db->dict[max_ent+1];
601 if (db->dict[dictp2->cptr].codem1 == max_ent)
602 db->dict[dictp2->cptr].codem1 = BADCODEM1;
603 dictp2->cptr = hval;
604 dictp->codem1 = max_ent;
605 dictp->f.fcode = fcode;
607 db->max_ent = ++max_ent;
609 ent = c;
612 OUTPUT(ent); /* output the last code */
613 db->bytes_out += olen;
614 db->in_count += ilen;
615 if (bitno < 32)
616 ++db->bytes_out; /* count complete bytes */
618 if (bsd_check(db))
619 OUTPUT(CLEAR); /* do not count the CLEAR */
622 * Pad dribble bits of last code with ones.
623 * Do not emit a completely useless byte of ones.
625 if (bitno != 32)
626 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
628 if (m != NULL) {
629 m->m_len = wptr - mtod(m, u_char *);
630 m->m_next = NULL;
634 * Increase code size if we would have without the packet
635 * boundary and as the decompressor will.
637 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
638 db->n_bits++;
640 db->uncomp_bytes += ilen;
641 ++db->uncomp_count;
642 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen) {
643 /* throw away the compressed stuff if it is longer than uncompressed */
644 if (*mret != NULL) {
645 m_freem(*mret);
646 *mret = NULL;
648 ++db->incomp_count;
649 db->incomp_bytes += ilen;
650 } else {
651 ++db->comp_count;
652 db->comp_bytes += olen + BSD_OVHD;
655 return olen + PPP_HDRLEN + BSD_OVHD;
656 #undef OUTPUT
657 #undef PUTBYTE
662 * Update the "BSD Compress" dictionary on the receiver for
663 * incompressible data by pretending to compress the incoming data.
665 static void
666 bsd_incomp(void *state, struct mbuf *dmsg)
668 struct bsd_db *db = (struct bsd_db *) state;
669 u_int hshift = db->hshift;
670 u_int max_ent = db->max_ent;
671 u_int n_bits = db->n_bits;
672 struct bsd_dict *dictp;
673 u_int32_t fcode;
674 u_char c;
675 u_int32_t hval, disp;
676 int slen, ilen;
677 u_int bitno = 7;
678 u_char *rptr;
679 u_int ent;
682 * If the protocol is not in the range we're interested in,
683 * just return without looking at the packet. If it is,
684 * the protocol becomes the first byte to "compress".
686 rptr = mtod(dmsg, u_char *);
687 ent = PPP_PROTOCOL(rptr);
688 if (ent < 0x21 || ent > 0xf9)
689 return;
691 db->seqno++;
692 ilen = 1; /* count the protocol as 1 byte */
693 rptr += PPP_HDRLEN;
694 slen = dmsg->m_len - PPP_HDRLEN;
695 for (;;) {
696 if (slen <= 0) {
697 dmsg = dmsg->m_next;
698 if (!dmsg)
699 break;
700 rptr = mtod(dmsg, u_char *);
701 slen = dmsg->m_len;
702 continue;
704 ilen += slen;
706 do {
707 c = *rptr++;
708 fcode = BSD_KEY(ent, c);
709 hval = BSD_HASH(ent, c, hshift);
710 dictp = &db->dict[hval];
712 /* validate and then check the entry */
713 if (dictp->codem1 >= max_ent)
714 goto nomatch;
715 if (dictp->f.fcode == fcode) {
716 ent = dictp->codem1+1;
717 continue; /* found (prefix,suffix) */
720 /* continue probing until a match or invalid entry */
721 disp = (hval == 0) ? 1 : hval;
722 do {
723 hval += disp;
724 if (hval >= db->hsize)
725 hval -= db->hsize;
726 dictp = &db->dict[hval];
727 if (dictp->codem1 >= max_ent)
728 goto nomatch;
729 } while (dictp->f.fcode != fcode);
730 ent = dictp->codem1+1;
731 continue; /* finally found (prefix,suffix) */
733 nomatch: /* output (count) the prefix */
734 bitno += n_bits;
736 /* code -> hashtable */
737 if (max_ent < db->maxmaxcode) {
738 struct bsd_dict *dictp2;
739 /* expand code size if needed */
740 if (max_ent >= MAXCODE(n_bits))
741 db->n_bits = ++n_bits;
743 /* Invalidate previous hash table entry
744 * assigned this code, and then take it over.
746 dictp2 = &db->dict[max_ent+1];
747 if (db->dict[dictp2->cptr].codem1 == max_ent)
748 db->dict[dictp2->cptr].codem1 = BADCODEM1;
749 dictp2->cptr = hval;
750 dictp->codem1 = max_ent;
751 dictp->f.fcode = fcode;
753 db->max_ent = ++max_ent;
754 db->lens[max_ent] = db->lens[ent]+1;
756 ent = c;
757 } while (--slen != 0);
759 bitno += n_bits; /* output (count) the last code */
760 db->bytes_out += bitno/8;
761 db->in_count += ilen;
762 bsd_check(db);
764 ++db->incomp_count;
765 db->incomp_bytes += ilen;
766 ++db->uncomp_count;
767 db->uncomp_bytes += ilen;
769 /* Increase code size if we would have without the packet
770 * boundary and as the decompressor will.
772 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
773 db->n_bits++;
778 * Decompress "BSD Compress".
780 * Because of patent problems, we return DECOMP_ERROR for errors
781 * found by inspecting the input data and for system problems, but
782 * DECOMP_FATALERROR for any errors which could possibly be said to
783 * be being detected "after" decompression. For DECOMP_ERROR,
784 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
785 * infringing a patent of Motorola's if we do, so we take CCP down
786 * instead.
788 * Given that the frame has the correct sequence number and a good FCS,
789 * errors such as invalid codes in the input most likely indicate a
790 * bug, so we return DECOMP_FATALERROR for them in order to turn off
791 * compression, even though they are detected by inspecting the input.
794 bsd_decompress(void *state, struct mbuf *cmp, struct mbuf **dmpp)
796 struct bsd_db *db = (struct bsd_db *) state;
797 u_int max_ent = db->max_ent;
798 u_int32_t accm = 0;
799 u_int bitno = 32; /* 1st valid bit in accm */
800 u_int n_bits = db->n_bits;
801 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
802 struct bsd_dict *dictp;
803 int explen, i, seq, len;
804 u_int incode, oldcode, finchar;
805 u_char *p, *rptr, *wptr;
806 struct mbuf *m, *dmp, *mret;
807 int adrs, ctrl, ilen;
808 int space, codelen, extra;
811 * Save the address/control from the PPP header
812 * and then get the sequence number.
814 *dmpp = NULL;
815 rptr = mtod(cmp, u_char *);
816 adrs = PPP_ADDRESS(rptr);
817 ctrl = PPP_CONTROL(rptr);
818 rptr += PPP_HDRLEN;
819 len = cmp->m_len - PPP_HDRLEN;
820 seq = 0;
821 for (i = 0; i < 2; ++i) {
822 while (len <= 0) {
823 cmp = cmp->m_next;
824 if (cmp == NULL)
825 return DECOMP_ERROR;
826 rptr = mtod(cmp, u_char *);
827 len = cmp->m_len;
829 seq = (seq << 8) + *rptr++;
830 --len;
834 * Check the sequence number and give up if it differs from
835 * the value we're expecting.
837 if (seq != db->seqno) {
838 if (db->debug)
839 kprintf("bsd_decomp%d: bad sequence # %d, expected %d\n",
840 db->unit, seq, db->seqno - 1);
841 return DECOMP_ERROR;
843 ++db->seqno;
846 * Allocate one mbuf to start with.
848 MGETHDR(dmp, MB_DONTWAIT, MT_DATA);
849 if (dmp == NULL)
850 return DECOMP_ERROR;
851 mret = dmp;
852 dmp->m_len = 0;
853 dmp->m_next = NULL;
854 MCLGET(dmp, MB_DONTWAIT);
855 dmp->m_data += db->hdrlen;
856 wptr = mtod(dmp, u_char *);
857 space = M_TRAILINGSPACE(dmp) - PPP_HDRLEN + 1;
860 * Fill in the ppp header, but not the last byte of the protocol
861 * (that comes from the decompressed data).
863 wptr[0] = adrs;
864 wptr[1] = ctrl;
865 wptr[2] = 0;
866 wptr += PPP_HDRLEN - 1;
868 ilen = len;
869 oldcode = CLEAR;
870 explen = 0;
871 for (;;) {
872 if (len == 0) {
873 cmp = cmp->m_next;
874 if (!cmp) /* quit at end of message */
875 break;
876 rptr = mtod(cmp, u_char *);
877 len = cmp->m_len;
878 ilen += len;
879 continue; /* handle 0-length buffers */
883 * Accumulate bytes until we have a complete code.
884 * Then get the next code, relying on the 32-bit,
885 * unsigned accm to mask the result.
887 bitno -= 8;
888 accm |= *rptr++ << bitno;
889 --len;
890 if (tgtbitno < bitno)
891 continue;
892 incode = accm >> tgtbitno;
893 accm <<= n_bits;
894 bitno += n_bits;
896 if (incode == CLEAR) {
898 * The dictionary must only be cleared at
899 * the end of a packet. But there could be an
900 * empty mbuf at the end.
902 if (len > 0 || cmp->m_next != NULL) {
903 while ((cmp = cmp->m_next) != NULL)
904 len += cmp->m_len;
905 if (len > 0) {
906 m_freem(mret);
907 if (db->debug)
908 kprintf("bsd_decomp%d: bad CLEAR\n", db->unit);
909 return DECOMP_FATALERROR; /* probably a bug */
912 bsd_clear(db);
913 explen = ilen = 0;
914 break;
917 if (incode > max_ent + 2 || incode > db->maxmaxcode
918 || (incode > max_ent && oldcode == CLEAR)) {
919 m_freem(mret);
920 if (db->debug) {
921 kprintf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
922 db->unit, incode, oldcode);
923 kprintf("max_ent=0x%x explen=%d seqno=%d\n",
924 max_ent, explen, db->seqno);
926 return DECOMP_FATALERROR; /* probably a bug */
929 /* Special case for KwKwK string. */
930 if (incode > max_ent) {
931 finchar = oldcode;
932 extra = 1;
933 } else {
934 finchar = incode;
935 extra = 0;
938 codelen = db->lens[finchar];
939 explen += codelen + extra;
940 if (explen > db->mru + 1) {
941 m_freem(mret);
942 if (db->debug) {
943 kprintf("bsd_decomp%d: ran out of mru\n", db->unit);
944 #ifdef DEBUG
945 while ((cmp = cmp->m_next) != NULL)
946 len += cmp->m_len;
947 kprintf(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
948 len, finchar, codelen, explen);
949 #endif
951 return DECOMP_FATALERROR;
955 * For simplicity, the decoded characters go in a single mbuf,
956 * so we allocate a single extra cluster mbuf if necessary.
958 if ((space -= codelen + extra) < 0) {
959 dmp->m_len = wptr - mtod(dmp, u_char *);
960 MGET(m, MB_DONTWAIT, MT_DATA);
961 if (m == NULL) {
962 m_freem(mret);
963 return DECOMP_ERROR;
965 m->m_len = 0;
966 m->m_next = NULL;
967 dmp->m_next = m;
968 MCLGET(m, MB_DONTWAIT);
969 space = M_TRAILINGSPACE(m) - (codelen + extra);
970 if (space < 0) {
971 /* now that's what I call *compression*. */
972 m_freem(mret);
973 return DECOMP_ERROR;
975 dmp = m;
976 wptr = mtod(dmp, u_char *);
980 * Decode this code and install it in the decompressed buffer.
982 p = (wptr += codelen);
983 while (finchar > LAST) {
984 dictp = &db->dict[db->dict[finchar].cptr];
985 #ifdef DEBUG
986 if (--codelen <= 0 || dictp->codem1 != finchar-1)
987 goto bad;
988 #endif
989 *--p = dictp->f.hs.suffix;
990 finchar = dictp->f.hs.prefix;
992 *--p = finchar;
994 #ifdef DEBUG
995 if (--codelen != 0)
996 kprintf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
997 db->unit, codelen, incode, max_ent);
998 #endif
1000 if (extra) /* the KwKwK case again */
1001 *wptr++ = finchar;
1004 * If not first code in a packet, and
1005 * if not out of code space, then allocate a new code.
1007 * Keep the hash table correct so it can be used
1008 * with uncompressed packets.
1010 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1011 struct bsd_dict *dictp2;
1012 u_int32_t fcode;
1013 u_int32_t hval, disp;
1015 fcode = BSD_KEY(oldcode,finchar);
1016 hval = BSD_HASH(oldcode,finchar,db->hshift);
1017 dictp = &db->dict[hval];
1019 /* look for a free hash table entry */
1020 if (dictp->codem1 < max_ent) {
1021 disp = (hval == 0) ? 1 : hval;
1022 do {
1023 hval += disp;
1024 if (hval >= db->hsize)
1025 hval -= db->hsize;
1026 dictp = &db->dict[hval];
1027 } while (dictp->codem1 < max_ent);
1031 * Invalidate previous hash table entry
1032 * assigned this code, and then take it over
1034 dictp2 = &db->dict[max_ent+1];
1035 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1036 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1038 dictp2->cptr = hval;
1039 dictp->codem1 = max_ent;
1040 dictp->f.fcode = fcode;
1042 db->max_ent = ++max_ent;
1043 db->lens[max_ent] = db->lens[oldcode]+1;
1045 /* Expand code size if needed. */
1046 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1047 db->n_bits = ++n_bits;
1048 tgtbitno = 32-n_bits;
1051 oldcode = incode;
1053 dmp->m_len = wptr - mtod(dmp, u_char *);
1056 * Keep the checkpoint right so that incompressible packets
1057 * clear the dictionary at the right times.
1059 db->bytes_out += ilen;
1060 db->in_count += explen;
1061 if (bsd_check(db) && db->debug) {
1062 kprintf("bsd_decomp%d: peer should have cleared dictionary\n",
1063 db->unit);
1066 ++db->comp_count;
1067 db->comp_bytes += ilen + BSD_OVHD;
1068 ++db->uncomp_count;
1069 db->uncomp_bytes += explen;
1071 *dmpp = mret;
1072 return DECOMP_OK;
1074 #ifdef DEBUG
1075 bad:
1076 if (codelen <= 0) {
1077 kprintf("bsd_decomp%d: fell off end of chain ", db->unit);
1078 kprintf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1079 incode, finchar, db->dict[finchar].cptr, max_ent);
1080 } else if (dictp->codem1 != finchar-1) {
1081 kprintf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1082 db->unit, incode, finchar);
1083 kprintf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
1084 db->dict[finchar].cptr, dictp->codem1);
1086 m_freem(mret);
1087 return DECOMP_FATALERROR;
1088 #endif /* DEBUG */
1090 #endif /* DO_BSD_COMPRESS */