]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/bsd_comp.c
xnu-792.6.76.tar.gz
[apple/xnu.git] / bsd / net / bsd_comp.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
37839358
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
1c79356b 11 *
37839358
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/* Because this code is derived from the 4.3BSD compress source:
23 *
24 *
25 * Copyright (c) 1985, 1986 The Regents of the University of California.
26 * All rights reserved.
27 *
28 * This code is derived from software contributed to Berkeley by
29 * James A. Woods, derived from original work by Spencer Thomas
30 * and Joseph Orost.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61/*
62 * This version is for use with mbufs on BSD-derived systems.
63 *
64 */
65
66#include <sys/param.h>
67#include <sys/systm.h>
68#include <sys/malloc.h>
69#include <sys/mbuf.h>
70#include <net/ppp_defs.h>
71
72#define PACKETPTR struct mbuf *
73#include <net/ppp_comp.h>
74
75#if DO_BSD_COMPRESS
76/*
77 * PPP "BSD compress" compression
78 * The differences between this compression and the classic BSD LZW
79 * source are obvious from the requirement that the classic code worked
80 * with files while this handles arbitrarily long streams that
81 * are broken into packets. They are:
82 *
83 * When the code size expands, a block of junk is not emitted by
84 * the compressor and not expected by the decompressor.
85 *
86 * New codes are not necessarily assigned every time an old
87 * code is output by the compressor. This is because a packet
88 * end forces a code to be emitted, but does not imply that a
89 * new sequence has been seen.
90 *
91 * The compression ratio is checked at the first end of a packet
92 * after the appropriate gap. Besides simplifying and speeding
93 * things up, this makes it more likely that the transmitter
94 * and receiver will agree when the dictionary is cleared when
95 * compression is not going well.
96 */
97
98/*
99 * A dictionary for doing BSD compress.
100 */
101struct bsd_db {
102 int totlen; /* length of this structure */
103 u_int hsize; /* size of the hash table */
104 u_char hshift; /* used in hash function */
105 u_char n_bits; /* current bits/code */
106 u_char maxbits;
107 u_char debug;
108 u_char unit;
109 u_int16_t seqno; /* sequence # of next packet */
110 u_int hdrlen; /* header length to preallocate */
111 u_int mru;
112 u_int maxmaxcode; /* largest valid code */
113 u_int max_ent; /* largest code in use */
114 u_int in_count; /* uncompressed bytes, aged */
115 u_int bytes_out; /* compressed bytes, aged */
116 u_int ratio; /* recent compression ratio */
117 u_int checkpoint; /* when to next check the ratio */
118 u_int clear_count; /* times dictionary cleared */
119 u_int incomp_count; /* incompressible packets */
120 u_int incomp_bytes; /* incompressible bytes */
121 u_int uncomp_count; /* uncompressed packets */
122 u_int uncomp_bytes; /* uncompressed bytes */
123 u_int comp_count; /* compressed packets */
124 u_int comp_bytes; /* compressed bytes */
125 u_int16_t *lens; /* array of lengths of codes */
126 struct bsd_dict {
127 union { /* hash value */
128 u_int32_t fcode;
129 struct {
130#if BYTE_ORDER == LITTLE_ENDIAN
131 u_int16_t prefix; /* preceding code */
132 u_char suffix; /* last character of new code */
133 u_char pad;
134#else
135 u_char pad;
136 u_char suffix; /* last character of new code */
137 u_int16_t prefix; /* preceding code */
138#endif
139 } hs;
140 } f;
141 u_int16_t codem1; /* output of hash table -1 */
142 u_int16_t cptr; /* map code to hash table entry */
143 } dict[1];
144};
145
146#define BSD_OVHD 2 /* BSD compress overhead/packet */
147#define BSD_INIT_BITS BSD_MIN_BITS
148
91447636
A
149static void bsd_clear(struct bsd_db *db);
150static int bsd_check(struct bsd_db *db);
151static void *bsd_alloc(u_char *options, int opt_len, int decomp);
152static int bsd_init_comp_db(struct bsd_db *db, u_char *options,
153 int opt_len,
1c79356b 154 int unit, int hdrlen, int mru, int debug,
91447636
A
155 int decomp);
156static void *bsd_comp_alloc(u_char *options, int opt_len);
157static void *bsd_decomp_alloc(u_char *options, int opt_len);
158static void bsd_free(void *state);
159static int bsd_comp_init(void *state, u_char *options, int opt_len,
160 int unit, int hdrlen, int debug);
161static int bsd_decomp_init(void *state, u_char *options, int opt_len,
162 int unit, int hdrlen, int mru, int debug);
163static int bsd_compress(void *state, struct mbuf **mret,
164 struct mbuf *mp, int slen, int maxolen);
165static void bsd_incomp(void *state, struct mbuf *dmsg);
166static int bsd_decompress(void *state, struct mbuf *cmp,
167 struct mbuf **dmpp);
168static void bsd_reset(void *state);
169static void bsd_comp_stats(void *state, struct compstat *stats);
1c79356b
A
170
171/*
172 * Procedures exported to if_ppp.c.
173 */
174struct compressor ppp_bsd_compress = {
175 CI_BSD_COMPRESS, /* compress_proto */
176 bsd_comp_alloc, /* comp_alloc */
177 bsd_free, /* comp_free */
178 bsd_comp_init, /* comp_init */
179 bsd_reset, /* comp_reset */
180 bsd_compress, /* compress */
181 bsd_comp_stats, /* comp_stat */
182 bsd_decomp_alloc, /* decomp_alloc */
183 bsd_free, /* decomp_free */
184 bsd_decomp_init, /* decomp_init */
185 bsd_reset, /* decomp_reset */
186 bsd_decompress, /* decompress */
187 bsd_incomp, /* incomp */
188 bsd_comp_stats, /* decomp_stat */
189};
190
191/*
192 * the next two codes should not be changed lightly, as they must not
193 * lie within the contiguous general code space.
194 */
195#define CLEAR 256 /* table clear output code */
196#define FIRST 257 /* first free entry */
197#define LAST 255
198
199#define MAXCODE(b) ((1 << (b)) - 1)
200#define BADCODEM1 MAXCODE(BSD_MAX_BITS)
201
202#define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
203 ^ (u_int32_t)(prefix))
204#define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
205 + (u_int32_t)(prefix))
206
207#define CHECK_GAP 10000 /* Ratio check interval */
208
209#define RATIO_SCALE_LOG 8
210#define RATIO_SCALE (1<<RATIO_SCALE_LOG)
211#define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
212
213/*
214 * clear the dictionary
215 */
216static void
217bsd_clear(db)
218 struct bsd_db *db;
219{
220 db->clear_count++;
221 db->max_ent = FIRST-1;
222 db->n_bits = BSD_INIT_BITS;
223 db->ratio = 0;
224 db->bytes_out = 0;
225 db->in_count = 0;
226 db->checkpoint = CHECK_GAP;
227}
228
229/*
230 * If the dictionary is full, then see if it is time to reset it.
231 *
232 * Compute the compression ratio using fixed-point arithmetic
233 * with 8 fractional bits.
234 *
235 * Since we have an infinite stream instead of a single file,
236 * watch only the local compression ratio.
237 *
238 * Since both peers must reset the dictionary at the same time even in
239 * the absence of CLEAR codes (while packets are incompressible), they
240 * must compute the same ratio.
241 */
242static int /* 1=output CLEAR */
243bsd_check(db)
244 struct bsd_db *db;
245{
246 u_int new_ratio;
247
248 if (db->in_count >= db->checkpoint) {
249 /* age the ratio by limiting the size of the counts */
250 if (db->in_count >= RATIO_MAX
251 || db->bytes_out >= RATIO_MAX) {
252 db->in_count -= db->in_count/4;
253 db->bytes_out -= db->bytes_out/4;
254 }
255
256 db->checkpoint = db->in_count + CHECK_GAP;
257
258 if (db->max_ent >= db->maxmaxcode) {
259 /* Reset the dictionary only if the ratio is worse,
260 * or if it looks as if it has been poisoned
261 * by incompressible data.
262 *
263 * This does not overflow, because
264 * db->in_count <= RATIO_MAX.
265 */
266 new_ratio = db->in_count << RATIO_SCALE_LOG;
267 if (db->bytes_out != 0)
268 new_ratio /= db->bytes_out;
269
270 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
271 bsd_clear(db);
272 return 1;
273 }
274 db->ratio = new_ratio;
275 }
276 }
277 return 0;
278}
279
280/*
281 * Return statistics.
282 */
283static void
284bsd_comp_stats(state, stats)
285 void *state;
286 struct compstat *stats;
287{
288 struct bsd_db *db = (struct bsd_db *) state;
289 u_int out;
290
291 stats->unc_bytes = db->uncomp_bytes;
292 stats->unc_packets = db->uncomp_count;
293 stats->comp_bytes = db->comp_bytes;
294 stats->comp_packets = db->comp_count;
295 stats->inc_bytes = db->incomp_bytes;
296 stats->inc_packets = db->incomp_count;
297 stats->ratio = db->in_count;
298 out = db->bytes_out;
299 if (stats->ratio <= 0x7fffff)
300 stats->ratio <<= 8;
301 else
302 out >>= 8;
303 if (out != 0)
304 stats->ratio /= out;
305}
306
307/*
308 * Reset state, as on a CCP ResetReq.
309 */
310static void
311bsd_reset(state)
312 void *state;
313{
314 struct bsd_db *db = (struct bsd_db *) state;
315
316 db->seqno = 0;
317 bsd_clear(db);
318 db->clear_count = 0;
319}
320
321/*
322 * Allocate space for a (de) compressor.
323 */
324static void *
325bsd_alloc(options, opt_len, decomp)
326 u_char *options;
327 int opt_len, decomp;
328{
329 int bits;
330 u_int newlen, hsize, hshift, maxmaxcode;
331 struct bsd_db *db;
332
333 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
334 || options[1] != CILEN_BSD_COMPRESS
335 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
336 return NULL;
337 bits = BSD_NBITS(options[2]);
338 switch (bits) {
339 case 9: /* needs 82152 for both directions */
340 case 10: /* needs 84144 */
341 case 11: /* needs 88240 */
342 case 12: /* needs 96432 */
343 hsize = 5003;
344 hshift = 4;
345 break;
346 case 13: /* needs 176784 */
347 hsize = 9001;
348 hshift = 5;
349 break;
350 case 14: /* needs 353744 */
351 hsize = 18013;
352 hshift = 6;
353 break;
354 case 15: /* needs 691440 */
355 hsize = 35023;
356 hshift = 7;
357 break;
358 case 16: /* needs 1366160--far too much, */
359 /* hsize = 69001; */ /* and 69001 is too big for cptr */
360 /* hshift = 8; */ /* in struct bsd_db */
361 /* break; */
362 default:
363 return NULL;
364 }
365
366 maxmaxcode = MAXCODE(bits);
367 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
368 MALLOC(db, struct bsd_db *, newlen, M_DEVBUF, M_NOWAIT);
369 if (!db)
370 return NULL;
371 bzero(db, sizeof(*db) - sizeof(db->dict));
372
373 if (!decomp) {
374 db->lens = NULL;
375 } else {
376 MALLOC(db->lens, u_int16_t *, (maxmaxcode+1) * sizeof(db->lens[0]),
377 M_DEVBUF, M_NOWAIT);
378 if (!db->lens) {
379 FREE(db, M_DEVBUF);
380 return NULL;
381 }
382 }
383
384 db->totlen = newlen;
385 db->hsize = hsize;
386 db->hshift = hshift;
387 db->maxmaxcode = maxmaxcode;
388 db->maxbits = bits;
389
390 return (void *) db;
391}
392
393static void
394bsd_free(state)
395 void *state;
396{
397 struct bsd_db *db = (struct bsd_db *) state;
398
399 if (db->lens)
400 FREE(db->lens, M_DEVBUF);
401 FREE(db, M_DEVBUF);
402}
403
404static void *
405bsd_comp_alloc(options, opt_len)
406 u_char *options;
407 int opt_len;
408{
409 return bsd_alloc(options, opt_len, 0);
410}
411
412static void *
413bsd_decomp_alloc(options, opt_len)
414 u_char *options;
415 int opt_len;
416{
417 return bsd_alloc(options, opt_len, 1);
418}
419
420/*
421 * Initialize the database.
422 */
423static int
424bsd_init_comp_db(db, options, opt_len, unit, hdrlen, mru, debug, decomp)
425 struct bsd_db *db;
426 u_char *options;
427 int opt_len, unit, hdrlen, mru, debug, decomp;
428{
429 int i;
430
431 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
432 || options[1] != CILEN_BSD_COMPRESS
433 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
434 || BSD_NBITS(options[2]) != db->maxbits
435 || (decomp && db->lens == NULL))
436 return 0;
437
438 if (decomp) {
439 i = LAST+1;
440 while (i != 0)
441 db->lens[--i] = 1;
442 }
443 i = db->hsize;
444 while (i != 0) {
445 db->dict[--i].codem1 = BADCODEM1;
446 db->dict[i].cptr = 0;
447 }
448
449 db->unit = unit;
450 db->hdrlen = hdrlen;
451 db->mru = mru;
452#ifndef DEBUG
453 if (debug)
454#endif
455 db->debug = 1;
456
457 bsd_reset(db);
458
459 return 1;
460}
461
462static int
463bsd_comp_init(state, options, opt_len, unit, hdrlen, debug)
464 void *state;
465 u_char *options;
466 int opt_len, unit, hdrlen, debug;
467{
468 return bsd_init_comp_db((struct bsd_db *) state, options, opt_len,
469 unit, hdrlen, 0, debug, 0);
470}
471
472static int
473bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
474 void *state;
475 u_char *options;
476 int opt_len, unit, hdrlen, mru, debug;
477{
478 return bsd_init_comp_db((struct bsd_db *) state, options, opt_len,
479 unit, hdrlen, mru, debug, 1);
480}
481
482
483/*
484 * compress a packet
485 * One change from the BSD compress command is that when the
486 * code size expands, we do not output a bunch of padding.
487 */
488int /* new slen */
489bsd_compress(state, mret, mp, slen, maxolen)
490 void *state;
491 struct mbuf **mret; /* return compressed mbuf chain here */
492 struct mbuf *mp; /* from here */
493 int slen; /* uncompressed length */
494 int maxolen; /* max compressed length */
495{
496 struct bsd_db *db = (struct bsd_db *) state;
497 int hshift = db->hshift;
498 u_int max_ent = db->max_ent;
499 u_int n_bits = db->n_bits;
500 u_int bitno = 32;
501 u_int32_t accm = 0, fcode;
502 struct bsd_dict *dictp;
503 u_char c;
504 int hval, disp, ent, ilen;
505 u_char *rptr, *wptr;
506 u_char *cp_end;
507 int olen;
508 struct mbuf *m;
509
510#define PUTBYTE(v) { \
511 ++olen; \
512 if (wptr) { \
513 *wptr++ = (v); \
514 if (wptr >= cp_end) { \
515 m->m_len = wptr - mtod(m, u_char *); \
516 MGET(m->m_next, M_DONTWAIT, MT_DATA); \
517 m = m->m_next; \
518 if (m) { \
519 m->m_len = 0; \
520 if (maxolen - olen > MLEN) \
521 MCLGET(m, M_DONTWAIT); \
522 wptr = mtod(m, u_char *); \
523 cp_end = wptr + M_TRAILINGSPACE(m); \
524 } else \
525 wptr = NULL; \
526 } \
527 } \
528}
529
530#define OUTPUT(ent) { \
531 bitno -= n_bits; \
532 accm |= ((ent) << bitno); \
533 do { \
534 PUTBYTE(accm >> 24); \
535 accm <<= 8; \
536 bitno += 8; \
537 } while (bitno <= 24); \
538}
539
540 /*
541 * If the protocol is not in the range we're interested in,
542 * just return without compressing the packet. If it is,
543 * the protocol becomes the first byte to compress.
544 */
545 rptr = mtod(mp, u_char *);
546 ent = PPP_PROTOCOL(rptr);
547 if (ent < 0x21 || ent > 0xf9) {
548 *mret = NULL;
549 return slen;
550 }
551
552 /* Don't generate compressed packets which are larger than
553 the uncompressed packet. */
554 if (maxolen > slen)
555 maxolen = slen;
556
557 /* Allocate one mbuf to start with. */
558 MGET(m, M_DONTWAIT, MT_DATA);
559 *mret = m;
560 if (m != NULL) {
561 m->m_len = 0;
562 if (maxolen + db->hdrlen > MLEN)
563 MCLGET(m, M_DONTWAIT);
564 m->m_data += db->hdrlen;
565 wptr = mtod(m, u_char *);
566 cp_end = wptr + M_TRAILINGSPACE(m);
567 } else
568 wptr = cp_end = NULL;
569
570 /*
571 * Copy the PPP header over, changing the protocol,
572 * and install the 2-byte packet sequence number.
573 */
574 if (wptr) {
575 *wptr++ = PPP_ADDRESS(rptr); /* assumes the ppp header is */
576 *wptr++ = PPP_CONTROL(rptr); /* all in one mbuf */
577 *wptr++ = 0; /* change the protocol */
578 *wptr++ = PPP_COMP;
579 *wptr++ = db->seqno >> 8;
580 *wptr++ = db->seqno;
581 }
582 ++db->seqno;
583
584 olen = 0;
585 rptr += PPP_HDRLEN;
586 slen = mp->m_len - PPP_HDRLEN;
587 ilen = slen + 1;
588 for (;;) {
589 if (slen <= 0) {
590 mp = mp->m_next;
591 if (!mp)
592 break;
593 rptr = mtod(mp, u_char *);
594 slen = mp->m_len;
595 if (!slen)
596 continue; /* handle 0-length buffers */
597 ilen += slen;
598 }
599
600 slen--;
601 c = *rptr++;
602 fcode = BSD_KEY(ent, c);
603 hval = BSD_HASH(ent, c, hshift);
604 dictp = &db->dict[hval];
605
606 /* Validate and then check the entry. */
607 if (dictp->codem1 >= max_ent)
608 goto nomatch;
609 if (dictp->f.fcode == fcode) {
610 ent = dictp->codem1+1;
611 continue; /* found (prefix,suffix) */
612 }
613
614 /* continue probing until a match or invalid entry */
615 disp = (hval == 0) ? 1 : hval;
616 do {
617 hval += disp;
618 if (hval >= db->hsize)
619 hval -= db->hsize;
620 dictp = &db->dict[hval];
621 if (dictp->codem1 >= max_ent)
622 goto nomatch;
623 } while (dictp->f.fcode != fcode);
624 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
625 continue;
626
627 nomatch:
628 OUTPUT(ent); /* output the prefix */
629
630 /* code -> hashtable */
631 if (max_ent < db->maxmaxcode) {
632 struct bsd_dict *dictp2;
633 /* expand code size if needed */
634 if (max_ent >= MAXCODE(n_bits))
635 db->n_bits = ++n_bits;
636
637 /* Invalidate old hash table entry using
638 * this code, and then take it over.
639 */
640 dictp2 = &db->dict[max_ent+1];
641 if (db->dict[dictp2->cptr].codem1 == max_ent)
642 db->dict[dictp2->cptr].codem1 = BADCODEM1;
643 dictp2->cptr = hval;
644 dictp->codem1 = max_ent;
645 dictp->f.fcode = fcode;
646
647 db->max_ent = ++max_ent;
648 }
649 ent = c;
650 }
651
652 OUTPUT(ent); /* output the last code */
653 db->bytes_out += olen;
654 db->in_count += ilen;
655 if (bitno < 32)
656 ++db->bytes_out; /* count complete bytes */
657
658 if (bsd_check(db))
659 OUTPUT(CLEAR); /* do not count the CLEAR */
660
661 /*
662 * Pad dribble bits of last code with ones.
663 * Do not emit a completely useless byte of ones.
664 */
665 if (bitno != 32)
666 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
667
668 if (m != NULL) {
669 m->m_len = wptr - mtod(m, u_char *);
670 m->m_next = NULL;
671 }
672
673 /*
674 * Increase code size if we would have without the packet
675 * boundary and as the decompressor will.
676 */
677 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
678 db->n_bits++;
679
680 db->uncomp_bytes += ilen;
681 ++db->uncomp_count;
682 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen) {
683 /* throw away the compressed stuff if it is longer than uncompressed */
684 if (*mret != NULL) {
685 m_freem(*mret);
686 *mret = NULL;
687 }
688 ++db->incomp_count;
689 db->incomp_bytes += ilen;
690 } else {
691 ++db->comp_count;
692 db->comp_bytes += olen + BSD_OVHD;
693 }
694
695 return olen + PPP_HDRLEN + BSD_OVHD;
696#undef OUTPUT
697#undef PUTBYTE
698}
699
700
701/*
702 * Update the "BSD Compress" dictionary on the receiver for
703 * incompressible data by pretending to compress the incoming data.
704 */
705static void
706bsd_incomp(state, dmsg)
707 void *state;
708 struct mbuf *dmsg;
709{
710 struct bsd_db *db = (struct bsd_db *) state;
711 u_int hshift = db->hshift;
712 u_int max_ent = db->max_ent;
713 u_int n_bits = db->n_bits;
714 struct bsd_dict *dictp;
715 u_int32_t fcode;
716 u_char c;
717 u_int32_t hval, disp;
718 int slen, ilen;
719 u_int bitno = 7;
720 u_char *rptr;
721 u_int ent;
722
723 /*
724 * If the protocol is not in the range we're interested in,
725 * just return without looking at the packet. If it is,
726 * the protocol becomes the first byte to "compress".
727 */
728 rptr = mtod(dmsg, u_char *);
729 ent = PPP_PROTOCOL(rptr);
730 if (ent < 0x21 || ent > 0xf9)
731 return;
732
733 db->seqno++;
734 ilen = 1; /* count the protocol as 1 byte */
735 rptr += PPP_HDRLEN;
736 slen = dmsg->m_len - PPP_HDRLEN;
737 for (;;) {
738 if (slen <= 0) {
739 dmsg = dmsg->m_next;
740 if (!dmsg)
741 break;
742 rptr = mtod(dmsg, u_char *);
743 slen = dmsg->m_len;
744 continue;
745 }
746 ilen += slen;
747
748 do {
749 c = *rptr++;
750 fcode = BSD_KEY(ent, c);
751 hval = BSD_HASH(ent, c, hshift);
752 dictp = &db->dict[hval];
753
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) */
760 }
761
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) */
774
775 nomatch: /* output (count) the prefix */
776 bitno += n_bits;
777
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;
784
785 /* Invalidate previous hash table entry
786 * assigned this code, and then take it over.
787 */
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;
794
795 db->max_ent = ++max_ent;
796 db->lens[max_ent] = db->lens[ent]+1;
797 }
798 ent = c;
799 } while (--slen != 0);
800 }
801 bitno += n_bits; /* output (count) the last code */
802 db->bytes_out += bitno/8;
803 db->in_count += ilen;
804 (void)bsd_check(db);
805
806 ++db->incomp_count;
807 db->incomp_bytes += ilen;
808 ++db->uncomp_count;
809 db->uncomp_bytes += ilen;
810
811 /* Increase code size if we would have without the packet
812 * boundary and as the decompressor will.
813 */
814 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
815 db->n_bits++;
816}
817
818
819/*
820 * Decompress "BSD Compress".
821 *
822 * Because of patent problems, we return DECOMP_ERROR for errors
823 * found by inspecting the input data and for system problems, but
824 * DECOMP_FATALERROR for any errors which could possibly be said to
825 * be being detected "after" decompression. For DECOMP_ERROR,
826 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
827 * infringing a patent of Motorola's if we do, so we take CCP down
828 * instead.
829 *
830 * Given that the frame has the correct sequence number and a good FCS,
831 * errors such as invalid codes in the input most likely indicate a
832 * bug, so we return DECOMP_FATALERROR for them in order to turn off
833 * compression, even though they are detected by inspecting the input.
834 */
835int
836bsd_decompress(state, cmp, dmpp)
837 void *state;
838 struct mbuf *cmp, **dmpp;
839{
840 struct bsd_db *db = (struct bsd_db *) state;
841 u_int max_ent = db->max_ent;
842 u_int32_t accm = 0;
843 u_int bitno = 32; /* 1st valid bit in accm */
844 u_int n_bits = db->n_bits;
845 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
846 struct bsd_dict *dictp;
847 int explen, i, seq, len;
848 u_int incode, oldcode, finchar;
849 u_char *p, *rptr, *wptr;
850 struct mbuf *m, *dmp, *mret;
851 int adrs, ctrl, ilen;
852 int space, codelen, extra;
853
854 /*
855 * Save the address/control from the PPP header
856 * and then get the sequence number.
857 */
858 *dmpp = NULL;
859 rptr = mtod(cmp, u_char *);
860 adrs = PPP_ADDRESS(rptr);
861 ctrl = PPP_CONTROL(rptr);
862 rptr += PPP_HDRLEN;
863 len = cmp->m_len - PPP_HDRLEN;
864 seq = 0;
865 for (i = 0; i < 2; ++i) {
866 while (len <= 0) {
867 cmp = cmp->m_next;
868 if (cmp == NULL)
869 return DECOMP_ERROR;
870 rptr = mtod(cmp, u_char *);
871 len = cmp->m_len;
872 }
873 seq = (seq << 8) + *rptr++;
874 --len;
875 }
876
877 /*
878 * Check the sequence number and give up if it differs from
879 * the value we're expecting.
880 */
881 if (seq != db->seqno) {
882 if (db->debug)
883 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
884 db->unit, seq, db->seqno - 1);
885 return DECOMP_ERROR;
886 }
887 ++db->seqno;
888
889 /*
890 * Allocate one mbuf to start with.
891 */
892 MGETHDR(dmp, M_DONTWAIT, MT_DATA);
893 if (dmp == NULL)
894 return DECOMP_ERROR;
895 mret = dmp;
896 dmp->m_len = 0;
897 dmp->m_next = NULL;
898 MCLGET(dmp, M_DONTWAIT);
899 dmp->m_data += db->hdrlen;
900 wptr = mtod(dmp, u_char *);
901 space = M_TRAILINGSPACE(dmp) - PPP_HDRLEN + 1;
902
903 /*
904 * Fill in the ppp header, but not the last byte of the protocol
905 * (that comes from the decompressed data).
906 */
907 wptr[0] = adrs;
908 wptr[1] = ctrl;
909 wptr[2] = 0;
910 wptr += PPP_HDRLEN - 1;
911
912 ilen = len;
913 oldcode = CLEAR;
914 explen = 0;
915 for (;;) {
916 if (len == 0) {
917 cmp = cmp->m_next;
918 if (!cmp) /* quit at end of message */
919 break;
920 rptr = mtod(cmp, u_char *);
921 len = cmp->m_len;
922 ilen += len;
923 continue; /* handle 0-length buffers */
924 }
925
926 /*
927 * Accumulate bytes until we have a complete code.
928 * Then get the next code, relying on the 32-bit,
929 * unsigned accm to mask the result.
930 */
931 bitno -= 8;
932 accm |= *rptr++ << bitno;
933 --len;
934 if (tgtbitno < bitno)
935 continue;
936 incode = accm >> tgtbitno;
937 accm <<= n_bits;
938 bitno += n_bits;
939
940 if (incode == CLEAR) {
941 /*
942 * The dictionary must only be cleared at
943 * the end of a packet. But there could be an
944 * empty mbuf at the end.
945 */
946 if (len > 0 || cmp->m_next != NULL) {
947 while ((cmp = cmp->m_next) != NULL)
948 len += cmp->m_len;
949 if (len > 0) {
950 m_freem(mret);
951 if (db->debug)
952 printf("bsd_decomp%d: bad CLEAR\n", db->unit);
953 return DECOMP_FATALERROR; /* probably a bug */
954 }
955 }
956 bsd_clear(db);
957 explen = ilen = 0;
958 break;
959 }
960
961 if (incode > max_ent + 2 || incode > db->maxmaxcode
962 || (incode > max_ent && oldcode == CLEAR)) {
963 m_freem(mret);
964 if (db->debug) {
965 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
966 db->unit, incode, oldcode);
967 printf("max_ent=0x%x explen=%d seqno=%d\n",
968 max_ent, explen, db->seqno);
969 }
970 return DECOMP_FATALERROR; /* probably a bug */
971 }
972
973 /* Special case for KwKwK string. */
974 if (incode > max_ent) {
975 finchar = oldcode;
976 extra = 1;
977 } else {
978 finchar = incode;
979 extra = 0;
980 }
981
982 codelen = db->lens[finchar];
983 explen += codelen + extra;
984 if (explen > db->mru + 1) {
985 m_freem(mret);
986 if (db->debug) {
987 printf("bsd_decomp%d: ran out of mru\n", db->unit);
988#ifdef DEBUG
989 while ((cmp = cmp->m_next) != NULL)
990 len += cmp->m_len;
991 printf(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
992 len, finchar, codelen, explen);
993#endif
994 }
995 return DECOMP_FATALERROR;
996 }
997
998 /*
999 * For simplicity, the decoded characters go in a single mbuf,
1000 * so we allocate a single extra cluster mbuf if necessary.
1001 */
1002 if ((space -= codelen + extra) < 0) {
1003 dmp->m_len = wptr - mtod(dmp, u_char *);
1004 MGET(m, M_DONTWAIT, MT_DATA);
1005 if (m == NULL) {
1006 m_freem(mret);
1007 return DECOMP_ERROR;
1008 }
1009 m->m_len = 0;
1010 m->m_next = NULL;
1011 dmp->m_next = m;
1012 MCLGET(m, M_DONTWAIT);
1013 space = M_TRAILINGSPACE(m) - (codelen + extra);
1014 if (space < 0) {
1015 /* now that's what I call *compression*. */
1016 m_freem(mret);
1017 return DECOMP_ERROR;
1018 }
1019 dmp = m;
1020 wptr = mtod(dmp, u_char *);
1021 }
1022
1023 /*
1024 * Decode this code and install it in the decompressed buffer.
1025 */
1026 p = (wptr += codelen);
1027 while (finchar > LAST) {
1028 dictp = &db->dict[db->dict[finchar].cptr];
1029#ifdef DEBUG
1030 if (--codelen <= 0 || dictp->codem1 != finchar-1)
1031 goto bad;
1032#endif
1033 *--p = dictp->f.hs.suffix;
1034 finchar = dictp->f.hs.prefix;
1035 }
1036 *--p = finchar;
1037
1038#ifdef DEBUG
1039 if (--codelen != 0)
1040 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1041 db->unit, codelen, incode, max_ent);
1042#endif
1043
1044 if (extra) /* the KwKwK case again */
1045 *wptr++ = finchar;
1046
1047 /*
1048 * If not first code in a packet, and
1049 * if not out of code space, then allocate a new code.
1050 *
1051 * Keep the hash table correct so it can be used
1052 * with uncompressed packets.
1053 */
1054 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1055 struct bsd_dict *dictp2;
1056 u_int32_t fcode;
1057 u_int32_t hval, disp;
1058
1059 fcode = BSD_KEY(oldcode,finchar);
1060 hval = BSD_HASH(oldcode,finchar,db->hshift);
1061 dictp = &db->dict[hval];
1062
1063 /* look for a free hash table entry */
1064 if (dictp->codem1 < max_ent) {
1065 disp = (hval == 0) ? 1 : hval;
1066 do {
1067 hval += disp;
1068 if (hval >= db->hsize)
1069 hval -= db->hsize;
1070 dictp = &db->dict[hval];
1071 } while (dictp->codem1 < max_ent);
1072 }
1073
1074 /*
1075 * Invalidate previous hash table entry
1076 * assigned this code, and then take it over
1077 */
1078 dictp2 = &db->dict[max_ent+1];
1079 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1080 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1081 }
1082 dictp2->cptr = hval;
1083 dictp->codem1 = max_ent;
1084 dictp->f.fcode = fcode;
1085
1086 db->max_ent = ++max_ent;
1087 db->lens[max_ent] = db->lens[oldcode]+1;
1088
1089 /* Expand code size if needed. */
1090 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1091 db->n_bits = ++n_bits;
1092 tgtbitno = 32-n_bits;
1093 }
1094 }
1095 oldcode = incode;
1096 }
1097 dmp->m_len = wptr - mtod(dmp, u_char *);
1098
1099 /*
1100 * Keep the checkpoint right so that incompressible packets
1101 * clear the dictionary at the right times.
1102 */
1103 db->bytes_out += ilen;
1104 db->in_count += explen;
1105 if (bsd_check(db) && db->debug) {
1106 printf("bsd_decomp%d: peer should have cleared dictionary\n",
1107 db->unit);
1108 }
1109
1110 ++db->comp_count;
1111 db->comp_bytes += ilen + BSD_OVHD;
1112 ++db->uncomp_count;
1113 db->uncomp_bytes += explen;
1114
1115 *dmpp = mret;
1116 return DECOMP_OK;
1117
1118#ifdef DEBUG
1119 bad:
1120 if (codelen <= 0) {
1121 printf("bsd_decomp%d: fell off end of chain ", db->unit);
1122 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1123 incode, finchar, db->dict[finchar].cptr, max_ent);
1124 } else if (dictp->codem1 != finchar-1) {
1125 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1126 db->unit, incode, finchar);
1127 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
1128 db->dict[finchar].cptr, dictp->codem1);
1129 }
1130 m_freem(mret);
1131 return DECOMP_FATALERROR;
1132#endif /* DEBUG */
1133}
1134#endif /* DO_BSD_COMPRESS */