]> git.ozlabs.org Git - ppp.git/blob - modules/deflate.c
describe new options, don't describe obsolete ones
[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.2 1996/04/04 02:44:56 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 #include "common/zlib.h"
45
46 #if DO_DEFLATE
47
48 /*
49  * State for a Deflate (de)compressor.
50  */
51 struct deflate_state {
52     int         seqno;
53     int         w_size;
54     int         unit;
55     int         hdrlen;
56     int         mru;
57     int         debug;
58     z_stream    strm;
59     struct compstat stats;
60 };
61
62 #define DEFLATE_OVHD    2               /* Deflate overhead/packet */
63
64 static void     *zalloc __P((void *, u_int items, u_int size));
65 static void     zfree __P((void *, void *ptr, u_int nb));
66 static void     *z_comp_alloc __P((u_char *options, int opt_len));
67 static void     *z_decomp_alloc __P((u_char *options, int opt_len));
68 static void     z_comp_free __P((void *state));
69 static void     z_decomp_free __P((void *state));
70 static int      z_comp_init __P((void *state, u_char *options, int opt_len,
71                                  int unit, int hdrlen, int debug));
72 static int      z_decomp_init __P((void *state, u_char *options, int opt_len,
73                                      int unit, int hdrlen, int mru, int debug));
74 static int      z_compress __P((void *state, mblk_t **mret,
75                                   mblk_t *mp, int slen, int maxolen));
76 static void     z_incomp __P((void *state, mblk_t *dmsg));
77 static int      z_decompress __P((void *state, mblk_t *cmp,
78                                     mblk_t **dmpp));
79 static void     z_comp_reset __P((void *state));
80 static void     z_decomp_reset __P((void *state));
81 static void     z_comp_stats __P((void *state, struct compstat *stats));
82
83 /*
84  * Procedures exported to if_ppp.c.
85  */
86 struct compressor ppp_deflate = {
87     CI_DEFLATE,                 /* compress_proto */
88     z_comp_alloc,               /* comp_alloc */
89     z_comp_free,                /* comp_free */
90     z_comp_init,                /* comp_init */
91     z_comp_reset,               /* comp_reset */
92     z_compress,                 /* compress */
93     z_comp_stats,               /* comp_stat */
94     z_decomp_alloc,             /* decomp_alloc */
95     z_decomp_free,              /* decomp_free */
96     z_decomp_init,              /* decomp_init */
97     z_decomp_reset,             /* decomp_reset */
98     z_decompress,               /* decompress */
99     z_incomp,                   /* incomp */
100     z_comp_stats,               /* decomp_stat */
101 };
102
103 #define DECOMP_CHUNK    512
104
105 /*
106  * Space allocation and freeing routines for use by zlib routines.
107  */
108 static void *
109 zalloc(notused, items, size)
110     void *notused;
111     u_int items, size;
112 {
113     return ALLOC_NOSLEEP(items * size);
114 }
115
116 static void
117 zfree(notused, ptr, nbytes)
118     void *notused;
119     void *ptr;
120     u_int nbytes;
121 {
122     FREE(ptr, nbytes);
123 }
124
125 /*
126  * Allocate space for a compressor.
127  */
128 static void *
129 z_comp_alloc(options, opt_len)
130     u_char *options;
131     int opt_len;
132 {
133     struct deflate_state *state;
134     int w_size;
135
136     if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
137         || options[1] != CILEN_DEFLATE
138         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
139         || options[3] != DEFLATE_CHK_SEQUENCE)
140         return NULL;
141     w_size = DEFLATE_SIZE(options[2]);
142     if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
143         return NULL;
144
145     state = (struct deflate_state *) ALLOC_NOSLEEP(sizeof(*state));
146     if (state == NULL)
147         return NULL;
148
149     state->strm.next_in = NULL;
150     state->strm.zalloc = (alloc_func) zalloc;
151     state->strm.zfree = (free_func) zfree;
152     if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
153                      -w_size, 8, Z_DEFAULT_STRATEGY, DEFLATE_OVHD+2) != Z_OK) {
154         FREE(state, sizeof(*state));
155         return NULL;
156     }
157
158     state->w_size = w_size;
159     bzero(&state->stats, sizeof(state->stats));
160     return (void *) state;
161 }
162
163 static void
164 z_comp_free(arg)
165     void *arg;
166 {
167     struct deflate_state *state = (struct deflate_state *) arg;
168
169     deflateEnd(&state->strm);
170     FREE(state, sizeof(*state));
171 }
172
173 static int
174 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
175     void *arg;
176     u_char *options;
177     int opt_len, unit, hdrlen, debug;
178 {
179     struct deflate_state *state = (struct deflate_state *) arg;
180
181     if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
182         || options[1] != CILEN_DEFLATE
183         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
184         || DEFLATE_SIZE(options[2]) != state->w_size
185         || options[3] != DEFLATE_CHK_SEQUENCE)
186         return 0;
187
188     state->seqno = 0;
189     state->unit = unit;
190     state->hdrlen = hdrlen;
191     state->debug = debug;
192
193     deflateReset(&state->strm);
194
195     return 1;
196 }
197
198 static void
199 z_comp_reset(arg)
200     void *arg;
201 {
202     struct deflate_state *state = (struct deflate_state *) arg;
203
204     state->seqno = 0;
205     deflateReset(&state->strm);
206 }
207
208 static int
209 z_compress(arg, mret, mp, orig_len, maxolen)
210     void *arg;
211     mblk_t **mret;              /* compressed packet (out) */
212     mblk_t *mp;         /* uncompressed packet (in) */
213     int orig_len, maxolen;
214 {
215     struct deflate_state *state = (struct deflate_state *) arg;
216     u_char *rptr, *wptr;
217     int proto, olen, wspace, r, flush;
218     mblk_t *m;
219
220     /*
221      * Check that the protocol is in the range we handle.
222      */
223     *mret = NULL;
224     rptr = mp->b_rptr;
225     if (rptr + PPP_HDRLEN > mp->b_wptr) {
226         if (!pullupmsg(mp, PPP_HDRLEN))
227             return 0;
228         rptr = mp->b_rptr;
229     }
230     proto = PPP_PROTOCOL(rptr);
231     if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
232         return orig_len;
233
234     /* Allocate one mblk initially. */
235     if (maxolen > orig_len)
236         maxolen = orig_len;
237     wspace = maxolen < 4096? maxolen: 4096;
238     m = allocb(wspace, BPRI_MED);
239     if (m != NULL) {
240         *mret = m;
241         if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
242             m->b_rptr += state->hdrlen;
243             m->b_wptr = m->b_rptr;
244             wspace -= state->hdrlen;
245         }
246         wptr = m->b_wptr;
247
248         /*
249          * Copy over the PPP header and store the 2-byte sequence number.
250          */
251         wptr[0] = PPP_ADDRESS(rptr);
252         wptr[1] = PPP_CONTROL(rptr);
253         wptr[2] = PPP_COMP >> 8;
254         wptr[3] = PPP_COMP;
255         wptr += PPP_HDRLEN;
256         wptr[0] = state->seqno >> 8;
257         wptr[1] = state->seqno;
258         wptr += 2;
259         state->strm.next_out = wptr;
260         state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
261     } else {
262         state->strm.next_out = NULL;
263         state->strm.avail_out = 1000000;
264     }
265     ++state->seqno;
266
267     rptr += (proto > 0xff)? 2: 3;       /* skip 1st proto byte if 0 */
268     state->strm.next_in = rptr;
269     state->strm.avail_in = mp->b_wptr - rptr;
270     mp = mp->b_cont;
271     flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
272     olen = 0;
273     for (;;) {
274         r = deflate(&state->strm, flush);
275         if (r != Z_OK) {
276             printf("z_compress: deflate returned %d (%s)\n",
277                    r, (state->strm.msg? state->strm.msg: ""));
278             break;
279         }
280         if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
281             break;              /* all done */
282         if (state->strm.avail_in == 0 && mp != NULL) {
283             state->strm.next_in = mp->b_rptr;
284             state->strm.avail_in = mp->b_wptr - mp->b_rptr;
285             mp = mp->b_cont;
286             if (mp == NULL)
287                 flush = Z_PACKET_FLUSH;
288         }
289         if (state->strm.avail_out == 0) {
290             if (m != NULL) {
291                 m->b_wptr += wspace;
292                 olen += wspace;
293                 wspace = maxolen - olen;
294                 if (wspace < 32)
295                     wspace = 32;
296                 else if (wspace > 4096)
297                     wspace = 4096;
298                 m->b_cont = allocb(wspace, BPRI_MED);
299                 m = m->b_cont;
300                 if (m != NULL) {
301                     state->strm.next_out = m->b_wptr;
302                     state->strm.avail_out = wspace;
303                 }
304             }
305             if (m == NULL) {
306                 state->strm.next_out = NULL;
307                 state->strm.avail_out = 1000000;
308             }
309         }
310     }
311     m->b_wptr += wspace - state->strm.avail_out;
312     olen += wspace - state->strm.avail_out;
313
314     /*
315      * See if we managed to reduce the size of the packet.
316      * If the compressor just gave us a single zero byte, it means
317      * the packet was incompressible.
318      */
319     if (olen < orig_len && m != NULL
320         && !(olen == PPP_HDRLEN + 3 && *wptr == 0)) {
321         state->stats.comp_bytes += olen;
322         state->stats.comp_packets++;
323     } else {
324         if (*mret != NULL) {
325             freemsg(*mret);
326             *mret = NULL;
327         }
328         state->stats.inc_bytes += orig_len;
329         state->stats.inc_packets++;
330         olen = orig_len;
331     }
332     state->stats.unc_bytes += orig_len;
333     state->stats.unc_packets++;
334
335     return olen;
336 }
337
338 static void
339 z_comp_stats(arg, stats)
340     void *arg;
341     struct compstat *stats;
342 {
343     struct deflate_state *state = (struct deflate_state *) arg;
344     u_int out;
345
346     *stats = state->stats;
347     stats->ratio = stats->unc_bytes;
348     out = stats->comp_bytes + stats->unc_bytes;
349     if (stats->ratio <= 0x7ffffff)
350         stats->ratio <<= 8;
351     else
352         out >>= 8;
353     if (out != 0)
354         stats->ratio /= out;
355 }
356
357 /*
358  * Allocate space for a decompressor.
359  */
360 static void *
361 z_decomp_alloc(options, opt_len)
362     u_char *options;
363     int opt_len;
364 {
365     struct deflate_state *state;
366     int w_size;
367
368     if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
369         || options[1] != CILEN_DEFLATE
370         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
371         || options[3] != DEFLATE_CHK_SEQUENCE)
372         return NULL;
373     w_size = DEFLATE_SIZE(options[2]);
374     if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
375         return NULL;
376
377     state = (struct deflate_state *) ALLOC_NOSLEEP(sizeof(*state));
378     if (state == NULL)
379         return NULL;
380
381     state->strm.next_out = NULL;
382     state->strm.zalloc = (alloc_func) zalloc;
383     state->strm.zfree = (free_func) zfree;
384     if (inflateInit2(&state->strm, -w_size) != Z_OK) {
385         FREE(state, sizeof(*state));
386         return NULL;
387     }
388
389     state->w_size = w_size;
390     bzero(&state->stats, sizeof(state->stats));
391     return (void *) state;
392 }
393
394 static void
395 z_decomp_free(arg)
396     void *arg;
397 {
398     struct deflate_state *state = (struct deflate_state *) arg;
399
400     inflateEnd(&state->strm);
401     FREE(state, sizeof(*state));
402 }
403
404 static int
405 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
406     void *arg;
407     u_char *options;
408     int opt_len, unit, hdrlen, mru, debug;
409 {
410     struct deflate_state *state = (struct deflate_state *) arg;
411
412     if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
413         || options[1] != CILEN_DEFLATE
414         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
415         || DEFLATE_SIZE(options[2]) != state->w_size
416         || options[3] != DEFLATE_CHK_SEQUENCE)
417         return 0;
418
419     state->seqno = 0;
420     state->unit = unit;
421     state->hdrlen = hdrlen;
422     state->debug = debug;
423     state->mru = mru;
424
425     inflateReset(&state->strm);
426
427     return 1;
428 }
429
430 static void
431 z_decomp_reset(arg)
432     void *arg;
433 {
434     struct deflate_state *state = (struct deflate_state *) arg;
435
436     state->seqno = 0;
437     inflateReset(&state->strm);
438 }
439
440 /*
441  * Decompress a Deflate-compressed packet.
442  *
443  * Because of patent problems, we return DECOMP_ERROR for errors
444  * found by inspecting the input data and for system problems, but
445  * DECOMP_FATALERROR for any errors which could possibly be said to
446  * be being detected "after" decompression.  For DECOMP_ERROR,
447  * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
448  * infringing a patent of Motorola's if we do, so we take CCP down
449  * instead.
450  *
451  * Given that the frame has the correct sequence number and a good FCS,
452  * errors such as invalid codes in the input most likely indicate a
453  * bug, so we return DECOMP_FATALERROR for them in order to turn off
454  * compression, even though they are detected by inspecting the input.
455  */
456 static int
457 z_decompress(arg, mi, mop)
458     void *arg;
459     mblk_t *mi, **mop;
460 {
461     struct deflate_state *state = (struct deflate_state *) arg;
462     mblk_t *mo, *mo_head;
463     u_char *rptr, *wptr;
464     int rlen, olen, ospace;
465     int seq, i, flush, r, decode_proto;
466     u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
467
468     *mop = NULL;
469     rptr = mi->b_rptr;
470     for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
471         while (rptr >= mi->b_wptr) {
472             mi = mi->b_cont;
473             if (mi == NULL)
474                 return DECOMP_ERROR;
475             rptr = mi->b_rptr;
476         }
477         hdr[i] = *rptr++;
478     }
479
480     /* Check the sequence number. */
481     seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
482     if (seq != state->seqno) {
483         if (state->debug)
484             printf("z_decompress%d: bad seq # %d, expected %d\n",
485                    state->unit, seq, state->seqno);
486         return DECOMP_ERROR;
487     }
488     ++state->seqno;
489
490     /* Allocate an output message block. */
491     mo = allocb(DECOMP_CHUNK + state->hdrlen, BPRI_MED);
492     if (mo == NULL)
493         return DECOMP_ERROR;
494     mo_head = mo;
495     mo->b_cont = NULL;
496     mo->b_rptr += state->hdrlen;
497     mo->b_wptr = wptr = mo->b_rptr;
498     ospace = DECOMP_CHUNK;
499
500     /*
501      * Fill in the first part of the PPP header.  The protocol field
502      * comes from the decompressed data.
503      */
504     wptr[0] = PPP_ADDRESS(hdr);
505     wptr[1] = PPP_CONTROL(hdr);
506     wptr[2] = 0;
507
508     /*
509      * Set up to call inflate.  We set avail_out to 1 initially so we can
510      * look at the first byte of the output and decide whether we have
511      * a 1-byte or 2-byte protocol field.
512      */
513     state->strm.next_in = rptr;
514     state->strm.avail_in = mi->b_wptr - rptr;
515     mi = mi->b_cont;
516     flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
517     rlen = state->strm.avail_in + PPP_HDRLEN + DEFLATE_OVHD;
518     state->strm.next_out = wptr + 3;
519     state->strm.avail_out = 1;
520     decode_proto = 1;
521     olen = PPP_HDRLEN;
522
523     /*
524      * Call inflate, supplying more input or output as needed.
525      */
526     for (;;) {
527         r = inflate(&state->strm, flush);
528         if (r != Z_OK) {
529             if (state->debug)
530                 printf("z_decompress%d: inflate returned %d (%s)\n",
531                        state->unit, r, (state->strm.msg? state->strm.msg: ""));
532             freemsg(mo_head);
533             return DECOMP_FATALERROR;
534         }
535         if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
536             break;              /* all done */
537         if (state->strm.avail_in == 0 && mi != NULL) {
538             state->strm.next_in = mi->b_rptr;
539             state->strm.avail_in = mi->b_wptr - mi->b_rptr;
540             rlen += state->strm.avail_in;
541             mi = mi->b_cont;
542             if (mi == NULL)
543                 flush = Z_PACKET_FLUSH;
544         }
545         if (state->strm.avail_out == 0) {
546             if (decode_proto) {
547                 state->strm.avail_out = ospace - PPP_HDRLEN;
548                 if ((wptr[3] & 1) == 0) {
549                     /* 2-byte protocol field */
550                     wptr[2] = wptr[3];
551                     --state->strm.next_out;
552                     ++state->strm.avail_out;
553                     --olen;
554                 }
555                 decode_proto = 0;
556             } else {
557                 mo->b_wptr += ospace;
558                 olen += ospace;
559                 mo->b_cont = allocb(DECOMP_CHUNK, BPRI_MED);
560                 mo = mo->b_cont;
561                 if (mo == NULL) {
562                     freemsg(mo_head);
563                     return DECOMP_ERROR;
564                 }
565                 state->strm.next_out = mo->b_rptr;
566                 state->strm.avail_out = ospace = DECOMP_CHUNK;
567             }
568         }
569     }
570     if (decode_proto) {
571         freemsg(mo_head);
572         return DECOMP_ERROR;
573     }
574     mo->b_wptr += ospace - state->strm.avail_out;
575     olen += ospace - state->strm.avail_out;
576
577     state->stats.unc_bytes += olen;
578     state->stats.unc_packets++;
579     state->stats.comp_bytes += rlen;
580     state->stats.comp_packets++;
581
582     *mop = mo_head;
583     return DECOMP_OK;
584 }
585
586 /*
587  * Incompressible data has arrived - add it to the history.
588  */
589 static void
590 z_incomp(arg, mi)
591     void *arg;
592     mblk_t *mi;
593 {
594     struct deflate_state *state = (struct deflate_state *) arg;
595     u_char *rptr;
596     int rlen, proto, r;
597
598     /*
599      * Check that the protocol is one we handle.
600      */
601     rptr = mi->b_rptr;
602     if (rptr + PPP_HDRLEN > mi->b_wptr) {
603         if (!pullupmsg(mi, PPP_HDRLEN))
604             return;
605         rptr = mi->b_rptr;
606     }
607     proto = PPP_PROTOCOL(rptr);
608     if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
609         return;
610
611     ++state->seqno;
612
613     /*
614      * Iterate through the message blocks, adding the characters in them
615      * to the decompressor's history.  For the first block, we start
616      * at the either the 1st or 2nd byte of the protocol field,
617      * depending on whether the protocol value is compressible.
618      */
619     rlen = mi->b_wptr - mi->b_rptr;
620     state->strm.next_in = rptr + 3;
621     state->strm.avail_in = rlen - 3;
622     if (proto > 0xff) {
623         --state->strm.next_in;
624         ++state->strm.avail_in;
625     }
626     for (;;) {
627         r = inflateIncomp(&state->strm);
628         if (r != Z_OK) {
629             /* gak! */
630             if (state->debug) {
631                 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
632                        state->unit, r, (state->strm.msg? state->strm.msg: ""));
633             }
634             return;
635         }
636         mi = mi->b_cont;
637         if (mi == NULL)
638             break;
639         state->strm.next_in = mi->b_rptr;
640         state->strm.avail_in = mi->b_wptr - mi->b_rptr;
641         rlen += state->strm.avail_in;
642     }
643
644     /*
645      * Update stats.
646      */
647     state->stats.inc_bytes += rlen;
648     state->stats.inc_packets++;
649     state->stats.unc_bytes += rlen;
650     state->stats.unc_packets++;
651 }
652
653 #endif /* DO_DEFLATE */