1 //==========================================================================
5 //==========================================================================
6 //####ECOSGPLCOPYRIGHTBEGIN####
7 // -------------------------------------------
8 // This file is part of eCos, the Embedded Configurable Operating System.
9 // Portions created by Nick Garnett are
10 // Copyright (C) 2003 eCosCentric Ltd.
12 // eCos is free software; you can redistribute it and/or modify it under
13 // the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 2 or (at your option) any later version.
16 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 // You should have received a copy of the GNU General Public License along
22 // with eCos; if not, write to the Free Software Foundation, Inc.,
23 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 // As a special exception, if other files instantiate templates or use macros
26 // or inline functions from this file, or you compile this file and link it
27 // with other works to produce a work based on this file, this file does not
28 // by itself cause the resulting work to be covered by the GNU General Public
29 // License. However the source code for this file must still be made available
30 // in accordance with section (3) of the GNU General Public License.
32 // This exception does not invalidate any other reasons why a work based on
33 // this file might be covered by the GNU General Public License.
35 // -------------------------------------------
36 //####ECOSGPLCOPYRIGHTEND####
37 //####BSDCOPYRIGHTBEGIN####
39 // -------------------------------------------
41 // Portions of this software may have been derived from OpenBSD,
42 // FreeBSD or other sources, and are covered by the appropriate
43 // copyright disclaimers included herein.
45 // -------------------------------------------
47 //####BSDCOPYRIGHTEND####
48 //==========================================================================
50 /* Because this code is derived from the 4.3BSD compress source:
53 * Copyright (c) 1985, 1986 The Regents of the University of California.
54 * All rights reserved.
56 * This code is derived from software contributed to Berkeley by
57 * James A. Woods, derived from original work by Spencer Thomas
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
63 * 1. Redistributions of source code must retain the above copyright
64 * notice, this list of conditions and the following disclaimer.
65 * 2. Redistributions in binary form must reproduce the above copyright
66 * notice, this list of conditions and the following disclaimer in the
67 * documentation and/or other materials provided with the distribution.
68 * 3. Neither the name of the University nor the names of its contributors
69 * may be used to endorse or promote products derived from this software
70 * without specific prior written permission.
72 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
73 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
74 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
75 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
76 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
77 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
78 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
79 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
80 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
81 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
86 * This version is for use with mbufs on BSD-derived systems.
88 * $FreeBSD: src/sys/net/bsd_comp.c,v 1.11 1999/08/28 00:48:14 peter Exp $
93 #include <sys/param.h>
94 //#include <sys/systm.h>
95 #include <sys/malloc.h>
97 #include <cyg/ppp/net/ppp_defs.h>
99 #define PACKETPTR struct mbuf *
100 #include <cyg/ppp/net/ppp_comp.h>
104 * PPP "BSD compress" compression
105 * The differences between this compression and the classic BSD LZW
106 * source are obvious from the requirement that the classic code worked
107 * with files while this handles arbitrarily long streams that
108 * are broken into packets. They are:
110 * When the code size expands, a block of junk is not emitted by
111 * the compressor and not expected by the decompressor.
113 * New codes are not necessarily assigned every time an old
114 * code is output by the compressor. This is because a packet
115 * end forces a code to be emitted, but does not imply that a
116 * new sequence has been seen.
118 * The compression ratio is checked at the first end of a packet
119 * after the appropriate gap. Besides simplifying and speeding
120 * things up, this makes it more likely that the transmitter
121 * and receiver will agree when the dictionary is cleared when
122 * compression is not going well.
126 * A dictionary for doing BSD compress.
129 int totlen; /* length of this structure */
130 u_int hsize; /* size of the hash table */
131 u_char hshift; /* used in hash function */
132 u_char n_bits; /* current bits/code */
136 u_int16_t seqno; /* sequence # of next packet */
137 u_int hdrlen; /* header length to preallocate */
139 u_int maxmaxcode; /* largest valid code */
140 u_int max_ent; /* largest code in use */
141 u_int in_count; /* uncompressed bytes, aged */
142 u_int bytes_out; /* compressed bytes, aged */
143 u_int ratio; /* recent compression ratio */
144 u_int checkpoint; /* when to next check the ratio */
145 u_int clear_count; /* times dictionary cleared */
146 u_int incomp_count; /* incompressible packets */
147 u_int incomp_bytes; /* incompressible bytes */
148 u_int uncomp_count; /* uncompressed packets */
149 u_int uncomp_bytes; /* uncompressed bytes */
150 u_int comp_count; /* compressed packets */
151 u_int comp_bytes; /* compressed bytes */
152 u_int16_t *lens; /* array of lengths of codes */
154 union { /* hash value */
157 #if BYTE_ORDER == LITTLE_ENDIAN
158 u_int16_t prefix; /* preceding code */
159 u_char suffix; /* last character of new code */
163 u_char suffix; /* last character of new code */
164 u_int16_t prefix; /* preceding code */
168 u_int16_t codem1; /* output of hash table -1 */
169 u_int16_t cptr; /* map code to hash table entry */
173 #define BSD_OVHD 2 /* BSD compress overhead/packet */
174 #define BSD_INIT_BITS BSD_MIN_BITS
176 static void bsd_clear __P((struct bsd_db *db));
177 static int bsd_check __P((struct bsd_db *db));
178 static void *bsd_alloc __P((u_char *options, int opt_len, int decomp));
179 static int bsd_init __P((struct bsd_db *db, u_char *options, int opt_len,
180 int unit, int hdrlen, int mru, int debug,
182 static void *bsd_comp_alloc __P((u_char *options, int opt_len));
183 static void *bsd_decomp_alloc __P((u_char *options, int opt_len));
184 static void bsd_free __P((void *state));
185 static int bsd_comp_init __P((void *state, u_char *options, int opt_len,
186 int unit, int hdrlen, int debug));
187 static int bsd_decomp_init __P((void *state, u_char *options, int opt_len,
188 int unit, int hdrlen, int mru, int debug));
189 static int bsd_compress __P((void *state, struct mbuf **mret,
190 struct mbuf *mp, int slen, int maxolen));
191 static void bsd_incomp __P((void *state, struct mbuf *dmsg));
192 static int bsd_decompress __P((void *state, struct mbuf *cmp,
193 struct mbuf **dmpp));
194 static void bsd_reset __P((void *state));
195 static void bsd_comp_stats __P((void *state, struct compstat *stats));
198 * Procedures exported to if_ppp.c.
200 struct compressor ppp_bsd_compress = {
201 CI_BSD_COMPRESS, /* compress_proto */
202 bsd_comp_alloc, /* comp_alloc */
203 bsd_free, /* comp_free */
204 bsd_comp_init, /* comp_init */
205 bsd_reset, /* comp_reset */
206 bsd_compress, /* compress */
207 bsd_comp_stats, /* comp_stat */
208 bsd_decomp_alloc, /* decomp_alloc */
209 bsd_free, /* decomp_free */
210 bsd_decomp_init, /* decomp_init */
211 bsd_reset, /* decomp_reset */
212 bsd_decompress, /* decompress */
213 bsd_incomp, /* incomp */
214 bsd_comp_stats, /* decomp_stat */
218 * the next two codes should not be changed lightly, as they must not
219 * lie within the contiguous general code space.
221 #define CLEAR 256 /* table clear output code */
222 #define FIRST 257 /* first free entry */
225 #define MAXCODE(b) ((1 << (b)) - 1)
226 #define BADCODEM1 MAXCODE(BSD_MAX_BITS)
228 #define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
229 ^ (u_int32_t)(prefix))
230 #define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
231 + (u_int32_t)(prefix))
233 #define CHECK_GAP 10000 /* Ratio check interval */
235 #define RATIO_SCALE_LOG 8
236 #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
237 #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
240 * clear the dictionary
247 db->max_ent = FIRST-1;
248 db->n_bits = BSD_INIT_BITS;
252 db->checkpoint = CHECK_GAP;
256 * If the dictionary is full, then see if it is time to reset it.
258 * Compute the compression ratio using fixed-point arithmetic
259 * with 8 fractional bits.
261 * Since we have an infinite stream instead of a single file,
262 * watch only the local compression ratio.
264 * Since both peers must reset the dictionary at the same time even in
265 * the absence of CLEAR codes (while packets are incompressible), they
266 * must compute the same ratio.
268 static int /* 1=output CLEAR */
274 if (db->in_count >= db->checkpoint) {
275 /* age the ratio by limiting the size of the counts */
276 if (db->in_count >= RATIO_MAX
277 || db->bytes_out >= RATIO_MAX) {
278 db->in_count -= db->in_count/4;
279 db->bytes_out -= db->bytes_out/4;
282 db->checkpoint = db->in_count + CHECK_GAP;
284 if (db->max_ent >= db->maxmaxcode) {
285 /* Reset the dictionary only if the ratio is worse,
286 * or if it looks as if it has been poisoned
287 * by incompressible data.
289 * This does not overflow, because
290 * db->in_count <= RATIO_MAX.
292 new_ratio = db->in_count << RATIO_SCALE_LOG;
293 if (db->bytes_out != 0)
294 new_ratio /= db->bytes_out;
296 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
300 db->ratio = new_ratio;
310 bsd_comp_stats(state, stats)
312 struct compstat *stats;
314 struct bsd_db *db = (struct bsd_db *) state;
317 stats->unc_bytes = db->uncomp_bytes;
318 stats->unc_packets = db->uncomp_count;
319 stats->comp_bytes = db->comp_bytes;
320 stats->comp_packets = db->comp_count;
321 stats->inc_bytes = db->incomp_bytes;
322 stats->inc_packets = db->incomp_count;
323 stats->ratio = db->in_count;
325 if (stats->ratio <= 0x7fffff)
334 * Reset state, as on a CCP ResetReq.
340 struct bsd_db *db = (struct bsd_db *) state;
348 * Allocate space for a (de) compressor.
351 bsd_alloc(options, opt_len, decomp)
356 u_int newlen, hsize, hshift, maxmaxcode;
359 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
360 || options[1] != CILEN_BSD_COMPRESS
361 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
363 bits = BSD_NBITS(options[2]);
365 case 9: /* needs 82152 for both directions */
366 case 10: /* needs 84144 */
367 case 11: /* needs 88240 */
368 case 12: /* needs 96432 */
372 case 13: /* needs 176784 */
376 case 14: /* needs 353744 */
380 case 15: /* needs 691440 */
384 case 16: /* needs 1366160--far too much, */
385 /* hsize = 69001; */ /* and 69001 is too big for cptr */
386 /* hshift = 8; */ /* in struct bsd_db */
392 maxmaxcode = MAXCODE(bits);
393 newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
394 MALLOC(db, struct bsd_db *, newlen, M_DEVBUF, M_NOWAIT);
397 bzero(db, sizeof(*db) - sizeof(db->dict));
402 MALLOC(db->lens, u_int16_t *, (maxmaxcode+1) * sizeof(db->lens[0]),
413 db->maxmaxcode = maxmaxcode;
423 struct bsd_db *db = (struct bsd_db *) state;
426 FREE(db->lens, M_DEVBUF);
431 bsd_comp_alloc(options, opt_len)
435 return bsd_alloc(options, opt_len, 0);
439 bsd_decomp_alloc(options, opt_len)
443 return bsd_alloc(options, opt_len, 1);
447 * Initialize the database.
450 bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp)
453 int opt_len, unit, hdrlen, mru, debug, decomp;
457 if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
458 || options[1] != CILEN_BSD_COMPRESS
459 || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
460 || BSD_NBITS(options[2]) != db->maxbits
461 || (decomp && db->lens == NULL))
471 db->dict[--i].codem1 = BADCODEM1;
472 db->dict[i].cptr = 0;
489 bsd_comp_init(state, options, opt_len, unit, hdrlen, debug)
492 int opt_len, unit, hdrlen, debug;
494 return bsd_init((struct bsd_db *) state, options, opt_len,
495 unit, hdrlen, 0, debug, 0);
499 bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
502 int opt_len, unit, hdrlen, mru, debug;
504 return bsd_init((struct bsd_db *) state, options, opt_len,
505 unit, hdrlen, mru, debug, 1);
511 * One change from the BSD compress command is that when the
512 * code size expands, we do not output a bunch of padding.
515 bsd_compress(state, mret, mp, slen, maxolen)
517 struct mbuf **mret; /* return compressed mbuf chain here */
518 struct mbuf *mp; /* from here */
519 int slen; /* uncompressed length */
520 int maxolen; /* max compressed length */
522 struct bsd_db *db = (struct bsd_db *) state;
523 int hshift = db->hshift;
524 u_int max_ent = db->max_ent;
525 u_int n_bits = db->n_bits;
527 u_int32_t accm = 0, fcode;
528 struct bsd_dict *dictp;
530 int hval, disp, ent, ilen;
536 #define PUTBYTE(v) { \
540 if (wptr >= cp_end) { \
541 m->m_len = wptr - mtod(m, u_char *); \
542 MGET(m->m_next, M_DONTWAIT, MT_DATA); \
546 if (maxolen - olen > MLEN) \
547 MCLGET(m, M_DONTWAIT); \
548 wptr = mtod(m, u_char *); \
549 cp_end = wptr + M_TRAILINGSPACE(m); \
556 #define OUTPUT(ent) { \
558 accm |= ((ent) << bitno); \
560 PUTBYTE(accm >> 24); \
563 } while (bitno <= 24); \
567 * If the protocol is not in the range we're interested in,
568 * just return without compressing the packet. If it is,
569 * the protocol becomes the first byte to compress.
571 rptr = mtod(mp, u_char *);
572 ent = PPP_PROTOCOL(rptr);
573 if (ent < 0x21 || ent > 0xf9) {
578 /* Don't generate compressed packets which are larger than
579 the uncompressed packet. */
583 /* Allocate one mbuf to start with. */
584 MGET(m, M_DONTWAIT, MT_DATA);
588 if (maxolen + db->hdrlen > MLEN)
589 MCLGET(m, M_DONTWAIT);
590 m->m_data += db->hdrlen;
591 wptr = mtod(m, u_char *);
592 cp_end = wptr + M_TRAILINGSPACE(m);
594 wptr = cp_end = NULL;
597 * Copy the PPP header over, changing the protocol,
598 * and install the 2-byte packet sequence number.
601 *wptr++ = PPP_ADDRESS(rptr); /* assumes the ppp header is */
602 *wptr++ = PPP_CONTROL(rptr); /* all in one mbuf */
603 *wptr++ = 0; /* change the protocol */
605 *wptr++ = db->seqno >> 8;
612 slen = mp->m_len - PPP_HDRLEN;
619 rptr = mtod(mp, u_char *);
622 continue; /* handle 0-length buffers */
628 fcode = BSD_KEY(ent, c);
629 hval = BSD_HASH(ent, c, hshift);
630 dictp = &db->dict[hval];
632 /* Validate and then check the entry. */
633 if (dictp->codem1 >= max_ent)
635 if (dictp->f.fcode == fcode) {
636 ent = dictp->codem1+1;
637 continue; /* found (prefix,suffix) */
640 /* continue probing until a match or invalid entry */
641 disp = (hval == 0) ? 1 : hval;
644 if (hval >= db->hsize)
646 dictp = &db->dict[hval];
647 if (dictp->codem1 >= max_ent)
649 } while (dictp->f.fcode != fcode);
650 ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
654 OUTPUT(ent); /* output the prefix */
656 /* code -> hashtable */
657 if (max_ent < db->maxmaxcode) {
658 struct bsd_dict *dictp2;
659 /* expand code size if needed */
660 if (max_ent >= MAXCODE(n_bits))
661 db->n_bits = ++n_bits;
663 /* Invalidate old hash table entry using
664 * this code, and then take it over.
666 dictp2 = &db->dict[max_ent+1];
667 if (db->dict[dictp2->cptr].codem1 == max_ent)
668 db->dict[dictp2->cptr].codem1 = BADCODEM1;
670 dictp->codem1 = max_ent;
671 dictp->f.fcode = fcode;
673 db->max_ent = ++max_ent;
678 OUTPUT(ent); /* output the last code */
679 db->bytes_out += olen;
680 db->in_count += ilen;
682 ++db->bytes_out; /* count complete bytes */
685 OUTPUT(CLEAR); /* do not count the CLEAR */
688 * Pad dribble bits of last code with ones.
689 * Do not emit a completely useless byte of ones.
692 PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
695 m->m_len = wptr - mtod(m, u_char *);
700 * Increase code size if we would have without the packet
701 * boundary and as the decompressor will.
703 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
706 db->uncomp_bytes += ilen;
708 if (olen + PPP_HDRLEN + BSD_OVHD > maxolen) {
709 /* throw away the compressed stuff if it is longer than uncompressed */
715 db->incomp_bytes += ilen;
718 db->comp_bytes += olen + BSD_OVHD;
721 return olen + PPP_HDRLEN + BSD_OVHD;
728 * Update the "BSD Compress" dictionary on the receiver for
729 * incompressible data by pretending to compress the incoming data.
732 bsd_incomp(state, dmsg)
736 struct bsd_db *db = (struct bsd_db *) state;
737 u_int hshift = db->hshift;
738 u_int max_ent = db->max_ent;
739 u_int n_bits = db->n_bits;
740 struct bsd_dict *dictp;
743 u_int32_t hval, disp;
750 * If the protocol is not in the range we're interested in,
751 * just return without looking at the packet. If it is,
752 * the protocol becomes the first byte to "compress".
754 rptr = mtod(dmsg, u_char *);
755 ent = PPP_PROTOCOL(rptr);
756 if (ent < 0x21 || ent > 0xf9)
760 ilen = 1; /* count the protocol as 1 byte */
762 slen = dmsg->m_len - PPP_HDRLEN;
768 rptr = mtod(dmsg, u_char *);
776 fcode = BSD_KEY(ent, c);
777 hval = BSD_HASH(ent, c, hshift);
778 dictp = &db->dict[hval];
780 /* validate and then check the entry */
781 if (dictp->codem1 >= max_ent)
783 if (dictp->f.fcode == fcode) {
784 ent = dictp->codem1+1;
785 continue; /* found (prefix,suffix) */
788 /* continue probing until a match or invalid entry */
789 disp = (hval == 0) ? 1 : hval;
792 if (hval >= db->hsize)
794 dictp = &db->dict[hval];
795 if (dictp->codem1 >= max_ent)
797 } while (dictp->f.fcode != fcode);
798 ent = dictp->codem1+1;
799 continue; /* finally found (prefix,suffix) */
801 nomatch: /* output (count) the prefix */
804 /* code -> hashtable */
805 if (max_ent < db->maxmaxcode) {
806 struct bsd_dict *dictp2;
807 /* expand code size if needed */
808 if (max_ent >= MAXCODE(n_bits))
809 db->n_bits = ++n_bits;
811 /* Invalidate previous hash table entry
812 * assigned this code, and then take it over.
814 dictp2 = &db->dict[max_ent+1];
815 if (db->dict[dictp2->cptr].codem1 == max_ent)
816 db->dict[dictp2->cptr].codem1 = BADCODEM1;
818 dictp->codem1 = max_ent;
819 dictp->f.fcode = fcode;
821 db->max_ent = ++max_ent;
822 db->lens[max_ent] = db->lens[ent]+1;
825 } while (--slen != 0);
827 bitno += n_bits; /* output (count) the last code */
828 db->bytes_out += bitno/8;
829 db->in_count += ilen;
833 db->incomp_bytes += ilen;
835 db->uncomp_bytes += ilen;
837 /* Increase code size if we would have without the packet
838 * boundary and as the decompressor will.
840 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
846 * Decompress "BSD Compress".
848 * Because of patent problems, we return DECOMP_ERROR for errors
849 * found by inspecting the input data and for system problems, but
850 * DECOMP_FATALERROR for any errors which could possibly be said to
851 * be being detected "after" decompression. For DECOMP_ERROR,
852 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
853 * infringing a patent of Motorola's if we do, so we take CCP down
856 * Given that the frame has the correct sequence number and a good FCS,
857 * errors such as invalid codes in the input most likely indicate a
858 * bug, so we return DECOMP_FATALERROR for them in order to turn off
859 * compression, even though they are detected by inspecting the input.
862 bsd_decompress(state, cmp, dmpp)
864 struct mbuf *cmp, **dmpp;
866 struct bsd_db *db = (struct bsd_db *) state;
867 u_int max_ent = db->max_ent;
869 u_int bitno = 32; /* 1st valid bit in accm */
870 u_int n_bits = db->n_bits;
871 u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
872 struct bsd_dict *dictp;
873 int explen, i, seq, len;
874 u_int incode, oldcode, finchar;
875 u_char *p, *rptr, *wptr;
876 struct mbuf *m, *dmp, *mret;
877 int adrs, ctrl, ilen;
878 int space, codelen, extra;
881 * Save the address/control from the PPP header
882 * and then get the sequence number.
885 rptr = mtod(cmp, u_char *);
886 adrs = PPP_ADDRESS(rptr);
887 ctrl = PPP_CONTROL(rptr);
889 len = cmp->m_len - PPP_HDRLEN;
891 for (i = 0; i < 2; ++i) {
896 rptr = mtod(cmp, u_char *);
899 seq = (seq << 8) + *rptr++;
904 * Check the sequence number and give up if it differs from
905 * the value we're expecting.
907 if (seq != db->seqno) {
909 printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
910 db->unit, seq, db->seqno - 1);
916 * Allocate one mbuf to start with.
918 MGETHDR(dmp, M_DONTWAIT, MT_DATA);
924 MCLGET(dmp, M_DONTWAIT);
925 dmp->m_data += db->hdrlen;
926 wptr = mtod(dmp, u_char *);
927 space = M_TRAILINGSPACE(dmp) - PPP_HDRLEN + 1;
930 * Fill in the ppp header, but not the last byte of the protocol
931 * (that comes from the decompressed data).
936 wptr += PPP_HDRLEN - 1;
944 if (!cmp) /* quit at end of message */
946 rptr = mtod(cmp, u_char *);
949 continue; /* handle 0-length buffers */
953 * Accumulate bytes until we have a complete code.
954 * Then get the next code, relying on the 32-bit,
955 * unsigned accm to mask the result.
958 accm |= *rptr++ << bitno;
960 if (tgtbitno < bitno)
962 incode = accm >> tgtbitno;
966 if (incode == CLEAR) {
968 * The dictionary must only be cleared at
969 * the end of a packet. But there could be an
970 * empty mbuf at the end.
972 if (len > 0 || cmp->m_next != NULL) {
973 while ((cmp = cmp->m_next) != NULL)
978 printf("bsd_decomp%d: bad CLEAR\n", db->unit);
979 return DECOMP_FATALERROR; /* probably a bug */
987 if (incode > max_ent + 2 || incode > db->maxmaxcode
988 || (incode > max_ent && oldcode == CLEAR)) {
991 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
992 db->unit, incode, oldcode);
993 printf("max_ent=0x%x explen=%d seqno=%d\n",
994 max_ent, explen, db->seqno);
996 return DECOMP_FATALERROR; /* probably a bug */
999 /* Special case for KwKwK string. */
1000 if (incode > max_ent) {
1008 codelen = db->lens[finchar];
1009 explen += codelen + extra;
1010 if (explen > db->mru + 1) {
1013 printf("bsd_decomp%d: ran out of mru\n", db->unit);
1015 while ((cmp = cmp->m_next) != NULL)
1017 printf(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
1018 len, finchar, codelen, explen);
1021 return DECOMP_FATALERROR;
1025 * For simplicity, the decoded characters go in a single mbuf,
1026 * so we allocate a single extra cluster mbuf if necessary.
1028 if ((space -= codelen + extra) < 0) {
1029 dmp->m_len = wptr - mtod(dmp, u_char *);
1030 MGET(m, M_DONTWAIT, MT_DATA);
1033 return DECOMP_ERROR;
1038 MCLGET(m, M_DONTWAIT);
1039 space = M_TRAILINGSPACE(m) - (codelen + extra);
1041 /* now that's what I call *compression*. */
1043 return DECOMP_ERROR;
1046 wptr = mtod(dmp, u_char *);
1050 * Decode this code and install it in the decompressed buffer.
1052 p = (wptr += codelen);
1053 while (finchar > LAST) {
1054 dictp = &db->dict[db->dict[finchar].cptr];
1056 if (--codelen <= 0 || dictp->codem1 != finchar-1)
1059 *--p = dictp->f.hs.suffix;
1060 finchar = dictp->f.hs.prefix;
1066 printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1067 db->unit, codelen, incode, max_ent);
1070 if (extra) /* the KwKwK case again */
1074 * If not first code in a packet, and
1075 * if not out of code space, then allocate a new code.
1077 * Keep the hash table correct so it can be used
1078 * with uncompressed packets.
1080 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1081 struct bsd_dict *dictp2;
1083 u_int32_t hval, disp;
1085 fcode = BSD_KEY(oldcode,finchar);
1086 hval = BSD_HASH(oldcode,finchar,db->hshift);
1087 dictp = &db->dict[hval];
1089 /* look for a free hash table entry */
1090 if (dictp->codem1 < max_ent) {
1091 disp = (hval == 0) ? 1 : hval;
1094 if (hval >= db->hsize)
1096 dictp = &db->dict[hval];
1097 } while (dictp->codem1 < max_ent);
1101 * Invalidate previous hash table entry
1102 * assigned this code, and then take it over
1104 dictp2 = &db->dict[max_ent+1];
1105 if (db->dict[dictp2->cptr].codem1 == max_ent) {
1106 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1108 dictp2->cptr = hval;
1109 dictp->codem1 = max_ent;
1110 dictp->f.fcode = fcode;
1112 db->max_ent = ++max_ent;
1113 db->lens[max_ent] = db->lens[oldcode]+1;
1115 /* Expand code size if needed. */
1116 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1117 db->n_bits = ++n_bits;
1118 tgtbitno = 32-n_bits;
1123 dmp->m_len = wptr - mtod(dmp, u_char *);
1126 * Keep the checkpoint right so that incompressible packets
1127 * clear the dictionary at the right times.
1129 db->bytes_out += ilen;
1130 db->in_count += explen;
1131 if (bsd_check(db) && db->debug) {
1132 printf("bsd_decomp%d: peer should have cleared dictionary\n",
1137 db->comp_bytes += ilen + BSD_OVHD;
1139 db->uncomp_bytes += explen;
1147 printf("bsd_decomp%d: fell off end of chain ", db->unit);
1148 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1149 incode, finchar, db->dict[finchar].cptr, max_ent);
1150 } else if (dictp->codem1 != finchar-1) {
1151 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1152 db->unit, incode, finchar);
1153 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
1154 db->dict[finchar].cptr, dictp->codem1);
1157 return DECOMP_FATALERROR;
1160 #endif /* DO_BSD_COMPRESS */