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 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
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
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
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.11 2002/12/06 09:49:15 paulus Exp $
42 #include <net/net_globals.h>
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/stream.h>
47 #include <net/ppp_defs.h>
50 #define PACKETPTR mblk_t *
51 #include <net/ppp-comp.h>
56 #include "../common/zlib.h"
61 #define DEFLATE_DEBUG 1
64 * State for a Deflate (de)compressor.
66 struct deflate_state {
74 struct compstat stats;
77 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
79 static void *z_alloc __P((void *, u_int items, u_int size));
80 static void *z_alloc_init __P((void *, u_int items, u_int size));
81 static void z_free __P((void *, void *ptr));
82 static void *z_comp_alloc __P((u_char *options, int opt_len));
83 static void *z_decomp_alloc __P((u_char *options, int opt_len));
84 static void z_comp_free __P((void *state));
85 static void z_decomp_free __P((void *state));
86 static int z_comp_init __P((void *state, u_char *options, int opt_len,
87 int unit, int hdrlen, int debug));
88 static int z_decomp_init __P((void *state, u_char *options, int opt_len,
89 int unit, int hdrlen, int mru, int debug));
90 static int z_compress __P((void *state, mblk_t **mret,
91 mblk_t *mp, int slen, int maxolen));
92 static void z_incomp __P((void *state, mblk_t *dmsg));
93 static int z_decompress __P((void *state, mblk_t *cmp,
95 static void z_comp_reset __P((void *state));
96 static void z_decomp_reset __P((void *state));
97 static void z_comp_stats __P((void *state, struct compstat *stats));
100 * Procedures exported to ppp_comp.c.
102 struct compressor ppp_deflate = {
103 CI_DEFLATE, /* compress_proto */
104 z_comp_alloc, /* comp_alloc */
105 z_comp_free, /* comp_free */
106 z_comp_init, /* comp_init */
107 z_comp_reset, /* comp_reset */
108 z_compress, /* compress */
109 z_comp_stats, /* comp_stat */
110 z_decomp_alloc, /* decomp_alloc */
111 z_decomp_free, /* decomp_free */
112 z_decomp_init, /* decomp_init */
113 z_decomp_reset, /* decomp_reset */
114 z_decompress, /* decompress */
115 z_incomp, /* incomp */
116 z_comp_stats, /* decomp_stat */
119 struct compressor ppp_deflate_draft = {
120 CI_DEFLATE_DRAFT, /* compress_proto */
121 z_comp_alloc, /* comp_alloc */
122 z_comp_free, /* comp_free */
123 z_comp_init, /* comp_init */
124 z_comp_reset, /* comp_reset */
125 z_compress, /* compress */
126 z_comp_stats, /* comp_stat */
127 z_decomp_alloc, /* decomp_alloc */
128 z_decomp_free, /* decomp_free */
129 z_decomp_init, /* decomp_init */
130 z_decomp_reset, /* decomp_reset */
131 z_decompress, /* decompress */
132 z_incomp, /* incomp */
133 z_comp_stats, /* decomp_stat */
136 #define DECOMP_CHUNK 512
139 * Space allocation and freeing routines for use by zlib routines.
146 #define GUARD_MAGIC 0x77a6011a
149 z_alloc_init(notused, items, size)
155 size = items * size + sizeof(struct zchunk);
157 z = (struct zchunk *) ALLOC_SLEEP(size);
159 z = (struct zchunk *) ALLOC_NOSLEEP(size);
162 z->guard = GUARD_MAGIC;
163 return (void *) (z + 1);
167 z_alloc(notused, items, size)
173 size = items * size + sizeof(struct zchunk);
174 z = (struct zchunk *) ALLOC_NOSLEEP(size);
176 z->guard = GUARD_MAGIC;
177 return (void *) (z + 1);
185 struct zchunk *z = ((struct zchunk *) ptr) - 1;
187 if (z->guard != GUARD_MAGIC) {
188 printf("ppp: z_free of corrupted chunk at %x (%x, %x)\n",
189 z, z->size, z->guard);
196 * Allocate space for a compressor.
199 z_comp_alloc(options, opt_len)
203 struct deflate_state *state;
206 if (opt_len != CILEN_DEFLATE
207 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
208 || options[1] != CILEN_DEFLATE
209 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
210 || options[3] != DEFLATE_CHK_SEQUENCE)
212 w_size = DEFLATE_SIZE(options[2]);
214 * N.B. the 9 below should be DEFLATE_MIN_SIZE (8), but using
215 * 8 will cause kernel crashes because of a bug in zlib.
217 if (w_size < 9 || w_size > DEFLATE_MAX_SIZE)
222 state = (struct deflate_state *) ALLOC_SLEEP(sizeof(*state));
224 state = (struct deflate_state *) ALLOC_NOSLEEP(sizeof(*state));
230 state->strm.next_in = NULL;
231 state->strm.zalloc = (alloc_func) z_alloc_init;
232 state->strm.zfree = (free_func) z_free;
233 if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
234 -w_size, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
235 FREE(state, sizeof(*state));
239 state->strm.zalloc = (alloc_func) z_alloc;
240 state->w_size = w_size;
241 bzero(&state->stats, sizeof(state->stats));
242 return (void *) state;
249 struct deflate_state *state = (struct deflate_state *) arg;
251 deflateEnd(&state->strm);
252 FREE(state, sizeof(*state));
256 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
259 int opt_len, unit, hdrlen, debug;
261 struct deflate_state *state = (struct deflate_state *) arg;
263 if (opt_len < CILEN_DEFLATE
264 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
265 || options[1] != CILEN_DEFLATE
266 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
267 || DEFLATE_SIZE(options[2]) != state->w_size
268 || options[3] != DEFLATE_CHK_SEQUENCE)
273 state->hdrlen = hdrlen;
274 state->debug = debug;
276 deflateReset(&state->strm);
285 struct deflate_state *state = (struct deflate_state *) arg;
288 deflateReset(&state->strm);
292 z_compress(arg, mret, mp, orig_len, maxolen)
294 mblk_t **mret; /* compressed packet (out) */
295 mblk_t *mp; /* uncompressed packet (in) */
296 int orig_len, maxolen;
298 struct deflate_state *state = (struct deflate_state *) arg;
300 int proto, olen, wspace, r, flush;
304 * Check that the protocol is in the range we handle.
308 if (rptr + PPP_HDRLEN > mp->b_wptr) {
309 if (!pullupmsg(mp, PPP_HDRLEN))
313 proto = PPP_PROTOCOL(rptr);
314 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
317 /* Allocate one mblk initially. */
318 if (maxolen > orig_len)
320 if (maxolen <= PPP_HDRLEN + 2) {
324 wspace = maxolen + state->hdrlen;
327 m = allocb(wspace, BPRI_MED);
331 if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
332 m->b_rptr += state->hdrlen;
333 m->b_wptr = m->b_rptr;
334 wspace -= state->hdrlen;
339 * Copy over the PPP header and store the 2-byte sequence number.
341 wptr[0] = PPP_ADDRESS(rptr);
342 wptr[1] = PPP_CONTROL(rptr);
343 wptr[2] = PPP_COMP >> 8;
346 wptr[0] = state->seqno >> 8;
347 wptr[1] = state->seqno;
349 state->strm.next_out = wptr;
350 state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
352 state->strm.next_out = NULL;
353 state->strm.avail_out = 1000000;
357 rptr += (proto > 0xff)? 2: 3; /* skip 1st proto byte if 0 */
358 state->strm.next_in = rptr;
359 state->strm.avail_in = mp->b_wptr - rptr;
361 flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
364 r = deflate(&state->strm, flush);
366 printf("z_compress: deflate returned %d (%s)\n",
367 r, (state->strm.msg? state->strm.msg: ""));
370 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
371 break; /* all done */
372 if (state->strm.avail_in == 0 && mp != NULL) {
373 state->strm.next_in = mp->b_rptr;
374 state->strm.avail_in = mp->b_wptr - mp->b_rptr;
377 flush = Z_PACKET_FLUSH;
379 if (state->strm.avail_out == 0) {
383 wspace = maxolen - olen;
390 else if (wspace > 4096)
392 m->b_cont = allocb(wspace, BPRI_MED);
396 state->strm.next_out = m->b_wptr;
397 state->strm.avail_out = wspace;
401 state->strm.next_out = NULL;
402 state->strm.avail_out = 1000000;
407 m->b_wptr += wspace - state->strm.avail_out;
408 olen += wspace - state->strm.avail_out;
412 * See if we managed to reduce the size of the packet.
414 if (olen < orig_len && m != NULL) {
415 state->stats.comp_bytes += olen;
416 state->stats.comp_packets++;
422 state->stats.inc_bytes += orig_len;
423 state->stats.inc_packets++;
426 state->stats.unc_bytes += orig_len;
427 state->stats.unc_packets++;
433 z_comp_stats(arg, stats)
435 struct compstat *stats;
437 struct deflate_state *state = (struct deflate_state *) arg;
440 *stats = state->stats;
441 stats->ratio = stats->unc_bytes;
442 out = stats->comp_bytes + stats->unc_bytes;
443 if (stats->ratio <= 0x7ffffff)
452 * Allocate space for a decompressor.
455 z_decomp_alloc(options, opt_len)
459 struct deflate_state *state;
462 if (opt_len != CILEN_DEFLATE
463 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
464 || options[1] != CILEN_DEFLATE
465 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
466 || options[3] != DEFLATE_CHK_SEQUENCE)
468 w_size = DEFLATE_SIZE(options[2]);
470 * N.B. the 9 below should be DEFLATE_MIN_SIZE (8), but using
471 * 8 will cause kernel crashes because of a bug in zlib.
473 if (w_size < 9 || w_size > DEFLATE_MAX_SIZE)
477 state = (struct deflate_state *) ALLOC_SLEEP(sizeof(*state));
479 state = (struct deflate_state *) ALLOC_NOSLEEP(sizeof(*state));
484 state->strm.next_out = NULL;
485 state->strm.zalloc = (alloc_func) z_alloc_init;
486 state->strm.zfree = (free_func) z_free;
487 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
488 FREE(state, sizeof(*state));
492 state->strm.zalloc = (alloc_func) z_alloc;
493 state->w_size = w_size;
494 bzero(&state->stats, sizeof(state->stats));
495 return (void *) state;
502 struct deflate_state *state = (struct deflate_state *) arg;
504 inflateEnd(&state->strm);
505 FREE(state, sizeof(*state));
509 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
512 int opt_len, unit, hdrlen, mru, debug;
514 struct deflate_state *state = (struct deflate_state *) arg;
516 if (opt_len < CILEN_DEFLATE
517 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
518 || options[1] != CILEN_DEFLATE
519 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
520 || DEFLATE_SIZE(options[2]) != state->w_size
521 || options[3] != DEFLATE_CHK_SEQUENCE)
526 state->hdrlen = hdrlen;
527 state->debug = debug;
530 inflateReset(&state->strm);
539 struct deflate_state *state = (struct deflate_state *) arg;
542 inflateReset(&state->strm);
546 * Decompress a Deflate-compressed packet.
548 * Because of patent problems, we return DECOMP_ERROR for errors
549 * found by inspecting the input data and for system problems, but
550 * DECOMP_FATALERROR for any errors which could possibly be said to
551 * be being detected "after" decompression. For DECOMP_ERROR,
552 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
553 * infringing a patent of Motorola's if we do, so we take CCP down
556 * Given that the frame has the correct sequence number and a good FCS,
557 * errors such as invalid codes in the input most likely indicate a
558 * bug, so we return DECOMP_FATALERROR for them in order to turn off
559 * compression, even though they are detected by inspecting the input.
562 z_decompress(arg, mi, mop)
566 struct deflate_state *state = (struct deflate_state *) arg;
567 mblk_t *mo, *mo_head;
569 int rlen, olen, ospace;
570 int seq, i, flush, r, decode_proto;
571 u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
575 for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
576 while (rptr >= mi->b_wptr) {
585 /* Check the sequence number. */
586 seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
587 if (seq != state->seqno) {
591 printf("z_decompress%d: bad seq # %d, expected %d\n",
592 state->unit, seq, state->seqno);
597 /* Allocate an output message block. */
598 mo = allocb(DECOMP_CHUNK + state->hdrlen, BPRI_MED);
603 mo->b_rptr += state->hdrlen;
604 mo->b_wptr = wptr = mo->b_rptr;
605 ospace = DECOMP_CHUNK;
609 * Fill in the first part of the PPP header. The protocol field
610 * comes from the decompressed data.
612 wptr[0] = PPP_ADDRESS(hdr);
613 wptr[1] = PPP_CONTROL(hdr);
617 * Set up to call inflate. We set avail_out to 1 initially so we can
618 * look at the first byte of the output and decide whether we have
619 * a 1-byte or 2-byte protocol field.
621 state->strm.next_in = rptr;
622 state->strm.avail_in = mi->b_wptr - rptr;
624 flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
625 rlen = state->strm.avail_in + PPP_HDRLEN + DEFLATE_OVHD;
626 state->strm.next_out = wptr + 3;
627 state->strm.avail_out = 1;
631 * Call inflate, supplying more input or output as needed.
634 r = inflate(&state->strm, flush);
639 printf("z_decompress%d: inflate returned %d (%s)\n",
640 state->unit, r, (state->strm.msg? state->strm.msg: ""));
642 return DECOMP_FATALERROR;
644 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
645 break; /* all done */
646 if (state->strm.avail_in == 0 && mi != NULL) {
647 state->strm.next_in = mi->b_rptr;
648 state->strm.avail_in = mi->b_wptr - mi->b_rptr;
649 rlen += state->strm.avail_in;
652 flush = Z_PACKET_FLUSH;
654 if (state->strm.avail_out == 0) {
656 state->strm.avail_out = ospace - PPP_HDRLEN;
657 if ((wptr[3] & 1) == 0) {
658 /* 2-byte protocol field */
660 --state->strm.next_out;
661 ++state->strm.avail_out;
665 mo->b_wptr += ospace;
667 mo->b_cont = allocb(DECOMP_CHUNK, BPRI_MED);
673 state->strm.next_out = mo->b_rptr;
674 state->strm.avail_out = ospace = DECOMP_CHUNK;
682 mo->b_wptr += ospace - state->strm.avail_out;
683 olen += ospace - state->strm.avail_out;
686 if (olen > state->mru + PPP_HDRLEN)
687 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
688 state->unit, olen, state->mru + PPP_HDRLEN);
691 state->stats.unc_bytes += olen;
692 state->stats.unc_packets++;
693 state->stats.comp_bytes += rlen;
694 state->stats.comp_packets++;
701 * Incompressible data has arrived - add it to the history.
708 struct deflate_state *state = (struct deflate_state *) arg;
713 * Check that the protocol is one we handle.
716 if (rptr + PPP_HDRLEN > mi->b_wptr) {
717 if (!pullupmsg(mi, PPP_HDRLEN))
721 proto = PPP_PROTOCOL(rptr);
722 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
728 * Iterate through the message blocks, adding the characters in them
729 * to the decompressor's history. For the first block, we start
730 * at the either the 1st or 2nd byte of the protocol field,
731 * depending on whether the protocol value is compressible.
733 rlen = mi->b_wptr - mi->b_rptr;
734 state->strm.next_in = rptr + 3;
735 state->strm.avail_in = rlen - 3;
737 --state->strm.next_in;
738 ++state->strm.avail_in;
741 r = inflateIncomp(&state->strm);
747 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
748 state->unit, r, (state->strm.msg? state->strm.msg: ""));
754 state->strm.next_in = mi->b_rptr;
755 state->strm.avail_in = mi->b_wptr - mi->b_rptr;
756 rlen += state->strm.avail_in;
762 state->stats.inc_bytes += rlen;
763 state->stats.inc_packets++;
764 state->stats.unc_bytes += rlen;
765 state->stats.unc_packets++;
768 #endif /* DO_DEFLATE */