1 /* Because this code is derived from the 4.3BSD compress source:
3 * Copyright (c) 1985, 1986 The Regents of the University of California.
6 * This code is derived from software contributed to Berkeley by
7 * James A. Woods, derived from original work by Spencer Thomas
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * This version is for use with contiguous buffers on Linux-derived systems.
42 * ==FILEVERSION 970607==
44 * NOTE TO MAINTAINERS:
45 * If you modify this file at all, please set the number above to the
46 * date of the modification as YYMMDD (year month day).
47 * bsd_comp.c is shipped with a PPP distribution as well as with
48 * the kernel; if everyone increases the FILEVERSION number above,
49 * then scripts can do the right thing when deciding whether to
50 * install a new bsd_comp.c file. Don't change the format of that
51 * line otherwise, so the installation script can recognize it.
53 * From: bsd_comp.c,v 1.3 1994/12/08 01:59:58 paulus Exp
57 #error This file must be compiled as a module.
60 #include <linux/version.h>
61 #include <linux/module.h>
62 #include <linux/kernel.h>
63 #include <linux/sched.h>
64 #include <linux/types.h>
65 #include <linux/fcntl.h>
66 #include <linux/interrupt.h>
67 #include <linux/ptrace.h>
68 #include <linux/malloc.h>
69 #include <linux/ioport.h>
73 /* a nice define to generate linux version numbers */
74 #define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
76 #if LINUX_VERSION_CODE >= VERSION(2,1,4)
77 #include <linux/vmalloc.h>
80 #include <linux/tty.h>
81 #include <linux/errno.h>
82 #include <linux/sched.h> /* to get the struct task_struct */
83 #include <linux/string.h> /* used in new tty drivers */
84 #include <linux/signal.h> /* used in new tty drivers */
86 #include <asm/system.h>
87 #include <asm/bitops.h>
88 #include <asm/byteorder.h>
92 #include <linux/if_ether.h>
93 #include <linux/netdevice.h>
94 #include <linux/skbuff.h>
95 #include <linux/inet.h>
96 #include <linux/ioctl.h>
98 #include <linux/ppp_defs.h>
102 #include <linux/ppp-comp.h>
106 * PPP "BSD compress" compression
107 * The differences between this compression and the classic BSD LZW
108 * source are obvious from the requirement that the classic code worked
109 * with files while this handles arbitrarily long streams that
110 * are broken into packets. They are:
112 * When the code size expands, a block of junk is not emitted by
113 * the compressor and not expected by the decompressor.
115 * New codes are not necessarily assigned every time an old
116 * code is output by the compressor. This is because a packet
117 * end forces a code to be emitted, but does not imply that a
118 * new sequence has been seen.
120 * The compression ratio is checked at the first end of a packet
121 * after the appropriate gap. Besides simplifying and speeding
122 * things up, this makes it more likely that the transmitter
123 * and receiver will agree when the dictionary is cleared when
124 * compression is not going well.
128 * Macros to extract protocol version and number of bits
129 * from the third byte of the BSD Compress CCP configuration option.
132 #define BSD_VERSION(x) ((x) >> 5)
133 #define BSD_NBITS(x) ((x) & 0x1F)
135 #define BSD_CURRENT_VERSION 1
138 * A dictionary for doing BSD compress.
142 union { /* hash value */
145 #if defined(__LITTLE_ENDIAN) /* Little endian order */
146 unsigned short prefix
; /* preceding code */
147 unsigned char suffix
; /* last character of new code */
149 #elif defined(__BIG_ENDIAN) /* Big endian order */
151 unsigned char suffix
; /* last character of new code */
152 unsigned short prefix
; /* preceding code */
154 #error Endianness not defined...
158 unsigned short codem1
; /* output of hash table -1 */
159 unsigned short cptr
; /* map code to hash table entry */
163 int totlen
; /* length of this structure */
164 unsigned int hsize
; /* size of the hash table */
165 unsigned char hshift
; /* used in hash function */
166 unsigned char n_bits
; /* current bits/code */
167 unsigned char maxbits
; /* maximum bits/code */
168 unsigned char debug
; /* non-zero if debug desired */
169 unsigned char unit
; /* ppp unit number */
170 unsigned short seqno
; /* sequence # of next packet */
171 unsigned int mru
; /* size of receive (decompress) bufr */
172 unsigned int maxmaxcode
; /* largest valid code */
173 unsigned int max_ent
; /* largest code in use */
174 unsigned int in_count
; /* uncompressed bytes, aged */
175 unsigned int bytes_out
; /* compressed bytes, aged */
176 unsigned int ratio
; /* recent compression ratio */
177 unsigned int checkpoint
; /* when to next check the ratio */
178 unsigned int clear_count
; /* times dictionary cleared */
179 unsigned int incomp_count
; /* incompressible packets */
180 unsigned int incomp_bytes
; /* incompressible bytes */
181 unsigned int uncomp_count
; /* uncompressed packets */
182 unsigned int uncomp_bytes
; /* uncompressed bytes */
183 unsigned int comp_count
; /* compressed packets */
184 unsigned int comp_bytes
; /* compressed bytes */
185 unsigned short *lens
; /* array of lengths of codes */
186 struct bsd_dict
*dict
; /* dictionary */
189 #define BSD_OVHD 2 /* BSD compress overhead/packet */
190 #define MIN_BSD_BITS 9
191 #define BSD_INIT_BITS MIN_BSD_BITS
192 #define MAX_BSD_BITS 15
194 static void bsd_free (void *state
);
195 static void *bsd_alloc(unsigned char *options
, int opt_len
, int decomp
);
196 static void *bsd_comp_alloc (unsigned char *options
, int opt_len
);
197 static void *bsd_decomp_alloc (unsigned char *options
, int opt_len
);
199 static int bsd_init (void *db
, unsigned char *options
,
200 int opt_len
, int unit
, int debug
, int decomp
);
201 static int bsd_comp_init (void *state
, unsigned char *options
,
202 int opt_len
, int unit
, int opthdr
, int debug
);
203 static int bsd_decomp_init (void *state
, unsigned char *options
,
204 int opt_len
, int unit
, int opthdr
, int mru
,
207 static void bsd_reset (void *state
);
208 static void bsd_comp_stats (void *state
, struct compstat
*stats
);
210 static int bsd_compress (void *state
, unsigned char *rptr
,
211 unsigned char *obuf
, int isize
, int osize
);
212 static void bsd_incomp (void *state
, unsigned char *ibuf
, int icnt
);
214 static int bsd_decompress (void *state
, unsigned char *ibuf
, int isize
,
215 unsigned char *obuf
, int osize
);
217 /* These are in ppp.c */
218 extern int ppp_register_compressor (struct compressor
*cp
);
219 extern void ppp_unregister_compressor (struct compressor
*cp
);
222 * the next two codes should not be changed lightly, as they must not
223 * lie within the contiguous general code space.
225 #define CLEAR 256 /* table clear output code */
226 #define FIRST 257 /* first free entry */
229 #define MAXCODE(b) ((1 << (b)) - 1)
230 #define BADCODEM1 MAXCODE(MAX_BSD_BITS);
232 #define BSD_HASH(prefix,suffix,hshift) ((((unsigned long)(suffix))<<(hshift)) \
233 ^ (unsigned long)(prefix))
234 #define BSD_KEY(prefix,suffix) ((((unsigned long)(suffix)) << 16) \
235 + (unsigned long)(prefix))
237 #define CHECK_GAP 10000 /* Ratio check interval */
239 #define RATIO_SCALE_LOG 8
240 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
241 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
244 * clear the dictionary
248 bsd_clear(struct bsd_db
*db
)
251 db
->max_ent
= FIRST
-1;
252 db
->n_bits
= BSD_INIT_BITS
;
256 db
->checkpoint
= CHECK_GAP
;
260 * If the dictionary is full, then see if it is time to reset it.
262 * Compute the compression ratio using fixed-point arithmetic
263 * with 8 fractional bits.
265 * Since we have an infinite stream instead of a single file,
266 * watch only the local compression ratio.
268 * Since both peers must reset the dictionary at the same time even in
269 * the absence of CLEAR codes (while packets are incompressible), they
270 * must compute the same ratio.
273 static int bsd_check (struct bsd_db
*db
) /* 1=output CLEAR */
275 unsigned int new_ratio
;
277 if (db
->in_count
>= db
->checkpoint
)
279 /* age the ratio by limiting the size of the counts */
280 if (db
->in_count
>= RATIO_MAX
|| db
->bytes_out
>= RATIO_MAX
)
282 db
->in_count
-= (db
->in_count
>> 2);
283 db
->bytes_out
-= (db
->bytes_out
>> 2);
286 db
->checkpoint
= db
->in_count
+ CHECK_GAP
;
288 if (db
->max_ent
>= db
->maxmaxcode
)
290 /* Reset the dictionary only if the ratio is worse,
291 * or if it looks as if it has been poisoned
292 * by incompressible data.
294 * This does not overflow, because
295 * db->in_count <= RATIO_MAX.
298 new_ratio
= db
->in_count
<< RATIO_SCALE_LOG
;
299 if (db
->bytes_out
!= 0)
301 new_ratio
/= db
->bytes_out
;
304 if (new_ratio
< db
->ratio
|| new_ratio
< 1 * RATIO_SCALE
)
309 db
->ratio
= new_ratio
;
319 static void bsd_comp_stats (void *state
, struct compstat
*stats
)
321 struct bsd_db
*db
= (struct bsd_db
*) state
;
323 stats
->unc_bytes
= db
->uncomp_bytes
;
324 stats
->unc_packets
= db
->uncomp_count
;
325 stats
->comp_bytes
= db
->comp_bytes
;
326 stats
->comp_packets
= db
->comp_count
;
327 stats
->inc_bytes
= db
->incomp_bytes
;
328 stats
->inc_packets
= db
->incomp_count
;
329 stats
->in_count
= db
->in_count
;
330 stats
->bytes_out
= db
->bytes_out
;
334 * Reset state, as on a CCP ResetReq.
337 static void bsd_reset (void *state
)
339 struct bsd_db
*db
= (struct bsd_db
*) state
;
348 * Release the compression structure
351 static void bsd_free (void *state
)
353 struct bsd_db
*db
= (struct bsd_db
*) state
;
358 * Release the dictionary
366 * Release the string buffer
374 * Finally release the structure itself.
382 * Allocate space for a (de) compressor.
385 static void *bsd_alloc (unsigned char *options
, int opt_len
, int decomp
)
388 unsigned int hsize
, hshift
, maxmaxcode
;
391 if (opt_len
!= 3 || options
[0] != CI_BSD_COMPRESS
|| options
[1] != 3
392 || BSD_VERSION(options
[2]) != BSD_CURRENT_VERSION
)
397 bits
= BSD_NBITS(options
[2]);
401 case 9: /* needs 82152 for both directions */
402 case 10: /* needs 84144 */
403 case 11: /* needs 88240 */
404 case 12: /* needs 96432 */
408 case 13: /* needs 176784 */
412 case 14: /* needs 353744 */
416 case 15: /* needs 691440 */
420 case 16: /* needs 1366160--far too much, */
421 /* hsize = 69001; */ /* and 69001 is too big for cptr */
422 /* hshift = 8; */ /* in struct bsd_db */
428 * Allocate the main control structure for this instance.
430 maxmaxcode
= MAXCODE(bits
);
431 db
= (struct bsd_db
*) kmalloc (sizeof (struct bsd_db
),
438 memset (db
, 0, sizeof(struct bsd_db
));
440 * Allocate space for the dictionary. This may be more than one page in
443 db
->dict
= (struct bsd_dict
*) vmalloc (hsize
*
444 sizeof (struct bsd_dict
));
453 * If this is the compression buffer then there is no length data.
460 * For decompression, the length information is needed as well.
464 db
->lens
= (unsigned short *) vmalloc ((maxmaxcode
+ 1) *
465 sizeof (db
->lens
[0]));
473 * Initialize the data information for the compression code
475 db
->totlen
= sizeof (struct bsd_db
) +
476 (sizeof (struct bsd_dict
) * hsize
);
480 db
->maxmaxcode
= maxmaxcode
;
486 static void *bsd_comp_alloc (unsigned char *options
, int opt_len
)
488 return bsd_alloc (options
, opt_len
, 0);
491 static void *bsd_decomp_alloc (unsigned char *options
, int opt_len
)
493 return bsd_alloc (options
, opt_len
, 1);
497 * Initialize the database.
500 static int bsd_init (void *state
, unsigned char *options
,
501 int opt_len
, int unit
, int debug
, int decomp
)
503 struct bsd_db
*db
= state
;
506 if ((opt_len
!= 3) || (options
[0] != CI_BSD_COMPRESS
) || (options
[1] != 3)
507 || (BSD_VERSION(options
[2]) != BSD_CURRENT_VERSION
)
508 || (BSD_NBITS(options
[2]) != db
->maxbits
)
509 || (decomp
&& db
->lens
== NULL
))
527 db
->dict
[indx
].codem1
= BADCODEM1
;
528 db
->dict
[indx
].cptr
= 0;
543 static int bsd_comp_init (void *state
, unsigned char *options
,
544 int opt_len
, int unit
, int opthdr
, int debug
)
546 return bsd_init (state
, options
, opt_len
, unit
, debug
, 0);
549 static int bsd_decomp_init (void *state
, unsigned char *options
,
550 int opt_len
, int unit
, int opthdr
, int mru
,
553 return bsd_init (state
, options
, opt_len
, unit
, debug
, 1);
557 * Obtain pointers to the various structures in the compression tables
560 #define dict_ptrx(p,idx) &(p->dict[idx])
561 #define lens_ptrx(p,idx) &(p->lens[idx])
564 static unsigned short *lens_ptr(struct bsd_db
*db
, int idx
)
566 if ((unsigned int) idx
> (unsigned int) db
->maxmaxcode
)
568 printk ("<9>ppp: lens_ptr(%d) > max\n", idx
);
571 return lens_ptrx (db
, idx
);
574 static struct bsd_dict
*dict_ptr(struct bsd_db
*db
, int idx
)
576 if ((unsigned int) idx
>= (unsigned int) db
->hsize
)
578 printk ("<9>ppp: dict_ptr(%d) > max\n", idx
);
581 return dict_ptrx (db
, idx
);
585 #define lens_ptr(db,idx) lens_ptrx(db,idx)
586 #define dict_ptr(db,idx) dict_ptrx(db,idx)
592 * The result of this function is the size of the compressed
593 * packet. A zero is returned if the packet was not compressed
594 * for some reason, such as the size being larger than uncompressed.
596 * One change from the BSD compress command is that when the
597 * code size expands, we do not output a bunch of padding.
600 static int bsd_compress (void *state
, unsigned char *rptr
, unsigned char *obuf
,
601 int isize
, int osize
)
605 unsigned int max_ent
;
611 struct bsd_dict
*dictp
;
625 *wptr++ = (unsigned char) (v); \
633 #define OUTPUT(ent) \
636 accm |= ((ent) << bitno); \
639 PUTBYTE(accm >> 24); \
643 while (bitno <= 24); \
647 * If the protocol is not in the range we're interested in,
648 * just return without compressing the packet. If it is,
649 * the protocol becomes the first byte to compress.
652 ent
= PPP_PROTOCOL(rptr
);
653 if (ent
< 0x21 || ent
> 0xf9)
658 db
= (struct bsd_db
*) state
;
660 max_ent
= db
->max_ent
;
664 mxcode
= MAXCODE (n_bits
);
666 /* Initialize the output pointers */
668 olen
= PPP_HDRLEN
+ BSD_OVHD
;
675 /* This is the PPP header information */
678 *wptr
++ = PPP_ADDRESS(rptr
);
679 *wptr
++ = PPP_CONTROL(rptr
);
682 *wptr
++ = db
->seqno
>> 8;
686 /* Skip the input header */
689 ilen
= ++isize
; /* Low byte of protocol is counted as input */
694 fcode
= BSD_KEY (ent
, c
);
695 hval
= BSD_HASH (ent
, c
, hshift
);
696 dictp
= dict_ptr (db
, hval
);
698 /* Validate and then check the entry. */
699 if (dictp
->codem1
>= max_ent
)
704 if (dictp
->f
.fcode
== fcode
)
706 ent
= dictp
->codem1
+ 1;
707 continue; /* found (prefix,suffix) */
710 /* continue probing until a match or invalid entry */
711 disp
= (hval
== 0) ? 1 : hval
;
716 if (hval
>= db
->hsize
)
720 dictp
= dict_ptr (db
, hval
);
721 if (dictp
->codem1
>= max_ent
)
726 while (dictp
->f
.fcode
!= fcode
);
728 ent
= dictp
->codem1
+ 1; /* finally found (prefix,suffix) */
732 OUTPUT(ent
); /* output the prefix */
734 /* code -> hashtable */
735 if (max_ent
< db
->maxmaxcode
)
737 struct bsd_dict
*dictp2
;
738 struct bsd_dict
*dictp3
;
741 /* expand code size if needed */
742 if (max_ent
>= mxcode
)
744 db
->n_bits
= ++n_bits
;
745 mxcode
= MAXCODE (n_bits
);
748 /* Invalidate old hash table entry using
749 * this code, and then take it over.
752 dictp2
= dict_ptr (db
, max_ent
+ 1);
754 dictp3
= dict_ptr (db
, indx
);
756 if (dictp3
->codem1
== max_ent
)
758 dictp3
->codem1
= BADCODEM1
;
762 dictp
->codem1
= max_ent
;
763 dictp
->f
.fcode
= fcode
;
764 db
->max_ent
= ++max_ent
;
768 unsigned short *len1
= lens_ptr (db
, max_ent
);
769 unsigned short *len2
= lens_ptr (db
, ent
);
776 OUTPUT(ent
); /* output the last code */
778 db
->bytes_out
+= olen
- PPP_HDRLEN
- BSD_OVHD
;
779 db
->uncomp_bytes
+= isize
;
780 db
->in_count
+= isize
;
786 ++db
->bytes_out
; /* must be set before calling bsd_check */
790 * Generate the clear command if needed
799 * Pad dribble bits of last code with ones.
800 * Do not emit a completely useless byte of ones.
805 PUTBYTE((accm
| (0xff << (bitno
-8))) >> 24);
809 * Increase code size if we would have without the packet
810 * boundary because the decompressor will do so.
813 if (max_ent
>= mxcode
&& max_ent
< db
->maxmaxcode
)
818 /* If output length is too large then this is an incomplete frame. */
822 db
->incomp_bytes
+= isize
;
825 else /* Count the number of compressed frames */
828 db
->comp_bytes
+= olen
;
831 /* Return the resulting output length */
838 * Update the "BSD Compress" dictionary on the receiver for
839 * incompressible data by pretending to compress the incoming data.
842 static void bsd_incomp (void *state
, unsigned char *ibuf
, int icnt
)
844 (void) bsd_compress (state
, ibuf
, (char *) 0, icnt
, 0);
848 * Decompress "BSD Compress".
850 * Because of patent problems, we return DECOMP_ERROR for errors
851 * found by inspecting the input data and for system problems, but
852 * DECOMP_FATALERROR for any errors which could possibly be said to
853 * be being detected "after" decompression. For DECOMP_ERROR,
854 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
855 * infringing a patent of Motorola's if we do, so we take CCP down
858 * Given that the frame has the correct sequence number and a good FCS,
859 * errors such as invalid codes in the input most likely indicate a
860 * bug, so we return DECOMP_FATALERROR for them in order to turn off
861 * compression, even though they are detected by inspecting the input.
864 static int bsd_decompress (void *state
, unsigned char *ibuf
, int isize
,
865 unsigned char *obuf
, int osize
)
868 unsigned int max_ent
;
870 unsigned int bitno
; /* 1st valid bit in accm */
872 unsigned int tgtbitno
; /* bitno when we have a code */
873 struct bsd_dict
*dictp
;
877 unsigned int oldcode
;
878 unsigned int finchar
;
887 db
= (struct bsd_db
*) state
;
888 max_ent
= db
->max_ent
;
890 bitno
= 32; /* 1st valid bit in accm */
892 tgtbitno
= 32 - n_bits
; /* bitno when we have a code */
895 * Save the address/control from the PPP header
896 * and then get the sequence number.
899 adrs
= PPP_ADDRESS (ibuf
);
900 ctrl
= PPP_CONTROL (ibuf
);
902 seq
= (ibuf
[4] << 8) + ibuf
[5];
904 ibuf
+= (PPP_HDRLEN
+ 2);
905 ilen
= isize
- (PPP_HDRLEN
+ 2);
908 * Check the sequence number and give up if it differs from
909 * the value we're expecting.
912 if (seq
!= db
->seqno
)
916 printk("bsd_decomp%d: bad sequence # %d, expected %d\n",
917 db
->unit
, seq
, db
->seqno
- 1);
923 db
->bytes_out
+= ilen
;
926 * Fill in the ppp header, but not the last byte of the protocol
927 * (that comes from the decompressed data).
939 * Keep the checkpoint correctly so that incompressible packets
940 * clear the dictionary at the proper times.
947 db
->in_count
+= (explen
- 3); /* don't count the header */
952 * Accumulate bytes until we have a complete code.
953 * Then get the next code, relying on the 32-bit,
954 * unsigned accm to mask the result.
958 accm
|= *ibuf
++ << bitno
;
959 if (tgtbitno
< bitno
)
964 incode
= accm
>> tgtbitno
;
969 * The dictionary must only be cleared at the end of a packet.
978 printk("bsd_decomp%d: bad CLEAR\n", db
->unit
);
980 return DECOMP_FATALERROR
; /* probably a bug */
987 if ((incode
> max_ent
+ 2) || (incode
> db
->maxmaxcode
)
988 || (incode
> max_ent
&& oldcode
== CLEAR
))
992 printk("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
993 db
->unit
, incode
, oldcode
);
994 printk("max_ent=0x%x explen=%d seqno=%d\n",
995 max_ent
, explen
, db
->seqno
);
997 return DECOMP_FATALERROR
; /* probably a bug */
1000 /* Special case for KwKwK string. */
1001 if (incode
> max_ent
)
1012 codelen
= *(lens_ptr (db
, finchar
));
1013 explen
+= codelen
+ extra
;
1018 printk("bsd_decomp%d: ran out of mru\n", db
->unit
);
1020 printk(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
1021 ilen
, finchar
, codelen
, explen
);
1024 return DECOMP_FATALERROR
;
1028 * Decode this code and install it in the decompressed buffer.
1033 while (finchar
> LAST
)
1035 struct bsd_dict
*dictp2
= dict_ptr (db
, finchar
);
1037 dictp
= dict_ptr (db
, dictp2
->cptr
);
1039 if (--codelen
<= 0 || dictp
->codem1
!= finchar
-1)
1043 printk("bsd_decomp%d: fell off end of chain ", db
->unit
);
1044 printk("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1045 incode
, finchar
, dictp2
->cptr
, max_ent
);
1049 if (dictp
->codem1
!= finchar
-1)
1051 printk("bsd_decomp%d: bad code chain 0x%x "
1053 db
->unit
, incode
, finchar
);
1055 printk("oldcode=0x%x cptr=0x%x codem1=0x%x\n",
1056 oldcode
, dictp2
->cptr
, dictp
->codem1
);
1059 return DECOMP_FATALERROR
;
1062 *--p
= dictp
->f
.hs
.suffix
;
1063 finchar
= dictp
->f
.hs
.prefix
;
1070 printk("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1071 db
->unit
, codelen
, incode
, max_ent
);
1075 if (extra
) /* the KwKwK case again */
1081 * If not first code in a packet, and
1082 * if not out of code space, then allocate a new code.
1084 * Keep the hash table correct so it can be used
1085 * with uncompressed packets.
1088 if (oldcode
!= CLEAR
&& max_ent
< db
->maxmaxcode
)
1090 struct bsd_dict
*dictp2
, *dictp3
;
1091 unsigned short *lens1
, *lens2
;
1092 unsigned long fcode
;
1093 int hval
, disp
, indx
;
1095 fcode
= BSD_KEY(oldcode
,finchar
);
1096 hval
= BSD_HASH(oldcode
,finchar
,db
->hshift
);
1097 dictp
= dict_ptr (db
, hval
);
1099 /* look for a free hash table entry */
1100 if (dictp
->codem1
< max_ent
)
1102 disp
= (hval
== 0) ? 1 : hval
;
1106 if (hval
>= db
->hsize
)
1110 dictp
= dict_ptr (db
, hval
);
1112 while (dictp
->codem1
< max_ent
);
1116 * Invalidate previous hash table entry
1117 * assigned this code, and then take it over
1120 dictp2
= dict_ptr (db
, max_ent
+ 1);
1121 indx
= dictp2
->cptr
;
1122 dictp3
= dict_ptr (db
, indx
);
1124 if (dictp3
->codem1
== max_ent
)
1126 dictp3
->codem1
= BADCODEM1
;
1129 dictp2
->cptr
= hval
;
1130 dictp
->codem1
= max_ent
;
1131 dictp
->f
.fcode
= fcode
;
1132 db
->max_ent
= ++max_ent
;
1134 /* Update the length of this string. */
1135 lens1
= lens_ptr (db
, max_ent
);
1136 lens2
= lens_ptr (db
, oldcode
);
1137 *lens1
= *lens2
+ 1;
1139 /* Expand code size if needed. */
1140 if (max_ent
>= MAXCODE(n_bits
) && max_ent
< db
->maxmaxcode
)
1142 db
->n_bits
= ++n_bits
;
1143 tgtbitno
= 32-n_bits
;
1151 db
->comp_bytes
+= isize
- BSD_OVHD
- PPP_HDRLEN
;
1152 db
->uncomp_bytes
+= explen
;
1158 printk("bsd_decomp%d: peer should have cleared dictionary on %d\n",
1159 db
->unit
, db
->seqno
- 1);
1165 /*************************************************************
1166 * Table of addresses for the BSD compression module
1167 *************************************************************/
1169 static struct compressor ppp_bsd_compress
= {
1170 CI_BSD_COMPRESS
, /* compress_proto */
1171 bsd_comp_alloc
, /* comp_alloc */
1172 bsd_free
, /* comp_free */
1173 bsd_comp_init
, /* comp_init */
1174 bsd_reset
, /* comp_reset */
1175 bsd_compress
, /* compress */
1176 bsd_comp_stats
, /* comp_stat */
1177 bsd_decomp_alloc
, /* decomp_alloc */
1178 bsd_free
, /* decomp_free */
1179 bsd_decomp_init
, /* decomp_init */
1180 bsd_reset
, /* decomp_reset */
1181 bsd_decompress
, /* decompress */
1182 bsd_incomp
, /* incomp */
1183 bsd_comp_stats
/* decomp_stat */
1186 /*************************************************************
1187 * Module support routines
1188 *************************************************************/
1193 int answer
= ppp_register_compressor (&ppp_bsd_compress
);
1195 printk (KERN_INFO
"PPP BSD Compression module registered\n");
1200 cleanup_module(void)
1202 ppp_unregister_compressor (&ppp_bsd_compress
);