2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Because this code is derived from the 4.3BSD compress source:
26 * Copyright (c) 1985, 1986 The Regents of the University of California.
27 * All rights reserved.
29 * This code is derived from software contributed to Berkeley by
30 * James A. Woods, derived from original work by Spencer Thomas
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * This version is for use with mbufs on BSD-derived systems.
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/malloc.h>
71 #include <net/ppp_defs.h>
73 #define PACKETPTR struct mbuf *
74 #include <net/ppp_comp.h>
78 * PPP "BSD compress" compression
79 * The differences between this compression and the classic BSD LZW
80 * source are obvious from the requirement that the classic code worked
81 * with files while this handles arbitrarily long streams that
82 * are broken into packets. They are:
84 * When the code size expands, a block of junk is not emitted by
85 * the compressor and not expected by the decompressor.
87 * New codes are not necessarily assigned every time an old
88 * code is output by the compressor. This is because a packet
89 * end forces a code to be emitted, but does not imply that a
90 * new sequence has been seen.
92 * The compression ratio is checked at the first end of a packet
93 * after the appropriate gap. Besides simplifying and speeding
94 * things up, this makes it more likely that the transmitter
95 * and receiver will agree when the dictionary is cleared when
96 * compression is not going well.
100 * A dictionary for doing BSD compress.
103 int totlen
; /* length of this structure */
104 u_int hsize
; /* size of the hash table */
105 u_char hshift
; /* used in hash function */
106 u_char n_bits
; /* current bits/code */
110 u_int16_t seqno
; /* sequence # of next packet */
111 u_int hdrlen
; /* header length to preallocate */
113 u_int maxmaxcode
; /* largest valid code */
114 u_int max_ent
; /* largest code in use */
115 u_int in_count
; /* uncompressed bytes, aged */
116 u_int bytes_out
; /* compressed bytes, aged */
117 u_int ratio
; /* recent compression ratio */
118 u_int checkpoint
; /* when to next check the ratio */
119 u_int clear_count
; /* times dictionary cleared */
120 u_int incomp_count
; /* incompressible packets */
121 u_int incomp_bytes
; /* incompressible bytes */
122 u_int uncomp_count
; /* uncompressed packets */
123 u_int uncomp_bytes
; /* uncompressed bytes */
124 u_int comp_count
; /* compressed packets */
125 u_int comp_bytes
; /* compressed bytes */
126 u_int16_t
*lens
; /* array of lengths of codes */
128 union { /* hash value */
131 #if BYTE_ORDER == LITTLE_ENDIAN
132 u_int16_t prefix
; /* preceding code */
133 u_char suffix
; /* last character of new code */
137 u_char suffix
; /* last character of new code */
138 u_int16_t prefix
; /* preceding code */
142 u_int16_t codem1
; /* output of hash table -1 */
143 u_int16_t cptr
; /* map code to hash table entry */
147 #define BSD_OVHD 2 /* BSD compress overhead/packet */
148 #define BSD_INIT_BITS BSD_MIN_BITS
150 static void bsd_clear(struct bsd_db
*db
);
151 static int bsd_check(struct bsd_db
*db
);
152 static void *bsd_alloc(u_char
*options
, int opt_len
, int decomp
);
153 static int bsd_init_comp_db(struct bsd_db
*db
, u_char
*options
,
155 int unit
, int hdrlen
, int mru
, int debug
,
157 static void *bsd_comp_alloc(u_char
*options
, int opt_len
);
158 static void *bsd_decomp_alloc(u_char
*options
, int opt_len
);
159 static void bsd_free(void *state
);
160 static int bsd_comp_init(void *state
, u_char
*options
, int opt_len
,
161 int unit
, int hdrlen
, int debug
);
162 static int bsd_decomp_init(void *state
, u_char
*options
, int opt_len
,
163 int unit
, int hdrlen
, int mru
, int debug
);
164 static int bsd_compress(void *state
, struct mbuf
**mret
,
165 struct mbuf
*mp
, int slen
, int maxolen
);
166 static void bsd_incomp(void *state
, struct mbuf
*dmsg
);
167 static int bsd_decompress(void *state
, struct mbuf
*cmp
,
169 static void bsd_reset(void *state
);
170 static void bsd_comp_stats(void *state
, struct compstat
*stats
);
173 * Procedures exported to if_ppp.c.
175 struct compressor ppp_bsd_compress
= {
176 CI_BSD_COMPRESS
, /* compress_proto */
177 bsd_comp_alloc
, /* comp_alloc */
178 bsd_free
, /* comp_free */
179 bsd_comp_init
, /* comp_init */
180 bsd_reset
, /* comp_reset */
181 bsd_compress
, /* compress */
182 bsd_comp_stats
, /* comp_stat */
183 bsd_decomp_alloc
, /* decomp_alloc */
184 bsd_free
, /* decomp_free */
185 bsd_decomp_init
, /* decomp_init */
186 bsd_reset
, /* decomp_reset */
187 bsd_decompress
, /* decompress */
188 bsd_incomp
, /* incomp */
189 bsd_comp_stats
, /* decomp_stat */
193 * the next two codes should not be changed lightly, as they must not
194 * lie within the contiguous general code space.
196 #define CLEAR 256 /* table clear output code */
197 #define FIRST 257 /* first free entry */
200 #define MAXCODE(b) ((1 << (b)) - 1)
201 #define BADCODEM1 MAXCODE(BSD_MAX_BITS)
203 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
204 ^ (u_int32_t)(prefix))
205 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
206 + (u_int32_t)(prefix))
208 #define CHECK_GAP 10000 /* Ratio check interval */
210 #define RATIO_SCALE_LOG 8
211 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
212 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
215 * clear the dictionary
222 db
->max_ent
= FIRST
-1;
223 db
->n_bits
= BSD_INIT_BITS
;
227 db
->checkpoint
= CHECK_GAP
;
231 * If the dictionary is full, then see if it is time to reset it.
233 * Compute the compression ratio using fixed-point arithmetic
234 * with 8 fractional bits.
236 * Since we have an infinite stream instead of a single file,
237 * watch only the local compression ratio.
239 * Since both peers must reset the dictionary at the same time even in
240 * the absence of CLEAR codes (while packets are incompressible), they
241 * must compute the same ratio.
243 static int /* 1=output CLEAR */
249 if (db
->in_count
>= db
->checkpoint
) {
250 /* age the ratio by limiting the size of the counts */
251 if (db
->in_count
>= RATIO_MAX
252 || db
->bytes_out
>= RATIO_MAX
) {
253 db
->in_count
-= db
->in_count
/4;
254 db
->bytes_out
-= db
->bytes_out
/4;
257 db
->checkpoint
= db
->in_count
+ CHECK_GAP
;
259 if (db
->max_ent
>= db
->maxmaxcode
) {
260 /* Reset the dictionary only if the ratio is worse,
261 * or if it looks as if it has been poisoned
262 * by incompressible data.
264 * This does not overflow, because
265 * db->in_count <= RATIO_MAX.
267 new_ratio
= db
->in_count
<< RATIO_SCALE_LOG
;
268 if (db
->bytes_out
!= 0)
269 new_ratio
/= db
->bytes_out
;
271 if (new_ratio
< db
->ratio
|| new_ratio
< 1 * RATIO_SCALE
) {
275 db
->ratio
= new_ratio
;
285 bsd_comp_stats(state
, stats
)
287 struct compstat
*stats
;
289 struct bsd_db
*db
= (struct bsd_db
*) state
;
292 stats
->unc_bytes
= db
->uncomp_bytes
;
293 stats
->unc_packets
= db
->uncomp_count
;
294 stats
->comp_bytes
= db
->comp_bytes
;
295 stats
->comp_packets
= db
->comp_count
;
296 stats
->inc_bytes
= db
->incomp_bytes
;
297 stats
->inc_packets
= db
->incomp_count
;
298 stats
->ratio
= db
->in_count
;
300 if (stats
->ratio
<= 0x7fffff)
309 * Reset state, as on a CCP ResetReq.
315 struct bsd_db
*db
= (struct bsd_db
*) state
;
323 * Allocate space for a (de) compressor.
326 bsd_alloc(options
, opt_len
, decomp
)
331 u_int newlen
, hsize
, hshift
, maxmaxcode
;
334 if (opt_len
< CILEN_BSD_COMPRESS
|| options
[0] != CI_BSD_COMPRESS
335 || options
[1] != CILEN_BSD_COMPRESS
336 || BSD_VERSION(options
[2]) != BSD_CURRENT_VERSION
)
338 bits
= BSD_NBITS(options
[2]);
340 case 9: /* needs 82152 for both directions */
341 case 10: /* needs 84144 */
342 case 11: /* needs 88240 */
343 case 12: /* needs 96432 */
347 case 13: /* needs 176784 */
351 case 14: /* needs 353744 */
355 case 15: /* needs 691440 */
359 case 16: /* needs 1366160--far too much, */
360 /* hsize = 69001; */ /* and 69001 is too big for cptr */
361 /* hshift = 8; */ /* in struct bsd_db */
367 maxmaxcode
= MAXCODE(bits
);
368 newlen
= sizeof(*db
) + (hsize
-1) * (sizeof(db
->dict
[0]));
369 MALLOC(db
, struct bsd_db
*, newlen
, M_DEVBUF
, M_NOWAIT
);
372 bzero(db
, sizeof(*db
) - sizeof(db
->dict
));
377 MALLOC(db
->lens
, u_int16_t
*, (maxmaxcode
+1) * sizeof(db
->lens
[0]),
388 db
->maxmaxcode
= maxmaxcode
;
398 struct bsd_db
*db
= (struct bsd_db
*) state
;
401 FREE(db
->lens
, M_DEVBUF
);
406 bsd_comp_alloc(options
, opt_len
)
410 return bsd_alloc(options
, opt_len
, 0);
414 bsd_decomp_alloc(options
, opt_len
)
418 return bsd_alloc(options
, opt_len
, 1);
422 * Initialize the database.
425 bsd_init_comp_db(db
, options
, opt_len
, unit
, hdrlen
, mru
, debug
, decomp
)
428 int opt_len
, unit
, hdrlen
, mru
, debug
, decomp
;
432 if (opt_len
< CILEN_BSD_COMPRESS
|| options
[0] != CI_BSD_COMPRESS
433 || options
[1] != CILEN_BSD_COMPRESS
434 || BSD_VERSION(options
[2]) != BSD_CURRENT_VERSION
435 || BSD_NBITS(options
[2]) != db
->maxbits
436 || (decomp
&& db
->lens
== NULL
))
446 db
->dict
[--i
].codem1
= BADCODEM1
;
447 db
->dict
[i
].cptr
= 0;
464 bsd_comp_init(state
, options
, opt_len
, unit
, hdrlen
, debug
)
467 int opt_len
, unit
, hdrlen
, debug
;
469 return bsd_init_comp_db((struct bsd_db
*) state
, options
, opt_len
,
470 unit
, hdrlen
, 0, debug
, 0);
474 bsd_decomp_init(state
, options
, opt_len
, unit
, hdrlen
, mru
, debug
)
477 int opt_len
, unit
, hdrlen
, mru
, debug
;
479 return bsd_init_comp_db((struct bsd_db
*) state
, options
, opt_len
,
480 unit
, hdrlen
, mru
, debug
, 1);
486 * One change from the BSD compress command is that when the
487 * code size expands, we do not output a bunch of padding.
490 bsd_compress(state
, mret
, mp
, slen
, maxolen
)
492 struct mbuf
**mret
; /* return compressed mbuf chain here */
493 struct mbuf
*mp
; /* from here */
494 int slen
; /* uncompressed length */
495 int maxolen
; /* max compressed length */
497 struct bsd_db
*db
= (struct bsd_db
*) state
;
498 int hshift
= db
->hshift
;
499 u_int max_ent
= db
->max_ent
;
500 u_int n_bits
= db
->n_bits
;
502 u_int32_t accm
= 0, fcode
;
503 struct bsd_dict
*dictp
;
505 int hval
, disp
, ent
, ilen
;
511 #define PUTBYTE(v) { \
515 if (wptr >= cp_end) { \
516 m->m_len = wptr - mtod(m, u_char *); \
517 MGET(m->m_next, M_DONTWAIT, MT_DATA); \
521 if (maxolen - olen > MLEN) \
522 MCLGET(m, M_DONTWAIT); \
523 wptr = mtod(m, u_char *); \
524 cp_end = wptr + M_TRAILINGSPACE(m); \
531 #define OUTPUT(ent) { \
533 accm |= ((ent) << bitno); \
535 PUTBYTE(accm >> 24); \
538 } while (bitno <= 24); \
542 * If the protocol is not in the range we're interested in,
543 * just return without compressing the packet. If it is,
544 * the protocol becomes the first byte to compress.
546 rptr
= mtod(mp
, u_char
*);
547 ent
= PPP_PROTOCOL(rptr
);
548 if (ent
< 0x21 || ent
> 0xf9) {
553 /* Don't generate compressed packets which are larger than
554 the uncompressed packet. */
558 /* Allocate one mbuf to start with. */
559 MGET(m
, M_DONTWAIT
, MT_DATA
);
563 if (maxolen
+ db
->hdrlen
> MLEN
)
564 MCLGET(m
, M_DONTWAIT
);
565 m
->m_data
+= db
->hdrlen
;
566 wptr
= mtod(m
, u_char
*);
567 cp_end
= wptr
+ M_TRAILINGSPACE(m
);
569 wptr
= cp_end
= NULL
;
572 * Copy the PPP header over, changing the protocol,
573 * and install the 2-byte packet sequence number.
576 *wptr
++ = PPP_ADDRESS(rptr
); /* assumes the ppp header is */
577 *wptr
++ = PPP_CONTROL(rptr
); /* all in one mbuf */
578 *wptr
++ = 0; /* change the protocol */
580 *wptr
++ = db
->seqno
>> 8;
587 slen
= mp
->m_len
- PPP_HDRLEN
;
594 rptr
= mtod(mp
, u_char
*);
597 continue; /* handle 0-length buffers */
603 fcode
= BSD_KEY(ent
, c
);
604 hval
= BSD_HASH(ent
, c
, hshift
);
605 dictp
= &db
->dict
[hval
];
607 /* Validate and then check the entry. */
608 if (dictp
->codem1
>= max_ent
)
610 if (dictp
->f
.fcode
== fcode
) {
611 ent
= dictp
->codem1
+1;
612 continue; /* found (prefix,suffix) */
615 /* continue probing until a match or invalid entry */
616 disp
= (hval
== 0) ? 1 : hval
;
619 if (hval
>= db
->hsize
)
621 dictp
= &db
->dict
[hval
];
622 if (dictp
->codem1
>= max_ent
)
624 } while (dictp
->f
.fcode
!= fcode
);
625 ent
= dictp
->codem1
+ 1; /* finally found (prefix,suffix) */
629 OUTPUT(ent
); /* output the prefix */
631 /* code -> hashtable */
632 if (max_ent
< db
->maxmaxcode
) {
633 struct bsd_dict
*dictp2
;
634 /* expand code size if needed */
635 if (max_ent
>= MAXCODE(n_bits
))
636 db
->n_bits
= ++n_bits
;
638 /* Invalidate old hash table entry using
639 * this code, and then take it over.
641 dictp2
= &db
->dict
[max_ent
+1];
642 if (db
->dict
[dictp2
->cptr
].codem1
== max_ent
)
643 db
->dict
[dictp2
->cptr
].codem1
= BADCODEM1
;
645 dictp
->codem1
= max_ent
;
646 dictp
->f
.fcode
= fcode
;
648 db
->max_ent
= ++max_ent
;
653 OUTPUT(ent
); /* output the last code */
654 db
->bytes_out
+= olen
;
655 db
->in_count
+= ilen
;
657 ++db
->bytes_out
; /* count complete bytes */
660 OUTPUT(CLEAR
); /* do not count the CLEAR */
663 * Pad dribble bits of last code with ones.
664 * Do not emit a completely useless byte of ones.
667 PUTBYTE((accm
| (0xff << (bitno
-8))) >> 24);
670 m
->m_len
= wptr
- mtod(m
, u_char
*);
675 * Increase code size if we would have without the packet
676 * boundary and as the decompressor will.
678 if (max_ent
>= MAXCODE(n_bits
) && max_ent
< db
->maxmaxcode
)
681 db
->uncomp_bytes
+= ilen
;
683 if (olen
+ PPP_HDRLEN
+ BSD_OVHD
> maxolen
) {
684 /* throw away the compressed stuff if it is longer than uncompressed */
690 db
->incomp_bytes
+= ilen
;
693 db
->comp_bytes
+= olen
+ BSD_OVHD
;
696 return olen
+ PPP_HDRLEN
+ BSD_OVHD
;
703 * Update the "BSD Compress" dictionary on the receiver for
704 * incompressible data by pretending to compress the incoming data.
707 bsd_incomp(state
, dmsg
)
711 struct bsd_db
*db
= (struct bsd_db
*) state
;
712 u_int hshift
= db
->hshift
;
713 u_int max_ent
= db
->max_ent
;
714 u_int n_bits
= db
->n_bits
;
715 struct bsd_dict
*dictp
;
718 u_int32_t hval
, disp
;
725 * If the protocol is not in the range we're interested in,
726 * just return without looking at the packet. If it is,
727 * the protocol becomes the first byte to "compress".
729 rptr
= mtod(dmsg
, u_char
*);
730 ent
= PPP_PROTOCOL(rptr
);
731 if (ent
< 0x21 || ent
> 0xf9)
735 ilen
= 1; /* count the protocol as 1 byte */
737 slen
= dmsg
->m_len
- PPP_HDRLEN
;
743 rptr
= mtod(dmsg
, u_char
*);
751 fcode
= BSD_KEY(ent
, c
);
752 hval
= BSD_HASH(ent
, c
, hshift
);
753 dictp
= &db
->dict
[hval
];
755 /* validate and then check the entry */
756 if (dictp
->codem1
>= max_ent
)
758 if (dictp
->f
.fcode
== fcode
) {
759 ent
= dictp
->codem1
+1;
760 continue; /* found (prefix,suffix) */
763 /* continue probing until a match or invalid entry */
764 disp
= (hval
== 0) ? 1 : hval
;
767 if (hval
>= db
->hsize
)
769 dictp
= &db
->dict
[hval
];
770 if (dictp
->codem1
>= max_ent
)
772 } while (dictp
->f
.fcode
!= fcode
);
773 ent
= dictp
->codem1
+1;
774 continue; /* finally found (prefix,suffix) */
776 nomatch
: /* output (count) the prefix */
779 /* code -> hashtable */
780 if (max_ent
< db
->maxmaxcode
) {
781 struct bsd_dict
*dictp2
;
782 /* expand code size if needed */
783 if (max_ent
>= MAXCODE(n_bits
))
784 db
->n_bits
= ++n_bits
;
786 /* Invalidate previous hash table entry
787 * assigned this code, and then take it over.
789 dictp2
= &db
->dict
[max_ent
+1];
790 if (db
->dict
[dictp2
->cptr
].codem1
== max_ent
)
791 db
->dict
[dictp2
->cptr
].codem1
= BADCODEM1
;
793 dictp
->codem1
= max_ent
;
794 dictp
->f
.fcode
= fcode
;
796 db
->max_ent
= ++max_ent
;
797 db
->lens
[max_ent
] = db
->lens
[ent
]+1;
800 } while (--slen
!= 0);
802 bitno
+= n_bits
; /* output (count) the last code */
803 db
->bytes_out
+= bitno
/8;
804 db
->in_count
+= ilen
;
808 db
->incomp_bytes
+= ilen
;
810 db
->uncomp_bytes
+= ilen
;
812 /* Increase code size if we would have without the packet
813 * boundary and as the decompressor will.
815 if (max_ent
>= MAXCODE(n_bits
) && max_ent
< db
->maxmaxcode
)
821 * Decompress "BSD Compress".
823 * Because of patent problems, we return DECOMP_ERROR for errors
824 * found by inspecting the input data and for system problems, but
825 * DECOMP_FATALERROR for any errors which could possibly be said to
826 * be being detected "after" decompression. For DECOMP_ERROR,
827 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
828 * infringing a patent of Motorola's if we do, so we take CCP down
831 * Given that the frame has the correct sequence number and a good FCS,
832 * errors such as invalid codes in the input most likely indicate a
833 * bug, so we return DECOMP_FATALERROR for them in order to turn off
834 * compression, even though they are detected by inspecting the input.
837 bsd_decompress(state
, cmp
, dmpp
)
839 struct mbuf
*cmp
, **dmpp
;
841 struct bsd_db
*db
= (struct bsd_db
*) state
;
842 u_int max_ent
= db
->max_ent
;
844 u_int bitno
= 32; /* 1st valid bit in accm */
845 u_int n_bits
= db
->n_bits
;
846 u_int tgtbitno
= 32-n_bits
; /* bitno when we have a code */
847 struct bsd_dict
*dictp
;
848 int explen
, i
, seq
, len
;
849 u_int incode
, oldcode
, finchar
;
850 u_char
*p
, *rptr
, *wptr
;
851 struct mbuf
*m
, *dmp
, *mret
;
852 int adrs
, ctrl
, ilen
;
853 int space
, codelen
, extra
;
856 * Save the address/control from the PPP header
857 * and then get the sequence number.
860 rptr
= mtod(cmp
, u_char
*);
861 adrs
= PPP_ADDRESS(rptr
);
862 ctrl
= PPP_CONTROL(rptr
);
864 len
= cmp
->m_len
- PPP_HDRLEN
;
866 for (i
= 0; i
< 2; ++i
) {
871 rptr
= mtod(cmp
, u_char
*);
874 seq
= (seq
<< 8) + *rptr
++;
879 * Check the sequence number and give up if it differs from
880 * the value we're expecting.
882 if (seq
!= db
->seqno
) {
884 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
885 db
->unit
, seq
, db
->seqno
- 1);
891 * Allocate one mbuf to start with.
893 MGETHDR(dmp
, M_DONTWAIT
, MT_DATA
);
899 MCLGET(dmp
, M_DONTWAIT
);
900 dmp
->m_data
+= db
->hdrlen
;
901 wptr
= mtod(dmp
, u_char
*);
902 space
= M_TRAILINGSPACE(dmp
) - PPP_HDRLEN
+ 1;
905 * Fill in the ppp header, but not the last byte of the protocol
906 * (that comes from the decompressed data).
911 wptr
+= PPP_HDRLEN
- 1;
919 if (!cmp
) /* quit at end of message */
921 rptr
= mtod(cmp
, u_char
*);
924 continue; /* handle 0-length buffers */
928 * Accumulate bytes until we have a complete code.
929 * Then get the next code, relying on the 32-bit,
930 * unsigned accm to mask the result.
933 accm
|= *rptr
++ << bitno
;
935 if (tgtbitno
< bitno
)
937 incode
= accm
>> tgtbitno
;
941 if (incode
== CLEAR
) {
943 * The dictionary must only be cleared at
944 * the end of a packet. But there could be an
945 * empty mbuf at the end.
947 if (len
> 0 || cmp
->m_next
!= NULL
) {
948 while ((cmp
= cmp
->m_next
) != NULL
)
953 printf("bsd_decomp%d: bad CLEAR\n", db
->unit
);
954 return DECOMP_FATALERROR
; /* probably a bug */
962 if (incode
> max_ent
+ 2 || incode
> db
->maxmaxcode
963 || (incode
> max_ent
&& oldcode
== CLEAR
)) {
966 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
967 db
->unit
, incode
, oldcode
);
968 printf("max_ent=0x%x explen=%d seqno=%d\n",
969 max_ent
, explen
, db
->seqno
);
971 return DECOMP_FATALERROR
; /* probably a bug */
974 /* Special case for KwKwK string. */
975 if (incode
> max_ent
) {
983 codelen
= db
->lens
[finchar
];
984 explen
+= codelen
+ extra
;
985 if (explen
> db
->mru
+ 1) {
988 printf("bsd_decomp%d: ran out of mru\n", db
->unit
);
990 while ((cmp
= cmp
->m_next
) != NULL
)
992 printf(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
993 len
, finchar
, codelen
, explen
);
996 return DECOMP_FATALERROR
;
1000 * For simplicity, the decoded characters go in a single mbuf,
1001 * so we allocate a single extra cluster mbuf if necessary.
1003 if ((space
-= codelen
+ extra
) < 0) {
1004 dmp
->m_len
= wptr
- mtod(dmp
, u_char
*);
1005 MGET(m
, M_DONTWAIT
, MT_DATA
);
1008 return DECOMP_ERROR
;
1013 MCLGET(m
, M_DONTWAIT
);
1014 space
= M_TRAILINGSPACE(m
) - (codelen
+ extra
);
1016 /* now that's what I call *compression*. */
1018 return DECOMP_ERROR
;
1021 wptr
= mtod(dmp
, u_char
*);
1025 * Decode this code and install it in the decompressed buffer.
1027 p
= (wptr
+= codelen
);
1028 while (finchar
> LAST
) {
1029 dictp
= &db
->dict
[db
->dict
[finchar
].cptr
];
1031 if (--codelen
<= 0 || dictp
->codem1
!= finchar
-1)
1034 *--p
= dictp
->f
.hs
.suffix
;
1035 finchar
= dictp
->f
.hs
.prefix
;
1041 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1042 db
->unit
, codelen
, incode
, max_ent
);
1045 if (extra
) /* the KwKwK case again */
1049 * If not first code in a packet, and
1050 * if not out of code space, then allocate a new code.
1052 * Keep the hash table correct so it can be used
1053 * with uncompressed packets.
1055 if (oldcode
!= CLEAR
&& max_ent
< db
->maxmaxcode
) {
1056 struct bsd_dict
*dictp2
;
1058 u_int32_t hval
, disp
;
1060 fcode
= BSD_KEY(oldcode
,finchar
);
1061 hval
= BSD_HASH(oldcode
,finchar
,db
->hshift
);
1062 dictp
= &db
->dict
[hval
];
1064 /* look for a free hash table entry */
1065 if (dictp
->codem1
< max_ent
) {
1066 disp
= (hval
== 0) ? 1 : hval
;
1069 if (hval
>= db
->hsize
)
1071 dictp
= &db
->dict
[hval
];
1072 } while (dictp
->codem1
< max_ent
);
1076 * Invalidate previous hash table entry
1077 * assigned this code, and then take it over
1079 dictp2
= &db
->dict
[max_ent
+1];
1080 if (db
->dict
[dictp2
->cptr
].codem1
== max_ent
) {
1081 db
->dict
[dictp2
->cptr
].codem1
= BADCODEM1
;
1083 dictp2
->cptr
= hval
;
1084 dictp
->codem1
= max_ent
;
1085 dictp
->f
.fcode
= fcode
;
1087 db
->max_ent
= ++max_ent
;
1088 db
->lens
[max_ent
] = db
->lens
[oldcode
]+1;
1090 /* Expand code size if needed. */
1091 if (max_ent
>= MAXCODE(n_bits
) && max_ent
< db
->maxmaxcode
) {
1092 db
->n_bits
= ++n_bits
;
1093 tgtbitno
= 32-n_bits
;
1098 dmp
->m_len
= wptr
- mtod(dmp
, u_char
*);
1101 * Keep the checkpoint right so that incompressible packets
1102 * clear the dictionary at the right times.
1104 db
->bytes_out
+= ilen
;
1105 db
->in_count
+= explen
;
1106 if (bsd_check(db
) && db
->debug
) {
1107 printf("bsd_decomp%d: peer should have cleared dictionary\n",
1112 db
->comp_bytes
+= ilen
+ BSD_OVHD
;
1114 db
->uncomp_bytes
+= explen
;
1122 printf("bsd_decomp%d: fell off end of chain ", db
->unit
);
1123 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1124 incode
, finchar
, db
->dict
[finchar
].cptr
, max_ent
);
1125 } else if (dictp
->codem1
!= finchar
-1) {
1126 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1127 db
->unit
, incode
, finchar
);
1128 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode
,
1129 db
->dict
[finchar
].cptr
, dictp
->codem1
);
1132 return DECOMP_FATALERROR
;
1135 #endif /* DO_BSD_COMPRESS */