diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/isdn/i4l/isdn_bsdcomp.c |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'drivers/isdn/i4l/isdn_bsdcomp.c')
-rw-r--r-- | drivers/isdn/i4l/isdn_bsdcomp.c | 937 |
1 files changed, 937 insertions, 0 deletions
diff --git a/drivers/isdn/i4l/isdn_bsdcomp.c b/drivers/isdn/i4l/isdn_bsdcomp.c new file mode 100644 index 000000000000..baf4bcad9bf9 --- /dev/null +++ b/drivers/isdn/i4l/isdn_bsdcomp.c | |||
@@ -0,0 +1,937 @@ | |||
1 | /* | ||
2 | * BSD compression module | ||
3 | * | ||
4 | * Patched version for ISDN syncPPP written 1997/1998 by Michael Hipp | ||
5 | * The whole module is now SKB based. | ||
6 | * | ||
7 | */ | ||
8 | |||
9 | /* | ||
10 | * Update: The Berkeley copyright was changed, and the change | ||
11 | * is retroactive to all "true" BSD software (ie everything | ||
12 | * from UCB as opposed to other peoples code that just carried | ||
13 | * the same license). The new copyright doesn't clash with the | ||
14 | * GPL, so the module-only restriction has been removed.. | ||
15 | */ | ||
16 | |||
17 | /* | ||
18 | * Original copyright notice: | ||
19 | * | ||
20 | * Copyright (c) 1985, 1986 The Regents of the University of California. | ||
21 | * All rights reserved. | ||
22 | * | ||
23 | * This code is derived from software contributed to Berkeley by | ||
24 | * James A. Woods, derived from original work by Spencer Thomas | ||
25 | * and Joseph Orost. | ||
26 | * | ||
27 | * Redistribution and use in source and binary forms, with or without | ||
28 | * modification, are permitted provided that the following conditions | ||
29 | * are met: | ||
30 | * 1. Redistributions of source code must retain the above copyright | ||
31 | * notice, this list of conditions and the following disclaimer. | ||
32 | * 2. Redistributions in binary form must reproduce the above copyright | ||
33 | * notice, this list of conditions and the following disclaimer in the | ||
34 | * documentation and/or other materials provided with the distribution. | ||
35 | * 3. All advertising materials mentioning features or use of this software | ||
36 | * must display the following acknowledgement: | ||
37 | * This product includes software developed by the University of | ||
38 | * California, Berkeley and its contributors. | ||
39 | * 4. Neither the name of the University nor the names of its contributors | ||
40 | * may be used to endorse or promote products derived from this software | ||
41 | * without specific prior written permission. | ||
42 | * | ||
43 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
44 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
45 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
46 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
47 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
48 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
49 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
50 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
51 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
52 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
53 | * SUCH DAMAGE. | ||
54 | */ | ||
55 | |||
56 | #include <linux/module.h> | ||
57 | #include <linux/init.h> | ||
58 | #include <linux/kernel.h> | ||
59 | #include <linux/sched.h> | ||
60 | #include <linux/types.h> | ||
61 | #include <linux/fcntl.h> | ||
62 | #include <linux/interrupt.h> | ||
63 | #include <linux/ptrace.h> | ||
64 | #include <linux/ioport.h> | ||
65 | #include <linux/in.h> | ||
66 | #include <linux/slab.h> | ||
67 | #include <linux/tty.h> | ||
68 | #include <linux/errno.h> | ||
69 | #include <linux/string.h> /* used in new tty drivers */ | ||
70 | #include <linux/signal.h> /* used in new tty drivers */ | ||
71 | #include <linux/bitops.h> | ||
72 | |||
73 | #include <asm/system.h> | ||
74 | #include <asm/byteorder.h> | ||
75 | #include <asm/types.h> | ||
76 | |||
77 | #include <linux/if.h> | ||
78 | |||
79 | #include <linux/if_ether.h> | ||
80 | #include <linux/netdevice.h> | ||
81 | #include <linux/skbuff.h> | ||
82 | #include <linux/inet.h> | ||
83 | #include <linux/ioctl.h> | ||
84 | #include <linux/vmalloc.h> | ||
85 | |||
86 | #include <linux/ppp_defs.h> | ||
87 | |||
88 | #include <linux/isdn.h> | ||
89 | #include <linux/isdn_ppp.h> | ||
90 | #include <linux/ip.h> | ||
91 | #include <linux/tcp.h> | ||
92 | #include <linux/if_arp.h> | ||
93 | #include <linux/ppp-comp.h> | ||
94 | |||
95 | #include "isdn_ppp.h" | ||
96 | |||
97 | MODULE_DESCRIPTION("ISDN4Linux: BSD Compression for PPP over ISDN"); | ||
98 | MODULE_LICENSE("Dual BSD/GPL"); | ||
99 | |||
100 | #define BSD_VERSION(x) ((x) >> 5) | ||
101 | #define BSD_NBITS(x) ((x) & 0x1F) | ||
102 | |||
103 | #define BSD_CURRENT_VERSION 1 | ||
104 | |||
105 | #define DEBUG 1 | ||
106 | |||
107 | /* | ||
108 | * A dictionary for doing BSD compress. | ||
109 | */ | ||
110 | |||
111 | struct bsd_dict { | ||
112 | u32 fcode; | ||
113 | u16 codem1; /* output of hash table -1 */ | ||
114 | u16 cptr; /* map code to hash table entry */ | ||
115 | }; | ||
116 | |||
117 | struct bsd_db { | ||
118 | int totlen; /* length of this structure */ | ||
119 | unsigned int hsize; /* size of the hash table */ | ||
120 | unsigned char hshift; /* used in hash function */ | ||
121 | unsigned char n_bits; /* current bits/code */ | ||
122 | unsigned char maxbits; /* maximum bits/code */ | ||
123 | unsigned char debug; /* non-zero if debug desired */ | ||
124 | unsigned char unit; /* ppp unit number */ | ||
125 | u16 seqno; /* sequence # of next packet */ | ||
126 | unsigned int mru; /* size of receive (decompress) bufr */ | ||
127 | unsigned int maxmaxcode; /* largest valid code */ | ||
128 | unsigned int max_ent; /* largest code in use */ | ||
129 | unsigned int in_count; /* uncompressed bytes, aged */ | ||
130 | unsigned int bytes_out; /* compressed bytes, aged */ | ||
131 | unsigned int ratio; /* recent compression ratio */ | ||
132 | unsigned int checkpoint; /* when to next check the ratio */ | ||
133 | unsigned int clear_count; /* times dictionary cleared */ | ||
134 | unsigned int incomp_count; /* incompressible packets */ | ||
135 | unsigned int incomp_bytes; /* incompressible bytes */ | ||
136 | unsigned int uncomp_count; /* uncompressed packets */ | ||
137 | unsigned int uncomp_bytes; /* uncompressed bytes */ | ||
138 | unsigned int comp_count; /* compressed packets */ | ||
139 | unsigned int comp_bytes; /* compressed bytes */ | ||
140 | unsigned short *lens; /* array of lengths of codes */ | ||
141 | struct bsd_dict *dict; /* dictionary */ | ||
142 | int xmit; | ||
143 | }; | ||
144 | |||
145 | #define BSD_OVHD 2 /* BSD compress overhead/packet */ | ||
146 | #define MIN_BSD_BITS 9 | ||
147 | #define BSD_INIT_BITS MIN_BSD_BITS | ||
148 | #define MAX_BSD_BITS 15 | ||
149 | |||
150 | /* | ||
151 | * the next two codes should not be changed lightly, as they must not | ||
152 | * lie within the contiguous general code space. | ||
153 | */ | ||
154 | #define CLEAR 256 /* table clear output code */ | ||
155 | #define FIRST 257 /* first free entry */ | ||
156 | #define LAST 255 | ||
157 | |||
158 | #define MAXCODE(b) ((1 << (b)) - 1) | ||
159 | #define BADCODEM1 MAXCODE(MAX_BSD_BITS); | ||
160 | |||
161 | #define BSD_HASH(prefix,suffix,hshift) ((((unsigned long)(suffix))<<(hshift)) \ | ||
162 | ^ (unsigned long)(prefix)) | ||
163 | #define BSD_KEY(prefix,suffix) ((((unsigned long)(suffix)) << 16) \ | ||
164 | + (unsigned long)(prefix)) | ||
165 | |||
166 | #define CHECK_GAP 10000 /* Ratio check interval */ | ||
167 | |||
168 | #define RATIO_SCALE_LOG 8 | ||
169 | #define RATIO_SCALE (1<<RATIO_SCALE_LOG) | ||
170 | #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG) | ||
171 | |||
172 | /* | ||
173 | * clear the dictionary | ||
174 | */ | ||
175 | |||
176 | static void bsd_clear(struct bsd_db *db) | ||
177 | { | ||
178 | db->clear_count++; | ||
179 | db->max_ent = FIRST-1; | ||
180 | db->n_bits = BSD_INIT_BITS; | ||
181 | db->bytes_out = 0; | ||
182 | db->in_count = 0; | ||
183 | db->incomp_count = 0; | ||
184 | db->ratio = 0; | ||
185 | db->checkpoint = CHECK_GAP; | ||
186 | } | ||
187 | |||
188 | /* | ||
189 | * If the dictionary is full, then see if it is time to reset it. | ||
190 | * | ||
191 | * Compute the compression ratio using fixed-point arithmetic | ||
192 | * with 8 fractional bits. | ||
193 | * | ||
194 | * Since we have an infinite stream instead of a single file, | ||
195 | * watch only the local compression ratio. | ||
196 | * | ||
197 | * Since both peers must reset the dictionary at the same time even in | ||
198 | * the absence of CLEAR codes (while packets are incompressible), they | ||
199 | * must compute the same ratio. | ||
200 | */ | ||
201 | static int bsd_check (struct bsd_db *db) /* 1=output CLEAR */ | ||
202 | { | ||
203 | unsigned int new_ratio; | ||
204 | |||
205 | if (db->in_count >= db->checkpoint) | ||
206 | { | ||
207 | /* age the ratio by limiting the size of the counts */ | ||
208 | if (db->in_count >= RATIO_MAX || db->bytes_out >= RATIO_MAX) | ||
209 | { | ||
210 | db->in_count -= (db->in_count >> 2); | ||
211 | db->bytes_out -= (db->bytes_out >> 2); | ||
212 | } | ||
213 | |||
214 | db->checkpoint = db->in_count + CHECK_GAP; | ||
215 | |||
216 | if (db->max_ent >= db->maxmaxcode) | ||
217 | { | ||
218 | /* Reset the dictionary only if the ratio is worse, | ||
219 | * or if it looks as if it has been poisoned | ||
220 | * by incompressible data. | ||
221 | * | ||
222 | * This does not overflow, because | ||
223 | * db->in_count <= RATIO_MAX. | ||
224 | */ | ||
225 | |||
226 | new_ratio = db->in_count << RATIO_SCALE_LOG; | ||
227 | if (db->bytes_out != 0) | ||
228 | { | ||
229 | new_ratio /= db->bytes_out; | ||
230 | } | ||
231 | |||
232 | if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) | ||
233 | { | ||
234 | bsd_clear (db); | ||
235 | return 1; | ||
236 | } | ||
237 | db->ratio = new_ratio; | ||
238 | } | ||
239 | } | ||
240 | return 0; | ||
241 | } | ||
242 | |||
243 | /* | ||
244 | * Return statistics. | ||
245 | */ | ||
246 | |||
247 | static void bsd_stats (void *state, struct compstat *stats) | ||
248 | { | ||
249 | struct bsd_db *db = (struct bsd_db *) state; | ||
250 | |||
251 | stats->unc_bytes = db->uncomp_bytes; | ||
252 | stats->unc_packets = db->uncomp_count; | ||
253 | stats->comp_bytes = db->comp_bytes; | ||
254 | stats->comp_packets = db->comp_count; | ||
255 | stats->inc_bytes = db->incomp_bytes; | ||
256 | stats->inc_packets = db->incomp_count; | ||
257 | stats->in_count = db->in_count; | ||
258 | stats->bytes_out = db->bytes_out; | ||
259 | } | ||
260 | |||
261 | /* | ||
262 | * Reset state, as on a CCP ResetReq. | ||
263 | */ | ||
264 | static void bsd_reset (void *state,unsigned char code, unsigned char id, | ||
265 | unsigned char *data, unsigned len, | ||
266 | struct isdn_ppp_resetparams *rsparm) | ||
267 | { | ||
268 | struct bsd_db *db = (struct bsd_db *) state; | ||
269 | |||
270 | bsd_clear(db); | ||
271 | db->seqno = 0; | ||
272 | db->clear_count = 0; | ||
273 | } | ||
274 | |||
275 | /* | ||
276 | * Release the compression structure | ||
277 | */ | ||
278 | static void bsd_free (void *state) | ||
279 | { | ||
280 | struct bsd_db *db = (struct bsd_db *) state; | ||
281 | |||
282 | if (db) { | ||
283 | /* | ||
284 | * Release the dictionary | ||
285 | */ | ||
286 | if (db->dict) { | ||
287 | vfree (db->dict); | ||
288 | db->dict = NULL; | ||
289 | } | ||
290 | |||
291 | /* | ||
292 | * Release the string buffer | ||
293 | */ | ||
294 | if (db->lens) { | ||
295 | vfree (db->lens); | ||
296 | db->lens = NULL; | ||
297 | } | ||
298 | |||
299 | /* | ||
300 | * Finally release the structure itself. | ||
301 | */ | ||
302 | kfree (db); | ||
303 | } | ||
304 | } | ||
305 | |||
306 | |||
307 | /* | ||
308 | * Allocate space for a (de) compressor. | ||
309 | */ | ||
310 | static void *bsd_alloc (struct isdn_ppp_comp_data *data) | ||
311 | { | ||
312 | int bits; | ||
313 | unsigned int hsize, hshift, maxmaxcode; | ||
314 | struct bsd_db *db; | ||
315 | int decomp; | ||
316 | |||
317 | static unsigned int htab[][2] = { | ||
318 | { 5003 , 4 } , { 5003 , 4 } , { 5003 , 4 } , { 5003 , 4 } , | ||
319 | { 9001 , 5 } , { 18013 , 6 } , { 35023 , 7 } , { 69001 , 8 } | ||
320 | }; | ||
321 | |||
322 | if (data->optlen != 1 || data->num != CI_BSD_COMPRESS | ||
323 | || BSD_VERSION(data->options[0]) != BSD_CURRENT_VERSION) | ||
324 | return NULL; | ||
325 | |||
326 | bits = BSD_NBITS(data->options[0]); | ||
327 | |||
328 | if(bits < 9 || bits > 15) | ||
329 | return NULL; | ||
330 | |||
331 | hsize = htab[bits-9][0]; | ||
332 | hshift = htab[bits-9][1]; | ||
333 | |||
334 | /* | ||
335 | * Allocate the main control structure for this instance. | ||
336 | */ | ||
337 | maxmaxcode = MAXCODE(bits); | ||
338 | db = (struct bsd_db *) kmalloc (sizeof (struct bsd_db),GFP_KERNEL); | ||
339 | if (!db) | ||
340 | return NULL; | ||
341 | |||
342 | memset (db, 0, sizeof(struct bsd_db)); | ||
343 | |||
344 | db->xmit = data->flags & IPPP_COMP_FLAG_XMIT; | ||
345 | decomp = db->xmit ? 0 : 1; | ||
346 | |||
347 | /* | ||
348 | * Allocate space for the dictionary. This may be more than one page in | ||
349 | * length. | ||
350 | */ | ||
351 | db->dict = (struct bsd_dict *) vmalloc (hsize * sizeof (struct bsd_dict)); | ||
352 | if (!db->dict) { | ||
353 | bsd_free (db); | ||
354 | return NULL; | ||
355 | } | ||
356 | |||
357 | /* | ||
358 | * If this is the compression buffer then there is no length data. | ||
359 | * For decompression, the length information is needed as well. | ||
360 | */ | ||
361 | if (!decomp) | ||
362 | db->lens = NULL; | ||
363 | else { | ||
364 | db->lens = (unsigned short *) vmalloc ((maxmaxcode + 1) * | ||
365 | sizeof (db->lens[0])); | ||
366 | if (!db->lens) { | ||
367 | bsd_free (db); | ||
368 | return (NULL); | ||
369 | } | ||
370 | } | ||
371 | |||
372 | /* | ||
373 | * Initialize the data information for the compression code | ||
374 | */ | ||
375 | db->totlen = sizeof (struct bsd_db) + (sizeof (struct bsd_dict) * hsize); | ||
376 | db->hsize = hsize; | ||
377 | db->hshift = hshift; | ||
378 | db->maxmaxcode = maxmaxcode; | ||
379 | db->maxbits = bits; | ||
380 | |||
381 | return (void *) db; | ||
382 | } | ||
383 | |||
384 | /* | ||
385 | * Initialize the database. | ||
386 | */ | ||
387 | static int bsd_init (void *state, struct isdn_ppp_comp_data *data, int unit, int debug) | ||
388 | { | ||
389 | struct bsd_db *db = state; | ||
390 | int indx; | ||
391 | int decomp; | ||
392 | |||
393 | if(!state || !data) { | ||
394 | printk(KERN_ERR "isdn_bsd_init: [%d] ERR, state %lx data %lx\n",unit,(long)state,(long)data); | ||
395 | return 0; | ||
396 | } | ||
397 | |||
398 | decomp = db->xmit ? 0 : 1; | ||
399 | |||
400 | if (data->optlen != 1 || data->num != CI_BSD_COMPRESS | ||
401 | || (BSD_VERSION(data->options[0]) != BSD_CURRENT_VERSION) | ||
402 | || (BSD_NBITS(data->options[0]) != db->maxbits) | ||
403 | || (decomp && db->lens == NULL)) { | ||
404 | printk(KERN_ERR "isdn_bsd: %d %d %d %d %lx\n",data->optlen,data->num,data->options[0],decomp,(unsigned long)db->lens); | ||
405 | return 0; | ||
406 | } | ||
407 | |||
408 | if (decomp) | ||
409 | for(indx=LAST;indx>=0;indx--) | ||
410 | db->lens[indx] = 1; | ||
411 | |||
412 | indx = db->hsize; | ||
413 | while (indx-- != 0) { | ||
414 | db->dict[indx].codem1 = BADCODEM1; | ||
415 | db->dict[indx].cptr = 0; | ||
416 | } | ||
417 | |||
418 | db->unit = unit; | ||
419 | db->mru = 0; | ||
420 | |||
421 | db->debug = 1; | ||
422 | |||
423 | bsd_reset(db,0,0,NULL,0,NULL); | ||
424 | |||
425 | return 1; | ||
426 | } | ||
427 | |||
428 | /* | ||
429 | * Obtain pointers to the various structures in the compression tables | ||
430 | */ | ||
431 | |||
432 | #define dict_ptrx(p,idx) &(p->dict[idx]) | ||
433 | #define lens_ptrx(p,idx) &(p->lens[idx]) | ||
434 | |||
435 | #ifdef DEBUG | ||
436 | static unsigned short *lens_ptr(struct bsd_db *db, int idx) | ||
437 | { | ||
438 | if ((unsigned int) idx > (unsigned int) db->maxmaxcode) { | ||
439 | printk (KERN_DEBUG "<9>ppp: lens_ptr(%d) > max\n", idx); | ||
440 | idx = 0; | ||
441 | } | ||
442 | return lens_ptrx (db, idx); | ||
443 | } | ||
444 | |||
445 | static struct bsd_dict *dict_ptr(struct bsd_db *db, int idx) | ||
446 | { | ||
447 | if ((unsigned int) idx >= (unsigned int) db->hsize) { | ||
448 | printk (KERN_DEBUG "<9>ppp: dict_ptr(%d) > max\n", idx); | ||
449 | idx = 0; | ||
450 | } | ||
451 | return dict_ptrx (db, idx); | ||
452 | } | ||
453 | |||
454 | #else | ||
455 | #define lens_ptr(db,idx) lens_ptrx(db,idx) | ||
456 | #define dict_ptr(db,idx) dict_ptrx(db,idx) | ||
457 | #endif | ||
458 | |||
459 | /* | ||
460 | * compress a packet | ||
461 | */ | ||
462 | static int bsd_compress (void *state, struct sk_buff *skb_in, struct sk_buff *skb_out,int proto) | ||
463 | { | ||
464 | struct bsd_db *db; | ||
465 | int hshift; | ||
466 | unsigned int max_ent; | ||
467 | unsigned int n_bits; | ||
468 | unsigned int bitno; | ||
469 | unsigned long accm; | ||
470 | int ent; | ||
471 | unsigned long fcode; | ||
472 | struct bsd_dict *dictp; | ||
473 | unsigned char c; | ||
474 | int hval,disp,ilen,mxcode; | ||
475 | unsigned char *rptr = skb_in->data; | ||
476 | int isize = skb_in->len; | ||
477 | |||
478 | #define OUTPUT(ent) \ | ||
479 | { \ | ||
480 | bitno -= n_bits; \ | ||
481 | accm |= ((ent) << bitno); \ | ||
482 | do { \ | ||
483 | if(skb_out && skb_tailroom(skb_out) > 0) \ | ||
484 | *(skb_put(skb_out,1)) = (unsigned char) (accm>>24); \ | ||
485 | accm <<= 8; \ | ||
486 | bitno += 8; \ | ||
487 | } while (bitno <= 24); \ | ||
488 | } | ||
489 | |||
490 | /* | ||
491 | * If the protocol is not in the range we're interested in, | ||
492 | * just return without compressing the packet. If it is, | ||
493 | * the protocol becomes the first byte to compress. | ||
494 | */ | ||
495 | printk(KERN_DEBUG "bsd_compress called with %x\n",proto); | ||
496 | |||
497 | ent = proto; | ||
498 | if (proto < 0x21 || proto > 0xf9 || !(proto & 0x1) ) | ||
499 | return 0; | ||
500 | |||
501 | db = (struct bsd_db *) state; | ||
502 | hshift = db->hshift; | ||
503 | max_ent = db->max_ent; | ||
504 | n_bits = db->n_bits; | ||
505 | bitno = 32; | ||
506 | accm = 0; | ||
507 | mxcode = MAXCODE (n_bits); | ||
508 | |||
509 | /* This is the PPP header information */ | ||
510 | if(skb_out && skb_tailroom(skb_out) >= 2) { | ||
511 | char *v = skb_put(skb_out,2); | ||
512 | /* we only push our own data on the header, | ||
513 | AC,PC and protos is pushed by caller */ | ||
514 | v[0] = db->seqno >> 8; | ||
515 | v[1] = db->seqno; | ||
516 | } | ||
517 | |||
518 | ilen = ++isize; /* This is off by one, but that is what is in draft! */ | ||
519 | |||
520 | while (--ilen > 0) { | ||
521 | c = *rptr++; | ||
522 | fcode = BSD_KEY (ent, c); | ||
523 | hval = BSD_HASH (ent, c, hshift); | ||
524 | dictp = dict_ptr (db, hval); | ||
525 | |||
526 | /* Validate and then check the entry. */ | ||
527 | if (dictp->codem1 >= max_ent) | ||
528 | goto nomatch; | ||
529 | |||
530 | if (dictp->fcode == fcode) { | ||
531 | ent = dictp->codem1 + 1; | ||
532 | continue; /* found (prefix,suffix) */ | ||
533 | } | ||
534 | |||
535 | /* continue probing until a match or invalid entry */ | ||
536 | disp = (hval == 0) ? 1 : hval; | ||
537 | |||
538 | do { | ||
539 | hval += disp; | ||
540 | if (hval >= db->hsize) | ||
541 | hval -= db->hsize; | ||
542 | dictp = dict_ptr (db, hval); | ||
543 | if (dictp->codem1 >= max_ent) | ||
544 | goto nomatch; | ||
545 | } while (dictp->fcode != fcode); | ||
546 | |||
547 | ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */ | ||
548 | continue; | ||
549 | |||
550 | nomatch: | ||
551 | OUTPUT(ent); /* output the prefix */ | ||
552 | |||
553 | /* code -> hashtable */ | ||
554 | if (max_ent < db->maxmaxcode) { | ||
555 | struct bsd_dict *dictp2; | ||
556 | struct bsd_dict *dictp3; | ||
557 | int indx; | ||
558 | |||
559 | /* expand code size if needed */ | ||
560 | if (max_ent >= mxcode) { | ||
561 | db->n_bits = ++n_bits; | ||
562 | mxcode = MAXCODE (n_bits); | ||
563 | } | ||
564 | |||
565 | /* | ||
566 | * Invalidate old hash table entry using | ||
567 | * this code, and then take it over. | ||
568 | */ | ||
569 | dictp2 = dict_ptr (db, max_ent + 1); | ||
570 | indx = dictp2->cptr; | ||
571 | dictp3 = dict_ptr (db, indx); | ||
572 | |||
573 | if (dictp3->codem1 == max_ent) | ||
574 | dictp3->codem1 = BADCODEM1; | ||
575 | |||
576 | dictp2->cptr = hval; | ||
577 | dictp->codem1 = max_ent; | ||
578 | dictp->fcode = fcode; | ||
579 | db->max_ent = ++max_ent; | ||
580 | |||
581 | if (db->lens) { | ||
582 | unsigned short *len1 = lens_ptr (db, max_ent); | ||
583 | unsigned short *len2 = lens_ptr (db, ent); | ||
584 | *len1 = *len2 + 1; | ||
585 | } | ||
586 | } | ||
587 | ent = c; | ||
588 | } | ||
589 | |||
590 | OUTPUT(ent); /* output the last code */ | ||
591 | |||
592 | if(skb_out) | ||
593 | db->bytes_out += skb_out->len; /* Do not count bytes from here */ | ||
594 | db->uncomp_bytes += isize; | ||
595 | db->in_count += isize; | ||
596 | ++db->uncomp_count; | ||
597 | ++db->seqno; | ||
598 | |||
599 | if (bitno < 32) | ||
600 | ++db->bytes_out; /* must be set before calling bsd_check */ | ||
601 | |||
602 | /* | ||
603 | * Generate the clear command if needed | ||
604 | */ | ||
605 | |||
606 | if (bsd_check(db)) | ||
607 | OUTPUT (CLEAR); | ||
608 | |||
609 | /* | ||
610 | * Pad dribble bits of last code with ones. | ||
611 | * Do not emit a completely useless byte of ones. | ||
612 | */ | ||
613 | if (bitno < 32 && skb_out && skb_tailroom(skb_out) > 0) | ||
614 | *(skb_put(skb_out,1)) = (unsigned char) ((accm | (0xff << (bitno-8))) >> 24); | ||
615 | |||
616 | /* | ||
617 | * Increase code size if we would have without the packet | ||
618 | * boundary because the decompressor will do so. | ||
619 | */ | ||
620 | if (max_ent >= mxcode && max_ent < db->maxmaxcode) | ||
621 | db->n_bits++; | ||
622 | |||
623 | /* If output length is too large then this is an incompressible frame. */ | ||
624 | if (!skb_out || (skb_out && skb_out->len >= skb_in->len) ) { | ||
625 | ++db->incomp_count; | ||
626 | db->incomp_bytes += isize; | ||
627 | return 0; | ||
628 | } | ||
629 | |||
630 | /* Count the number of compressed frames */ | ||
631 | ++db->comp_count; | ||
632 | db->comp_bytes += skb_out->len; | ||
633 | return skb_out->len; | ||
634 | |||
635 | #undef OUTPUT | ||
636 | } | ||
637 | |||
638 | /* | ||
639 | * Update the "BSD Compress" dictionary on the receiver for | ||
640 | * incompressible data by pretending to compress the incoming data. | ||
641 | */ | ||
642 | static void bsd_incomp (void *state, struct sk_buff *skb_in,int proto) | ||
643 | { | ||
644 | bsd_compress (state, skb_in, NULL, proto); | ||
645 | } | ||
646 | |||
647 | /* | ||
648 | * Decompress "BSD Compress". | ||
649 | */ | ||
650 | static int bsd_decompress (void *state, struct sk_buff *skb_in, struct sk_buff *skb_out, | ||
651 | struct isdn_ppp_resetparams *rsparm) | ||
652 | { | ||
653 | struct bsd_db *db; | ||
654 | unsigned int max_ent; | ||
655 | unsigned long accm; | ||
656 | unsigned int bitno; /* 1st valid bit in accm */ | ||
657 | unsigned int n_bits; | ||
658 | unsigned int tgtbitno; /* bitno when we have a code */ | ||
659 | struct bsd_dict *dictp; | ||
660 | int seq; | ||
661 | unsigned int incode; | ||
662 | unsigned int oldcode; | ||
663 | unsigned int finchar; | ||
664 | unsigned char *p,*ibuf; | ||
665 | int ilen; | ||
666 | int codelen; | ||
667 | int extra; | ||
668 | |||
669 | db = (struct bsd_db *) state; | ||
670 | max_ent = db->max_ent; | ||
671 | accm = 0; | ||
672 | bitno = 32; /* 1st valid bit in accm */ | ||
673 | n_bits = db->n_bits; | ||
674 | tgtbitno = 32 - n_bits; /* bitno when we have a code */ | ||
675 | |||
676 | printk(KERN_DEBUG "bsd_decompress called\n"); | ||
677 | |||
678 | if(!skb_in || !skb_out) { | ||
679 | printk(KERN_ERR "bsd_decompress called with NULL parameter\n"); | ||
680 | return DECOMP_ERROR; | ||
681 | } | ||
682 | |||
683 | /* | ||
684 | * Get the sequence number. | ||
685 | */ | ||
686 | if( (p = skb_pull(skb_in,2)) == NULL) { | ||
687 | return DECOMP_ERROR; | ||
688 | } | ||
689 | p-=2; | ||
690 | seq = (p[0] << 8) + p[1]; | ||
691 | ilen = skb_in->len; | ||
692 | ibuf = skb_in->data; | ||
693 | |||
694 | /* | ||
695 | * Check the sequence number and give up if it differs from | ||
696 | * the value we're expecting. | ||
697 | */ | ||
698 | if (seq != db->seqno) { | ||
699 | if (db->debug) { | ||
700 | printk(KERN_DEBUG "bsd_decomp%d: bad sequence # %d, expected %d\n", | ||
701 | db->unit, seq, db->seqno - 1); | ||
702 | } | ||
703 | return DECOMP_ERROR; | ||
704 | } | ||
705 | |||
706 | ++db->seqno; | ||
707 | db->bytes_out += ilen; | ||
708 | |||
709 | if(skb_tailroom(skb_out) > 0) | ||
710 | *(skb_put(skb_out,1)) = 0; | ||
711 | else | ||
712 | return DECOMP_ERR_NOMEM; | ||
713 | |||
714 | oldcode = CLEAR; | ||
715 | |||
716 | /* | ||
717 | * Keep the checkpoint correctly so that incompressible packets | ||
718 | * clear the dictionary at the proper times. | ||
719 | */ | ||
720 | |||
721 | for (;;) { | ||
722 | if (ilen-- <= 0) { | ||
723 | db->in_count += (skb_out->len - 1); /* don't count the header */ | ||
724 | break; | ||
725 | } | ||
726 | |||
727 | /* | ||
728 | * Accumulate bytes until we have a complete code. | ||
729 | * Then get the next code, relying on the 32-bit, | ||
730 | * unsigned accm to mask the result. | ||
731 | */ | ||
732 | |||
733 | bitno -= 8; | ||
734 | accm |= *ibuf++ << bitno; | ||
735 | if (tgtbitno < bitno) | ||
736 | continue; | ||
737 | |||
738 | incode = accm >> tgtbitno; | ||
739 | accm <<= n_bits; | ||
740 | bitno += n_bits; | ||
741 | |||
742 | /* | ||
743 | * The dictionary must only be cleared at the end of a packet. | ||
744 | */ | ||
745 | |||
746 | if (incode == CLEAR) { | ||
747 | if (ilen > 0) { | ||
748 | if (db->debug) | ||
749 | printk(KERN_DEBUG "bsd_decomp%d: bad CLEAR\n", db->unit); | ||
750 | return DECOMP_FATALERROR; /* probably a bug */ | ||
751 | } | ||
752 | bsd_clear(db); | ||
753 | break; | ||
754 | } | ||
755 | |||
756 | if ((incode > max_ent + 2) || (incode > db->maxmaxcode) | ||
757 | || (incode > max_ent && oldcode == CLEAR)) { | ||
758 | if (db->debug) { | ||
759 | printk(KERN_DEBUG "bsd_decomp%d: bad code 0x%x oldcode=0x%x ", | ||
760 | db->unit, incode, oldcode); | ||
761 | printk(KERN_DEBUG "max_ent=0x%x skb->Len=%d seqno=%d\n", | ||
762 | max_ent, skb_out->len, db->seqno); | ||
763 | } | ||
764 | return DECOMP_FATALERROR; /* probably a bug */ | ||
765 | } | ||
766 | |||
767 | /* Special case for KwKwK string. */ | ||
768 | if (incode > max_ent) { | ||
769 | finchar = oldcode; | ||
770 | extra = 1; | ||
771 | } else { | ||
772 | finchar = incode; | ||
773 | extra = 0; | ||
774 | } | ||
775 | |||
776 | codelen = *(lens_ptr (db, finchar)); | ||
777 | if( skb_tailroom(skb_out) < codelen + extra) { | ||
778 | if (db->debug) { | ||
779 | printk(KERN_DEBUG "bsd_decomp%d: ran out of mru\n", db->unit); | ||
780 | #ifdef DEBUG | ||
781 | printk(KERN_DEBUG " len=%d, finchar=0x%x, codelen=%d,skblen=%d\n", | ||
782 | ilen, finchar, codelen, skb_out->len); | ||
783 | #endif | ||
784 | } | ||
785 | return DECOMP_FATALERROR; | ||
786 | } | ||
787 | |||
788 | /* | ||
789 | * Decode this code and install it in the decompressed buffer. | ||
790 | */ | ||
791 | |||
792 | p = skb_put(skb_out,codelen); | ||
793 | p += codelen; | ||
794 | while (finchar > LAST) { | ||
795 | struct bsd_dict *dictp2 = dict_ptr (db, finchar); | ||
796 | |||
797 | dictp = dict_ptr (db, dictp2->cptr); | ||
798 | |||
799 | #ifdef DEBUG | ||
800 | if (--codelen <= 0 || dictp->codem1 != finchar-1) { | ||
801 | if (codelen <= 0) { | ||
802 | printk(KERN_ERR "bsd_decomp%d: fell off end of chain ", db->unit); | ||
803 | printk(KERN_ERR "0x%x at 0x%x by 0x%x, max_ent=0x%x\n", incode, finchar, dictp2->cptr, max_ent); | ||
804 | } else { | ||
805 | if (dictp->codem1 != finchar-1) { | ||
806 | printk(KERN_ERR "bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",db->unit, incode, finchar); | ||
807 | printk(KERN_ERR "oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode, dictp2->cptr, dictp->codem1); | ||
808 | } | ||
809 | } | ||
810 | return DECOMP_FATALERROR; | ||
811 | } | ||
812 | #endif | ||
813 | |||
814 | { | ||
815 | u32 fcode = dictp->fcode; | ||
816 | *--p = (fcode >> 16) & 0xff; | ||
817 | finchar = fcode & 0xffff; | ||
818 | } | ||
819 | } | ||
820 | *--p = finchar; | ||
821 | |||
822 | #ifdef DEBUG | ||
823 | if (--codelen != 0) | ||
824 | printk(KERN_ERR "bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n", db->unit, codelen, incode, max_ent); | ||
825 | #endif | ||
826 | |||
827 | if (extra) /* the KwKwK case again */ | ||
828 | *(skb_put(skb_out,1)) = finchar; | ||
829 | |||
830 | /* | ||
831 | * If not first code in a packet, and | ||
832 | * if not out of code space, then allocate a new code. | ||
833 | * | ||
834 | * Keep the hash table correct so it can be used | ||
835 | * with uncompressed packets. | ||
836 | */ | ||
837 | if (oldcode != CLEAR && max_ent < db->maxmaxcode) { | ||
838 | struct bsd_dict *dictp2, *dictp3; | ||
839 | u16 *lens1, *lens2; | ||
840 | unsigned long fcode; | ||
841 | int hval, disp, indx; | ||
842 | |||
843 | fcode = BSD_KEY(oldcode,finchar); | ||
844 | hval = BSD_HASH(oldcode,finchar,db->hshift); | ||
845 | dictp = dict_ptr (db, hval); | ||
846 | |||
847 | /* look for a free hash table entry */ | ||
848 | if (dictp->codem1 < max_ent) { | ||
849 | disp = (hval == 0) ? 1 : hval; | ||
850 | do { | ||
851 | hval += disp; | ||
852 | if (hval >= db->hsize) | ||
853 | hval -= db->hsize; | ||
854 | dictp = dict_ptr (db, hval); | ||
855 | } while (dictp->codem1 < max_ent); | ||
856 | } | ||
857 | |||
858 | /* | ||
859 | * Invalidate previous hash table entry | ||
860 | * assigned this code, and then take it over | ||
861 | */ | ||
862 | |||
863 | dictp2 = dict_ptr (db, max_ent + 1); | ||
864 | indx = dictp2->cptr; | ||
865 | dictp3 = dict_ptr (db, indx); | ||
866 | |||
867 | if (dictp3->codem1 == max_ent) | ||
868 | dictp3->codem1 = BADCODEM1; | ||
869 | |||
870 | dictp2->cptr = hval; | ||
871 | dictp->codem1 = max_ent; | ||
872 | dictp->fcode = fcode; | ||
873 | db->max_ent = ++max_ent; | ||
874 | |||
875 | /* Update the length of this string. */ | ||
876 | lens1 = lens_ptr (db, max_ent); | ||
877 | lens2 = lens_ptr (db, oldcode); | ||
878 | *lens1 = *lens2 + 1; | ||
879 | |||
880 | /* Expand code size if needed. */ | ||
881 | if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) { | ||
882 | db->n_bits = ++n_bits; | ||
883 | tgtbitno = 32-n_bits; | ||
884 | } | ||
885 | } | ||
886 | oldcode = incode; | ||
887 | } | ||
888 | |||
889 | ++db->comp_count; | ||
890 | ++db->uncomp_count; | ||
891 | db->comp_bytes += skb_in->len - BSD_OVHD; | ||
892 | db->uncomp_bytes += skb_out->len; | ||
893 | |||
894 | if (bsd_check(db)) { | ||
895 | if (db->debug) | ||
896 | printk(KERN_DEBUG "bsd_decomp%d: peer should have cleared dictionary on %d\n", | ||
897 | db->unit, db->seqno - 1); | ||
898 | } | ||
899 | return skb_out->len; | ||
900 | } | ||
901 | |||
902 | /************************************************************* | ||
903 | * Table of addresses for the BSD compression module | ||
904 | *************************************************************/ | ||
905 | |||
906 | static struct isdn_ppp_compressor ippp_bsd_compress = { | ||
907 | .owner = THIS_MODULE, | ||
908 | .num = CI_BSD_COMPRESS, | ||
909 | .alloc = bsd_alloc, | ||
910 | .free = bsd_free, | ||
911 | .init = bsd_init, | ||
912 | .reset = bsd_reset, | ||
913 | .compress = bsd_compress, | ||
914 | .decompress = bsd_decompress, | ||
915 | .incomp = bsd_incomp, | ||
916 | .stat = bsd_stats, | ||
917 | }; | ||
918 | |||
919 | /************************************************************* | ||
920 | * Module support routines | ||
921 | *************************************************************/ | ||
922 | |||
923 | static int __init isdn_bsdcomp_init(void) | ||
924 | { | ||
925 | int answer = isdn_ppp_register_compressor (&ippp_bsd_compress); | ||
926 | if (answer == 0) | ||
927 | printk (KERN_INFO "PPP BSD Compression module registered\n"); | ||
928 | return answer; | ||
929 | } | ||
930 | |||
931 | static void __exit isdn_bsdcomp_exit(void) | ||
932 | { | ||
933 | isdn_ppp_unregister_compressor (&ippp_bsd_compress); | ||
934 | } | ||
935 | |||
936 | module_init(isdn_bsdcomp_init); | ||
937 | module_exit(isdn_bsdcomp_exit); | ||