Home | History | Annotate | Line # | Download | only in net
bsd-comp.c revision 1.10
      1 /*	$NetBSD: bsd-comp.c,v 1.10 2001/11/12 23:49:34 lukem Exp $	*/
      2 /*	Id: bsd-comp.c,v 1.6 1996/08/28 06:31:58 paulus Exp 	*/
      3 
      4 /* Because this code is derived from the 4.3BSD compress source:
      5  *
      6  *
      7  * Copyright (c) 1985, 1986 The Regents of the University of California.
      8  * All rights reserved.
      9  *
     10  * This code is derived from software contributed to Berkeley by
     11  * James A. Woods, derived from original work by Spencer Thomas
     12  * and Joseph Orost.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  * 3. All advertising materials mentioning features or use of this software
     23  *    must display the following acknowledgement:
     24  *	This product includes software developed by the University of
     25  *	California, Berkeley and its contributors.
     26  * 4. Neither the name of the University nor the names of its contributors
     27  *    may be used to endorse or promote products derived from this software
     28  *    without specific prior written permission.
     29  *
     30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     40  * SUCH DAMAGE.
     41  */
     42 
     43 /*
     44  * This version is for use with mbufs on BSD-derived systems.
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: bsd-comp.c,v 1.10 2001/11/12 23:49:34 lukem Exp $");
     49 
     50 #include <sys/param.h>
     51 #include <sys/types.h>
     52 #include <sys/systm.h>
     53 #include <sys/mbuf.h>
     54 #include <sys/socket.h>
     55 #include <net/if.h>
     56 #include <net/if_types.h>
     57 #include <net/ppp_defs.h>
     58 #include <net/if_ppp.h>
     59 
     60 #define PACKETPTR	struct mbuf *
     61 #include <net/ppp-comp.h>
     62 
     63 #if DO_BSD_COMPRESS
     64 /*
     65  * PPP "BSD compress" compression
     66  *  The differences between this compression and the classic BSD LZW
     67  *  source are obvious from the requirement that the classic code worked
     68  *  with files while this handles arbitrarily long streams that
     69  *  are broken into packets.  They are:
     70  *
     71  *	When the code size expands, a block of junk is not emitted by
     72  *	    the compressor and not expected by the decompressor.
     73  *
     74  *	New codes are not necessarily assigned every time an old
     75  *	    code is output by the compressor.  This is because a packet
     76  *	    end forces a code to be emitted, but does not imply that a
     77  *	    new sequence has been seen.
     78  *
     79  *	The compression ratio is checked at the first end of a packet
     80  *	    after the appropriate gap.	Besides simplifying and speeding
     81  *	    things up, this makes it more likely that the transmitter
     82  *	    and receiver will agree when the dictionary is cleared when
     83  *	    compression is not going well.
     84  */
     85 
     86 /*
     87  * A dictionary for doing BSD compress.
     88  */
     89 struct bsd_db {
     90     int	    totlen;			/* length of this structure */
     91     u_int   hsize;			/* size of the hash table */
     92     u_char  hshift;			/* used in hash function */
     93     u_char  n_bits;			/* current bits/code */
     94     u_char  maxbits;
     95     u_char  debug;
     96     u_char  unit;
     97     u_int16_t seqno;			/* sequence # of next packet */
     98     u_int   hdrlen;			/* header length to preallocate */
     99     u_int   mru;
    100     u_int   maxmaxcode;			/* largest valid code */
    101     u_int   max_ent;			/* largest code in use */
    102     u_int   in_count;			/* uncompressed bytes, aged */
    103     u_int   bytes_out;			/* compressed bytes, aged */
    104     u_int   ratio;			/* recent compression ratio */
    105     u_int   checkpoint;			/* when to next check the ratio */
    106     u_int   clear_count;		/* times dictionary cleared */
    107     u_int   incomp_count;		/* incompressible packets */
    108     u_int   incomp_bytes;		/* incompressible bytes */
    109     u_int   uncomp_count;		/* uncompressed packets */
    110     u_int   uncomp_bytes;		/* uncompressed bytes */
    111     u_int   comp_count;			/* compressed packets */
    112     u_int   comp_bytes;			/* compressed bytes */
    113     u_int16_t *lens;			/* array of lengths of codes */
    114     struct bsd_dict {
    115 	union {				/* hash value */
    116 	    u_int32_t	fcode;
    117 	    struct {
    118 #if BYTE_ORDER == LITTLE_ENDIAN
    119 		u_int16_t prefix;	/* preceding code */
    120 		u_char	suffix;		/* last character of new code */
    121 		u_char	pad;
    122 #else
    123 		u_char	pad;
    124 		u_char	suffix;		/* last character of new code */
    125 		u_int16_t prefix;	/* preceding code */
    126 #endif
    127 	    } hs;
    128 	} f;
    129 	u_int16_t codem1;		/* output of hash table -1 */
    130 	u_int16_t cptr;			/* map code to hash table entry */
    131     } dict[1];
    132 };
    133 
    134 #define BSD_OVHD	2		/* BSD compress overhead/packet */
    135 #define BSD_INIT_BITS	BSD_MIN_BITS
    136 
    137 static void	*bsd_comp_alloc __P((u_char *options, int opt_len));
    138 static void	*bsd_decomp_alloc __P((u_char *options, int opt_len));
    139 static void	bsd_free __P((void *state));
    140 static int	bsd_comp_init __P((void *state, u_char *options, int opt_len,
    141 				   int unit, int hdrlen, int debug));
    142 static int	bsd_decomp_init __P((void *state, u_char *options, int opt_len,
    143 				     int unit, int hdrlen, int mru, int debug));
    144 static int	bsd_compress __P((void *state, struct mbuf **mret,
    145 				  struct mbuf *mp, int slen, int maxolen));
    146 static void	bsd_incomp __P((void *state, struct mbuf *dmsg));
    147 static int	bsd_decompress __P((void *state, struct mbuf *cmp,
    148 				    struct mbuf **dmpp));
    149 static void	bsd_reset __P((void *state));
    150 static void	bsd_comp_stats __P((void *state, struct compstat *stats));
    151 
    152 /*
    153  * Procedures exported to if_ppp.c.
    154  */
    155 struct compressor ppp_bsd_compress = {
    156     CI_BSD_COMPRESS,		/* compress_proto */
    157     bsd_comp_alloc,		/* comp_alloc */
    158     bsd_free,			/* comp_free */
    159     bsd_comp_init,		/* comp_init */
    160     bsd_reset,			/* comp_reset */
    161     bsd_compress,		/* compress */
    162     bsd_comp_stats,		/* comp_stat */
    163     bsd_decomp_alloc,		/* decomp_alloc */
    164     bsd_free,			/* decomp_free */
    165     bsd_decomp_init,		/* decomp_init */
    166     bsd_reset,			/* decomp_reset */
    167     bsd_decompress,		/* decompress */
    168     bsd_incomp,			/* incomp */
    169     bsd_comp_stats,		/* decomp_stat */
    170 };
    171 
    172 /*
    173  * the next two codes should not be changed lightly, as they must not
    174  * lie within the contiguous general code space.
    175  */
    176 #define CLEAR	256			/* table clear output code */
    177 #define FIRST	257			/* first free entry */
    178 #define LAST	255
    179 
    180 #define MAXCODE(b)	((1 << (b)) - 1)
    181 #define BADCODEM1	MAXCODE(BSD_MAX_BITS)
    182 
    183 #define BSD_HASH(prefix,suffix,hshift)	((((u_int32_t)(suffix)) << (hshift)) \
    184 					 ^ (u_int32_t)(prefix))
    185 #define BSD_KEY(prefix,suffix)		((((u_int32_t)(suffix)) << 16) \
    186 					 + (u_int32_t)(prefix))
    187 
    188 #define CHECK_GAP	10000		/* Ratio check interval */
    189 
    190 #define RATIO_SCALE_LOG	8
    191 #define RATIO_SCALE	(1<<RATIO_SCALE_LOG)
    192 #define RATIO_MAX	(0x7fffffff>>RATIO_SCALE_LOG)
    193 
    194 static void bsd_clear __P((struct bsd_db *));
    195 static int bsd_check __P((struct bsd_db *));
    196 static void *bsd_alloc __P((u_char *, int, int));
    197 static int bsd_init __P((struct bsd_db *, u_char *, int, int, int, int,
    198 			 int, int));
    199 
    200 /*
    201  * clear the dictionary
    202  */
    203 static void
    204 bsd_clear(db)
    205     struct bsd_db *db;
    206 {
    207     db->clear_count++;
    208     db->max_ent = FIRST-1;
    209     db->n_bits = BSD_INIT_BITS;
    210     db->ratio = 0;
    211     db->bytes_out = 0;
    212     db->in_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     db = malloc(newlen, M_DEVBUF, M_NOWAIT);
    356     if (!db)
    357 	return NULL;
    358     memset(db, 0, sizeof(*db) - sizeof(db->dict));
    359 
    360     if (!decomp) {
    361 	db->lens = NULL;
    362     } else {
    363 	db->lens = malloc((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->seqno++;
    721     ilen = 1;		/* count the protocol as 1 byte */
    722     rptr += PPP_HDRLEN;
    723     slen = dmsg->m_len - PPP_HDRLEN;
    724     for (;;) {
    725 	if (slen <= 0) {
    726 	    dmsg = dmsg->m_next;
    727 	    if (!dmsg)
    728 		break;
    729 	    rptr = mtod(dmsg, u_char *);
    730 	    slen = dmsg->m_len;
    731 	    continue;
    732 	}
    733 	ilen += slen;
    734 
    735 	do {
    736 	    c = *rptr++;
    737 	    fcode = BSD_KEY(ent, c);
    738 	    hval = BSD_HASH(ent, c, hshift);
    739 	    dictp = &db->dict[hval];
    740 
    741 	    /* validate and then check the entry */
    742 	    if (dictp->codem1 >= max_ent)
    743 		goto nomatch;
    744 	    if (dictp->f.fcode == fcode) {
    745 		ent = dictp->codem1+1;
    746 		continue;   /* found (prefix,suffix) */
    747 	    }
    748 
    749 	    /* continue probing until a match or invalid entry */
    750 	    disp = (hval == 0) ? 1 : hval;
    751 	    do {
    752 		hval += disp;
    753 		if (hval >= db->hsize)
    754 		    hval -= db->hsize;
    755 		dictp = &db->dict[hval];
    756 		if (dictp->codem1 >= max_ent)
    757 		    goto nomatch;
    758 	    } while (dictp->f.fcode != fcode);
    759 	    ent = dictp->codem1+1;
    760 	    continue;	/* finally found (prefix,suffix) */
    761 
    762 	nomatch:		/* output (count) the prefix */
    763 	    bitno += n_bits;
    764 
    765 	    /* code -> hashtable */
    766 	    if (max_ent < db->maxmaxcode) {
    767 		struct bsd_dict *dictp2;
    768 		/* expand code size if needed */
    769 		if (max_ent >= MAXCODE(n_bits))
    770 		    db->n_bits = ++n_bits;
    771 
    772 		/* Invalidate previous hash table entry
    773 		 * assigned this code, and then take it over.
    774 		 */
    775 		dictp2 = &db->dict[max_ent+1];
    776 		if (db->dict[dictp2->cptr].codem1 == max_ent)
    777 		    db->dict[dictp2->cptr].codem1 = BADCODEM1;
    778 		dictp2->cptr = hval;
    779 		dictp->codem1 = max_ent;
    780 		dictp->f.fcode = fcode;
    781 
    782 		db->max_ent = ++max_ent;
    783 		db->lens[max_ent] = db->lens[ent]+1;
    784 	    }
    785 	    ent = c;
    786 	} while (--slen != 0);
    787     }
    788     bitno += n_bits;		/* output (count) the last code */
    789     db->bytes_out += bitno/8;
    790     db->in_count += ilen;
    791     (void)bsd_check(db);
    792 
    793     ++db->incomp_count;
    794     db->incomp_bytes += ilen;
    795     ++db->uncomp_count;
    796     db->uncomp_bytes += ilen;
    797 
    798     /* Increase code size if we would have without the packet
    799      * boundary and as the decompressor will.
    800      */
    801     if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
    802 	db->n_bits++;
    803 }
    804 
    805 
    806 /*
    807  * Decompress "BSD Compress".
    808  *
    809  * Because of patent problems, we return DECOMP_ERROR for errors
    810  * found by inspecting the input data and for system problems, but
    811  * DECOMP_FATALERROR for any errors which could possibly be said to
    812  * be being detected "after" decompression.  For DECOMP_ERROR,
    813  * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
    814  * infringing a patent of Motorola's if we do, so we take CCP down
    815  * instead.
    816  *
    817  * Given that the frame has the correct sequence number and a good FCS,
    818  * errors such as invalid codes in the input most likely indicate a
    819  * bug, so we return DECOMP_FATALERROR for them in order to turn off
    820  * compression, even though they are detected by inspecting the input.
    821  */
    822 int
    823 bsd_decompress(state, cmp, dmpp)
    824     void *state;
    825     struct mbuf *cmp, **dmpp;
    826 {
    827     struct bsd_db *db = (struct bsd_db *) state;
    828     u_int max_ent = db->max_ent;
    829     u_int32_t accm = 0;
    830     u_int bitno = 32;		/* 1st valid bit in accm */
    831     u_int n_bits = db->n_bits;
    832     u_int tgtbitno = 32-n_bits;	/* bitno when we have a code */
    833     struct bsd_dict *dictp;
    834     int explen, i, seq, len;
    835     u_int incode, oldcode, finchar;
    836     u_char *p, *rptr, *wptr;
    837     struct mbuf *m, *dmp, *mret;
    838     int adrs, ctrl, ilen;
    839     int space, codelen, extra;
    840 
    841     /*
    842      * Save the address/control from the PPP header
    843      * and then get the sequence number.
    844      */
    845     *dmpp = NULL;
    846     rptr = mtod(cmp, u_char *);
    847     adrs = PPP_ADDRESS(rptr);
    848     ctrl = PPP_CONTROL(rptr);
    849     rptr += PPP_HDRLEN;
    850     len = cmp->m_len - PPP_HDRLEN;
    851     seq = 0;
    852     for (i = 0; i < 2; ++i) {
    853 	while (len <= 0) {
    854 	    cmp = cmp->m_next;
    855 	    if (cmp == NULL)
    856 		return DECOMP_ERROR;
    857 	    rptr = mtod(cmp, u_char *);
    858 	    len = cmp->m_len;
    859 	}
    860 	seq = (seq << 8) + *rptr++;
    861 	--len;
    862     }
    863 
    864     /*
    865      * Check the sequence number and give up if it differs from
    866      * the value we're expecting.
    867      */
    868     if (seq != db->seqno) {
    869 	if (db->debug)
    870 	    printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
    871 		   db->unit, seq, db->seqno - 1);
    872 	return DECOMP_ERROR;
    873     }
    874     ++db->seqno;
    875 
    876     /*
    877      * Allocate one mbuf to start with.
    878      */
    879     MGETHDR(dmp, M_DONTWAIT, MT_DATA);
    880     if (dmp == NULL)
    881 	return DECOMP_ERROR;
    882     mret = dmp;
    883     dmp->m_len = 0;
    884     dmp->m_next = NULL;
    885     MCLGET(dmp, M_DONTWAIT);
    886     dmp->m_data += db->hdrlen;
    887     wptr = mtod(dmp, u_char *);
    888     space = M_TRAILINGSPACE(dmp) - PPP_HDRLEN + 1;
    889 
    890     /*
    891      * Fill in the ppp header, but not the last byte of the protocol
    892      * (that comes from the decompressed data).
    893      */
    894     wptr[0] = adrs;
    895     wptr[1] = ctrl;
    896     wptr[2] = 0;
    897     wptr += PPP_HDRLEN - 1;
    898 
    899     ilen = len;
    900     oldcode = CLEAR;
    901     explen = 0;
    902     for (;;) {
    903 	if (len == 0) {
    904 	    cmp = cmp->m_next;
    905 	    if (!cmp)		/* quit at end of message */
    906 		break;
    907 	    rptr = mtod(cmp, u_char *);
    908 	    len = cmp->m_len;
    909 	    ilen += len;
    910 	    continue;		/* handle 0-length buffers */
    911 	}
    912 
    913 	/*
    914 	 * Accumulate bytes until we have a complete code.
    915 	 * Then get the next code, relying on the 32-bit,
    916 	 * unsigned accm to mask the result.
    917 	 */
    918 	bitno -= 8;
    919 	accm |= *rptr++ << bitno;
    920 	--len;
    921 	if (tgtbitno < bitno)
    922 	    continue;
    923 	incode = accm >> tgtbitno;
    924 	accm <<= n_bits;
    925 	bitno += n_bits;
    926 
    927 	if (incode == CLEAR) {
    928 	    /*
    929 	     * The dictionary must only be cleared at
    930 	     * the end of a packet.  But there could be an
    931 	     * empty mbuf at the end.
    932 	     */
    933 	    if (len > 0 || cmp->m_next != NULL) {
    934 		while ((cmp = cmp->m_next) != NULL)
    935 		    len += cmp->m_len;
    936 		if (len > 0) {
    937 		    m_freem(mret);
    938 		    if (db->debug)
    939 			printf("bsd_decomp%d: bad CLEAR\n", db->unit);
    940 		    return DECOMP_FATALERROR;	/* probably a bug */
    941 		}
    942 	    }
    943 	    bsd_clear(db);
    944 	    explen = ilen = 0;
    945 	    break;
    946 	}
    947 
    948 	if (incode > max_ent + 2 || incode > db->maxmaxcode
    949 	    || (incode > max_ent && oldcode == CLEAR)) {
    950 	    m_freem(mret);
    951 	    if (db->debug) {
    952 		printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
    953 		       db->unit, incode, oldcode);
    954 		printf("max_ent=0x%x explen=%d seqno=%d\n",
    955 		       max_ent, explen, db->seqno);
    956 	    }
    957 	    return DECOMP_FATALERROR;	/* probably a bug */
    958 	}
    959 
    960 	/* Special case for KwKwK string. */
    961 	if (incode > max_ent) {
    962 	    finchar = oldcode;
    963 	    extra = 1;
    964 	} else {
    965 	    finchar = incode;
    966 	    extra = 0;
    967 	}
    968 
    969 	codelen = db->lens[finchar];
    970 	explen += codelen + extra;
    971 	if (explen > db->mru + 1) {
    972 	    m_freem(mret);
    973 	    if (db->debug) {
    974 		printf("bsd_decomp%d: ran out of mru\n", db->unit);
    975 #ifdef DEBUG
    976 		while ((cmp = cmp->m_next) != NULL)
    977 		    len += cmp->m_len;
    978 		printf("  len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
    979 		       len, finchar, codelen, explen);
    980 #endif
    981 	    }
    982 	    return DECOMP_FATALERROR;
    983 	}
    984 
    985 	/*
    986 	 * For simplicity, the decoded characters go in a single mbuf,
    987 	 * so we allocate a single extra cluster mbuf if necessary.
    988 	 */
    989 	if ((space -= codelen + extra) < 0) {
    990 	    dmp->m_len = wptr - mtod(dmp, u_char *);
    991 	    MGET(m, M_DONTWAIT, MT_DATA);
    992 	    if (m == NULL) {
    993 		m_freem(mret);
    994 		return DECOMP_ERROR;
    995 	    }
    996 	    m->m_len = 0;
    997 	    m->m_next = NULL;
    998 	    dmp->m_next = m;
    999 	    MCLGET(m, M_DONTWAIT);
   1000 	    space = M_TRAILINGSPACE(m) - (codelen + extra);
   1001 	    if (space < 0) {
   1002 		/* now that's what I call *compression*. */
   1003 		m_freem(mret);
   1004 		return DECOMP_ERROR;
   1005 	    }
   1006 	    dmp = m;
   1007 	    wptr = mtod(dmp, u_char *);
   1008 	}
   1009 
   1010 	/*
   1011 	 * Decode this code and install it in the decompressed buffer.
   1012 	 */
   1013 	p = (wptr += codelen);
   1014 	while (finchar > LAST) {
   1015 	    dictp = &db->dict[db->dict[finchar].cptr];
   1016 #ifdef DEBUG
   1017 	    if (--codelen <= 0 || dictp->codem1 != finchar-1)
   1018 		goto bad;
   1019 #endif
   1020 	    *--p = dictp->f.hs.suffix;
   1021 	    finchar = dictp->f.hs.prefix;
   1022 	}
   1023 	*--p = finchar;
   1024 
   1025 #ifdef DEBUG
   1026 	if (--codelen != 0)
   1027 	    printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
   1028 		   db->unit, codelen, incode, max_ent);
   1029 #endif
   1030 
   1031 	if (extra)		/* the KwKwK case again */
   1032 	    *wptr++ = finchar;
   1033 
   1034 	/*
   1035 	 * If not first code in a packet, and
   1036 	 * if not out of code space, then allocate a new code.
   1037 	 *
   1038 	 * Keep the hash table correct so it can be used
   1039 	 * with uncompressed packets.
   1040 	 */
   1041 	if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
   1042 	    struct bsd_dict *dictp2;
   1043 	    u_int32_t fcode;
   1044 	    u_int32_t hval, disp;
   1045 
   1046 	    fcode = BSD_KEY(oldcode,finchar);
   1047 	    hval = BSD_HASH(oldcode,finchar,db->hshift);
   1048 	    dictp = &db->dict[hval];
   1049 
   1050 	    /* look for a free hash table entry */
   1051 	    if (dictp->codem1 < max_ent) {
   1052 		disp = (hval == 0) ? 1 : hval;
   1053 		do {
   1054 		    hval += disp;
   1055 		    if (hval >= db->hsize)
   1056 			hval -= db->hsize;
   1057 		    dictp = &db->dict[hval];
   1058 		} while (dictp->codem1 < max_ent);
   1059 	    }
   1060 
   1061 	    /*
   1062 	     * Invalidate previous hash table entry
   1063 	     * assigned this code, and then take it over
   1064 	     */
   1065 	    dictp2 = &db->dict[max_ent+1];
   1066 	    if (db->dict[dictp2->cptr].codem1 == max_ent) {
   1067 		db->dict[dictp2->cptr].codem1 = BADCODEM1;
   1068 	    }
   1069 	    dictp2->cptr = hval;
   1070 	    dictp->codem1 = max_ent;
   1071 	    dictp->f.fcode = fcode;
   1072 
   1073 	    db->max_ent = ++max_ent;
   1074 	    db->lens[max_ent] = db->lens[oldcode]+1;
   1075 
   1076 	    /* Expand code size if needed. */
   1077 	    if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
   1078 		db->n_bits = ++n_bits;
   1079 		tgtbitno = 32-n_bits;
   1080 	    }
   1081 	}
   1082 	oldcode = incode;
   1083     }
   1084     dmp->m_len = wptr - mtod(dmp, u_char *);
   1085 
   1086     /*
   1087      * Keep the checkpoint right so that incompressible packets
   1088      * clear the dictionary at the right times.
   1089      */
   1090     db->bytes_out += ilen;
   1091     db->in_count += explen;
   1092     if (bsd_check(db) && db->debug) {
   1093 	printf("bsd_decomp%d: peer should have cleared dictionary\n",
   1094 	       db->unit);
   1095     }
   1096 
   1097     ++db->comp_count;
   1098     db->comp_bytes += ilen + BSD_OVHD;
   1099     ++db->uncomp_count;
   1100     db->uncomp_bytes += explen;
   1101 
   1102     *dmpp = mret;
   1103     return DECOMP_OK;
   1104 
   1105 #ifdef DEBUG
   1106  bad:
   1107     if (codelen <= 0) {
   1108 	printf("bsd_decomp%d: fell off end of chain ", db->unit);
   1109 	printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
   1110 	       incode, finchar, db->dict[finchar].cptr, max_ent);
   1111     } else if (dictp->codem1 != finchar-1) {
   1112 	printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
   1113 	       db->unit, incode, finchar);
   1114 	printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
   1115 	       db->dict[finchar].cptr, dictp->codem1);
   1116     }
   1117     m_freem(mret);
   1118     return DECOMP_FATALERROR;
   1119 #endif /* DEBUG */
   1120 }
   1121 #endif /* DO_BSD_COMPRESS */
   1122