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