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