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