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