56 #include <linux/module.h>
58 #include <linux/kernel.h>
59 #include <linux/types.h>
60 #include <linux/fcntl.h>
62 #include <linux/ptrace.h>
65 #include <linux/slab.h>
66 #include <linux/tty.h>
67 #include <linux/errno.h>
68 #include <linux/string.h>
69 #include <linux/signal.h>
70 #include <linux/bitops.h>
72 #include <asm/byteorder.h>
73 #include <asm/types.h>
77 #include <linux/if_ether.h>
78 #include <linux/netdevice.h>
84 #include <linux/ppp_defs.h>
86 #include <linux/isdn.h>
87 #include <linux/isdn_ppp.h>
89 #include <linux/tcp.h>
90 #include <linux/if_arp.h>
91 #include <linux/ppp-comp.h>
98 #define BSD_VERSION(x) ((x) >> 5)
99 #define BSD_NBITS(x) ((x) & 0x1F)
101 #define BSD_CURRENT_VERSION 1
144 #define MIN_BSD_BITS 9
145 #define BSD_INIT_BITS MIN_BSD_BITS
146 #define MAX_BSD_BITS 15
156 #define MAXCODE(b) ((1 << (b)) - 1)
157 #define BADCODEM1 MAXCODE(MAX_BSD_BITS)
159 #define BSD_HASH(prefix, suffix, hshift) ((((unsigned long)(suffix)) << (hshift)) \
160 ^ (unsigned long)(prefix))
161 #define BSD_KEY(prefix, suffix) ((((unsigned long)(suffix)) << 16) \
162 + (unsigned long)(prefix))
164 #define CHECK_GAP 10000
166 #define RATIO_SCALE_LOG 8
167 #define RATIO_SCALE (1 << RATIO_SCALE_LOG)
168 #define RATIO_MAX (0x7fffffff >> RATIO_SCALE_LOG)
174 static void bsd_clear(
struct bsd_db *db)
199 static int bsd_check(
struct bsd_db *db)
201 unsigned int new_ratio;
235 db->
ratio = new_ratio;
262 static void bsd_reset(
void *
state,
unsigned char code,
unsigned char id,
263 unsigned char *
data,
unsigned len,
276 static void bsd_free(
void *
state)
311 static unsigned int htab[][2] = {
312 { 5003 , 4 } , { 5003 , 4 } , { 5003 , 4 } , { 5003 , 4 } ,
313 { 9001 , 5 } , { 18013 , 6 } , { 35023 , 7 } , { 69001 , 8 }
322 if (bits < 9 || bits > 15)
325 hsize = htab[bits - 9][0];
326 hshift = htab[bits - 9][1];
337 decomp = db->
xmit ? 0 : 1;
384 if (!state || !data) {
385 printk(
KERN_ERR "isdn_bsd_init: [%d] ERR, state %lx data %lx\n", unit, (
long)state, (
long)data);
389 decomp = db->
xmit ? 0 : 1;
400 for (indx =
LAST; indx >= 0; indx--)
404 while (indx-- != 0) {
406 db->
dict[indx].cptr = 0;
423 #define dict_ptrx(p, idx) &(p->dict[idx])
424 #define lens_ptrx(p, idx) &(p->lens[idx])
429 if ((
unsigned int) idx > (
unsigned int) db->
maxmaxcode) {
438 if ((
unsigned int) idx >= (
unsigned int) db->
hsize) {
446 #define lens_ptr(db, idx) lens_ptrx(db, idx)
447 #define dict_ptr(db, idx) dict_ptrx(db, idx)
453 static int bsd_compress(
void *state,
struct sk_buff *skb_in,
struct sk_buff *skb_out,
int proto)
465 int hval, disp, ilen, mxcode;
467 int isize = skb_in->
len;
469 #define OUTPUT(ent) \
472 accm |= ((ent) << bitno); \
474 if (skb_out && skb_tailroom(skb_out) > 0) \
475 *(skb_put(skb_out, 1)) = (unsigned char)(accm >> 24); \
478 } while (bitno <= 24); \
492 db = (
struct bsd_db *) state;
501 if (skb_out && skb_tailroom(skb_out) >= 2) {
505 v[0] = db->
seqno >> 8;
518 if (dictp->
codem1 >= max_ent)
521 if (dictp->
fcode == fcode) {
527 disp = (hval == 0) ? 1 : hval;
531 if (hval >= db->
hsize)
534 if (dictp->
codem1 >= max_ent)
536 }
while (dictp->
fcode != fcode);
545 if (max_ent < db->maxmaxcode) {
551 if (max_ent >= mxcode) {
564 if (dictp3->
codem1 == max_ent)
573 unsigned short *len1 =
lens_ptr(db, max_ent);
574 unsigned short *len2 =
lens_ptr(db, ent);
604 if (bitno < 32 && skb_out && skb_tailroom(skb_out) > 0)
605 *(
skb_put(skb_out, 1)) = (
unsigned char)((accm | (0xff << (bitno - 8))) >> 24);
611 if (max_ent >= mxcode && max_ent < db->maxmaxcode)
615 if (!skb_out || skb_out->
len >= skb_in->
len) {
633 static void bsd_incomp(
void *state,
struct sk_buff *skb_in,
int proto)
635 bsd_compress(state, skb_in,
NULL, proto);
641 static int bsd_decompress(
void *state,
struct sk_buff *skb_in,
struct sk_buff *skb_out,
649 unsigned int tgtbitno;
653 unsigned int oldcode;
654 unsigned int finchar;
655 unsigned char *
p, *ibuf;
660 db = (
struct bsd_db *) state;
669 if (!skb_in || !skb_out) {
681 seq = (p[0] << 8) + p[1];
689 if (seq != db->
seqno) {
700 if (skb_tailroom(skb_out) > 0)
725 accm |= *ibuf++ << bitno;
726 if (tgtbitno < bitno)
729 incode = accm >> tgtbitno;
737 if (incode ==
CLEAR) {
747 if ((incode > max_ent + 2) || (incode > db->
maxmaxcode)
748 || (incode > max_ent && oldcode ==
CLEAR)) {
751 db->
unit, incode, oldcode);
759 if (incode > max_ent) {
768 if (skb_tailroom(skb_out) < codelen +
extra) {
773 ilen, finchar, codelen, skb_out->
len);
785 while (finchar >
LAST) {
791 if (--codelen <= 0 || dictp->
codem1 != finchar - 1) {
794 printk(
KERN_ERR "0x%x at 0x%x by 0x%x, max_ent=0x%x\n", incode, finchar, dictp2->
cptr, max_ent);
796 if (dictp->
codem1 != finchar - 1) {
797 printk(
KERN_ERR "bsd_decomp%d: bad code chain 0x%x finchar=0x%x ", db->
unit, incode, finchar);
807 *--p = (fcode >> 16) & 0xff;
808 finchar = fcode & 0xffff;
815 printk(
KERN_ERR "bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n", db->
unit, codelen, incode, max_ent);
819 *(
skb_put(skb_out, 1)) = finchar;
828 if (oldcode !=
CLEAR && max_ent < db->maxmaxcode) {
832 int hval, disp, indx;
834 fcode =
BSD_KEY(oldcode, finchar);
839 if (dictp->
codem1 < max_ent) {
840 disp = (hval == 0) ? 1 : hval;
843 if (hval >= db->
hsize)
846 }
while (dictp->
codem1 < max_ent);
858 if (dictp3->
codem1 == max_ent)
872 if (max_ent >=
MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
874 tgtbitno = 32-n_bits;
887 printk(
KERN_DEBUG "bsd_decomp%d: peer should have cleared dictionary on %d\n",
904 .compress = bsd_compress,
905 .decompress = bsd_decompress,
906 .incomp = bsd_incomp,
914 static int __init isdn_bsdcomp_init(
void)
922 static void __exit isdn_bsdcomp_exit(
void)