2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Because this code is derived from the 4.3BSD compress source:
25 * Copyright (c) 1985, 1986 The Regents of the University of California.
26 * All rights reserved.
28 * This code is derived from software contributed to Berkeley by
29 * James A. Woods, derived from original work by Spencer Thomas
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * This version is for use with mbufs on BSD-derived systems.
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/malloc.h>
70 #include <net/ppp_defs.h>
72 #define PACKETPTR struct mbuf *
73 #include <net/ppp_comp.h>
77 * PPP "BSD compress" compression
78 * The differences between this compression and the classic BSD LZW
79 * source are obvious from the requirement that the classic code worked
80 * with files while this handles arbitrarily long streams that
81 * are broken into packets. They are:
83 * When the code size expands, a block of junk is not emitted by
84 * the compressor and not expected by the decompressor.
86 * New codes are not necessarily assigned every time an old
87 * code is output by the compressor. This is because a packet
88 * end forces a code to be emitted, but does not imply that a
89 * new sequence has been seen.
91 * The compression ratio is checked at the first end of a packet
92 * after the appropriate gap. Besides simplifying and speeding
93 * things up, this makes it more likely that the transmitter
94 * and receiver will agree when the dictionary is cleared when
95 * compression is not going well.
99 * A dictionary for doing BSD compress.
102 int totlen
; /* length of this structure */
103 u_int hsize
; /* size of the hash table */
104 u_char hshift
; /* used in hash function */
105 u_char n_bits
; /* current bits/code */
109 u_int16_t seqno
; /* sequence # of next packet */
110 u_int hdrlen
; /* header length to preallocate */
112 u_int maxmaxcode
; /* largest valid code */
113 u_int max_ent
; /* largest code in use */
114 u_int in_count
; /* uncompressed bytes, aged */
115 u_int bytes_out
; /* compressed bytes, aged */
116 u_int ratio
; /* recent compression ratio */
117 u_int checkpoint
; /* when to next check the ratio */
118 u_int clear_count
; /* times dictionary cleared */
119 u_int incomp_count
; /* incompressible packets */
120 u_int incomp_bytes
; /* incompressible bytes */
121 u_int uncomp_count
; /* uncompressed packets */
122 u_int uncomp_bytes
; /* uncompressed bytes */
123 u_int comp_count
; /* compressed packets */
124 u_int comp_bytes
; /* compressed bytes */
125 u_int16_t
*lens
; /* array of lengths of codes */
127 union { /* hash value */
130 #if BYTE_ORDER == LITTLE_ENDIAN
131 u_int16_t prefix
; /* preceding code */
132 u_char suffix
; /* last character of new code */
136 u_char suffix
; /* last character of new code */
137 u_int16_t prefix
; /* preceding code */
141 u_int16_t codem1
; /* output of hash table -1 */
142 u_int16_t cptr
; /* map code to hash table entry */
146 #define BSD_OVHD 2 /* BSD compress overhead/packet */
147 #define BSD_INIT_BITS BSD_MIN_BITS
149 static void bsd_clear(struct bsd_db
*db
);
150 static int bsd_check(struct bsd_db
*db
);
151 static void *bsd_alloc(u_char
*options
, int opt_len
, int decomp
);
152 static int bsd_init_comp_db(struct bsd_db
*db
, u_char
*options
,
154 int unit
, int hdrlen
, int mru
, int debug
,
156 static void *bsd_comp_alloc(u_char
*options
, int opt_len
);
157 static void *bsd_decomp_alloc(u_char
*options
, int opt_len
);
158 static void bsd_free(void *state
);
159 static int bsd_comp_init(void *state
, u_char
*options
, int opt_len
,
160 int unit
, int hdrlen
, int debug
);
161 static int bsd_decomp_init(void *state
, u_char
*options
, int opt_len
,
162 int unit
, int hdrlen
, int mru
, int debug
);
163 static int bsd_compress(void *state
, struct mbuf
**mret
,
164 struct mbuf
*mp
, int slen
, int maxolen
);
165 static void bsd_incomp(void *state
, struct mbuf
*dmsg
);
166 static int bsd_decompress(void *state
, struct mbuf
*cmp
,
168 static void bsd_reset(void *state
);
169 static void bsd_comp_stats(void *state
, struct compstat
*stats
);
172 * Procedures exported to if_ppp.c.
174 struct compressor ppp_bsd_compress
= {
175 CI_BSD_COMPRESS
, /* compress_proto */
176 bsd_comp_alloc
, /* comp_alloc */
177 bsd_free
, /* comp_free */
178 bsd_comp_init
, /* comp_init */
179 bsd_reset
, /* comp_reset */
180 bsd_compress
, /* compress */
181 bsd_comp_stats
, /* comp_stat */
182 bsd_decomp_alloc
, /* decomp_alloc */
183 bsd_free
, /* decomp_free */
184 bsd_decomp_init
, /* decomp_init */
185 bsd_reset
, /* decomp_reset */
186 bsd_decompress
, /* decompress */
187 bsd_incomp
, /* incomp */
188 bsd_comp_stats
, /* decomp_stat */
192 * the next two codes should not be changed lightly, as they must not
193 * lie within the contiguous general code space.
195 #define CLEAR 256 /* table clear output code */
196 #define FIRST 257 /* first free entry */
199 #define MAXCODE(b) ((1 << (b)) - 1)
200 #define BADCODEM1 MAXCODE(BSD_MAX_BITS)
202 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
203 ^ (u_int32_t)(prefix))
204 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
205 + (u_int32_t)(prefix))
207 #define CHECK_GAP 10000 /* Ratio check interval */
209 #define RATIO_SCALE_LOG 8
210 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
211 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
214 * clear the dictionary
221 db
->max_ent
= FIRST
-1;
222 db
->n_bits
= BSD_INIT_BITS
;
226 db
->checkpoint
= CHECK_GAP
;
230 * If the dictionary is full, then see if it is time to reset it.
232 * Compute the compression ratio using fixed-point arithmetic
233 * with 8 fractional bits.
235 * Since we have an infinite stream instead of a single file,
236 * watch only the local compression ratio.
238 * Since both peers must reset the dictionary at the same time even in
239 * the absence of CLEAR codes (while packets are incompressible), they
240 * must compute the same ratio.
242 static int /* 1=output CLEAR */
248 if (db
->in_count
>= db
->checkpoint
) {
249 /* age the ratio by limiting the size of the counts */
250 if (db
->in_count
>= RATIO_MAX
251 || db
->bytes_out
>= RATIO_MAX
) {
252 db
->in_count
-= db
->in_count
/4;
253 db
->bytes_out
-= db
->bytes_out
/4;
256 db
->checkpoint
= db
->in_count
+ CHECK_GAP
;
258 if (db
->max_ent
>= db
->maxmaxcode
) {
259 /* Reset the dictionary only if the ratio is worse,
260 * or if it looks as if it has been poisoned
261 * by incompressible data.
263 * This does not overflow, because
264 * db->in_count <= RATIO_MAX.
266 new_ratio
= db
->in_count
<< RATIO_SCALE_LOG
;
267 if (db
->bytes_out
!= 0)
268 new_ratio
/= db
->bytes_out
;
270 if (new_ratio
< db
->ratio
|| new_ratio
< 1 * RATIO_SCALE
) {
274 db
->ratio
= new_ratio
;
284 bsd_comp_stats(state
, stats
)
286 struct compstat
*stats
;
288 struct bsd_db
*db
= (struct bsd_db
*) state
;
291 stats
->unc_bytes
= db
->uncomp_bytes
;
292 stats
->unc_packets
= db
->uncomp_count
;
293 stats
->comp_bytes
= db
->comp_bytes
;
294 stats
->comp_packets
= db
->comp_count
;
295 stats
->inc_bytes
= db
->incomp_bytes
;
296 stats
->inc_packets
= db
->incomp_count
;
297 stats
->ratio
= db
->in_count
;
299 if (stats
->ratio
<= 0x7fffff)
308 * Reset state, as on a CCP ResetReq.
314 struct bsd_db
*db
= (struct bsd_db
*) state
;
322 * Allocate space for a (de) compressor.
325 bsd_alloc(options
, opt_len
, decomp
)
330 u_int newlen
, hsize
, hshift
, maxmaxcode
;
333 if (opt_len
< CILEN_BSD_COMPRESS
|| options
[0] != CI_BSD_COMPRESS
334 || options
[1] != CILEN_BSD_COMPRESS
335 || BSD_VERSION(options
[2]) != BSD_CURRENT_VERSION
)
337 bits
= BSD_NBITS(options
[2]);
339 case 9: /* needs 82152 for both directions */
340 case 10: /* needs 84144 */
341 case 11: /* needs 88240 */
342 case 12: /* needs 96432 */
346 case 13: /* needs 176784 */
350 case 14: /* needs 353744 */
354 case 15: /* needs 691440 */
358 case 16: /* needs 1366160--far too much, */
359 /* hsize = 69001; */ /* and 69001 is too big for cptr */
360 /* hshift = 8; */ /* in struct bsd_db */
366 maxmaxcode
= MAXCODE(bits
);
367 newlen
= sizeof(*db
) + (hsize
-1) * (sizeof(db
->dict
[0]));
368 MALLOC(db
, struct bsd_db
*, newlen
, M_DEVBUF
, M_NOWAIT
);
371 bzero(db
, sizeof(*db
) - sizeof(db
->dict
));
376 MALLOC(db
->lens
, u_int16_t
*, (maxmaxcode
+1) * sizeof(db
->lens
[0]),
387 db
->maxmaxcode
= maxmaxcode
;
397 struct bsd_db
*db
= (struct bsd_db
*) state
;
400 FREE(db
->lens
, M_DEVBUF
);
405 bsd_comp_alloc(options
, opt_len
)
409 return bsd_alloc(options
, opt_len
, 0);
413 bsd_decomp_alloc(options
, opt_len
)
417 return bsd_alloc(options
, opt_len
, 1);
421 * Initialize the database.
424 bsd_init_comp_db(db
, options
, opt_len
, unit
, hdrlen
, mru
, debug
, decomp
)
427 int opt_len
, unit
, hdrlen
, mru
, debug
, decomp
;
431 if (opt_len
< CILEN_BSD_COMPRESS
|| options
[0] != CI_BSD_COMPRESS
432 || options
[1] != CILEN_BSD_COMPRESS
433 || BSD_VERSION(options
[2]) != BSD_CURRENT_VERSION
434 || BSD_NBITS(options
[2]) != db
->maxbits
435 || (decomp
&& db
->lens
== NULL
))
445 db
->dict
[--i
].codem1
= BADCODEM1
;
446 db
->dict
[i
].cptr
= 0;
463 bsd_comp_init(state
, options
, opt_len
, unit
, hdrlen
, debug
)
466 int opt_len
, unit
, hdrlen
, debug
;
468 return bsd_init_comp_db((struct bsd_db
*) state
, options
, opt_len
,
469 unit
, hdrlen
, 0, debug
, 0);
473 bsd_decomp_init(state
, options
, opt_len
, unit
, hdrlen
, mru
, debug
)
476 int opt_len
, unit
, hdrlen
, mru
, debug
;
478 return bsd_init_comp_db((struct bsd_db
*) state
, options
, opt_len
,
479 unit
, hdrlen
, mru
, debug
, 1);
485 * One change from the BSD compress command is that when the
486 * code size expands, we do not output a bunch of padding.
489 bsd_compress(state
, mret
, mp
, slen
, maxolen
)
491 struct mbuf
**mret
; /* return compressed mbuf chain here */
492 struct mbuf
*mp
; /* from here */
493 int slen
; /* uncompressed length */
494 int maxolen
; /* max compressed length */
496 struct bsd_db
*db
= (struct bsd_db
*) state
;
497 int hshift
= db
->hshift
;
498 u_int max_ent
= db
->max_ent
;
499 u_int n_bits
= db
->n_bits
;
501 u_int32_t accm
= 0, fcode
;
502 struct bsd_dict
*dictp
;
504 int hval
, disp
, ent
, ilen
;
510 #define PUTBYTE(v) { \
514 if (wptr >= cp_end) { \
515 m->m_len = wptr - mtod(m, u_char *); \
516 MGET(m->m_next, M_DONTWAIT, MT_DATA); \
520 if (maxolen - olen > MLEN) \
521 MCLGET(m, M_DONTWAIT); \
522 wptr = mtod(m, u_char *); \
523 cp_end = wptr + M_TRAILINGSPACE(m); \
530 #define OUTPUT(ent) { \
532 accm |= ((ent) << bitno); \
534 PUTBYTE(accm >> 24); \
537 } while (bitno <= 24); \
541 * If the protocol is not in the range we're interested in,
542 * just return without compressing the packet. If it is,
543 * the protocol becomes the first byte to compress.
545 rptr
= mtod(mp
, u_char
*);
546 ent
= PPP_PROTOCOL(rptr
);
547 if (ent
< 0x21 || ent
> 0xf9) {
552 /* Don't generate compressed packets which are larger than
553 the uncompressed packet. */
557 /* Allocate one mbuf to start with. */
558 MGET(m
, M_DONTWAIT
, MT_DATA
);
562 if (maxolen
+ db
->hdrlen
> MLEN
)
563 MCLGET(m
, M_DONTWAIT
);
564 m
->m_data
+= db
->hdrlen
;
565 wptr
= mtod(m
, u_char
*);
566 cp_end
= wptr
+ M_TRAILINGSPACE(m
);
568 wptr
= cp_end
= NULL
;
571 * Copy the PPP header over, changing the protocol,
572 * and install the 2-byte packet sequence number.
575 *wptr
++ = PPP_ADDRESS(rptr
); /* assumes the ppp header is */
576 *wptr
++ = PPP_CONTROL(rptr
); /* all in one mbuf */
577 *wptr
++ = 0; /* change the protocol */
579 *wptr
++ = db
->seqno
>> 8;
586 slen
= mp
->m_len
- PPP_HDRLEN
;
593 rptr
= mtod(mp
, u_char
*);
596 continue; /* handle 0-length buffers */
602 fcode
= BSD_KEY(ent
, c
);
603 hval
= BSD_HASH(ent
, c
, hshift
);
604 dictp
= &db
->dict
[hval
];
606 /* Validate and then check the entry. */
607 if (dictp
->codem1
>= max_ent
)
609 if (dictp
->f
.fcode
== fcode
) {
610 ent
= dictp
->codem1
+1;
611 continue; /* found (prefix,suffix) */
614 /* continue probing until a match or invalid entry */
615 disp
= (hval
== 0) ? 1 : hval
;
618 if (hval
>= db
->hsize
)
620 dictp
= &db
->dict
[hval
];
621 if (dictp
->codem1
>= max_ent
)
623 } while (dictp
->f
.fcode
!= fcode
);
624 ent
= dictp
->codem1
+ 1; /* finally found (prefix,suffix) */
628 OUTPUT(ent
); /* output the prefix */
630 /* code -> hashtable */
631 if (max_ent
< db
->maxmaxcode
) {
632 struct bsd_dict
*dictp2
;
633 /* expand code size if needed */
634 if (max_ent
>= MAXCODE(n_bits
))
635 db
->n_bits
= ++n_bits
;
637 /* Invalidate old hash table entry using
638 * this code, and then take it over.
640 dictp2
= &db
->dict
[max_ent
+1];
641 if (db
->dict
[dictp2
->cptr
].codem1
== max_ent
)
642 db
->dict
[dictp2
->cptr
].codem1
= BADCODEM1
;
644 dictp
->codem1
= max_ent
;
645 dictp
->f
.fcode
= fcode
;
647 db
->max_ent
= ++max_ent
;
652 OUTPUT(ent
); /* output the last code */
653 db
->bytes_out
+= olen
;
654 db
->in_count
+= ilen
;
656 ++db
->bytes_out
; /* count complete bytes */
659 OUTPUT(CLEAR
); /* do not count the CLEAR */
662 * Pad dribble bits of last code with ones.
663 * Do not emit a completely useless byte of ones.
666 PUTBYTE((accm
| (0xff << (bitno
-8))) >> 24);
669 m
->m_len
= wptr
- mtod(m
, u_char
*);
674 * Increase code size if we would have without the packet
675 * boundary and as the decompressor will.
677 if (max_ent
>= MAXCODE(n_bits
) && max_ent
< db
->maxmaxcode
)
680 db
->uncomp_bytes
+= ilen
;
682 if (olen
+ PPP_HDRLEN
+ BSD_OVHD
> maxolen
) {
683 /* throw away the compressed stuff if it is longer than uncompressed */
689 db
->incomp_bytes
+= ilen
;
692 db
->comp_bytes
+= olen
+ BSD_OVHD
;
695 return olen
+ PPP_HDRLEN
+ BSD_OVHD
;
702 * Update the "BSD Compress" dictionary on the receiver for
703 * incompressible data by pretending to compress the incoming data.
706 bsd_incomp(state
, dmsg
)
710 struct bsd_db
*db
= (struct bsd_db
*) state
;
711 u_int hshift
= db
->hshift
;
712 u_int max_ent
= db
->max_ent
;
713 u_int n_bits
= db
->n_bits
;
714 struct bsd_dict
*dictp
;
717 u_int32_t hval
, disp
;
724 * If the protocol is not in the range we're interested in,
725 * just return without looking at the packet. If it is,
726 * the protocol becomes the first byte to "compress".
728 rptr
= mtod(dmsg
, u_char
*);
729 ent
= PPP_PROTOCOL(rptr
);
730 if (ent
< 0x21 || ent
> 0xf9)
734 ilen
= 1; /* count the protocol as 1 byte */
736 slen
= dmsg
->m_len
- PPP_HDRLEN
;
742 rptr
= mtod(dmsg
, u_char
*);
750 fcode
= BSD_KEY(ent
, c
);
751 hval
= BSD_HASH(ent
, c
, hshift
);
752 dictp
= &db
->dict
[hval
];
754 /* validate and then check the entry */
755 if (dictp
->codem1
>= max_ent
)
757 if (dictp
->f
.fcode
== fcode
) {
758 ent
= dictp
->codem1
+1;
759 continue; /* found (prefix,suffix) */
762 /* continue probing until a match or invalid entry */
763 disp
= (hval
== 0) ? 1 : hval
;
766 if (hval
>= db
->hsize
)
768 dictp
= &db
->dict
[hval
];
769 if (dictp
->codem1
>= max_ent
)
771 } while (dictp
->f
.fcode
!= fcode
);
772 ent
= dictp
->codem1
+1;
773 continue; /* finally found (prefix,suffix) */
775 nomatch
: /* output (count) the prefix */
778 /* code -> hashtable */
779 if (max_ent
< db
->maxmaxcode
) {
780 struct bsd_dict
*dictp2
;
781 /* expand code size if needed */
782 if (max_ent
>= MAXCODE(n_bits
))
783 db
->n_bits
= ++n_bits
;
785 /* Invalidate previous hash table entry
786 * assigned this code, and then take it over.
788 dictp2
= &db
->dict
[max_ent
+1];
789 if (db
->dict
[dictp2
->cptr
].codem1
== max_ent
)
790 db
->dict
[dictp2
->cptr
].codem1
= BADCODEM1
;
792 dictp
->codem1
= max_ent
;
793 dictp
->f
.fcode
= fcode
;
795 db
->max_ent
= ++max_ent
;
796 db
->lens
[max_ent
] = db
->lens
[ent
]+1;
799 } while (--slen
!= 0);
801 bitno
+= n_bits
; /* output (count) the last code */
802 db
->bytes_out
+= bitno
/8;
803 db
->in_count
+= ilen
;
807 db
->incomp_bytes
+= ilen
;
809 db
->uncomp_bytes
+= ilen
;
811 /* Increase code size if we would have without the packet
812 * boundary and as the decompressor will.
814 if (max_ent
>= MAXCODE(n_bits
) && max_ent
< db
->maxmaxcode
)
820 * Decompress "BSD Compress".
822 * Because of patent problems, we return DECOMP_ERROR for errors
823 * found by inspecting the input data and for system problems, but
824 * DECOMP_FATALERROR for any errors which could possibly be said to
825 * be being detected "after" decompression. For DECOMP_ERROR,
826 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
827 * infringing a patent of Motorola's if we do, so we take CCP down
830 * Given that the frame has the correct sequence number and a good FCS,
831 * errors such as invalid codes in the input most likely indicate a
832 * bug, so we return DECOMP_FATALERROR for them in order to turn off
833 * compression, even though they are detected by inspecting the input.
836 bsd_decompress(state
, cmp
, dmpp
)
838 struct mbuf
*cmp
, **dmpp
;
840 struct bsd_db
*db
= (struct bsd_db
*) state
;
841 u_int max_ent
= db
->max_ent
;
843 u_int bitno
= 32; /* 1st valid bit in accm */
844 u_int n_bits
= db
->n_bits
;
845 u_int tgtbitno
= 32-n_bits
; /* bitno when we have a code */
846 struct bsd_dict
*dictp
;
847 int explen
, i
, seq
, len
;
848 u_int incode
, oldcode
, finchar
;
849 u_char
*p
, *rptr
, *wptr
;
850 struct mbuf
*m
, *dmp
, *mret
;
851 int adrs
, ctrl
, ilen
;
852 int space
, codelen
, extra
;
855 * Save the address/control from the PPP header
856 * and then get the sequence number.
859 rptr
= mtod(cmp
, u_char
*);
860 adrs
= PPP_ADDRESS(rptr
);
861 ctrl
= PPP_CONTROL(rptr
);
863 len
= cmp
->m_len
- PPP_HDRLEN
;
865 for (i
= 0; i
< 2; ++i
) {
870 rptr
= mtod(cmp
, u_char
*);
873 seq
= (seq
<< 8) + *rptr
++;
878 * Check the sequence number and give up if it differs from
879 * the value we're expecting.
881 if (seq
!= db
->seqno
) {
883 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
884 db
->unit
, seq
, db
->seqno
- 1);
890 * Allocate one mbuf to start with.
892 MGETHDR(dmp
, M_DONTWAIT
, MT_DATA
);
898 MCLGET(dmp
, M_DONTWAIT
);
899 dmp
->m_data
+= db
->hdrlen
;
900 wptr
= mtod(dmp
, u_char
*);
901 space
= M_TRAILINGSPACE(dmp
) - PPP_HDRLEN
+ 1;
904 * Fill in the ppp header, but not the last byte of the protocol
905 * (that comes from the decompressed data).
910 wptr
+= PPP_HDRLEN
- 1;
918 if (!cmp
) /* quit at end of message */
920 rptr
= mtod(cmp
, u_char
*);
923 continue; /* handle 0-length buffers */
927 * Accumulate bytes until we have a complete code.
928 * Then get the next code, relying on the 32-bit,
929 * unsigned accm to mask the result.
932 accm
|= *rptr
++ << bitno
;
934 if (tgtbitno
< bitno
)
936 incode
= accm
>> tgtbitno
;
940 if (incode
== CLEAR
) {
942 * The dictionary must only be cleared at
943 * the end of a packet. But there could be an
944 * empty mbuf at the end.
946 if (len
> 0 || cmp
->m_next
!= NULL
) {
947 while ((cmp
= cmp
->m_next
) != NULL
)
952 printf("bsd_decomp%d: bad CLEAR\n", db
->unit
);
953 return DECOMP_FATALERROR
; /* probably a bug */
961 if (incode
> max_ent
+ 2 || incode
> db
->maxmaxcode
962 || (incode
> max_ent
&& oldcode
== CLEAR
)) {
965 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
966 db
->unit
, incode
, oldcode
);
967 printf("max_ent=0x%x explen=%d seqno=%d\n",
968 max_ent
, explen
, db
->seqno
);
970 return DECOMP_FATALERROR
; /* probably a bug */
973 /* Special case for KwKwK string. */
974 if (incode
> max_ent
) {
982 codelen
= db
->lens
[finchar
];
983 explen
+= codelen
+ extra
;
984 if (explen
> db
->mru
+ 1) {
987 printf("bsd_decomp%d: ran out of mru\n", db
->unit
);
989 while ((cmp
= cmp
->m_next
) != NULL
)
991 printf(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
992 len
, finchar
, codelen
, explen
);
995 return DECOMP_FATALERROR
;
999 * For simplicity, the decoded characters go in a single mbuf,
1000 * so we allocate a single extra cluster mbuf if necessary.
1002 if ((space
-= codelen
+ extra
) < 0) {
1003 dmp
->m_len
= wptr
- mtod(dmp
, u_char
*);
1004 MGET(m
, M_DONTWAIT
, MT_DATA
);
1007 return DECOMP_ERROR
;
1012 MCLGET(m
, M_DONTWAIT
);
1013 space
= M_TRAILINGSPACE(m
) - (codelen
+ extra
);
1015 /* now that's what I call *compression*. */
1017 return DECOMP_ERROR
;
1020 wptr
= mtod(dmp
, u_char
*);
1024 * Decode this code and install it in the decompressed buffer.
1026 p
= (wptr
+= codelen
);
1027 while (finchar
> LAST
) {
1028 dictp
= &db
->dict
[db
->dict
[finchar
].cptr
];
1030 if (--codelen
<= 0 || dictp
->codem1
!= finchar
-1)
1033 *--p
= dictp
->f
.hs
.suffix
;
1034 finchar
= dictp
->f
.hs
.prefix
;
1040 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1041 db
->unit
, codelen
, incode
, max_ent
);
1044 if (extra
) /* the KwKwK case again */
1048 * If not first code in a packet, and
1049 * if not out of code space, then allocate a new code.
1051 * Keep the hash table correct so it can be used
1052 * with uncompressed packets.
1054 if (oldcode
!= CLEAR
&& max_ent
< db
->maxmaxcode
) {
1055 struct bsd_dict
*dictp2
;
1057 u_int32_t hval
, disp
;
1059 fcode
= BSD_KEY(oldcode
,finchar
);
1060 hval
= BSD_HASH(oldcode
,finchar
,db
->hshift
);
1061 dictp
= &db
->dict
[hval
];
1063 /* look for a free hash table entry */
1064 if (dictp
->codem1
< max_ent
) {
1065 disp
= (hval
== 0) ? 1 : hval
;
1068 if (hval
>= db
->hsize
)
1070 dictp
= &db
->dict
[hval
];
1071 } while (dictp
->codem1
< max_ent
);
1075 * Invalidate previous hash table entry
1076 * assigned this code, and then take it over
1078 dictp2
= &db
->dict
[max_ent
+1];
1079 if (db
->dict
[dictp2
->cptr
].codem1
== max_ent
) {
1080 db
->dict
[dictp2
->cptr
].codem1
= BADCODEM1
;
1082 dictp2
->cptr
= hval
;
1083 dictp
->codem1
= max_ent
;
1084 dictp
->f
.fcode
= fcode
;
1086 db
->max_ent
= ++max_ent
;
1087 db
->lens
[max_ent
] = db
->lens
[oldcode
]+1;
1089 /* Expand code size if needed. */
1090 if (max_ent
>= MAXCODE(n_bits
) && max_ent
< db
->maxmaxcode
) {
1091 db
->n_bits
= ++n_bits
;
1092 tgtbitno
= 32-n_bits
;
1097 dmp
->m_len
= wptr
- mtod(dmp
, u_char
*);
1100 * Keep the checkpoint right so that incompressible packets
1101 * clear the dictionary at the right times.
1103 db
->bytes_out
+= ilen
;
1104 db
->in_count
+= explen
;
1105 if (bsd_check(db
) && db
->debug
) {
1106 printf("bsd_decomp%d: peer should have cleared dictionary\n",
1111 db
->comp_bytes
+= ilen
+ BSD_OVHD
;
1113 db
->uncomp_bytes
+= explen
;
1121 printf("bsd_decomp%d: fell off end of chain ", db
->unit
);
1122 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1123 incode
, finchar
, db
->dict
[finchar
].cptr
, max_ent
);
1124 } else if (dictp
->codem1
!= finchar
-1) {
1125 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1126 db
->unit
, incode
, finchar
);
1127 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode
,
1128 db
->dict
[finchar
].cptr
, dictp
->codem1
);
1131 return DECOMP_FATALERROR
;
1134 #endif /* DO_BSD_COMPRESS */