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@ozlabs.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.5 2004/01/17 05:47:55 carlsonj Exp $
39 #include <sys/types.h>
50 #define DEFLATE_DEBUG 1
53 * State for a Deflate (de)compressor.
55 struct deflate_state {
63 struct compstat stats;
66 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
68 static void *z_alloc(void *, u_int items, u_int size);
69 static void z_free(void *, void *ptr, u_int nb);
70 static void *z_decomp_alloc(u_char *options, int opt_len);
71 static void z_decomp_free(void *state);
72 static int z_decomp_init(void *state, u_char *options, int opt_len,
73 int unit, int hdrlen, int mru, int debug);
74 static void z_incomp(void *state, u_char *dmsg, int len);
75 static int z_decompress(void *state, u_char *cmp, int inlen,
76 u_char *dmp, int *outlenp);
77 static void z_decomp_reset(void *state);
78 static void z_comp_stats(void *state, struct compstat *stats);
81 * Procedures exported to if_ppp.c.
83 struct compressor ppp_deflate = {
84 CI_DEFLATE, /* compress_proto */
85 z_decomp_alloc, /* decomp_alloc */
86 z_decomp_free, /* decomp_free */
87 z_decomp_init, /* decomp_init */
88 z_decomp_reset, /* decomp_reset */
89 z_decompress, /* decompress */
90 z_incomp, /* incomp */
91 z_comp_stats, /* decomp_stat */
95 * Space allocation and freeing routines for use by zlib routines.
98 z_alloc(void *notused, u_int items, u_int size)
100 return malloc(items * size);
104 z_free(void *notused, void *ptr, u_int nbytes)
110 z_comp_stats(void *arg, struct compstat *stats)
112 struct deflate_state *state = (struct deflate_state *) arg;
115 *stats = state->stats;
116 stats->ratio = stats->unc_bytes;
117 out = stats->comp_bytes + stats->unc_bytes;
118 u_int ratio = stats->ratio;
119 if (ratio <= 0x7ffffff)
124 stats->ratio = ratio / out;
128 * Allocate space for a decompressor.
131 z_decomp_alloc(u_char *options, int opt_len)
133 struct deflate_state *state;
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)
141 w_size = DEFLATE_SIZE(options[2]);
142 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
145 state = (struct deflate_state *) malloc(sizeof(*state));
149 state->strm.next_out = NULL;
150 state->strm.zalloc = (alloc_func) z_alloc;
151 state->strm.zfree = (free_func) z_free;
152 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
157 state->w_size = w_size;
158 memset(&state->stats, 0, sizeof(state->stats));
159 return (void *) state;
163 z_decomp_free(void *arg)
165 struct deflate_state *state = (struct deflate_state *) arg;
167 inflateEnd(&state->strm);
172 z_decomp_init(void *arg, u_char *options, int opt_len,
173 int unit, int hdrlen, int mru, int debug)
175 struct deflate_state *state = (struct deflate_state *) arg;
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)
186 state->hdrlen = hdrlen;
187 state->debug = debug;
190 inflateReset(&state->strm);
196 z_decomp_reset(void *arg)
198 struct deflate_state *state = (struct deflate_state *) arg;
201 inflateReset(&state->strm);
205 * Decompress a Deflate-compressed packet.
207 * Because of patent problems, we return DECOMP_ERROR for errors
208 * found by inspecting the input data and for system problems, but
209 * DECOMP_FATALERROR for any errors which could possibly be said to
210 * be being detected "after" decompression. For DECOMP_ERROR,
211 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
212 * infringing a patent of Motorola's if we do, so we take CCP down
215 * Given that the frame has the correct sequence number and a good FCS,
216 * errors such as invalid codes in the input most likely indicate a
217 * bug, so we return DECOMP_FATALERROR for them in order to turn off
218 * compression, even though they are detected by inspecting the input.
221 z_decompress(void *arg, u_char *mi, int inlen, u_char *mo, int *outlenp)
223 struct deflate_state *state = (struct deflate_state *) arg;
233 /* Check the sequence number. */
234 seq = (rptr[0] << 8) + rptr[1];
236 if (seq != state->seqno) {
240 printf("z_decompress%d: bad seq # %d, expected %d\n",
241 state->unit, seq, state->seqno);
247 * Set up to call inflate.
250 state->strm.next_in = rptr;
251 state->strm.avail_in = mi + inlen - rptr;
252 rlen = state->strm.avail_in + PPP_HDRLEN + DEFLATE_OVHD;
253 state->strm.next_out = wptr;
254 state->strm.avail_out = state->mru + 2;
256 r = inflate(&state->strm, Z_PACKET_FLUSH);
261 printf("z_decompress%d: inflate returned %d (%s)\n",
262 state->unit, r, (state->strm.msg? state->strm.msg: ""));
263 return DECOMP_FATALERROR;
265 olen = state->mru + 2 - state->strm.avail_out;
268 if ((wptr[0] & 1) != 0)
269 ++olen; /* for suppressed protocol high byte */
270 olen += 2; /* for address, control */
273 if (olen > state->mru + PPP_HDRLEN)
274 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
275 state->unit, olen, state->mru + PPP_HDRLEN);
278 state->stats.unc_bytes += olen;
279 state->stats.unc_packets++;
280 state->stats.comp_bytes += rlen;
281 state->stats.comp_packets++;
287 * Incompressible data has arrived - add it to the history.
290 z_incomp(void *arg, u_char *mi, int mlen)
292 struct deflate_state *state = (struct deflate_state *) arg;
297 * Check that the protocol is one we handle.
301 if ((proto & 1) == 0)
302 proto = (proto << 8) + rptr[1];
303 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
310 rlen = mi + mlen - rptr;
311 state->strm.next_in = rptr;
312 state->strm.avail_in = rlen;
313 r = inflateIncomp(&state->strm);
319 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
320 state->unit, r, (state->strm.msg? state->strm.msg: ""));
330 state->stats.inc_bytes += rlen;
331 state->stats.inc_packets++;
332 state->stats.unc_bytes += rlen;
333 state->stats.unc_packets++;
336 #endif /* DO_DEFLATE */