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