2 * ppp_deflate.c - interface the zlib procedures for Deflate compression
3 * and decompression (as used by gzip) to the PPP code.
4 * This version is for use with STREAMS under SunOS 4.x, Solaris 2,
5 * SVR4, OSF/1 and AIX 4.x.
7 * Copyright (c) 1994 The Australian National University.
10 * Permission to use, copy, modify, and distribute this software and its
11 * documentation is hereby granted, provided that the above copyright
12 * notice appears in all copies. This software is provided without any
13 * warranty, express or implied. The Australian National University
14 * makes no representations about the suitability of this software for
17 * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
18 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
19 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
20 * THE AUSTRALIAN NATIONAL UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY
23 * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
26 * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
27 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
30 * $Id: deflate.c,v 1.2 1996/04/04 02:44:56 paulus Exp $
34 #include <net/net_globals.h>
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/stream.h>
39 #include <net/ppp_defs.h>
42 #define PACKETPTR mblk_t *
43 #include <net/ppp-comp.h>
44 #include "common/zlib.h"
49 * State for a Deflate (de)compressor.
51 struct deflate_state {
59 struct compstat stats;
62 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
64 static void *zalloc __P((void *, u_int items, u_int size));
65 static void zfree __P((void *, void *ptr, u_int nb));
66 static void *z_comp_alloc __P((u_char *options, int opt_len));
67 static void *z_decomp_alloc __P((u_char *options, int opt_len));
68 static void z_comp_free __P((void *state));
69 static void z_decomp_free __P((void *state));
70 static int z_comp_init __P((void *state, u_char *options, int opt_len,
71 int unit, int hdrlen, int debug));
72 static int z_decomp_init __P((void *state, u_char *options, int opt_len,
73 int unit, int hdrlen, int mru, int debug));
74 static int z_compress __P((void *state, mblk_t **mret,
75 mblk_t *mp, int slen, int maxolen));
76 static void z_incomp __P((void *state, mblk_t *dmsg));
77 static int z_decompress __P((void *state, mblk_t *cmp,
79 static void z_comp_reset __P((void *state));
80 static void z_decomp_reset __P((void *state));
81 static void z_comp_stats __P((void *state, struct compstat *stats));
84 * Procedures exported to if_ppp.c.
86 struct compressor ppp_deflate = {
87 CI_DEFLATE, /* compress_proto */
88 z_comp_alloc, /* comp_alloc */
89 z_comp_free, /* comp_free */
90 z_comp_init, /* comp_init */
91 z_comp_reset, /* comp_reset */
92 z_compress, /* compress */
93 z_comp_stats, /* comp_stat */
94 z_decomp_alloc, /* decomp_alloc */
95 z_decomp_free, /* decomp_free */
96 z_decomp_init, /* decomp_init */
97 z_decomp_reset, /* decomp_reset */
98 z_decompress, /* decompress */
99 z_incomp, /* incomp */
100 z_comp_stats, /* decomp_stat */
103 #define DECOMP_CHUNK 512
106 * Space allocation and freeing routines for use by zlib routines.
109 zalloc(notused, items, size)
113 return ALLOC_NOSLEEP(items * size);
117 zfree(notused, ptr, nbytes)
126 * Allocate space for a compressor.
129 z_comp_alloc(options, 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 *) ALLOC_NOSLEEP(sizeof(*state));
149 state->strm.next_in = NULL;
150 state->strm.zalloc = (alloc_func) zalloc;
151 state->strm.zfree = (free_func) zfree;
152 if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
153 -w_size, 8, Z_DEFAULT_STRATEGY, DEFLATE_OVHD+2) != Z_OK) {
154 FREE(state, sizeof(*state));
158 state->w_size = w_size;
159 bzero(&state->stats, sizeof(state->stats));
160 return (void *) state;
167 struct deflate_state *state = (struct deflate_state *) arg;
169 deflateEnd(&state->strm);
170 FREE(state, sizeof(*state));
174 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
177 int opt_len, unit, hdrlen, debug;
179 struct deflate_state *state = (struct deflate_state *) arg;
181 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
182 || options[1] != CILEN_DEFLATE
183 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
184 || DEFLATE_SIZE(options[2]) != state->w_size
185 || options[3] != DEFLATE_CHK_SEQUENCE)
190 state->hdrlen = hdrlen;
191 state->debug = debug;
193 deflateReset(&state->strm);
202 struct deflate_state *state = (struct deflate_state *) arg;
205 deflateReset(&state->strm);
209 z_compress(arg, mret, mp, orig_len, maxolen)
211 mblk_t **mret; /* compressed packet (out) */
212 mblk_t *mp; /* uncompressed packet (in) */
213 int orig_len, maxolen;
215 struct deflate_state *state = (struct deflate_state *) arg;
217 int proto, olen, wspace, r, flush;
221 * Check that the protocol is in the range we handle.
225 if (rptr + PPP_HDRLEN > mp->b_wptr) {
226 if (!pullupmsg(mp, PPP_HDRLEN))
230 proto = PPP_PROTOCOL(rptr);
231 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
234 /* Allocate one mblk initially. */
235 if (maxolen > orig_len)
237 wspace = maxolen < 4096? maxolen: 4096;
238 m = allocb(wspace, BPRI_MED);
241 if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
242 m->b_rptr += state->hdrlen;
243 m->b_wptr = m->b_rptr;
244 wspace -= state->hdrlen;
249 * Copy over the PPP header and store the 2-byte sequence number.
251 wptr[0] = PPP_ADDRESS(rptr);
252 wptr[1] = PPP_CONTROL(rptr);
253 wptr[2] = PPP_COMP >> 8;
256 wptr[0] = state->seqno >> 8;
257 wptr[1] = state->seqno;
259 state->strm.next_out = wptr;
260 state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
262 state->strm.next_out = NULL;
263 state->strm.avail_out = 1000000;
267 rptr += (proto > 0xff)? 2: 3; /* skip 1st proto byte if 0 */
268 state->strm.next_in = rptr;
269 state->strm.avail_in = mp->b_wptr - rptr;
271 flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
274 r = deflate(&state->strm, flush);
276 printf("z_compress: deflate returned %d (%s)\n",
277 r, (state->strm.msg? state->strm.msg: ""));
280 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
281 break; /* all done */
282 if (state->strm.avail_in == 0 && mp != NULL) {
283 state->strm.next_in = mp->b_rptr;
284 state->strm.avail_in = mp->b_wptr - mp->b_rptr;
287 flush = Z_PACKET_FLUSH;
289 if (state->strm.avail_out == 0) {
293 wspace = maxolen - olen;
296 else if (wspace > 4096)
298 m->b_cont = allocb(wspace, BPRI_MED);
301 state->strm.next_out = m->b_wptr;
302 state->strm.avail_out = wspace;
306 state->strm.next_out = NULL;
307 state->strm.avail_out = 1000000;
311 m->b_wptr += wspace - state->strm.avail_out;
312 olen += wspace - state->strm.avail_out;
315 * See if we managed to reduce the size of the packet.
316 * If the compressor just gave us a single zero byte, it means
317 * the packet was incompressible.
319 if (olen < orig_len && m != NULL
320 && !(olen == PPP_HDRLEN + 3 && *wptr == 0)) {
321 state->stats.comp_bytes += olen;
322 state->stats.comp_packets++;
328 state->stats.inc_bytes += orig_len;
329 state->stats.inc_packets++;
332 state->stats.unc_bytes += orig_len;
333 state->stats.unc_packets++;
339 z_comp_stats(arg, stats)
341 struct compstat *stats;
343 struct deflate_state *state = (struct deflate_state *) arg;
346 *stats = state->stats;
347 stats->ratio = stats->unc_bytes;
348 out = stats->comp_bytes + stats->unc_bytes;
349 if (stats->ratio <= 0x7ffffff)
358 * Allocate space for a decompressor.
361 z_decomp_alloc(options, opt_len)
365 struct deflate_state *state;
368 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
369 || options[1] != CILEN_DEFLATE
370 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
371 || options[3] != DEFLATE_CHK_SEQUENCE)
373 w_size = DEFLATE_SIZE(options[2]);
374 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
377 state = (struct deflate_state *) ALLOC_NOSLEEP(sizeof(*state));
381 state->strm.next_out = NULL;
382 state->strm.zalloc = (alloc_func) zalloc;
383 state->strm.zfree = (free_func) zfree;
384 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
385 FREE(state, sizeof(*state));
389 state->w_size = w_size;
390 bzero(&state->stats, sizeof(state->stats));
391 return (void *) state;
398 struct deflate_state *state = (struct deflate_state *) arg;
400 inflateEnd(&state->strm);
401 FREE(state, sizeof(*state));
405 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
408 int opt_len, unit, hdrlen, mru, debug;
410 struct deflate_state *state = (struct deflate_state *) arg;
412 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
413 || options[1] != CILEN_DEFLATE
414 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
415 || DEFLATE_SIZE(options[2]) != state->w_size
416 || options[3] != DEFLATE_CHK_SEQUENCE)
421 state->hdrlen = hdrlen;
422 state->debug = debug;
425 inflateReset(&state->strm);
434 struct deflate_state *state = (struct deflate_state *) arg;
437 inflateReset(&state->strm);
441 * Decompress a Deflate-compressed packet.
443 * Because of patent problems, we return DECOMP_ERROR for errors
444 * found by inspecting the input data and for system problems, but
445 * DECOMP_FATALERROR for any errors which could possibly be said to
446 * be being detected "after" decompression. For DECOMP_ERROR,
447 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
448 * infringing a patent of Motorola's if we do, so we take CCP down
451 * Given that the frame has the correct sequence number and a good FCS,
452 * errors such as invalid codes in the input most likely indicate a
453 * bug, so we return DECOMP_FATALERROR for them in order to turn off
454 * compression, even though they are detected by inspecting the input.
457 z_decompress(arg, mi, mop)
461 struct deflate_state *state = (struct deflate_state *) arg;
462 mblk_t *mo, *mo_head;
464 int rlen, olen, ospace;
465 int seq, i, flush, r, decode_proto;
466 u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
470 for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
471 while (rptr >= mi->b_wptr) {
480 /* Check the sequence number. */
481 seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
482 if (seq != state->seqno) {
484 printf("z_decompress%d: bad seq # %d, expected %d\n",
485 state->unit, seq, state->seqno);
490 /* Allocate an output message block. */
491 mo = allocb(DECOMP_CHUNK + state->hdrlen, BPRI_MED);
496 mo->b_rptr += state->hdrlen;
497 mo->b_wptr = wptr = mo->b_rptr;
498 ospace = DECOMP_CHUNK;
501 * Fill in the first part of the PPP header. The protocol field
502 * comes from the decompressed data.
504 wptr[0] = PPP_ADDRESS(hdr);
505 wptr[1] = PPP_CONTROL(hdr);
509 * Set up to call inflate. We set avail_out to 1 initially so we can
510 * look at the first byte of the output and decide whether we have
511 * a 1-byte or 2-byte protocol field.
513 state->strm.next_in = rptr;
514 state->strm.avail_in = mi->b_wptr - rptr;
516 flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
517 rlen = state->strm.avail_in + PPP_HDRLEN + DEFLATE_OVHD;
518 state->strm.next_out = wptr + 3;
519 state->strm.avail_out = 1;
524 * Call inflate, supplying more input or output as needed.
527 r = inflate(&state->strm, flush);
530 printf("z_decompress%d: inflate returned %d (%s)\n",
531 state->unit, r, (state->strm.msg? state->strm.msg: ""));
533 return DECOMP_FATALERROR;
535 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
536 break; /* all done */
537 if (state->strm.avail_in == 0 && mi != NULL) {
538 state->strm.next_in = mi->b_rptr;
539 state->strm.avail_in = mi->b_wptr - mi->b_rptr;
540 rlen += state->strm.avail_in;
543 flush = Z_PACKET_FLUSH;
545 if (state->strm.avail_out == 0) {
547 state->strm.avail_out = ospace - PPP_HDRLEN;
548 if ((wptr[3] & 1) == 0) {
549 /* 2-byte protocol field */
551 --state->strm.next_out;
552 ++state->strm.avail_out;
557 mo->b_wptr += ospace;
559 mo->b_cont = allocb(DECOMP_CHUNK, BPRI_MED);
565 state->strm.next_out = mo->b_rptr;
566 state->strm.avail_out = ospace = DECOMP_CHUNK;
574 mo->b_wptr += ospace - state->strm.avail_out;
575 olen += ospace - state->strm.avail_out;
577 state->stats.unc_bytes += olen;
578 state->stats.unc_packets++;
579 state->stats.comp_bytes += rlen;
580 state->stats.comp_packets++;
587 * Incompressible data has arrived - add it to the history.
594 struct deflate_state *state = (struct deflate_state *) arg;
599 * Check that the protocol is one we handle.
602 if (rptr + PPP_HDRLEN > mi->b_wptr) {
603 if (!pullupmsg(mi, PPP_HDRLEN))
607 proto = PPP_PROTOCOL(rptr);
608 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
614 * Iterate through the message blocks, adding the characters in them
615 * to the decompressor's history. For the first block, we start
616 * at the either the 1st or 2nd byte of the protocol field,
617 * depending on whether the protocol value is compressible.
619 rlen = mi->b_wptr - mi->b_rptr;
620 state->strm.next_in = rptr + 3;
621 state->strm.avail_in = rlen - 3;
623 --state->strm.next_in;
624 ++state->strm.avail_in;
627 r = inflateIncomp(&state->strm);
631 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
632 state->unit, r, (state->strm.msg? state->strm.msg: ""));
639 state->strm.next_in = mi->b_rptr;
640 state->strm.avail_in = mi->b_wptr - mi->b_rptr;
641 rlen += state->strm.avail_in;
647 state->stats.inc_bytes += rlen;
648 state->stats.inc_packets++;
649 state->stats.unc_bytes += rlen;
650 state->stats.unc_packets++;
653 #endif /* DO_DEFLATE */