1*7c478bd9Sstevel@tonic-gate /* Because this code is derived from the 4.3BSD compress source: 2*7c478bd9Sstevel@tonic-gate * 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * Copyright (c) 1985, 1986 The Regents of the University of California. 5*7c478bd9Sstevel@tonic-gate * All rights reserved. 6*7c478bd9Sstevel@tonic-gate * 7*7c478bd9Sstevel@tonic-gate * This code is derived from software contributed to Berkeley by 8*7c478bd9Sstevel@tonic-gate * James A. Woods, derived from original work by Spencer Thomas 9*7c478bd9Sstevel@tonic-gate * and Joseph Orost. 10*7c478bd9Sstevel@tonic-gate * 11*7c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 12*7c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions 13*7c478bd9Sstevel@tonic-gate * are met: 14*7c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 15*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 16*7c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 17*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 18*7c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 19*7c478bd9Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 20*7c478bd9Sstevel@tonic-gate * must display the following acknowledgement: 21*7c478bd9Sstevel@tonic-gate * This product includes software developed by the University of 22*7c478bd9Sstevel@tonic-gate * California, Berkeley and its contributors. 23*7c478bd9Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 24*7c478bd9Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 25*7c478bd9Sstevel@tonic-gate * without specific prior written permission. 26*7c478bd9Sstevel@tonic-gate * 27*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28*7c478bd9Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29*7c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30*7c478bd9Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31*7c478bd9Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32*7c478bd9Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33*7c478bd9Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34*7c478bd9Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35*7c478bd9Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36*7c478bd9Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37*7c478bd9Sstevel@tonic-gate * SUCH DAMAGE. 38*7c478bd9Sstevel@tonic-gate */ 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate /* 41*7c478bd9Sstevel@tonic-gate * $Id: bsd-comp.c,v 1.3 1999/04/16 11:35:59 paulus Exp $ 42*7c478bd9Sstevel@tonic-gate */ 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 45*7c478bd9Sstevel@tonic-gate #include <stddef.h> 46*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 47*7c478bd9Sstevel@tonic-gate #ifdef PPP_DEFS_IN_NET 48*7c478bd9Sstevel@tonic-gate #include <net/ppp_defs.h> 49*7c478bd9Sstevel@tonic-gate #else 50*7c478bd9Sstevel@tonic-gate #include "ppp_defs.h" 51*7c478bd9Sstevel@tonic-gate #endif 52*7c478bd9Sstevel@tonic-gate #include "ppp-comp.h" 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate #if DO_BSD_COMPRESS 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate /* 57*7c478bd9Sstevel@tonic-gate * PPP "BSD compress" compression 58*7c478bd9Sstevel@tonic-gate * The differences between this compression and the classic BSD LZW 59*7c478bd9Sstevel@tonic-gate * source are obvious from the requirement that the classic code worked 60*7c478bd9Sstevel@tonic-gate * with files while this handles arbitrarily long streams that 61*7c478bd9Sstevel@tonic-gate * are broken into packets. They are: 62*7c478bd9Sstevel@tonic-gate * 63*7c478bd9Sstevel@tonic-gate * When the code size expands, a block of junk is not emitted by 64*7c478bd9Sstevel@tonic-gate * the compressor and not expected by the decompressor. 65*7c478bd9Sstevel@tonic-gate * 66*7c478bd9Sstevel@tonic-gate * New codes are not necessarily assigned every time an old 67*7c478bd9Sstevel@tonic-gate * code is output by the compressor. This is because a packet 68*7c478bd9Sstevel@tonic-gate * end forces a code to be emitted, but does not imply that a 69*7c478bd9Sstevel@tonic-gate * new sequence has been seen. 70*7c478bd9Sstevel@tonic-gate * 71*7c478bd9Sstevel@tonic-gate * The compression ratio is checked at the first end of a packet 72*7c478bd9Sstevel@tonic-gate * after the appropriate gap. Besides simplifying and speeding 73*7c478bd9Sstevel@tonic-gate * things up, this makes it more likely that the transmitter 74*7c478bd9Sstevel@tonic-gate * and receiver will agree when the dictionary is cleared when 75*7c478bd9Sstevel@tonic-gate * compression is not going well. 76*7c478bd9Sstevel@tonic-gate */ 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gate /* 79*7c478bd9Sstevel@tonic-gate * A dictionary for doing BSD compress. 80*7c478bd9Sstevel@tonic-gate */ 81*7c478bd9Sstevel@tonic-gate struct bsd_db { 82*7c478bd9Sstevel@tonic-gate int totlen; /* length of this structure */ 83*7c478bd9Sstevel@tonic-gate u_int hsize; /* size of the hash table */ 84*7c478bd9Sstevel@tonic-gate u_char hshift; /* used in hash function */ 85*7c478bd9Sstevel@tonic-gate u_char n_bits; /* current bits/code */ 86*7c478bd9Sstevel@tonic-gate u_char maxbits; 87*7c478bd9Sstevel@tonic-gate u_char debug; 88*7c478bd9Sstevel@tonic-gate u_char unit; 89*7c478bd9Sstevel@tonic-gate u_short seqno; /* sequence number of next packet */ 90*7c478bd9Sstevel@tonic-gate u_int hdrlen; /* header length to preallocate */ 91*7c478bd9Sstevel@tonic-gate u_int mru; 92*7c478bd9Sstevel@tonic-gate u_int maxmaxcode; /* largest valid code */ 93*7c478bd9Sstevel@tonic-gate u_int max_ent; /* largest code in use */ 94*7c478bd9Sstevel@tonic-gate u_int in_count; /* uncompressed bytes, aged */ 95*7c478bd9Sstevel@tonic-gate u_int bytes_out; /* compressed bytes, aged */ 96*7c478bd9Sstevel@tonic-gate u_int ratio; /* recent compression ratio */ 97*7c478bd9Sstevel@tonic-gate u_int checkpoint; /* when to next check the ratio */ 98*7c478bd9Sstevel@tonic-gate u_int clear_count; /* times dictionary cleared */ 99*7c478bd9Sstevel@tonic-gate u_int incomp_count; /* incompressible packets */ 100*7c478bd9Sstevel@tonic-gate u_int incomp_bytes; /* incompressible bytes */ 101*7c478bd9Sstevel@tonic-gate u_int uncomp_count; /* uncompressed packets */ 102*7c478bd9Sstevel@tonic-gate u_int uncomp_bytes; /* uncompressed bytes */ 103*7c478bd9Sstevel@tonic-gate u_int comp_count; /* compressed packets */ 104*7c478bd9Sstevel@tonic-gate u_int comp_bytes; /* compressed bytes */ 105*7c478bd9Sstevel@tonic-gate u_short *lens; /* array of lengths of codes */ 106*7c478bd9Sstevel@tonic-gate struct bsd_dict { 107*7c478bd9Sstevel@tonic-gate union { /* hash value */ 108*7c478bd9Sstevel@tonic-gate u_int32_t fcode; 109*7c478bd9Sstevel@tonic-gate struct { 110*7c478bd9Sstevel@tonic-gate #ifdef BSD_LITTLE_ENDIAN 111*7c478bd9Sstevel@tonic-gate u_short prefix; /* preceding code */ 112*7c478bd9Sstevel@tonic-gate u_char suffix; /* last character of new code */ 113*7c478bd9Sstevel@tonic-gate u_char pad; 114*7c478bd9Sstevel@tonic-gate #else 115*7c478bd9Sstevel@tonic-gate u_char pad; 116*7c478bd9Sstevel@tonic-gate u_char suffix; /* last character of new code */ 117*7c478bd9Sstevel@tonic-gate u_short prefix; /* preceding code */ 118*7c478bd9Sstevel@tonic-gate #endif 119*7c478bd9Sstevel@tonic-gate } hs; 120*7c478bd9Sstevel@tonic-gate } f; 121*7c478bd9Sstevel@tonic-gate u_short codem1; /* output of hash table -1 */ 122*7c478bd9Sstevel@tonic-gate u_short cptr; /* map code to hash table entry */ 123*7c478bd9Sstevel@tonic-gate } dict[1]; 124*7c478bd9Sstevel@tonic-gate }; 125*7c478bd9Sstevel@tonic-gate 126*7c478bd9Sstevel@tonic-gate #define BSD_OVHD 2 /* BSD compress overhead/packet */ 127*7c478bd9Sstevel@tonic-gate #define BSD_INIT_BITS BSD_MIN_BITS 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate static void *bsd_decomp_alloc __P((u_char *options, int opt_len)); 130*7c478bd9Sstevel@tonic-gate static void bsd_free __P((void *state)); 131*7c478bd9Sstevel@tonic-gate static int bsd_decomp_init __P((void *state, u_char *options, int opt_len, 132*7c478bd9Sstevel@tonic-gate int unit, int hdrlen, int mru, int debug)); 133*7c478bd9Sstevel@tonic-gate static void bsd_incomp __P((void *state, u_char *dmsg, int len)); 134*7c478bd9Sstevel@tonic-gate static int bsd_decompress __P((void *state, u_char *cmp, int inlen, 135*7c478bd9Sstevel@tonic-gate u_char *dmp, int *outlen)); 136*7c478bd9Sstevel@tonic-gate static void bsd_reset __P((void *state)); 137*7c478bd9Sstevel@tonic-gate static void bsd_comp_stats __P((void *state, struct compstat *stats)); 138*7c478bd9Sstevel@tonic-gate 139*7c478bd9Sstevel@tonic-gate /* 140*7c478bd9Sstevel@tonic-gate * Exported procedures. 141*7c478bd9Sstevel@tonic-gate */ 142*7c478bd9Sstevel@tonic-gate struct compressor ppp_bsd_compress = { 143*7c478bd9Sstevel@tonic-gate CI_BSD_COMPRESS, /* compress_proto */ 144*7c478bd9Sstevel@tonic-gate bsd_decomp_alloc, /* decomp_alloc */ 145*7c478bd9Sstevel@tonic-gate bsd_free, /* decomp_free */ 146*7c478bd9Sstevel@tonic-gate bsd_decomp_init, /* decomp_init */ 147*7c478bd9Sstevel@tonic-gate bsd_reset, /* decomp_reset */ 148*7c478bd9Sstevel@tonic-gate bsd_decompress, /* decompress */ 149*7c478bd9Sstevel@tonic-gate bsd_incomp, /* incomp */ 150*7c478bd9Sstevel@tonic-gate bsd_comp_stats, /* decomp_stat */ 151*7c478bd9Sstevel@tonic-gate }; 152*7c478bd9Sstevel@tonic-gate 153*7c478bd9Sstevel@tonic-gate /* 154*7c478bd9Sstevel@tonic-gate * the next two codes should not be changed lightly, as they must not 155*7c478bd9Sstevel@tonic-gate * lie within the contiguous general code space. 156*7c478bd9Sstevel@tonic-gate */ 157*7c478bd9Sstevel@tonic-gate #define CLEAR 256 /* table clear output code */ 158*7c478bd9Sstevel@tonic-gate #define FIRST 257 /* first free entry */ 159*7c478bd9Sstevel@tonic-gate #define LAST 255 160*7c478bd9Sstevel@tonic-gate 161*7c478bd9Sstevel@tonic-gate #define MAXCODE(b) ((1 << (b)) - 1) 162*7c478bd9Sstevel@tonic-gate #define BADCODEM1 MAXCODE(BSD_MAX_BITS) 163*7c478bd9Sstevel@tonic-gate 164*7c478bd9Sstevel@tonic-gate #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \ 165*7c478bd9Sstevel@tonic-gate ^ (u_int32_t)(prefix)) 166*7c478bd9Sstevel@tonic-gate #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \ 167*7c478bd9Sstevel@tonic-gate + (u_int32_t)(prefix)) 168*7c478bd9Sstevel@tonic-gate 169*7c478bd9Sstevel@tonic-gate #define CHECK_GAP 10000 /* Ratio check interval */ 170*7c478bd9Sstevel@tonic-gate 171*7c478bd9Sstevel@tonic-gate #define RATIO_SCALE_LOG 8 172*7c478bd9Sstevel@tonic-gate #define RATIO_SCALE (1<<RATIO_SCALE_LOG) 173*7c478bd9Sstevel@tonic-gate #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG) 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate /* 176*7c478bd9Sstevel@tonic-gate * clear the dictionary 177*7c478bd9Sstevel@tonic-gate */ 178*7c478bd9Sstevel@tonic-gate static void 179*7c478bd9Sstevel@tonic-gate bsd_clear(db) 180*7c478bd9Sstevel@tonic-gate struct bsd_db *db; 181*7c478bd9Sstevel@tonic-gate { 182*7c478bd9Sstevel@tonic-gate db->clear_count++; 183*7c478bd9Sstevel@tonic-gate db->max_ent = FIRST-1; 184*7c478bd9Sstevel@tonic-gate db->n_bits = BSD_INIT_BITS; 185*7c478bd9Sstevel@tonic-gate db->ratio = 0; 186*7c478bd9Sstevel@tonic-gate db->bytes_out = 0; 187*7c478bd9Sstevel@tonic-gate db->in_count = 0; 188*7c478bd9Sstevel@tonic-gate db->checkpoint = CHECK_GAP; 189*7c478bd9Sstevel@tonic-gate } 190*7c478bd9Sstevel@tonic-gate 191*7c478bd9Sstevel@tonic-gate /* 192*7c478bd9Sstevel@tonic-gate * If the dictionary is full, then see if it is time to reset it. 193*7c478bd9Sstevel@tonic-gate * 194*7c478bd9Sstevel@tonic-gate * Compute the compression ratio using fixed-point arithmetic 195*7c478bd9Sstevel@tonic-gate * with 8 fractional bits. 196*7c478bd9Sstevel@tonic-gate * 197*7c478bd9Sstevel@tonic-gate * Since we have an infinite stream instead of a single file, 198*7c478bd9Sstevel@tonic-gate * watch only the local compression ratio. 199*7c478bd9Sstevel@tonic-gate * 200*7c478bd9Sstevel@tonic-gate * Since both peers must reset the dictionary at the same time even in 201*7c478bd9Sstevel@tonic-gate * the absence of CLEAR codes (while packets are incompressible), they 202*7c478bd9Sstevel@tonic-gate * must compute the same ratio. 203*7c478bd9Sstevel@tonic-gate */ 204*7c478bd9Sstevel@tonic-gate static int /* 1=output CLEAR */ 205*7c478bd9Sstevel@tonic-gate bsd_check(db) 206*7c478bd9Sstevel@tonic-gate struct bsd_db *db; 207*7c478bd9Sstevel@tonic-gate { 208*7c478bd9Sstevel@tonic-gate u_int new_ratio; 209*7c478bd9Sstevel@tonic-gate 210*7c478bd9Sstevel@tonic-gate if (db->in_count >= db->checkpoint) { 211*7c478bd9Sstevel@tonic-gate /* age the ratio by limiting the size of the counts */ 212*7c478bd9Sstevel@tonic-gate if (db->in_count >= RATIO_MAX 213*7c478bd9Sstevel@tonic-gate || db->bytes_out >= RATIO_MAX) { 214*7c478bd9Sstevel@tonic-gate db->in_count -= db->in_count/4; 215*7c478bd9Sstevel@tonic-gate db->bytes_out -= db->bytes_out/4; 216*7c478bd9Sstevel@tonic-gate } 217*7c478bd9Sstevel@tonic-gate 218*7c478bd9Sstevel@tonic-gate db->checkpoint = db->in_count + CHECK_GAP; 219*7c478bd9Sstevel@tonic-gate 220*7c478bd9Sstevel@tonic-gate if (db->max_ent >= db->maxmaxcode) { 221*7c478bd9Sstevel@tonic-gate /* Reset the dictionary only if the ratio is worse, 222*7c478bd9Sstevel@tonic-gate * or if it looks as if it has been poisoned 223*7c478bd9Sstevel@tonic-gate * by incompressible data. 224*7c478bd9Sstevel@tonic-gate * 225*7c478bd9Sstevel@tonic-gate * This does not overflow, because 226*7c478bd9Sstevel@tonic-gate * db->in_count <= RATIO_MAX. 227*7c478bd9Sstevel@tonic-gate */ 228*7c478bd9Sstevel@tonic-gate new_ratio = db->in_count << RATIO_SCALE_LOG; 229*7c478bd9Sstevel@tonic-gate if (db->bytes_out != 0) 230*7c478bd9Sstevel@tonic-gate new_ratio /= db->bytes_out; 231*7c478bd9Sstevel@tonic-gate 232*7c478bd9Sstevel@tonic-gate if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) { 233*7c478bd9Sstevel@tonic-gate bsd_clear(db); 234*7c478bd9Sstevel@tonic-gate return 1; 235*7c478bd9Sstevel@tonic-gate } 236*7c478bd9Sstevel@tonic-gate db->ratio = new_ratio; 237*7c478bd9Sstevel@tonic-gate } 238*7c478bd9Sstevel@tonic-gate } 239*7c478bd9Sstevel@tonic-gate return 0; 240*7c478bd9Sstevel@tonic-gate } 241*7c478bd9Sstevel@tonic-gate 242*7c478bd9Sstevel@tonic-gate /* 243*7c478bd9Sstevel@tonic-gate * Return statistics. 244*7c478bd9Sstevel@tonic-gate */ 245*7c478bd9Sstevel@tonic-gate static void 246*7c478bd9Sstevel@tonic-gate bsd_comp_stats(state, stats) 247*7c478bd9Sstevel@tonic-gate void *state; 248*7c478bd9Sstevel@tonic-gate struct compstat *stats; 249*7c478bd9Sstevel@tonic-gate { 250*7c478bd9Sstevel@tonic-gate struct bsd_db *db = (struct bsd_db *) state; 251*7c478bd9Sstevel@tonic-gate u_int out; 252*7c478bd9Sstevel@tonic-gate 253*7c478bd9Sstevel@tonic-gate stats->unc_bytes = db->uncomp_bytes; 254*7c478bd9Sstevel@tonic-gate stats->unc_packets = db->uncomp_count; 255*7c478bd9Sstevel@tonic-gate stats->comp_bytes = db->comp_bytes; 256*7c478bd9Sstevel@tonic-gate stats->comp_packets = db->comp_count; 257*7c478bd9Sstevel@tonic-gate stats->inc_bytes = db->incomp_bytes; 258*7c478bd9Sstevel@tonic-gate stats->inc_packets = db->incomp_count; 259*7c478bd9Sstevel@tonic-gate stats->ratio = db->in_count; 260*7c478bd9Sstevel@tonic-gate out = db->bytes_out; 261*7c478bd9Sstevel@tonic-gate if (stats->ratio <= 0x7fffff) 262*7c478bd9Sstevel@tonic-gate stats->ratio <<= 8; 263*7c478bd9Sstevel@tonic-gate else 264*7c478bd9Sstevel@tonic-gate out >>= 8; 265*7c478bd9Sstevel@tonic-gate if (out != 0) 266*7c478bd9Sstevel@tonic-gate stats->ratio /= out; 267*7c478bd9Sstevel@tonic-gate } 268*7c478bd9Sstevel@tonic-gate 269*7c478bd9Sstevel@tonic-gate /* 270*7c478bd9Sstevel@tonic-gate * Reset state, as on a CCP ResetReq. 271*7c478bd9Sstevel@tonic-gate */ 272*7c478bd9Sstevel@tonic-gate static void 273*7c478bd9Sstevel@tonic-gate bsd_reset(state) 274*7c478bd9Sstevel@tonic-gate void *state; 275*7c478bd9Sstevel@tonic-gate { 276*7c478bd9Sstevel@tonic-gate struct bsd_db *db = (struct bsd_db *) state; 277*7c478bd9Sstevel@tonic-gate 278*7c478bd9Sstevel@tonic-gate db->seqno = 0; 279*7c478bd9Sstevel@tonic-gate bsd_clear(db); 280*7c478bd9Sstevel@tonic-gate db->clear_count = 0; 281*7c478bd9Sstevel@tonic-gate } 282*7c478bd9Sstevel@tonic-gate 283*7c478bd9Sstevel@tonic-gate /* 284*7c478bd9Sstevel@tonic-gate * Allocate space for a (de) compressor. 285*7c478bd9Sstevel@tonic-gate */ 286*7c478bd9Sstevel@tonic-gate static void * 287*7c478bd9Sstevel@tonic-gate bsd_alloc(options, opt_len, decomp) 288*7c478bd9Sstevel@tonic-gate u_char *options; 289*7c478bd9Sstevel@tonic-gate int opt_len, decomp; 290*7c478bd9Sstevel@tonic-gate { 291*7c478bd9Sstevel@tonic-gate int bits; 292*7c478bd9Sstevel@tonic-gate u_int newlen, hsize, hshift, maxmaxcode; 293*7c478bd9Sstevel@tonic-gate struct bsd_db *db; 294*7c478bd9Sstevel@tonic-gate 295*7c478bd9Sstevel@tonic-gate if (opt_len != 3 || options[0] != CI_BSD_COMPRESS || options[1] != 3 296*7c478bd9Sstevel@tonic-gate || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION) 297*7c478bd9Sstevel@tonic-gate return NULL; 298*7c478bd9Sstevel@tonic-gate 299*7c478bd9Sstevel@tonic-gate bits = BSD_NBITS(options[2]); 300*7c478bd9Sstevel@tonic-gate switch (bits) { 301*7c478bd9Sstevel@tonic-gate case 9: /* needs 82152 for both directions */ 302*7c478bd9Sstevel@tonic-gate case 10: /* needs 84144 */ 303*7c478bd9Sstevel@tonic-gate case 11: /* needs 88240 */ 304*7c478bd9Sstevel@tonic-gate case 12: /* needs 96432 */ 305*7c478bd9Sstevel@tonic-gate hsize = 5003; 306*7c478bd9Sstevel@tonic-gate hshift = 4; 307*7c478bd9Sstevel@tonic-gate break; 308*7c478bd9Sstevel@tonic-gate case 13: /* needs 176784 */ 309*7c478bd9Sstevel@tonic-gate hsize = 9001; 310*7c478bd9Sstevel@tonic-gate hshift = 5; 311*7c478bd9Sstevel@tonic-gate break; 312*7c478bd9Sstevel@tonic-gate case 14: /* needs 353744 */ 313*7c478bd9Sstevel@tonic-gate hsize = 18013; 314*7c478bd9Sstevel@tonic-gate hshift = 6; 315*7c478bd9Sstevel@tonic-gate break; 316*7c478bd9Sstevel@tonic-gate case 15: /* needs 691440 */ 317*7c478bd9Sstevel@tonic-gate hsize = 35023; 318*7c478bd9Sstevel@tonic-gate hshift = 7; 319*7c478bd9Sstevel@tonic-gate break; 320*7c478bd9Sstevel@tonic-gate case 16: /* needs 1366160--far too much, */ 321*7c478bd9Sstevel@tonic-gate /* hsize = 69001; */ /* and 69001 is too big for cptr */ 322*7c478bd9Sstevel@tonic-gate /* hshift = 8; */ /* in struct bsd_db */ 323*7c478bd9Sstevel@tonic-gate /* break; */ 324*7c478bd9Sstevel@tonic-gate default: 325*7c478bd9Sstevel@tonic-gate return NULL; 326*7c478bd9Sstevel@tonic-gate } 327*7c478bd9Sstevel@tonic-gate 328*7c478bd9Sstevel@tonic-gate maxmaxcode = MAXCODE(bits); 329*7c478bd9Sstevel@tonic-gate newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0])); 330*7c478bd9Sstevel@tonic-gate db = (struct bsd_db *) malloc(newlen); 331*7c478bd9Sstevel@tonic-gate if (!db) 332*7c478bd9Sstevel@tonic-gate return NULL; 333*7c478bd9Sstevel@tonic-gate memset(db, 0, sizeof(*db) - sizeof(db->dict)); 334*7c478bd9Sstevel@tonic-gate 335*7c478bd9Sstevel@tonic-gate if (!decomp) { 336*7c478bd9Sstevel@tonic-gate db->lens = NULL; 337*7c478bd9Sstevel@tonic-gate } else { 338*7c478bd9Sstevel@tonic-gate db->lens = (u_short *) malloc((maxmaxcode+1) * sizeof(db->lens[0])); 339*7c478bd9Sstevel@tonic-gate if (!db->lens) { 340*7c478bd9Sstevel@tonic-gate free(db); 341*7c478bd9Sstevel@tonic-gate return NULL; 342*7c478bd9Sstevel@tonic-gate } 343*7c478bd9Sstevel@tonic-gate } 344*7c478bd9Sstevel@tonic-gate 345*7c478bd9Sstevel@tonic-gate db->totlen = newlen; 346*7c478bd9Sstevel@tonic-gate db->hsize = hsize; 347*7c478bd9Sstevel@tonic-gate db->hshift = hshift; 348*7c478bd9Sstevel@tonic-gate db->maxmaxcode = maxmaxcode; 349*7c478bd9Sstevel@tonic-gate db->maxbits = bits; 350*7c478bd9Sstevel@tonic-gate 351*7c478bd9Sstevel@tonic-gate return (void *) db; 352*7c478bd9Sstevel@tonic-gate } 353*7c478bd9Sstevel@tonic-gate 354*7c478bd9Sstevel@tonic-gate static void 355*7c478bd9Sstevel@tonic-gate bsd_free(state) 356*7c478bd9Sstevel@tonic-gate void *state; 357*7c478bd9Sstevel@tonic-gate { 358*7c478bd9Sstevel@tonic-gate struct bsd_db *db = (struct bsd_db *) state; 359*7c478bd9Sstevel@tonic-gate 360*7c478bd9Sstevel@tonic-gate if (db->lens) 361*7c478bd9Sstevel@tonic-gate free(db->lens); 362*7c478bd9Sstevel@tonic-gate free(db); 363*7c478bd9Sstevel@tonic-gate } 364*7c478bd9Sstevel@tonic-gate 365*7c478bd9Sstevel@tonic-gate static void * 366*7c478bd9Sstevel@tonic-gate bsd_decomp_alloc(options, opt_len) 367*7c478bd9Sstevel@tonic-gate u_char *options; 368*7c478bd9Sstevel@tonic-gate int opt_len; 369*7c478bd9Sstevel@tonic-gate { 370*7c478bd9Sstevel@tonic-gate return bsd_alloc(options, opt_len, 1); 371*7c478bd9Sstevel@tonic-gate } 372*7c478bd9Sstevel@tonic-gate 373*7c478bd9Sstevel@tonic-gate /* 374*7c478bd9Sstevel@tonic-gate * Initialize the database. 375*7c478bd9Sstevel@tonic-gate */ 376*7c478bd9Sstevel@tonic-gate static int 377*7c478bd9Sstevel@tonic-gate bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp) 378*7c478bd9Sstevel@tonic-gate struct bsd_db *db; 379*7c478bd9Sstevel@tonic-gate u_char *options; 380*7c478bd9Sstevel@tonic-gate int opt_len, unit, hdrlen, mru, debug, decomp; 381*7c478bd9Sstevel@tonic-gate { 382*7c478bd9Sstevel@tonic-gate int i; 383*7c478bd9Sstevel@tonic-gate 384*7c478bd9Sstevel@tonic-gate if (opt_len < CILEN_BSD_COMPRESS 385*7c478bd9Sstevel@tonic-gate || options[0] != CI_BSD_COMPRESS || options[1] != CILEN_BSD_COMPRESS 386*7c478bd9Sstevel@tonic-gate || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION 387*7c478bd9Sstevel@tonic-gate || BSD_NBITS(options[2]) != db->maxbits 388*7c478bd9Sstevel@tonic-gate || decomp && db->lens == NULL) 389*7c478bd9Sstevel@tonic-gate return 0; 390*7c478bd9Sstevel@tonic-gate 391*7c478bd9Sstevel@tonic-gate if (decomp) { 392*7c478bd9Sstevel@tonic-gate i = LAST+1; 393*7c478bd9Sstevel@tonic-gate while (i != 0) 394*7c478bd9Sstevel@tonic-gate db->lens[--i] = 1; 395*7c478bd9Sstevel@tonic-gate } 396*7c478bd9Sstevel@tonic-gate i = db->hsize; 397*7c478bd9Sstevel@tonic-gate while (i != 0) { 398*7c478bd9Sstevel@tonic-gate db->dict[--i].codem1 = BADCODEM1; 399*7c478bd9Sstevel@tonic-gate db->dict[i].cptr = 0; 400*7c478bd9Sstevel@tonic-gate } 401*7c478bd9Sstevel@tonic-gate 402*7c478bd9Sstevel@tonic-gate db->unit = unit; 403*7c478bd9Sstevel@tonic-gate db->hdrlen = hdrlen; 404*7c478bd9Sstevel@tonic-gate db->mru = mru; 405*7c478bd9Sstevel@tonic-gate if (debug) 406*7c478bd9Sstevel@tonic-gate db->debug = 1; 407*7c478bd9Sstevel@tonic-gate 408*7c478bd9Sstevel@tonic-gate bsd_reset(db); 409*7c478bd9Sstevel@tonic-gate 410*7c478bd9Sstevel@tonic-gate return 1; 411*7c478bd9Sstevel@tonic-gate } 412*7c478bd9Sstevel@tonic-gate 413*7c478bd9Sstevel@tonic-gate static int 414*7c478bd9Sstevel@tonic-gate bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug) 415*7c478bd9Sstevel@tonic-gate void *state; 416*7c478bd9Sstevel@tonic-gate u_char *options; 417*7c478bd9Sstevel@tonic-gate int opt_len, unit, hdrlen, mru, debug; 418*7c478bd9Sstevel@tonic-gate { 419*7c478bd9Sstevel@tonic-gate return bsd_init((struct bsd_db *) state, options, opt_len, 420*7c478bd9Sstevel@tonic-gate unit, hdrlen, mru, debug, 1); 421*7c478bd9Sstevel@tonic-gate } 422*7c478bd9Sstevel@tonic-gate 423*7c478bd9Sstevel@tonic-gate 424*7c478bd9Sstevel@tonic-gate /* 425*7c478bd9Sstevel@tonic-gate * Update the "BSD Compress" dictionary on the receiver for 426*7c478bd9Sstevel@tonic-gate * incompressible data by pretending to compress the incoming data. 427*7c478bd9Sstevel@tonic-gate */ 428*7c478bd9Sstevel@tonic-gate static void 429*7c478bd9Sstevel@tonic-gate bsd_incomp(state, dmsg, mlen) 430*7c478bd9Sstevel@tonic-gate void *state; 431*7c478bd9Sstevel@tonic-gate u_char *dmsg; 432*7c478bd9Sstevel@tonic-gate int mlen; 433*7c478bd9Sstevel@tonic-gate { 434*7c478bd9Sstevel@tonic-gate struct bsd_db *db = (struct bsd_db *) state; 435*7c478bd9Sstevel@tonic-gate u_int hshift = db->hshift; 436*7c478bd9Sstevel@tonic-gate u_int max_ent = db->max_ent; 437*7c478bd9Sstevel@tonic-gate u_int n_bits = db->n_bits; 438*7c478bd9Sstevel@tonic-gate struct bsd_dict *dictp; 439*7c478bd9Sstevel@tonic-gate u_int32_t fcode; 440*7c478bd9Sstevel@tonic-gate u_char c; 441*7c478bd9Sstevel@tonic-gate long hval, disp; 442*7c478bd9Sstevel@tonic-gate int slen, ilen; 443*7c478bd9Sstevel@tonic-gate u_int bitno = 7; 444*7c478bd9Sstevel@tonic-gate u_char *rptr; 445*7c478bd9Sstevel@tonic-gate u_int ent; 446*7c478bd9Sstevel@tonic-gate 447*7c478bd9Sstevel@tonic-gate rptr = dmsg; 448*7c478bd9Sstevel@tonic-gate ent = rptr[0]; /* get the protocol */ 449*7c478bd9Sstevel@tonic-gate if (ent == 0) { 450*7c478bd9Sstevel@tonic-gate ++rptr; 451*7c478bd9Sstevel@tonic-gate --mlen; 452*7c478bd9Sstevel@tonic-gate ent = rptr[0]; 453*7c478bd9Sstevel@tonic-gate } 454*7c478bd9Sstevel@tonic-gate if ((ent & 1) == 0 || ent < 0x21 || ent > 0xf9) 455*7c478bd9Sstevel@tonic-gate return; 456*7c478bd9Sstevel@tonic-gate 457*7c478bd9Sstevel@tonic-gate db->seqno++; 458*7c478bd9Sstevel@tonic-gate ilen = 1; /* count the protocol as 1 byte */ 459*7c478bd9Sstevel@tonic-gate ++rptr; 460*7c478bd9Sstevel@tonic-gate slen = dmsg + mlen - rptr; 461*7c478bd9Sstevel@tonic-gate ilen += slen; 462*7c478bd9Sstevel@tonic-gate for (; slen > 0; --slen) { 463*7c478bd9Sstevel@tonic-gate c = *rptr++; 464*7c478bd9Sstevel@tonic-gate fcode = BSD_KEY(ent, c); 465*7c478bd9Sstevel@tonic-gate hval = BSD_HASH(ent, c, hshift); 466*7c478bd9Sstevel@tonic-gate dictp = &db->dict[hval]; 467*7c478bd9Sstevel@tonic-gate 468*7c478bd9Sstevel@tonic-gate /* validate and then check the entry */ 469*7c478bd9Sstevel@tonic-gate if (dictp->codem1 >= max_ent) 470*7c478bd9Sstevel@tonic-gate goto nomatch; 471*7c478bd9Sstevel@tonic-gate if (dictp->f.fcode == fcode) { 472*7c478bd9Sstevel@tonic-gate ent = dictp->codem1+1; 473*7c478bd9Sstevel@tonic-gate continue; /* found (prefix,suffix) */ 474*7c478bd9Sstevel@tonic-gate } 475*7c478bd9Sstevel@tonic-gate 476*7c478bd9Sstevel@tonic-gate /* continue probing until a match or invalid entry */ 477*7c478bd9Sstevel@tonic-gate disp = (hval == 0) ? 1 : hval; 478*7c478bd9Sstevel@tonic-gate do { 479*7c478bd9Sstevel@tonic-gate hval += disp; 480*7c478bd9Sstevel@tonic-gate if (hval >= db->hsize) 481*7c478bd9Sstevel@tonic-gate hval -= db->hsize; 482*7c478bd9Sstevel@tonic-gate dictp = &db->dict[hval]; 483*7c478bd9Sstevel@tonic-gate if (dictp->codem1 >= max_ent) 484*7c478bd9Sstevel@tonic-gate goto nomatch; 485*7c478bd9Sstevel@tonic-gate } while (dictp->f.fcode != fcode); 486*7c478bd9Sstevel@tonic-gate ent = dictp->codem1+1; 487*7c478bd9Sstevel@tonic-gate continue; /* finally found (prefix,suffix) */ 488*7c478bd9Sstevel@tonic-gate 489*7c478bd9Sstevel@tonic-gate nomatch: /* output (count) the prefix */ 490*7c478bd9Sstevel@tonic-gate bitno += n_bits; 491*7c478bd9Sstevel@tonic-gate 492*7c478bd9Sstevel@tonic-gate /* code -> hashtable */ 493*7c478bd9Sstevel@tonic-gate if (max_ent < db->maxmaxcode) { 494*7c478bd9Sstevel@tonic-gate struct bsd_dict *dictp2; 495*7c478bd9Sstevel@tonic-gate /* expand code size if needed */ 496*7c478bd9Sstevel@tonic-gate if (max_ent >= MAXCODE(n_bits)) 497*7c478bd9Sstevel@tonic-gate db->n_bits = ++n_bits; 498*7c478bd9Sstevel@tonic-gate 499*7c478bd9Sstevel@tonic-gate /* Invalidate previous hash table entry 500*7c478bd9Sstevel@tonic-gate * assigned this code, and then take it over. 501*7c478bd9Sstevel@tonic-gate */ 502*7c478bd9Sstevel@tonic-gate dictp2 = &db->dict[max_ent+1]; 503*7c478bd9Sstevel@tonic-gate if (db->dict[dictp2->cptr].codem1 == max_ent) 504*7c478bd9Sstevel@tonic-gate db->dict[dictp2->cptr].codem1 = BADCODEM1; 505*7c478bd9Sstevel@tonic-gate dictp2->cptr = hval; 506*7c478bd9Sstevel@tonic-gate dictp->codem1 = max_ent; 507*7c478bd9Sstevel@tonic-gate dictp->f.fcode = fcode; 508*7c478bd9Sstevel@tonic-gate 509*7c478bd9Sstevel@tonic-gate db->max_ent = ++max_ent; 510*7c478bd9Sstevel@tonic-gate db->lens[max_ent] = db->lens[ent]+1; 511*7c478bd9Sstevel@tonic-gate } 512*7c478bd9Sstevel@tonic-gate ent = c; 513*7c478bd9Sstevel@tonic-gate } 514*7c478bd9Sstevel@tonic-gate bitno += n_bits; /* output (count) the last code */ 515*7c478bd9Sstevel@tonic-gate db->bytes_out += bitno/8; 516*7c478bd9Sstevel@tonic-gate db->in_count += ilen; 517*7c478bd9Sstevel@tonic-gate (void)bsd_check(db); 518*7c478bd9Sstevel@tonic-gate 519*7c478bd9Sstevel@tonic-gate ++db->incomp_count; 520*7c478bd9Sstevel@tonic-gate db->incomp_bytes += ilen; 521*7c478bd9Sstevel@tonic-gate ++db->uncomp_count; 522*7c478bd9Sstevel@tonic-gate db->uncomp_bytes += ilen; 523*7c478bd9Sstevel@tonic-gate 524*7c478bd9Sstevel@tonic-gate /* Increase code size if we would have without the packet 525*7c478bd9Sstevel@tonic-gate * boundary and as the decompressor will. 526*7c478bd9Sstevel@tonic-gate */ 527*7c478bd9Sstevel@tonic-gate if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) 528*7c478bd9Sstevel@tonic-gate db->n_bits++; 529*7c478bd9Sstevel@tonic-gate } 530*7c478bd9Sstevel@tonic-gate 531*7c478bd9Sstevel@tonic-gate 532*7c478bd9Sstevel@tonic-gate /* 533*7c478bd9Sstevel@tonic-gate * Decompress "BSD Compress" 534*7c478bd9Sstevel@tonic-gate * 535*7c478bd9Sstevel@tonic-gate * Because of patent problems, we return DECOMP_ERROR for errors 536*7c478bd9Sstevel@tonic-gate * found by inspecting the input data and for system problems, but 537*7c478bd9Sstevel@tonic-gate * DECOMP_FATALERROR for any errors which could possibly be said to 538*7c478bd9Sstevel@tonic-gate * be being detected "after" decompression. For DECOMP_ERROR, 539*7c478bd9Sstevel@tonic-gate * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be 540*7c478bd9Sstevel@tonic-gate * infringing a patent of Motorola's if we do, so we take CCP down 541*7c478bd9Sstevel@tonic-gate * instead. 542*7c478bd9Sstevel@tonic-gate * 543*7c478bd9Sstevel@tonic-gate * Given that the frame has the correct sequence number and a good FCS, 544*7c478bd9Sstevel@tonic-gate * errors such as invalid codes in the input most likely indicate a 545*7c478bd9Sstevel@tonic-gate * bug, so we return DECOMP_FATALERROR for them in order to turn off 546*7c478bd9Sstevel@tonic-gate * compression, even though they are detected by inspecting the input. 547*7c478bd9Sstevel@tonic-gate */ 548*7c478bd9Sstevel@tonic-gate static int 549*7c478bd9Sstevel@tonic-gate bsd_decompress(state, cmsg, inlen, dmp, outlenp) 550*7c478bd9Sstevel@tonic-gate void *state; 551*7c478bd9Sstevel@tonic-gate u_char *cmsg, *dmp; 552*7c478bd9Sstevel@tonic-gate int inlen, *outlenp; 553*7c478bd9Sstevel@tonic-gate { 554*7c478bd9Sstevel@tonic-gate struct bsd_db *db = (struct bsd_db *) state; 555*7c478bd9Sstevel@tonic-gate u_int max_ent = db->max_ent; 556*7c478bd9Sstevel@tonic-gate u_int32_t accm = 0; 557*7c478bd9Sstevel@tonic-gate u_int bitno = 32; /* 1st valid bit in accm */ 558*7c478bd9Sstevel@tonic-gate u_int n_bits = db->n_bits; 559*7c478bd9Sstevel@tonic-gate u_int tgtbitno = 32-n_bits; /* bitno when we have a code */ 560*7c478bd9Sstevel@tonic-gate struct bsd_dict *dictp; 561*7c478bd9Sstevel@tonic-gate int explen, i, seq, len; 562*7c478bd9Sstevel@tonic-gate u_int incode, oldcode, finchar; 563*7c478bd9Sstevel@tonic-gate u_char *p, *rptr, *wptr; 564*7c478bd9Sstevel@tonic-gate int ilen; 565*7c478bd9Sstevel@tonic-gate int dlen, space, codelen, extra; 566*7c478bd9Sstevel@tonic-gate 567*7c478bd9Sstevel@tonic-gate rptr = cmsg; 568*7c478bd9Sstevel@tonic-gate if (*rptr == 0) 569*7c478bd9Sstevel@tonic-gate ++rptr; 570*7c478bd9Sstevel@tonic-gate ++rptr; /* skip protocol (assumed 0xfd) */ 571*7c478bd9Sstevel@tonic-gate seq = (rptr[0] << 8) + rptr[1]; 572*7c478bd9Sstevel@tonic-gate rptr += BSD_OVHD; 573*7c478bd9Sstevel@tonic-gate ilen = len = cmsg + inlen - rptr; 574*7c478bd9Sstevel@tonic-gate 575*7c478bd9Sstevel@tonic-gate /* 576*7c478bd9Sstevel@tonic-gate * Check the sequence number and give up if it is not what we expect. 577*7c478bd9Sstevel@tonic-gate */ 578*7c478bd9Sstevel@tonic-gate if (seq != db->seqno++) { 579*7c478bd9Sstevel@tonic-gate if (db->debug) 580*7c478bd9Sstevel@tonic-gate printf("bsd_decomp%d: bad sequence # %d, expected %d\n", 581*7c478bd9Sstevel@tonic-gate db->unit, seq, db->seqno - 1); 582*7c478bd9Sstevel@tonic-gate return DECOMP_ERROR; 583*7c478bd9Sstevel@tonic-gate } 584*7c478bd9Sstevel@tonic-gate 585*7c478bd9Sstevel@tonic-gate wptr = dmp + db->hdrlen; 586*7c478bd9Sstevel@tonic-gate 587*7c478bd9Sstevel@tonic-gate oldcode = CLEAR; 588*7c478bd9Sstevel@tonic-gate explen = 0; 589*7c478bd9Sstevel@tonic-gate while (len > 0) { 590*7c478bd9Sstevel@tonic-gate /* 591*7c478bd9Sstevel@tonic-gate * Accumulate bytes until we have a complete code. 592*7c478bd9Sstevel@tonic-gate * Then get the next code, relying on the 32-bit, 593*7c478bd9Sstevel@tonic-gate * unsigned accm to mask the result. 594*7c478bd9Sstevel@tonic-gate */ 595*7c478bd9Sstevel@tonic-gate bitno -= 8; 596*7c478bd9Sstevel@tonic-gate accm |= *rptr++ << bitno; 597*7c478bd9Sstevel@tonic-gate --len; 598*7c478bd9Sstevel@tonic-gate if (tgtbitno < bitno) 599*7c478bd9Sstevel@tonic-gate continue; 600*7c478bd9Sstevel@tonic-gate incode = accm >> tgtbitno; 601*7c478bd9Sstevel@tonic-gate accm <<= n_bits; 602*7c478bd9Sstevel@tonic-gate bitno += n_bits; 603*7c478bd9Sstevel@tonic-gate 604*7c478bd9Sstevel@tonic-gate if (incode == CLEAR) { 605*7c478bd9Sstevel@tonic-gate /* 606*7c478bd9Sstevel@tonic-gate * The dictionary must only be cleared at 607*7c478bd9Sstevel@tonic-gate * the end of a packet. But there could be an 608*7c478bd9Sstevel@tonic-gate * empty message block at the end. 609*7c478bd9Sstevel@tonic-gate */ 610*7c478bd9Sstevel@tonic-gate if (len > 0) { 611*7c478bd9Sstevel@tonic-gate if (db->debug) 612*7c478bd9Sstevel@tonic-gate printf("bsd_decomp%d: bad CLEAR\n", db->unit); 613*7c478bd9Sstevel@tonic-gate return DECOMP_FATALERROR; 614*7c478bd9Sstevel@tonic-gate } 615*7c478bd9Sstevel@tonic-gate bsd_clear(db); 616*7c478bd9Sstevel@tonic-gate explen = ilen = 0; 617*7c478bd9Sstevel@tonic-gate break; 618*7c478bd9Sstevel@tonic-gate } 619*7c478bd9Sstevel@tonic-gate 620*7c478bd9Sstevel@tonic-gate if (incode > max_ent + 2 || incode > db->maxmaxcode 621*7c478bd9Sstevel@tonic-gate || incode > max_ent && oldcode == CLEAR) { 622*7c478bd9Sstevel@tonic-gate if (db->debug) { 623*7c478bd9Sstevel@tonic-gate printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ", 624*7c478bd9Sstevel@tonic-gate db->unit, incode, oldcode); 625*7c478bd9Sstevel@tonic-gate printf("max_ent=0x%x dlen=%d seqno=%d\n", 626*7c478bd9Sstevel@tonic-gate max_ent, dlen, db->seqno); 627*7c478bd9Sstevel@tonic-gate } 628*7c478bd9Sstevel@tonic-gate return DECOMP_FATALERROR; /* probably a bug */ 629*7c478bd9Sstevel@tonic-gate } 630*7c478bd9Sstevel@tonic-gate 631*7c478bd9Sstevel@tonic-gate /* Special case for KwKwK string. */ 632*7c478bd9Sstevel@tonic-gate if (incode > max_ent) { 633*7c478bd9Sstevel@tonic-gate finchar = oldcode; 634*7c478bd9Sstevel@tonic-gate extra = 1; 635*7c478bd9Sstevel@tonic-gate } else { 636*7c478bd9Sstevel@tonic-gate finchar = incode; 637*7c478bd9Sstevel@tonic-gate extra = 0; 638*7c478bd9Sstevel@tonic-gate } 639*7c478bd9Sstevel@tonic-gate 640*7c478bd9Sstevel@tonic-gate codelen = db->lens[finchar]; 641*7c478bd9Sstevel@tonic-gate explen += codelen + extra; 642*7c478bd9Sstevel@tonic-gate if (explen > db->mru + 1) { 643*7c478bd9Sstevel@tonic-gate if (db->debug) 644*7c478bd9Sstevel@tonic-gate printf("bsd_decomp%d: ran out of mru\n", db->unit); 645*7c478bd9Sstevel@tonic-gate return DECOMP_FATALERROR; 646*7c478bd9Sstevel@tonic-gate } 647*7c478bd9Sstevel@tonic-gate 648*7c478bd9Sstevel@tonic-gate /* 649*7c478bd9Sstevel@tonic-gate * Decode this code and install it in the decompressed buffer. 650*7c478bd9Sstevel@tonic-gate */ 651*7c478bd9Sstevel@tonic-gate p = (wptr += codelen); 652*7c478bd9Sstevel@tonic-gate while (finchar > LAST) { 653*7c478bd9Sstevel@tonic-gate dictp = &db->dict[db->dict[finchar].cptr]; 654*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 655*7c478bd9Sstevel@tonic-gate --codelen; 656*7c478bd9Sstevel@tonic-gate if (codelen <= 0) { 657*7c478bd9Sstevel@tonic-gate printf("bsd_decomp%d: fell off end of chain ", db->unit); 658*7c478bd9Sstevel@tonic-gate printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n", 659*7c478bd9Sstevel@tonic-gate incode, finchar, db->dict[finchar].cptr, max_ent); 660*7c478bd9Sstevel@tonic-gate return DECOMP_FATALERROR; 661*7c478bd9Sstevel@tonic-gate } 662*7c478bd9Sstevel@tonic-gate if (dictp->codem1 != finchar-1) { 663*7c478bd9Sstevel@tonic-gate printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ", 664*7c478bd9Sstevel@tonic-gate db->unit, incode, finchar); 665*7c478bd9Sstevel@tonic-gate printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode, 666*7c478bd9Sstevel@tonic-gate db->dict[finchar].cptr, dictp->codem1); 667*7c478bd9Sstevel@tonic-gate return DECOMP_FATALERROR; 668*7c478bd9Sstevel@tonic-gate } 669*7c478bd9Sstevel@tonic-gate #endif 670*7c478bd9Sstevel@tonic-gate *--p = dictp->f.hs.suffix; 671*7c478bd9Sstevel@tonic-gate finchar = dictp->f.hs.prefix; 672*7c478bd9Sstevel@tonic-gate } 673*7c478bd9Sstevel@tonic-gate *--p = finchar; 674*7c478bd9Sstevel@tonic-gate 675*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 676*7c478bd9Sstevel@tonic-gate if (--codelen != 0) 677*7c478bd9Sstevel@tonic-gate printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n", 678*7c478bd9Sstevel@tonic-gate db->unit, codelen, incode, max_ent); 679*7c478bd9Sstevel@tonic-gate #endif 680*7c478bd9Sstevel@tonic-gate 681*7c478bd9Sstevel@tonic-gate if (extra) /* the KwKwK case again */ 682*7c478bd9Sstevel@tonic-gate *wptr++ = finchar; 683*7c478bd9Sstevel@tonic-gate 684*7c478bd9Sstevel@tonic-gate /* 685*7c478bd9Sstevel@tonic-gate * If not first code in a packet, and 686*7c478bd9Sstevel@tonic-gate * if not out of code space, then allocate a new code. 687*7c478bd9Sstevel@tonic-gate * 688*7c478bd9Sstevel@tonic-gate * Keep the hash table correct so it can be used 689*7c478bd9Sstevel@tonic-gate * with uncompressed packets. 690*7c478bd9Sstevel@tonic-gate */ 691*7c478bd9Sstevel@tonic-gate if (oldcode != CLEAR && max_ent < db->maxmaxcode) { 692*7c478bd9Sstevel@tonic-gate struct bsd_dict *dictp2; 693*7c478bd9Sstevel@tonic-gate u_int32_t fcode; 694*7c478bd9Sstevel@tonic-gate int hval, disp; 695*7c478bd9Sstevel@tonic-gate 696*7c478bd9Sstevel@tonic-gate fcode = BSD_KEY(oldcode,finchar); 697*7c478bd9Sstevel@tonic-gate hval = BSD_HASH(oldcode,finchar,db->hshift); 698*7c478bd9Sstevel@tonic-gate dictp = &db->dict[hval]; 699*7c478bd9Sstevel@tonic-gate 700*7c478bd9Sstevel@tonic-gate /* look for a free hash table entry */ 701*7c478bd9Sstevel@tonic-gate if (dictp->codem1 < max_ent) { 702*7c478bd9Sstevel@tonic-gate disp = (hval == 0) ? 1 : hval; 703*7c478bd9Sstevel@tonic-gate do { 704*7c478bd9Sstevel@tonic-gate hval += disp; 705*7c478bd9Sstevel@tonic-gate if (hval >= db->hsize) 706*7c478bd9Sstevel@tonic-gate hval -= db->hsize; 707*7c478bd9Sstevel@tonic-gate dictp = &db->dict[hval]; 708*7c478bd9Sstevel@tonic-gate } while (dictp->codem1 < max_ent); 709*7c478bd9Sstevel@tonic-gate } 710*7c478bd9Sstevel@tonic-gate 711*7c478bd9Sstevel@tonic-gate /* 712*7c478bd9Sstevel@tonic-gate * Invalidate previous hash table entry 713*7c478bd9Sstevel@tonic-gate * assigned this code, and then take it over 714*7c478bd9Sstevel@tonic-gate */ 715*7c478bd9Sstevel@tonic-gate dictp2 = &db->dict[max_ent+1]; 716*7c478bd9Sstevel@tonic-gate if (db->dict[dictp2->cptr].codem1 == max_ent) { 717*7c478bd9Sstevel@tonic-gate db->dict[dictp2->cptr].codem1 = BADCODEM1; 718*7c478bd9Sstevel@tonic-gate } 719*7c478bd9Sstevel@tonic-gate dictp2->cptr = hval; 720*7c478bd9Sstevel@tonic-gate dictp->codem1 = max_ent; 721*7c478bd9Sstevel@tonic-gate dictp->f.fcode = fcode; 722*7c478bd9Sstevel@tonic-gate 723*7c478bd9Sstevel@tonic-gate db->max_ent = ++max_ent; 724*7c478bd9Sstevel@tonic-gate db->lens[max_ent] = db->lens[oldcode]+1; 725*7c478bd9Sstevel@tonic-gate 726*7c478bd9Sstevel@tonic-gate /* Expand code size if needed. */ 727*7c478bd9Sstevel@tonic-gate if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) { 728*7c478bd9Sstevel@tonic-gate db->n_bits = ++n_bits; 729*7c478bd9Sstevel@tonic-gate tgtbitno = 32-n_bits; 730*7c478bd9Sstevel@tonic-gate } 731*7c478bd9Sstevel@tonic-gate } 732*7c478bd9Sstevel@tonic-gate oldcode = incode; 733*7c478bd9Sstevel@tonic-gate } 734*7c478bd9Sstevel@tonic-gate *outlenp = wptr - (dmp + db->hdrlen); 735*7c478bd9Sstevel@tonic-gate 736*7c478bd9Sstevel@tonic-gate /* 737*7c478bd9Sstevel@tonic-gate * Keep the checkpoint right so that incompressible packets 738*7c478bd9Sstevel@tonic-gate * clear the dictionary at the right times. 739*7c478bd9Sstevel@tonic-gate */ 740*7c478bd9Sstevel@tonic-gate db->bytes_out += ilen; 741*7c478bd9Sstevel@tonic-gate db->in_count += explen; 742*7c478bd9Sstevel@tonic-gate if (bsd_check(db) && db->debug) { 743*7c478bd9Sstevel@tonic-gate printf("bsd_decomp%d: peer should have cleared dictionary\n", 744*7c478bd9Sstevel@tonic-gate db->unit); 745*7c478bd9Sstevel@tonic-gate } 746*7c478bd9Sstevel@tonic-gate 747*7c478bd9Sstevel@tonic-gate ++db->comp_count; 748*7c478bd9Sstevel@tonic-gate db->comp_bytes += ilen + BSD_OVHD; 749*7c478bd9Sstevel@tonic-gate ++db->uncomp_count; 750*7c478bd9Sstevel@tonic-gate db->uncomp_bytes += explen; 751*7c478bd9Sstevel@tonic-gate 752*7c478bd9Sstevel@tonic-gate return DECOMP_OK; 753*7c478bd9Sstevel@tonic-gate } 754*7c478bd9Sstevel@tonic-gate #endif /* DO_BSD_COMPRESS */ 755