xref: /openbsd-src/sys/net/bsd-comp.c (revision 9b9d2a55a62c8e82206c25f94fcc7f4e2765250e)
1 /*	$OpenBSD: bsd-comp.c,v 1.11 2015/07/15 22:16:41 deraadt Exp $	*/
2 /*	$NetBSD: bsd-comp.c,v 1.6 1996/10/13 02:10:58 christos Exp $	*/
3 
4 /* Because this code is derived from the 4.3BSD compress source:
5  *
6  *
7  * Copyright (c) 1985, 1986 The Regents of the University of California.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * James A. Woods, derived from original work by Spencer Thomas
12  * and Joseph Orost.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 /*
40  * This version is for use with mbufs on BSD-derived systems.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/types.h>
45 #include <sys/systm.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <net/if.h>
49 #include <net/if_var.h>
50 #include <net/if_types.h>
51 #include <net/ppp_defs.h>
52 #include <net/if_ppp.h>
53 
54 #define PACKETPTR	struct mbuf *
55 #include <net/ppp-comp.h>
56 
57 #if DO_BSD_COMPRESS
58 /*
59  * PPP "BSD compress" compression
60  *  The differences between this compression and the classic BSD LZW
61  *  source are obvious from the requirement that the classic code worked
62  *  with files while this handles arbitrarily long streams that
63  *  are broken into packets.  They are:
64  *
65  *	When the code size expands, a block of junk is not emitted by
66  *	    the compressor and not expected by the decompressor.
67  *
68  *	New codes are not necessarily assigned every time an old
69  *	    code is output by the compressor.  This is because a packet
70  *	    end forces a code to be emitted, but does not imply that a
71  *	    new sequence has been seen.
72  *
73  *	The compression ratio is checked at the first end of a packet
74  *	    after the appropriate gap.	Besides simplifying and speeding
75  *	    things up, this makes it more likely that the transmitter
76  *	    and receiver will agree when the dictionary is cleared when
77  *	    compression is not going well.
78  */
79 
80 /*
81  * A dictionary for doing BSD compress.
82  */
83 struct bsd_db {
84     int	    totlen;			/* length of this structure */
85     u_int   hsize;			/* size of the hash table */
86     u_char  hshift;			/* used in hash function */
87     u_char  n_bits;			/* current bits/code */
88     u_char  maxbits;
89     u_char  debug;
90     u_char  unit;
91     u_int16_t seqno;			/* sequence # of next packet */
92     u_int   hdrlen;			/* header length to preallocate */
93     u_int   mru;
94     u_int   maxmaxcode;			/* largest valid code */
95     u_int   max_ent;			/* largest code in use */
96     u_int   in_count;			/* uncompressed bytes, aged */
97     u_int   bytes_out;			/* compressed bytes, aged */
98     u_int   ratio;			/* recent compression ratio */
99     u_int   checkpoint;			/* when to next check the ratio */
100     u_int   clear_count;		/* times dictionary cleared */
101     u_int   incomp_count;		/* incompressible packets */
102     u_int   incomp_bytes;		/* incompressible bytes */
103     u_int   uncomp_count;		/* uncompressed packets */
104     u_int   uncomp_bytes;		/* uncompressed bytes */
105     u_int   comp_count;			/* compressed packets */
106     u_int   comp_bytes;			/* compressed bytes */
107     u_int16_t *lens;			/* array of lengths of codes */
108     struct bsd_dict {
109 	union {				/* hash value */
110 	    u_int32_t	fcode;
111 	    struct {
112 #if BYTE_ORDER == LITTLE_ENDIAN
113 		u_int16_t prefix;	/* preceding code */
114 		u_char	suffix;		/* last character of new code */
115 		u_char	pad;
116 #else
117 		u_char	pad;
118 		u_char	suffix;		/* last character of new code */
119 		u_int16_t prefix;	/* preceding code */
120 #endif
121 	    } hs;
122 	} f;
123 	u_int16_t codem1;		/* output of hash table -1 */
124 	u_int16_t cptr;			/* map code to hash table entry */
125     } dict[1];
126 };
127 
128 #define BSD_OVHD	2		/* BSD compress overhead/packet */
129 #define BSD_INIT_BITS	BSD_MIN_BITS
130 
131 static void	*bsd_comp_alloc(u_char *options, int opt_len);
132 static void	*bsd_decomp_alloc(u_char *options, int opt_len);
133 static void	bsd_free(void *state);
134 static int	bsd_comp_init(void *state, u_char *options, int opt_len,
135 				   int unit, int hdrlen, int debug);
136 static int	bsd_decomp_init(void *state, u_char *options, int opt_len,
137 				     int unit, int hdrlen, int mru, int debug);
138 static int	bsd_compress(void *state, struct mbuf **mret,
139 				  struct mbuf *mp, int slen, int maxolen);
140 static void	bsd_incomp(void *state, struct mbuf *dmsg);
141 static int	bsd_decompress(void *state, struct mbuf *cmp,
142 				    struct mbuf **dmpp);
143 static void	bsd_reset(void *state);
144 static void	bsd_comp_stats(void *state, struct compstat *stats);
145 
146 /*
147  * Procedures exported to if_ppp.c.
148  */
149 struct compressor ppp_bsd_compress = {
150     CI_BSD_COMPRESS,		/* compress_proto */
151     bsd_comp_alloc,		/* comp_alloc */
152     bsd_free,			/* comp_free */
153     bsd_comp_init,		/* comp_init */
154     bsd_reset,			/* comp_reset */
155     bsd_compress,		/* compress */
156     bsd_comp_stats,		/* comp_stat */
157     bsd_decomp_alloc,		/* decomp_alloc */
158     bsd_free,			/* decomp_free */
159     bsd_decomp_init,		/* decomp_init */
160     bsd_reset,			/* decomp_reset */
161     bsd_decompress,		/* decompress */
162     bsd_incomp,			/* incomp */
163     bsd_comp_stats,		/* decomp_stat */
164 };
165 
166 /*
167  * the next two codes should not be changed lightly, as they must not
168  * lie within the contiguous general code space.
169  */
170 #define CLEAR	256			/* table clear output code */
171 #define FIRST	257			/* first free entry */
172 #define LAST	255
173 
174 #define MAXCODE(b)	((1 << (b)) - 1)
175 #define BADCODEM1	MAXCODE(BSD_MAX_BITS)
176 
177 #define BSD_HASH(prefix,suffix,hshift)	((((u_int32_t)(suffix)) << (hshift)) \
178 					 ^ (u_int32_t)(prefix))
179 #define BSD_KEY(prefix,suffix)		((((u_int32_t)(suffix)) << 16) \
180 					 + (u_int32_t)(prefix))
181 
182 #define CHECK_GAP	10000		/* Ratio check interval */
183 
184 #define RATIO_SCALE_LOG	8
185 #define RATIO_SCALE	(1<<RATIO_SCALE_LOG)
186 #define RATIO_MAX	(0x7fffffff>>RATIO_SCALE_LOG)
187 
188 static void bsd_clear(struct bsd_db *);
189 static int bsd_check(struct bsd_db *);
190 static void *bsd_alloc(u_char *, int, int);
191 static int bsd_init(struct bsd_db *, u_char *, int, int, int, int,
192 			 int, int);
193 
194 /*
195  * clear the dictionary
196  */
197 static void
198 bsd_clear(db)
199     struct bsd_db *db;
200 {
201     db->clear_count++;
202     db->max_ent = FIRST-1;
203     db->n_bits = BSD_INIT_BITS;
204     db->ratio = 0;
205     db->bytes_out = 0;
206     db->in_count = 0;
207     db->incomp_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|M_ZERO);
351     if (!db)
352 	return NULL;
353 
354     if (!decomp) {
355 	db->lens = NULL;
356     } else {
357 	db->lens = mallocarray(maxmaxcode + 1, sizeof(db->lens[0]), M_DEVBUF,
358 	    M_NOWAIT);
359 	if (!db->lens) {
360 	    free(db, M_DEVBUF, 0);
361 	    return NULL;
362 	}
363     }
364 
365     db->totlen = newlen;
366     db->hsize = hsize;
367     db->hshift = hshift;
368     db->maxmaxcode = maxmaxcode;
369     db->maxbits = bits;
370 
371     return (void *) db;
372 }
373 
374 static void
375 bsd_free(state)
376     void *state;
377 {
378     struct bsd_db *db = (struct bsd_db *) state;
379 
380     if (db->lens)
381 	free(db->lens, M_DEVBUF, 0);
382     free(db, M_DEVBUF, 0);
383 }
384 
385 static void *
386 bsd_comp_alloc(options, opt_len)
387     u_char *options;
388     int opt_len;
389 {
390     return bsd_alloc(options, opt_len, 0);
391 }
392 
393 static void *
394 bsd_decomp_alloc(options, opt_len)
395     u_char *options;
396     int opt_len;
397 {
398     return bsd_alloc(options, opt_len, 1);
399 }
400 
401 /*
402  * Initialize the database.
403  */
404 static int
405 bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp)
406     struct bsd_db *db;
407     u_char *options;
408     int opt_len, unit, hdrlen, mru, debug, decomp;
409 {
410     int i;
411 
412     if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
413 	|| options[1] != CILEN_BSD_COMPRESS
414 	|| BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
415 	|| BSD_NBITS(options[2]) != db->maxbits
416 	|| (decomp && db->lens == NULL))
417 	return 0;
418 
419     if (decomp) {
420 	i = LAST+1;
421 	while (i != 0)
422 	    db->lens[--i] = 1;
423     }
424     i = db->hsize;
425     while (i != 0) {
426 	db->dict[--i].codem1 = BADCODEM1;
427 	db->dict[i].cptr = 0;
428     }
429 
430     db->unit = unit;
431     db->hdrlen = hdrlen;
432     db->mru = mru;
433 #ifndef DEBUG
434     if (debug)
435 #endif
436 	db->debug = 1;
437 
438     bsd_reset(db);
439 
440     return 1;
441 }
442 
443 static int
444 bsd_comp_init(state, options, opt_len, unit, hdrlen, debug)
445     void *state;
446     u_char *options;
447     int opt_len, unit, hdrlen, debug;
448 {
449     return bsd_init((struct bsd_db *) state, options, opt_len,
450 		    unit, hdrlen, 0, debug, 0);
451 }
452 
453 static int
454 bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
455     void *state;
456     u_char *options;
457     int opt_len, unit, hdrlen, mru, debug;
458 {
459     return bsd_init((struct bsd_db *) state, options, opt_len,
460 		    unit, hdrlen, mru, debug, 1);
461 }
462 
463 
464 /*
465  * compress a packet
466  *	One change from the BSD compress command is that when the
467  *	code size expands, we do not output a bunch of padding.
468  */
469 int					/* new slen */
470 bsd_compress(state, mret, mp, slen, maxolen)
471     void *state;
472     struct mbuf **mret;		/* return compressed mbuf chain here */
473     struct mbuf *mp;		/* from here */
474     int slen;			/* uncompressed length */
475     int maxolen;		/* max compressed length */
476 {
477     struct bsd_db *db = (struct bsd_db *) state;
478     int hshift = db->hshift;
479     u_int max_ent = db->max_ent;
480     u_int n_bits = db->n_bits;
481     u_int bitno = 32;
482     u_int32_t accm = 0, fcode;
483     struct bsd_dict *dictp;
484     u_char c;
485     int hval, disp, ent, ilen;
486     u_char *rptr, *wptr;
487     u_char *cp_end;
488     int olen;
489     struct mbuf *m;
490 
491 #define PUTBYTE(v) {					\
492     ++olen;						\
493     if (wptr) {						\
494 	*wptr++ = (v);					\
495 	if (wptr >= cp_end) {				\
496 	    m->m_len = wptr - mtod(m, u_char *);	\
497 	    MGET(m->m_next, M_DONTWAIT, MT_DATA);	\
498 	    m = m->m_next;				\
499 	    if (m) {					\
500 		m->m_len = 0;				\
501 		if (maxolen - olen > MLEN)		\
502 		    MCLGET(m, M_DONTWAIT);		\
503 		wptr = mtod(m, u_char *);		\
504 		cp_end = wptr + M_TRAILINGSPACE(m);	\
505 	    } else					\
506 		wptr = NULL;				\
507 	}						\
508     }							\
509 }
510 
511 #define OUTPUT(ent) {					\
512     bitno -= n_bits;					\
513     accm |= ((ent) << bitno);				\
514     do {						\
515 	PUTBYTE(accm >> 24);				\
516 	accm <<= 8;					\
517 	bitno += 8;					\
518     } while (bitno <= 24);				\
519 }
520 
521     /*
522      * If the protocol is not in the range we're interested in,
523      * just return without compressing the packet.  If it is,
524      * the protocol becomes the first byte to compress.
525      */
526     rptr = mtod(mp, u_char *);
527     ent = PPP_PROTOCOL(rptr);
528     if (ent < 0x21 || ent > 0xf9) {
529 	*mret = NULL;
530 	return slen;
531     }
532 
533     /* Don't generate compressed packets which are larger than
534        the uncompressed packet. */
535     if (maxolen > slen)
536 	maxolen = slen;
537 
538     /* Allocate one mbuf to start with. */
539     MGET(m, M_DONTWAIT, MT_DATA);
540     *mret = m;
541     if (m != NULL) {
542 	m->m_len = 0;
543 	if (maxolen + db->hdrlen > MLEN)
544 	    MCLGET(m, M_DONTWAIT);
545 	m->m_data += db->hdrlen;
546 	wptr = mtod(m, u_char *);
547 	cp_end = wptr + M_TRAILINGSPACE(m);
548     } else
549 	wptr = cp_end = NULL;
550 
551     /*
552      * Copy the PPP header over, changing the protocol,
553      * and install the 2-byte packet sequence number.
554      */
555     if (wptr) {
556 	*wptr++ = PPP_ADDRESS(rptr);	/* assumes the ppp header is */
557 	*wptr++ = PPP_CONTROL(rptr);	/* all in one mbuf */
558 	*wptr++ = 0;			/* change the protocol */
559 	*wptr++ = PPP_COMP;
560 	*wptr++ = db->seqno >> 8;
561 	*wptr++ = db->seqno;
562     }
563     ++db->seqno;
564 
565     olen = 0;
566     rptr += PPP_HDRLEN;
567     slen = mp->m_len - PPP_HDRLEN;
568     ilen = slen + 1;
569     for (;;) {
570 	if (slen <= 0) {
571 	    mp = mp->m_next;
572 	    if (!mp)
573 		break;
574 	    rptr = mtod(mp, u_char *);
575 	    slen = mp->m_len;
576 	    if (!slen)
577 		continue;   /* handle 0-length buffers */
578 	    ilen += slen;
579 	}
580 
581 	slen--;
582 	c = *rptr++;
583 	fcode = BSD_KEY(ent, c);
584 	hval = BSD_HASH(ent, c, hshift);
585 	dictp = &db->dict[hval];
586 
587 	/* Validate and then check the entry. */
588 	if (dictp->codem1 >= max_ent)
589 	    goto nomatch;
590 	if (dictp->f.fcode == fcode) {
591 	    ent = dictp->codem1+1;
592 	    continue;	/* found (prefix,suffix) */
593 	}
594 
595 	/* continue probing until a match or invalid entry */
596 	disp = (hval == 0) ? 1 : hval;
597 	do {
598 	    hval += disp;
599 	    if (hval >= db->hsize)
600 		hval -= db->hsize;
601 	    dictp = &db->dict[hval];
602 	    if (dictp->codem1 >= max_ent)
603 		goto nomatch;
604 	} while (dictp->f.fcode != fcode);
605 	ent = dictp->codem1 + 1;	/* finally found (prefix,suffix) */
606 	continue;
607 
608     nomatch:
609 	OUTPUT(ent);		/* output the prefix */
610 
611 	/* code -> hashtable */
612 	if (max_ent < db->maxmaxcode) {
613 	    struct bsd_dict *dictp2;
614 	    /* expand code size if needed */
615 	    if (max_ent >= MAXCODE(n_bits))
616 		db->n_bits = ++n_bits;
617 
618 	    /* Invalidate old hash table entry using
619 	     * this code, and then take it over.
620 	     */
621 	    dictp2 = &db->dict[max_ent+1];
622 	    if (db->dict[dictp2->cptr].codem1 == max_ent)
623 		db->dict[dictp2->cptr].codem1 = BADCODEM1;
624 	    dictp2->cptr = hval;
625 	    dictp->codem1 = max_ent;
626 	    dictp->f.fcode = fcode;
627 
628 	    db->max_ent = ++max_ent;
629 	}
630 	ent = c;
631     }
632 
633     OUTPUT(ent);		/* output the last code */
634     db->bytes_out += olen;
635     db->in_count += ilen;
636     if (bitno < 32)
637 	++db->bytes_out;	/* count complete bytes */
638 
639     if (bsd_check(db))
640 	OUTPUT(CLEAR);		/* do not count the CLEAR */
641 
642     /*
643      * Pad dribble bits of last code with ones.
644      * Do not emit a completely useless byte of ones.
645      */
646     if (bitno != 32)
647 	PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
648 
649     if (m != NULL) {
650 	m->m_len = wptr - mtod(m, u_char *);
651 	m->m_next = NULL;
652     }
653 
654     /*
655      * Increase code size if we would have without the packet
656      * boundary and as the decompressor will.
657      */
658     if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
659 	db->n_bits++;
660 
661     db->uncomp_bytes += ilen;
662     ++db->uncomp_count;
663     if (olen + PPP_HDRLEN + BSD_OVHD > maxolen) {
664 	/* throw away the compressed stuff if it is longer than uncompressed */
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 
835     /*
836      * Save the address/control from the PPP header
837      * and then get the sequence number.
838      */
839     *dmpp = NULL;
840     rptr = mtod(cmp, u_char *);
841     adrs = PPP_ADDRESS(rptr);
842     ctrl = PPP_CONTROL(rptr);
843     rptr += PPP_HDRLEN;
844     len = cmp->m_len - PPP_HDRLEN;
845     seq = 0;
846     for (i = 0; i < 2; ++i) {
847 	while (len <= 0) {
848 	    cmp = cmp->m_next;
849 	    if (cmp == NULL)
850 		return DECOMP_ERROR;
851 	    rptr = mtod(cmp, u_char *);
852 	    len = cmp->m_len;
853 	}
854 	seq = (seq << 8) + *rptr++;
855 	--len;
856     }
857 
858     /*
859      * Check the sequence number and give up if it differs from
860      * the value we're expecting.
861      */
862     if (seq != db->seqno) {
863 	if (db->debug)
864 	    printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
865 		   db->unit, seq, db->seqno - 1);
866 	return DECOMP_ERROR;
867     }
868     ++db->seqno;
869 
870     /*
871      * Allocate one mbuf to start with.
872      */
873     MGETHDR(dmp, M_DONTWAIT, MT_DATA);
874     if (dmp == NULL)
875 	return DECOMP_ERROR;
876     mret = dmp;
877     dmp->m_len = 0;
878     dmp->m_next = NULL;
879     MCLGET(dmp, M_DONTWAIT);
880     dmp->m_data += db->hdrlen;
881     wptr = mtod(dmp, u_char *);
882     space = M_TRAILINGSPACE(dmp) - PPP_HDRLEN + 1;
883 
884     /*
885      * Fill in the ppp header, but not the last byte of the protocol
886      * (that comes from the decompressed data).
887      */
888     wptr[0] = adrs;
889     wptr[1] = ctrl;
890     wptr[2] = 0;
891     wptr += PPP_HDRLEN - 1;
892 
893     ilen = len;
894     oldcode = CLEAR;
895     explen = 0;
896     for (;;) {
897 	if (len == 0) {
898 	    cmp = cmp->m_next;
899 	    if (!cmp)		/* quit at end of message */
900 		break;
901 	    rptr = mtod(cmp, u_char *);
902 	    len = cmp->m_len;
903 	    ilen += len;
904 	    continue;		/* handle 0-length buffers */
905 	}
906 
907 	/*
908 	 * Accumulate bytes until we have a complete code.
909 	 * Then get the next code, relying on the 32-bit,
910 	 * unsigned accm to mask the result.
911 	 */
912 	bitno -= 8;
913 	accm |= *rptr++ << bitno;
914 	--len;
915 	if (tgtbitno < bitno)
916 	    continue;
917 	incode = accm >> tgtbitno;
918 	accm <<= n_bits;
919 	bitno += n_bits;
920 
921 	if (incode == CLEAR) {
922 	    /*
923 	     * The dictionary must only be cleared at
924 	     * the end of a packet.  But there could be an
925 	     * empty mbuf at the end.
926 	     */
927 	    if (len > 0 || cmp->m_next != NULL) {
928 		while ((cmp = cmp->m_next) != NULL)
929 		    len += cmp->m_len;
930 		if (len > 0) {
931 		    m_freem(mret);
932 		    if (db->debug)
933 			printf("bsd_decomp%d: bad CLEAR\n", db->unit);
934 		    return DECOMP_FATALERROR;	/* probably a bug */
935 		}
936 	    }
937 	    bsd_clear(db);
938 	    explen = ilen = 0;
939 	    break;
940 	}
941 
942 	if (incode > max_ent + 2 || incode > db->maxmaxcode
943 	    || (incode > max_ent && oldcode == CLEAR)) {
944 	    m_freem(mret);
945 	    if (db->debug) {
946 		printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
947 		       db->unit, incode, oldcode);
948 		printf("max_ent=0x%x explen=%d seqno=%d\n",
949 		       max_ent, explen, db->seqno);
950 	    }
951 	    return DECOMP_FATALERROR;	/* probably a bug */
952 	}
953 
954 	/* Special case for KwKwK string. */
955 	if (incode > max_ent) {
956 	    finchar = oldcode;
957 	    extra = 1;
958 	} else {
959 	    finchar = incode;
960 	    extra = 0;
961 	}
962 
963 	codelen = db->lens[finchar];
964 	explen += codelen + extra;
965 	if (explen > db->mru + 1) {
966 	    m_freem(mret);
967 	    if (db->debug) {
968 		printf("bsd_decomp%d: ran out of mru\n", db->unit);
969 #ifdef DEBUG
970 		while ((cmp = cmp->m_next) != NULL)
971 		    len += cmp->m_len;
972 		printf("  len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
973 		       len, finchar, codelen, explen);
974 #endif
975 	    }
976 	    return DECOMP_FATALERROR;
977 	}
978 
979 	/*
980 	 * For simplicity, the decoded characters go in a single mbuf,
981 	 * so we allocate a single extra cluster mbuf if necessary.
982 	 */
983 	if ((space -= codelen + extra) < 0) {
984 	    dmp->m_len = wptr - mtod(dmp, u_char *);
985 	    MGET(m, M_DONTWAIT, MT_DATA);
986 	    if (m == NULL) {
987 		m_freem(mret);
988 		return DECOMP_ERROR;
989 	    }
990 	    m->m_len = 0;
991 	    m->m_next = NULL;
992 	    dmp->m_next = m;
993 	    MCLGET(m, M_DONTWAIT);
994 	    space = M_TRAILINGSPACE(m) - (codelen + extra);
995 	    if (space < 0) {
996 		/* now that's what I call *compression*. */
997 		m_freem(mret);
998 		return DECOMP_ERROR;
999 	    }
1000 	    dmp = m;
1001 	    wptr = mtod(dmp, u_char *);
1002 	}
1003 
1004 	/*
1005 	 * Decode this code and install it in the decompressed buffer.
1006 	 */
1007 	p = (wptr += codelen);
1008 	while (finchar > LAST) {
1009 	    dictp = &db->dict[db->dict[finchar].cptr];
1010 #ifdef DEBUG
1011 	    if (--codelen <= 0 || dictp->codem1 != finchar-1)
1012 		goto bad;
1013 #endif
1014 	    *--p = dictp->f.hs.suffix;
1015 	    finchar = dictp->f.hs.prefix;
1016 	}
1017 	*--p = finchar;
1018 
1019 #ifdef DEBUG
1020 	if (--codelen != 0)
1021 	    printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1022 		   db->unit, codelen, incode, max_ent);
1023 #endif
1024 
1025 	if (extra)		/* the KwKwK case again */
1026 	    *wptr++ = finchar;
1027 
1028 	/*
1029 	 * If not first code in a packet, and
1030 	 * if not out of code space, then allocate a new code.
1031 	 *
1032 	 * Keep the hash table correct so it can be used
1033 	 * with uncompressed packets.
1034 	 */
1035 	if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1036 	    struct bsd_dict *dictp2;
1037 	    u_int32_t fcode;
1038 	    u_int32_t hval, disp;
1039 
1040 	    fcode = BSD_KEY(oldcode,finchar);
1041 	    hval = BSD_HASH(oldcode,finchar,db->hshift);
1042 	    dictp = &db->dict[hval];
1043 
1044 	    /* look for a free hash table entry */
1045 	    if (dictp->codem1 < max_ent) {
1046 		disp = (hval == 0) ? 1 : hval;
1047 		do {
1048 		    hval += disp;
1049 		    if (hval >= db->hsize)
1050 			hval -= db->hsize;
1051 		    dictp = &db->dict[hval];
1052 		} while (dictp->codem1 < max_ent);
1053 	    }
1054 
1055 	    /*
1056 	     * Invalidate previous hash table entry
1057 	     * assigned this code, and then take it over
1058 	     */
1059 	    dictp2 = &db->dict[max_ent+1];
1060 	    if (db->dict[dictp2->cptr].codem1 == max_ent) {
1061 		db->dict[dictp2->cptr].codem1 = BADCODEM1;
1062 	    }
1063 	    dictp2->cptr = hval;
1064 	    dictp->codem1 = max_ent;
1065 	    dictp->f.fcode = fcode;
1066 
1067 	    db->max_ent = ++max_ent;
1068 	    db->lens[max_ent] = db->lens[oldcode]+1;
1069 
1070 	    /* Expand code size if needed. */
1071 	    if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1072 		db->n_bits = ++n_bits;
1073 		tgtbitno = 32-n_bits;
1074 	    }
1075 	}
1076 	oldcode = incode;
1077     }
1078     dmp->m_len = wptr - mtod(dmp, u_char *);
1079 
1080     /*
1081      * Keep the checkpoint right so that incompressible packets
1082      * clear the dictionary at the right times.
1083      */
1084     db->bytes_out += ilen;
1085     db->in_count += explen;
1086     if (bsd_check(db) && db->debug) {
1087 	printf("bsd_decomp%d: peer should have cleared dictionary\n",
1088 	       db->unit);
1089     }
1090 
1091     ++db->comp_count;
1092     db->comp_bytes += ilen + BSD_OVHD;
1093     ++db->uncomp_count;
1094     db->uncomp_bytes += explen;
1095 
1096     *dmpp = mret;
1097     return DECOMP_OK;
1098 
1099 #ifdef DEBUG
1100  bad:
1101     if (codelen <= 0) {
1102 	printf("bsd_decomp%d: fell off end of chain ", db->unit);
1103 	printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1104 	       incode, finchar, db->dict[finchar].cptr, max_ent);
1105     } else if (dictp->codem1 != finchar-1) {
1106 	printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1107 	       db->unit, incode, finchar);
1108 	printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
1109 	       db->dict[finchar].cptr, dictp->codem1);
1110     }
1111     m_freem(mret);
1112     return DECOMP_FATALERROR;
1113 #endif /* DEBUG */
1114 }
1115 #endif /* DO_BSD_COMPRESS */
1116