64 #include <linux/module.h>
66 #include <linux/slab.h>
68 #include <linux/string.h>
70 #include <linux/ppp_defs.h>
74 #include <linux/ppp-comp.h>
77 #include <asm/byteorder.h>
106 #define BSD_VERSION(x) ((x) >> 5)
107 #define BSD_NBITS(x) ((x) & 0x1F)
109 #define BSD_CURRENT_VERSION 1
119 #if defined(__LITTLE_ENDIAN)
121 unsigned char suffix;
123 #elif defined(__BIG_ENDIAN)
125 unsigned char suffix;
128 #error Endianness not defined...
159 unsigned short *
lens;
164 #define MIN_BSD_BITS 9
165 #define BSD_INIT_BITS MIN_BSD_BITS
166 #define MAX_BSD_BITS 15
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);
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,
181 static void bsd_reset (
void *
state);
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);
188 static int bsd_decompress (
void *
state,
unsigned char *ibuf,
int isize,
189 unsigned char *obuf,
int osize);
203 #define MAXCODE(b) ((1 << (b)) - 1)
204 #define BADCODEM1 MAXCODE(MAX_BSD_BITS)
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))
211 #define CHECK_GAP 10000
213 #define RATIO_SCALE_LOG 8
214 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
215 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
222 bsd_clear(
struct bsd_db *db)
247 static int bsd_check (
struct bsd_db *db)
249 unsigned int new_ratio;
283 db->
ratio = new_ratio;
311 static void bsd_reset (
void *
state)
325 static void bsd_free (
void *
state)
352 static void *bsd_alloc (
unsigned char *
options,
int opt_len,
int decomp)
398 db = kzalloc(
sizeof (
struct bsd_db),
449 static void *bsd_comp_alloc (
unsigned char *options,
int opt_len)
451 return bsd_alloc (options, opt_len, 0);
454 static void *bsd_decomp_alloc (
unsigned char *options,
int opt_len)
456 return bsd_alloc (options, opt_len, 1);
463 static int bsd_init (
void *state,
unsigned char *options,
464 int opt_len,
int unit,
int debug,
int decomp)
469 if ((opt_len != 3) || (options[0] !=
CI_BSD_COMPRESS) || (options[1] != 3)
491 db->
dict[indx].cptr = 0;
506 static int bsd_comp_init (
void *state,
unsigned char *options,
507 int opt_len,
int unit,
int opthdr,
int debug)
509 return bsd_init (state, options, opt_len, unit, debug, 0);
512 static int bsd_decomp_init (
void *state,
unsigned char *options,
513 int opt_len,
int unit,
int opthdr,
int mru,
516 return bsd_init (state, options, opt_len, unit, debug, 1);
523 #define dict_ptrx(p,idx) &(p->dict[idx])
524 #define lens_ptrx(p,idx) &(p->lens[idx])
529 if ((
unsigned int) idx > (
unsigned int) db->
maxmaxcode)
531 printk (
"<9>ppp: lens_ptr(%d) > max\n", idx);
539 if ((
unsigned int) idx >= (
unsigned int) db->
hsize)
541 printk (
"<9>ppp: dict_ptr(%d) > max\n", idx);
548 #define lens_ptr(db,idx) lens_ptrx(db,idx)
549 #define dict_ptr(db,idx) dict_ptrx(db,idx)
563 static int bsd_compress (
void *state,
unsigned char *
rptr,
unsigned char *obuf,
564 int isize,
int osize)
588 *wptr++ = (unsigned char) (v); \
596 #define OUTPUT(ent) \
599 accm |= ((ent) << bitno); \
602 PUTBYTE(accm >> 24); \
606 while (bitno <= 24); \
621 db = (
struct bsd_db *) state;
645 *wptr++ = db->
seqno >> 8;
662 if (dictp->
codem1 >= max_ent)
667 if (dictp->
f.
fcode == fcode)
674 disp = (hval == 0) ? 1 : hval;
679 if (hval >= db->
hsize)
684 if (dictp->
codem1 >= max_ent)
689 while (dictp->
f.
fcode != fcode);
698 if (max_ent < db->maxmaxcode)
705 if (max_ent >= mxcode)
715 dictp2 =
dict_ptr (db, max_ent + 1);
719 if (dictp3->
codem1 == max_ent)
731 unsigned short *len1 =
lens_ptr (db, max_ent);
732 unsigned short *len2 =
lens_ptr (db, ent);
768 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
776 if (max_ent >= mxcode && max_ent < db->maxmaxcode)
805 static void bsd_incomp (
void *state,
unsigned char *ibuf,
int icnt)
807 (
void) bsd_compress (state, ibuf, (
char *) 0,
icnt, 0);
827 static int bsd_decompress (
void *state,
unsigned char *ibuf,
int isize,
828 unsigned char *obuf,
int osize)
835 unsigned int tgtbitno;
840 unsigned int oldcode;
841 unsigned int finchar;
850 db = (
struct bsd_db *) state;
865 seq = (ibuf[4] << 8) + ibuf[5];
875 if (seq != db->
seqno)
879 printk(
"bsd_decomp%d: bad sequence # %d, expected %d\n",
921 accm |= *ibuf++ << bitno;
922 if (tgtbitno < bitno)
927 incode = accm >> tgtbitno;
941 printk(
"bsd_decomp%d: bad CLEAR\n", db->
unit);
950 if ((incode > max_ent + 2) || (incode > db->
maxmaxcode)
951 || (incode > max_ent && oldcode ==
CLEAR))
955 printk(
"bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
956 db->
unit, incode, oldcode);
957 printk(
"max_ent=0x%x explen=%d seqno=%d\n",
958 max_ent, explen, db->
seqno);
964 if (incode > max_ent)
975 codelen = *(
lens_ptr (db, finchar));
976 explen += codelen +
extra;
981 printk(
"bsd_decomp%d: ran out of mru\n", db->
unit);
983 printk(
" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
984 ilen, finchar, codelen, explen);
996 while (finchar >
LAST)
1002 if (--codelen <= 0 || dictp->
codem1 != finchar-1)
1006 printk(
"bsd_decomp%d: fell off end of chain ", db->
unit);
1007 printk(
"0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1008 incode, finchar, dictp2->
cptr, max_ent);
1012 if (dictp->
codem1 != finchar-1)
1014 printk(
"bsd_decomp%d: bad code chain 0x%x "
1016 db->
unit, incode, finchar);
1018 printk(
"oldcode=0x%x cptr=0x%x codem1=0x%x\n",
1025 *--p = dictp->
f.
hs.suffix;
1026 finchar = dictp->
f.
hs.prefix;
1033 printk(
"bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1034 db->
unit, codelen, incode, max_ent);
1051 if (oldcode !=
CLEAR && max_ent < db->maxmaxcode)
1054 unsigned short *lens1, *lens2;
1055 unsigned long fcode;
1056 int hval, disp, indx;
1058 fcode =
BSD_KEY(oldcode,finchar);
1063 if (dictp->
codem1 < max_ent)
1065 disp = (hval == 0) ? 1 : hval;
1069 if (hval >= db->
hsize)
1075 while (dictp->
codem1 < max_ent);
1083 dictp2 =
dict_ptr (db, max_ent + 1);
1084 indx = dictp2->
cptr;
1087 if (dictp3->
codem1 == max_ent)
1092 dictp2->
cptr = hval;
1100 *lens1 = *lens2 + 1;
1103 if (max_ent >=
MAXCODE(n_bits) && max_ent < db->maxmaxcode)
1106 tgtbitno = 32-n_bits;
1121 printk(
"bsd_decomp%d: peer should have cleared dictionary on %d\n",
1132 static struct compressor ppp_bsd_compress = {
1134 .comp_alloc = bsd_comp_alloc,
1135 .comp_free = bsd_free,
1136 .comp_init = bsd_comp_init,
1137 .comp_reset = bsd_reset,
1138 .compress = bsd_compress,
1139 .comp_stat = bsd_comp_stats,
1140 .decomp_alloc = bsd_decomp_alloc,
1141 .decomp_free = bsd_free,
1142 .decomp_init = bsd_decomp_init,
1143 .decomp_reset = bsd_reset,
1144 .decompress = bsd_decompress,
1145 .incomp = bsd_incomp,
1146 .decomp_stat = bsd_comp_stats,
1154 static int __init bsdcomp_init(
void)
1162 static void __exit bsdcomp_cleanup(
void)