Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
6aa20a22 | 2 | * Update: The Berkeley copyright was changed, and the change |
1da177e4 LT |
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 | /* Because this code is derived from the 4.3BSD compress source: | |
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 { /* hash value */ | |
117 | unsigned long fcode; | |
118 | struct { | |
119 | #if defined(__LITTLE_ENDIAN) /* Little endian order */ | |
120 | unsigned short prefix; /* preceding code */ | |
121 | unsigned char suffix; /* last character of new code */ | |
122 | unsigned char pad; | |
123 | #elif defined(__BIG_ENDIAN) /* Big endian order */ | |
124 | unsigned char pad; | |
125 | unsigned char suffix; /* last character of new code */ | |
126 | unsigned short prefix; /* preceding code */ | |
127 | #else | |
128 | #error Endianness not defined... | |
129 | #endif | |
130 | } hs; | |
131 | } f; | |
132 | unsigned short codem1; /* output of hash table -1 */ | |
133 | unsigned short cptr; /* map code to hash table entry */ | |
134 | }; | |
135 | ||
136 | struct bsd_db { | |
137 | int totlen; /* length of this structure */ | |
138 | unsigned int hsize; /* size of the hash table */ | |
139 | unsigned char hshift; /* used in hash function */ | |
140 | unsigned char n_bits; /* current bits/code */ | |
141 | unsigned char maxbits; /* maximum bits/code */ | |
142 | unsigned char debug; /* non-zero if debug desired */ | |
143 | unsigned char unit; /* ppp unit number */ | |
144 | unsigned short seqno; /* sequence # of next packet */ | |
145 | unsigned int mru; /* size of receive (decompress) bufr */ | |
146 | unsigned int maxmaxcode; /* largest valid code */ | |
147 | unsigned int max_ent; /* largest code in use */ | |
148 | unsigned int in_count; /* uncompressed bytes, aged */ | |
149 | unsigned int bytes_out; /* compressed bytes, aged */ | |
150 | unsigned int ratio; /* recent compression ratio */ | |
151 | unsigned int checkpoint; /* when to next check the ratio */ | |
152 | unsigned int clear_count; /* times dictionary cleared */ | |
153 | unsigned int incomp_count; /* incompressible packets */ | |
154 | unsigned int incomp_bytes; /* incompressible bytes */ | |
155 | unsigned int uncomp_count; /* uncompressed packets */ | |
156 | unsigned int uncomp_bytes; /* uncompressed bytes */ | |
157 | unsigned int comp_count; /* compressed packets */ | |
158 | unsigned int comp_bytes; /* compressed bytes */ | |
159 | unsigned short *lens; /* array of lengths of codes */ | |
160 | struct bsd_dict *dict; /* dictionary */ | |
161 | }; | |
162 | ||
163 | #define BSD_OVHD 2 /* BSD compress overhead/packet */ | |
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 | /* These are in ppp_generic.c */ | |
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 /* table clear output code */ | |
200 | #define FIRST 257 /* first free entry */ | |
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 /* Ratio check interval */ | |
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) /* 1=output CLEAR */ | |
248 | { | |
249 | unsigned int new_ratio; | |
250 | ||
251 | if (db->in_count >= db->checkpoint) | |
252 | { | |
253 | /* age the ratio by limiting the size of the counts */ | |
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 | } | |
6aa20a22 | 259 | |
1da177e4 | 260 | db->checkpoint = db->in_count + CHECK_GAP; |
6aa20a22 | 261 | |
1da177e4 LT |
262 | if (db->max_ent >= db->maxmaxcode) |
263 | { | |
264 | /* Reset the dictionary only if the ratio is worse, | |
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 | } | |
6aa20a22 | 277 | |
1da177e4 LT |
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; | |
6aa20a22 | 296 | |
1da177e4 LT |
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) | |
f9101210 JJ |
326 | { |
327 | struct bsd_db *db = state; | |
6aa20a22 | 328 | |
f9101210 JJ |
329 | if (!db) |
330 | return; | |
331 | ||
1da177e4 LT |
332 | /* |
333 | * Release the dictionary | |
334 | */ | |
f9101210 JJ |
335 | vfree(db->dict); |
336 | db->dict = NULL; | |
1da177e4 LT |
337 | /* |
338 | * Release the string buffer | |
339 | */ | |
f9101210 JJ |
340 | vfree(db->lens); |
341 | db->lens = NULL; | |
1da177e4 LT |
342 | /* |
343 | * Finally release the structure itself. | |
344 | */ | |
f9101210 JJ |
345 | kfree(db); |
346 | } | |
1da177e4 LT |
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: /* needs 82152 for both directions */ | |
369 | case 10: /* needs 84144 */ | |
370 | case 11: /* needs 88240 */ | |
371 | case 12: /* needs 96432 */ | |
372 | hsize = 5003; | |
373 | hshift = 4; | |
374 | break; | |
375 | case 13: /* needs 176784 */ | |
376 | hsize = 9001; | |
377 | hshift = 5; | |
378 | break; | |
379 | case 14: /* needs 353744 */ | |
380 | hsize = 18013; | |
381 | hshift = 6; | |
382 | break; | |
383 | case 15: /* needs 691440 */ | |
384 | hsize = 35023; | |
385 | hshift = 7; | |
386 | break; | |
387 | case 16: /* needs 1366160--far too much, */ | |
388 | /* hsize = 69001; */ /* and 69001 is too big for cptr */ | |
389 | /* hshift = 8; */ /* in struct bsd_db */ | |
390 | /* break; */ | |
391 | default: | |
392 | return NULL; | |
393 | } | |
394 | /* | |
395 | * Allocate the main control structure for this instance. | |
396 | */ | |
397 | maxmaxcode = MAXCODE(bits); | |
dd00cc48 | 398 | db = kzalloc(sizeof (struct bsd_db), |
1da177e4 LT |
399 | GFP_KERNEL); |
400 | if (!db) | |
401 | { | |
402 | return NULL; | |
403 | } | |
404 | ||
1da177e4 LT |
405 | /* |
406 | * Allocate space for the dictionary. This may be more than one page in | |
407 | * length. | |
408 | */ | |
409 | db->dict = (struct bsd_dict *) vmalloc (hsize * | |
410 | sizeof (struct bsd_dict)); | |
411 | if (!db->dict) | |
412 | { | |
413 | bsd_free (db); | |
414 | return NULL; | |
415 | } | |
416 | ||
417 | /* | |
418 | * If this is the compression buffer then there is no length data. | |
419 | */ | |
420 | if (!decomp) | |
421 | { | |
422 | db->lens = NULL; | |
423 | } | |
424 | /* | |
425 | * For decompression, the length information is needed as well. | |
426 | */ | |
427 | else | |
428 | { | |
429 | db->lens = (unsigned short *) vmalloc ((maxmaxcode + 1) * | |
430 | sizeof (db->lens[0])); | |
431 | if (!db->lens) | |
432 | { | |
433 | bsd_free (db); | |
434 | return (NULL); | |
435 | } | |
436 | } | |
437 | /* | |
438 | * Initialize the data information for the compression code | |
439 | */ | |
440 | db->totlen = sizeof (struct bsd_db) + | |
441 | (sizeof (struct bsd_dict) * hsize); | |
442 | ||
443 | db->hsize = hsize; | |
444 | db->hshift = hshift; | |
445 | db->maxmaxcode = maxmaxcode; | |
446 | db->maxbits = bits; | |
447 | ||
448 | return (void *) db; | |
449 | } | |
450 | ||
451 | static void *bsd_comp_alloc (unsigned char *options, int opt_len) | |
452 | { | |
453 | return bsd_alloc (options, opt_len, 0); | |
454 | } | |
455 | ||
456 | static void *bsd_decomp_alloc (unsigned char *options, int opt_len) | |
457 | { | |
458 | return bsd_alloc (options, opt_len, 1); | |
459 | } | |
460 | ||
461 | /* | |
462 | * Initialize the database. | |
463 | */ | |
464 | ||
465 | static int bsd_init (void *state, unsigned char *options, | |
466 | int opt_len, int unit, int debug, int decomp) | |
467 | { | |
468 | struct bsd_db *db = state; | |
469 | int indx; | |
6aa20a22 | 470 | |
1da177e4 LT |
471 | if ((opt_len != 3) || (options[0] != CI_BSD_COMPRESS) || (options[1] != 3) |
472 | || (BSD_VERSION(options[2]) != BSD_CURRENT_VERSION) | |
473 | || (BSD_NBITS(options[2]) != db->maxbits) | |
474 | || (decomp && db->lens == NULL)) | |
475 | { | |
476 | return 0; | |
477 | } | |
478 | ||
479 | if (decomp) | |
480 | { | |
481 | indx = LAST; | |
482 | do | |
483 | { | |
484 | db->lens[indx] = 1; | |
485 | } | |
486 | while (indx-- > 0); | |
487 | } | |
488 | ||
489 | indx = db->hsize; | |
490 | while (indx-- != 0) | |
491 | { | |
492 | db->dict[indx].codem1 = BADCODEM1; | |
493 | db->dict[indx].cptr = 0; | |
494 | } | |
495 | ||
496 | db->unit = unit; | |
497 | db->mru = 0; | |
498 | #ifndef DEBUG | |
499 | if (debug) | |
500 | #endif | |
501 | db->debug = 1; | |
6aa20a22 | 502 | |
1da177e4 | 503 | bsd_reset(db); |
6aa20a22 | 504 | |
1da177e4 LT |
505 | return 1; |
506 | } | |
507 | ||
508 | static int bsd_comp_init (void *state, unsigned char *options, | |
509 | int opt_len, int unit, int opthdr, int debug) | |
510 | { | |
511 | return bsd_init (state, options, opt_len, unit, debug, 0); | |
512 | } | |
513 | ||
514 | static int bsd_decomp_init (void *state, unsigned char *options, | |
515 | int opt_len, int unit, int opthdr, int mru, | |
516 | int debug) | |
517 | { | |
518 | return bsd_init (state, options, opt_len, unit, debug, 1); | |
519 | } | |
520 | ||
521 | /* | |
522 | * Obtain pointers to the various structures in the compression tables | |
523 | */ | |
524 | ||
525 | #define dict_ptrx(p,idx) &(p->dict[idx]) | |
526 | #define lens_ptrx(p,idx) &(p->lens[idx]) | |
527 | ||
528 | #ifdef DEBUG | |
529 | static unsigned short *lens_ptr(struct bsd_db *db, int idx) | |
530 | { | |
531 | if ((unsigned int) idx > (unsigned int) db->maxmaxcode) | |
532 | { | |
533 | printk ("<9>ppp: lens_ptr(%d) > max\n", idx); | |
534 | idx = 0; | |
535 | } | |
536 | return lens_ptrx (db, idx); | |
537 | } | |
538 | ||
539 | static struct bsd_dict *dict_ptr(struct bsd_db *db, int idx) | |
540 | { | |
541 | if ((unsigned int) idx >= (unsigned int) db->hsize) | |
542 | { | |
543 | printk ("<9>ppp: dict_ptr(%d) > max\n", idx); | |
544 | idx = 0; | |
545 | } | |
546 | return dict_ptrx (db, idx); | |
547 | } | |
548 | ||
549 | #else | |
550 | #define lens_ptr(db,idx) lens_ptrx(db,idx) | |
551 | #define dict_ptr(db,idx) dict_ptrx(db,idx) | |
552 | #endif | |
553 | ||
554 | /* | |
555 | * compress a packet | |
556 | * | |
557 | * The result of this function is the size of the compressed | |
558 | * packet. A zero is returned if the packet was not compressed | |
559 | * for some reason, such as the size being larger than uncompressed. | |
560 | * | |
561 | * One change from the BSD compress command is that when the | |
562 | * code size expands, we do not output a bunch of padding. | |
563 | */ | |
564 | ||
565 | static int bsd_compress (void *state, unsigned char *rptr, unsigned char *obuf, | |
566 | int isize, int osize) | |
567 | { | |
568 | struct bsd_db *db; | |
569 | int hshift; | |
570 | unsigned int max_ent; | |
571 | unsigned int n_bits; | |
572 | unsigned int bitno; | |
573 | unsigned long accm; | |
574 | int ent; | |
575 | unsigned long fcode; | |
576 | struct bsd_dict *dictp; | |
577 | unsigned char c; | |
578 | int hval; | |
579 | int disp; | |
580 | int ilen; | |
581 | int mxcode; | |
582 | unsigned char *wptr; | |
583 | int olen; | |
584 | ||
585 | #define PUTBYTE(v) \ | |
586 | { \ | |
587 | ++olen; \ | |
588 | if (wptr) \ | |
589 | { \ | |
590 | *wptr++ = (unsigned char) (v); \ | |
591 | if (olen >= osize) \ | |
592 | { \ | |
593 | wptr = NULL; \ | |
594 | } \ | |
595 | } \ | |
596 | } | |
597 | ||
598 | #define OUTPUT(ent) \ | |
599 | { \ | |
600 | bitno -= n_bits; \ | |
601 | accm |= ((ent) << bitno); \ | |
602 | do \ | |
603 | { \ | |
604 | PUTBYTE(accm >> 24); \ | |
605 | accm <<= 8; \ | |
606 | bitno += 8; \ | |
607 | } \ | |
608 | while (bitno <= 24); \ | |
609 | } | |
610 | ||
611 | /* | |
612 | * If the protocol is not in the range we're interested in, | |
613 | * just return without compressing the packet. If it is, | |
614 | * the protocol becomes the first byte to compress. | |
615 | */ | |
616 | ||
617 | ent = PPP_PROTOCOL(rptr); | |
618 | if (ent < 0x21 || ent > 0xf9) | |
619 | { | |
620 | return 0; | |
621 | } | |
622 | ||
623 | db = (struct bsd_db *) state; | |
624 | hshift = db->hshift; | |
625 | max_ent = db->max_ent; | |
626 | n_bits = db->n_bits; | |
627 | bitno = 32; | |
628 | accm = 0; | |
629 | mxcode = MAXCODE (n_bits); | |
630 | ||
631 | /* Initialize the output pointers */ | |
632 | wptr = obuf; | |
633 | olen = PPP_HDRLEN + BSD_OVHD; | |
634 | ||
635 | if (osize > isize) | |
636 | { | |
637 | osize = isize; | |
638 | } | |
639 | ||
640 | /* This is the PPP header information */ | |
641 | if (wptr) | |
642 | { | |
643 | *wptr++ = PPP_ADDRESS(rptr); | |
644 | *wptr++ = PPP_CONTROL(rptr); | |
645 | *wptr++ = 0; | |
646 | *wptr++ = PPP_COMP; | |
647 | *wptr++ = db->seqno >> 8; | |
648 | *wptr++ = db->seqno; | |
649 | } | |
650 | ||
651 | /* Skip the input header */ | |
652 | rptr += PPP_HDRLEN; | |
653 | isize -= PPP_HDRLEN; | |
654 | ilen = ++isize; /* Low byte of protocol is counted as input */ | |
655 | ||
656 | while (--ilen > 0) | |
657 | { | |
658 | c = *rptr++; | |
659 | fcode = BSD_KEY (ent, c); | |
660 | hval = BSD_HASH (ent, c, hshift); | |
661 | dictp = dict_ptr (db, hval); | |
6aa20a22 | 662 | |
1da177e4 LT |
663 | /* Validate and then check the entry. */ |
664 | if (dictp->codem1 >= max_ent) | |
665 | { | |
666 | goto nomatch; | |
667 | } | |
668 | ||
669 | if (dictp->f.fcode == fcode) | |
670 | { | |
671 | ent = dictp->codem1 + 1; | |
672 | continue; /* found (prefix,suffix) */ | |
673 | } | |
6aa20a22 | 674 | |
1da177e4 LT |
675 | /* continue probing until a match or invalid entry */ |
676 | disp = (hval == 0) ? 1 : hval; | |
677 | ||
678 | do | |
679 | { | |
680 | hval += disp; | |
681 | if (hval >= db->hsize) | |
682 | { | |
683 | hval -= db->hsize; | |
684 | } | |
685 | dictp = dict_ptr (db, hval); | |
686 | if (dictp->codem1 >= max_ent) | |
687 | { | |
688 | goto nomatch; | |
689 | } | |
690 | } | |
691 | while (dictp->f.fcode != fcode); | |
692 | ||
693 | ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */ | |
694 | continue; | |
6aa20a22 | 695 | |
1da177e4 LT |
696 | nomatch: |
697 | OUTPUT(ent); /* output the prefix */ | |
6aa20a22 | 698 | |
1da177e4 LT |
699 | /* code -> hashtable */ |
700 | if (max_ent < db->maxmaxcode) | |
701 | { | |
702 | struct bsd_dict *dictp2; | |
703 | struct bsd_dict *dictp3; | |
704 | int indx; | |
705 | ||
706 | /* expand code size if needed */ | |
707 | if (max_ent >= mxcode) | |
708 | { | |
709 | db->n_bits = ++n_bits; | |
710 | mxcode = MAXCODE (n_bits); | |
711 | } | |
6aa20a22 | 712 | |
1da177e4 LT |
713 | /* Invalidate old hash table entry using |
714 | * this code, and then take it over. | |
715 | */ | |
716 | ||
717 | dictp2 = dict_ptr (db, max_ent + 1); | |
718 | indx = dictp2->cptr; | |
719 | dictp3 = dict_ptr (db, indx); | |
720 | ||
721 | if (dictp3->codem1 == max_ent) | |
722 | { | |
723 | dictp3->codem1 = BADCODEM1; | |
724 | } | |
725 | ||
726 | dictp2->cptr = hval; | |
727 | dictp->codem1 = max_ent; | |
728 | dictp->f.fcode = fcode; | |
729 | db->max_ent = ++max_ent; | |
730 | ||
731 | if (db->lens) | |
732 | { | |
733 | unsigned short *len1 = lens_ptr (db, max_ent); | |
734 | unsigned short *len2 = lens_ptr (db, ent); | |
735 | *len1 = *len2 + 1; | |
736 | } | |
737 | } | |
738 | ent = c; | |
739 | } | |
6aa20a22 | 740 | |
1da177e4 LT |
741 | OUTPUT(ent); /* output the last code */ |
742 | ||
743 | db->bytes_out += olen - PPP_HDRLEN - BSD_OVHD; | |
744 | db->uncomp_bytes += isize; | |
745 | db->in_count += isize; | |
746 | ++db->uncomp_count; | |
747 | ++db->seqno; | |
748 | ||
749 | if (bitno < 32) | |
750 | { | |
751 | ++db->bytes_out; /* must be set before calling bsd_check */ | |
752 | } | |
753 | ||
754 | /* | |
755 | * Generate the clear command if needed | |
756 | */ | |
757 | ||
758 | if (bsd_check(db)) | |
759 | { | |
760 | OUTPUT (CLEAR); | |
761 | } | |
6aa20a22 | 762 | |
1da177e4 LT |
763 | /* |
764 | * Pad dribble bits of last code with ones. | |
765 | * Do not emit a completely useless byte of ones. | |
766 | */ | |
767 | ||
768 | if (bitno != 32) | |
769 | { | |
770 | PUTBYTE((accm | (0xff << (bitno-8))) >> 24); | |
771 | } | |
6aa20a22 | 772 | |
1da177e4 LT |
773 | /* |
774 | * Increase code size if we would have without the packet | |
775 | * boundary because the decompressor will do so. | |
776 | */ | |
777 | ||
778 | if (max_ent >= mxcode && max_ent < db->maxmaxcode) | |
779 | { | |
780 | db->n_bits++; | |
781 | } | |
782 | ||
783 | /* If output length is too large then this is an incomplete frame. */ | |
784 | if (wptr == NULL) | |
785 | { | |
786 | ++db->incomp_count; | |
787 | db->incomp_bytes += isize; | |
788 | olen = 0; | |
789 | } | |
790 | else /* Count the number of compressed frames */ | |
791 | { | |
792 | ++db->comp_count; | |
793 | db->comp_bytes += olen; | |
794 | } | |
795 | ||
796 | /* Return the resulting output length */ | |
797 | return olen; | |
798 | #undef OUTPUT | |
799 | #undef PUTBYTE | |
800 | } | |
801 | ||
802 | /* | |
803 | * Update the "BSD Compress" dictionary on the receiver for | |
804 | * incompressible data by pretending to compress the incoming data. | |
805 | */ | |
806 | ||
807 | static void bsd_incomp (void *state, unsigned char *ibuf, int icnt) | |
808 | { | |
809 | (void) bsd_compress (state, ibuf, (char *) 0, icnt, 0); | |
810 | } | |
811 | ||
812 | /* | |
813 | * Decompress "BSD Compress". | |
814 | * | |
815 | * Because of patent problems, we return DECOMP_ERROR for errors | |
816 | * found by inspecting the input data and for system problems, but | |
817 | * DECOMP_FATALERROR for any errors which could possibly be said to | |
818 | * be being detected "after" decompression. For DECOMP_ERROR, | |
819 | * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be | |
820 | * infringing a patent of Motorola's if we do, so we take CCP down | |
821 | * instead. | |
822 | * | |
823 | * Given that the frame has the correct sequence number and a good FCS, | |
824 | * errors such as invalid codes in the input most likely indicate a | |
825 | * bug, so we return DECOMP_FATALERROR for them in order to turn off | |
826 | * compression, even though they are detected by inspecting the input. | |
827 | */ | |
828 | ||
829 | static int bsd_decompress (void *state, unsigned char *ibuf, int isize, | |
830 | unsigned char *obuf, int osize) | |
831 | { | |
832 | struct bsd_db *db; | |
833 | unsigned int max_ent; | |
834 | unsigned long accm; | |
835 | unsigned int bitno; /* 1st valid bit in accm */ | |
836 | unsigned int n_bits; | |
837 | unsigned int tgtbitno; /* bitno when we have a code */ | |
838 | struct bsd_dict *dictp; | |
839 | int explen; | |
840 | int seq; | |
841 | unsigned int incode; | |
842 | unsigned int oldcode; | |
843 | unsigned int finchar; | |
844 | unsigned char *p; | |
845 | unsigned char *wptr; | |
846 | int adrs; | |
847 | int ctrl; | |
848 | int ilen; | |
849 | int codelen; | |
850 | int extra; | |
851 | ||
852 | db = (struct bsd_db *) state; | |
853 | max_ent = db->max_ent; | |
854 | accm = 0; | |
855 | bitno = 32; /* 1st valid bit in accm */ | |
856 | n_bits = db->n_bits; | |
857 | tgtbitno = 32 - n_bits; /* bitno when we have a code */ | |
6aa20a22 | 858 | |
1da177e4 LT |
859 | /* |
860 | * Save the address/control from the PPP header | |
861 | * and then get the sequence number. | |
862 | */ | |
863 | ||
864 | adrs = PPP_ADDRESS (ibuf); | |
865 | ctrl = PPP_CONTROL (ibuf); | |
866 | ||
867 | seq = (ibuf[4] << 8) + ibuf[5]; | |
868 | ||
869 | ibuf += (PPP_HDRLEN + 2); | |
870 | ilen = isize - (PPP_HDRLEN + 2); | |
6aa20a22 | 871 | |
1da177e4 LT |
872 | /* |
873 | * Check the sequence number and give up if it differs from | |
874 | * the value we're expecting. | |
875 | */ | |
876 | ||
877 | if (seq != db->seqno) | |
878 | { | |
879 | if (db->debug) | |
880 | { | |
881 | printk("bsd_decomp%d: bad sequence # %d, expected %d\n", | |
882 | db->unit, seq, db->seqno - 1); | |
883 | } | |
884 | return DECOMP_ERROR; | |
885 | } | |
886 | ||
887 | ++db->seqno; | |
888 | db->bytes_out += ilen; | |
889 | ||
890 | /* | |
891 | * Fill in the ppp header, but not the last byte of the protocol | |
892 | * (that comes from the decompressed data). | |
893 | */ | |
894 | ||
895 | wptr = obuf; | |
896 | *wptr++ = adrs; | |
897 | *wptr++ = ctrl; | |
898 | *wptr++ = 0; | |
6aa20a22 | 899 | |
1da177e4 LT |
900 | oldcode = CLEAR; |
901 | explen = 3; | |
902 | ||
903 | /* | |
904 | * Keep the checkpoint correctly so that incompressible packets | |
905 | * clear the dictionary at the proper times. | |
906 | */ | |
907 | ||
908 | for (;;) | |
909 | { | |
910 | if (ilen-- <= 0) | |
911 | { | |
912 | db->in_count += (explen - 3); /* don't count the header */ | |
913 | break; | |
914 | } | |
915 | ||
916 | /* | |
917 | * Accumulate bytes until we have a complete code. | |
918 | * Then get the next code, relying on the 32-bit, | |
919 | * unsigned accm to mask the result. | |
920 | */ | |
921 | ||
922 | bitno -= 8; | |
923 | accm |= *ibuf++ << bitno; | |
924 | if (tgtbitno < bitno) | |
925 | { | |
926 | continue; | |
927 | } | |
928 | ||
929 | incode = accm >> tgtbitno; | |
930 | accm <<= n_bits; | |
931 | bitno += n_bits; | |
932 | ||
933 | /* | |
934 | * The dictionary must only be cleared at the end of a packet. | |
935 | */ | |
6aa20a22 | 936 | |
1da177e4 LT |
937 | if (incode == CLEAR) |
938 | { | |
939 | if (ilen > 0) | |
940 | { | |
941 | if (db->debug) | |
942 | { | |
943 | printk("bsd_decomp%d: bad CLEAR\n", db->unit); | |
944 | } | |
945 | return DECOMP_FATALERROR; /* probably a bug */ | |
946 | } | |
6aa20a22 | 947 | |
1da177e4 LT |
948 | bsd_clear(db); |
949 | break; | |
950 | } | |
951 | ||
952 | if ((incode > max_ent + 2) || (incode > db->maxmaxcode) | |
953 | || (incode > max_ent && oldcode == CLEAR)) | |
954 | { | |
955 | if (db->debug) | |
956 | { | |
957 | printk("bsd_decomp%d: bad code 0x%x oldcode=0x%x ", | |
958 | db->unit, incode, oldcode); | |
959 | printk("max_ent=0x%x explen=%d seqno=%d\n", | |
960 | max_ent, explen, db->seqno); | |
961 | } | |
962 | return DECOMP_FATALERROR; /* probably a bug */ | |
963 | } | |
6aa20a22 | 964 | |
1da177e4 LT |
965 | /* Special case for KwKwK string. */ |
966 | if (incode > max_ent) | |
967 | { | |
968 | finchar = oldcode; | |
969 | extra = 1; | |
970 | } | |
971 | else | |
972 | { | |
973 | finchar = incode; | |
974 | extra = 0; | |
975 | } | |
6aa20a22 | 976 | |
1da177e4 LT |
977 | codelen = *(lens_ptr (db, finchar)); |
978 | explen += codelen + extra; | |
979 | if (explen > osize) | |
980 | { | |
981 | if (db->debug) | |
982 | { | |
983 | printk("bsd_decomp%d: ran out of mru\n", db->unit); | |
984 | #ifdef DEBUG | |
985 | printk(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n", | |
986 | ilen, finchar, codelen, explen); | |
987 | #endif | |
988 | } | |
989 | return DECOMP_FATALERROR; | |
990 | } | |
6aa20a22 | 991 | |
1da177e4 LT |
992 | /* |
993 | * Decode this code and install it in the decompressed buffer. | |
994 | */ | |
995 | ||
996 | wptr += codelen; | |
997 | p = wptr; | |
998 | while (finchar > LAST) | |
999 | { | |
1000 | struct bsd_dict *dictp2 = dict_ptr (db, finchar); | |
6aa20a22 | 1001 | |
1da177e4 LT |
1002 | dictp = dict_ptr (db, dictp2->cptr); |
1003 | #ifdef DEBUG | |
1004 | if (--codelen <= 0 || dictp->codem1 != finchar-1) | |
1005 | { | |
1006 | if (codelen <= 0) | |
1007 | { | |
1008 | printk("bsd_decomp%d: fell off end of chain ", db->unit); | |
1009 | printk("0x%x at 0x%x by 0x%x, max_ent=0x%x\n", | |
1010 | incode, finchar, dictp2->cptr, max_ent); | |
1011 | } | |
1012 | else | |
1013 | { | |
1014 | if (dictp->codem1 != finchar-1) | |
1015 | { | |
1016 | printk("bsd_decomp%d: bad code chain 0x%x " | |
1017 | "finchar=0x%x ", | |
1018 | db->unit, incode, finchar); | |
1019 | ||
1020 | printk("oldcode=0x%x cptr=0x%x codem1=0x%x\n", | |
1021 | oldcode, dictp2->cptr, dictp->codem1); | |
1022 | } | |
1023 | } | |
1024 | return DECOMP_FATALERROR; | |
1025 | } | |
1026 | #endif | |
1027 | *--p = dictp->f.hs.suffix; | |
1028 | finchar = dictp->f.hs.prefix; | |
1029 | } | |
1030 | *--p = finchar; | |
6aa20a22 | 1031 | |
1da177e4 LT |
1032 | #ifdef DEBUG |
1033 | if (--codelen != 0) | |
1034 | { | |
1035 | printk("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n", | |
1036 | db->unit, codelen, incode, max_ent); | |
1037 | } | |
1038 | #endif | |
6aa20a22 | 1039 | |
1da177e4 LT |
1040 | if (extra) /* the KwKwK case again */ |
1041 | { | |
1042 | *wptr++ = finchar; | |
1043 | } | |
6aa20a22 | 1044 | |
1da177e4 LT |
1045 | /* |
1046 | * If not first code in a packet, and | |
1047 | * if not out of code space, then allocate a new code. | |
1048 | * | |
1049 | * Keep the hash table correct so it can be used | |
1050 | * with uncompressed packets. | |
1051 | */ | |
1052 | ||
1053 | if (oldcode != CLEAR && max_ent < db->maxmaxcode) | |
1054 | { | |
1055 | struct bsd_dict *dictp2, *dictp3; | |
1056 | unsigned short *lens1, *lens2; | |
1057 | unsigned long fcode; | |
1058 | int hval, disp, indx; | |
6aa20a22 | 1059 | |
1da177e4 LT |
1060 | fcode = BSD_KEY(oldcode,finchar); |
1061 | hval = BSD_HASH(oldcode,finchar,db->hshift); | |
1062 | dictp = dict_ptr (db, hval); | |
6aa20a22 | 1063 | |
1da177e4 LT |
1064 | /* look for a free hash table entry */ |
1065 | if (dictp->codem1 < max_ent) | |
1066 | { | |
1067 | disp = (hval == 0) ? 1 : hval; | |
1068 | do | |
1069 | { | |
1070 | hval += disp; | |
1071 | if (hval >= db->hsize) | |
1072 | { | |
1073 | hval -= db->hsize; | |
1074 | } | |
1075 | dictp = dict_ptr (db, hval); | |
1076 | } | |
1077 | while (dictp->codem1 < max_ent); | |
1078 | } | |
6aa20a22 | 1079 | |
1da177e4 LT |
1080 | /* |
1081 | * Invalidate previous hash table entry | |
1082 | * assigned this code, and then take it over | |
1083 | */ | |
1084 | ||
1085 | dictp2 = dict_ptr (db, max_ent + 1); | |
1086 | indx = dictp2->cptr; | |
1087 | dictp3 = dict_ptr (db, indx); | |
1088 | ||
1089 | if (dictp3->codem1 == max_ent) | |
1090 | { | |
1091 | dictp3->codem1 = BADCODEM1; | |
1092 | } | |
1093 | ||
1094 | dictp2->cptr = hval; | |
1095 | dictp->codem1 = max_ent; | |
1096 | dictp->f.fcode = fcode; | |
1097 | db->max_ent = ++max_ent; | |
1098 | ||
1099 | /* Update the length of this string. */ | |
1100 | lens1 = lens_ptr (db, max_ent); | |
1101 | lens2 = lens_ptr (db, oldcode); | |
1102 | *lens1 = *lens2 + 1; | |
6aa20a22 | 1103 | |
1da177e4 LT |
1104 | /* Expand code size if needed. */ |
1105 | if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) | |
1106 | { | |
1107 | db->n_bits = ++n_bits; | |
1108 | tgtbitno = 32-n_bits; | |
1109 | } | |
1110 | } | |
1111 | oldcode = incode; | |
1112 | } | |
1113 | ||
1114 | ++db->comp_count; | |
1115 | ++db->uncomp_count; | |
1116 | db->comp_bytes += isize - BSD_OVHD - PPP_HDRLEN; | |
1117 | db->uncomp_bytes += explen; | |
1118 | ||
1119 | if (bsd_check(db)) | |
1120 | { | |
1121 | if (db->debug) | |
1122 | { | |
1123 | printk("bsd_decomp%d: peer should have cleared dictionary on %d\n", | |
1124 | db->unit, db->seqno - 1); | |
1125 | } | |
1126 | } | |
1127 | return explen; | |
1128 | } | |
6aa20a22 | 1129 | |
1da177e4 LT |
1130 | /************************************************************* |
1131 | * Table of addresses for the BSD compression module | |
1132 | *************************************************************/ | |
1133 | ||
1134 | static struct compressor ppp_bsd_compress = { | |
1135 | .compress_proto = CI_BSD_COMPRESS, | |
1136 | .comp_alloc = bsd_comp_alloc, | |
1137 | .comp_free = bsd_free, | |
1138 | .comp_init = bsd_comp_init, | |
1139 | .comp_reset = bsd_reset, | |
1140 | .compress = bsd_compress, | |
1141 | .comp_stat = bsd_comp_stats, | |
1142 | .decomp_alloc = bsd_decomp_alloc, | |
1143 | .decomp_free = bsd_free, | |
1144 | .decomp_init = bsd_decomp_init, | |
1145 | .decomp_reset = bsd_reset, | |
1146 | .decompress = bsd_decompress, | |
1147 | .incomp = bsd_incomp, | |
1148 | .decomp_stat = bsd_comp_stats, | |
1149 | .owner = THIS_MODULE | |
1150 | }; | |
1151 | ||
1152 | /************************************************************* | |
1153 | * Module support routines | |
1154 | *************************************************************/ | |
1155 | ||
1156 | static int __init bsdcomp_init(void) | |
1157 | { | |
1158 | int answer = ppp_register_compressor(&ppp_bsd_compress); | |
1159 | if (answer == 0) | |
1160 | printk(KERN_INFO "PPP BSD Compression module registered\n"); | |
1161 | return answer; | |
1162 | } | |
1163 | ||
1164 | static void __exit bsdcomp_cleanup(void) | |
1165 | { | |
1166 | ppp_unregister_compressor(&ppp_bsd_compress); | |
1167 | } | |
1168 | ||
1169 | module_init(bsdcomp_init); | |
1170 | module_exit(bsdcomp_cleanup); | |
1171 | MODULE_LICENSE("Dual BSD/GPL"); | |
1172 | MODULE_ALIAS("ppp-compress-" __stringify(CI_BSD_COMPRESS)); |