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