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.9 1999/01/19 23:58:35 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>
48 #include "../common/zlib.h"
53 #define DEFLATE_DEBUG 1
56 * State for a Deflate (de)compressor.
58 struct deflate_state {
66 struct compstat stats;
69 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
71 static void *z_alloc __P((void *, u_int items, u_int size));
72 static void *z_alloc_init __P((void *, u_int items, u_int size));
73 static void z_free __P((void *, void *ptr));
74 static void *z_comp_alloc __P((u_char *options, int opt_len));
75 static void *z_decomp_alloc __P((u_char *options, int opt_len));
76 static void z_comp_free __P((void *state));
77 static void z_decomp_free __P((void *state));
78 static int z_comp_init __P((void *state, u_char *options, int opt_len,
79 int unit, int hdrlen, int debug));
80 static int z_decomp_init __P((void *state, u_char *options, int opt_len,
81 int unit, int hdrlen, int mru, int debug));
82 static int z_compress __P((void *state, mblk_t **mret,
83 mblk_t *mp, int slen, int maxolen));
84 static void z_incomp __P((void *state, mblk_t *dmsg));
85 static int z_decompress __P((void *state, mblk_t *cmp,
87 static void z_comp_reset __P((void *state));
88 static void z_decomp_reset __P((void *state));
89 static void z_comp_stats __P((void *state, struct compstat *stats));
92 * Procedures exported to ppp_comp.c.
94 struct compressor ppp_deflate = {
95 CI_DEFLATE, /* compress_proto */
96 z_comp_alloc, /* comp_alloc */
97 z_comp_free, /* comp_free */
98 z_comp_init, /* comp_init */
99 z_comp_reset, /* comp_reset */
100 z_compress, /* compress */
101 z_comp_stats, /* comp_stat */
102 z_decomp_alloc, /* decomp_alloc */
103 z_decomp_free, /* decomp_free */
104 z_decomp_init, /* decomp_init */
105 z_decomp_reset, /* decomp_reset */
106 z_decompress, /* decompress */
107 z_incomp, /* incomp */
108 z_comp_stats, /* decomp_stat */
111 struct compressor ppp_deflate_draft = {
112 CI_DEFLATE_DRAFT, /* compress_proto */
113 z_comp_alloc, /* comp_alloc */
114 z_comp_free, /* comp_free */
115 z_comp_init, /* comp_init */
116 z_comp_reset, /* comp_reset */
117 z_compress, /* compress */
118 z_comp_stats, /* comp_stat */
119 z_decomp_alloc, /* decomp_alloc */
120 z_decomp_free, /* decomp_free */
121 z_decomp_init, /* decomp_init */
122 z_decomp_reset, /* decomp_reset */
123 z_decompress, /* decompress */
124 z_incomp, /* incomp */
125 z_comp_stats, /* decomp_stat */
128 #define DECOMP_CHUNK 512
131 * Space allocation and freeing routines for use by zlib routines.
138 #define GUARD_MAGIC 0x77a6011a
141 z_alloc_init(notused, items, size)
147 size = items * size + sizeof(struct zchunk);
149 z = (struct zchunk *) ALLOC_SLEEP(size);
151 z = (struct zchunk *) ALLOC_NOSLEEP(size);
154 z->guard = GUARD_MAGIC;
155 return (void *) (z + 1);
159 z_alloc(notused, items, size)
165 size = items * size + sizeof(struct zchunk);
166 z = (struct zchunk *) ALLOC_NOSLEEP(size);
168 z->guard = GUARD_MAGIC;
169 return (void *) (z + 1);
177 struct zchunk *z = ((struct zchunk *) ptr) - 1;
179 if (z->guard != GUARD_MAGIC) {
180 printf("ppp: z_free of corrupted chunk at %x (%x, %x)\n",
181 z, z->size, z->guard);
188 * Allocate space for a compressor.
191 z_comp_alloc(options, opt_len)
195 struct deflate_state *state;
198 if (opt_len != CILEN_DEFLATE
199 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
200 || options[1] != CILEN_DEFLATE
201 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
202 || options[3] != DEFLATE_CHK_SEQUENCE)
204 w_size = DEFLATE_SIZE(options[2]);
205 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
210 state = (struct deflate_state *) ALLOC_SLEEP(sizeof(*state));
212 state = (struct deflate_state *) ALLOC_NOSLEEP(sizeof(*state));
218 state->strm.next_in = NULL;
219 state->strm.zalloc = (alloc_func) z_alloc_init;
220 state->strm.zfree = (free_func) z_free;
221 if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
222 -w_size, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
223 FREE(state, sizeof(*state));
227 state->strm.zalloc = (alloc_func) z_alloc;
228 state->w_size = w_size;
229 bzero(&state->stats, sizeof(state->stats));
230 return (void *) state;
237 struct deflate_state *state = (struct deflate_state *) arg;
239 deflateEnd(&state->strm);
240 FREE(state, sizeof(*state));
244 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
247 int opt_len, unit, hdrlen, debug;
249 struct deflate_state *state = (struct deflate_state *) arg;
251 if (opt_len < CILEN_DEFLATE
252 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
253 || options[1] != CILEN_DEFLATE
254 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
255 || DEFLATE_SIZE(options[2]) != state->w_size
256 || options[3] != DEFLATE_CHK_SEQUENCE)
261 state->hdrlen = hdrlen;
262 state->debug = debug;
264 deflateReset(&state->strm);
273 struct deflate_state *state = (struct deflate_state *) arg;
276 deflateReset(&state->strm);
280 z_compress(arg, mret, mp, orig_len, maxolen)
282 mblk_t **mret; /* compressed packet (out) */
283 mblk_t *mp; /* uncompressed packet (in) */
284 int orig_len, maxolen;
286 struct deflate_state *state = (struct deflate_state *) arg;
288 int proto, olen, wspace, r, flush;
292 * Check that the protocol is in the range we handle.
296 if (rptr + PPP_HDRLEN > mp->b_wptr) {
297 if (!pullupmsg(mp, PPP_HDRLEN))
301 proto = PPP_PROTOCOL(rptr);
302 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
305 /* Allocate one mblk initially. */
306 if (maxolen > orig_len)
308 if (maxolen <= PPP_HDRLEN + 2) {
312 wspace = maxolen + state->hdrlen;
315 m = allocb(wspace, BPRI_MED);
319 if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
320 m->b_rptr += state->hdrlen;
321 m->b_wptr = m->b_rptr;
322 wspace -= state->hdrlen;
327 * Copy over the PPP header and store the 2-byte sequence number.
329 wptr[0] = PPP_ADDRESS(rptr);
330 wptr[1] = PPP_CONTROL(rptr);
331 wptr[2] = PPP_COMP >> 8;
334 wptr[0] = state->seqno >> 8;
335 wptr[1] = state->seqno;
337 state->strm.next_out = wptr;
338 state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
340 state->strm.next_out = NULL;
341 state->strm.avail_out = 1000000;
345 rptr += (proto > 0xff)? 2: 3; /* skip 1st proto byte if 0 */
346 state->strm.next_in = rptr;
347 state->strm.avail_in = mp->b_wptr - rptr;
349 flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
352 r = deflate(&state->strm, flush);
354 printf("z_compress: deflate returned %d (%s)\n",
355 r, (state->strm.msg? state->strm.msg: ""));
358 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
359 break; /* all done */
360 if (state->strm.avail_in == 0 && mp != NULL) {
361 state->strm.next_in = mp->b_rptr;
362 state->strm.avail_in = mp->b_wptr - mp->b_rptr;
365 flush = Z_PACKET_FLUSH;
367 if (state->strm.avail_out == 0) {
371 wspace = maxolen - olen;
378 else if (wspace > 4096)
380 m->b_cont = allocb(wspace, BPRI_MED);
384 state->strm.next_out = m->b_wptr;
385 state->strm.avail_out = wspace;
389 state->strm.next_out = NULL;
390 state->strm.avail_out = 1000000;
395 m->b_wptr += wspace - state->strm.avail_out;
396 olen += wspace - state->strm.avail_out;
400 * See if we managed to reduce the size of the packet.
402 if (olen < orig_len && m != NULL) {
403 state->stats.comp_bytes += olen;
404 state->stats.comp_packets++;
410 state->stats.inc_bytes += orig_len;
411 state->stats.inc_packets++;
414 state->stats.unc_bytes += orig_len;
415 state->stats.unc_packets++;
421 z_comp_stats(arg, stats)
423 struct compstat *stats;
425 struct deflate_state *state = (struct deflate_state *) arg;
428 *stats = state->stats;
429 stats->ratio = stats->unc_bytes;
430 out = stats->comp_bytes + stats->unc_bytes;
431 if (stats->ratio <= 0x7ffffff)
440 * Allocate space for a decompressor.
443 z_decomp_alloc(options, opt_len)
447 struct deflate_state *state;
450 if (opt_len != CILEN_DEFLATE
451 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
452 || options[1] != CILEN_DEFLATE
453 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
454 || options[3] != DEFLATE_CHK_SEQUENCE)
456 w_size = DEFLATE_SIZE(options[2]);
457 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
461 state = (struct deflate_state *) ALLOC_SLEEP(sizeof(*state));
463 state = (struct deflate_state *) ALLOC_NOSLEEP(sizeof(*state));
468 state->strm.next_out = NULL;
469 state->strm.zalloc = (alloc_func) z_alloc_init;
470 state->strm.zfree = (free_func) z_free;
471 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
472 FREE(state, sizeof(*state));
476 state->strm.zalloc = (alloc_func) z_alloc;
477 state->w_size = w_size;
478 bzero(&state->stats, sizeof(state->stats));
479 return (void *) state;
486 struct deflate_state *state = (struct deflate_state *) arg;
488 inflateEnd(&state->strm);
489 FREE(state, sizeof(*state));
493 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
496 int opt_len, unit, hdrlen, mru, debug;
498 struct deflate_state *state = (struct deflate_state *) arg;
500 if (opt_len < CILEN_DEFLATE
501 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
502 || options[1] != CILEN_DEFLATE
503 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
504 || DEFLATE_SIZE(options[2]) != state->w_size
505 || options[3] != DEFLATE_CHK_SEQUENCE)
510 state->hdrlen = hdrlen;
511 state->debug = debug;
514 inflateReset(&state->strm);
523 struct deflate_state *state = (struct deflate_state *) arg;
526 inflateReset(&state->strm);
530 * Decompress a Deflate-compressed packet.
532 * Because of patent problems, we return DECOMP_ERROR for errors
533 * found by inspecting the input data and for system problems, but
534 * DECOMP_FATALERROR for any errors which could possibly be said to
535 * be being detected "after" decompression. For DECOMP_ERROR,
536 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
537 * infringing a patent of Motorola's if we do, so we take CCP down
540 * Given that the frame has the correct sequence number and a good FCS,
541 * errors such as invalid codes in the input most likely indicate a
542 * bug, so we return DECOMP_FATALERROR for them in order to turn off
543 * compression, even though they are detected by inspecting the input.
546 z_decompress(arg, mi, mop)
550 struct deflate_state *state = (struct deflate_state *) arg;
551 mblk_t *mo, *mo_head;
553 int rlen, olen, ospace;
554 int seq, i, flush, r, decode_proto;
555 u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
559 for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
560 while (rptr >= mi->b_wptr) {
569 /* Check the sequence number. */
570 seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
571 if (seq != state->seqno) {
575 printf("z_decompress%d: bad seq # %d, expected %d\n",
576 state->unit, seq, state->seqno);
581 /* Allocate an output message block. */
582 mo = allocb(DECOMP_CHUNK + state->hdrlen, BPRI_MED);
587 mo->b_rptr += state->hdrlen;
588 mo->b_wptr = wptr = mo->b_rptr;
589 ospace = DECOMP_CHUNK;
593 * Fill in the first part of the PPP header. The protocol field
594 * comes from the decompressed data.
596 wptr[0] = PPP_ADDRESS(hdr);
597 wptr[1] = PPP_CONTROL(hdr);
601 * Set up to call inflate. We set avail_out to 1 initially so we can
602 * look at the first byte of the output and decide whether we have
603 * a 1-byte or 2-byte protocol field.
605 state->strm.next_in = rptr;
606 state->strm.avail_in = mi->b_wptr - rptr;
608 flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
609 rlen = state->strm.avail_in + PPP_HDRLEN + DEFLATE_OVHD;
610 state->strm.next_out = wptr + 3;
611 state->strm.avail_out = 1;
615 * Call inflate, supplying more input or output as needed.
618 r = inflate(&state->strm, flush);
623 printf("z_decompress%d: inflate returned %d (%s)\n",
624 state->unit, r, (state->strm.msg? state->strm.msg: ""));
626 return DECOMP_FATALERROR;
628 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
629 break; /* all done */
630 if (state->strm.avail_in == 0 && mi != NULL) {
631 state->strm.next_in = mi->b_rptr;
632 state->strm.avail_in = mi->b_wptr - mi->b_rptr;
633 rlen += state->strm.avail_in;
636 flush = Z_PACKET_FLUSH;
638 if (state->strm.avail_out == 0) {
640 state->strm.avail_out = ospace - PPP_HDRLEN;
641 if ((wptr[3] & 1) == 0) {
642 /* 2-byte protocol field */
644 --state->strm.next_out;
645 ++state->strm.avail_out;
649 mo->b_wptr += ospace;
651 mo->b_cont = allocb(DECOMP_CHUNK, BPRI_MED);
657 state->strm.next_out = mo->b_rptr;
658 state->strm.avail_out = ospace = DECOMP_CHUNK;
666 mo->b_wptr += ospace - state->strm.avail_out;
667 olen += ospace - state->strm.avail_out;
670 if (olen > state->mru + PPP_HDRLEN)
671 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
672 state->unit, olen, state->mru + PPP_HDRLEN);
675 state->stats.unc_bytes += olen;
676 state->stats.unc_packets++;
677 state->stats.comp_bytes += rlen;
678 state->stats.comp_packets++;
685 * Incompressible data has arrived - add it to the history.
692 struct deflate_state *state = (struct deflate_state *) arg;
697 * Check that the protocol is one we handle.
700 if (rptr + PPP_HDRLEN > mi->b_wptr) {
701 if (!pullupmsg(mi, PPP_HDRLEN))
705 proto = PPP_PROTOCOL(rptr);
706 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
712 * Iterate through the message blocks, adding the characters in them
713 * to the decompressor's history. For the first block, we start
714 * at the either the 1st or 2nd byte of the protocol field,
715 * depending on whether the protocol value is compressible.
717 rlen = mi->b_wptr - mi->b_rptr;
718 state->strm.next_in = rptr + 3;
719 state->strm.avail_in = rlen - 3;
721 --state->strm.next_in;
722 ++state->strm.avail_in;
725 r = inflateIncomp(&state->strm);
731 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
732 state->unit, r, (state->strm.msg? state->strm.msg: ""));
738 state->strm.next_in = mi->b_rptr;
739 state->strm.avail_in = mi->b_wptr - mi->b_rptr;
740 rlen += state->strm.avail_in;
746 state->stats.inc_bytes += rlen;
747 state->stats.inc_packets++;
748 state->stats.unc_bytes += rlen;
749 state->stats.unc_packets++;
752 #endif /* DO_DEFLATE */