Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / pppd / pppdump / deflate.c
blobc9ba49db844209ea102458d02df3ce08d70ba762
1 /* $NetBSD: deflate.c,v 1.1.1.1 2005/02/20 10:28:54 cube Exp $ */
3 /*
4 * ppp_deflate.c - interface the zlib procedures for Deflate compression
5 * and decompression (as used by gzip) to the PPP code.
7 * Copyright (c) 1994 Paul Mackerras. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * 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
18 * the documentation and/or other materials provided with the
19 * distribution.
21 * 3. The name(s) of the authors of this software must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission.
25 * 4. Redistributions of any form whatsoever must retain the following
26 * acknowledgment:
27 * "This product includes software developed by Paul Mackerras
28 * <paulus@samba.org>".
30 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
31 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
32 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
33 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
34 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
35 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
36 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38 * Id: deflate.c,v 1.5 2004/01/17 05:47:55 carlsonj Exp
41 #include <sys/types.h>
42 #include <stdio.h>
43 #include <stddef.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include "pppdump.h"
47 #include <net/ppp_defs.h>
48 #include <net/ppp-comp.h>
49 #include <zlib.h>
51 #if DO_DEFLATE
53 #define DEFLATE_DEBUG 1
56 * State for a Deflate (de)compressor.
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;
69 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
71 static void *z_alloc __P((void *, u_int items, u_int size));
72 static void z_free __P((void *, void *ptr, u_int nb));
73 static void *z_decomp_alloc __P((u_char *options, int opt_len));
74 static void z_decomp_free __P((void *state));
75 static int z_decomp_init __P((void *state, u_char *options, int opt_len,
76 int unit, int hdrlen, int mru, int debug));
77 static void z_incomp __P((void *state, PACKETPTR mi));
78 static int z_decompress __P((void *state, PACKETPTR mi, PACKETPTR *mo));
79 static void z_decomp_reset __P((void *state));
80 static void z_comp_stats __P((void *state, struct compstat *stats));
83 * Procedures exported to if_ppp.c.
85 struct compressor ppp_deflate = {
86 CI_DEFLATE, /* compress_proto */
87 NULL, /* comp_alloc */
88 NULL, /* comp_free */
89 NULL, /* comp_init */
90 NULL, /* comp_reset */
91 NULL, /* comp_compress */
92 NULL, /* comp_stat */
93 z_decomp_alloc, /* decomp_alloc */
94 z_decomp_free, /* decomp_free */
95 z_decomp_init, /* decomp_init */
96 z_decomp_reset, /* decomp_reset */
97 z_decompress, /* decompress */
98 z_incomp, /* incomp */
99 z_comp_stats, /* decomp_stat */
103 * Space allocation and freeing routines for use by zlib routines.
105 static void *
106 z_alloc(notused, items, size)
107 void *notused;
108 u_int items, size;
110 return malloc(items * size);
113 static void
114 z_free(notused, ptr, nbytes)
115 void *notused;
116 void *ptr;
117 u_int nbytes;
119 free(ptr);
122 static void
123 z_comp_stats(arg, stats)
124 void *arg;
125 struct compstat *stats;
127 struct deflate_state *state = (struct deflate_state *) arg;
128 u_int out;
130 *stats = state->stats;
131 stats->ratio = stats->unc_bytes;
132 out = stats->comp_bytes + stats->unc_bytes;
133 if (stats->ratio <= 0x7ffffff)
134 stats->ratio <<= 8;
135 else
136 out >>= 8;
137 if (out != 0)
138 stats->ratio /= out;
142 * Allocate space for a decompressor.
144 static void *
145 z_decomp_alloc(options, opt_len)
146 u_char *options;
147 int opt_len;
149 struct deflate_state *state;
150 int w_size;
152 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
153 || options[1] != CILEN_DEFLATE
154 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
155 || options[3] != DEFLATE_CHK_SEQUENCE)
156 return NULL;
157 w_size = DEFLATE_SIZE(options[2]);
158 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
159 return NULL;
161 state = (struct deflate_state *) malloc(sizeof(*state));
162 if (state == NULL)
163 return NULL;
165 state->strm.next_out = NULL;
166 state->strm.zalloc = (alloc_func) z_alloc;
167 state->strm.zfree = (free_func) z_free;
168 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
169 free(state);
170 return NULL;
173 state->w_size = w_size;
174 memset(&state->stats, 0, sizeof(state->stats));
175 return (void *) state;
178 static void
179 z_decomp_free(arg)
180 void *arg;
182 struct deflate_state *state = (struct deflate_state *) arg;
184 inflateEnd(&state->strm);
185 free(state);
188 static int
189 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
190 void *arg;
191 u_char *options;
192 int opt_len, unit, hdrlen, mru, debug;
194 struct deflate_state *state = (struct deflate_state *) arg;
196 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
197 || options[1] != CILEN_DEFLATE
198 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
199 || DEFLATE_SIZE(options[2]) != state->w_size
200 || options[3] != DEFLATE_CHK_SEQUENCE)
201 return 0;
203 state->seqno = 0;
204 state->unit = unit;
205 state->hdrlen = hdrlen;
206 state->debug = debug;
207 state->mru = mru;
209 inflateReset(&state->strm);
211 return 1;
214 static void
215 z_decomp_reset(arg)
216 void *arg;
218 struct deflate_state *state = (struct deflate_state *) arg;
220 state->seqno = 0;
221 inflateReset(&state->strm);
225 * Decompress a Deflate-compressed packet.
227 * Because of patent problems, we return DECOMP_ERROR for errors
228 * found by inspecting the input data and for system problems, but
229 * DECOMP_FATALERROR for any errors which could possibly be said to
230 * be being detected "after" decompression. For DECOMP_ERROR,
231 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
232 * infringing a patent of Motorola's if we do, so we take CCP down
233 * instead.
235 * Given that the frame has the correct sequence number and a good FCS,
236 * errors such as invalid codes in the input most likely indicate a
237 * bug, so we return DECOMP_FATALERROR for them in order to turn off
238 * compression, even though they are detected by inspecting the input.
240 static int
241 z_decompress(arg, mi, mo)
242 void *arg;
243 PACKETPTR mi;
244 PACKETPTR *mo;
246 struct deflate_state *state = (struct deflate_state *) arg;
247 u_char *rptr, *wptr;
248 int rlen, olen;
249 int seq, r;
251 rptr = mi->buf;
252 if (*rptr == 0)
253 ++rptr;
254 ++rptr;
256 /* Check the sequence number. */
257 seq = (rptr[0] << 8) + rptr[1];
258 rptr += 2;
259 if (seq != state->seqno) {
260 #if !DEFLATE_DEBUG
261 if (state->debug)
262 #endif
263 printf("z_decompress%d: bad seq # %d, expected %d\n",
264 state->unit, seq, state->seqno);
265 return DECOMP_ERROR;
267 ++state->seqno;
270 * Set up to call inflate.
272 wptr = (*mo)->buf;
273 state->strm.next_in = rptr;
274 state->strm.avail_in = mi->buf + mi->len - rptr;
275 rlen = state->strm.avail_in + PPP_HDRLEN + DEFLATE_OVHD;
276 state->strm.next_out = wptr;
277 state->strm.avail_out = state->mru + 2;
279 r = inflate(&state->strm, Z_PACKET_FLUSH);
280 if (r != Z_OK) {
281 #if !DEFLATE_DEBUG
282 if (state->debug)
283 #endif
284 printf("z_decompress%d: inflate returned %d (%s)\n",
285 state->unit, r, (state->strm.msg? state->strm.msg: ""));
286 return DECOMP_FATALERROR;
288 olen = state->mru + 2 - state->strm.avail_out;
289 (*mo)->len = olen;
291 if ((wptr[0] & 1) != 0)
292 ++olen; /* for suppressed protocol high byte */
293 olen += 2; /* for address, control */
295 #if DEFLATE_DEBUG
296 if (olen > state->mru + PPP_HDRLEN)
297 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
298 state->unit, olen, state->mru + PPP_HDRLEN);
299 #endif
301 state->stats.unc_bytes += olen;
302 state->stats.unc_packets++;
303 state->stats.comp_bytes += rlen;
304 state->stats.comp_packets++;
306 return DECOMP_OK;
310 * Incompressible data has arrived - add it to the history.
312 static void
313 z_incomp(arg, mi)
314 void *arg;
315 PACKETPTR mi;
317 struct deflate_state *state = (struct deflate_state *) arg;
318 u_char *rptr;
319 int rlen, proto, r;
322 * Check that the protocol is one we handle.
324 rptr = mi->buf;
325 proto = rptr[0];
326 if ((proto & 1) == 0)
327 proto = (proto << 8) + rptr[1];
328 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
329 return;
331 ++state->seqno;
333 if (rptr[0] == 0)
334 ++rptr;
335 rlen = mi->buf + mi->len - rptr;
336 state->strm.next_in = rptr;
337 state->strm.avail_in = rlen;
338 r = inflateIncomp(&state->strm);
339 if (r != Z_OK) {
340 /* gak! */
341 #if !DEFLATE_DEBUG
342 if (state->debug)
343 #endif
344 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
345 state->unit, r, (state->strm.msg? state->strm.msg: ""));
346 return;
350 * Update stats.
352 if (proto <= 0xff)
353 ++rlen;
354 rlen += 2;
355 state->stats.inc_bytes += rlen;
356 state->stats.inc_packets++;
357 state->stats.unc_bytes += rlen;
358 state->stats.unc_packets++;
361 #endif /* DO_DEFLATE */