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