]> git.ozlabs.org Git - ppp.git/blob - pppdump/deflate.c
check in pppdump sources
[ppp.git] / pppdump / 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  *
5  * Copyright (c) 1994 The Australian National University.
6  * All rights reserved.
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation is hereby granted, provided that the above copyright
10  * notice appears in all copies.  This software is provided without any
11  * warranty, express or implied. The Australian National University
12  * makes no representations about the suitability of this software for
13  * any purpose.
14  *
15  * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
16  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
17  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
18  * THE AUSTRALIAN NATIONAL UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY
19  * OF SUCH DAMAGE.
20  *
21  * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
22  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
24  * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
25  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
26  * OR MODIFICATIONS.
27  *
28  * $Id: deflate.c,v 1.1 1999/03/23 03:21:57 paulus Exp $
29  */
30
31 #include <sys/types.h>
32 #include <stdlib.h>
33 #include "ppp_defs.h"
34 #include "ppp-comp.h"
35 #include "zlib.h"
36
37 #if DO_DEFLATE
38
39 #define DEFLATE_DEBUG   1
40
41 /*
42  * State for a Deflate (de)compressor.
43  */
44 struct deflate_state {
45     int         seqno;
46     int         w_size;
47     int         unit;
48     int         hdrlen;
49     int         mru;
50     int         debug;
51     z_stream    strm;
52     struct compstat stats;
53 };
54
55 #define DEFLATE_OVHD    2               /* Deflate overhead/packet */
56
57 static void     *z_alloc __P((void *, u_int items, u_int size));
58 static void     z_free __P((void *, void *ptr, u_int nb));
59 static void     *z_decomp_alloc __P((u_char *options, int opt_len));
60 static void     z_decomp_free __P((void *state));
61 static int      z_decomp_init __P((void *state, u_char *options, int opt_len,
62                                      int unit, int hdrlen, int mru, int debug));
63 static void     z_incomp __P((void *state, u_char *dmsg, int len));
64 static int      z_decompress __P((void *state, u_char *cmp, int inlen,
65                                     u_char *dmp, int *outlenp));
66 static void     z_decomp_reset __P((void *state));
67 static void     z_comp_stats __P((void *state, struct compstat *stats));
68
69 /*
70  * Procedures exported to if_ppp.c.
71  */
72 struct compressor ppp_deflate = {
73     CI_DEFLATE,                 /* compress_proto */
74     z_decomp_alloc,             /* decomp_alloc */
75     z_decomp_free,              /* decomp_free */
76     z_decomp_init,              /* decomp_init */
77     z_decomp_reset,             /* decomp_reset */
78     z_decompress,               /* decompress */
79     z_incomp,                   /* incomp */
80     z_comp_stats,               /* decomp_stat */
81 };
82
83 /*
84  * Space allocation and freeing routines for use by zlib routines.
85  */
86 static void *
87 z_alloc(notused, items, size)
88     void *notused;
89     u_int items, size;
90 {
91     return malloc(items * size);
92 }
93
94 static void
95 z_free(notused, ptr, nbytes)
96     void *notused;
97     void *ptr;
98     u_int nbytes;
99 {
100     free(ptr);
101 }
102
103 static void
104 z_comp_stats(arg, stats)
105     void *arg;
106     struct compstat *stats;
107 {
108     struct deflate_state *state = (struct deflate_state *) arg;
109     u_int out;
110
111     *stats = state->stats;
112     stats->ratio = stats->unc_bytes;
113     out = stats->comp_bytes + stats->unc_bytes;
114     if (stats->ratio <= 0x7ffffff)
115         stats->ratio <<= 8;
116     else
117         out >>= 8;
118     if (out != 0)
119         stats->ratio /= out;
120 }
121
122 /*
123  * Allocate space for a decompressor.
124  */
125 static void *
126 z_decomp_alloc(options, opt_len)
127     u_char *options;
128     int opt_len;
129 {
130     struct deflate_state *state;
131     int w_size;
132
133     if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
134         || options[1] != CILEN_DEFLATE
135         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
136         || options[3] != DEFLATE_CHK_SEQUENCE)
137         return NULL;
138     w_size = DEFLATE_SIZE(options[2]);
139     if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
140         return NULL;
141
142     state = (struct deflate_state *) malloc(sizeof(*state));
143     if (state == NULL)
144         return NULL;
145
146     state->strm.next_out = NULL;
147     state->strm.zalloc = (alloc_func) z_alloc;
148     state->strm.zfree = (free_func) z_free;
149     if (inflateInit2(&state->strm, -w_size) != Z_OK) {
150         free(state);
151         return NULL;
152     }
153
154     state->w_size = w_size;
155     bzero(&state->stats, sizeof(state->stats));
156     return (void *) state;
157 }
158
159 static void
160 z_decomp_free(arg)
161     void *arg;
162 {
163     struct deflate_state *state = (struct deflate_state *) arg;
164
165     inflateEnd(&state->strm);
166     free(state);
167 }
168
169 static int
170 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
171     void *arg;
172     u_char *options;
173     int opt_len, unit, hdrlen, mru, debug;
174 {
175     struct deflate_state *state = (struct deflate_state *) arg;
176
177     if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
178         || options[1] != CILEN_DEFLATE
179         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
180         || DEFLATE_SIZE(options[2]) != state->w_size
181         || options[3] != DEFLATE_CHK_SEQUENCE)
182         return 0;
183
184     state->seqno = 0;
185     state->unit = unit;
186     state->hdrlen = hdrlen;
187     state->debug = debug;
188     state->mru = mru;
189
190     inflateReset(&state->strm);
191
192     return 1;
193 }
194
195 static void
196 z_decomp_reset(arg)
197     void *arg;
198 {
199     struct deflate_state *state = (struct deflate_state *) arg;
200
201     state->seqno = 0;
202     inflateReset(&state->strm);
203 }
204
205 /*
206  * Decompress a Deflate-compressed packet.
207  *
208  * Because of patent problems, we return DECOMP_ERROR for errors
209  * found by inspecting the input data and for system problems, but
210  * DECOMP_FATALERROR for any errors which could possibly be said to
211  * be being detected "after" decompression.  For DECOMP_ERROR,
212  * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
213  * infringing a patent of Motorola's if we do, so we take CCP down
214  * instead.
215  *
216  * Given that the frame has the correct sequence number and a good FCS,
217  * errors such as invalid codes in the input most likely indicate a
218  * bug, so we return DECOMP_FATALERROR for them in order to turn off
219  * compression, even though they are detected by inspecting the input.
220  */
221 static int
222 z_decompress(arg, mi, inlen, mo, outlenp)
223     void *arg;
224     u_char *mi, *mo;
225     int inlen, *outlenp;
226 {
227     struct deflate_state *state = (struct deflate_state *) arg;
228     u_char *rptr, *wptr;
229     int rlen, olen, ospace;
230     int seq, i, flush, r, decode_proto;
231
232     rptr = mi;
233     if (*rptr == 0)
234         ++rptr;
235     ++rptr;
236
237     /* Check the sequence number. */
238     seq = (rptr[0] << 8) + rptr[1];
239     rptr += 2;
240     if (seq != state->seqno) {
241 #if !DEFLATE_DEBUG
242         if (state->debug)
243 #endif
244             printf("z_decompress%d: bad seq # %d, expected %d\n",
245                    state->unit, seq, state->seqno);
246         return DECOMP_ERROR;
247     }
248     ++state->seqno;
249
250     /*
251      * Set up to call inflate.
252      */
253     wptr = mo;
254     state->strm.next_in = rptr;
255     state->strm.avail_in = mi + inlen - rptr;
256     rlen = state->strm.avail_in + PPP_HDRLEN + DEFLATE_OVHD;
257     state->strm.next_out = wptr;
258     state->strm.avail_out = state->mru + 2;
259
260     r = inflate(&state->strm, Z_PACKET_FLUSH);
261     if (r != Z_OK) {
262 #if !DEFLATE_DEBUG
263         if (state->debug)
264 #endif
265             printf("z_decompress%d: inflate returned %d (%s)\n",
266                    state->unit, r, (state->strm.msg? state->strm.msg: ""));
267         return DECOMP_FATALERROR;
268     }
269     olen = state->mru + 2 - state->strm.avail_out;
270     *outlenp = olen;
271
272     if ((wptr[0] & 1) != 0)
273         ++olen;                 /* for suppressed protocol high byte */
274     olen += 2;                  /* for address, control */
275
276 #if DEFLATE_DEBUG
277     if (olen > state->mru + PPP_HDRLEN)
278         printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
279                state->unit, olen, state->mru + PPP_HDRLEN);
280 #endif
281
282     state->stats.unc_bytes += olen;
283     state->stats.unc_packets++;
284     state->stats.comp_bytes += rlen;
285     state->stats.comp_packets++;
286
287     return DECOMP_OK;
288 }
289
290 /*
291  * Incompressible data has arrived - add it to the history.
292  */
293 static void
294 z_incomp(arg, mi, mlen)
295     void *arg;
296     u_char *mi;
297     int mlen;
298 {
299     struct deflate_state *state = (struct deflate_state *) arg;
300     u_char *rptr;
301     int rlen, proto, r;
302
303     /*
304      * Check that the protocol is one we handle.
305      */
306     rptr = mi;
307     proto = rptr[0];
308     if ((proto & 1) == 0)
309         proto = (proto << 8) + rptr[1];
310     if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
311         return;
312
313     ++state->seqno;
314
315     if (rptr[0] == 0)
316         ++rptr;
317     rlen = mi + mlen - rptr;
318     state->strm.next_in = rptr;
319     state->strm.avail_in = rlen;
320     r = inflateIncomp(&state->strm);
321     if (r != Z_OK) {
322         /* gak! */
323 #if !DEFLATE_DEBUG
324         if (state->debug)
325 #endif
326             printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
327                    state->unit, r, (state->strm.msg? state->strm.msg: ""));
328         return;
329     }
330
331     /*
332      * Update stats.
333      */
334     if (proto <= 0xff)
335         ++rlen;
336     rlen += 2;
337     state->stats.inc_bytes += rlen;
338     state->stats.inc_packets++;
339     state->stats.unc_bytes += rlen;
340     state->stats.unc_packets++;
341 }
342
343 #endif /* DO_DEFLATE */