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