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