xref: /netbsd-src/sys/net/bsd-comp.c (revision 9573504567626934c7ee01c7dce0c4bb1dfe7403)
1 /*	$NetBSD: bsd-comp.c,v 1.1 1995/07/04 06:28:17 paulus Exp $	*/
2 
3 /* Because this code is derived from the 4.3BSD compress source:
4  *
5  *
6  * Copyright (c) 1985, 1986 The Regents of the University of California.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * James A. Woods, derived from original work by Spencer Thomas
11  * and Joseph Orost.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  */
41 
42 /*
43  * This version is for use with mbufs on BSD-derived systems.
44  *
45  * $Id: bsd-comp.c,v 1.1 1995/07/04 06:28:17 paulus Exp $
46  */
47 
48 #include <sys/param.h>
49 #include <sys/types.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 /*
192  * clear the dictionary
193  */
194 static void
195 bsd_clear(db)
196     struct bsd_db *db;
197 {
198     db->clear_count++;
199     db->max_ent = FIRST-1;
200     db->n_bits = BSD_INIT_BITS;
201     db->ratio = 0;
202     db->bytes_out = 0;
203     db->in_count = 0;
204     db->incomp_count = 0;
205     db->checkpoint = CHECK_GAP;
206 }
207 
208 /*
209  * If the dictionary is full, then see if it is time to reset it.
210  *
211  * Compute the compression ratio using fixed-point arithmetic
212  * with 8 fractional bits.
213  *
214  * Since we have an infinite stream instead of a single file,
215  * watch only the local compression ratio.
216  *
217  * Since both peers must reset the dictionary at the same time even in
218  * the absence of CLEAR codes (while packets are incompressible), they
219  * must compute the same ratio.
220  */
221 static int				/* 1=output CLEAR */
222 bsd_check(db)
223     struct bsd_db *db;
224 {
225     u_int new_ratio;
226 
227     if (db->in_count >= db->checkpoint) {
228 	/* age the ratio by limiting the size of the counts */
229 	if (db->in_count >= RATIO_MAX
230 	    || db->bytes_out >= RATIO_MAX) {
231 	    db->in_count -= db->in_count/4;
232 	    db->bytes_out -= db->bytes_out/4;
233 	}
234 
235 	db->checkpoint = db->in_count + CHECK_GAP;
236 
237 	if (db->max_ent >= db->maxmaxcode) {
238 	    /* Reset the dictionary only if the ratio is worse,
239 	     * or if it looks as if it has been poisoned
240 	     * by incompressible data.
241 	     *
242 	     * This does not overflow, because
243 	     *	db->in_count <= RATIO_MAX.
244 	     */
245 	    new_ratio = db->in_count << RATIO_SCALE_LOG;
246 	    if (db->bytes_out != 0)
247 		new_ratio /= db->bytes_out;
248 
249 	    if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
250 		bsd_clear(db);
251 		return 1;
252 	    }
253 	    db->ratio = new_ratio;
254 	}
255     }
256     return 0;
257 }
258 
259 /*
260  * Return statistics.
261  */
262 static void
263 bsd_comp_stats(state, stats)
264     void *state;
265     struct compstat *stats;
266 {
267     struct bsd_db *db = (struct bsd_db *) state;
268     u_int out;
269 
270     stats->unc_bytes = db->uncomp_bytes;
271     stats->unc_packets = db->uncomp_count;
272     stats->comp_bytes = db->comp_bytes;
273     stats->comp_packets = db->comp_count;
274     stats->inc_bytes = db->incomp_bytes;
275     stats->inc_packets = db->incomp_count;
276     stats->ratio = db->in_count;
277     out = db->bytes_out;
278     if (stats->ratio <= 0x7fffff)
279 	stats->ratio <<= 8;
280     else
281 	out >>= 8;
282     if (out != 0)
283 	stats->ratio /= out;
284 }
285 
286 /*
287  * Reset state, as on a CCP ResetReq.
288  */
289 static void
290 bsd_reset(state)
291     void *state;
292 {
293     struct bsd_db *db = (struct bsd_db *) state;
294 
295     db->seqno = 0;
296     bsd_clear(db);
297     db->clear_count = 0;
298 }
299 
300 /*
301  * Allocate space for a (de) compressor.
302  */
303 static void *
304 bsd_alloc(options, opt_len, decomp)
305     u_char *options;
306     int opt_len, decomp;
307 {
308     int bits;
309     u_int newlen, hsize, hshift, maxmaxcode;
310     struct bsd_db *db;
311 
312     if (opt_len != CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
313 	|| options[1] != CILEN_BSD_COMPRESS
314 	|| BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
315 	return NULL;
316     bits = BSD_NBITS(options[2]);
317     switch (bits) {
318     case 9:			/* needs 82152 for both directions */
319     case 10:			/* needs 84144 */
320     case 11:			/* needs 88240 */
321     case 12:			/* needs 96432 */
322 	hsize = 5003;
323 	hshift = 4;
324 	break;
325     case 13:			/* needs 176784 */
326 	hsize = 9001;
327 	hshift = 5;
328 	break;
329     case 14:			/* needs 353744 */
330 	hsize = 18013;
331 	hshift = 6;
332 	break;
333     case 15:			/* needs 691440 */
334 	hsize = 35023;
335 	hshift = 7;
336 	break;
337     case 16:			/* needs 1366160--far too much, */
338 	/* hsize = 69001; */	/* and 69001 is too big for cptr */
339 	/* hshift = 8; */	/* in struct bsd_db */
340 	/* break; */
341     default:
342 	return NULL;
343     }
344 
345     maxmaxcode = MAXCODE(bits);
346     newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
347     MALLOC(db, struct bsd_db *, newlen, M_DEVBUF, M_NOWAIT);
348     if (!db)
349 	return NULL;
350     bzero(db, sizeof(*db) - sizeof(db->dict));
351 
352     if (!decomp) {
353 	db->lens = NULL;
354     } else {
355 	MALLOC(db->lens, u_int16_t *, (maxmaxcode+1) * sizeof(db->lens[0]),
356 	       M_DEVBUF, M_NOWAIT);
357 	if (!db->lens) {
358 	    FREE(db, M_DEVBUF);
359 	    return NULL;
360 	}
361     }
362 
363     db->totlen = newlen;
364     db->hsize = hsize;
365     db->hshift = hshift;
366     db->maxmaxcode = maxmaxcode;
367     db->maxbits = bits;
368 
369     return (void *) db;
370 }
371 
372 static void
373 bsd_free(state)
374     void *state;
375 {
376     struct bsd_db *db = (struct bsd_db *) state;
377 
378     if (db->lens)
379 	FREE(db->lens, M_DEVBUF);
380     FREE(db, M_DEVBUF);
381 }
382 
383 static void *
384 bsd_comp_alloc(options, opt_len)
385     u_char *options;
386     int opt_len;
387 {
388     return bsd_alloc(options, opt_len, 0);
389 }
390 
391 static void *
392 bsd_decomp_alloc(options, opt_len)
393     u_char *options;
394     int opt_len;
395 {
396     return bsd_alloc(options, opt_len, 1);
397 }
398 
399 /*
400  * Initialize the database.
401  */
402 static int
403 bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp)
404     struct bsd_db *db;
405     u_char *options;
406     int opt_len, unit, hdrlen, mru, debug, decomp;
407 {
408     int i;
409 
410     if (opt_len != CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
411 	|| options[1] != CILEN_BSD_COMPRESS
412 	|| BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
413 	|| BSD_NBITS(options[2]) != db->maxbits
414 	|| decomp && db->lens == NULL)
415 	return 0;
416 
417     if (decomp) {
418 	i = LAST+1;
419 	while (i != 0)
420 	    db->lens[--i] = 1;
421     }
422     i = db->hsize;
423     while (i != 0) {
424 	db->dict[--i].codem1 = BADCODEM1;
425 	db->dict[i].cptr = 0;
426     }
427 
428     db->unit = unit;
429     db->hdrlen = hdrlen;
430     db->mru = mru;
431 #ifndef DEBUG
432     if (debug)
433 #endif
434 	db->debug = 1;
435 
436     bsd_reset(db);
437 
438     return 1;
439 }
440 
441 static int
442 bsd_comp_init(state, options, opt_len, unit, hdrlen, debug)
443     void *state;
444     u_char *options;
445     int opt_len, unit, hdrlen, debug;
446 {
447     return bsd_init((struct bsd_db *) state, options, opt_len,
448 		    unit, hdrlen, 0, debug, 0);
449 }
450 
451 static int
452 bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
453     void *state;
454     u_char *options;
455     int opt_len, unit, hdrlen, mru, debug;
456 {
457     return bsd_init((struct bsd_db *) state, options, opt_len,
458 		    unit, hdrlen, mru, debug, 1);
459 }
460 
461 
462 /*
463  * compress a packet
464  *	One change from the BSD compress command is that when the
465  *	code size expands, we do not output a bunch of padding.
466  */
467 int					/* new slen */
468 bsd_compress(state, mret, mp, slen, maxolen)
469     void *state;
470     struct mbuf **mret;		/* return compressed mbuf chain here */
471     struct mbuf *mp;		/* from here */
472     int slen;			/* uncompressed length */
473     int maxolen;		/* max compressed length */
474 {
475     struct bsd_db *db = (struct bsd_db *) state;
476     int hshift = db->hshift;
477     u_int max_ent = db->max_ent;
478     u_int n_bits = db->n_bits;
479     u_int bitno = 32;
480     u_int32_t accm = 0, fcode;
481     struct bsd_dict *dictp;
482     u_char c;
483     int hval, disp, ent, ilen;
484     struct mbuf *np;
485     u_char *rptr, *wptr;
486     u_char *cp_end;
487     int olen;
488     struct mbuf *m, **mnp;
489 
490 #define PUTBYTE(v) {					\
491     ++olen;						\
492     if (wptr) {						\
493 	*wptr++ = (v);					\
494 	if (wptr >= cp_end) {				\
495 	    m->m_len = wptr - mtod(m, u_char *);	\
496 	    MGET(m->m_next, M_DONTWAIT, MT_DATA);	\
497 	    m = m->m_next;				\
498 	    if (m) {					\
499 		m->m_len = 0;				\
500 		if (maxolen - olen > MLEN)		\
501 		    MCLGET(m, M_DONTWAIT);		\
502 		wptr = mtod(m, u_char *);		\
503 		cp_end = wptr + M_TRAILINGSPACE(m);	\
504 	    } else					\
505 		wptr = NULL;				\
506 	}						\
507     }							\
508 }
509 
510 #define OUTPUT(ent) {					\
511     bitno -= n_bits;					\
512     accm |= ((ent) << bitno);				\
513     do {						\
514 	PUTBYTE(accm >> 24);				\
515 	accm <<= 8;					\
516 	bitno += 8;					\
517     } while (bitno <= 24);				\
518 }
519 
520     /*
521      * If the protocol is not in the range we're interested in,
522      * just return without compressing the packet.  If it is,
523      * the protocol becomes the first byte to compress.
524      */
525     rptr = mtod(mp, u_char *);
526     ent = PPP_PROTOCOL(rptr);
527     if (ent < 0x21 || ent > 0xf9) {
528 	*mret = NULL;
529 	return slen;
530     }
531 
532     /* Don't generate compressed packets which are larger than
533        the uncompressed packet. */
534     if (maxolen > slen)
535 	maxolen = slen;
536 
537     /* Allocate one mbuf to start with. */
538     MGET(m, M_DONTWAIT, MT_DATA);
539     *mret = m;
540     if (m != NULL) {
541 	m->m_len = 0;
542 	if (maxolen + db->hdrlen > MLEN)
543 	    MCLGET(m, M_DONTWAIT);
544 	m->m_data += db->hdrlen;
545 	wptr = mtod(m, u_char *);
546 	cp_end = wptr + M_TRAILINGSPACE(m);
547     } else
548 	wptr = cp_end = NULL;
549 
550     /*
551      * Copy the PPP header over, changing the protocol,
552      * and install the 2-byte packet sequence number.
553      */
554     if (wptr) {
555 	*wptr++ = PPP_ADDRESS(rptr);	/* assumes the ppp header is */
556 	*wptr++ = PPP_CONTROL(rptr);	/* all in one mbuf */
557 	*wptr++ = 0;			/* change the protocol */
558 	*wptr++ = PPP_COMP;
559 	*wptr++ = db->seqno >> 8;
560 	*wptr++ = db->seqno;
561     }
562     ++db->seqno;
563 
564     olen = 0;
565     rptr += PPP_HDRLEN;
566     slen = mp->m_len - PPP_HDRLEN;
567     ilen = slen + 1;
568     for (;;) {
569 	if (slen <= 0) {
570 	    mp = mp->m_next;
571 	    if (!mp)
572 		break;
573 	    rptr = mtod(mp, u_char *);
574 	    slen = mp->m_len;
575 	    if (!slen)
576 		continue;   /* handle 0-length buffers */
577 	    ilen += slen;
578 	}
579 
580 	slen--;
581 	c = *rptr++;
582 	fcode = BSD_KEY(ent, c);
583 	hval = BSD_HASH(ent, c, hshift);
584 	dictp = &db->dict[hval];
585 
586 	/* Validate and then check the entry. */
587 	if (dictp->codem1 >= max_ent)
588 	    goto nomatch;
589 	if (dictp->f.fcode == fcode) {
590 	    ent = dictp->codem1+1;
591 	    continue;	/* found (prefix,suffix) */
592 	}
593 
594 	/* continue probing until a match or invalid entry */
595 	disp = (hval == 0) ? 1 : hval;
596 	do {
597 	    hval += disp;
598 	    if (hval >= db->hsize)
599 		hval -= db->hsize;
600 	    dictp = &db->dict[hval];
601 	    if (dictp->codem1 >= max_ent)
602 		goto nomatch;
603 	} while (dictp->f.fcode != fcode);
604 	ent = dictp->codem1 + 1;	/* finally found (prefix,suffix) */
605 	continue;
606 
607     nomatch:
608 	OUTPUT(ent);		/* output the prefix */
609 
610 	/* code -> hashtable */
611 	if (max_ent < db->maxmaxcode) {
612 	    struct bsd_dict *dictp2;
613 	    /* expand code size if needed */
614 	    if (max_ent >= MAXCODE(n_bits))
615 		db->n_bits = ++n_bits;
616 
617 	    /* Invalidate old hash table entry using
618 	     * this code, and then take it over.
619 	     */
620 	    dictp2 = &db->dict[max_ent+1];
621 	    if (db->dict[dictp2->cptr].codem1 == max_ent)
622 		db->dict[dictp2->cptr].codem1 = BADCODEM1;
623 	    dictp2->cptr = hval;
624 	    dictp->codem1 = max_ent;
625 	    dictp->f.fcode = fcode;
626 
627 	    db->max_ent = ++max_ent;
628 	}
629 	ent = c;
630     }
631 
632     OUTPUT(ent);		/* output the last code */
633     db->bytes_out += olen;
634     db->in_count += ilen;
635     if (bitno < 32)
636 	++db->bytes_out;	/* count complete bytes */
637 
638     if (bsd_check(db))
639 	OUTPUT(CLEAR);		/* do not count the CLEAR */
640 
641     /*
642      * Pad dribble bits of last code with ones.
643      * Do not emit a completely useless byte of ones.
644      */
645     if (bitno != 32)
646 	PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
647 
648     if (m != NULL) {
649 	m->m_len = wptr - mtod(m, u_char *);
650 	m->m_next = NULL;
651     }
652 
653     /*
654      * Increase code size if we would have without the packet
655      * boundary and as the decompressor will.
656      */
657     if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
658 	db->n_bits++;
659 
660     db->uncomp_bytes += ilen;
661     ++db->uncomp_count;
662     if (olen + PPP_HDRLEN + BSD_OVHD > maxolen) {
663 	/* throw away the compressed stuff if it is longer than uncompressed */
664 	if (*mret != NULL) {
665 	    m_freem(*mret);
666 	    *mret = NULL;
667 	}
668 	++db->incomp_count;
669 	db->incomp_bytes += ilen;
670     } else {
671 	++db->comp_count;
672 	db->comp_bytes += olen + BSD_OVHD;
673     }
674 
675     return olen + PPP_HDRLEN + BSD_OVHD;
676 #undef OUTPUT
677 #undef PUTBYTE
678 }
679 
680 
681 /*
682  * Update the "BSD Compress" dictionary on the receiver for
683  * incompressible data by pretending to compress the incoming data.
684  */
685 static void
686 bsd_incomp(state, dmsg)
687     void *state;
688     struct mbuf *dmsg;
689 {
690     struct bsd_db *db = (struct bsd_db *) state;
691     u_int hshift = db->hshift;
692     u_int max_ent = db->max_ent;
693     u_int n_bits = db->n_bits;
694     struct bsd_dict *dictp;
695     u_int32_t fcode;
696     u_char c;
697     u_int32_t hval, disp;
698     int slen, ilen;
699     u_int bitno = 7;
700     u_char *rptr;
701     u_int ent;
702 
703     /*
704      * If the protocol is not in the range we're interested in,
705      * just return without looking at the packet.  If it is,
706      * the protocol becomes the first byte to "compress".
707      */
708     rptr = mtod(dmsg, u_char *);
709     ent = PPP_PROTOCOL(rptr);
710     if (ent < 0x21 || ent > 0xf9)
711 	return;
712 
713     db->incomp_count++;
714     db->seqno++;
715     ilen = 1;		/* count the protocol as 1 byte */
716     rptr += PPP_HDRLEN;
717     slen = dmsg->m_len - PPP_HDRLEN;
718     for (;;) {
719 	if (slen <= 0) {
720 	    dmsg = dmsg->m_next;
721 	    if (!dmsg)
722 		break;
723 	    rptr = mtod(dmsg, u_char *);
724 	    slen = dmsg->m_len;
725 	    continue;
726 	}
727 	ilen += slen;
728 
729 	do {
730 	    c = *rptr++;
731 	    fcode = BSD_KEY(ent, c);
732 	    hval = BSD_HASH(ent, c, hshift);
733 	    dictp = &db->dict[hval];
734 
735 	    /* validate and then check the entry */
736 	    if (dictp->codem1 >= max_ent)
737 		goto nomatch;
738 	    if (dictp->f.fcode == fcode) {
739 		ent = dictp->codem1+1;
740 		continue;   /* found (prefix,suffix) */
741 	    }
742 
743 	    /* continue probing until a match or invalid entry */
744 	    disp = (hval == 0) ? 1 : hval;
745 	    do {
746 		hval += disp;
747 		if (hval >= db->hsize)
748 		    hval -= db->hsize;
749 		dictp = &db->dict[hval];
750 		if (dictp->codem1 >= max_ent)
751 		    goto nomatch;
752 	    } while (dictp->f.fcode != fcode);
753 	    ent = dictp->codem1+1;
754 	    continue;	/* finally found (prefix,suffix) */
755 
756 	nomatch:		/* output (count) the prefix */
757 	    bitno += n_bits;
758 
759 	    /* code -> hashtable */
760 	    if (max_ent < db->maxmaxcode) {
761 		struct bsd_dict *dictp2;
762 		/* expand code size if needed */
763 		if (max_ent >= MAXCODE(n_bits))
764 		    db->n_bits = ++n_bits;
765 
766 		/* Invalidate previous hash table entry
767 		 * assigned this code, and then take it over.
768 		 */
769 		dictp2 = &db->dict[max_ent+1];
770 		if (db->dict[dictp2->cptr].codem1 == max_ent)
771 		    db->dict[dictp2->cptr].codem1 = BADCODEM1;
772 		dictp2->cptr = hval;
773 		dictp->codem1 = max_ent;
774 		dictp->f.fcode = fcode;
775 
776 		db->max_ent = ++max_ent;
777 		db->lens[max_ent] = db->lens[ent]+1;
778 	    }
779 	    ent = c;
780 	} while (--slen != 0);
781     }
782     bitno += n_bits;		/* output (count) the last code */
783     db->bytes_out += bitno/8;
784     db->in_count += ilen;
785     (void)bsd_check(db);
786 
787     ++db->incomp_count;
788     db->incomp_bytes += ilen;
789     ++db->uncomp_count;
790     db->uncomp_bytes += ilen;
791 
792     /* Increase code size if we would have without the packet
793      * boundary and as the decompressor will.
794      */
795     if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
796 	db->n_bits++;
797 }
798 
799 
800 /*
801  * Decompress "BSD Compress".
802  *
803  * Because of patent problems, we return DECOMP_ERROR for errors
804  * found by inspecting the input data and for system problems, but
805  * DECOMP_FATALERROR for any errors which could possibly be said to
806  * be being detected "after" decompression.  For DECOMP_ERROR,
807  * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
808  * infringing a patent of Motorola's if we do, so we take CCP down
809  * instead.
810  *
811  * Given that the frame has the correct sequence number and a good FCS,
812  * errors such as invalid codes in the input most likely indicate a
813  * bug, so we return DECOMP_FATALERROR for them in order to turn off
814  * compression, even though they are detected by inspecting the input.
815  */
816 int
817 bsd_decompress(state, cmp, dmpp)
818     void *state;
819     struct mbuf *cmp, **dmpp;
820 {
821     struct bsd_db *db = (struct bsd_db *) state;
822     u_int max_ent = db->max_ent;
823     u_int32_t accm = 0;
824     u_int bitno = 32;		/* 1st valid bit in accm */
825     u_int n_bits = db->n_bits;
826     u_int tgtbitno = 32-n_bits;	/* bitno when we have a code */
827     struct bsd_dict *dictp;
828     int explen, i, seq, len;
829     u_int incode, oldcode, finchar;
830     u_char *p, *rptr, *wptr;
831     struct mbuf *m, *dmp, *mret;
832     int adrs, ctrl, ilen;
833     int space, codelen, extra;
834     struct mbuf *last;
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