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/module.h>
61 #include <linux/kernel.h>
62 #include <linux/sched.h>
63 #include <linux/types.h>
64 #include <linux/fcntl.h>
65 #include <linux/interrupt.h>
66 #include <linux/ptrace.h>
67 #include <linux/ioport.h>
69 #include <linux/malloc.h>
70 #include <linux/vmalloc.h>
71 #include <linux/tty.h>
72 #include <linux/errno.h>
73 #include <linux/string.h> /* used in new tty drivers */
74 #include <linux/signal.h> /* used in new tty drivers */
76 #include <asm/system.h>
77 #include <asm/bitops.h>
78 #include <asm/byteorder.h>
82 #include <linux/if_ether.h>
83 #include <linux/netdevice.h>
84 #include <linux/skbuff.h>
85 #include <linux/inet.h>
86 #include <linux/ioctl.h>
88 #include <linux/ppp_defs.h>
92 #include <linux/ppp-comp.h>
96 * PPP "BSD compress" compression
97 * The differences between this compression and the classic BSD LZW
98 * source are obvious from the requirement that the classic code worked
99 * with files while this handles arbitrarily long streams that
100 * are broken into packets. They are:
102 * When the code size expands, a block of junk is not emitted by
103 * the compressor and not expected by the decompressor.
105 * New codes are not necessarily assigned every time an old
106 * code is output by the compressor. This is because a packet
107 * end forces a code to be emitted, but does not imply that a
108 * new sequence has been seen.
110 * The compression ratio is checked at the first end of a packet
111 * after the appropriate gap. Besides simplifying and speeding
112 * things up, this makes it more likely that the transmitter
113 * and receiver will agree when the dictionary is cleared when
114 * compression is not going well.
118 * Macros to extract protocol version and number of bits
119 * from the third byte of the BSD Compress CCP configuration option.
122 #define BSD_VERSION(x) ((x) >> 5)
123 #define BSD_NBITS(x) ((x) & 0x1F)
125 #define BSD_CURRENT_VERSION 1
128 * A dictionary for doing BSD compress.
132 union { /* hash value */
135 #if defined(__LITTLE_ENDIAN) /* Little endian order */
136 unsigned short prefix
; /* preceding code */
137 unsigned char suffix
; /* last character of new code */
139 #elif defined(__BIG_ENDIAN) /* Big endian order */
141 unsigned char suffix
; /* last character of new code */
142 unsigned short prefix
; /* preceding code */
144 #error Endianness not defined...
148 unsigned short codem1
; /* output of hash table -1 */
149 unsigned short cptr
; /* map code to hash table entry */
153 int totlen
; /* length of this structure */
154 unsigned int hsize
; /* size of the hash table */
155 unsigned char hshift
; /* used in hash function */
156 unsigned char n_bits
; /* current bits/code */
157 unsigned char maxbits
; /* maximum bits/code */
158 unsigned char debug
; /* non-zero if debug desired */
159 unsigned char unit
; /* ppp unit number */
160 unsigned short seqno
; /* sequence # of next packet */
161 unsigned int mru
; /* size of receive (decompress) bufr */
162 unsigned int maxmaxcode
; /* largest valid code */
163 unsigned int max_ent
; /* largest code in use */
164 unsigned int in_count
; /* uncompressed bytes, aged */
165 unsigned int bytes_out
; /* compressed bytes, aged */
166 unsigned int ratio
; /* recent compression ratio */
167 unsigned int checkpoint
; /* when to next check the ratio */
168 unsigned int clear_count
; /* times dictionary cleared */
169 unsigned int incomp_count
; /* incompressible packets */
170 unsigned int incomp_bytes
; /* incompressible bytes */
171 unsigned int uncomp_count
; /* uncompressed packets */
172 unsigned int uncomp_bytes
; /* uncompressed bytes */
173 unsigned int comp_count
; /* compressed packets */
174 unsigned int comp_bytes
; /* compressed bytes */
175 unsigned short *lens
; /* array of lengths of codes */
176 struct bsd_dict
*dict
; /* dictionary */
179 #define BSD_OVHD 2 /* BSD compress overhead/packet */
180 #define MIN_BSD_BITS 9
181 #define BSD_INIT_BITS MIN_BSD_BITS
182 #define MAX_BSD_BITS 15
184 static void bsd_free (void *state
);
185 static void *bsd_alloc(unsigned char *options
, int opt_len
, int decomp
);
186 static void *bsd_comp_alloc (unsigned char *options
, int opt_len
);
187 static void *bsd_decomp_alloc (unsigned char *options
, int opt_len
);
189 static int bsd_init (void *db
, unsigned char *options
,
190 int opt_len
, int unit
, int debug
, int decomp
);
191 static int bsd_comp_init (void *state
, unsigned char *options
,
192 int opt_len
, int unit
, int opthdr
, int debug
);
193 static int bsd_decomp_init (void *state
, unsigned char *options
,
194 int opt_len
, int unit
, int opthdr
, int mru
,
197 static void bsd_reset (void *state
);
198 static void bsd_comp_stats (void *state
, struct compstat
*stats
);
200 static int bsd_compress (void *state
, unsigned char *rptr
,
201 unsigned char *obuf
, int isize
, int osize
);
202 static void bsd_incomp (void *state
, unsigned char *ibuf
, int icnt
);
204 static int bsd_decompress (void *state
, unsigned char *ibuf
, int isize
,
205 unsigned char *obuf
, int osize
);
207 /* These are in ppp.c */
208 extern int ppp_register_compressor (struct compressor
*cp
);
209 extern void ppp_unregister_compressor (struct compressor
*cp
);
212 * the next two codes should not be changed lightly, as they must not
213 * lie within the contiguous general code space.
215 #define CLEAR 256 /* table clear output code */
216 #define FIRST 257 /* first free entry */
219 #define MAXCODE(b) ((1 << (b)) - 1)
220 #define BADCODEM1 MAXCODE(MAX_BSD_BITS);
222 #define BSD_HASH(prefix,suffix,hshift) ((((unsigned long)(suffix))<<(hshift)) \
223 ^ (unsigned long)(prefix))
224 #define BSD_KEY(prefix,suffix) ((((unsigned long)(suffix)) << 16) \
225 + (unsigned long)(prefix))
227 #define CHECK_GAP 10000 /* Ratio check interval */
229 #define RATIO_SCALE_LOG 8
230 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
231 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
234 * clear the dictionary
238 bsd_clear(struct bsd_db
*db
)
241 db
->max_ent
= FIRST
-1;
242 db
->n_bits
= BSD_INIT_BITS
;
246 db
->checkpoint
= CHECK_GAP
;
250 * If the dictionary is full, then see if it is time to reset it.
252 * Compute the compression ratio using fixed-point arithmetic
253 * with 8 fractional bits.
255 * Since we have an infinite stream instead of a single file,
256 * watch only the local compression ratio.
258 * Since both peers must reset the dictionary at the same time even in
259 * the absence of CLEAR codes (while packets are incompressible), they
260 * must compute the same ratio.
263 static int bsd_check (struct bsd_db
*db
) /* 1=output CLEAR */
265 unsigned int new_ratio
;
267 if (db
->in_count
>= db
->checkpoint
)
269 /* age the ratio by limiting the size of the counts */
270 if (db
->in_count
>= RATIO_MAX
|| db
->bytes_out
>= RATIO_MAX
)
272 db
->in_count
-= (db
->in_count
>> 2);
273 db
->bytes_out
-= (db
->bytes_out
>> 2);
276 db
->checkpoint
= db
->in_count
+ CHECK_GAP
;
278 if (db
->max_ent
>= db
->maxmaxcode
)
280 /* Reset the dictionary only if the ratio is worse,
281 * or if it looks as if it has been poisoned
282 * by incompressible data.
284 * This does not overflow, because
285 * db->in_count <= RATIO_MAX.
288 new_ratio
= db
->in_count
<< RATIO_SCALE_LOG
;
289 if (db
->bytes_out
!= 0)
291 new_ratio
/= db
->bytes_out
;
294 if (new_ratio
< db
->ratio
|| new_ratio
< 1 * RATIO_SCALE
)
299 db
->ratio
= new_ratio
;
309 static void bsd_comp_stats (void *state
, struct compstat
*stats
)
311 struct bsd_db
*db
= (struct bsd_db
*) state
;
313 stats
->unc_bytes
= db
->uncomp_bytes
;
314 stats
->unc_packets
= db
->uncomp_count
;
315 stats
->comp_bytes
= db
->comp_bytes
;
316 stats
->comp_packets
= db
->comp_count
;
317 stats
->inc_bytes
= db
->incomp_bytes
;
318 stats
->inc_packets
= db
->incomp_count
;
319 stats
->in_count
= db
->in_count
;
320 stats
->bytes_out
= db
->bytes_out
;
324 * Reset state, as on a CCP ResetReq.
327 static void bsd_reset (void *state
)
329 struct bsd_db
*db
= (struct bsd_db
*) state
;
338 * Release the compression structure
341 static void bsd_free (void *state
)
343 struct bsd_db
*db
= (struct bsd_db
*) state
;
348 * Release the dictionary
356 * Release the string buffer
364 * Finally release the structure itself.
372 * Allocate space for a (de) compressor.
375 static void *bsd_alloc (unsigned char *options
, int opt_len
, int decomp
)
378 unsigned int hsize
, hshift
, maxmaxcode
;
381 if (opt_len
!= 3 || options
[0] != CI_BSD_COMPRESS
|| options
[1] != 3
382 || BSD_VERSION(options
[2]) != BSD_CURRENT_VERSION
)
387 bits
= BSD_NBITS(options
[2]);
391 case 9: /* needs 82152 for both directions */
392 case 10: /* needs 84144 */
393 case 11: /* needs 88240 */
394 case 12: /* needs 96432 */
398 case 13: /* needs 176784 */
402 case 14: /* needs 353744 */
406 case 15: /* needs 691440 */
410 case 16: /* needs 1366160--far too much, */
411 /* hsize = 69001; */ /* and 69001 is too big for cptr */
412 /* hshift = 8; */ /* in struct bsd_db */
418 * Allocate the main control structure for this instance.
420 maxmaxcode
= MAXCODE(bits
);
421 db
= (struct bsd_db
*) kmalloc (sizeof (struct bsd_db
),
428 memset (db
, 0, sizeof(struct bsd_db
));
430 * Allocate space for the dictionary. This may be more than one page in
433 db
->dict
= (struct bsd_dict
*) vmalloc (hsize
*
434 sizeof (struct bsd_dict
));
443 * If this is the compression buffer then there is no length data.
450 * For decompression, the length information is needed as well.
454 db
->lens
= (unsigned short *) vmalloc ((maxmaxcode
+ 1) *
455 sizeof (db
->lens
[0]));
463 * Initialize the data information for the compression code
465 db
->totlen
= sizeof (struct bsd_db
) +
466 (sizeof (struct bsd_dict
) * hsize
);
470 db
->maxmaxcode
= maxmaxcode
;
476 static void *bsd_comp_alloc (unsigned char *options
, int opt_len
)
478 return bsd_alloc (options
, opt_len
, 0);
481 static void *bsd_decomp_alloc (unsigned char *options
, int opt_len
)
483 return bsd_alloc (options
, opt_len
, 1);
487 * Initialize the database.
490 static int bsd_init (void *state
, unsigned char *options
,
491 int opt_len
, int unit
, int debug
, int decomp
)
493 struct bsd_db
*db
= state
;
496 if ((opt_len
!= 3) || (options
[0] != CI_BSD_COMPRESS
) || (options
[1] != 3)
497 || (BSD_VERSION(options
[2]) != BSD_CURRENT_VERSION
)
498 || (BSD_NBITS(options
[2]) != db
->maxbits
)
499 || (decomp
&& db
->lens
== NULL
))
517 db
->dict
[indx
].codem1
= BADCODEM1
;
518 db
->dict
[indx
].cptr
= 0;
533 static int bsd_comp_init (void *state
, unsigned char *options
,
534 int opt_len
, int unit
, int opthdr
, int debug
)
536 return bsd_init (state
, options
, opt_len
, unit
, debug
, 0);
539 static int bsd_decomp_init (void *state
, unsigned char *options
,
540 int opt_len
, int unit
, int opthdr
, int mru
,
543 return bsd_init (state
, options
, opt_len
, unit
, debug
, 1);
547 * Obtain pointers to the various structures in the compression tables
550 #define dict_ptrx(p,idx) &(p->dict[idx])
551 #define lens_ptrx(p,idx) &(p->lens[idx])
554 static unsigned short *lens_ptr(struct bsd_db
*db
, int idx
)
556 if ((unsigned int) idx
> (unsigned int) db
->maxmaxcode
)
558 printk ("<9>ppp: lens_ptr(%d) > max\n", idx
);
561 return lens_ptrx (db
, idx
);
564 static struct bsd_dict
*dict_ptr(struct bsd_db
*db
, int idx
)
566 if ((unsigned int) idx
>= (unsigned int) db
->hsize
)
568 printk ("<9>ppp: dict_ptr(%d) > max\n", idx
);
571 return dict_ptrx (db
, idx
);
575 #define lens_ptr(db,idx) lens_ptrx(db,idx)
576 #define dict_ptr(db,idx) dict_ptrx(db,idx)
582 * The result of this function is the size of the compressed
583 * packet. A zero is returned if the packet was not compressed
584 * for some reason, such as the size being larger than uncompressed.
586 * One change from the BSD compress command is that when the
587 * code size expands, we do not output a bunch of padding.
590 static int bsd_compress (void *state
, unsigned char *rptr
, unsigned char *obuf
,
591 int isize
, int osize
)
595 unsigned int max_ent
;
601 struct bsd_dict
*dictp
;
615 *wptr++ = (unsigned char) (v); \
623 #define OUTPUT(ent) \
626 accm |= ((ent) << bitno); \
629 PUTBYTE(accm >> 24); \
633 while (bitno <= 24); \
637 * If the protocol is not in the range we're interested in,
638 * just return without compressing the packet. If it is,
639 * the protocol becomes the first byte to compress.
642 ent
= PPP_PROTOCOL(rptr
);
643 if (ent
< 0x21 || ent
> 0xf9)
648 db
= (struct bsd_db
*) state
;
650 max_ent
= db
->max_ent
;
654 mxcode
= MAXCODE (n_bits
);
656 /* Initialize the output pointers */
658 olen
= PPP_HDRLEN
+ BSD_OVHD
;
665 /* This is the PPP header information */
668 *wptr
++ = PPP_ADDRESS(rptr
);
669 *wptr
++ = PPP_CONTROL(rptr
);
672 *wptr
++ = db
->seqno
>> 8;
676 /* Skip the input header */
679 ilen
= ++isize
; /* Low byte of protocol is counted as input */
684 fcode
= BSD_KEY (ent
, c
);
685 hval
= BSD_HASH (ent
, c
, hshift
);
686 dictp
= dict_ptr (db
, hval
);
688 /* Validate and then check the entry. */
689 if (dictp
->codem1
>= max_ent
)
694 if (dictp
->f
.fcode
== fcode
)
696 ent
= dictp
->codem1
+ 1;
697 continue; /* found (prefix,suffix) */
700 /* continue probing until a match or invalid entry */
701 disp
= (hval
== 0) ? 1 : hval
;
706 if (hval
>= db
->hsize
)
710 dictp
= dict_ptr (db
, hval
);
711 if (dictp
->codem1
>= max_ent
)
716 while (dictp
->f
.fcode
!= fcode
);
718 ent
= dictp
->codem1
+ 1; /* finally found (prefix,suffix) */
722 OUTPUT(ent
); /* output the prefix */
724 /* code -> hashtable */
725 if (max_ent
< db
->maxmaxcode
)
727 struct bsd_dict
*dictp2
;
728 struct bsd_dict
*dictp3
;
731 /* expand code size if needed */
732 if (max_ent
>= mxcode
)
734 db
->n_bits
= ++n_bits
;
735 mxcode
= MAXCODE (n_bits
);
738 /* Invalidate old hash table entry using
739 * this code, and then take it over.
742 dictp2
= dict_ptr (db
, max_ent
+ 1);
744 dictp3
= dict_ptr (db
, indx
);
746 if (dictp3
->codem1
== max_ent
)
748 dictp3
->codem1
= BADCODEM1
;
752 dictp
->codem1
= max_ent
;
753 dictp
->f
.fcode
= fcode
;
754 db
->max_ent
= ++max_ent
;
758 unsigned short *len1
= lens_ptr (db
, max_ent
);
759 unsigned short *len2
= lens_ptr (db
, ent
);
766 OUTPUT(ent
); /* output the last code */
768 db
->bytes_out
+= olen
- PPP_HDRLEN
- BSD_OVHD
;
769 db
->uncomp_bytes
+= isize
;
770 db
->in_count
+= isize
;
776 ++db
->bytes_out
; /* must be set before calling bsd_check */
780 * Generate the clear command if needed
789 * Pad dribble bits of last code with ones.
790 * Do not emit a completely useless byte of ones.
795 PUTBYTE((accm
| (0xff << (bitno
-8))) >> 24);
799 * Increase code size if we would have without the packet
800 * boundary because the decompressor will do so.
803 if (max_ent
>= mxcode
&& max_ent
< db
->maxmaxcode
)
808 /* If output length is too large then this is an incomplete frame. */
812 db
->incomp_bytes
+= isize
;
815 else /* Count the number of compressed frames */
818 db
->comp_bytes
+= olen
;
821 /* Return the resulting output length */
828 * Update the "BSD Compress" dictionary on the receiver for
829 * incompressible data by pretending to compress the incoming data.
832 static void bsd_incomp (void *state
, unsigned char *ibuf
, int icnt
)
834 (void) bsd_compress (state
, ibuf
, (char *) 0, icnt
, 0);
838 * Decompress "BSD Compress".
840 * Because of patent problems, we return DECOMP_ERROR for errors
841 * found by inspecting the input data and for system problems, but
842 * DECOMP_FATALERROR for any errors which could possibly be said to
843 * be being detected "after" decompression. For DECOMP_ERROR,
844 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
845 * infringing a patent of Motorola's if we do, so we take CCP down
848 * Given that the frame has the correct sequence number and a good FCS,
849 * errors such as invalid codes in the input most likely indicate a
850 * bug, so we return DECOMP_FATALERROR for them in order to turn off
851 * compression, even though they are detected by inspecting the input.
854 static int bsd_decompress (void *state
, unsigned char *ibuf
, int isize
,
855 unsigned char *obuf
, int osize
)
858 unsigned int max_ent
;
860 unsigned int bitno
; /* 1st valid bit in accm */
862 unsigned int tgtbitno
; /* bitno when we have a code */
863 struct bsd_dict
*dictp
;
867 unsigned int oldcode
;
868 unsigned int finchar
;
877 db
= (struct bsd_db
*) state
;
878 max_ent
= db
->max_ent
;
880 bitno
= 32; /* 1st valid bit in accm */
882 tgtbitno
= 32 - n_bits
; /* bitno when we have a code */
885 * Save the address/control from the PPP header
886 * and then get the sequence number.
889 adrs
= PPP_ADDRESS (ibuf
);
890 ctrl
= PPP_CONTROL (ibuf
);
892 seq
= (ibuf
[4] << 8) + ibuf
[5];
894 ibuf
+= (PPP_HDRLEN
+ 2);
895 ilen
= isize
- (PPP_HDRLEN
+ 2);
898 * Check the sequence number and give up if it differs from
899 * the value we're expecting.
902 if (seq
!= db
->seqno
)
906 printk("bsd_decomp%d: bad sequence # %d, expected %d\n",
907 db
->unit
, seq
, db
->seqno
- 1);
913 db
->bytes_out
+= ilen
;
916 * Fill in the ppp header, but not the last byte of the protocol
917 * (that comes from the decompressed data).
929 * Keep the checkpoint correctly so that incompressible packets
930 * clear the dictionary at the proper times.
937 db
->in_count
+= (explen
- 3); /* don't count the header */
942 * Accumulate bytes until we have a complete code.
943 * Then get the next code, relying on the 32-bit,
944 * unsigned accm to mask the result.
948 accm
|= *ibuf
++ << bitno
;
949 if (tgtbitno
< bitno
)
954 incode
= accm
>> tgtbitno
;
959 * The dictionary must only be cleared at the end of a packet.
968 printk("bsd_decomp%d: bad CLEAR\n", db
->unit
);
970 return DECOMP_FATALERROR
; /* probably a bug */
977 if ((incode
> max_ent
+ 2) || (incode
> db
->maxmaxcode
)
978 || (incode
> max_ent
&& oldcode
== CLEAR
))
982 printk("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
983 db
->unit
, incode
, oldcode
);
984 printk("max_ent=0x%x explen=%d seqno=%d\n",
985 max_ent
, explen
, db
->seqno
);
987 return DECOMP_FATALERROR
; /* probably a bug */
990 /* Special case for KwKwK string. */
991 if (incode
> max_ent
)
1002 codelen
= *(lens_ptr (db
, finchar
));
1003 explen
+= codelen
+ extra
;
1008 printk("bsd_decomp%d: ran out of mru\n", db
->unit
);
1010 printk(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
1011 ilen
, finchar
, codelen
, explen
);
1014 return DECOMP_FATALERROR
;
1018 * Decode this code and install it in the decompressed buffer.
1023 while (finchar
> LAST
)
1025 struct bsd_dict
*dictp2
= dict_ptr (db
, finchar
);
1027 dictp
= dict_ptr (db
, dictp2
->cptr
);
1029 if (--codelen
<= 0 || dictp
->codem1
!= finchar
-1)
1033 printk("bsd_decomp%d: fell off end of chain ", db
->unit
);
1034 printk("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1035 incode
, finchar
, dictp2
->cptr
, max_ent
);
1039 if (dictp
->codem1
!= finchar
-1)
1041 printk("bsd_decomp%d: bad code chain 0x%x "
1043 db
->unit
, incode
, finchar
);
1045 printk("oldcode=0x%x cptr=0x%x codem1=0x%x\n",
1046 oldcode
, dictp2
->cptr
, dictp
->codem1
);
1049 return DECOMP_FATALERROR
;
1052 *--p
= dictp
->f
.hs
.suffix
;
1053 finchar
= dictp
->f
.hs
.prefix
;
1060 printk("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1061 db
->unit
, codelen
, incode
, max_ent
);
1065 if (extra
) /* the KwKwK case again */
1071 * If not first code in a packet, and
1072 * if not out of code space, then allocate a new code.
1074 * Keep the hash table correct so it can be used
1075 * with uncompressed packets.
1078 if (oldcode
!= CLEAR
&& max_ent
< db
->maxmaxcode
)
1080 struct bsd_dict
*dictp2
, *dictp3
;
1081 unsigned short *lens1
, *lens2
;
1082 unsigned long fcode
;
1083 int hval
, disp
, indx
;
1085 fcode
= BSD_KEY(oldcode
,finchar
);
1086 hval
= BSD_HASH(oldcode
,finchar
,db
->hshift
);
1087 dictp
= dict_ptr (db
, hval
);
1089 /* look for a free hash table entry */
1090 if (dictp
->codem1
< max_ent
)
1092 disp
= (hval
== 0) ? 1 : hval
;
1096 if (hval
>= db
->hsize
)
1100 dictp
= dict_ptr (db
, hval
);
1102 while (dictp
->codem1
< max_ent
);
1106 * Invalidate previous hash table entry
1107 * assigned this code, and then take it over
1110 dictp2
= dict_ptr (db
, max_ent
+ 1);
1111 indx
= dictp2
->cptr
;
1112 dictp3
= dict_ptr (db
, indx
);
1114 if (dictp3
->codem1
== max_ent
)
1116 dictp3
->codem1
= BADCODEM1
;
1119 dictp2
->cptr
= hval
;
1120 dictp
->codem1
= max_ent
;
1121 dictp
->f
.fcode
= fcode
;
1122 db
->max_ent
= ++max_ent
;
1124 /* Update the length of this string. */
1125 lens1
= lens_ptr (db
, max_ent
);
1126 lens2
= lens_ptr (db
, oldcode
);
1127 *lens1
= *lens2
+ 1;
1129 /* Expand code size if needed. */
1130 if (max_ent
>= MAXCODE(n_bits
) && max_ent
< db
->maxmaxcode
)
1132 db
->n_bits
= ++n_bits
;
1133 tgtbitno
= 32-n_bits
;
1141 db
->comp_bytes
+= isize
- BSD_OVHD
- PPP_HDRLEN
;
1142 db
->uncomp_bytes
+= explen
;
1148 printk("bsd_decomp%d: peer should have cleared dictionary on %d\n",
1149 db
->unit
, db
->seqno
- 1);
1155 /*************************************************************
1156 * Table of addresses for the BSD compression module
1157 *************************************************************/
1159 static struct compressor ppp_bsd_compress
= {
1160 CI_BSD_COMPRESS
, /* compress_proto */
1161 bsd_comp_alloc
, /* comp_alloc */
1162 bsd_free
, /* comp_free */
1163 bsd_comp_init
, /* comp_init */
1164 bsd_reset
, /* comp_reset */
1165 bsd_compress
, /* compress */
1166 bsd_comp_stats
, /* comp_stat */
1167 bsd_decomp_alloc
, /* decomp_alloc */
1168 bsd_free
, /* decomp_free */
1169 bsd_decomp_init
, /* decomp_init */
1170 bsd_reset
, /* decomp_reset */
1171 bsd_decompress
, /* decompress */
1172 bsd_incomp
, /* incomp */
1173 bsd_comp_stats
/* decomp_stat */
1176 /*************************************************************
1177 * Module support routines
1178 *************************************************************/
1183 int answer
= ppp_register_compressor (&ppp_bsd_compress
);
1185 printk (KERN_INFO
"PPP BSD Compression module registered\n");
1190 cleanup_module(void)
1192 ppp_unregister_compressor (&ppp_bsd_compress
);