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.3 1996/06/26 00:53:16 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"
54 * State for a Deflate (de)compressor.
56 struct deflate_state {
64 struct compstat stats;
67 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
69 static void *z_alloc __P((void *, u_int items, u_int size));
70 static void z_free __P((void *, void *ptr, u_int nb));
71 static void *z_comp_alloc __P((u_char *options, int opt_len));
72 static void *z_decomp_alloc __P((u_char *options, int opt_len));
73 static void z_comp_free __P((void *state));
74 static void z_decomp_free __P((void *state));
75 static int z_comp_init __P((void *state, u_char *options, int opt_len,
76 int unit, int hdrlen, int debug));
77 static int z_decomp_init __P((void *state, u_char *options, int opt_len,
78 int unit, int hdrlen, int mru, int debug));
79 static int z_compress __P((void *state, mblk_t **mret,
80 mblk_t *mp, int slen, int maxolen));
81 static void z_incomp __P((void *state, mblk_t *dmsg));
82 static int z_decompress __P((void *state, mblk_t *cmp,
84 static void z_comp_reset __P((void *state));
85 static void z_decomp_reset __P((void *state));
86 static void z_comp_stats __P((void *state, struct compstat *stats));
89 * Procedures exported to if_ppp.c.
91 struct compressor ppp_deflate = {
92 CI_DEFLATE, /* compress_proto */
93 z_comp_alloc, /* comp_alloc */
94 z_comp_free, /* comp_free */
95 z_comp_init, /* comp_init */
96 z_comp_reset, /* comp_reset */
97 z_compress, /* compress */
98 z_comp_stats, /* comp_stat */
99 z_decomp_alloc, /* decomp_alloc */
100 z_decomp_free, /* decomp_free */
101 z_decomp_init, /* decomp_init */
102 z_decomp_reset, /* decomp_reset */
103 z_decompress, /* decompress */
104 z_incomp, /* incomp */
105 z_comp_stats, /* decomp_stat */
108 #define DECOMP_CHUNK 512
111 * Space allocation and freeing routines for use by zlib routines.
114 z_alloc(notused, items, size)
119 return ALLOC_SLEEP(items * size);
121 return ALLOC_NOSLEEP(items * size);
126 z_free(notused, ptr, nbytes)
135 * Allocate space for a compressor.
138 z_comp_alloc(options, opt_len)
142 struct deflate_state *state;
145 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
146 || options[1] != CILEN_DEFLATE
147 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
148 || options[3] != DEFLATE_CHK_SEQUENCE)
150 w_size = DEFLATE_SIZE(options[2]);
151 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
154 state = (struct deflate_state *) ALLOC_NOSLEEP(sizeof(*state));
158 state->strm.next_in = NULL;
159 state->strm.zalloc = (alloc_func) z_alloc;
160 state->strm.zfree = (free_func) z_free;
161 if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
162 -w_size, 8, Z_DEFAULT_STRATEGY, DEFLATE_OVHD+2) != Z_OK) {
163 FREE(state, sizeof(*state));
167 state->w_size = w_size;
168 bzero(&state->stats, sizeof(state->stats));
169 return (void *) state;
176 struct deflate_state *state = (struct deflate_state *) arg;
178 deflateEnd(&state->strm);
179 FREE(state, sizeof(*state));
183 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
186 int opt_len, unit, hdrlen, debug;
188 struct deflate_state *state = (struct deflate_state *) arg;
190 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
191 || options[1] != CILEN_DEFLATE
192 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
193 || DEFLATE_SIZE(options[2]) != state->w_size
194 || options[3] != DEFLATE_CHK_SEQUENCE)
199 state->hdrlen = hdrlen;
200 state->debug = debug;
202 deflateReset(&state->strm);
211 struct deflate_state *state = (struct deflate_state *) arg;
214 deflateReset(&state->strm);
218 z_compress(arg, mret, mp, orig_len, maxolen)
220 mblk_t **mret; /* compressed packet (out) */
221 mblk_t *mp; /* uncompressed packet (in) */
222 int orig_len, maxolen;
224 struct deflate_state *state = (struct deflate_state *) arg;
226 int proto, olen, wspace, r, flush;
230 * Check that the protocol is in the range we handle.
234 if (rptr + PPP_HDRLEN > mp->b_wptr) {
235 if (!pullupmsg(mp, PPP_HDRLEN))
239 proto = PPP_PROTOCOL(rptr);
240 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
243 /* Allocate one mblk initially. */
244 if (maxolen > orig_len)
246 wspace = maxolen < 4096? maxolen: 4096;
247 m = allocb(wspace, BPRI_MED);
250 if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
251 m->b_rptr += state->hdrlen;
252 m->b_wptr = m->b_rptr;
253 wspace -= state->hdrlen;
258 * Copy over the PPP header and store the 2-byte sequence number.
260 wptr[0] = PPP_ADDRESS(rptr);
261 wptr[1] = PPP_CONTROL(rptr);
262 wptr[2] = PPP_COMP >> 8;
265 wptr[0] = state->seqno >> 8;
266 wptr[1] = state->seqno;
268 state->strm.next_out = wptr;
269 state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
271 state->strm.next_out = NULL;
272 state->strm.avail_out = 1000000;
276 rptr += (proto > 0xff)? 2: 3; /* skip 1st proto byte if 0 */
277 state->strm.next_in = rptr;
278 state->strm.avail_in = mp->b_wptr - rptr;
280 flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
283 r = deflate(&state->strm, flush);
285 printf("z_compress: deflate returned %d (%s)\n",
286 r, (state->strm.msg? state->strm.msg: ""));
289 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
290 break; /* all done */
291 if (state->strm.avail_in == 0 && mp != NULL) {
292 state->strm.next_in = mp->b_rptr;
293 state->strm.avail_in = mp->b_wptr - mp->b_rptr;
296 flush = Z_PACKET_FLUSH;
298 if (state->strm.avail_out == 0) {
302 wspace = maxolen - olen;
305 else if (wspace > 4096)
307 m->b_cont = allocb(wspace, BPRI_MED);
310 state->strm.next_out = m->b_wptr;
311 state->strm.avail_out = wspace;
315 state->strm.next_out = NULL;
316 state->strm.avail_out = 1000000;
320 m->b_wptr += wspace - state->strm.avail_out;
321 olen += wspace - state->strm.avail_out;
324 * See if we managed to reduce the size of the packet.
325 * If the compressor just gave us a single zero byte, it means
326 * the packet was incompressible.
328 if (olen < orig_len && m != NULL
329 && !(olen == PPP_HDRLEN + 3 && *wptr == 0)) {
330 state->stats.comp_bytes += olen;
331 state->stats.comp_packets++;
337 state->stats.inc_bytes += orig_len;
338 state->stats.inc_packets++;
341 state->stats.unc_bytes += orig_len;
342 state->stats.unc_packets++;
348 z_comp_stats(arg, stats)
350 struct compstat *stats;
352 struct deflate_state *state = (struct deflate_state *) arg;
355 *stats = state->stats;
356 stats->ratio = stats->unc_bytes;
357 out = stats->comp_bytes + stats->unc_bytes;
358 if (stats->ratio <= 0x7ffffff)
367 * Allocate space for a decompressor.
370 z_decomp_alloc(options, opt_len)
374 struct deflate_state *state;
377 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
378 || options[1] != CILEN_DEFLATE
379 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
380 || options[3] != DEFLATE_CHK_SEQUENCE)
382 w_size = DEFLATE_SIZE(options[2]);
383 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
386 state = (struct deflate_state *) ALLOC_NOSLEEP(sizeof(*state));
390 state->strm.next_out = NULL;
391 state->strm.zalloc = (alloc_func) z_alloc;
392 state->strm.zfree = (free_func) z_free;
393 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
394 FREE(state, sizeof(*state));
398 state->w_size = w_size;
399 bzero(&state->stats, sizeof(state->stats));
400 return (void *) state;
407 struct deflate_state *state = (struct deflate_state *) arg;
409 inflateEnd(&state->strm);
410 FREE(state, sizeof(*state));
414 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
417 int opt_len, unit, hdrlen, mru, debug;
419 struct deflate_state *state = (struct deflate_state *) arg;
421 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
422 || options[1] != CILEN_DEFLATE
423 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
424 || DEFLATE_SIZE(options[2]) != state->w_size
425 || options[3] != DEFLATE_CHK_SEQUENCE)
430 state->hdrlen = hdrlen;
431 state->debug = debug;
434 inflateReset(&state->strm);
443 struct deflate_state *state = (struct deflate_state *) arg;
446 inflateReset(&state->strm);
450 * Decompress a Deflate-compressed packet.
452 * Because of patent problems, we return DECOMP_ERROR for errors
453 * found by inspecting the input data and for system problems, but
454 * DECOMP_FATALERROR for any errors which could possibly be said to
455 * be being detected "after" decompression. For DECOMP_ERROR,
456 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
457 * infringing a patent of Motorola's if we do, so we take CCP down
460 * Given that the frame has the correct sequence number and a good FCS,
461 * errors such as invalid codes in the input most likely indicate a
462 * bug, so we return DECOMP_FATALERROR for them in order to turn off
463 * compression, even though they are detected by inspecting the input.
466 z_decompress(arg, mi, mop)
470 struct deflate_state *state = (struct deflate_state *) arg;
471 mblk_t *mo, *mo_head;
473 int rlen, olen, ospace;
474 int seq, i, flush, r, decode_proto;
475 u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
479 for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
480 while (rptr >= mi->b_wptr) {
489 /* Check the sequence number. */
490 seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
491 if (seq != state->seqno) {
493 printf("z_decompress%d: bad seq # %d, expected %d\n",
494 state->unit, seq, state->seqno);
499 /* Allocate an output message block. */
500 mo = allocb(DECOMP_CHUNK + state->hdrlen, BPRI_MED);
505 mo->b_rptr += state->hdrlen;
506 mo->b_wptr = wptr = mo->b_rptr;
507 ospace = DECOMP_CHUNK;
510 * Fill in the first part of the PPP header. The protocol field
511 * comes from the decompressed data.
513 wptr[0] = PPP_ADDRESS(hdr);
514 wptr[1] = PPP_CONTROL(hdr);
518 * Set up to call inflate. We set avail_out to 1 initially so we can
519 * look at the first byte of the output and decide whether we have
520 * a 1-byte or 2-byte protocol field.
522 state->strm.next_in = rptr;
523 state->strm.avail_in = mi->b_wptr - rptr;
525 flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
526 rlen = state->strm.avail_in + PPP_HDRLEN + DEFLATE_OVHD;
527 state->strm.next_out = wptr + 3;
528 state->strm.avail_out = 1;
533 * Call inflate, supplying more input or output as needed.
536 r = inflate(&state->strm, flush);
539 printf("z_decompress%d: inflate returned %d (%s)\n",
540 state->unit, r, (state->strm.msg? state->strm.msg: ""));
542 return DECOMP_FATALERROR;
544 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
545 break; /* all done */
546 if (state->strm.avail_in == 0 && mi != NULL) {
547 state->strm.next_in = mi->b_rptr;
548 state->strm.avail_in = mi->b_wptr - mi->b_rptr;
549 rlen += state->strm.avail_in;
552 flush = Z_PACKET_FLUSH;
554 if (state->strm.avail_out == 0) {
556 state->strm.avail_out = ospace - PPP_HDRLEN;
557 if ((wptr[3] & 1) == 0) {
558 /* 2-byte protocol field */
560 --state->strm.next_out;
561 ++state->strm.avail_out;
566 mo->b_wptr += ospace;
568 mo->b_cont = allocb(DECOMP_CHUNK, BPRI_MED);
574 state->strm.next_out = mo->b_rptr;
575 state->strm.avail_out = ospace = DECOMP_CHUNK;
583 mo->b_wptr += ospace - state->strm.avail_out;
584 olen += ospace - state->strm.avail_out;
586 state->stats.unc_bytes += olen;
587 state->stats.unc_packets++;
588 state->stats.comp_bytes += rlen;
589 state->stats.comp_packets++;
596 * Incompressible data has arrived - add it to the history.
603 struct deflate_state *state = (struct deflate_state *) arg;
608 * Check that the protocol is one we handle.
611 if (rptr + PPP_HDRLEN > mi->b_wptr) {
612 if (!pullupmsg(mi, PPP_HDRLEN))
616 proto = PPP_PROTOCOL(rptr);
617 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
623 * Iterate through the message blocks, adding the characters in them
624 * to the decompressor's history. For the first block, we start
625 * at the either the 1st or 2nd byte of the protocol field,
626 * depending on whether the protocol value is compressible.
628 rlen = mi->b_wptr - mi->b_rptr;
629 state->strm.next_in = rptr + 3;
630 state->strm.avail_in = rlen - 3;
632 --state->strm.next_in;
633 ++state->strm.avail_in;
636 r = inflateIncomp(&state->strm);
640 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
641 state->unit, r, (state->strm.msg? state->strm.msg: ""));
648 state->strm.next_in = mi->b_rptr;
649 state->strm.avail_in = mi->b_wptr - mi->b_rptr;
650 rlen += state->strm.avail_in;
656 state->stats.inc_bytes += rlen;
657 state->stats.inc_packets++;
658 state->stats.unc_bytes += rlen;
659 state->stats.unc_packets++;
662 #endif /* DO_DEFLATE */