1 /* $NetBSD: bsd-comp.c,v 1.19 2008/11/25 02:40:36 cube Exp $ */
2 /* Id: bsd-comp.c,v 1.6 1996/08/28 06:31:58 paulus Exp */
4 /* Because this code is derived from the 4.3BSD compress source:
7 * Copyright (c) 1985, 1986 The Regents of the University of California.
10 * This code is derived from software contributed to Berkeley by
11 * James A. Woods, derived from original work by Spencer Thomas
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. 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 mbufs on BSD-derived systems.
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: bsd-comp.c,v 1.19 2008/11/25 02:40:36 cube Exp $");
46 #include <sys/param.h>
47 #include <sys/systm.h>
49 #include <sys/socket.h>
50 #include <sys/module.h>
52 #include <net/if_types.h>
53 #include <net/ppp_defs.h>
54 #include <net/if_ppp.h>
56 #define PACKETPTR struct mbuf *
57 #include <net/ppp-comp.h>
61 * PPP "BSD compress" compression
62 * The differences between this compression and the classic BSD LZW
63 * source are obvious from the requirement that the classic code worked
64 * with files while this handles arbitrarily long streams that
65 * are broken into packets. They are:
67 * When the code size expands, a block of junk is not emitted by
68 * the compressor and not expected by the decompressor.
70 * New codes are not necessarily assigned every time an old
71 * code is output by the compressor. This is because a packet
72 * end forces a code to be emitted, but does not imply that a
73 * new sequence has been seen.
75 * The compression ratio is checked at the first end of a packet
76 * after the appropriate gap. Besides simplifying and speeding
77 * things up, this makes it more likely that the transmitter
78 * and receiver will agree when the dictionary is cleared when
79 * compression is not going well.
83 * A dictionary for doing BSD compress.
86 int totlen
; /* length of this structure */
87 u_int hsize
; /* size of the hash table */
88 u_char hshift
; /* used in hash function */
89 u_char n_bits
; /* current bits/code */
93 uint16_t seqno
; /* sequence # of next packet */
94 u_int hdrlen
; /* header length to preallocate */
96 u_int maxmaxcode
; /* largest valid code */
97 u_int max_ent
; /* largest code in use */
98 u_int in_count
; /* uncompressed bytes, aged */
99 u_int bytes_out
; /* compressed bytes, aged */
100 u_int ratio
; /* recent compression ratio */
101 u_int checkpoint
; /* when to next check the ratio */
102 u_int clear_count
; /* times dictionary cleared */
103 u_int incomp_count
; /* incompressible packets */
104 u_int incomp_bytes
; /* incompressible bytes */
105 u_int uncomp_count
; /* uncompressed packets */
106 u_int uncomp_bytes
; /* uncompressed bytes */
107 u_int comp_count
; /* compressed packets */
108 u_int comp_bytes
; /* compressed bytes */
109 uint16_t *lens
; /* array of lengths of codes */
111 union { /* hash value */
114 #if BYTE_ORDER == LITTLE_ENDIAN
115 uint16_t prefix
; /* preceding code */
116 u_char suffix
; /* last character of new code */
120 u_char suffix
; /* last character of new code */
121 uint16_t prefix
; /* preceding code */
125 uint16_t codem1
; /* output of hash table -1 */
126 uint16_t cptr
; /* map code to hash table entry */
130 #define BSD_OVHD 2 /* BSD compress overhead/packet */
131 #define BSD_INIT_BITS BSD_MIN_BITS
133 static void *bsd_comp_alloc(u_char
*options
, int opt_len
);
134 static void *bsd_decomp_alloc(u_char
*options
, int opt_len
);
135 static void bsd_free(void *state
);
136 static int bsd_comp_init(void *state
, u_char
*options
, int opt_len
,
137 int unit
, int hdrlen
, int debug
);
138 static int bsd_decomp_init(void *state
, u_char
*options
, int opt_len
,
139 int unit
, int hdrlen
, int mru
, int debug
);
140 static int bsd_compress(void *state
, struct mbuf
**mret
,
141 struct mbuf
*mp
, int slen
, int maxolen
);
142 static void bsd_incomp(void *state
, struct mbuf
*dmsg
);
143 static int bsd_decompress(void *state
, struct mbuf
*cmp
,
145 static void bsd_reset(void *state
);
146 static void bsd_comp_stats(void *state
, struct compstat
*stats
);
149 * Procedures exported to if_ppp.c.
151 static struct compressor ppp_bsd_compress
= {
152 .compress_proto
= CI_BSD_COMPRESS
,
153 .comp_alloc
= bsd_comp_alloc
,
154 .comp_free
= bsd_free
,
155 .comp_init
= bsd_comp_init
,
156 .comp_reset
= bsd_reset
,
157 .compress
= bsd_compress
,
158 .comp_stat
= bsd_comp_stats
,
159 .decomp_alloc
= bsd_decomp_alloc
,
160 .decomp_free
= bsd_free
,
161 .decomp_init
= bsd_decomp_init
,
162 .decomp_reset
= bsd_reset
,
163 .decompress
= bsd_decompress
,
164 .incomp
= bsd_incomp
,
165 .decomp_stat
= bsd_comp_stats
,
169 * the next two codes should not be changed lightly, as they must not
170 * lie within the contiguous general code space.
172 #define CLEAR 256 /* table clear output code */
173 #define FIRST 257 /* first free entry */
176 #define MAXCODE(b) ((1 << (b)) - 1)
177 #define BADCODEM1 MAXCODE(BSD_MAX_BITS)
179 #define BSD_HASH(prefix,suffix,hshift) ((((uint32_t)(suffix)) << (hshift)) \
180 ^ (uint32_t)(prefix))
181 #define BSD_KEY(prefix,suffix) ((((uint32_t)(suffix)) << 16) \
182 + (uint32_t)(prefix))
184 #define CHECK_GAP 10000 /* Ratio check interval */
186 #define RATIO_SCALE_LOG 8
187 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
188 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
190 static void bsd_clear(struct bsd_db
*);
191 static int bsd_check(struct bsd_db
*);
192 static void *bsd_alloc(u_char
*, int, int);
193 static int bsd_init(struct bsd_db
*, u_char
*, int, int, int, int,
197 * clear the dictionary
200 bsd_clear(struct bsd_db
*db
)
203 db
->max_ent
= FIRST
-1;
204 db
->n_bits
= BSD_INIT_BITS
;
208 db
->checkpoint
= CHECK_GAP
;
212 * If the dictionary is full, then see if it is time to reset it.
214 * Compute the compression ratio using fixed-point arithmetic
215 * with 8 fractional bits.
217 * Since we have an infinite stream instead of a single file,
218 * watch only the local compression ratio.
220 * Since both peers must reset the dictionary at the same time even in
221 * the absence of CLEAR codes (while packets are incompressible), they
222 * must compute the same ratio.
224 static int /* 1=output CLEAR */
225 bsd_check(struct bsd_db
*db
)
229 if (db
->in_count
>= db
->checkpoint
) {
230 /* age the ratio by limiting the size of the counts */
231 if (db
->in_count
>= RATIO_MAX
232 || db
->bytes_out
>= RATIO_MAX
) {
233 db
->in_count
-= db
->in_count
/4;
234 db
->bytes_out
-= db
->bytes_out
/4;
237 db
->checkpoint
= db
->in_count
+ CHECK_GAP
;
239 if (db
->max_ent
>= db
->maxmaxcode
) {
240 /* Reset the dictionary only if the ratio is worse,
241 * or if it looks as if it has been poisoned
242 * by incompressible data.
244 * This does not overflow, because
245 * db->in_count <= RATIO_MAX.
247 new_ratio
= db
->in_count
<< RATIO_SCALE_LOG
;
248 if (db
->bytes_out
!= 0)
249 new_ratio
/= db
->bytes_out
;
251 if (new_ratio
< db
->ratio
|| new_ratio
< 1 * RATIO_SCALE
) {
255 db
->ratio
= new_ratio
;
265 bsd_comp_stats(void *state
, struct compstat
*stats
)
267 struct bsd_db
*db
= (struct bsd_db
*) state
;
270 stats
->unc_bytes
= db
->uncomp_bytes
;
271 stats
->unc_packets
= db
->uncomp_count
;
272 stats
->comp_bytes
= db
->comp_bytes
;
273 stats
->comp_packets
= db
->comp_count
;
274 stats
->inc_bytes
= db
->incomp_bytes
;
275 stats
->inc_packets
= db
->incomp_count
;
276 stats
->ratio
= db
->in_count
;
278 if (stats
->ratio
<= 0x7fffff)
287 * Reset state, as on a CCP ResetReq.
290 bsd_reset(void *state
)
292 struct bsd_db
*db
= (struct bsd_db
*) state
;
300 * Allocate space for a (de) compressor.
303 bsd_alloc(u_char
*options
, int opt_len
, int decomp
)
306 u_int newlen
, hsize
, hshift
, maxmaxcode
;
309 if (opt_len
< CILEN_BSD_COMPRESS
|| options
[0] != CI_BSD_COMPRESS
310 || options
[1] != CILEN_BSD_COMPRESS
311 || BSD_VERSION(options
[2]) != BSD_CURRENT_VERSION
)
313 bits
= BSD_NBITS(options
[2]);
315 case 9: /* needs 82152 for both directions */
316 case 10: /* needs 84144 */
317 case 11: /* needs 88240 */
318 case 12: /* needs 96432 */
322 case 13: /* needs 176784 */
326 case 14: /* needs 353744 */
330 case 15: /* needs 691440 */
334 case 16: /* needs 1366160--far too much, */
335 /* hsize = 69001; */ /* and 69001 is too big for cptr */
336 /* hshift = 8; */ /* in struct bsd_db */
342 maxmaxcode
= MAXCODE(bits
);
343 newlen
= sizeof(*db
) + (hsize
-1) * (sizeof(db
->dict
[0]));
344 db
= malloc(newlen
, M_DEVBUF
, M_NOWAIT
|M_ZERO
);
351 db
->lens
= malloc((maxmaxcode
+1) * sizeof(db
->lens
[0]),
362 db
->maxmaxcode
= maxmaxcode
;
369 bsd_free(void *state
)
371 struct bsd_db
*db
= (struct bsd_db
*) state
;
374 free(db
->lens
, M_DEVBUF
);
379 bsd_comp_alloc(u_char
*options
, int opt_len
)
381 return bsd_alloc(options
, opt_len
, 0);
385 bsd_decomp_alloc(u_char
*options
, int opt_len
)
387 return bsd_alloc(options
, opt_len
, 1);
391 * Initialize the database.
394 bsd_init(struct bsd_db
*db
, u_char
*options
, int opt_len
, int unit
, int hdrlen
,
395 int mru
, int debug
, int decomp
)
399 if (opt_len
< CILEN_BSD_COMPRESS
|| options
[0] != CI_BSD_COMPRESS
400 || options
[1] != CILEN_BSD_COMPRESS
401 || BSD_VERSION(options
[2]) != BSD_CURRENT_VERSION
402 || BSD_NBITS(options
[2]) != db
->maxbits
403 || (decomp
&& db
->lens
== NULL
))
413 db
->dict
[--i
].codem1
= BADCODEM1
;
414 db
->dict
[i
].cptr
= 0;
431 bsd_comp_init(void *state
, u_char
*options
, int opt_len
, int unit
, int hdrlen
,
434 return bsd_init((struct bsd_db
*) state
, options
, opt_len
,
435 unit
, hdrlen
, 0, debug
, 0);
439 bsd_decomp_init(void *state
, u_char
*options
, int opt_len
, int unit
, int hdrlen
,
442 return bsd_init((struct bsd_db
*) state
, options
, opt_len
,
443 unit
, hdrlen
, mru
, debug
, 1);
449 * One change from the BSD compress command is that when the
450 * code size expands, we do not output a bunch of padding.
453 bsd_compress(void *state
,
454 struct mbuf
**mret
/* return compressed mbuf chain here */,
455 struct mbuf
*mp
/* from here */,
456 int slen
, int maxolen
)
458 struct bsd_db
*db
= (struct bsd_db
*) state
;
459 int hshift
= db
->hshift
;
460 u_int max_ent
= db
->max_ent
;
461 u_int n_bits
= db
->n_bits
;
463 uint32_t accm
= 0, fcode
;
464 struct bsd_dict
*dictp
;
466 int hval
, disp
, ent
, ilen
;
472 #define PUTBYTE(v) { \
476 if (wptr >= cp_end) { \
477 m->m_len = wptr - mtod(m, u_char *); \
478 MGET(m->m_next, M_DONTWAIT, MT_DATA); \
482 if (maxolen - olen > MLEN) \
483 MCLGET(m, M_DONTWAIT); \
484 wptr = mtod(m, u_char *); \
485 cp_end = wptr + M_TRAILINGSPACE(m); \
492 #define OUTPUT(ent) { \
494 accm |= ((ent) << bitno); \
496 PUTBYTE(accm >> 24); \
499 } while (bitno <= 24); \
503 * If the protocol is not in the range we're interested in,
504 * just return without compressing the packet. If it is,
505 * the protocol becomes the first byte to compress.
507 rptr
= mtod(mp
, u_char
*);
508 ent
= PPP_PROTOCOL(rptr
);
509 if (ent
< 0x21 || ent
> 0xf9) {
514 /* Don't generate compressed packets which are larger than
515 the uncompressed packet. */
519 /* Allocate one mbuf to start with. */
520 MGET(m
, M_DONTWAIT
, MT_DATA
);
524 if (maxolen
+ db
->hdrlen
> MLEN
)
525 MCLGET(m
, M_DONTWAIT
);
526 m
->m_data
+= db
->hdrlen
;
527 wptr
= mtod(m
, u_char
*);
528 cp_end
= wptr
+ M_TRAILINGSPACE(m
);
530 wptr
= cp_end
= NULL
;
533 * Copy the PPP header over, changing the protocol,
534 * and install the 2-byte packet sequence number.
537 *wptr
++ = PPP_ADDRESS(rptr
); /* assumes the ppp header is */
538 *wptr
++ = PPP_CONTROL(rptr
); /* all in one mbuf */
539 *wptr
++ = 0; /* change the protocol */
541 *wptr
++ = db
->seqno
>> 8;
548 slen
= mp
->m_len
- PPP_HDRLEN
;
555 rptr
= mtod(mp
, u_char
*);
558 continue; /* handle 0-length buffers */
564 fcode
= BSD_KEY(ent
, c
);
565 hval
= BSD_HASH(ent
, c
, hshift
);
566 dictp
= &db
->dict
[hval
];
568 /* Validate and then check the entry. */
569 if (dictp
->codem1
>= max_ent
)
571 if (dictp
->f
.fcode
== fcode
) {
572 ent
= dictp
->codem1
+1;
573 continue; /* found (prefix,suffix) */
576 /* continue probing until a match or invalid entry */
577 disp
= (hval
== 0) ? 1 : hval
;
580 if (hval
>= db
->hsize
)
582 dictp
= &db
->dict
[hval
];
583 if (dictp
->codem1
>= max_ent
)
585 } while (dictp
->f
.fcode
!= fcode
);
586 ent
= dictp
->codem1
+ 1; /* finally found (prefix,suffix) */
590 OUTPUT(ent
); /* output the prefix */
592 /* code -> hashtable */
593 if (max_ent
< db
->maxmaxcode
) {
594 struct bsd_dict
*dictp2
;
595 /* expand code size if needed */
596 if (max_ent
>= MAXCODE(n_bits
))
597 db
->n_bits
= ++n_bits
;
599 /* Invalidate old hash table entry using
600 * this code, and then take it over.
602 dictp2
= &db
->dict
[max_ent
+1];
603 if (db
->dict
[dictp2
->cptr
].codem1
== max_ent
)
604 db
->dict
[dictp2
->cptr
].codem1
= BADCODEM1
;
606 dictp
->codem1
= max_ent
;
607 dictp
->f
.fcode
= fcode
;
609 db
->max_ent
= ++max_ent
;
614 OUTPUT(ent
); /* output the last code */
615 db
->bytes_out
+= olen
;
616 db
->in_count
+= ilen
;
618 ++db
->bytes_out
; /* count complete bytes */
621 OUTPUT(CLEAR
); /* do not count the CLEAR */
624 * Pad dribble bits of last code with ones.
625 * Do not emit a completely useless byte of ones.
628 PUTBYTE((accm
| (0xff << (bitno
-8))) >> 24);
631 m
->m_len
= wptr
- mtod(m
, u_char
*);
636 * Increase code size if we would have without the packet
637 * boundary and as the decompressor will.
639 if (max_ent
>= MAXCODE(n_bits
) && max_ent
< db
->maxmaxcode
)
642 db
->uncomp_bytes
+= ilen
;
644 if (olen
+ PPP_HDRLEN
+ BSD_OVHD
> maxolen
) {
645 /* throw away the compressed stuff if it is longer than uncompressed */
651 db
->incomp_bytes
+= ilen
;
654 db
->comp_bytes
+= olen
+ BSD_OVHD
;
657 return olen
+ PPP_HDRLEN
+ BSD_OVHD
;
664 * Update the "BSD Compress" dictionary on the receiver for
665 * incompressible data by pretending to compress the incoming data.
668 bsd_incomp(void *state
, struct mbuf
*dmsg
)
670 struct bsd_db
*db
= (struct bsd_db
*) state
;
671 u_int hshift
= db
->hshift
;
672 u_int max_ent
= db
->max_ent
;
673 u_int n_bits
= db
->n_bits
;
674 struct bsd_dict
*dictp
;
684 * If the protocol is not in the range we're interested in,
685 * just return without looking at the packet. If it is,
686 * the protocol becomes the first byte to "compress".
688 rptr
= mtod(dmsg
, u_char
*);
689 ent
= PPP_PROTOCOL(rptr
);
690 if (ent
< 0x21 || ent
> 0xf9)
694 ilen
= 1; /* count the protocol as 1 byte */
696 slen
= dmsg
->m_len
- PPP_HDRLEN
;
702 rptr
= mtod(dmsg
, u_char
*);
710 fcode
= BSD_KEY(ent
, c
);
711 hval
= BSD_HASH(ent
, c
, hshift
);
712 dictp
= &db
->dict
[hval
];
714 /* validate and then check the entry */
715 if (dictp
->codem1
>= max_ent
)
717 if (dictp
->f
.fcode
== fcode
) {
718 ent
= dictp
->codem1
+1;
719 continue; /* found (prefix,suffix) */
722 /* continue probing until a match or invalid entry */
723 disp
= (hval
== 0) ? 1 : hval
;
726 if (hval
>= db
->hsize
)
728 dictp
= &db
->dict
[hval
];
729 if (dictp
->codem1
>= max_ent
)
731 } while (dictp
->f
.fcode
!= fcode
);
732 ent
= dictp
->codem1
+1;
733 continue; /* finally found (prefix,suffix) */
735 nomatch
: /* output (count) the prefix */
738 /* code -> hashtable */
739 if (max_ent
< db
->maxmaxcode
) {
740 struct bsd_dict
*dictp2
;
741 /* expand code size if needed */
742 if (max_ent
>= MAXCODE(n_bits
))
743 db
->n_bits
= ++n_bits
;
745 /* Invalidate previous hash table entry
746 * assigned this code, and then take it over.
748 dictp2
= &db
->dict
[max_ent
+1];
749 if (db
->dict
[dictp2
->cptr
].codem1
== max_ent
)
750 db
->dict
[dictp2
->cptr
].codem1
= BADCODEM1
;
752 dictp
->codem1
= max_ent
;
753 dictp
->f
.fcode
= fcode
;
755 db
->max_ent
= ++max_ent
;
756 db
->lens
[max_ent
] = db
->lens
[ent
]+1;
759 } while (--slen
!= 0);
761 bitno
+= n_bits
; /* output (count) the last code */
762 db
->bytes_out
+= bitno
/8;
763 db
->in_count
+= ilen
;
767 db
->incomp_bytes
+= ilen
;
769 db
->uncomp_bytes
+= ilen
;
771 /* Increase code size if we would have without the packet
772 * boundary and as the decompressor will.
774 if (max_ent
>= MAXCODE(n_bits
) && max_ent
< db
->maxmaxcode
)
780 * Decompress "BSD Compress".
782 * Because of patent problems, we return DECOMP_ERROR for errors
783 * found by inspecting the input data and for system problems, but
784 * DECOMP_FATALERROR for any errors which could possibly be said to
785 * be being detected "after" decompression. For DECOMP_ERROR,
786 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
787 * infringing a patent of Motorola's if we do, so we take CCP down
790 * Given that the frame has the correct sequence number and a good FCS,
791 * errors such as invalid codes in the input most likely indicate a
792 * bug, so we return DECOMP_FATALERROR for them in order to turn off
793 * compression, even though they are detected by inspecting the input.
796 bsd_decompress(void *state
, struct mbuf
*cmp
, struct mbuf
**dmpp
)
798 struct bsd_db
*db
= (struct bsd_db
*) state
;
799 u_int max_ent
= db
->max_ent
;
801 u_int bitno
= 32; /* 1st valid bit in accm */
802 u_int n_bits
= db
->n_bits
;
803 u_int tgtbitno
= 32-n_bits
; /* bitno when we have a code */
804 struct bsd_dict
*dictp
;
805 int explen
, i
, seq
, len
;
806 u_int incode
, oldcode
, finchar
;
807 u_char
*p
, *rptr
, *wptr
;
808 struct mbuf
*m
, *dmp
, *mret
;
809 int adrs
, ctrl
, ilen
;
810 int space
, codelen
, extra
;
813 * Save the address/control from the PPP header
814 * and then get the sequence number.
817 rptr
= mtod(cmp
, u_char
*);
818 adrs
= PPP_ADDRESS(rptr
);
819 ctrl
= PPP_CONTROL(rptr
);
821 len
= cmp
->m_len
- PPP_HDRLEN
;
823 for (i
= 0; i
< 2; ++i
) {
828 rptr
= mtod(cmp
, u_char
*);
831 seq
= (seq
<< 8) + *rptr
++;
836 * Check the sequence number and give up if it differs from
837 * the value we're expecting.
839 if (seq
!= db
->seqno
) {
841 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
842 db
->unit
, seq
, db
->seqno
- 1);
848 * Allocate one mbuf to start with.
850 MGETHDR(dmp
, M_DONTWAIT
, MT_DATA
);
856 MCLGET(dmp
, M_DONTWAIT
);
857 dmp
->m_data
+= db
->hdrlen
;
858 wptr
= mtod(dmp
, u_char
*);
859 space
= M_TRAILINGSPACE(dmp
) - PPP_HDRLEN
+ 1;
862 * Fill in the ppp header, but not the last byte of the protocol
863 * (that comes from the decompressed data).
868 wptr
+= PPP_HDRLEN
- 1;
876 if (!cmp
) /* quit at end of message */
878 rptr
= mtod(cmp
, u_char
*);
881 continue; /* handle 0-length buffers */
885 * Accumulate bytes until we have a complete code.
886 * Then get the next code, relying on the 32-bit,
887 * unsigned accm to mask the result.
890 accm
|= *rptr
++ << bitno
;
892 if (tgtbitno
< bitno
)
894 incode
= accm
>> tgtbitno
;
898 if (incode
== CLEAR
) {
900 * The dictionary must only be cleared at
901 * the end of a packet. But there could be an
902 * empty mbuf at the end.
904 if (len
> 0 || cmp
->m_next
!= NULL
) {
905 while ((cmp
= cmp
->m_next
) != NULL
)
910 printf("bsd_decomp%d: bad CLEAR\n", db
->unit
);
911 return DECOMP_FATALERROR
; /* probably a bug */
919 if (incode
> max_ent
+ 2 || incode
> db
->maxmaxcode
920 || (incode
> max_ent
&& oldcode
== CLEAR
)) {
923 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
924 db
->unit
, incode
, oldcode
);
925 printf("max_ent=0x%x explen=%d seqno=%d\n",
926 max_ent
, explen
, db
->seqno
);
928 return DECOMP_FATALERROR
; /* probably a bug */
931 /* Special case for KwKwK string. */
932 if (incode
> max_ent
) {
940 codelen
= db
->lens
[finchar
];
941 explen
+= codelen
+ extra
;
942 if (explen
> db
->mru
+ 1) {
945 printf("bsd_decomp%d: ran out of mru\n", db
->unit
);
947 while ((cmp
= cmp
->m_next
) != NULL
)
949 printf(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
950 len
, finchar
, codelen
, explen
);
953 return DECOMP_FATALERROR
;
957 * For simplicity, the decoded characters go in a single mbuf,
958 * so we allocate a single extra cluster mbuf if necessary.
960 if ((space
-= codelen
+ extra
) < 0) {
961 dmp
->m_len
= wptr
- mtod(dmp
, u_char
*);
962 MGET(m
, M_DONTWAIT
, MT_DATA
);
970 MCLGET(m
, M_DONTWAIT
);
971 space
= M_TRAILINGSPACE(m
) - (codelen
+ extra
);
973 /* now that's what I call *compression*. */
978 wptr
= mtod(dmp
, u_char
*);
982 * Decode this code and install it in the decompressed buffer.
984 p
= (wptr
+= codelen
);
985 while (finchar
> LAST
) {
986 dictp
= &db
->dict
[db
->dict
[finchar
].cptr
];
988 if (--codelen
<= 0 || dictp
->codem1
!= finchar
-1)
991 *--p
= dictp
->f
.hs
.suffix
;
992 finchar
= dictp
->f
.hs
.prefix
;
998 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
999 db
->unit
, codelen
, incode
, max_ent
);
1002 if (extra
) /* the KwKwK case again */
1006 * If not first code in a packet, and
1007 * if not out of code space, then allocate a new code.
1009 * Keep the hash table correct so it can be used
1010 * with uncompressed packets.
1012 if (oldcode
!= CLEAR
&& max_ent
< db
->maxmaxcode
) {
1013 struct bsd_dict
*dictp2
;
1015 uint32_t hval
, disp
;
1017 fcode
= BSD_KEY(oldcode
,finchar
);
1018 hval
= BSD_HASH(oldcode
,finchar
,db
->hshift
);
1019 dictp
= &db
->dict
[hval
];
1021 /* look for a free hash table entry */
1022 if (dictp
->codem1
< max_ent
) {
1023 disp
= (hval
== 0) ? 1 : hval
;
1026 if (hval
>= db
->hsize
)
1028 dictp
= &db
->dict
[hval
];
1029 } while (dictp
->codem1
< max_ent
);
1033 * Invalidate previous hash table entry
1034 * assigned this code, and then take it over
1036 dictp2
= &db
->dict
[max_ent
+1];
1037 if (db
->dict
[dictp2
->cptr
].codem1
== max_ent
) {
1038 db
->dict
[dictp2
->cptr
].codem1
= BADCODEM1
;
1040 dictp2
->cptr
= hval
;
1041 dictp
->codem1
= max_ent
;
1042 dictp
->f
.fcode
= fcode
;
1044 db
->max_ent
= ++max_ent
;
1045 db
->lens
[max_ent
] = db
->lens
[oldcode
]+1;
1047 /* Expand code size if needed. */
1048 if (max_ent
>= MAXCODE(n_bits
) && max_ent
< db
->maxmaxcode
) {
1049 db
->n_bits
= ++n_bits
;
1050 tgtbitno
= 32-n_bits
;
1055 dmp
->m_len
= wptr
- mtod(dmp
, u_char
*);
1058 * Keep the checkpoint right so that incompressible packets
1059 * clear the dictionary at the right times.
1061 db
->bytes_out
+= ilen
;
1062 db
->in_count
+= explen
;
1063 if (bsd_check(db
) && db
->debug
) {
1064 printf("bsd_decomp%d: peer should have cleared dictionary\n",
1069 db
->comp_bytes
+= ilen
+ BSD_OVHD
;
1071 db
->uncomp_bytes
+= explen
;
1079 printf("bsd_decomp%d: fell off end of chain ", db
->unit
);
1080 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1081 incode
, finchar
, db
->dict
[finchar
].cptr
, max_ent
);
1082 } else if (dictp
->codem1
!= finchar
-1) {
1083 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1084 db
->unit
, incode
, finchar
);
1085 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode
,
1086 db
->dict
[finchar
].cptr
, dictp
->codem1
);
1089 return DECOMP_FATALERROR
;
1093 MODULE(MODULE_CLASS_MISC
, ppp_bsdcomp
, NULL
);
1096 ppp_bsdcomp_modcmd(modcmd_t cmd
, void *arg
)
1100 case MODULE_CMD_INIT
:
1101 return ppp_register_compressor(&ppp_bsd_compress
, 1);
1102 case MODULE_CMD_FINI
:
1103 return ppp_unregister_compressor(&ppp_bsd_compress
, 1);
1104 case MODULE_CMD_STAT
:
1112 #endif /* DO_BSD_COMPRESS */