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