Import 2.3.18pre1
[davej-history.git] / drivers / net / bsd_comp.c
blob073a7392ab2b7e656a4f9a9d3dc7db715c466099
1 /* Because this code is derived from the 4.3BSD compress source:
3 * Copyright (c) 1985, 1986 The Regents of the University of California.
4 * All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * James A. Woods, derived from original work by Spencer Thomas
8 * and Joseph Orost.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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
36 * SUCH DAMAGE.
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
56 #ifndef MODULE
57 #error This file must be compiled as a module.
58 #endif
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>
68 #include <linux/in.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>
80 #include <linux/if.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>
90 #undef PACKETPTR
91 #define PACKETPTR 1
92 #include <linux/ppp-comp.h>
93 #undef PACKETPTR
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.
131 struct bsd_dict {
132 union { /* hash value */
133 unsigned long fcode;
134 struct {
135 #if defined(__LITTLE_ENDIAN) /* Little endian order */
136 unsigned short prefix; /* preceding code */
137 unsigned char suffix; /* last character of new code */
138 unsigned char pad;
139 #elif defined(__BIG_ENDIAN) /* Big endian order */
140 unsigned char pad;
141 unsigned char suffix; /* last character of new code */
142 unsigned short prefix; /* preceding code */
143 #else
144 #error Endianness not defined...
145 #endif
146 } hs;
147 } f;
148 unsigned short codem1; /* output of hash table -1 */
149 unsigned short cptr; /* map code to hash table entry */
152 struct bsd_db {
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,
195 int debug);
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 */
217 #define LAST 255
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
237 static void
238 bsd_clear(struct bsd_db *db)
240 db->clear_count++;
241 db->max_ent = FIRST-1;
242 db->n_bits = BSD_INIT_BITS;
243 db->bytes_out = 0;
244 db->in_count = 0;
245 db->ratio = 0;
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)
296 bsd_clear (db);
297 return 1;
299 db->ratio = new_ratio;
302 return 0;
306 * Return statistics.
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;
331 bsd_clear(db);
333 db->seqno = 0;
334 db->clear_count = 0;
338 * Release the compression structure
341 static void bsd_free (void *state)
343 struct bsd_db *db = (struct bsd_db *) state;
345 if (db)
348 * Release the dictionary
350 if (db->dict)
352 vfree (db->dict);
353 db->dict = NULL;
356 * Release the string buffer
358 if (db->lens)
360 vfree (db->lens);
361 db->lens = NULL;
364 * Finally release the structure itself.
366 kfree (db);
367 MOD_DEC_USE_COUNT;
372 * Allocate space for a (de) compressor.
375 static void *bsd_alloc (unsigned char *options, int opt_len, int decomp)
377 int bits;
378 unsigned int hsize, hshift, maxmaxcode;
379 struct bsd_db *db;
381 if (opt_len != 3 || options[0] != CI_BSD_COMPRESS || options[1] != 3
382 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
384 return NULL;
387 bits = BSD_NBITS(options[2]);
389 switch (bits)
391 case 9: /* needs 82152 for both directions */
392 case 10: /* needs 84144 */
393 case 11: /* needs 88240 */
394 case 12: /* needs 96432 */
395 hsize = 5003;
396 hshift = 4;
397 break;
398 case 13: /* needs 176784 */
399 hsize = 9001;
400 hshift = 5;
401 break;
402 case 14: /* needs 353744 */
403 hsize = 18013;
404 hshift = 6;
405 break;
406 case 15: /* needs 691440 */
407 hsize = 35023;
408 hshift = 7;
409 break;
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 */
413 /* break; */
414 default:
415 return NULL;
418 * Allocate the main control structure for this instance.
420 maxmaxcode = MAXCODE(bits);
421 db = (struct bsd_db *) kmalloc (sizeof (struct bsd_db),
422 GFP_KERNEL);
423 if (!db)
425 return NULL;
428 memset (db, 0, sizeof(struct bsd_db));
430 * Allocate space for the dictionary. This may be more than one page in
431 * length.
433 db->dict = (struct bsd_dict *) vmalloc (hsize *
434 sizeof (struct bsd_dict));
435 if (!db->dict)
437 bsd_free (db);
438 return NULL;
441 MOD_INC_USE_COUNT;
443 * If this is the compression buffer then there is no length data.
445 if (!decomp)
447 db->lens = NULL;
450 * For decompression, the length information is needed as well.
452 else
454 db->lens = (unsigned short *) vmalloc ((maxmaxcode + 1) *
455 sizeof (db->lens[0]));
456 if (!db->lens)
458 bsd_free (db);
459 return (NULL);
463 * Initialize the data information for the compression code
465 db->totlen = sizeof (struct bsd_db) +
466 (sizeof (struct bsd_dict) * hsize);
468 db->hsize = hsize;
469 db->hshift = hshift;
470 db->maxmaxcode = maxmaxcode;
471 db->maxbits = bits;
473 return (void *) db;
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;
494 int indx;
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))
501 return 0;
504 if (decomp)
506 indx = LAST;
509 db->lens[indx] = 1;
511 while (indx-- > 0);
514 indx = db->hsize;
515 while (indx-- != 0)
517 db->dict[indx].codem1 = BADCODEM1;
518 db->dict[indx].cptr = 0;
521 db->unit = unit;
522 db->mru = 0;
523 #ifndef DEBUG
524 if (debug)
525 #endif
526 db->debug = 1;
528 bsd_reset(db);
530 return 1;
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,
541 int debug)
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])
553 #ifdef DEBUG
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);
559 idx = 0;
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);
569 idx = 0;
571 return dict_ptrx (db, idx);
574 #else
575 #define lens_ptr(db,idx) lens_ptrx(db,idx)
576 #define dict_ptr(db,idx) dict_ptrx(db,idx)
577 #endif
580 * compress a packet
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)
593 struct bsd_db *db;
594 int hshift;
595 unsigned int max_ent;
596 unsigned int n_bits;
597 unsigned int bitno;
598 unsigned long accm;
599 int ent;
600 unsigned long fcode;
601 struct bsd_dict *dictp;
602 unsigned char c;
603 int hval;
604 int disp;
605 int ilen;
606 int mxcode;
607 unsigned char *wptr;
608 int olen;
610 #define PUTBYTE(v) \
612 ++olen; \
613 if (wptr) \
615 *wptr++ = (unsigned char) (v); \
616 if (olen >= osize) \
618 wptr = NULL; \
623 #define OUTPUT(ent) \
625 bitno -= n_bits; \
626 accm |= ((ent) << bitno); \
627 do \
629 PUTBYTE(accm >> 24); \
630 accm <<= 8; \
631 bitno += 8; \
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)
645 return 0;
648 db = (struct bsd_db *) state;
649 hshift = db->hshift;
650 max_ent = db->max_ent;
651 n_bits = db->n_bits;
652 bitno = 32;
653 accm = 0;
654 mxcode = MAXCODE (n_bits);
656 /* Initialize the output pointers */
657 wptr = obuf;
658 olen = PPP_HDRLEN + BSD_OVHD;
660 if (osize > isize)
662 osize = isize;
665 /* This is the PPP header information */
666 if (wptr)
668 *wptr++ = PPP_ADDRESS(rptr);
669 *wptr++ = PPP_CONTROL(rptr);
670 *wptr++ = 0;
671 *wptr++ = PPP_COMP;
672 *wptr++ = db->seqno >> 8;
673 *wptr++ = db->seqno;
676 /* Skip the input header */
677 rptr += PPP_HDRLEN;
678 isize -= PPP_HDRLEN;
679 ilen = ++isize; /* Low byte of protocol is counted as input */
681 while (--ilen > 0)
683 c = *rptr++;
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)
691 goto nomatch;
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;
705 hval += disp;
706 if (hval >= db->hsize)
708 hval -= db->hsize;
710 dictp = dict_ptr (db, hval);
711 if (dictp->codem1 >= max_ent)
713 goto nomatch;
716 while (dictp->f.fcode != fcode);
718 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
719 continue;
721 nomatch:
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;
729 int indx;
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);
743 indx = dictp2->cptr;
744 dictp3 = dict_ptr (db, indx);
746 if (dictp3->codem1 == max_ent)
748 dictp3->codem1 = BADCODEM1;
751 dictp2->cptr = hval;
752 dictp->codem1 = max_ent;
753 dictp->f.fcode = fcode;
754 db->max_ent = ++max_ent;
756 if (db->lens)
758 unsigned short *len1 = lens_ptr (db, max_ent);
759 unsigned short *len2 = lens_ptr (db, ent);
760 *len1 = *len2 + 1;
763 ent = c;
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;
771 ++db->uncomp_count;
772 ++db->seqno;
774 if (bitno < 32)
776 ++db->bytes_out; /* must be set before calling bsd_check */
780 * Generate the clear command if needed
783 if (bsd_check(db))
785 OUTPUT (CLEAR);
789 * Pad dribble bits of last code with ones.
790 * Do not emit a completely useless byte of ones.
793 if (bitno != 32)
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)
805 db->n_bits++;
808 /* If output length is too large then this is an incomplete frame. */
809 if (wptr == NULL)
811 ++db->incomp_count;
812 db->incomp_bytes += isize;
813 olen = 0;
815 else /* Count the number of compressed frames */
817 ++db->comp_count;
818 db->comp_bytes += olen;
821 /* Return the resulting output length */
822 return olen;
823 #undef OUTPUT
824 #undef PUTBYTE
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
846 * instead.
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)
857 struct bsd_db *db;
858 unsigned int max_ent;
859 unsigned long accm;
860 unsigned int bitno; /* 1st valid bit in accm */
861 unsigned int n_bits;
862 unsigned int tgtbitno; /* bitno when we have a code */
863 struct bsd_dict *dictp;
864 int explen;
865 int seq;
866 unsigned int incode;
867 unsigned int oldcode;
868 unsigned int finchar;
869 unsigned char *p;
870 unsigned char *wptr;
871 int adrs;
872 int ctrl;
873 int ilen;
874 int codelen;
875 int extra;
877 db = (struct bsd_db *) state;
878 max_ent = db->max_ent;
879 accm = 0;
880 bitno = 32; /* 1st valid bit in accm */
881 n_bits = db->n_bits;
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)
904 if (db->debug)
906 printk("bsd_decomp%d: bad sequence # %d, expected %d\n",
907 db->unit, seq, db->seqno - 1);
909 return DECOMP_ERROR;
912 ++db->seqno;
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).
920 wptr = obuf;
921 *wptr++ = adrs;
922 *wptr++ = ctrl;
923 *wptr++ = 0;
925 oldcode = CLEAR;
926 explen = 3;
929 * Keep the checkpoint correctly so that incompressible packets
930 * clear the dictionary at the proper times.
933 for (;;)
935 if (ilen-- <= 0)
937 db->in_count += (explen - 3); /* don't count the header */
938 break;
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.
947 bitno -= 8;
948 accm |= *ibuf++ << bitno;
949 if (tgtbitno < bitno)
951 continue;
954 incode = accm >> tgtbitno;
955 accm <<= n_bits;
956 bitno += n_bits;
959 * The dictionary must only be cleared at the end of a packet.
962 if (incode == CLEAR)
964 if (ilen > 0)
966 if (db->debug)
968 printk("bsd_decomp%d: bad CLEAR\n", db->unit);
970 return DECOMP_FATALERROR; /* probably a bug */
973 bsd_clear(db);
974 break;
977 if ((incode > max_ent + 2) || (incode > db->maxmaxcode)
978 || (incode > max_ent && oldcode == CLEAR))
980 if (db->debug)
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)
993 finchar = oldcode;
994 extra = 1;
996 else
998 finchar = incode;
999 extra = 0;
1002 codelen = *(lens_ptr (db, finchar));
1003 explen += codelen + extra;
1004 if (explen > osize)
1006 if (db->debug)
1008 printk("bsd_decomp%d: ran out of mru\n", db->unit);
1009 #ifdef DEBUG
1010 printk(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
1011 ilen, finchar, codelen, explen);
1012 #endif
1014 return DECOMP_FATALERROR;
1018 * Decode this code and install it in the decompressed buffer.
1021 wptr += codelen;
1022 p = wptr;
1023 while (finchar > LAST)
1025 struct bsd_dict *dictp2 = dict_ptr (db, finchar);
1027 dictp = dict_ptr (db, dictp2->cptr);
1028 #ifdef DEBUG
1029 if (--codelen <= 0 || dictp->codem1 != finchar-1)
1031 if (codelen <= 0)
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);
1037 else
1039 if (dictp->codem1 != finchar-1)
1041 printk("bsd_decomp%d: bad code chain 0x%x "
1042 "finchar=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;
1051 #endif
1052 *--p = dictp->f.hs.suffix;
1053 finchar = dictp->f.hs.prefix;
1055 *--p = finchar;
1057 #ifdef DEBUG
1058 if (--codelen != 0)
1060 printk("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1061 db->unit, codelen, incode, max_ent);
1063 #endif
1065 if (extra) /* the KwKwK case again */
1067 *wptr++ = finchar;
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;
1095 hval += disp;
1096 if (hval >= db->hsize)
1098 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;
1136 oldcode = incode;
1139 ++db->comp_count;
1140 ++db->uncomp_count;
1141 db->comp_bytes += isize - BSD_OVHD - PPP_HDRLEN;
1142 db->uncomp_bytes += explen;
1144 if (bsd_check(db))
1146 if (db->debug)
1148 printk("bsd_decomp%d: peer should have cleared dictionary on %d\n",
1149 db->unit, db->seqno - 1);
1152 return explen;
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 *************************************************************/
1181 init_module(void)
1183 int answer = ppp_register_compressor (&ppp_bsd_compress);
1184 if (answer == 0)
1185 printk (KERN_INFO "PPP BSD Compression module registered\n");
1186 return answer;
1189 void
1190 cleanup_module(void)
1192 ppp_unregister_compressor (&ppp_bsd_compress);