1 /* Because this code is derived from the 4.3BSD compress source:
4 * Copyright (c) 1985, 1986 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
8 * James A. Woods, derived from original work by Spencer Thomas
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * This version is for use with mbufs on BSD-derived systems.
39 * $FreeBSD: src/sys/net/bsd_comp.c,v 1.11.2.1 2002/04/14 21:41:48 luigi Exp $
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
46 #include <net/ppp_layer/ppp_defs.h>
48 #define PACKETPTR struct mbuf *
49 #include <net/ppp_layer/ppp_comp.h>
53 * PPP "BSD compress" compression
54 * The differences between this compression and the classic BSD LZW
55 * source are obvious from the requirement that the classic code worked
56 * with files while this handles arbitrarily long streams that
57 * are broken into packets. They are:
59 * When the code size expands, a block of junk is not emitted by
60 * the compressor and not expected by the decompressor.
62 * New codes are not necessarily assigned every time an old
63 * code is output by the compressor. This is because a packet
64 * end forces a code to be emitted, but does not imply that a
65 * new sequence has been seen.
67 * The compression ratio is checked at the first end of a packet
68 * after the appropriate gap. Besides simplifying and speeding
69 * things up, this makes it more likely that the transmitter
70 * and receiver will agree when the dictionary is cleared when
71 * compression is not going well.
75 * A dictionary for doing BSD compress.
78 int totlen
; /* length of this structure */
79 u_int hsize
; /* size of the hash table */
80 u_char hshift
; /* used in hash function */
81 u_char n_bits
; /* current bits/code */
85 u_int16_t seqno
; /* sequence # of next packet */
86 u_int hdrlen
; /* header length to preallocate */
88 u_int maxmaxcode
; /* largest valid code */
89 u_int max_ent
; /* largest code in use */
90 u_int in_count
; /* uncompressed bytes, aged */
91 u_int bytes_out
; /* compressed bytes, aged */
92 u_int ratio
; /* recent compression ratio */
93 u_int checkpoint
; /* when to next check the ratio */
94 u_int clear_count
; /* times dictionary cleared */
95 u_int incomp_count
; /* incompressible packets */
96 u_int incomp_bytes
; /* incompressible bytes */
97 u_int uncomp_count
; /* uncompressed packets */
98 u_int uncomp_bytes
; /* uncompressed bytes */
99 u_int comp_count
; /* compressed packets */
100 u_int comp_bytes
; /* compressed bytes */
101 u_int16_t
*lens
; /* array of lengths of codes */
103 union { /* hash value */
106 #if BYTE_ORDER == LITTLE_ENDIAN
107 u_int16_t prefix
; /* preceding code */
108 u_char suffix
; /* last character of new code */
112 u_char suffix
; /* last character of new code */
113 u_int16_t prefix
; /* preceding code */
117 u_int16_t codem1
; /* output of hash table -1 */
118 u_int16_t cptr
; /* map code to hash table entry */
122 #define BSD_OVHD 2 /* BSD compress overhead/packet */
123 #define BSD_INIT_BITS BSD_MIN_BITS
125 static void bsd_clear (struct bsd_db
*db
);
126 static int bsd_check (struct bsd_db
*db
);
127 static void *bsd_alloc (u_char
*options
, int opt_len
, int decomp
);
128 static int bsd_init (struct bsd_db
*db
, u_char
*options
, int opt_len
,
129 int unit
, int hdrlen
, int mru
, int debug
,
131 static void *bsd_comp_alloc (u_char
*options
, int opt_len
);
132 static void *bsd_decomp_alloc (u_char
*options
, int opt_len
);
133 static void bsd_free (void *state
);
134 static int bsd_comp_init (void *state
, u_char
*options
, int opt_len
,
135 int unit
, int hdrlen
, int debug
);
136 static int bsd_decomp_init (void *state
, u_char
*options
, int opt_len
,
137 int unit
, int hdrlen
, int mru
, int debug
);
138 static int bsd_compress (void *state
, struct mbuf
**mret
,
139 struct mbuf
*mp
, int slen
, int maxolen
);
140 static void bsd_incomp (void *state
, struct mbuf
*dmsg
);
141 static int bsd_decompress (void *state
, struct mbuf
*cmp
,
143 static void bsd_reset (void *state
);
144 static void bsd_comp_stats (void *state
, struct compstat
*stats
);
147 * Procedures exported to if_ppp.c.
149 struct compressor ppp_bsd_compress
= {
150 CI_BSD_COMPRESS
, /* compress_proto */
151 bsd_comp_alloc
, /* comp_alloc */
152 bsd_free
, /* comp_free */
153 bsd_comp_init
, /* comp_init */
154 bsd_reset
, /* comp_reset */
155 bsd_compress
, /* compress */
156 bsd_comp_stats
, /* comp_stat */
157 bsd_decomp_alloc
, /* decomp_alloc */
158 bsd_free
, /* decomp_free */
159 bsd_decomp_init
, /* decomp_init */
160 bsd_reset
, /* decomp_reset */
161 bsd_decompress
, /* decompress */
162 bsd_incomp
, /* incomp */
163 bsd_comp_stats
, /* decomp_stat */
167 * the next two codes should not be changed lightly, as they must not
168 * lie within the contiguous general code space.
170 #define CLEAR 256 /* table clear output code */
171 #define FIRST 257 /* first free entry */
174 #define MAXCODE(b) ((1 << (b)) - 1)
175 #define BADCODEM1 MAXCODE(BSD_MAX_BITS)
177 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
178 ^ (u_int32_t)(prefix))
179 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
180 + (u_int32_t)(prefix))
182 #define CHECK_GAP 10000 /* Ratio check interval */
184 #define RATIO_SCALE_LOG 8
185 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
186 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
189 * clear the dictionary
192 bsd_clear(struct bsd_db
*db
)
195 db
->max_ent
= FIRST
-1;
196 db
->n_bits
= BSD_INIT_BITS
;
200 db
->checkpoint
= CHECK_GAP
;
204 * If the dictionary is full, then see if it is time to reset it.
206 * Compute the compression ratio using fixed-point arithmetic
207 * with 8 fractional bits.
209 * Since we have an infinite stream instead of a single file,
210 * watch only the local compression ratio.
212 * Since both peers must reset the dictionary at the same time even in
213 * the absence of CLEAR codes (while packets are incompressible), they
214 * must compute the same ratio.
216 * Return 1=output CLEAR
219 bsd_check(struct bsd_db
*db
)
223 if (db
->in_count
>= db
->checkpoint
) {
224 /* age the ratio by limiting the size of the counts */
225 if (db
->in_count
>= RATIO_MAX
226 || db
->bytes_out
>= RATIO_MAX
) {
227 db
->in_count
-= db
->in_count
/4;
228 db
->bytes_out
-= db
->bytes_out
/4;
231 db
->checkpoint
= db
->in_count
+ CHECK_GAP
;
233 if (db
->max_ent
>= db
->maxmaxcode
) {
234 /* Reset the dictionary only if the ratio is worse,
235 * or if it looks as if it has been poisoned
236 * by incompressible data.
238 * This does not overflow, because
239 * db->in_count <= RATIO_MAX.
241 new_ratio
= db
->in_count
<< RATIO_SCALE_LOG
;
242 if (db
->bytes_out
!= 0)
243 new_ratio
/= db
->bytes_out
;
245 if (new_ratio
< db
->ratio
|| new_ratio
< 1 * RATIO_SCALE
) {
249 db
->ratio
= new_ratio
;
259 bsd_comp_stats(void *state
, struct compstat
*stats
)
261 struct bsd_db
*db
= (struct bsd_db
*) state
;
264 stats
->unc_bytes
= db
->uncomp_bytes
;
265 stats
->unc_packets
= db
->uncomp_count
;
266 stats
->comp_bytes
= db
->comp_bytes
;
267 stats
->comp_packets
= db
->comp_count
;
268 stats
->inc_bytes
= db
->incomp_bytes
;
269 stats
->inc_packets
= db
->incomp_count
;
270 stats
->ratio
= db
->in_count
;
272 if (stats
->ratio
<= 0x7fffff)
281 * Reset state, as on a CCP ResetReq.
284 bsd_reset(void *state
)
286 struct bsd_db
*db
= (struct bsd_db
*) state
;
294 * Allocate space for a (de) compressor.
297 bsd_alloc(u_char
*options
, int opt_len
, int decomp
)
300 u_int newlen
, hsize
, hshift
, maxmaxcode
;
303 if (opt_len
< CILEN_BSD_COMPRESS
|| options
[0] != CI_BSD_COMPRESS
304 || options
[1] != CILEN_BSD_COMPRESS
305 || BSD_VERSION(options
[2]) != BSD_CURRENT_VERSION
)
307 bits
= BSD_NBITS(options
[2]);
309 case 9: /* needs 82152 for both directions */
310 case 10: /* needs 84144 */
311 case 11: /* needs 88240 */
312 case 12: /* needs 96432 */
316 case 13: /* needs 176784 */
320 case 14: /* needs 353744 */
324 case 15: /* needs 691440 */
328 case 16: /* needs 1366160--far too much, */
329 /* hsize = 69001; */ /* and 69001 is too big for cptr */
330 /* hshift = 8; */ /* in struct bsd_db */
336 maxmaxcode
= MAXCODE(bits
);
337 newlen
= sizeof(*db
) + (hsize
-1) * (sizeof(db
->dict
[0]));
338 db
= kmalloc(newlen
, M_DEVBUF
, M_WAITOK
);
339 bzero(db
, sizeof(*db
) - sizeof(db
->dict
));
344 db
->lens
= kmalloc((maxmaxcode
+ 1) * sizeof(db
->lens
[0]), M_DEVBUF
,
351 db
->maxmaxcode
= maxmaxcode
;
358 bsd_free(void *state
)
360 struct bsd_db
*db
= (struct bsd_db
*) state
;
363 kfree(db
->lens
, M_DEVBUF
);
368 bsd_comp_alloc(u_char
*options
, int opt_len
)
370 return bsd_alloc(options
, opt_len
, 0);
374 bsd_decomp_alloc(u_char
*options
, int opt_len
)
376 return bsd_alloc(options
, opt_len
, 1);
380 * Initialize the database.
383 bsd_init(struct bsd_db
*db
, u_char
*options
, int opt_len
, int unit
,
384 int hdrlen
, int mru
, int debug
, int decomp
)
388 if (opt_len
< CILEN_BSD_COMPRESS
|| options
[0] != CI_BSD_COMPRESS
389 || options
[1] != CILEN_BSD_COMPRESS
390 || BSD_VERSION(options
[2]) != BSD_CURRENT_VERSION
391 || BSD_NBITS(options
[2]) != db
->maxbits
392 || (decomp
&& db
->lens
== NULL
))
402 db
->dict
[--i
].codem1
= BADCODEM1
;
403 db
->dict
[i
].cptr
= 0;
420 bsd_comp_init(void *state
, u_char
*options
, int opt_len
, int unit
,
421 int hdrlen
, int debug
)
423 return bsd_init((struct bsd_db
*) state
, options
, opt_len
,
424 unit
, hdrlen
, 0, debug
, 0);
428 bsd_decomp_init(void *state
, u_char
*options
, int opt_len
,
429 int unit
, int hdrlen
, int mru
, int debug
)
431 return bsd_init((struct bsd_db
*) state
, options
, opt_len
,
432 unit
, hdrlen
, mru
, debug
, 1);
438 * One change from the BSD compress command is that when the
439 * code size expands, we do not output a bunch of padding.
440 * **mret - return compressed mbuf chain here
442 * slen - uncompressed length
443 * maxolen - max compressed length
448 bsd_compress(void *state
, struct mbuf
**mret
, struct mbuf
*mp
,
449 int slen
, int maxolen
)
451 struct bsd_db
*db
= (struct bsd_db
*) state
;
452 int hshift
= db
->hshift
;
453 u_int max_ent
= db
->max_ent
;
454 u_int n_bits
= db
->n_bits
;
456 u_int32_t accm
= 0, fcode
;
457 struct bsd_dict
*dictp
;
459 int hval
, disp
, ent
, ilen
;
465 #define PUTBYTE(v) { \
469 if (wptr >= cp_end) { \
470 m->m_len = wptr - mtod(m, u_char *); \
471 MGET(m->m_next, M_NOWAIT, MT_DATA); \
475 if (maxolen - olen > MLEN) \
476 MCLGET(m, M_NOWAIT); \
477 wptr = mtod(m, u_char *); \
478 cp_end = wptr + M_TRAILINGSPACE(m); \
485 #define OUTPUT(ent) { \
487 accm |= ((ent) << bitno); \
489 PUTBYTE(accm >> 24); \
492 } while (bitno <= 24); \
496 * If the protocol is not in the range we're interested in,
497 * just return without compressing the packet. If it is,
498 * the protocol becomes the first byte to compress.
500 rptr
= mtod(mp
, u_char
*);
501 ent
= PPP_PROTOCOL(rptr
);
502 if (ent
< 0x21 || ent
> 0xf9) {
507 /* Don't generate compressed packets which are larger than
508 the uncompressed packet. */
512 /* Allocate one mbuf to start with. */
513 MGET(m
, M_NOWAIT
, MT_DATA
);
517 if (maxolen
+ db
->hdrlen
> MLEN
)
519 m
->m_data
+= db
->hdrlen
;
520 wptr
= mtod(m
, u_char
*);
521 cp_end
= wptr
+ M_TRAILINGSPACE(m
);
523 wptr
= cp_end
= NULL
;
526 * Copy the PPP header over, changing the protocol,
527 * and install the 2-byte packet sequence number.
530 *wptr
++ = PPP_ADDRESS(rptr
); /* assumes the ppp header is */
531 *wptr
++ = PPP_CONTROL(rptr
); /* all in one mbuf */
532 *wptr
++ = 0; /* change the protocol */
534 *wptr
++ = db
->seqno
>> 8;
541 slen
= mp
->m_len
- PPP_HDRLEN
;
548 rptr
= mtod(mp
, u_char
*);
551 continue; /* handle 0-length buffers */
557 fcode
= BSD_KEY(ent
, c
);
558 hval
= BSD_HASH(ent
, c
, hshift
);
559 dictp
= &db
->dict
[hval
];
561 /* Validate and then check the entry. */
562 if (dictp
->codem1
>= max_ent
)
564 if (dictp
->f
.fcode
== fcode
) {
565 ent
= dictp
->codem1
+1;
566 continue; /* found (prefix,suffix) */
569 /* continue probing until a match or invalid entry */
570 disp
= (hval
== 0) ? 1 : hval
;
573 if (hval
>= db
->hsize
)
575 dictp
= &db
->dict
[hval
];
576 if (dictp
->codem1
>= max_ent
)
578 } while (dictp
->f
.fcode
!= fcode
);
579 ent
= dictp
->codem1
+ 1; /* finally found (prefix,suffix) */
583 OUTPUT(ent
); /* output the prefix */
585 /* code -> hashtable */
586 if (max_ent
< db
->maxmaxcode
) {
587 struct bsd_dict
*dictp2
;
588 /* expand code size if needed */
589 if (max_ent
>= MAXCODE(n_bits
))
590 db
->n_bits
= ++n_bits
;
592 /* Invalidate old hash table entry using
593 * this code, and then take it over.
595 dictp2
= &db
->dict
[max_ent
+1];
596 if (db
->dict
[dictp2
->cptr
].codem1
== max_ent
)
597 db
->dict
[dictp2
->cptr
].codem1
= BADCODEM1
;
599 dictp
->codem1
= max_ent
;
600 dictp
->f
.fcode
= fcode
;
602 db
->max_ent
= ++max_ent
;
607 OUTPUT(ent
); /* output the last code */
608 db
->bytes_out
+= olen
;
609 db
->in_count
+= ilen
;
611 ++db
->bytes_out
; /* count complete bytes */
614 OUTPUT(CLEAR
); /* do not count the CLEAR */
617 * Pad dribble bits of last code with ones.
618 * Do not emit a completely useless byte of ones.
621 PUTBYTE((accm
| (0xff << (bitno
-8))) >> 24);
624 m
->m_len
= wptr
- mtod(m
, u_char
*);
629 * Increase code size if we would have without the packet
630 * boundary and as the decompressor will.
632 if (max_ent
>= MAXCODE(n_bits
) && max_ent
< db
->maxmaxcode
)
635 db
->uncomp_bytes
+= ilen
;
637 if (olen
+ PPP_HDRLEN
+ BSD_OVHD
> maxolen
) {
638 /* throw away the compressed stuff if it is longer than uncompressed */
644 db
->incomp_bytes
+= ilen
;
647 db
->comp_bytes
+= olen
+ BSD_OVHD
;
650 return olen
+ PPP_HDRLEN
+ BSD_OVHD
;
657 * Update the "BSD Compress" dictionary on the receiver for
658 * incompressible data by pretending to compress the incoming data.
661 bsd_incomp(void *state
, struct mbuf
*dmsg
)
663 struct bsd_db
*db
= (struct bsd_db
*) state
;
664 u_int hshift
= db
->hshift
;
665 u_int max_ent
= db
->max_ent
;
666 u_int n_bits
= db
->n_bits
;
667 struct bsd_dict
*dictp
;
670 u_int32_t hval
, disp
;
677 * If the protocol is not in the range we're interested in,
678 * just return without looking at the packet. If it is,
679 * the protocol becomes the first byte to "compress".
681 rptr
= mtod(dmsg
, u_char
*);
682 ent
= PPP_PROTOCOL(rptr
);
683 if (ent
< 0x21 || ent
> 0xf9)
687 ilen
= 1; /* count the protocol as 1 byte */
689 slen
= dmsg
->m_len
- PPP_HDRLEN
;
695 rptr
= mtod(dmsg
, u_char
*);
703 fcode
= BSD_KEY(ent
, c
);
704 hval
= BSD_HASH(ent
, c
, hshift
);
705 dictp
= &db
->dict
[hval
];
707 /* validate and then check the entry */
708 if (dictp
->codem1
>= max_ent
)
710 if (dictp
->f
.fcode
== fcode
) {
711 ent
= dictp
->codem1
+1;
712 continue; /* found (prefix,suffix) */
715 /* continue probing until a match or invalid entry */
716 disp
= (hval
== 0) ? 1 : hval
;
719 if (hval
>= db
->hsize
)
721 dictp
= &db
->dict
[hval
];
722 if (dictp
->codem1
>= max_ent
)
724 } while (dictp
->f
.fcode
!= fcode
);
725 ent
= dictp
->codem1
+1;
726 continue; /* finally found (prefix,suffix) */
728 nomatch
: /* output (count) the prefix */
731 /* code -> hashtable */
732 if (max_ent
< db
->maxmaxcode
) {
733 struct bsd_dict
*dictp2
;
734 /* expand code size if needed */
735 if (max_ent
>= MAXCODE(n_bits
))
736 db
->n_bits
= ++n_bits
;
738 /* Invalidate previous hash table entry
739 * assigned this code, and then take it over.
741 dictp2
= &db
->dict
[max_ent
+1];
742 if (db
->dict
[dictp2
->cptr
].codem1
== max_ent
)
743 db
->dict
[dictp2
->cptr
].codem1
= BADCODEM1
;
745 dictp
->codem1
= max_ent
;
746 dictp
->f
.fcode
= fcode
;
748 db
->max_ent
= ++max_ent
;
749 db
->lens
[max_ent
] = db
->lens
[ent
]+1;
752 } while (--slen
!= 0);
754 bitno
+= n_bits
; /* output (count) the last code */
755 db
->bytes_out
+= bitno
/8;
756 db
->in_count
+= ilen
;
760 db
->incomp_bytes
+= ilen
;
762 db
->uncomp_bytes
+= ilen
;
764 /* Increase code size if we would have without the packet
765 * boundary and as the decompressor will.
767 if (max_ent
>= MAXCODE(n_bits
) && max_ent
< db
->maxmaxcode
)
773 * Decompress "BSD Compress".
775 * Because of patent problems, we return DECOMP_ERROR for errors
776 * found by inspecting the input data and for system problems, but
777 * DECOMP_FATALERROR for any errors which could possibly be said to
778 * be being detected "after" decompression. For DECOMP_ERROR,
779 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
780 * infringing a patent of Motorola's if we do, so we take CCP down
783 * Given that the frame has the correct sequence number and a good FCS,
784 * errors such as invalid codes in the input most likely indicate a
785 * bug, so we return DECOMP_FATALERROR for them in order to turn off
786 * compression, even though they are detected by inspecting the input.
789 bsd_decompress(void *state
, struct mbuf
*cmp
, struct mbuf
**dmpp
)
791 struct bsd_db
*db
= (struct bsd_db
*) state
;
792 u_int max_ent
= db
->max_ent
;
794 u_int bitno
= 32; /* 1st valid bit in accm */
795 u_int n_bits
= db
->n_bits
;
796 u_int tgtbitno
= 32-n_bits
; /* bitno when we have a code */
797 struct bsd_dict
*dictp
;
798 int explen
, i
, seq
, len
;
799 u_int incode
, oldcode
, finchar
;
800 u_char
*p
, *rptr
, *wptr
;
801 struct mbuf
*m
, *dmp
, *mret
;
802 int adrs
, ctrl
, ilen
;
803 int space
, codelen
, extra
;
806 * Save the address/control from the PPP header
807 * and then get the sequence number.
810 rptr
= mtod(cmp
, u_char
*);
811 adrs
= PPP_ADDRESS(rptr
);
812 ctrl
= PPP_CONTROL(rptr
);
814 len
= cmp
->m_len
- PPP_HDRLEN
;
816 for (i
= 0; i
< 2; ++i
) {
821 rptr
= mtod(cmp
, u_char
*);
824 seq
= (seq
<< 8) + *rptr
++;
829 * Check the sequence number and give up if it differs from
830 * the value we're expecting.
832 if (seq
!= db
->seqno
) {
834 kprintf("bsd_decomp%d: bad sequence # %d, expected %d\n",
835 db
->unit
, seq
, db
->seqno
- 1);
841 * Allocate one mbuf to start with.
843 MGETHDR(dmp
, M_NOWAIT
, MT_DATA
);
849 MCLGET(dmp
, M_NOWAIT
);
850 dmp
->m_data
+= db
->hdrlen
;
851 wptr
= mtod(dmp
, u_char
*);
852 space
= M_TRAILINGSPACE(dmp
) - PPP_HDRLEN
+ 1;
855 * Fill in the ppp header, but not the last byte of the protocol
856 * (that comes from the decompressed data).
861 wptr
+= PPP_HDRLEN
- 1;
869 if (!cmp
) /* quit at end of message */
871 rptr
= mtod(cmp
, u_char
*);
874 continue; /* handle 0-length buffers */
878 * Accumulate bytes until we have a complete code.
879 * Then get the next code, relying on the 32-bit,
880 * unsigned accm to mask the result.
883 accm
|= *rptr
++ << bitno
;
885 if (tgtbitno
< bitno
)
887 incode
= accm
>> tgtbitno
;
891 if (incode
== CLEAR
) {
893 * The dictionary must only be cleared at
894 * the end of a packet. But there could be an
895 * empty mbuf at the end.
897 if (len
> 0 || cmp
->m_next
!= NULL
) {
898 while ((cmp
= cmp
->m_next
) != NULL
)
903 kprintf("bsd_decomp%d: bad CLEAR\n", db
->unit
);
904 return DECOMP_FATALERROR
; /* probably a bug */
912 if (incode
> max_ent
+ 2 || incode
> db
->maxmaxcode
913 || (incode
> max_ent
&& oldcode
== CLEAR
)) {
916 kprintf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
917 db
->unit
, incode
, oldcode
);
918 kprintf("max_ent=0x%x explen=%d seqno=%d\n",
919 max_ent
, explen
, db
->seqno
);
921 return DECOMP_FATALERROR
; /* probably a bug */
924 /* Special case for KwKwK string. */
925 if (incode
> max_ent
) {
933 codelen
= db
->lens
[finchar
];
934 explen
+= codelen
+ extra
;
935 if (explen
> db
->mru
+ 1) {
938 kprintf("bsd_decomp%d: ran out of mru\n", db
->unit
);
940 while ((cmp
= cmp
->m_next
) != NULL
)
942 kprintf(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
943 len
, finchar
, codelen
, explen
);
946 return DECOMP_FATALERROR
;
950 * For simplicity, the decoded characters go in a single mbuf,
951 * so we allocate a single extra cluster mbuf if necessary.
953 if ((space
-= codelen
+ extra
) < 0) {
954 dmp
->m_len
= wptr
- mtod(dmp
, u_char
*);
955 MGET(m
, M_NOWAIT
, MT_DATA
);
964 space
= M_TRAILINGSPACE(m
) - (codelen
+ extra
);
966 /* now that's what I call *compression*. */
971 wptr
= mtod(dmp
, u_char
*);
975 * Decode this code and install it in the decompressed buffer.
977 p
= (wptr
+= codelen
);
978 while (finchar
> LAST
) {
979 dictp
= &db
->dict
[db
->dict
[finchar
].cptr
];
981 if (--codelen
<= 0 || dictp
->codem1
!= finchar
-1)
984 *--p
= dictp
->f
.hs
.suffix
;
985 finchar
= dictp
->f
.hs
.prefix
;
991 kprintf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
992 db
->unit
, codelen
, incode
, max_ent
);
995 if (extra
) /* the KwKwK case again */
999 * If not first code in a packet, and
1000 * if not out of code space, then allocate a new code.
1002 * Keep the hash table correct so it can be used
1003 * with uncompressed packets.
1005 if (oldcode
!= CLEAR
&& max_ent
< db
->maxmaxcode
) {
1006 struct bsd_dict
*dictp2
;
1008 u_int32_t hval
, disp
;
1010 fcode
= BSD_KEY(oldcode
,finchar
);
1011 hval
= BSD_HASH(oldcode
,finchar
,db
->hshift
);
1012 dictp
= &db
->dict
[hval
];
1014 /* look for a free hash table entry */
1015 if (dictp
->codem1
< max_ent
) {
1016 disp
= (hval
== 0) ? 1 : hval
;
1019 if (hval
>= db
->hsize
)
1021 dictp
= &db
->dict
[hval
];
1022 } while (dictp
->codem1
< max_ent
);
1026 * Invalidate previous hash table entry
1027 * assigned this code, and then take it over
1029 dictp2
= &db
->dict
[max_ent
+1];
1030 if (db
->dict
[dictp2
->cptr
].codem1
== max_ent
) {
1031 db
->dict
[dictp2
->cptr
].codem1
= BADCODEM1
;
1033 dictp2
->cptr
= hval
;
1034 dictp
->codem1
= max_ent
;
1035 dictp
->f
.fcode
= fcode
;
1037 db
->max_ent
= ++max_ent
;
1038 db
->lens
[max_ent
] = db
->lens
[oldcode
]+1;
1040 /* Expand code size if needed. */
1041 if (max_ent
>= MAXCODE(n_bits
) && max_ent
< db
->maxmaxcode
) {
1042 db
->n_bits
= ++n_bits
;
1043 tgtbitno
= 32-n_bits
;
1048 dmp
->m_len
= wptr
- mtod(dmp
, u_char
*);
1051 * Keep the checkpoint right so that incompressible packets
1052 * clear the dictionary at the right times.
1054 db
->bytes_out
+= ilen
;
1055 db
->in_count
+= explen
;
1056 if (bsd_check(db
) && db
->debug
) {
1057 kprintf("bsd_decomp%d: peer should have cleared dictionary\n",
1062 db
->comp_bytes
+= ilen
+ BSD_OVHD
;
1064 db
->uncomp_bytes
+= explen
;
1072 kprintf("bsd_decomp%d: fell off end of chain ", db
->unit
);
1073 kprintf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1074 incode
, finchar
, db
->dict
[finchar
].cptr
, max_ent
);
1075 } else if (dictp
->codem1
!= finchar
-1) {
1076 kprintf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1077 db
->unit
, incode
, finchar
);
1078 kprintf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode
,
1079 db
->dict
[finchar
].cptr
, dictp
->codem1
);
1082 return DECOMP_FATALERROR
;
1085 #endif /* DO_BSD_COMPRESS */