2 * ppp_deflate.c - interface the zlib procedures for Deflate compression
3 * and decompression (as used by gzip) to the PPP code.
5 * Copyright (c) 1994 Paul Mackerras. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The name(s) of the authors of this software must not be used to
20 * endorse or promote products derived from this software without
21 * prior written permission.
23 * 4. Redistributions of any form whatsoever must retain the following
25 * "This product includes software developed by Paul Mackerras
26 * <paulus@samba.org>".
28 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
29 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
30 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
31 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
32 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
33 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
34 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
36 * $Id: deflate.c,v 1.4 2002/12/06 09:49:16 paulus Exp $
39 #include <sys/types.h>
48 #define DEFLATE_DEBUG 1
51 * State for a Deflate (de)compressor.
53 struct deflate_state {
61 struct compstat stats;
64 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
66 static void *z_alloc __P((void *, u_int items, u_int size));
67 static void z_free __P((void *, void *ptr, u_int nb));
68 static void *z_decomp_alloc __P((u_char *options, int opt_len));
69 static void z_decomp_free __P((void *state));
70 static int z_decomp_init __P((void *state, u_char *options, int opt_len,
71 int unit, int hdrlen, int mru, int debug));
72 static void z_incomp __P((void *state, u_char *dmsg, int len));
73 static int z_decompress __P((void *state, u_char *cmp, int inlen,
74 u_char *dmp, int *outlenp));
75 static void z_decomp_reset __P((void *state));
76 static void z_comp_stats __P((void *state, struct compstat *stats));
79 * Procedures exported to if_ppp.c.
81 struct compressor ppp_deflate = {
82 CI_DEFLATE, /* compress_proto */
83 z_decomp_alloc, /* decomp_alloc */
84 z_decomp_free, /* decomp_free */
85 z_decomp_init, /* decomp_init */
86 z_decomp_reset, /* decomp_reset */
87 z_decompress, /* decompress */
88 z_incomp, /* incomp */
89 z_comp_stats, /* decomp_stat */
93 * Space allocation and freeing routines for use by zlib routines.
96 z_alloc(notused, items, size)
100 return malloc(items * size);
104 z_free(notused, ptr, nbytes)
113 z_comp_stats(arg, stats)
115 struct compstat *stats;
117 struct deflate_state *state = (struct deflate_state *) arg;
120 *stats = state->stats;
121 stats->ratio = stats->unc_bytes;
122 out = stats->comp_bytes + stats->unc_bytes;
123 if (stats->ratio <= 0x7ffffff)
132 * Allocate space for a decompressor.
135 z_decomp_alloc(options, opt_len)
139 struct deflate_state *state;
142 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
143 || options[1] != CILEN_DEFLATE
144 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
145 || options[3] != DEFLATE_CHK_SEQUENCE)
147 w_size = DEFLATE_SIZE(options[2]);
148 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
151 state = (struct deflate_state *) malloc(sizeof(*state));
155 state->strm.next_out = NULL;
156 state->strm.zalloc = (alloc_func) z_alloc;
157 state->strm.zfree = (free_func) z_free;
158 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
163 state->w_size = w_size;
164 memset(&state->stats, 0, sizeof(state->stats));
165 return (void *) state;
172 struct deflate_state *state = (struct deflate_state *) arg;
174 inflateEnd(&state->strm);
179 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
182 int opt_len, unit, hdrlen, mru, debug;
184 struct deflate_state *state = (struct deflate_state *) arg;
186 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
187 || options[1] != CILEN_DEFLATE
188 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
189 || DEFLATE_SIZE(options[2]) != state->w_size
190 || options[3] != DEFLATE_CHK_SEQUENCE)
195 state->hdrlen = hdrlen;
196 state->debug = debug;
199 inflateReset(&state->strm);
208 struct deflate_state *state = (struct deflate_state *) arg;
211 inflateReset(&state->strm);
215 * Decompress a Deflate-compressed packet.
217 * Because of patent problems, we return DECOMP_ERROR for errors
218 * found by inspecting the input data and for system problems, but
219 * DECOMP_FATALERROR for any errors which could possibly be said to
220 * be being detected "after" decompression. For DECOMP_ERROR,
221 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
222 * infringing a patent of Motorola's if we do, so we take CCP down
225 * Given that the frame has the correct sequence number and a good FCS,
226 * errors such as invalid codes in the input most likely indicate a
227 * bug, so we return DECOMP_FATALERROR for them in order to turn off
228 * compression, even though they are detected by inspecting the input.
231 z_decompress(arg, mi, inlen, mo, outlenp)
236 struct deflate_state *state = (struct deflate_state *) arg;
238 int rlen, olen, ospace;
239 int seq, i, flush, r, decode_proto;
246 /* Check the sequence number. */
247 seq = (rptr[0] << 8) + rptr[1];
249 if (seq != state->seqno) {
253 printf("z_decompress%d: bad seq # %d, expected %d\n",
254 state->unit, seq, state->seqno);
260 * Set up to call inflate.
263 state->strm.next_in = rptr;
264 state->strm.avail_in = mi + inlen - rptr;
265 rlen = state->strm.avail_in + PPP_HDRLEN + DEFLATE_OVHD;
266 state->strm.next_out = wptr;
267 state->strm.avail_out = state->mru + 2;
269 r = inflate(&state->strm, Z_PACKET_FLUSH);
274 printf("z_decompress%d: inflate returned %d (%s)\n",
275 state->unit, r, (state->strm.msg? state->strm.msg: ""));
276 return DECOMP_FATALERROR;
278 olen = state->mru + 2 - state->strm.avail_out;
281 if ((wptr[0] & 1) != 0)
282 ++olen; /* for suppressed protocol high byte */
283 olen += 2; /* for address, control */
286 if (olen > state->mru + PPP_HDRLEN)
287 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
288 state->unit, olen, state->mru + PPP_HDRLEN);
291 state->stats.unc_bytes += olen;
292 state->stats.unc_packets++;
293 state->stats.comp_bytes += rlen;
294 state->stats.comp_packets++;
300 * Incompressible data has arrived - add it to the history.
303 z_incomp(arg, mi, mlen)
308 struct deflate_state *state = (struct deflate_state *) arg;
313 * Check that the protocol is one we handle.
317 if ((proto & 1) == 0)
318 proto = (proto << 8) + rptr[1];
319 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
326 rlen = mi + mlen - rptr;
327 state->strm.next_in = rptr;
328 state->strm.avail_in = rlen;
329 r = inflateIncomp(&state->strm);
335 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
336 state->unit, r, (state->strm.msg? state->strm.msg: ""));
346 state->stats.inc_bytes += rlen;
347 state->stats.inc_packets++;
348 state->stats.unc_bytes += rlen;
349 state->stats.unc_packets++;
352 #endif /* DO_DEFLATE */