CPU: Wrong CPU Load %.
[tomato.git] / release / src / router / ppp / pppdump / bsd-comp.c
blob276523838334ded22851aff20c94d15cd82d6339
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 * $Id: bsd-comp.c,v 1.1.1.4 2003/10/14 08:09:53 sparq Exp $
44 #include <sys/types.h>
45 #include <stddef.h>
46 #include <stdlib.h>
47 #include "ppp_defs.h"
48 #include "ppp-comp.h"
50 #if DO_BSD_COMPRESS
53 * PPP "BSD compress" compression
54 * The differences between this compression and the classic BSD LZW
55 * source are obvious from the requirement that the classic code worked
56 * with files while this handles arbitrarily long streams that
57 * are broken into packets. They are:
59 * When the code size expands, a block of junk is not emitted by
60 * the compressor and not expected by the decompressor.
62 * New codes are not necessarily assigned every time an old
63 * code is output by the compressor. This is because a packet
64 * end forces a code to be emitted, but does not imply that a
65 * new sequence has been seen.
67 * The compression ratio is checked at the first end of a packet
68 * after the appropriate gap. Besides simplifying and speeding
69 * things up, this makes it more likely that the transmitter
70 * and receiver will agree when the dictionary is cleared when
71 * compression is not going well.
75 * A dictionary for doing BSD compress.
77 struct bsd_db {
78 int totlen; /* length of this structure */
79 u_int hsize; /* size of the hash table */
80 u_char hshift; /* used in hash function */
81 u_char n_bits; /* current bits/code */
82 u_char maxbits;
83 u_char debug;
84 u_char unit;
85 u_short seqno; /* sequence number of next packet */
86 u_int hdrlen; /* header length to preallocate */
87 u_int mru;
88 u_int maxmaxcode; /* largest valid code */
89 u_int max_ent; /* largest code in use */
90 u_int in_count; /* uncompressed bytes, aged */
91 u_int bytes_out; /* compressed bytes, aged */
92 u_int ratio; /* recent compression ratio */
93 u_int checkpoint; /* when to next check the ratio */
94 u_int clear_count; /* times dictionary cleared */
95 u_int incomp_count; /* incompressible packets */
96 u_int incomp_bytes; /* incompressible bytes */
97 u_int uncomp_count; /* uncompressed packets */
98 u_int uncomp_bytes; /* uncompressed bytes */
99 u_int comp_count; /* compressed packets */
100 u_int comp_bytes; /* compressed bytes */
101 u_short *lens; /* array of lengths of codes */
102 struct bsd_dict {
103 union { /* hash value */
104 u_int32_t fcode;
105 struct {
106 #ifdef BSD_LITTLE_ENDIAN
107 u_short prefix; /* preceding code */
108 u_char suffix; /* last character of new code */
109 u_char pad;
110 #else
111 u_char pad;
112 u_char suffix; /* last character of new code */
113 u_short prefix; /* preceding code */
114 #endif
115 } hs;
116 } f;
117 u_short codem1; /* output of hash table -1 */
118 u_short cptr; /* map code to hash table entry */
119 } dict[1];
122 #define BSD_OVHD 2 /* BSD compress overhead/packet */
123 #define BSD_INIT_BITS BSD_MIN_BITS
125 static void *bsd_decomp_alloc __P((u_char *options, int opt_len));
126 static void bsd_free __P((void *state));
127 static int bsd_decomp_init __P((void *state, u_char *options, int opt_len,
128 int unit, int hdrlen, int mru, int debug));
129 static void bsd_incomp __P((void *state, u_char *dmsg, int len));
130 static int bsd_decompress __P((void *state, u_char *cmp, int inlen,
131 u_char *dmp, int *outlen));
132 static void bsd_reset __P((void *state));
133 static void bsd_comp_stats __P((void *state, struct compstat *stats));
136 * Exported procedures.
138 struct compressor ppp_bsd_compress = {
139 CI_BSD_COMPRESS, /* compress_proto */
140 bsd_decomp_alloc, /* decomp_alloc */
141 bsd_free, /* decomp_free */
142 bsd_decomp_init, /* decomp_init */
143 bsd_reset, /* decomp_reset */
144 bsd_decompress, /* decompress */
145 bsd_incomp, /* incomp */
146 bsd_comp_stats, /* decomp_stat */
150 * the next two codes should not be changed lightly, as they must not
151 * lie within the contiguous general code space.
153 #define CLEAR 256 /* table clear output code */
154 #define FIRST 257 /* first free entry */
155 #define LAST 255
157 #define MAXCODE(b) ((1 << (b)) - 1)
158 #define BADCODEM1 MAXCODE(BSD_MAX_BITS)
160 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
161 ^ (u_int32_t)(prefix))
162 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
163 + (u_int32_t)(prefix))
165 #define CHECK_GAP 10000 /* Ratio check interval */
167 #define RATIO_SCALE_LOG 8
168 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
169 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
172 * clear the dictionary
174 static void
175 bsd_clear(db)
176 struct bsd_db *db;
178 db->clear_count++;
179 db->max_ent = FIRST-1;
180 db->n_bits = BSD_INIT_BITS;
181 db->ratio = 0;
182 db->bytes_out = 0;
183 db->in_count = 0;
184 db->checkpoint = CHECK_GAP;
188 * If the dictionary is full, then see if it is time to reset it.
190 * Compute the compression ratio using fixed-point arithmetic
191 * with 8 fractional bits.
193 * Since we have an infinite stream instead of a single file,
194 * watch only the local compression ratio.
196 * Since both peers must reset the dictionary at the same time even in
197 * the absence of CLEAR codes (while packets are incompressible), they
198 * must compute the same ratio.
200 static int /* 1=output CLEAR */
201 bsd_check(db)
202 struct bsd_db *db;
204 u_int new_ratio;
206 if (db->in_count >= db->checkpoint) {
207 /* age the ratio by limiting the size of the counts */
208 if (db->in_count >= RATIO_MAX
209 || db->bytes_out >= RATIO_MAX) {
210 db->in_count -= db->in_count/4;
211 db->bytes_out -= db->bytes_out/4;
214 db->checkpoint = db->in_count + CHECK_GAP;
216 if (db->max_ent >= db->maxmaxcode) {
217 /* Reset the dictionary only if the ratio is worse,
218 * or if it looks as if it has been poisoned
219 * by incompressible data.
221 * This does not overflow, because
222 * db->in_count <= RATIO_MAX.
224 new_ratio = db->in_count << RATIO_SCALE_LOG;
225 if (db->bytes_out != 0)
226 new_ratio /= db->bytes_out;
228 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
229 bsd_clear(db);
230 return 1;
232 db->ratio = new_ratio;
235 return 0;
239 * Return statistics.
241 static void
242 bsd_comp_stats(state, stats)
243 void *state;
244 struct compstat *stats;
246 struct bsd_db *db = (struct bsd_db *) state;
247 u_int out;
249 stats->unc_bytes = db->uncomp_bytes;
250 stats->unc_packets = db->uncomp_count;
251 stats->comp_bytes = db->comp_bytes;
252 stats->comp_packets = db->comp_count;
253 stats->inc_bytes = db->incomp_bytes;
254 stats->inc_packets = db->incomp_count;
255 stats->ratio = db->in_count;
256 out = db->bytes_out;
257 if (stats->ratio <= 0x7fffff)
258 stats->ratio <<= 8;
259 else
260 out >>= 8;
261 if (out != 0)
262 stats->ratio /= out;
266 * Reset state, as on a CCP ResetReq.
268 static void
269 bsd_reset(state)
270 void *state;
272 struct bsd_db *db = (struct bsd_db *) state;
274 db->seqno = 0;
275 bsd_clear(db);
276 db->clear_count = 0;
280 * Allocate space for a (de) compressor.
282 static void *
283 bsd_alloc(options, opt_len, decomp)
284 u_char *options;
285 int opt_len, decomp;
287 int bits;
288 u_int newlen, hsize, hshift, maxmaxcode;
289 struct bsd_db *db;
291 if (opt_len != 3 || options[0] != CI_BSD_COMPRESS || options[1] != 3
292 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
293 return NULL;
295 bits = BSD_NBITS(options[2]);
296 switch (bits) {
297 case 9: /* needs 82152 for both directions */
298 case 10: /* needs 84144 */
299 case 11: /* needs 88240 */
300 case 12: /* needs 96432 */
301 hsize = 5003;
302 hshift = 4;
303 break;
304 case 13: /* needs 176784 */
305 hsize = 9001;
306 hshift = 5;
307 break;
308 case 14: /* needs 353744 */
309 hsize = 18013;
310 hshift = 6;
311 break;
312 case 15: /* needs 691440 */
313 hsize = 35023;
314 hshift = 7;
315 break;
316 case 16: /* needs 1366160--far too much, */
317 /* hsize = 69001; */ /* and 69001 is too big for cptr */
318 /* hshift = 8; */ /* in struct bsd_db */
319 /* break; */
320 default:
321 return NULL;
324 maxmaxcode = MAXCODE(bits);
325 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
326 db = (struct bsd_db *) malloc(newlen);
327 if (!db)
328 return NULL;
329 memset(db, 0, sizeof(*db) - sizeof(db->dict));
331 if (!decomp) {
332 db->lens = NULL;
333 } else {
334 db->lens = (u_short *) malloc((maxmaxcode+1) * sizeof(db->lens[0]));
335 if (!db->lens) {
336 free(db);
337 return NULL;
341 db->totlen = newlen;
342 db->hsize = hsize;
343 db->hshift = hshift;
344 db->maxmaxcode = maxmaxcode;
345 db->maxbits = bits;
347 return (void *) db;
350 static void
351 bsd_free(state)
352 void *state;
354 struct bsd_db *db = (struct bsd_db *) state;
356 if (db->lens)
357 free(db->lens);
358 free(db);
361 static void *
362 bsd_decomp_alloc(options, opt_len)
363 u_char *options;
364 int opt_len;
366 return bsd_alloc(options, opt_len, 1);
370 * Initialize the database.
372 static int
373 bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp)
374 struct bsd_db *db;
375 u_char *options;
376 int opt_len, unit, hdrlen, mru, debug, decomp;
378 int i;
380 if (opt_len < CILEN_BSD_COMPRESS
381 || options[0] != CI_BSD_COMPRESS || options[1] != CILEN_BSD_COMPRESS
382 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
383 || BSD_NBITS(options[2]) != db->maxbits
384 || decomp && db->lens == NULL)
385 return 0;
387 if (decomp) {
388 i = LAST+1;
389 while (i != 0)
390 db->lens[--i] = 1;
392 i = db->hsize;
393 while (i != 0) {
394 db->dict[--i].codem1 = BADCODEM1;
395 db->dict[i].cptr = 0;
398 db->unit = unit;
399 db->hdrlen = hdrlen;
400 db->mru = mru;
401 if (debug)
402 db->debug = 1;
404 bsd_reset(db);
406 return 1;
409 static int
410 bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
411 void *state;
412 u_char *options;
413 int opt_len, unit, hdrlen, mru, debug;
415 return bsd_init((struct bsd_db *) state, options, opt_len,
416 unit, hdrlen, mru, debug, 1);
421 * Update the "BSD Compress" dictionary on the receiver for
422 * incompressible data by pretending to compress the incoming data.
424 static void
425 bsd_incomp(state, dmsg, mlen)
426 void *state;
427 u_char *dmsg;
428 int mlen;
430 struct bsd_db *db = (struct bsd_db *) state;
431 u_int hshift = db->hshift;
432 u_int max_ent = db->max_ent;
433 u_int n_bits = db->n_bits;
434 struct bsd_dict *dictp;
435 u_int32_t fcode;
436 u_char c;
437 long hval, disp;
438 int slen, ilen;
439 u_int bitno = 7;
440 u_char *rptr;
441 u_int ent;
443 rptr = dmsg;
444 ent = rptr[0]; /* get the protocol */
445 if (ent == 0) {
446 ++rptr;
447 --mlen;
448 ent = rptr[0];
450 if ((ent & 1) == 0 || ent < 0x21 || ent > 0xf9)
451 return;
453 db->seqno++;
454 ilen = 1; /* count the protocol as 1 byte */
455 ++rptr;
456 slen = dmsg + mlen - rptr;
457 ilen += slen;
458 for (; slen > 0; --slen) {
459 c = *rptr++;
460 fcode = BSD_KEY(ent, c);
461 hval = BSD_HASH(ent, c, hshift);
462 dictp = &db->dict[hval];
464 /* validate and then check the entry */
465 if (dictp->codem1 >= max_ent)
466 goto nomatch;
467 if (dictp->f.fcode == fcode) {
468 ent = dictp->codem1+1;
469 continue; /* found (prefix,suffix) */
472 /* continue probing until a match or invalid entry */
473 disp = (hval == 0) ? 1 : hval;
474 do {
475 hval += disp;
476 if (hval >= db->hsize)
477 hval -= db->hsize;
478 dictp = &db->dict[hval];
479 if (dictp->codem1 >= max_ent)
480 goto nomatch;
481 } while (dictp->f.fcode != fcode);
482 ent = dictp->codem1+1;
483 continue; /* finally found (prefix,suffix) */
485 nomatch: /* output (count) the prefix */
486 bitno += n_bits;
488 /* code -> hashtable */
489 if (max_ent < db->maxmaxcode) {
490 struct bsd_dict *dictp2;
491 /* expand code size if needed */
492 if (max_ent >= MAXCODE(n_bits))
493 db->n_bits = ++n_bits;
495 /* Invalidate previous hash table entry
496 * assigned this code, and then take it over.
498 dictp2 = &db->dict[max_ent+1];
499 if (db->dict[dictp2->cptr].codem1 == max_ent)
500 db->dict[dictp2->cptr].codem1 = BADCODEM1;
501 dictp2->cptr = hval;
502 dictp->codem1 = max_ent;
503 dictp->f.fcode = fcode;
505 db->max_ent = ++max_ent;
506 db->lens[max_ent] = db->lens[ent]+1;
508 ent = c;
510 bitno += n_bits; /* output (count) the last code */
511 db->bytes_out += bitno/8;
512 db->in_count += ilen;
513 (void)bsd_check(db);
515 ++db->incomp_count;
516 db->incomp_bytes += ilen;
517 ++db->uncomp_count;
518 db->uncomp_bytes += ilen;
520 /* Increase code size if we would have without the packet
521 * boundary and as the decompressor will.
523 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
524 db->n_bits++;
529 * Decompress "BSD Compress"
531 * Because of patent problems, we return DECOMP_ERROR for errors
532 * found by inspecting the input data and for system problems, but
533 * DECOMP_FATALERROR for any errors which could possibly be said to
534 * be being detected "after" decompression. For DECOMP_ERROR,
535 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
536 * infringing a patent of Motorola's if we do, so we take CCP down
537 * instead.
539 * Given that the frame has the correct sequence number and a good FCS,
540 * errors such as invalid codes in the input most likely indicate a
541 * bug, so we return DECOMP_FATALERROR for them in order to turn off
542 * compression, even though they are detected by inspecting the input.
544 static int
545 bsd_decompress(state, cmsg, inlen, dmp, outlenp)
546 void *state;
547 u_char *cmsg, *dmp;
548 int inlen, *outlenp;
550 struct bsd_db *db = (struct bsd_db *) state;
551 u_int max_ent = db->max_ent;
552 u_int32_t accm = 0;
553 u_int bitno = 32; /* 1st valid bit in accm */
554 u_int n_bits = db->n_bits;
555 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
556 struct bsd_dict *dictp;
557 int explen, i, seq, len;
558 u_int incode, oldcode, finchar;
559 u_char *p, *rptr, *wptr;
560 int ilen;
561 int dlen, space, codelen, extra;
563 rptr = cmsg;
564 if (*rptr == 0)
565 ++rptr;
566 ++rptr; /* skip protocol (assumed 0xfd) */
567 seq = (rptr[0] << 8) + rptr[1];
568 rptr += BSD_OVHD;
569 ilen = len = cmsg + inlen - rptr;
572 * Check the sequence number and give up if it is not what we expect.
574 if (seq != db->seqno++) {
575 if (db->debug)
576 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
577 db->unit, seq, db->seqno - 1);
578 return DECOMP_ERROR;
581 wptr = dmp + db->hdrlen;
583 oldcode = CLEAR;
584 explen = 0;
585 while (len > 0) {
587 * Accumulate bytes until we have a complete code.
588 * Then get the next code, relying on the 32-bit,
589 * unsigned accm to mask the result.
591 bitno -= 8;
592 accm |= *rptr++ << bitno;
593 --len;
594 if (tgtbitno < bitno)
595 continue;
596 incode = accm >> tgtbitno;
597 accm <<= n_bits;
598 bitno += n_bits;
600 if (incode == CLEAR) {
602 * The dictionary must only be cleared at
603 * the end of a packet. But there could be an
604 * empty message block at the end.
606 if (len > 0) {
607 if (db->debug)
608 printf("bsd_decomp%d: bad CLEAR\n", db->unit);
609 return DECOMP_FATALERROR;
611 bsd_clear(db);
612 explen = ilen = 0;
613 break;
616 if (incode > max_ent + 2 || incode > db->maxmaxcode
617 || incode > max_ent && oldcode == CLEAR) {
618 if (db->debug) {
619 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
620 db->unit, incode, oldcode);
621 printf("max_ent=0x%x dlen=%d seqno=%d\n",
622 max_ent, dlen, db->seqno);
624 return DECOMP_FATALERROR; /* probably a bug */
627 /* Special case for KwKwK string. */
628 if (incode > max_ent) {
629 finchar = oldcode;
630 extra = 1;
631 } else {
632 finchar = incode;
633 extra = 0;
636 codelen = db->lens[finchar];
637 explen += codelen + extra;
638 if (explen > db->mru + 1) {
639 if (db->debug)
640 printf("bsd_decomp%d: ran out of mru\n", db->unit);
641 return DECOMP_FATALERROR;
645 * Decode this code and install it in the decompressed buffer.
647 p = (wptr += codelen);
648 while (finchar > LAST) {
649 dictp = &db->dict[db->dict[finchar].cptr];
650 #ifdef DEBUG
651 --codelen;
652 if (codelen <= 0) {
653 printf("bsd_decomp%d: fell off end of chain ", db->unit);
654 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
655 incode, finchar, db->dict[finchar].cptr, max_ent);
656 return DECOMP_FATALERROR;
658 if (dictp->codem1 != finchar-1) {
659 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
660 db->unit, incode, finchar);
661 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
662 db->dict[finchar].cptr, dictp->codem1);
663 return DECOMP_FATALERROR;
665 #endif
666 *--p = dictp->f.hs.suffix;
667 finchar = dictp->f.hs.prefix;
669 *--p = finchar;
671 #ifdef DEBUG
672 if (--codelen != 0)
673 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
674 db->unit, codelen, incode, max_ent);
675 #endif
677 if (extra) /* the KwKwK case again */
678 *wptr++ = finchar;
681 * If not first code in a packet, and
682 * if not out of code space, then allocate a new code.
684 * Keep the hash table correct so it can be used
685 * with uncompressed packets.
687 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
688 struct bsd_dict *dictp2;
689 u_int32_t fcode;
690 int hval, disp;
692 fcode = BSD_KEY(oldcode,finchar);
693 hval = BSD_HASH(oldcode,finchar,db->hshift);
694 dictp = &db->dict[hval];
696 /* look for a free hash table entry */
697 if (dictp->codem1 < max_ent) {
698 disp = (hval == 0) ? 1 : hval;
699 do {
700 hval += disp;
701 if (hval >= db->hsize)
702 hval -= db->hsize;
703 dictp = &db->dict[hval];
704 } while (dictp->codem1 < max_ent);
708 * Invalidate previous hash table entry
709 * assigned this code, and then take it over
711 dictp2 = &db->dict[max_ent+1];
712 if (db->dict[dictp2->cptr].codem1 == max_ent) {
713 db->dict[dictp2->cptr].codem1 = BADCODEM1;
715 dictp2->cptr = hval;
716 dictp->codem1 = max_ent;
717 dictp->f.fcode = fcode;
719 db->max_ent = ++max_ent;
720 db->lens[max_ent] = db->lens[oldcode]+1;
722 /* Expand code size if needed. */
723 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
724 db->n_bits = ++n_bits;
725 tgtbitno = 32-n_bits;
728 oldcode = incode;
730 *outlenp = wptr - (dmp + db->hdrlen);
733 * Keep the checkpoint right so that incompressible packets
734 * clear the dictionary at the right times.
736 db->bytes_out += ilen;
737 db->in_count += explen;
738 if (bsd_check(db) && db->debug) {
739 printf("bsd_decomp%d: peer should have cleared dictionary\n",
740 db->unit);
743 ++db->comp_count;
744 db->comp_bytes += ilen + BSD_OVHD;
745 ++db->uncomp_count;
746 db->uncomp_bytes += explen;
748 return DECOMP_OK;
750 #endif /* DO_BSD_COMPRESS */