2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Because this code is derived from the 4.3BSD compress source:
28 * Copyright (c) 1985, 1986 The Regents of the University of California.
29 * All rights reserved.
31 * This code is derived from software contributed to Berkeley by
32 * James A. Woods, derived from original work by Spencer Thomas
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * This version is for use with mbufs on BSD-derived systems.
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/malloc.h>
73 #include <net/ppp_defs.h>
75 #define PACKETPTR struct mbuf *
76 #include <net/ppp_comp.h>
80 * PPP "BSD compress" compression
81 * The differences between this compression and the classic BSD LZW
82 * source are obvious from the requirement that the classic code worked
83 * with files while this handles arbitrarily long streams that
84 * are broken into packets. They are:
86 * When the code size expands, a block of junk is not emitted by
87 * the compressor and not expected by the decompressor.
89 * New codes are not necessarily assigned every time an old
90 * code is output by the compressor. This is because a packet
91 * end forces a code to be emitted, but does not imply that a
92 * new sequence has been seen.
94 * The compression ratio is checked at the first end of a packet
95 * after the appropriate gap. Besides simplifying and speeding
96 * things up, this makes it more likely that the transmitter
97 * and receiver will agree when the dictionary is cleared when
98 * compression is not going well.
102 * A dictionary for doing BSD compress.
105 int totlen
; /* length of this structure */
106 u_int hsize
; /* size of the hash table */
107 u_char hshift
; /* used in hash function */
108 u_char n_bits
; /* current bits/code */
112 u_int16_t seqno
; /* sequence # of next packet */
113 u_int hdrlen
; /* header length to preallocate */
115 u_int maxmaxcode
; /* largest valid code */
116 u_int max_ent
; /* largest code in use */
117 u_int in_count
; /* uncompressed bytes, aged */
118 u_int bytes_out
; /* compressed bytes, aged */
119 u_int ratio
; /* recent compression ratio */
120 u_int checkpoint
; /* when to next check the ratio */
121 u_int clear_count
; /* times dictionary cleared */
122 u_int incomp_count
; /* incompressible packets */
123 u_int incomp_bytes
; /* incompressible bytes */
124 u_int uncomp_count
; /* uncompressed packets */
125 u_int uncomp_bytes
; /* uncompressed bytes */
126 u_int comp_count
; /* compressed packets */
127 u_int comp_bytes
; /* compressed bytes */
128 u_int16_t
*lens
; /* array of lengths of codes */
130 union { /* hash value */
133 #if BYTE_ORDER == LITTLE_ENDIAN
134 u_int16_t prefix
; /* preceding code */
135 u_char suffix
; /* last character of new code */
139 u_char suffix
; /* last character of new code */
140 u_int16_t prefix
; /* preceding code */
144 u_int16_t codem1
; /* output of hash table -1 */
145 u_int16_t cptr
; /* map code to hash table entry */
149 #define BSD_OVHD 2 /* BSD compress overhead/packet */
150 #define BSD_INIT_BITS BSD_MIN_BITS
152 static void bsd_clear
__P((struct bsd_db
*db
));
153 static int bsd_check
__P((struct bsd_db
*db
));
154 static void *bsd_alloc
__P((u_char
*options
, int opt_len
, int decomp
));
155 static int bsd_init_comp_db
__P((struct bsd_db
*db
, u_char
*options
, int opt_len
,
156 int unit
, int hdrlen
, int mru
, int debug
,
158 static void *bsd_comp_alloc
__P((u_char
*options
, int opt_len
));
159 static void *bsd_decomp_alloc
__P((u_char
*options
, int opt_len
));
160 static void bsd_free
__P((void *state
));
161 static int bsd_comp_init
__P((void *state
, u_char
*options
, int opt_len
,
162 int unit
, int hdrlen
, int debug
));
163 static int bsd_decomp_init
__P((void *state
, u_char
*options
, int opt_len
,
164 int unit
, int hdrlen
, int mru
, int debug
));
165 static int bsd_compress
__P((void *state
, struct mbuf
**mret
,
166 struct mbuf
*mp
, int slen
, int maxolen
));
167 static void bsd_incomp
__P((void *state
, struct mbuf
*dmsg
));
168 static int bsd_decompress
__P((void *state
, struct mbuf
*cmp
,
169 struct mbuf
**dmpp
));
170 static void bsd_reset
__P((void *state
));
171 static void bsd_comp_stats
__P((void *state
, struct compstat
*stats
));
174 * Procedures exported to if_ppp.c.
176 struct compressor ppp_bsd_compress
= {
177 CI_BSD_COMPRESS
, /* compress_proto */
178 bsd_comp_alloc
, /* comp_alloc */
179 bsd_free
, /* comp_free */
180 bsd_comp_init
, /* comp_init */
181 bsd_reset
, /* comp_reset */
182 bsd_compress
, /* compress */
183 bsd_comp_stats
, /* comp_stat */
184 bsd_decomp_alloc
, /* decomp_alloc */
185 bsd_free
, /* decomp_free */
186 bsd_decomp_init
, /* decomp_init */
187 bsd_reset
, /* decomp_reset */
188 bsd_decompress
, /* decompress */
189 bsd_incomp
, /* incomp */
190 bsd_comp_stats
, /* decomp_stat */
194 * the next two codes should not be changed lightly, as they must not
195 * lie within the contiguous general code space.
197 #define CLEAR 256 /* table clear output code */
198 #define FIRST 257 /* first free entry */
201 #define MAXCODE(b) ((1 << (b)) - 1)
202 #define BADCODEM1 MAXCODE(BSD_MAX_BITS)
204 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
205 ^ (u_int32_t)(prefix))
206 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
207 + (u_int32_t)(prefix))
209 #define CHECK_GAP 10000 /* Ratio check interval */
211 #define RATIO_SCALE_LOG 8
212 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
213 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
216 * clear the dictionary
223 db
->max_ent
= FIRST
-1;
224 db
->n_bits
= BSD_INIT_BITS
;
228 db
->checkpoint
= CHECK_GAP
;
232 * If the dictionary is full, then see if it is time to reset it.
234 * Compute the compression ratio using fixed-point arithmetic
235 * with 8 fractional bits.
237 * Since we have an infinite stream instead of a single file,
238 * watch only the local compression ratio.
240 * Since both peers must reset the dictionary at the same time even in
241 * the absence of CLEAR codes (while packets are incompressible), they
242 * must compute the same ratio.
244 static int /* 1=output CLEAR */
250 if (db
->in_count
>= db
->checkpoint
) {
251 /* age the ratio by limiting the size of the counts */
252 if (db
->in_count
>= RATIO_MAX
253 || db
->bytes_out
>= RATIO_MAX
) {
254 db
->in_count
-= db
->in_count
/4;
255 db
->bytes_out
-= db
->bytes_out
/4;
258 db
->checkpoint
= db
->in_count
+ CHECK_GAP
;
260 if (db
->max_ent
>= db
->maxmaxcode
) {
261 /* Reset the dictionary only if the ratio is worse,
262 * or if it looks as if it has been poisoned
263 * by incompressible data.
265 * This does not overflow, because
266 * db->in_count <= RATIO_MAX.
268 new_ratio
= db
->in_count
<< RATIO_SCALE_LOG
;
269 if (db
->bytes_out
!= 0)
270 new_ratio
/= db
->bytes_out
;
272 if (new_ratio
< db
->ratio
|| new_ratio
< 1 * RATIO_SCALE
) {
276 db
->ratio
= new_ratio
;
286 bsd_comp_stats(state
, stats
)
288 struct compstat
*stats
;
290 struct bsd_db
*db
= (struct bsd_db
*) state
;
293 stats
->unc_bytes
= db
->uncomp_bytes
;
294 stats
->unc_packets
= db
->uncomp_count
;
295 stats
->comp_bytes
= db
->comp_bytes
;
296 stats
->comp_packets
= db
->comp_count
;
297 stats
->inc_bytes
= db
->incomp_bytes
;
298 stats
->inc_packets
= db
->incomp_count
;
299 stats
->ratio
= db
->in_count
;
301 if (stats
->ratio
<= 0x7fffff)
310 * Reset state, as on a CCP ResetReq.
316 struct bsd_db
*db
= (struct bsd_db
*) state
;
324 * Allocate space for a (de) compressor.
327 bsd_alloc(options
, opt_len
, decomp
)
332 u_int newlen
, hsize
, hshift
, maxmaxcode
;
335 if (opt_len
< CILEN_BSD_COMPRESS
|| options
[0] != CI_BSD_COMPRESS
336 || options
[1] != CILEN_BSD_COMPRESS
337 || BSD_VERSION(options
[2]) != BSD_CURRENT_VERSION
)
339 bits
= BSD_NBITS(options
[2]);
341 case 9: /* needs 82152 for both directions */
342 case 10: /* needs 84144 */
343 case 11: /* needs 88240 */
344 case 12: /* needs 96432 */
348 case 13: /* needs 176784 */
352 case 14: /* needs 353744 */
356 case 15: /* needs 691440 */
360 case 16: /* needs 1366160--far too much, */
361 /* hsize = 69001; */ /* and 69001 is too big for cptr */
362 /* hshift = 8; */ /* in struct bsd_db */
368 maxmaxcode
= MAXCODE(bits
);
369 newlen
= sizeof(*db
) + (hsize
-1) * (sizeof(db
->dict
[0]));
370 MALLOC(db
, struct bsd_db
*, newlen
, M_DEVBUF
, M_NOWAIT
);
373 bzero(db
, sizeof(*db
) - sizeof(db
->dict
));
378 MALLOC(db
->lens
, u_int16_t
*, (maxmaxcode
+1) * sizeof(db
->lens
[0]),
389 db
->maxmaxcode
= maxmaxcode
;
399 struct bsd_db
*db
= (struct bsd_db
*) state
;
402 FREE(db
->lens
, M_DEVBUF
);
407 bsd_comp_alloc(options
, opt_len
)
411 return bsd_alloc(options
, opt_len
, 0);
415 bsd_decomp_alloc(options
, opt_len
)
419 return bsd_alloc(options
, opt_len
, 1);
423 * Initialize the database.
426 bsd_init_comp_db(db
, options
, opt_len
, unit
, hdrlen
, mru
, debug
, decomp
)
429 int opt_len
, unit
, hdrlen
, mru
, debug
, decomp
;
433 if (opt_len
< CILEN_BSD_COMPRESS
|| options
[0] != CI_BSD_COMPRESS
434 || options
[1] != CILEN_BSD_COMPRESS
435 || BSD_VERSION(options
[2]) != BSD_CURRENT_VERSION
436 || BSD_NBITS(options
[2]) != db
->maxbits
437 || (decomp
&& db
->lens
== NULL
))
447 db
->dict
[--i
].codem1
= BADCODEM1
;
448 db
->dict
[i
].cptr
= 0;
465 bsd_comp_init(state
, options
, opt_len
, unit
, hdrlen
, debug
)
468 int opt_len
, unit
, hdrlen
, debug
;
470 return bsd_init_comp_db((struct bsd_db
*) state
, options
, opt_len
,
471 unit
, hdrlen
, 0, debug
, 0);
475 bsd_decomp_init(state
, options
, opt_len
, unit
, hdrlen
, mru
, debug
)
478 int opt_len
, unit
, hdrlen
, mru
, debug
;
480 return bsd_init_comp_db((struct bsd_db
*) state
, options
, opt_len
,
481 unit
, hdrlen
, mru
, debug
, 1);
487 * One change from the BSD compress command is that when the
488 * code size expands, we do not output a bunch of padding.
491 bsd_compress(state
, mret
, mp
, slen
, maxolen
)
493 struct mbuf
**mret
; /* return compressed mbuf chain here */
494 struct mbuf
*mp
; /* from here */
495 int slen
; /* uncompressed length */
496 int maxolen
; /* max compressed length */
498 struct bsd_db
*db
= (struct bsd_db
*) state
;
499 int hshift
= db
->hshift
;
500 u_int max_ent
= db
->max_ent
;
501 u_int n_bits
= db
->n_bits
;
503 u_int32_t accm
= 0, fcode
;
504 struct bsd_dict
*dictp
;
506 int hval
, disp
, ent
, ilen
;
512 #define PUTBYTE(v) { \
516 if (wptr >= cp_end) { \
517 m->m_len = wptr - mtod(m, u_char *); \
518 MGET(m->m_next, M_DONTWAIT, MT_DATA); \
522 if (maxolen - olen > MLEN) \
523 MCLGET(m, M_DONTWAIT); \
524 wptr = mtod(m, u_char *); \
525 cp_end = wptr + M_TRAILINGSPACE(m); \
532 #define OUTPUT(ent) { \
534 accm |= ((ent) << bitno); \
536 PUTBYTE(accm >> 24); \
539 } while (bitno <= 24); \
543 * If the protocol is not in the range we're interested in,
544 * just return without compressing the packet. If it is,
545 * the protocol becomes the first byte to compress.
547 rptr
= mtod(mp
, u_char
*);
548 ent
= PPP_PROTOCOL(rptr
);
549 if (ent
< 0x21 || ent
> 0xf9) {
554 /* Don't generate compressed packets which are larger than
555 the uncompressed packet. */
559 /* Allocate one mbuf to start with. */
560 MGET(m
, M_DONTWAIT
, MT_DATA
);
564 if (maxolen
+ db
->hdrlen
> MLEN
)
565 MCLGET(m
, M_DONTWAIT
);
566 m
->m_data
+= db
->hdrlen
;
567 wptr
= mtod(m
, u_char
*);
568 cp_end
= wptr
+ M_TRAILINGSPACE(m
);
570 wptr
= cp_end
= NULL
;
573 * Copy the PPP header over, changing the protocol,
574 * and install the 2-byte packet sequence number.
577 *wptr
++ = PPP_ADDRESS(rptr
); /* assumes the ppp header is */
578 *wptr
++ = PPP_CONTROL(rptr
); /* all in one mbuf */
579 *wptr
++ = 0; /* change the protocol */
581 *wptr
++ = db
->seqno
>> 8;
588 slen
= mp
->m_len
- PPP_HDRLEN
;
595 rptr
= mtod(mp
, u_char
*);
598 continue; /* handle 0-length buffers */
604 fcode
= BSD_KEY(ent
, c
);
605 hval
= BSD_HASH(ent
, c
, hshift
);
606 dictp
= &db
->dict
[hval
];
608 /* Validate and then check the entry. */
609 if (dictp
->codem1
>= max_ent
)
611 if (dictp
->f
.fcode
== fcode
) {
612 ent
= dictp
->codem1
+1;
613 continue; /* found (prefix,suffix) */
616 /* continue probing until a match or invalid entry */
617 disp
= (hval
== 0) ? 1 : hval
;
620 if (hval
>= db
->hsize
)
622 dictp
= &db
->dict
[hval
];
623 if (dictp
->codem1
>= max_ent
)
625 } while (dictp
->f
.fcode
!= fcode
);
626 ent
= dictp
->codem1
+ 1; /* finally found (prefix,suffix) */
630 OUTPUT(ent
); /* output the prefix */
632 /* code -> hashtable */
633 if (max_ent
< db
->maxmaxcode
) {
634 struct bsd_dict
*dictp2
;
635 /* expand code size if needed */
636 if (max_ent
>= MAXCODE(n_bits
))
637 db
->n_bits
= ++n_bits
;
639 /* Invalidate old hash table entry using
640 * this code, and then take it over.
642 dictp2
= &db
->dict
[max_ent
+1];
643 if (db
->dict
[dictp2
->cptr
].codem1
== max_ent
)
644 db
->dict
[dictp2
->cptr
].codem1
= BADCODEM1
;
646 dictp
->codem1
= max_ent
;
647 dictp
->f
.fcode
= fcode
;
649 db
->max_ent
= ++max_ent
;
654 OUTPUT(ent
); /* output the last code */
655 db
->bytes_out
+= olen
;
656 db
->in_count
+= ilen
;
658 ++db
->bytes_out
; /* count complete bytes */
661 OUTPUT(CLEAR
); /* do not count the CLEAR */
664 * Pad dribble bits of last code with ones.
665 * Do not emit a completely useless byte of ones.
668 PUTBYTE((accm
| (0xff << (bitno
-8))) >> 24);
671 m
->m_len
= wptr
- mtod(m
, u_char
*);
676 * Increase code size if we would have without the packet
677 * boundary and as the decompressor will.
679 if (max_ent
>= MAXCODE(n_bits
) && max_ent
< db
->maxmaxcode
)
682 db
->uncomp_bytes
+= ilen
;
684 if (olen
+ PPP_HDRLEN
+ BSD_OVHD
> maxolen
) {
685 /* throw away the compressed stuff if it is longer than uncompressed */
691 db
->incomp_bytes
+= ilen
;
694 db
->comp_bytes
+= olen
+ BSD_OVHD
;
697 return olen
+ PPP_HDRLEN
+ BSD_OVHD
;
704 * Update the "BSD Compress" dictionary on the receiver for
705 * incompressible data by pretending to compress the incoming data.
708 bsd_incomp(state
, dmsg
)
712 struct bsd_db
*db
= (struct bsd_db
*) state
;
713 u_int hshift
= db
->hshift
;
714 u_int max_ent
= db
->max_ent
;
715 u_int n_bits
= db
->n_bits
;
716 struct bsd_dict
*dictp
;
719 u_int32_t hval
, disp
;
726 * If the protocol is not in the range we're interested in,
727 * just return without looking at the packet. If it is,
728 * the protocol becomes the first byte to "compress".
730 rptr
= mtod(dmsg
, u_char
*);
731 ent
= PPP_PROTOCOL(rptr
);
732 if (ent
< 0x21 || ent
> 0xf9)
736 ilen
= 1; /* count the protocol as 1 byte */
738 slen
= dmsg
->m_len
- PPP_HDRLEN
;
744 rptr
= mtod(dmsg
, u_char
*);
752 fcode
= BSD_KEY(ent
, c
);
753 hval
= BSD_HASH(ent
, c
, hshift
);
754 dictp
= &db
->dict
[hval
];
756 /* validate and then check the entry */
757 if (dictp
->codem1
>= max_ent
)
759 if (dictp
->f
.fcode
== fcode
) {
760 ent
= dictp
->codem1
+1;
761 continue; /* found (prefix,suffix) */
764 /* continue probing until a match or invalid entry */
765 disp
= (hval
== 0) ? 1 : hval
;
768 if (hval
>= db
->hsize
)
770 dictp
= &db
->dict
[hval
];
771 if (dictp
->codem1
>= max_ent
)
773 } while (dictp
->f
.fcode
!= fcode
);
774 ent
= dictp
->codem1
+1;
775 continue; /* finally found (prefix,suffix) */
777 nomatch
: /* output (count) the prefix */
780 /* code -> hashtable */
781 if (max_ent
< db
->maxmaxcode
) {
782 struct bsd_dict
*dictp2
;
783 /* expand code size if needed */
784 if (max_ent
>= MAXCODE(n_bits
))
785 db
->n_bits
= ++n_bits
;
787 /* Invalidate previous hash table entry
788 * assigned this code, and then take it over.
790 dictp2
= &db
->dict
[max_ent
+1];
791 if (db
->dict
[dictp2
->cptr
].codem1
== max_ent
)
792 db
->dict
[dictp2
->cptr
].codem1
= BADCODEM1
;
794 dictp
->codem1
= max_ent
;
795 dictp
->f
.fcode
= fcode
;
797 db
->max_ent
= ++max_ent
;
798 db
->lens
[max_ent
] = db
->lens
[ent
]+1;
801 } while (--slen
!= 0);
803 bitno
+= n_bits
; /* output (count) the last code */
804 db
->bytes_out
+= bitno
/8;
805 db
->in_count
+= ilen
;
809 db
->incomp_bytes
+= ilen
;
811 db
->uncomp_bytes
+= ilen
;
813 /* Increase code size if we would have without the packet
814 * boundary and as the decompressor will.
816 if (max_ent
>= MAXCODE(n_bits
) && max_ent
< db
->maxmaxcode
)
822 * Decompress "BSD Compress".
824 * Because of patent problems, we return DECOMP_ERROR for errors
825 * found by inspecting the input data and for system problems, but
826 * DECOMP_FATALERROR for any errors which could possibly be said to
827 * be being detected "after" decompression. For DECOMP_ERROR,
828 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
829 * infringing a patent of Motorola's if we do, so we take CCP down
832 * Given that the frame has the correct sequence number and a good FCS,
833 * errors such as invalid codes in the input most likely indicate a
834 * bug, so we return DECOMP_FATALERROR for them in order to turn off
835 * compression, even though they are detected by inspecting the input.
838 bsd_decompress(state
, cmp
, dmpp
)
840 struct mbuf
*cmp
, **dmpp
;
842 struct bsd_db
*db
= (struct bsd_db
*) state
;
843 u_int max_ent
= db
->max_ent
;
845 u_int bitno
= 32; /* 1st valid bit in accm */
846 u_int n_bits
= db
->n_bits
;
847 u_int tgtbitno
= 32-n_bits
; /* bitno when we have a code */
848 struct bsd_dict
*dictp
;
849 int explen
, i
, seq
, len
;
850 u_int incode
, oldcode
, finchar
;
851 u_char
*p
, *rptr
, *wptr
;
852 struct mbuf
*m
, *dmp
, *mret
;
853 int adrs
, ctrl
, ilen
;
854 int space
, codelen
, extra
;
857 * Save the address/control from the PPP header
858 * and then get the sequence number.
861 rptr
= mtod(cmp
, u_char
*);
862 adrs
= PPP_ADDRESS(rptr
);
863 ctrl
= PPP_CONTROL(rptr
);
865 len
= cmp
->m_len
- PPP_HDRLEN
;
867 for (i
= 0; i
< 2; ++i
) {
872 rptr
= mtod(cmp
, u_char
*);
875 seq
= (seq
<< 8) + *rptr
++;
880 * Check the sequence number and give up if it differs from
881 * the value we're expecting.
883 if (seq
!= db
->seqno
) {
885 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
886 db
->unit
, seq
, db
->seqno
- 1);
892 * Allocate one mbuf to start with.
894 MGETHDR(dmp
, M_DONTWAIT
, MT_DATA
);
900 MCLGET(dmp
, M_DONTWAIT
);
901 dmp
->m_data
+= db
->hdrlen
;
902 wptr
= mtod(dmp
, u_char
*);
903 space
= M_TRAILINGSPACE(dmp
) - PPP_HDRLEN
+ 1;
906 * Fill in the ppp header, but not the last byte of the protocol
907 * (that comes from the decompressed data).
912 wptr
+= PPP_HDRLEN
- 1;
920 if (!cmp
) /* quit at end of message */
922 rptr
= mtod(cmp
, u_char
*);
925 continue; /* handle 0-length buffers */
929 * Accumulate bytes until we have a complete code.
930 * Then get the next code, relying on the 32-bit,
931 * unsigned accm to mask the result.
934 accm
|= *rptr
++ << bitno
;
936 if (tgtbitno
< bitno
)
938 incode
= accm
>> tgtbitno
;
942 if (incode
== CLEAR
) {
944 * The dictionary must only be cleared at
945 * the end of a packet. But there could be an
946 * empty mbuf at the end.
948 if (len
> 0 || cmp
->m_next
!= NULL
) {
949 while ((cmp
= cmp
->m_next
) != NULL
)
954 printf("bsd_decomp%d: bad CLEAR\n", db
->unit
);
955 return DECOMP_FATALERROR
; /* probably a bug */
963 if (incode
> max_ent
+ 2 || incode
> db
->maxmaxcode
964 || (incode
> max_ent
&& oldcode
== CLEAR
)) {
967 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
968 db
->unit
, incode
, oldcode
);
969 printf("max_ent=0x%x explen=%d seqno=%d\n",
970 max_ent
, explen
, db
->seqno
);
972 return DECOMP_FATALERROR
; /* probably a bug */
975 /* Special case for KwKwK string. */
976 if (incode
> max_ent
) {
984 codelen
= db
->lens
[finchar
];
985 explen
+= codelen
+ extra
;
986 if (explen
> db
->mru
+ 1) {
989 printf("bsd_decomp%d: ran out of mru\n", db
->unit
);
991 while ((cmp
= cmp
->m_next
) != NULL
)
993 printf(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
994 len
, finchar
, codelen
, explen
);
997 return DECOMP_FATALERROR
;
1001 * For simplicity, the decoded characters go in a single mbuf,
1002 * so we allocate a single extra cluster mbuf if necessary.
1004 if ((space
-= codelen
+ extra
) < 0) {
1005 dmp
->m_len
= wptr
- mtod(dmp
, u_char
*);
1006 MGET(m
, M_DONTWAIT
, MT_DATA
);
1009 return DECOMP_ERROR
;
1014 MCLGET(m
, M_DONTWAIT
);
1015 space
= M_TRAILINGSPACE(m
) - (codelen
+ extra
);
1017 /* now that's what I call *compression*. */
1019 return DECOMP_ERROR
;
1022 wptr
= mtod(dmp
, u_char
*);
1026 * Decode this code and install it in the decompressed buffer.
1028 p
= (wptr
+= codelen
);
1029 while (finchar
> LAST
) {
1030 dictp
= &db
->dict
[db
->dict
[finchar
].cptr
];
1032 if (--codelen
<= 0 || dictp
->codem1
!= finchar
-1)
1035 *--p
= dictp
->f
.hs
.suffix
;
1036 finchar
= dictp
->f
.hs
.prefix
;
1042 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1043 db
->unit
, codelen
, incode
, max_ent
);
1046 if (extra
) /* the KwKwK case again */
1050 * If not first code in a packet, and
1051 * if not out of code space, then allocate a new code.
1053 * Keep the hash table correct so it can be used
1054 * with uncompressed packets.
1056 if (oldcode
!= CLEAR
&& max_ent
< db
->maxmaxcode
) {
1057 struct bsd_dict
*dictp2
;
1059 u_int32_t hval
, disp
;
1061 fcode
= BSD_KEY(oldcode
,finchar
);
1062 hval
= BSD_HASH(oldcode
,finchar
,db
->hshift
);
1063 dictp
= &db
->dict
[hval
];
1065 /* look for a free hash table entry */
1066 if (dictp
->codem1
< max_ent
) {
1067 disp
= (hval
== 0) ? 1 : hval
;
1070 if (hval
>= db
->hsize
)
1072 dictp
= &db
->dict
[hval
];
1073 } while (dictp
->codem1
< max_ent
);
1077 * Invalidate previous hash table entry
1078 * assigned this code, and then take it over
1080 dictp2
= &db
->dict
[max_ent
+1];
1081 if (db
->dict
[dictp2
->cptr
].codem1
== max_ent
) {
1082 db
->dict
[dictp2
->cptr
].codem1
= BADCODEM1
;
1084 dictp2
->cptr
= hval
;
1085 dictp
->codem1
= max_ent
;
1086 dictp
->f
.fcode
= fcode
;
1088 db
->max_ent
= ++max_ent
;
1089 db
->lens
[max_ent
] = db
->lens
[oldcode
]+1;
1091 /* Expand code size if needed. */
1092 if (max_ent
>= MAXCODE(n_bits
) && max_ent
< db
->maxmaxcode
) {
1093 db
->n_bits
= ++n_bits
;
1094 tgtbitno
= 32-n_bits
;
1099 dmp
->m_len
= wptr
- mtod(dmp
, u_char
*);
1102 * Keep the checkpoint right so that incompressible packets
1103 * clear the dictionary at the right times.
1105 db
->bytes_out
+= ilen
;
1106 db
->in_count
+= explen
;
1107 if (bsd_check(db
) && db
->debug
) {
1108 printf("bsd_decomp%d: peer should have cleared dictionary\n",
1113 db
->comp_bytes
+= ilen
+ BSD_OVHD
;
1115 db
->uncomp_bytes
+= explen
;
1123 printf("bsd_decomp%d: fell off end of chain ", db
->unit
);
1124 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1125 incode
, finchar
, db
->dict
[finchar
].cptr
, max_ent
);
1126 } else if (dictp
->codem1
!= finchar
-1) {
1127 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1128 db
->unit
, incode
, finchar
);
1129 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode
,
1130 db
->dict
[finchar
].cptr
, dictp
->codem1
);
1133 return DECOMP_FATALERROR
;
1136 #endif /* DO_BSD_COMPRESS */