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.6 1997/11/27 06:05:17 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 if_ppp.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 #define DECOMP_CHUNK 512
114 * Space allocation and freeing routines for use by zlib routines.
121 #define GUARD_MAGIC 0x77a6011a
124 z_alloc_init(notused, items, size)
130 size = items * size + sizeof(struct zchunk);
132 z = (struct zchunk *) ALLOC_SLEEP(size);
134 z = (struct zchunk *) ALLOC_NOSLEEP(size);
137 z->guard = GUARD_MAGIC;
138 return (void *) (z + 1);
142 z_alloc(notused, items, size)
148 size = items * size + sizeof(struct zchunk);
149 z = (struct zchunk *) ALLOC_NOSLEEP(size);
151 z->guard = GUARD_MAGIC;
152 return (void *) (z + 1);
160 struct zchunk *z = ((struct zchunk *) ptr) - 1;
162 if (z->guard != GUARD_MAGIC) {
163 printf("ppp: z_free of corrupted chunk at %x (%x, %x)\n",
164 z, z->size, z->guard);
171 * Allocate space for a compressor.
174 z_comp_alloc(options, opt_len)
178 struct deflate_state *state;
181 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
182 || options[1] != CILEN_DEFLATE
183 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
184 || options[3] != DEFLATE_CHK_SEQUENCE)
186 w_size = DEFLATE_SIZE(options[2]);
187 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
192 state = (struct deflate_state *) ALLOC_SLEEP(sizeof(*state));
194 state = (struct deflate_state *) ALLOC_NOSLEEP(sizeof(*state));
200 state->strm.next_in = NULL;
201 state->strm.zalloc = (alloc_func) z_alloc_init;
202 state->strm.zfree = (free_func) z_free;
203 if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
204 -w_size, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
205 FREE(state, sizeof(*state));
209 state->strm.zalloc = (alloc_func) z_alloc;
210 state->w_size = w_size;
211 bzero(&state->stats, sizeof(state->stats));
212 return (void *) state;
219 struct deflate_state *state = (struct deflate_state *) arg;
221 deflateEnd(&state->strm);
222 FREE(state, sizeof(*state));
226 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
229 int opt_len, unit, hdrlen, debug;
231 struct deflate_state *state = (struct deflate_state *) arg;
233 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
234 || options[1] != CILEN_DEFLATE
235 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
236 || DEFLATE_SIZE(options[2]) != state->w_size
237 || options[3] != DEFLATE_CHK_SEQUENCE)
242 state->hdrlen = hdrlen;
243 state->debug = debug;
245 deflateReset(&state->strm);
254 struct deflate_state *state = (struct deflate_state *) arg;
257 deflateReset(&state->strm);
261 z_compress(arg, mret, mp, orig_len, maxolen)
263 mblk_t **mret; /* compressed packet (out) */
264 mblk_t *mp; /* uncompressed packet (in) */
265 int orig_len, maxolen;
267 struct deflate_state *state = (struct deflate_state *) arg;
269 int proto, olen, wspace, r, flush;
273 * Check that the protocol is in the range we handle.
277 if (rptr + PPP_HDRLEN > mp->b_wptr) {
278 if (!pullupmsg(mp, PPP_HDRLEN))
282 proto = PPP_PROTOCOL(rptr);
283 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
286 /* Allocate one mblk initially. */
287 if (maxolen > orig_len)
289 wspace = maxolen < 4096? maxolen: 4096;
290 m = allocb(wspace, BPRI_MED);
293 if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
294 m->b_rptr += state->hdrlen;
295 m->b_wptr = m->b_rptr;
296 wspace -= state->hdrlen;
301 * Copy over the PPP header and store the 2-byte sequence number.
303 wptr[0] = PPP_ADDRESS(rptr);
304 wptr[1] = PPP_CONTROL(rptr);
305 wptr[2] = PPP_COMP >> 8;
308 wptr[0] = state->seqno >> 8;
309 wptr[1] = state->seqno;
311 state->strm.next_out = wptr;
312 state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
314 state->strm.next_out = NULL;
315 state->strm.avail_out = 1000000;
319 rptr += (proto > 0xff)? 2: 3; /* skip 1st proto byte if 0 */
320 state->strm.next_in = rptr;
321 state->strm.avail_in = mp->b_wptr - rptr;
323 flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
326 r = deflate(&state->strm, flush);
328 printf("z_compress: deflate returned %d (%s)\n",
329 r, (state->strm.msg? state->strm.msg: ""));
332 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
333 break; /* all done */
334 if (state->strm.avail_in == 0 && mp != NULL) {
335 state->strm.next_in = mp->b_rptr;
336 state->strm.avail_in = mp->b_wptr - mp->b_rptr;
339 flush = Z_PACKET_FLUSH;
341 if (state->strm.avail_out == 0) {
345 wspace = maxolen - olen;
348 else if (wspace > 4096)
350 m->b_cont = allocb(wspace, BPRI_MED);
353 state->strm.next_out = m->b_wptr;
354 state->strm.avail_out = wspace;
358 state->strm.next_out = NULL;
359 state->strm.avail_out = 1000000;
363 m->b_wptr += wspace - state->strm.avail_out;
364 olen += wspace - state->strm.avail_out;
367 * See if we managed to reduce the size of the packet.
369 if (olen < orig_len && m != NULL) {
370 state->stats.comp_bytes += olen;
371 state->stats.comp_packets++;
377 state->stats.inc_bytes += orig_len;
378 state->stats.inc_packets++;
381 state->stats.unc_bytes += orig_len;
382 state->stats.unc_packets++;
388 z_comp_stats(arg, stats)
390 struct compstat *stats;
392 struct deflate_state *state = (struct deflate_state *) arg;
395 *stats = state->stats;
396 stats->ratio = stats->unc_bytes;
397 out = stats->comp_bytes + stats->unc_bytes;
398 if (stats->ratio <= 0x7ffffff)
407 * Allocate space for a decompressor.
410 z_decomp_alloc(options, opt_len)
414 struct deflate_state *state;
417 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
418 || options[1] != CILEN_DEFLATE
419 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
420 || options[3] != DEFLATE_CHK_SEQUENCE)
422 w_size = DEFLATE_SIZE(options[2]);
423 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
427 state = (struct deflate_state *) ALLOC_SLEEP(sizeof(*state));
429 state = (struct deflate_state *) ALLOC_NOSLEEP(sizeof(*state));
434 state->strm.next_out = NULL;
435 state->strm.zalloc = (alloc_func) z_alloc_init;
436 state->strm.zfree = (free_func) z_free;
437 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
438 FREE(state, sizeof(*state));
442 state->strm.zalloc = (alloc_func) z_alloc;
443 state->w_size = w_size;
444 bzero(&state->stats, sizeof(state->stats));
445 return (void *) state;
452 struct deflate_state *state = (struct deflate_state *) arg;
454 inflateEnd(&state->strm);
455 FREE(state, sizeof(*state));
459 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
462 int opt_len, unit, hdrlen, mru, debug;
464 struct deflate_state *state = (struct deflate_state *) arg;
466 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
467 || options[1] != CILEN_DEFLATE
468 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
469 || DEFLATE_SIZE(options[2]) != state->w_size
470 || options[3] != DEFLATE_CHK_SEQUENCE)
475 state->hdrlen = hdrlen;
476 state->debug = debug;
479 inflateReset(&state->strm);
488 struct deflate_state *state = (struct deflate_state *) arg;
491 inflateReset(&state->strm);
495 * Decompress a Deflate-compressed packet.
497 * Because of patent problems, we return DECOMP_ERROR for errors
498 * found by inspecting the input data and for system problems, but
499 * DECOMP_FATALERROR for any errors which could possibly be said to
500 * be being detected "after" decompression. For DECOMP_ERROR,
501 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
502 * infringing a patent of Motorola's if we do, so we take CCP down
505 * Given that the frame has the correct sequence number and a good FCS,
506 * errors such as invalid codes in the input most likely indicate a
507 * bug, so we return DECOMP_FATALERROR for them in order to turn off
508 * compression, even though they are detected by inspecting the input.
511 z_decompress(arg, mi, mop)
515 struct deflate_state *state = (struct deflate_state *) arg;
516 mblk_t *mo, *mo_head;
518 int rlen, olen, ospace;
519 int seq, i, flush, r, decode_proto;
520 u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
524 for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
525 while (rptr >= mi->b_wptr) {
534 /* Check the sequence number. */
535 seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
536 if (seq != state->seqno) {
540 printf("z_decompress%d: bad seq # %d, expected %d\n",
541 state->unit, seq, state->seqno);
546 /* Allocate an output message block. */
547 mo = allocb(DECOMP_CHUNK + state->hdrlen, BPRI_MED);
552 mo->b_rptr += state->hdrlen;
553 mo->b_wptr = wptr = mo->b_rptr;
554 ospace = DECOMP_CHUNK;
557 * Fill in the first part of the PPP header. The protocol field
558 * comes from the decompressed data.
560 wptr[0] = PPP_ADDRESS(hdr);
561 wptr[1] = PPP_CONTROL(hdr);
565 * Set up to call inflate. We set avail_out to 1 initially so we can
566 * look at the first byte of the output and decide whether we have
567 * a 1-byte or 2-byte protocol field.
569 state->strm.next_in = rptr;
570 state->strm.avail_in = mi->b_wptr - rptr;
572 flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
573 rlen = state->strm.avail_in + PPP_HDRLEN + DEFLATE_OVHD;
574 state->strm.next_out = wptr + 3;
575 state->strm.avail_out = 1;
580 * Call inflate, supplying more input or output as needed.
583 r = inflate(&state->strm, flush);
588 printf("z_decompress%d: inflate returned %d (%s)\n",
589 state->unit, r, (state->strm.msg? state->strm.msg: ""));
591 return DECOMP_FATALERROR;
593 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
594 break; /* all done */
595 if (state->strm.avail_in == 0 && mi != NULL) {
596 state->strm.next_in = mi->b_rptr;
597 state->strm.avail_in = mi->b_wptr - mi->b_rptr;
598 rlen += state->strm.avail_in;
601 flush = Z_PACKET_FLUSH;
603 if (state->strm.avail_out == 0) {
605 state->strm.avail_out = ospace - PPP_HDRLEN;
606 if ((wptr[3] & 1) == 0) {
607 /* 2-byte protocol field */
609 --state->strm.next_out;
610 ++state->strm.avail_out;
615 mo->b_wptr += ospace;
617 mo->b_cont = allocb(DECOMP_CHUNK, BPRI_MED);
623 state->strm.next_out = mo->b_rptr;
624 state->strm.avail_out = ospace = DECOMP_CHUNK;
632 mo->b_wptr += ospace - state->strm.avail_out;
633 olen += ospace - state->strm.avail_out;
636 if (olen > state->mru + PPP_HDRLEN)
637 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
638 state->unit, olen, state->mru + PPP_HDRLEN);
641 state->stats.unc_bytes += olen;
642 state->stats.unc_packets++;
643 state->stats.comp_bytes += rlen;
644 state->stats.comp_packets++;
651 * Incompressible data has arrived - add it to the history.
658 struct deflate_state *state = (struct deflate_state *) arg;
663 * Check that the protocol is one we handle.
666 if (rptr + PPP_HDRLEN > mi->b_wptr) {
667 if (!pullupmsg(mi, PPP_HDRLEN))
671 proto = PPP_PROTOCOL(rptr);
672 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
678 * Iterate through the message blocks, adding the characters in them
679 * to the decompressor's history. For the first block, we start
680 * at the either the 1st or 2nd byte of the protocol field,
681 * depending on whether the protocol value is compressible.
683 rlen = mi->b_wptr - mi->b_rptr;
684 state->strm.next_in = rptr + 3;
685 state->strm.avail_in = rlen - 3;
687 --state->strm.next_in;
688 ++state->strm.avail_in;
691 r = inflateIncomp(&state->strm);
697 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
698 state->unit, r, (state->strm.msg? state->strm.msg: ""));
704 state->strm.next_in = mi->b_rptr;
705 state->strm.avail_in = mi->b_wptr - mi->b_rptr;
706 rlen += state->strm.avail_in;
712 state->stats.inc_bytes += rlen;
713 state->stats.inc_packets++;
714 state->stats.unc_bytes += rlen;
715 state->stats.unc_packets++;
718 #endif /* DO_DEFLATE */