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