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