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