]> git.ozlabs.org Git - ppp.git/blob - modules/bsd-comp.c
added ppp_ddinfo and SIOCGPPPCSTATS
[ppp.git] / modules / bsd-comp.c
1 /* Because this code is derived from the 4.3BSD compress source:
2  *
3  *
4  * Copyright (c) 1985, 1986 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * James A. Woods, derived from original work by Spencer Thomas
9  * and Joseph Orost.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39
40 /*
41  * This version is for use with STREAMS under SunOS 4.x,
42  * DEC Alpha OSF/1, and AIX 4.x.
43  *
44  * $Id: bsd-comp.c,v 1.9 1995/04/28 06:13:56 paulus Exp $
45  */
46
47 #ifdef __aix4__
48 #include <net/net_globals.h>
49 #endif
50 #include <sys/param.h>
51 #include <sys/types.h>
52 #include <sys/stream.h>
53 #include <sys/socket.h>
54 #include <net/if.h>
55 #include <net/ppp_defs.h>
56 #include <net/ppp_str.h>
57
58 #ifdef sun
59 #include <sys/kmem_alloc.h>
60 #define ALLOCATE(n)     kmem_alloc((n), KMEM_NOSLEEP)
61 #define FREE(p, n)      kmem_free((p), (n))
62 #endif
63
64 #ifdef __osf__
65 #include <kern/kalloc.h>
66 #define ALLOCATE(n)     kalloc((n))
67 #define FREE(p, n)      kfree((p), (n))
68 #endif
69
70 #ifdef __aix4__
71 #define ALLOCATE(n)     xmalloc((n), 0, pinned_heap)
72 #define FREE(p, n)      xmfree((p), pinned_heap)
73 #endif
74
75 #define PACKETPTR       mblk_t *
76 #include <net/ppp-comp.h>
77
78 /*
79  * PPP "BSD compress" compression
80  *  The differences between this compression and the classic BSD LZW
81  *  source are obvious from the requirement that the classic code worked
82  *  with files while this handles arbitrarily long streams that
83  *  are broken into packets.  They are:
84  *
85  *      When the code size expands, a block of junk is not emitted by
86  *          the compressor and not expected by the decompressor.
87  *
88  *      New codes are not necessarily assigned every time an old
89  *          code is output by the compressor.  This is because a packet
90  *          end forces a code to be emitted, but does not imply that a
91  *          new sequence has been seen.
92  *
93  *      The compression ratio is checked at the first end of a packet
94  *          after the appropriate gap.  Besides simplifying and speeding
95  *          things up, this makes it more likely that the transmitter
96  *          and receiver will agree when the dictionary is cleared when
97  *          compression is not going well.
98  */
99
100 /*
101  * A dictionary for doing BSD compress.
102  */
103 struct bsd_db {
104     int     totlen;                     /* length of this structure */
105     u_int   hsize;                      /* size of the hash table */
106     u_char  hshift;                     /* used in hash function */
107     u_char  n_bits;                     /* current bits/code */
108     u_char  maxbits;
109     u_char  debug;
110     u_char  unit;
111     u_short seqno;                      /* sequence number of next packet */
112     u_int   hdrlen;                     /* header length to preallocate */
113     u_int   mru;
114     u_int   maxmaxcode;                 /* largest valid code */
115     u_int   max_ent;                    /* largest code in use */
116     u_int   in_count;                   /* uncompressed bytes, aged */
117     u_int   bytes_out;                  /* compressed bytes, aged */
118     u_int   ratio;                      /* recent compression ratio */
119     u_int   checkpoint;                 /* when to next check the ratio */
120     u_int   clear_count;                /* times dictionary cleared */
121     u_int   incomp_count;               /* incompressible packets */
122     u_int   incomp_bytes;               /* incompressible bytes */
123     u_int   uncomp_count;               /* uncompressed packets */
124     u_int   uncomp_bytes;               /* uncompressed bytes */
125     u_int   comp_count;                 /* compressed packets */
126     u_int   comp_bytes;                 /* compressed bytes */
127     u_short *lens;                      /* array of lengths of codes */
128     struct bsd_dict {
129         union {                         /* hash value */
130             u_int32_t   fcode;
131             struct {
132 #ifdef BSD_LITTLE_ENDIAN
133                 u_short prefix;         /* preceding code */
134                 u_char  suffix;         /* last character of new code */
135                 u_char  pad;
136 #else
137                 u_char  pad;
138                 u_char  suffix;         /* last character of new code */
139                 u_short prefix;         /* preceding code */
140 #endif
141             } hs;
142         } f;
143         u_short codem1;                 /* output of hash table -1 */
144         u_short cptr;                   /* map code to hash table entry */
145     } dict[1];
146 };
147
148 #define BSD_OVHD        2               /* BSD compress overhead/packet */
149 #define BSD_INIT_BITS   MIN_BSD_BITS
150
151 static void     *bsd_comp_alloc __P((u_char *options, int opt_len));
152 static void     *bsd_decomp_alloc __P((u_char *options, int opt_len));
153 static void     bsd_free __P((void *state));
154 static int      bsd_comp_init __P((void *state, u_char *options, int opt_len,
155                                    int unit, int hdrlen, int debug));
156 static int      bsd_decomp_init __P((void *state, u_char *options, int opt_len,
157                                      int unit, int hdrlen, int mru, int debug));
158 static int      bsd_compress __P((void *state, mblk_t **mret,
159                                   mblk_t *mp, int slen, int maxolen));
160 static void     bsd_incomp __P((void *state, mblk_t *dmsg));
161 static int      bsd_decompress __P((void *state, mblk_t *cmp, mblk_t **dmpp));
162 static void     bsd_reset __P((void *state));
163 static void     bsd_comp_stats __P((void *state, struct compstat *stats));
164
165 /*
166  * Procedures exported to ppp_comp.c.
167  */
168 struct compressor ppp_bsd_compress = {
169     CI_BSD_COMPRESS,            /* compress_proto */
170     bsd_comp_alloc,             /* comp_alloc */
171     bsd_free,                   /* comp_free */
172     bsd_comp_init,              /* comp_init */
173     bsd_reset,                  /* comp_reset */
174     bsd_compress,               /* compress */
175     bsd_comp_stats,             /* comp_stat */
176     bsd_decomp_alloc,           /* decomp_alloc */
177     bsd_free,                   /* decomp_free */
178     bsd_decomp_init,            /* decomp_init */
179     bsd_reset,                  /* decomp_reset */
180     bsd_decompress,             /* decompress */
181     bsd_incomp,                 /* incomp */
182     bsd_comp_stats,             /* decomp_stat */
183 };
184
185 /*
186  * the next two codes should not be changed lightly, as they must not
187  * lie within the contiguous general code space.
188  */
189 #define CLEAR   256                     /* table clear output code */
190 #define FIRST   257                     /* first free entry */
191 #define LAST    255
192
193 #define MAXCODE(b)      ((1 << (b)) - 1)
194 #define BADCODEM1       MAXCODE(BSD_MAX_BITS)
195
196 #define BSD_HASH(prefix,suffix,hshift)  ((((u_int32_t)(suffix)) << (hshift)) \
197                                          ^ (u_int32_t)(prefix))
198 #define BSD_KEY(prefix,suffix)          ((((u_int32_t)(suffix)) << 16) \
199                                          + (u_int32_t)(prefix))
200
201 #define CHECK_GAP       10000           /* Ratio check interval */
202
203 #define RATIO_SCALE_LOG 8
204 #define RATIO_SCALE     (1<<RATIO_SCALE_LOG)
205 #define RATIO_MAX       (0x7fffffff>>RATIO_SCALE_LOG)
206
207 #define DECOMP_CHUNK    256
208
209 /*
210  * clear the dictionary
211  */
212 static void
213 bsd_clear(db)
214     struct bsd_db *db;
215 {
216     db->clear_count++;
217     db->max_ent = FIRST-1;
218     db->n_bits = BSD_INIT_BITS;
219     db->ratio = 0;
220     db->bytes_out = 0;
221     db->in_count = 0;
222     db->incomp_count = 0;
223     db->checkpoint = CHECK_GAP;
224 }
225
226 /*
227  * If the dictionary is full, then see if it is time to reset it.
228  *
229  * Compute the compression ratio using fixed-point arithmetic
230  * with 8 fractional bits.
231  *
232  * Since we have an infinite stream instead of a single file,
233  * watch only the local compression ratio.
234  *
235  * Since both peers must reset the dictionary at the same time even in
236  * the absence of CLEAR codes (while packets are incompressible), they
237  * must compute the same ratio.
238  */
239 static int                              /* 1=output CLEAR */
240 bsd_check(db)
241     struct bsd_db *db;
242 {
243     u_int new_ratio;
244
245     if (db->in_count >= db->checkpoint) {
246         /* age the ratio by limiting the size of the counts */
247         if (db->in_count >= RATIO_MAX
248             || db->bytes_out >= RATIO_MAX) {
249             db->in_count -= db->in_count/4;
250             db->bytes_out -= db->bytes_out/4;
251         }
252
253         db->checkpoint = db->in_count + CHECK_GAP;
254
255         if (db->max_ent >= db->maxmaxcode) {
256             /* Reset the dictionary only if the ratio is worse,
257              * or if it looks as if it has been poisoned
258              * by incompressible data.
259              *
260              * This does not overflow, because
261              *  db->in_count <= RATIO_MAX.
262              */
263             new_ratio = db->in_count << RATIO_SCALE_LOG;
264             if (db->bytes_out != 0)
265                 new_ratio /= db->bytes_out;
266
267             if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
268                 bsd_clear(db);
269                 return 1;
270             }
271             db->ratio = new_ratio;
272         }
273     }
274     return 0;
275 }
276
277 /*
278  * Return statistics.
279  */
280 static void
281 bsd_comp_stats(state, stats)
282     void *state;
283     struct compstat *stats;
284 {
285     struct bsd_db *db = (struct bsd_db *) state;
286     u_int out;
287
288     stats->unc_bytes = db->uncomp_bytes;
289     stats->unc_packets = db->uncomp_count;
290     stats->comp_bytes = db->comp_bytes;
291     stats->comp_packets = db->comp_count;
292     stats->inc_bytes = db->incomp_bytes;
293     stats->inc_packets = db->incomp_count;
294     stats->ratio = db->in_count;
295     out = db->bytes_out;
296     if (stats->ratio <= 0x7fffff)
297         stats->ratio <<= 8;
298     else
299         out >>= 8;
300     if (out != 0)
301         stats->ratio /= out;
302 }
303
304 /*
305  * Reset state, as on a CCP ResetReq.
306  */
307 static void
308 bsd_reset(state)
309     void *state;
310 {
311     struct bsd_db *db = (struct bsd_db *) state;
312
313     db->seqno = 0;
314     bsd_clear(db);
315     db->clear_count = 0;
316 }
317
318 /*
319  * Allocate space for a (de) compressor.
320  */
321 static void *
322 bsd_alloc(options, opt_len, decomp)
323     u_char *options;
324     int opt_len, decomp;
325 {
326     int bits;
327     u_int newlen, hsize, hshift, maxmaxcode;
328     struct bsd_db *db;
329
330     if (opt_len != 3 || options[0] != CI_BSD_COMPRESS || options[1] != 3
331         || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
332         return NULL;
333     bits = BSD_NBITS(options[2]);
334     switch (bits) {
335     case 9:                     /* needs 82152 for both directions */
336     case 10:                    /* needs 84144 */
337     case 11:                    /* needs 88240 */
338     case 12:                    /* needs 96432 */
339         hsize = 5003;
340         hshift = 4;
341         break;
342     case 13:                    /* needs 176784 */
343         hsize = 9001;
344         hshift = 5;
345         break;
346     case 14:                    /* needs 353744 */
347         hsize = 18013;
348         hshift = 6;
349         break;
350     case 15:                    /* needs 691440 */
351         hsize = 35023;
352         hshift = 7;
353         break;
354     case 16:                    /* needs 1366160--far too much, */
355         /* hsize = 69001; */    /* and 69001 is too big for cptr */
356         /* hshift = 8; */       /* in struct bsd_db */
357         /* break; */
358     default:
359         return NULL;
360     }
361
362     maxmaxcode = MAXCODE(bits);
363     newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
364     db = (struct bsd_db *) ALLOCATE(newlen);
365     if (!db)
366         return NULL;
367     bzero(db, sizeof(*db) - sizeof(db->dict));
368
369     if (!decomp) {
370         db->lens = NULL;
371     } else {
372         db->lens = (u_short *) ALLOCATE((maxmaxcode+1) * sizeof(db->lens[0]));
373         if (!db->lens) {
374             FREE(db, newlen);
375             return NULL;
376         }
377     }
378
379     db->totlen = newlen;
380     db->hsize = hsize;
381     db->hshift = hshift;
382     db->maxmaxcode = maxmaxcode;
383     db->maxbits = bits;
384
385     return (void *) db;
386 }
387
388 static void
389 bsd_free(state)
390     void *state;
391 {
392     struct bsd_db *db = (struct bsd_db *) state;
393
394     if (db->lens)
395         FREE(db->lens, (db->maxmaxcode+1) * sizeof(db->lens[0]));
396     FREE(db, db->totlen);
397 }
398
399 static void *
400 bsd_comp_alloc(options, opt_len)
401     u_char *options;
402     int opt_len;
403 {
404     return bsd_alloc(options, opt_len, 0);
405 }
406
407 static void *
408 bsd_decomp_alloc(options, opt_len)
409     u_char *options;
410     int opt_len;
411 {
412     return bsd_alloc(options, opt_len, 1);
413 }
414
415 /*
416  * Initialize the database.
417  */
418 static int
419 bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp)
420     struct bsd_db *db;
421     u_char *options;
422     int opt_len, unit, hdrlen, mru, debug, decomp;
423 {
424     int i;
425
426     if (opt_len != 3 || options[0] != CI_BSD_COMPRESS || options[1] != 3
427         || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
428         || BSD_NBITS(options[2]) != db->maxbits
429         || decomp && db->lens == NULL)
430         return 0;
431
432     if (decomp) {
433         i = LAST+1;
434         while (i != 0)
435             db->lens[--i] = 1;
436     }
437     i = db->hsize;
438     while (i != 0) {
439         db->dict[--i].codem1 = BADCODEM1;
440         db->dict[i].cptr = 0;
441     }
442
443     db->unit = unit;
444     db->hdrlen = hdrlen;
445     db->mru = mru;
446     if (debug)
447         db->debug = 1;
448
449     bsd_reset(db);
450
451     return 1;
452 }
453
454 static int
455 bsd_comp_init(state, options, opt_len, unit, hdrlen, debug)
456     void *state;
457     u_char *options;
458     int opt_len, unit, hdrlen, debug;
459 {
460     return bsd_init((struct bsd_db *) state, options, opt_len,
461                     unit, hdrlen, 0, debug, 0);
462 }
463
464 static int
465 bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
466     void *state;
467     u_char *options;
468     int opt_len, unit, hdrlen, mru, debug;
469 {
470     return bsd_init((struct bsd_db *) state, options, opt_len,
471                     unit, hdrlen, mru, debug, 1);
472 }
473
474
475 /*
476  * compress a packet
477  *      One change from the BSD compress command is that when the
478  *      code size expands, we do not output a bunch of padding.
479  *
480  * N.B. at present, we ignore the hdrlen specified in the comp_init call.
481  */
482 static int                      /* new slen */
483 bsd_compress(state, mretp, mp, slen, maxolen)
484     void *state;
485     mblk_t **mretp;             /* return compressed mbuf chain here */
486     mblk_t *mp;                 /* from here */
487     int slen;                   /* uncompressed length */
488     int maxolen;                /* max compressed length */
489 {
490     struct bsd_db *db = (struct bsd_db *) state;
491     int hshift = db->hshift;
492     u_int max_ent = db->max_ent;
493     u_int n_bits = db->n_bits;
494     u_int bitno = 32;
495     u_int32_t accm = 0, fcode;
496     struct bsd_dict *dictp;
497     u_char c;
498     int hval, disp, ent, ilen;
499     mblk_t *np, *mret;
500     u_char *rptr, *wptr;
501     u_char *cp_end;
502     int olen;
503     mblk_t *m, **mnp;
504
505 #define PUTBYTE(v) {                                    \
506     if (wptr) {                                         \
507         *wptr++ = (v);                                  \
508         if (wptr >= cp_end) {                           \
509             m->b_wptr = wptr;                           \
510             m = m->b_cont;                              \
511             if (m) {                                    \
512                 wptr = m->b_wptr;                       \
513                 cp_end = m->b_datap->db_lim;            \
514             } else                                      \
515                 wptr = NULL;                            \
516         }                                               \
517     }                                                   \
518     ++olen;                                             \
519 }
520
521 #define OUTPUT(ent) {                                   \
522     bitno -= n_bits;                                    \
523     accm |= ((ent) << bitno);                           \
524     do {                                                \
525         PUTBYTE(accm >> 24);                            \
526         accm <<= 8;                                     \
527         bitno += 8;                                     \
528     } while (bitno <= 24);                              \
529 }
530
531     /*
532      * First get the protocol and check that we're
533      * interested in this packet.
534      */
535     *mretp = NULL;
536     rptr = mp->b_rptr;
537     if (rptr + PPP_HDRLEN > mp->b_wptr) {
538         if (!pullupmsg(mp, PPP_HDRLEN))
539             return 0;
540         rptr = mp->b_rptr;
541     }
542     ent = PPP_PROTOCOL(rptr);           /* get the protocol */
543     if (ent < 0x21 || ent > 0xf9)
544         return 0;
545
546     /* Don't generate compressed packets which are larger than
547        the uncompressed packet. */
548     if (maxolen > slen)
549         maxolen = slen;
550
551     /* Allocate enough message blocks to give maxolen total space. */
552     mnp = &mret;
553     for (olen = maxolen; olen > 0; ) {
554         m = allocb((olen < 4096? olen: 4096), BPRI_MED);
555         *mnp = m;
556         if (m == NULL) {
557             if (mret != NULL) {
558                 freemsg(mret);
559                 mnp = &mret;
560             }
561             break;
562         }
563         mnp = &m->b_cont;
564         olen -= m->b_datap->db_lim - m->b_wptr;
565     }
566     *mnp = NULL;
567
568     if ((m = mret) != NULL) {
569         wptr = m->b_wptr;
570         cp_end = m->b_datap->db_lim;
571     } else
572         wptr = cp_end = NULL;
573     olen = 0;
574
575     /*
576      * Copy the PPP header over, changing the protocol,
577      * and install the 2-byte sequence number.
578      */
579     if (wptr) {
580         wptr[0] = PPP_ADDRESS(rptr);
581         wptr[1] = PPP_CONTROL(rptr);
582         wptr[2] = 0;            /* change the protocol */
583         wptr[3] = PPP_COMP;
584         wptr[4] = db->seqno >> 8;
585         wptr[5] = db->seqno;
586         wptr += PPP_HDRLEN + BSD_OVHD;
587     }
588     ++db->seqno;
589     rptr += PPP_HDRLEN;
590
591     slen = mp->b_wptr - rptr;
592     ilen = slen + 1;
593     np = mp->b_cont;
594     for (;;) {
595         if (slen <= 0) {
596             if (!np)
597                 break;
598             rptr = np->b_rptr;
599             slen = np->b_wptr - rptr;
600             np = np->b_cont;
601             if (!slen)
602                 continue;   /* handle 0-length buffers */
603             ilen += slen;
604         }
605
606         slen--;
607         c = *rptr++;
608         fcode = BSD_KEY(ent, c);
609         hval = BSD_HASH(ent, c, hshift);
610         dictp = &db->dict[hval];
611
612         /* Validate and then check the entry. */
613         if (dictp->codem1 >= max_ent)
614             goto nomatch;
615         if (dictp->f.fcode == fcode) {
616             ent = dictp->codem1+1;
617             continue;   /* found (prefix,suffix) */
618         }
619
620         /* continue probing until a match or invalid entry */
621         disp = (hval == 0) ? 1 : hval;
622         do {
623             hval += disp;
624             if (hval >= db->hsize)
625                 hval -= db->hsize;
626             dictp = &db->dict[hval];
627             if (dictp->codem1 >= max_ent)
628                 goto nomatch;
629         } while (dictp->f.fcode != fcode);
630         ent = dictp->codem1 + 1;        /* finally found (prefix,suffix) */
631         continue;
632
633     nomatch:
634         OUTPUT(ent);            /* output the prefix */
635
636         /* code -> hashtable */
637         if (max_ent < db->maxmaxcode) {
638             struct bsd_dict *dictp2;
639             /* expand code size if needed */
640             if (max_ent >= MAXCODE(n_bits))
641                 db->n_bits = ++n_bits;
642
643             /* Invalidate old hash table entry using
644              * this code, and then take it over.
645              */
646             dictp2 = &db->dict[max_ent+1];
647             if (db->dict[dictp2->cptr].codem1 == max_ent)
648                 db->dict[dictp2->cptr].codem1 = BADCODEM1;
649             dictp2->cptr = hval;
650             dictp->codem1 = max_ent;
651             dictp->f.fcode = fcode;
652
653             db->max_ent = ++max_ent;
654         }
655         ent = c;
656     }
657
658     OUTPUT(ent);                /* output the last code */
659     db->bytes_out += olen;
660     db->in_count += ilen;
661     if (bitno < 32)
662         ++db->bytes_out;        /* count complete bytes */
663
664     if (bsd_check(db))
665         OUTPUT(CLEAR);          /* do not count the CLEAR */
666
667     /*
668      * Pad dribble bits of last code with ones.
669      * Do not emit a completely useless byte of ones.
670      */
671     if (bitno != 32)
672         PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
673
674     /*
675      * Increase code size if we would have without the packet
676      * boundary and as the decompressor will.
677      */
678     if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
679         db->n_bits++;
680
681     db->uncomp_bytes += ilen;
682     ++db->uncomp_count;
683     if (olen + PPP_HDRLEN + BSD_OVHD > maxolen && mret != NULL) {
684         /* throw away the compressed stuff if it is longer than uncompressed */
685         freemsg(mret);
686         mret = NULL;
687         ++db->incomp_count;
688         db->incomp_bytes += ilen;
689     } else if (wptr != NULL) {
690         m->b_wptr = wptr;
691         if (m->b_cont) {
692             freemsg(m->b_cont);
693             m->b_cont = NULL;
694         }
695         ++db->comp_count;
696         db->comp_bytes += olen + BSD_OVHD;
697     }
698
699     *mretp = mret;
700     return olen + PPP_HDRLEN + BSD_OVHD;
701 #undef OUTPUT
702 #undef PUTBYTE
703 }
704
705
706 /*
707  * Update the "BSD Compress" dictionary on the receiver for
708  * incompressible data by pretending to compress the incoming data.
709  */
710 static void
711 bsd_incomp(state, dmsg)
712     void *state;
713     mblk_t *dmsg;
714 {
715     struct bsd_db *db = (struct bsd_db *) state;
716     u_int hshift = db->hshift;
717     u_int max_ent = db->max_ent;
718     u_int n_bits = db->n_bits;
719     struct bsd_dict *dictp;
720     u_int32_t fcode;
721     u_char c;
722     long hval, disp;
723     int slen, ilen;
724     u_int bitno = 7;
725     u_char *rptr;
726     u_int ent;
727
728     rptr = dmsg->b_rptr;
729     if (rptr + PPP_HDRLEN > dmsg->b_wptr) {
730         if (!pullupmsg(dmsg, PPP_HDRLEN))
731             return;
732         rptr = dmsg->b_rptr;
733     }
734     ent = PPP_PROTOCOL(rptr);           /* get the protocol */
735     if (ent < 0x21 || ent > 0xf9)
736         return;
737
738     db->incomp_count++;
739     db->seqno++;
740     ilen = 1;           /* count the protocol as 1 byte */
741     rptr += PPP_HDRLEN;
742     for (;;) {
743         slen = dmsg->b_wptr - rptr;
744         if (slen <= 0) {
745             dmsg = dmsg->b_cont;
746             if (!dmsg)
747                 break;
748             rptr = dmsg->b_rptr;
749             continue;           /* skip zero-length buffers */
750         }
751         ilen += slen;
752
753         do {
754             c = *rptr++;
755             fcode = BSD_KEY(ent, c);
756             hval = BSD_HASH(ent, c, hshift);
757             dictp = &db->dict[hval];
758
759             /* validate and then check the entry */
760             if (dictp->codem1 >= max_ent)
761                 goto nomatch;
762             if (dictp->f.fcode == fcode) {
763                 ent = dictp->codem1+1;
764                 continue;   /* found (prefix,suffix) */
765             }
766
767             /* continue probing until a match or invalid entry */
768             disp = (hval == 0) ? 1 : hval;
769             do {
770                 hval += disp;
771                 if (hval >= db->hsize)
772                     hval -= db->hsize;
773                 dictp = &db->dict[hval];
774                 if (dictp->codem1 >= max_ent)
775                     goto nomatch;
776             } while (dictp->f.fcode != fcode);
777             ent = dictp->codem1+1;
778             continue;   /* finally found (prefix,suffix) */
779
780         nomatch:                /* output (count) the prefix */
781             bitno += n_bits;
782
783             /* code -> hashtable */
784             if (max_ent < db->maxmaxcode) {
785                 struct bsd_dict *dictp2;
786                 /* expand code size if needed */
787                 if (max_ent >= MAXCODE(n_bits))
788                     db->n_bits = ++n_bits;
789
790                 /* Invalidate previous hash table entry
791                  * assigned this code, and then take it over.
792                  */
793                 dictp2 = &db->dict[max_ent+1];
794                 if (db->dict[dictp2->cptr].codem1 == max_ent)
795                     db->dict[dictp2->cptr].codem1 = BADCODEM1;
796                 dictp2->cptr = hval;
797                 dictp->codem1 = max_ent;
798                 dictp->f.fcode = fcode;
799
800                 db->max_ent = ++max_ent;
801                 db->lens[max_ent] = db->lens[ent]+1;
802             }
803             ent = c;
804         } while (--slen != 0);
805     }
806     bitno += n_bits;            /* output (count) the last code */
807     db->bytes_out += bitno/8;
808     db->in_count += ilen;
809     (void)bsd_check(db);
810
811     ++db->incomp_count;
812     db->incomp_bytes += ilen;
813     ++db->uncomp_count;
814     db->uncomp_bytes += ilen;
815
816     /* Increase code size if we would have without the packet
817      * boundary and as the decompressor will.
818      */
819     if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
820         db->n_bits++;
821 }
822
823
824 /*
825  * Decompress "BSD Compress"
826  *
827  * Because of patent problems, we return DECOMP_ERROR for errors
828  * found by inspecting the input data and for system problems, but
829  * DECOMP_FATALERROR for any errors which could possibly be said to
830  * be being detected "after" decompression.  For DECOMP_ERROR,
831  * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
832  * infringing a patent of Motorola's if we do, so we take CCP down
833  * instead.
834  *
835  * Given that the frame has the correct sequence number and a good FCS,
836  * errors such as invalid codes in the input most likely indicate a
837  * bug, so we return DECOMP_FATALERROR for them in order to turn off
838  * compression, even though they are detected by inspecting the input.
839  */
840 static int
841 bsd_decompress(state, cmsg, dmpp)
842     void *state;
843     mblk_t *cmsg, **dmpp;
844 {
845     struct bsd_db *db = (struct bsd_db *) state;
846     u_int max_ent = db->max_ent;
847     u_int32_t accm = 0;
848     u_int bitno = 32;           /* 1st valid bit in accm */
849     u_int n_bits = db->n_bits;
850     u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
851     struct bsd_dict *dictp;
852     int explen, i, seq, len;
853     u_int incode, oldcode, finchar;
854     u_char *p, *rptr, *wptr;
855     mblk_t *dmsg, *mret;
856     int adrs, ctrl, ilen;
857     int dlen, space, codelen, extra;
858
859     /*
860      * Get at least the BSD Compress header in the first buffer
861      */
862     rptr = cmsg->b_rptr;
863     if (rptr + PPP_HDRLEN + BSD_OVHD >= cmsg->b_wptr) {
864         if (!pullupmsg(cmsg, PPP_HDRLEN + BSD_OVHD + 1)) {
865             if (db->debug)
866                 printf("bsd_decomp%d: failed to pullup\n", db->unit);
867             return DECOMP_ERROR;
868         }
869         rptr = cmsg->b_rptr;
870     }
871
872     /*
873      * Save the address/control from the PPP header
874      * and then get the sequence number.
875      */
876     adrs = PPP_ADDRESS(rptr);
877     ctrl = PPP_CONTROL(rptr);
878     rptr += PPP_HDRLEN;
879     seq = (rptr[0] << 8) + rptr[1];
880     rptr += BSD_OVHD;
881     ilen = len = cmsg->b_wptr - rptr;
882
883     /*
884      * Check the sequence number and give up if it is not what we expect.
885      */
886     if (seq != db->seqno++) {
887         if (db->debug)
888             printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
889                    db->unit, seq, db->seqno - 1);
890         return DECOMP_ERROR;
891     }
892
893     /*
894      * Allocate one message block to start with.
895      */
896     if ((dmsg = allocb(DECOMP_CHUNK + db->hdrlen, BPRI_MED)) == NULL)
897         return DECOMP_ERROR;
898     mret = dmsg;
899     dmsg->b_wptr += db->hdrlen;
900     dmsg->b_rptr = wptr = dmsg->b_wptr;
901
902     /* Fill in the ppp header, but not the last byte of the protocol
903        (that comes from the decompressed data). */
904     wptr[0] = adrs;
905     wptr[1] = ctrl;
906     wptr[2] = 0;
907     wptr += PPP_HDRLEN - 1;
908     space = dmsg->b_datap->db_lim - wptr;
909
910     oldcode = CLEAR;
911     explen = 0;
912     for (;;) {
913         if (len == 0) {
914             cmsg = cmsg->b_cont;
915             if (!cmsg)          /* quit at end of message */
916                 break;
917             rptr = cmsg->b_rptr;
918             len = cmsg->b_wptr - rptr;
919             ilen += len;
920             continue;           /* handle 0-length buffers */
921         }
922
923         /*
924          * Accumulate bytes until we have a complete code.
925          * Then get the next code, relying on the 32-bit,
926          * unsigned accm to mask the result.
927          */
928         bitno -= 8;
929         accm |= *rptr++ << bitno;
930         --len;
931         if (tgtbitno < bitno)
932             continue;
933         incode = accm >> tgtbitno;
934         accm <<= n_bits;
935         bitno += n_bits;
936
937         if (incode == CLEAR) {
938             /*
939              * The dictionary must only be cleared at
940              * the end of a packet.  But there could be an
941              * empty message block at the end.
942              */
943             if (len > 0 || cmsg->b_cont != 0) {
944                 if (cmsg->b_cont)
945                     len += msgdsize(cmsg->b_cont);
946                 if (len > 0) {
947                     freemsg(dmsg);
948                     if (db->debug)
949                         printf("bsd_decomp%d: bad CLEAR\n", db->unit);
950                     return DECOMP_FATALERROR;
951                 }
952             }
953             bsd_clear(db);
954             explen = ilen = 0;
955             break;
956         }
957
958         if (incode > max_ent + 2 || incode > db->maxmaxcode
959             || incode > max_ent && oldcode == CLEAR) {
960             freemsg(dmsg);
961             if (db->debug) {
962                 printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
963                        db->unit, incode, oldcode);
964                 printf("max_ent=0x%x dlen=%d seqno=%d\n",
965                        max_ent, dlen, db->seqno);
966             }
967             return DECOMP_FATALERROR;   /* probably a bug */
968         }
969
970         /* Special case for KwKwK string. */
971         if (incode > max_ent) {
972             finchar = oldcode;
973             extra = 1;
974         } else {
975             finchar = incode;
976             extra = 0;
977         }
978
979         codelen = db->lens[finchar];
980         explen += codelen + extra;
981         if (explen > db->mru + 1) {
982             freemsg(dmsg);
983             if (db->debug)
984                 printf("bsd_decomp%d: ran out of mru\n", db->unit);
985             return DECOMP_FATALERROR;
986         }
987
988         /*
989          * Decode this code and install it in the decompressed buffer.
990          */
991         space -= codelen + extra;
992         if (space < 0) {
993             /* Allocate another message block. */
994             dmsg->b_wptr = wptr;
995             dlen = codelen + extra;
996             if (dlen < DECOMP_CHUNK)
997                 dlen = DECOMP_CHUNK;
998             if ((dmsg->b_cont = allocb(dlen, BPRI_MED)) == NULL) {
999                 freemsg(dmsg);
1000                 return DECOMP_ERROR;
1001             }
1002             dmsg = dmsg->b_cont;
1003             wptr = dmsg->b_wptr;
1004             space = dmsg->b_datap->db_lim - wptr - codelen - extra;
1005         }
1006         p = (wptr += codelen);
1007         while (finchar > LAST) {
1008             dictp = &db->dict[db->dict[finchar].cptr];
1009 #ifdef DEBUG
1010             --codelen;
1011             if (codelen <= 0) {
1012                 freemsg(dmsg);
1013                 printf("bsd_decomp%d: fell off end of chain ", db->unit);
1014                 printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1015                        incode, finchar, db->dict[finchar].cptr, max_ent);
1016                 return DECOMP_FATALERROR;
1017             }
1018             if (dictp->codem1 != finchar-1) {
1019                 freemsg(dmsg);
1020                 printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1021                        db->unit, incode, finchar);
1022                 printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
1023                        db->dict[finchar].cptr, dictp->codem1);
1024                 return DECOMP_FATALERROR;
1025             }
1026 #endif
1027             *--p = dictp->f.hs.suffix;
1028             finchar = dictp->f.hs.prefix;
1029         }
1030         *--p = finchar;
1031
1032 #ifdef DEBUG
1033         if (--codelen != 0)
1034             printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1035                    db->unit, codelen, incode, max_ent);
1036 #endif
1037
1038         if (extra)              /* the KwKwK case again */
1039             *wptr++ = finchar;
1040
1041         /*
1042          * If not first code in a packet, and
1043          * if not out of code space, then allocate a new code.
1044          *
1045          * Keep the hash table correct so it can be used
1046          * with uncompressed packets.
1047          */
1048         if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1049             struct bsd_dict *dictp2;
1050             u_int32_t fcode;
1051             int hval, disp;
1052
1053             fcode = BSD_KEY(oldcode,finchar);
1054             hval = BSD_HASH(oldcode,finchar,db->hshift);
1055             dictp = &db->dict[hval];
1056
1057             /* look for a free hash table entry */
1058             if (dictp->codem1 < max_ent) {
1059                 disp = (hval == 0) ? 1 : hval;
1060                 do {
1061                     hval += disp;
1062                     if (hval >= db->hsize)
1063                         hval -= db->hsize;
1064                     dictp = &db->dict[hval];
1065                 } while (dictp->codem1 < max_ent);
1066             }
1067
1068             /*
1069              * Invalidate previous hash table entry
1070              * assigned this code, and then take it over
1071              */
1072             dictp2 = &db->dict[max_ent+1];
1073             if (db->dict[dictp2->cptr].codem1 == max_ent) {
1074                 db->dict[dictp2->cptr].codem1 = BADCODEM1;
1075             }
1076             dictp2->cptr = hval;
1077             dictp->codem1 = max_ent;
1078             dictp->f.fcode = fcode;
1079
1080             db->max_ent = ++max_ent;
1081             db->lens[max_ent] = db->lens[oldcode]+1;
1082
1083             /* Expand code size if needed. */
1084             if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1085                 db->n_bits = ++n_bits;
1086                 tgtbitno = 32-n_bits;
1087             }
1088         }
1089         oldcode = incode;
1090     }
1091     dmsg->b_wptr = wptr;
1092
1093     /*
1094      * Keep the checkpoint right so that incompressible packets
1095      * clear the dictionary at the right times.
1096      */
1097     db->bytes_out += ilen;
1098     db->in_count += explen;
1099     if (bsd_check(db) && db->debug) {
1100         printf("bsd_decomp%d: peer should have cleared dictionary\n",
1101                db->unit);
1102     }
1103
1104     ++db->comp_count;
1105     db->comp_bytes += ilen + BSD_OVHD;
1106     ++db->uncomp_count;
1107     db->uncomp_bytes += explen;
1108
1109     *dmpp = mret;
1110     return DECOMP_OK;
1111 }