]> git.ozlabs.org Git - ppp.git/blob - modules/deflate.c
Added updetach option.
[ppp.git] / modules / deflate.c
1 /*
2  * ppp_deflate.c - interface the zlib procedures for Deflate compression
3  * and decompression (as used by gzip) to the PPP code.
4  * This version is for use with STREAMS under SunOS 4.x, Solaris 2,
5  * SVR4, OSF/1 and AIX 4.x.
6  *
7  * Copyright (c) 1994 The Australian National University.
8  * All rights reserved.
9  *
10  * Permission to use, copy, modify, and distribute this software and its
11  * documentation is hereby granted, provided that the above copyright
12  * notice appears in all copies.  This software is provided without any
13  * warranty, express or implied. The Australian National University
14  * makes no representations about the suitability of this software for
15  * any purpose.
16  *
17  * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
18  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
19  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
20  * THE AUSTRALIAN NATIONAL UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY
21  * OF SUCH DAMAGE.
22  *
23  * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
24  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
26  * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
27  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
28  * OR MODIFICATIONS.
29  *
30  * $Id: deflate.c,v 1.8 1998/03/24 23:52:31 paulus Exp $
31  */
32
33 #ifdef AIX4
34 #include <net/net_globals.h>
35 #endif
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/stream.h>
39 #include <net/ppp_defs.h>
40 #include "ppp_mod.h"
41
42 #define PACKETPTR       mblk_t *
43 #include <net/ppp-comp.h>
44
45 #ifdef __osf__
46 #include "zlib.h"
47 #else
48 #include "../common/zlib.h"
49 #endif
50
51 #if DO_DEFLATE
52
53 #define DEFLATE_DEBUG   1
54
55 /*
56  * State for a Deflate (de)compressor.
57  */
58 struct deflate_state {
59     int         seqno;
60     int         w_size;
61     int         unit;
62     int         hdrlen;
63     int         mru;
64     int         debug;
65     z_stream    strm;
66     struct compstat stats;
67 };
68
69 #define DEFLATE_OVHD    2               /* Deflate overhead/packet */
70
71 static void     *z_alloc __P((void *, u_int items, u_int size));
72 static void     *z_alloc_init __P((void *, u_int items, u_int size));
73 static void     z_free __P((void *, void *ptr));
74 static void     *z_comp_alloc __P((u_char *options, int opt_len));
75 static void     *z_decomp_alloc __P((u_char *options, int opt_len));
76 static void     z_comp_free __P((void *state));
77 static void     z_decomp_free __P((void *state));
78 static int      z_comp_init __P((void *state, u_char *options, int opt_len,
79                                  int unit, int hdrlen, int debug));
80 static int      z_decomp_init __P((void *state, u_char *options, int opt_len,
81                                      int unit, int hdrlen, int mru, int debug));
82 static int      z_compress __P((void *state, mblk_t **mret,
83                                   mblk_t *mp, int slen, int maxolen));
84 static void     z_incomp __P((void *state, mblk_t *dmsg));
85 static int      z_decompress __P((void *state, mblk_t *cmp,
86                                     mblk_t **dmpp));
87 static void     z_comp_reset __P((void *state));
88 static void     z_decomp_reset __P((void *state));
89 static void     z_comp_stats __P((void *state, struct compstat *stats));
90
91 /*
92  * Procedures exported to ppp_comp.c.
93  */
94 struct compressor ppp_deflate = {
95     CI_DEFLATE,                 /* compress_proto */
96     z_comp_alloc,               /* comp_alloc */
97     z_comp_free,                /* comp_free */
98     z_comp_init,                /* comp_init */
99     z_comp_reset,               /* comp_reset */
100     z_compress,                 /* compress */
101     z_comp_stats,               /* comp_stat */
102     z_decomp_alloc,             /* decomp_alloc */
103     z_decomp_free,              /* decomp_free */
104     z_decomp_init,              /* decomp_init */
105     z_decomp_reset,             /* decomp_reset */
106     z_decompress,               /* decompress */
107     z_incomp,                   /* incomp */
108     z_comp_stats,               /* decomp_stat */
109 };
110
111 struct compressor ppp_deflate_draft = {
112     CI_DEFLATE_DRAFT,           /* compress_proto */
113     z_comp_alloc,               /* comp_alloc */
114     z_comp_free,                /* comp_free */
115     z_comp_init,                /* comp_init */
116     z_comp_reset,               /* comp_reset */
117     z_compress,                 /* compress */
118     z_comp_stats,               /* comp_stat */
119     z_decomp_alloc,             /* decomp_alloc */
120     z_decomp_free,              /* decomp_free */
121     z_decomp_init,              /* decomp_init */
122     z_decomp_reset,             /* decomp_reset */
123     z_decompress,               /* decompress */
124     z_incomp,                   /* incomp */
125     z_comp_stats,               /* decomp_stat */
126 };
127
128 #define DECOMP_CHUNK    512
129
130 /*
131  * Space allocation and freeing routines for use by zlib routines.
132  */
133 struct zchunk {
134     u_int       size;
135     u_int       guard;
136 };
137
138 #define GUARD_MAGIC     0x77a6011a
139
140 static void *
141 z_alloc_init(notused, items, size)
142     void *notused;
143     u_int items, size;
144 {
145     struct zchunk *z;
146
147     size = items * size + sizeof(struct zchunk);
148 #ifdef __osf__
149     z = (struct zchunk *) ALLOC_SLEEP(size);
150 #else
151     z = (struct zchunk *) ALLOC_NOSLEEP(size);
152 #endif
153     z->size = size;
154     z->guard = GUARD_MAGIC;
155     return (void *) (z + 1);
156 }
157
158 static void *
159 z_alloc(notused, items, size)
160     void *notused;
161     u_int items, size;
162 {
163     struct zchunk *z;
164
165     size = items * size + sizeof(struct zchunk);
166     z = (struct zchunk *) ALLOC_NOSLEEP(size);
167     z->size = size;
168     z->guard = GUARD_MAGIC;
169     return (void *) (z + 1);
170 }
171
172 static void
173 z_free(notused, ptr)
174     void *notused;
175     void *ptr;
176 {
177     struct zchunk *z = ((struct zchunk *) ptr) - 1;
178
179     if (z->guard != GUARD_MAGIC) {
180         printf("ppp: z_free of corrupted chunk at %x (%x, %x)\n",
181                z, z->size, z->guard);
182         return;
183     }
184     FREE(z, z->size);
185 }
186
187 /*
188  * Allocate space for a compressor.
189  */
190 static void *
191 z_comp_alloc(options, opt_len)
192     u_char *options;
193     int opt_len;
194 {
195     struct deflate_state *state;
196     int w_size;
197
198     if (opt_len != CILEN_DEFLATE
199         || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
200         || options[1] != CILEN_DEFLATE
201         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
202         || options[3] != DEFLATE_CHK_SEQUENCE)
203         return NULL;
204     w_size = DEFLATE_SIZE(options[2]);
205     if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
206         return NULL;
207
208
209 #ifdef __osf__
210     state = (struct deflate_state *) ALLOC_SLEEP(sizeof(*state));
211 #else
212     state = (struct deflate_state *) ALLOC_NOSLEEP(sizeof(*state));
213 #endif
214
215     if (state == NULL)
216         return NULL;
217
218     state->strm.next_in = NULL;
219     state->strm.zalloc = (alloc_func) z_alloc_init;
220     state->strm.zfree = (free_func) z_free;
221     if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
222                      -w_size, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
223         FREE(state, sizeof(*state));
224         return NULL;
225     }
226
227     state->strm.zalloc = (alloc_func) z_alloc;
228     state->w_size = w_size;
229     bzero(&state->stats, sizeof(state->stats));
230     return (void *) state;
231 }
232
233 static void
234 z_comp_free(arg)
235     void *arg;
236 {
237     struct deflate_state *state = (struct deflate_state *) arg;
238
239     deflateEnd(&state->strm);
240     FREE(state, sizeof(*state));
241 }
242
243 static int
244 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
245     void *arg;
246     u_char *options;
247     int opt_len, unit, hdrlen, debug;
248 {
249     struct deflate_state *state = (struct deflate_state *) arg;
250
251     if (opt_len < CILEN_DEFLATE
252         || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
253         || options[1] != CILEN_DEFLATE
254         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
255         || DEFLATE_SIZE(options[2]) != state->w_size
256         || options[3] != DEFLATE_CHK_SEQUENCE)
257         return 0;
258
259     state->seqno = 0;
260     state->unit = unit;
261     state->hdrlen = hdrlen;
262     state->debug = debug;
263
264     deflateReset(&state->strm);
265
266     return 1;
267 }
268
269 static void
270 z_comp_reset(arg)
271     void *arg;
272 {
273     struct deflate_state *state = (struct deflate_state *) arg;
274
275     state->seqno = 0;
276     deflateReset(&state->strm);
277 }
278
279 static int
280 z_compress(arg, mret, mp, orig_len, maxolen)
281     void *arg;
282     mblk_t **mret;              /* compressed packet (out) */
283     mblk_t *mp;         /* uncompressed packet (in) */
284     int orig_len, maxolen;
285 {
286     struct deflate_state *state = (struct deflate_state *) arg;
287     u_char *rptr, *wptr;
288     int proto, olen, wspace, r, flush;
289     mblk_t *m;
290
291     /*
292      * Check that the protocol is in the range we handle.
293      */
294     *mret = NULL;
295     rptr = mp->b_rptr;
296     if (rptr + PPP_HDRLEN > mp->b_wptr) {
297         if (!pullupmsg(mp, PPP_HDRLEN))
298             return 0;
299         rptr = mp->b_rptr;
300     }
301     proto = PPP_PROTOCOL(rptr);
302     if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
303         return orig_len;
304
305     /* Allocate one mblk initially. */
306     if (maxolen > orig_len)
307         maxolen = orig_len;
308     if (maxolen <= PPP_HDRLEN + 2) {
309         wspace = 0;
310         m = NULL;
311     } else {
312         wspace = maxolen + state->hdrlen;
313         if (wspace > 4096)
314             wspace = 4096;
315         m = allocb(wspace, BPRI_MED);
316     }
317     if (m != NULL) {
318         *mret = m;
319         if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
320             m->b_rptr += state->hdrlen;
321             m->b_wptr = m->b_rptr;
322             wspace -= state->hdrlen;
323         }
324         wptr = m->b_wptr;
325
326         /*
327          * Copy over the PPP header and store the 2-byte sequence number.
328          */
329         wptr[0] = PPP_ADDRESS(rptr);
330         wptr[1] = PPP_CONTROL(rptr);
331         wptr[2] = PPP_COMP >> 8;
332         wptr[3] = PPP_COMP;
333         wptr += PPP_HDRLEN;
334         wptr[0] = state->seqno >> 8;
335         wptr[1] = state->seqno;
336         wptr += 2;
337         state->strm.next_out = wptr;
338         state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
339     } else {
340         state->strm.next_out = NULL;
341         state->strm.avail_out = 1000000;
342     }
343     ++state->seqno;
344
345     rptr += (proto > 0xff)? 2: 3;       /* skip 1st proto byte if 0 */
346     state->strm.next_in = rptr;
347     state->strm.avail_in = mp->b_wptr - rptr;
348     mp = mp->b_cont;
349     flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
350     olen = 0;
351     for (;;) {
352         r = deflate(&state->strm, flush);
353         if (r != Z_OK) {
354             printf("z_compress: deflate returned %d (%s)\n",
355                    r, (state->strm.msg? state->strm.msg: ""));
356             break;
357         }
358         if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
359             break;              /* all done */
360         if (state->strm.avail_in == 0 && mp != NULL) {
361             state->strm.next_in = mp->b_rptr;
362             state->strm.avail_in = mp->b_wptr - mp->b_rptr;
363             mp = mp->b_cont;
364             if (mp == NULL)
365                 flush = Z_PACKET_FLUSH;
366         }
367         if (state->strm.avail_out == 0) {
368             if (m != NULL) {
369                 m->b_wptr += wspace;
370                 olen += wspace;
371                 wspace = maxolen - olen;
372                 if (wspace <= 0) {
373                     wspace = 0;
374                     m->b_cont = NULL;
375                 } else {
376                     if (wspace < 32)
377                         wspace = 32;
378                     else if (wspace > 4096)
379                         wspace = 4096;
380                     m->b_cont = allocb(wspace, BPRI_MED);
381                 }
382                 m = m->b_cont;
383                 if (m != NULL) {
384                     state->strm.next_out = m->b_wptr;
385                     state->strm.avail_out = wspace;
386                 }
387             }
388             if (m == NULL) {
389                 state->strm.next_out = NULL;
390                 state->strm.avail_out = 1000000;
391             }
392         }
393     }
394     if (m != NULL) {
395         m->b_wptr += wspace - state->strm.avail_out;
396         olen += wspace - state->strm.avail_out;
397     }
398
399     /*
400      * See if we managed to reduce the size of the packet.
401      */
402     if (olen < orig_len && m != NULL) {
403         state->stats.comp_bytes += olen;
404         state->stats.comp_packets++;
405     } else {
406         if (*mret != NULL) {
407             freemsg(*mret);
408             *mret = NULL;
409         }
410         state->stats.inc_bytes += orig_len;
411         state->stats.inc_packets++;
412         olen = orig_len;
413     }
414     state->stats.unc_bytes += orig_len;
415     state->stats.unc_packets++;
416
417     return olen;
418 }
419
420 static void
421 z_comp_stats(arg, stats)
422     void *arg;
423     struct compstat *stats;
424 {
425     struct deflate_state *state = (struct deflate_state *) arg;
426     u_int out;
427
428     *stats = state->stats;
429     stats->ratio = stats->unc_bytes;
430     out = stats->comp_bytes + stats->unc_bytes;
431     if (stats->ratio <= 0x7ffffff)
432         stats->ratio <<= 8;
433     else
434         out >>= 8;
435     if (out != 0)
436         stats->ratio /= out;
437 }
438
439 /*
440  * Allocate space for a decompressor.
441  */
442 static void *
443 z_decomp_alloc(options, opt_len)
444     u_char *options;
445     int opt_len;
446 {
447     struct deflate_state *state;
448     int w_size;
449
450     if (opt_len != CILEN_DEFLATE
451         || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
452         || options[1] != CILEN_DEFLATE
453         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
454         || options[3] != DEFLATE_CHK_SEQUENCE)
455         return NULL;
456     w_size = DEFLATE_SIZE(options[2]);
457     if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
458         return NULL;
459
460 #ifdef __osf__
461     state = (struct deflate_state *) ALLOC_SLEEP(sizeof(*state));
462 #else
463     state = (struct deflate_state *) ALLOC_NOSLEEP(sizeof(*state));
464 #endif
465     if (state == NULL)
466         return NULL;
467
468     state->strm.next_out = NULL;
469     state->strm.zalloc = (alloc_func) z_alloc_init;
470     state->strm.zfree = (free_func) z_free;
471     if (inflateInit2(&state->strm, -w_size) != Z_OK) {
472         FREE(state, sizeof(*state));
473         return NULL;
474     }
475
476     state->strm.zalloc = (alloc_func) z_alloc;
477     state->w_size = w_size;
478     bzero(&state->stats, sizeof(state->stats));
479     return (void *) state;
480 }
481
482 static void
483 z_decomp_free(arg)
484     void *arg;
485 {
486     struct deflate_state *state = (struct deflate_state *) arg;
487
488     inflateEnd(&state->strm);
489     FREE(state, sizeof(*state));
490 }
491
492 static int
493 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
494     void *arg;
495     u_char *options;
496     int opt_len, unit, hdrlen, mru, debug;
497 {
498     struct deflate_state *state = (struct deflate_state *) arg;
499
500     if (opt_len < CILEN_DEFLATE
501         || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
502         || options[1] != CILEN_DEFLATE
503         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
504         || DEFLATE_SIZE(options[2]) != state->w_size
505         || options[3] != DEFLATE_CHK_SEQUENCE)
506         return 0;
507
508     state->seqno = 0;
509     state->unit = unit;
510     state->hdrlen = hdrlen;
511     state->debug = debug;
512     state->mru = mru;
513
514     inflateReset(&state->strm);
515
516     return 1;
517 }
518
519 static void
520 z_decomp_reset(arg)
521     void *arg;
522 {
523     struct deflate_state *state = (struct deflate_state *) arg;
524
525     state->seqno = 0;
526     inflateReset(&state->strm);
527 }
528
529 /*
530  * Decompress a Deflate-compressed packet.
531  *
532  * Because of patent problems, we return DECOMP_ERROR for errors
533  * found by inspecting the input data and for system problems, but
534  * DECOMP_FATALERROR for any errors which could possibly be said to
535  * be being detected "after" decompression.  For DECOMP_ERROR,
536  * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
537  * infringing a patent of Motorola's if we do, so we take CCP down
538  * instead.
539  *
540  * Given that the frame has the correct sequence number and a good FCS,
541  * errors such as invalid codes in the input most likely indicate a
542  * bug, so we return DECOMP_FATALERROR for them in order to turn off
543  * compression, even though they are detected by inspecting the input.
544  */
545 static int
546 z_decompress(arg, mi, mop)
547     void *arg;
548     mblk_t *mi, **mop;
549 {
550     struct deflate_state *state = (struct deflate_state *) arg;
551     mblk_t *mo, *mo_head;
552     u_char *rptr, *wptr;
553     int rlen, olen, ospace;
554     int seq, i, flush, r, decode_proto;
555     u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
556
557     *mop = NULL;
558     rptr = mi->b_rptr;
559     for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
560         while (rptr >= mi->b_wptr) {
561             mi = mi->b_cont;
562             if (mi == NULL)
563                 return DECOMP_ERROR;
564             rptr = mi->b_rptr;
565         }
566         hdr[i] = *rptr++;
567     }
568
569     /* Check the sequence number. */
570     seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
571     if (seq != state->seqno) {
572 #if !DEFLATE_DEBUG
573         if (state->debug)
574 #endif
575             printf("z_decompress%d: bad seq # %d, expected %d\n",
576                    state->unit, seq, state->seqno);
577         return DECOMP_ERROR;
578     }
579     ++state->seqno;
580
581     /* Allocate an output message block. */
582     mo = allocb(DECOMP_CHUNK + state->hdrlen, BPRI_MED);
583     if (mo == NULL)
584         return DECOMP_ERROR;
585     mo_head = mo;
586     mo->b_cont = NULL;
587     mo->b_rptr += state->hdrlen;
588     mo->b_wptr = wptr = mo->b_rptr;
589     ospace = DECOMP_CHUNK;
590
591     /*
592      * Fill in the first part of the PPP header.  The protocol field
593      * comes from the decompressed data.
594      */
595     wptr[0] = PPP_ADDRESS(hdr);
596     wptr[1] = PPP_CONTROL(hdr);
597     wptr[2] = 0;
598
599     /*
600      * Set up to call inflate.  We set avail_out to 1 initially so we can
601      * look at the first byte of the output and decide whether we have
602      * a 1-byte or 2-byte protocol field.
603      */
604     state->strm.next_in = rptr;
605     state->strm.avail_in = mi->b_wptr - rptr;
606     mi = mi->b_cont;
607     flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
608     rlen = state->strm.avail_in + PPP_HDRLEN + DEFLATE_OVHD;
609     state->strm.next_out = wptr + 3;
610     state->strm.avail_out = 1;
611     decode_proto = 1;
612     olen = PPP_HDRLEN;
613
614     /*
615      * Call inflate, supplying more input or output as needed.
616      */
617     for (;;) {
618         r = inflate(&state->strm, flush);
619         if (r != Z_OK) {
620 #if !DEFLATE_DEBUG
621             if (state->debug)
622 #endif
623                 printf("z_decompress%d: inflate returned %d (%s)\n",
624                        state->unit, r, (state->strm.msg? state->strm.msg: ""));
625             freemsg(mo_head);
626             return DECOMP_FATALERROR;
627         }
628         if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
629             break;              /* all done */
630         if (state->strm.avail_in == 0 && mi != NULL) {
631             state->strm.next_in = mi->b_rptr;
632             state->strm.avail_in = mi->b_wptr - mi->b_rptr;
633             rlen += state->strm.avail_in;
634             mi = mi->b_cont;
635             if (mi == NULL)
636                 flush = Z_PACKET_FLUSH;
637         }
638         if (state->strm.avail_out == 0) {
639             if (decode_proto) {
640                 state->strm.avail_out = ospace - PPP_HDRLEN;
641                 if ((wptr[3] & 1) == 0) {
642                     /* 2-byte protocol field */
643                     wptr[2] = wptr[3];
644                     --state->strm.next_out;
645                     ++state->strm.avail_out;
646                     --olen;
647                 }
648                 decode_proto = 0;
649             } else {
650                 mo->b_wptr += ospace;
651                 olen += ospace;
652                 mo->b_cont = allocb(DECOMP_CHUNK, BPRI_MED);
653                 mo = mo->b_cont;
654                 if (mo == NULL) {
655                     freemsg(mo_head);
656                     return DECOMP_ERROR;
657                 }
658                 state->strm.next_out = mo->b_rptr;
659                 state->strm.avail_out = ospace = DECOMP_CHUNK;
660             }
661         }
662     }
663     if (decode_proto) {
664         freemsg(mo_head);
665         return DECOMP_ERROR;
666     }
667     mo->b_wptr += ospace - state->strm.avail_out;
668     olen += ospace - state->strm.avail_out;
669
670 #if DEFLATE_DEBUG
671     if (olen > state->mru + PPP_HDRLEN)
672         printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
673                state->unit, olen, state->mru + PPP_HDRLEN);
674 #endif
675
676     state->stats.unc_bytes += olen;
677     state->stats.unc_packets++;
678     state->stats.comp_bytes += rlen;
679     state->stats.comp_packets++;
680
681     *mop = mo_head;
682     return DECOMP_OK;
683 }
684
685 /*
686  * Incompressible data has arrived - add it to the history.
687  */
688 static void
689 z_incomp(arg, mi)
690     void *arg;
691     mblk_t *mi;
692 {
693     struct deflate_state *state = (struct deflate_state *) arg;
694     u_char *rptr;
695     int rlen, proto, r;
696
697     /*
698      * Check that the protocol is one we handle.
699      */
700     rptr = mi->b_rptr;
701     if (rptr + PPP_HDRLEN > mi->b_wptr) {
702         if (!pullupmsg(mi, PPP_HDRLEN))
703             return;
704         rptr = mi->b_rptr;
705     }
706     proto = PPP_PROTOCOL(rptr);
707     if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
708         return;
709
710     ++state->seqno;
711
712     /*
713      * Iterate through the message blocks, adding the characters in them
714      * to the decompressor's history.  For the first block, we start
715      * at the either the 1st or 2nd byte of the protocol field,
716      * depending on whether the protocol value is compressible.
717      */
718     rlen = mi->b_wptr - mi->b_rptr;
719     state->strm.next_in = rptr + 3;
720     state->strm.avail_in = rlen - 3;
721     if (proto > 0xff) {
722         --state->strm.next_in;
723         ++state->strm.avail_in;
724     }
725     for (;;) {
726         r = inflateIncomp(&state->strm);
727         if (r != Z_OK) {
728             /* gak! */
729 #if !DEFLATE_DEBUG
730             if (state->debug)
731 #endif
732                 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
733                        state->unit, r, (state->strm.msg? state->strm.msg: ""));
734             return;
735         }
736         mi = mi->b_cont;
737         if (mi == NULL)
738             break;
739         state->strm.next_in = mi->b_rptr;
740         state->strm.avail_in = mi->b_wptr - mi->b_rptr;
741         rlen += state->strm.avail_in;
742     }
743
744     /*
745      * Update stats.
746      */
747     state->stats.inc_bytes += rlen;
748     state->stats.inc_packets++;
749     state->stats.unc_bytes += rlen;
750     state->stats.unc_packets++;
751 }
752
753 #endif /* DO_DEFLATE */