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