RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / isdn / i4l / isdn_bsdcomp.c
blob90a23795db7e3a64ea24154c03d53b5a57de375f
1 /*
2 * BSD compression module
4 * Patched version for ISDN syncPPP written 1997/1998 by Michael Hipp
5 * The whole module is now SKB based.
7 */
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..
18 * Original copyright notice:
20 * Copyright (c) 1985, 1986 The Regents of the University of California.
21 * All rights reserved.
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.
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.
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.
56 #include <linux/module.h>
57 #include <linux/init.h>
58 #include <linux/kernel.h>
59 #include <linux/types.h>
60 #include <linux/fcntl.h>
61 #include <linux/interrupt.h>
62 #include <linux/ptrace.h>
63 #include <linux/ioport.h>
64 #include <linux/in.h>
65 #include <linux/slab.h>
66 #include <linux/tty.h>
67 #include <linux/errno.h>
68 #include <linux/string.h> /* used in new tty drivers */
69 #include <linux/signal.h> /* used in new tty drivers */
70 #include <linux/bitops.h>
72 #include <asm/system.h>
73 #include <asm/byteorder.h>
74 #include <asm/types.h>
76 #include <linux/if.h>
78 #include <linux/if_ether.h>
79 #include <linux/netdevice.h>
80 #include <linux/skbuff.h>
81 #include <linux/inet.h>
82 #include <linux/ioctl.h>
83 #include <linux/vmalloc.h>
85 #include <linux/ppp_defs.h>
87 #include <linux/isdn.h>
88 #include <linux/isdn_ppp.h>
89 #include <linux/ip.h>
90 #include <linux/tcp.h>
91 #include <linux/if_arp.h>
92 #include <linux/ppp-comp.h>
94 #include "isdn_ppp.h"
96 MODULE_DESCRIPTION("ISDN4Linux: BSD Compression for PPP over ISDN");
97 MODULE_LICENSE("Dual BSD/GPL");
99 #define BSD_VERSION(x) ((x) >> 5)
100 #define BSD_NBITS(x) ((x) & 0x1F)
102 #define BSD_CURRENT_VERSION 1
104 #define DEBUG 1
107 * A dictionary for doing BSD compress.
110 struct bsd_dict {
111 u32 fcode;
112 u16 codem1; /* output of hash table -1 */
113 u16 cptr; /* map code to hash table entry */
116 struct bsd_db {
117 int totlen; /* length of this structure */
118 unsigned int hsize; /* size of the hash table */
119 unsigned char hshift; /* used in hash function */
120 unsigned char n_bits; /* current bits/code */
121 unsigned char maxbits; /* maximum bits/code */
122 unsigned char debug; /* non-zero if debug desired */
123 unsigned char unit; /* ppp unit number */
124 u16 seqno; /* sequence # of next packet */
125 unsigned int mru; /* size of receive (decompress) bufr */
126 unsigned int maxmaxcode; /* largest valid code */
127 unsigned int max_ent; /* largest code in use */
128 unsigned int in_count; /* uncompressed bytes, aged */
129 unsigned int bytes_out; /* compressed bytes, aged */
130 unsigned int ratio; /* recent compression ratio */
131 unsigned int checkpoint; /* when to next check the ratio */
132 unsigned int clear_count; /* times dictionary cleared */
133 unsigned int incomp_count; /* incompressible packets */
134 unsigned int incomp_bytes; /* incompressible bytes */
135 unsigned int uncomp_count; /* uncompressed packets */
136 unsigned int uncomp_bytes; /* uncompressed bytes */
137 unsigned int comp_count; /* compressed packets */
138 unsigned int comp_bytes; /* compressed bytes */
139 unsigned short *lens; /* array of lengths of codes */
140 struct bsd_dict *dict; /* dictionary */
141 int xmit;
144 #define BSD_OVHD 2 /* BSD compress overhead/packet */
145 #define MIN_BSD_BITS 9
146 #define BSD_INIT_BITS MIN_BSD_BITS
147 #define MAX_BSD_BITS 15
150 * the next two codes should not be changed lightly, as they must not
151 * lie within the contiguous general code space.
153 #define CLEAR 256 /* table clear output code */
154 #define FIRST 257 /* first free entry */
155 #define LAST 255
157 #define MAXCODE(b) ((1 << (b)) - 1)
158 #define BADCODEM1 MAXCODE(MAX_BSD_BITS);
160 #define BSD_HASH(prefix,suffix,hshift) ((((unsigned long)(suffix))<<(hshift)) \
161 ^ (unsigned long)(prefix))
162 #define BSD_KEY(prefix,suffix) ((((unsigned long)(suffix)) << 16) \
163 + (unsigned long)(prefix))
165 #define CHECK_GAP 10000 /* Ratio check interval */
167 #define RATIO_SCALE_LOG 8
168 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
169 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
172 * clear the dictionary
175 static void bsd_clear(struct bsd_db *db)
177 db->clear_count++;
178 db->max_ent = FIRST-1;
179 db->n_bits = BSD_INIT_BITS;
180 db->bytes_out = 0;
181 db->in_count = 0;
182 db->incomp_count = 0;
183 db->ratio = 0;
184 db->checkpoint = CHECK_GAP;
188 * If the dictionary is full, then see if it is time to reset it.
190 * Compute the compression ratio using fixed-point arithmetic
191 * with 8 fractional bits.
193 * Since we have an infinite stream instead of a single file,
194 * watch only the local compression ratio.
196 * Since both peers must reset the dictionary at the same time even in
197 * the absence of CLEAR codes (while packets are incompressible), they
198 * must compute the same ratio.
200 static int bsd_check (struct bsd_db *db) /* 1=output CLEAR */
202 unsigned int new_ratio;
204 if (db->in_count >= db->checkpoint)
206 /* age the ratio by limiting the size of the counts */
207 if (db->in_count >= RATIO_MAX || db->bytes_out >= RATIO_MAX)
209 db->in_count -= (db->in_count >> 2);
210 db->bytes_out -= (db->bytes_out >> 2);
213 db->checkpoint = db->in_count + CHECK_GAP;
215 if (db->max_ent >= db->maxmaxcode)
217 /* Reset the dictionary only if the ratio is worse,
218 * or if it looks as if it has been poisoned
219 * by incompressible data.
221 * This does not overflow, because
222 * db->in_count <= RATIO_MAX.
225 new_ratio = db->in_count << RATIO_SCALE_LOG;
226 if (db->bytes_out != 0)
228 new_ratio /= db->bytes_out;
231 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE)
233 bsd_clear (db);
234 return 1;
236 db->ratio = new_ratio;
239 return 0;
243 * Return statistics.
246 static void bsd_stats (void *state, struct compstat *stats)
248 struct bsd_db *db = (struct bsd_db *) state;
250 stats->unc_bytes = db->uncomp_bytes;
251 stats->unc_packets = db->uncomp_count;
252 stats->comp_bytes = db->comp_bytes;
253 stats->comp_packets = db->comp_count;
254 stats->inc_bytes = db->incomp_bytes;
255 stats->inc_packets = db->incomp_count;
256 stats->in_count = db->in_count;
257 stats->bytes_out = db->bytes_out;
261 * Reset state, as on a CCP ResetReq.
263 static void bsd_reset (void *state,unsigned char code, unsigned char id,
264 unsigned char *data, unsigned len,
265 struct isdn_ppp_resetparams *rsparm)
267 struct bsd_db *db = (struct bsd_db *) state;
269 bsd_clear(db);
270 db->seqno = 0;
271 db->clear_count = 0;
275 * Release the compression structure
277 static void bsd_free (void *state)
279 struct bsd_db *db = (struct bsd_db *) state;
281 if (db) {
283 * Release the dictionary
285 vfree(db->dict);
286 db->dict = NULL;
289 * Release the string buffer
291 vfree(db->lens);
292 db->lens = NULL;
295 * Finally release the structure itself.
297 kfree(db);
303 * Allocate space for a (de) compressor.
305 static void *bsd_alloc (struct isdn_ppp_comp_data *data)
307 int bits;
308 unsigned int hsize, hshift, maxmaxcode;
309 struct bsd_db *db;
310 int decomp;
312 static unsigned int htab[][2] = {
313 { 5003 , 4 } , { 5003 , 4 } , { 5003 , 4 } , { 5003 , 4 } ,
314 { 9001 , 5 } , { 18013 , 6 } , { 35023 , 7 } , { 69001 , 8 }
317 if (data->optlen != 1 || data->num != CI_BSD_COMPRESS
318 || BSD_VERSION(data->options[0]) != BSD_CURRENT_VERSION)
319 return NULL;
321 bits = BSD_NBITS(data->options[0]);
323 if(bits < 9 || bits > 15)
324 return NULL;
326 hsize = htab[bits-9][0];
327 hshift = htab[bits-9][1];
330 * Allocate the main control structure for this instance.
332 maxmaxcode = MAXCODE(bits);
333 db = kzalloc (sizeof (struct bsd_db),GFP_KERNEL);
334 if (!db)
335 return NULL;
337 db->xmit = data->flags & IPPP_COMP_FLAG_XMIT;
338 decomp = db->xmit ? 0 : 1;
341 * Allocate space for the dictionary. This may be more than one page in
342 * length.
344 db->dict = (struct bsd_dict *) vmalloc (hsize * sizeof (struct bsd_dict));
345 if (!db->dict) {
346 bsd_free (db);
347 return NULL;
351 * If this is the compression buffer then there is no length data.
352 * For decompression, the length information is needed as well.
354 if (!decomp)
355 db->lens = NULL;
356 else {
357 db->lens = (unsigned short *) vmalloc ((maxmaxcode + 1) *
358 sizeof (db->lens[0]));
359 if (!db->lens) {
360 bsd_free (db);
361 return (NULL);
366 * Initialize the data information for the compression code
368 db->totlen = sizeof (struct bsd_db) + (sizeof (struct bsd_dict) * hsize);
369 db->hsize = hsize;
370 db->hshift = hshift;
371 db->maxmaxcode = maxmaxcode;
372 db->maxbits = bits;
374 return (void *) db;
378 * Initialize the database.
380 static int bsd_init (void *state, struct isdn_ppp_comp_data *data, int unit, int debug)
382 struct bsd_db *db = state;
383 int indx;
384 int decomp;
386 if(!state || !data) {
387 printk(KERN_ERR "isdn_bsd_init: [%d] ERR, state %lx data %lx\n",unit,(long)state,(long)data);
388 return 0;
391 decomp = db->xmit ? 0 : 1;
393 if (data->optlen != 1 || data->num != CI_BSD_COMPRESS
394 || (BSD_VERSION(data->options[0]) != BSD_CURRENT_VERSION)
395 || (BSD_NBITS(data->options[0]) != db->maxbits)
396 || (decomp && db->lens == NULL)) {
397 printk(KERN_ERR "isdn_bsd: %d %d %d %d %lx\n",data->optlen,data->num,data->options[0],decomp,(unsigned long)db->lens);
398 return 0;
401 if (decomp)
402 for(indx=LAST;indx>=0;indx--)
403 db->lens[indx] = 1;
405 indx = db->hsize;
406 while (indx-- != 0) {
407 db->dict[indx].codem1 = BADCODEM1;
408 db->dict[indx].cptr = 0;
411 db->unit = unit;
412 db->mru = 0;
414 db->debug = 1;
416 bsd_reset(db,0,0,NULL,0,NULL);
418 return 1;
422 * Obtain pointers to the various structures in the compression tables
425 #define dict_ptrx(p,idx) &(p->dict[idx])
426 #define lens_ptrx(p,idx) &(p->lens[idx])
428 #ifdef DEBUG
429 static unsigned short *lens_ptr(struct bsd_db *db, int idx)
431 if ((unsigned int) idx > (unsigned int) db->maxmaxcode) {
432 printk (KERN_DEBUG "<9>ppp: lens_ptr(%d) > max\n", idx);
433 idx = 0;
435 return lens_ptrx (db, idx);
438 static struct bsd_dict *dict_ptr(struct bsd_db *db, int idx)
440 if ((unsigned int) idx >= (unsigned int) db->hsize) {
441 printk (KERN_DEBUG "<9>ppp: dict_ptr(%d) > max\n", idx);
442 idx = 0;
444 return dict_ptrx (db, idx);
447 #else
448 #define lens_ptr(db,idx) lens_ptrx(db,idx)
449 #define dict_ptr(db,idx) dict_ptrx(db,idx)
450 #endif
453 * compress a packet
455 static int bsd_compress (void *state, struct sk_buff *skb_in, struct sk_buff *skb_out,int proto)
457 struct bsd_db *db;
458 int hshift;
459 unsigned int max_ent;
460 unsigned int n_bits;
461 unsigned int bitno;
462 unsigned long accm;
463 int ent;
464 unsigned long fcode;
465 struct bsd_dict *dictp;
466 unsigned char c;
467 int hval,disp,ilen,mxcode;
468 unsigned char *rptr = skb_in->data;
469 int isize = skb_in->len;
471 #define OUTPUT(ent) \
473 bitno -= n_bits; \
474 accm |= ((ent) << bitno); \
475 do { \
476 if(skb_out && skb_tailroom(skb_out) > 0) \
477 *(skb_put(skb_out,1)) = (unsigned char) (accm>>24); \
478 accm <<= 8; \
479 bitno += 8; \
480 } while (bitno <= 24); \
484 * If the protocol is not in the range we're interested in,
485 * just return without compressing the packet. If it is,
486 * the protocol becomes the first byte to compress.
488 printk(KERN_DEBUG "bsd_compress called with %x\n",proto);
490 ent = proto;
491 if (proto < 0x21 || proto > 0xf9 || !(proto & 0x1) )
492 return 0;
494 db = (struct bsd_db *) state;
495 hshift = db->hshift;
496 max_ent = db->max_ent;
497 n_bits = db->n_bits;
498 bitno = 32;
499 accm = 0;
500 mxcode = MAXCODE (n_bits);
502 /* This is the PPP header information */
503 if(skb_out && skb_tailroom(skb_out) >= 2) {
504 char *v = skb_put(skb_out,2);
505 /* we only push our own data on the header,
506 AC,PC and protos is pushed by caller */
507 v[0] = db->seqno >> 8;
508 v[1] = db->seqno;
511 ilen = ++isize; /* This is off by one, but that is what is in draft! */
513 while (--ilen > 0) {
514 c = *rptr++;
515 fcode = BSD_KEY (ent, c);
516 hval = BSD_HASH (ent, c, hshift);
517 dictp = dict_ptr (db, hval);
519 /* Validate and then check the entry. */
520 if (dictp->codem1 >= max_ent)
521 goto nomatch;
523 if (dictp->fcode == fcode) {
524 ent = dictp->codem1 + 1;
525 continue; /* found (prefix,suffix) */
528 /* continue probing until a match or invalid entry */
529 disp = (hval == 0) ? 1 : hval;
531 do {
532 hval += disp;
533 if (hval >= db->hsize)
534 hval -= db->hsize;
535 dictp = dict_ptr (db, hval);
536 if (dictp->codem1 >= max_ent)
537 goto nomatch;
538 } while (dictp->fcode != fcode);
540 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
541 continue;
543 nomatch:
544 OUTPUT(ent); /* output the prefix */
546 /* code -> hashtable */
547 if (max_ent < db->maxmaxcode) {
548 struct bsd_dict *dictp2;
549 struct bsd_dict *dictp3;
550 int indx;
552 /* expand code size if needed */
553 if (max_ent >= mxcode) {
554 db->n_bits = ++n_bits;
555 mxcode = MAXCODE (n_bits);
559 * Invalidate old hash table entry using
560 * this code, and then take it over.
562 dictp2 = dict_ptr (db, max_ent + 1);
563 indx = dictp2->cptr;
564 dictp3 = dict_ptr (db, indx);
566 if (dictp3->codem1 == max_ent)
567 dictp3->codem1 = BADCODEM1;
569 dictp2->cptr = hval;
570 dictp->codem1 = max_ent;
571 dictp->fcode = fcode;
572 db->max_ent = ++max_ent;
574 if (db->lens) {
575 unsigned short *len1 = lens_ptr (db, max_ent);
576 unsigned short *len2 = lens_ptr (db, ent);
577 *len1 = *len2 + 1;
580 ent = c;
583 OUTPUT(ent); /* output the last code */
585 if(skb_out)
586 db->bytes_out += skb_out->len; /* Do not count bytes from here */
587 db->uncomp_bytes += isize;
588 db->in_count += isize;
589 ++db->uncomp_count;
590 ++db->seqno;
592 if (bitno < 32)
593 ++db->bytes_out; /* must be set before calling bsd_check */
596 * Generate the clear command if needed
599 if (bsd_check(db))
600 OUTPUT (CLEAR);
603 * Pad dribble bits of last code with ones.
604 * Do not emit a completely useless byte of ones.
606 if (bitno < 32 && skb_out && skb_tailroom(skb_out) > 0)
607 *(skb_put(skb_out,1)) = (unsigned char) ((accm | (0xff << (bitno-8))) >> 24);
610 * Increase code size if we would have without the packet
611 * boundary because the decompressor will do so.
613 if (max_ent >= mxcode && max_ent < db->maxmaxcode)
614 db->n_bits++;
616 /* If output length is too large then this is an incompressible frame. */
617 if (!skb_out || (skb_out && skb_out->len >= skb_in->len) ) {
618 ++db->incomp_count;
619 db->incomp_bytes += isize;
620 return 0;
623 /* Count the number of compressed frames */
624 ++db->comp_count;
625 db->comp_bytes += skb_out->len;
626 return skb_out->len;
628 #undef OUTPUT
632 * Update the "BSD Compress" dictionary on the receiver for
633 * incompressible data by pretending to compress the incoming data.
635 static void bsd_incomp (void *state, struct sk_buff *skb_in,int proto)
637 bsd_compress (state, skb_in, NULL, proto);
641 * Decompress "BSD Compress".
643 static int bsd_decompress (void *state, struct sk_buff *skb_in, struct sk_buff *skb_out,
644 struct isdn_ppp_resetparams *rsparm)
646 struct bsd_db *db;
647 unsigned int max_ent;
648 unsigned long accm;
649 unsigned int bitno; /* 1st valid bit in accm */
650 unsigned int n_bits;
651 unsigned int tgtbitno; /* bitno when we have a code */
652 struct bsd_dict *dictp;
653 int seq;
654 unsigned int incode;
655 unsigned int oldcode;
656 unsigned int finchar;
657 unsigned char *p,*ibuf;
658 int ilen;
659 int codelen;
660 int extra;
662 db = (struct bsd_db *) state;
663 max_ent = db->max_ent;
664 accm = 0;
665 bitno = 32; /* 1st valid bit in accm */
666 n_bits = db->n_bits;
667 tgtbitno = 32 - n_bits; /* bitno when we have a code */
669 printk(KERN_DEBUG "bsd_decompress called\n");
671 if(!skb_in || !skb_out) {
672 printk(KERN_ERR "bsd_decompress called with NULL parameter\n");
673 return DECOMP_ERROR;
677 * Get the sequence number.
679 if( (p = skb_pull(skb_in,2)) == NULL) {
680 return DECOMP_ERROR;
682 p-=2;
683 seq = (p[0] << 8) + p[1];
684 ilen = skb_in->len;
685 ibuf = skb_in->data;
688 * Check the sequence number and give up if it differs from
689 * the value we're expecting.
691 if (seq != db->seqno) {
692 if (db->debug) {
693 printk(KERN_DEBUG "bsd_decomp%d: bad sequence # %d, expected %d\n",
694 db->unit, seq, db->seqno - 1);
696 return DECOMP_ERROR;
699 ++db->seqno;
700 db->bytes_out += ilen;
702 if(skb_tailroom(skb_out) > 0)
703 *(skb_put(skb_out,1)) = 0;
704 else
705 return DECOMP_ERR_NOMEM;
707 oldcode = CLEAR;
710 * Keep the checkpoint correctly so that incompressible packets
711 * clear the dictionary at the proper times.
714 for (;;) {
715 if (ilen-- <= 0) {
716 db->in_count += (skb_out->len - 1); /* don't count the header */
717 break;
721 * Accumulate bytes until we have a complete code.
722 * Then get the next code, relying on the 32-bit,
723 * unsigned accm to mask the result.
726 bitno -= 8;
727 accm |= *ibuf++ << bitno;
728 if (tgtbitno < bitno)
729 continue;
731 incode = accm >> tgtbitno;
732 accm <<= n_bits;
733 bitno += n_bits;
736 * The dictionary must only be cleared at the end of a packet.
739 if (incode == CLEAR) {
740 if (ilen > 0) {
741 if (db->debug)
742 printk(KERN_DEBUG "bsd_decomp%d: bad CLEAR\n", db->unit);
743 return DECOMP_FATALERROR; /* probably a bug */
745 bsd_clear(db);
746 break;
749 if ((incode > max_ent + 2) || (incode > db->maxmaxcode)
750 || (incode > max_ent && oldcode == CLEAR)) {
751 if (db->debug) {
752 printk(KERN_DEBUG "bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
753 db->unit, incode, oldcode);
754 printk(KERN_DEBUG "max_ent=0x%x skb->Len=%d seqno=%d\n",
755 max_ent, skb_out->len, db->seqno);
757 return DECOMP_FATALERROR; /* probably a bug */
760 /* Special case for KwKwK string. */
761 if (incode > max_ent) {
762 finchar = oldcode;
763 extra = 1;
764 } else {
765 finchar = incode;
766 extra = 0;
769 codelen = *(lens_ptr (db, finchar));
770 if( skb_tailroom(skb_out) < codelen + extra) {
771 if (db->debug) {
772 printk(KERN_DEBUG "bsd_decomp%d: ran out of mru\n", db->unit);
773 #ifdef DEBUG
774 printk(KERN_DEBUG " len=%d, finchar=0x%x, codelen=%d,skblen=%d\n",
775 ilen, finchar, codelen, skb_out->len);
776 #endif
778 return DECOMP_FATALERROR;
782 * Decode this code and install it in the decompressed buffer.
785 p = skb_put(skb_out,codelen);
786 p += codelen;
787 while (finchar > LAST) {
788 struct bsd_dict *dictp2 = dict_ptr (db, finchar);
790 dictp = dict_ptr (db, dictp2->cptr);
792 #ifdef DEBUG
793 if (--codelen <= 0 || dictp->codem1 != finchar-1) {
794 if (codelen <= 0) {
795 printk(KERN_ERR "bsd_decomp%d: fell off end of chain ", db->unit);
796 printk(KERN_ERR "0x%x at 0x%x by 0x%x, max_ent=0x%x\n", incode, finchar, dictp2->cptr, max_ent);
797 } else {
798 if (dictp->codem1 != finchar-1) {
799 printk(KERN_ERR "bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",db->unit, incode, finchar);
800 printk(KERN_ERR "oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode, dictp2->cptr, dictp->codem1);
803 return DECOMP_FATALERROR;
805 #endif
808 u32 fcode = dictp->fcode;
809 *--p = (fcode >> 16) & 0xff;
810 finchar = fcode & 0xffff;
813 *--p = finchar;
815 #ifdef DEBUG
816 if (--codelen != 0)
817 printk(KERN_ERR "bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n", db->unit, codelen, incode, max_ent);
818 #endif
820 if (extra) /* the KwKwK case again */
821 *(skb_put(skb_out,1)) = finchar;
824 * If not first code in a packet, and
825 * if not out of code space, then allocate a new code.
827 * Keep the hash table correct so it can be used
828 * with uncompressed packets.
830 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
831 struct bsd_dict *dictp2, *dictp3;
832 u16 *lens1, *lens2;
833 unsigned long fcode;
834 int hval, disp, indx;
836 fcode = BSD_KEY(oldcode,finchar);
837 hval = BSD_HASH(oldcode,finchar,db->hshift);
838 dictp = dict_ptr (db, hval);
840 /* look for a free hash table entry */
841 if (dictp->codem1 < max_ent) {
842 disp = (hval == 0) ? 1 : hval;
843 do {
844 hval += disp;
845 if (hval >= db->hsize)
846 hval -= db->hsize;
847 dictp = dict_ptr (db, hval);
848 } while (dictp->codem1 < max_ent);
852 * Invalidate previous hash table entry
853 * assigned this code, and then take it over
856 dictp2 = dict_ptr (db, max_ent + 1);
857 indx = dictp2->cptr;
858 dictp3 = dict_ptr (db, indx);
860 if (dictp3->codem1 == max_ent)
861 dictp3->codem1 = BADCODEM1;
863 dictp2->cptr = hval;
864 dictp->codem1 = max_ent;
865 dictp->fcode = fcode;
866 db->max_ent = ++max_ent;
868 /* Update the length of this string. */
869 lens1 = lens_ptr (db, max_ent);
870 lens2 = lens_ptr (db, oldcode);
871 *lens1 = *lens2 + 1;
873 /* Expand code size if needed. */
874 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
875 db->n_bits = ++n_bits;
876 tgtbitno = 32-n_bits;
879 oldcode = incode;
882 ++db->comp_count;
883 ++db->uncomp_count;
884 db->comp_bytes += skb_in->len - BSD_OVHD;
885 db->uncomp_bytes += skb_out->len;
887 if (bsd_check(db)) {
888 if (db->debug)
889 printk(KERN_DEBUG "bsd_decomp%d: peer should have cleared dictionary on %d\n",
890 db->unit, db->seqno - 1);
892 return skb_out->len;
895 /*************************************************************
896 * Table of addresses for the BSD compression module
897 *************************************************************/
899 static struct isdn_ppp_compressor ippp_bsd_compress = {
900 .owner = THIS_MODULE,
901 .num = CI_BSD_COMPRESS,
902 .alloc = bsd_alloc,
903 .free = bsd_free,
904 .init = bsd_init,
905 .reset = bsd_reset,
906 .compress = bsd_compress,
907 .decompress = bsd_decompress,
908 .incomp = bsd_incomp,
909 .stat = bsd_stats,
912 /*************************************************************
913 * Module support routines
914 *************************************************************/
916 static int __init isdn_bsdcomp_init(void)
918 int answer = isdn_ppp_register_compressor (&ippp_bsd_compress);
919 if (answer == 0)
920 printk (KERN_INFO "PPP BSD Compression module registered\n");
921 return answer;
924 static void __exit isdn_bsdcomp_exit(void)
926 isdn_ppp_unregister_compressor (&ippp_bsd_compress);
929 module_init(isdn_bsdcomp_init);
930 module_exit(isdn_bsdcomp_exit);