1 /* $Id: ppp-deflate.c,v 1.6 1997/04/30 05:47:23 paulus Exp $ */
4 * ppp_deflate.c - interface the zlib procedures for Deflate compression
5 * and decompression (as used by gzip) to the PPP code.
6 * This version is for use with mbufs on BSD-derived systems.
8 * Copyright (c) 1994 The Australian National University.
11 * Permission to use, copy, modify, and distribute this software and its
12 * documentation is hereby granted, provided that the above copyright
13 * notice appears in all copies. This software is provided without any
14 * warranty, express or implied. The Australian National University
15 * makes no representations about the suitability of this software for
18 * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
19 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
20 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
21 * THE AUSTRALIAN NATIONAL UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY
24 * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
26 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
27 * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
28 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
36 #include <net/ppp_defs.h>
39 #define PACKETPTR struct mbuf *
40 #include <net/ppp-comp.h>
44 #define DEFLATE_DEBUG 1
47 * State for a Deflate (de)compressor.
49 struct deflate_state {
57 struct compstat stats;
60 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
62 static void *zalloc __P((void *, u_int items, u_int size));
63 static void zfree __P((void *, void *ptr, u_int nb));
64 static void *z_comp_alloc __P((u_char *options, int opt_len));
65 static void *z_decomp_alloc __P((u_char *options, int opt_len));
66 static void z_comp_free __P((void *state));
67 static void z_decomp_free __P((void *state));
68 static int z_comp_init __P((void *state, u_char *options, int opt_len,
69 int unit, int hdrlen, int debug));
70 static int z_decomp_init __P((void *state, u_char *options, int opt_len,
71 int unit, int hdrlen, int mru, int debug));
72 static int z_compress __P((void *state, struct mbuf **mret,
73 struct mbuf *mp, int slen, int maxolen));
74 static void z_incomp __P((void *state, struct mbuf *dmsg));
75 static int z_decompress __P((void *state, struct mbuf *cmp,
77 static void z_comp_reset __P((void *state));
78 static void z_decomp_reset __P((void *state));
79 static void z_comp_stats __P((void *state, struct compstat *stats));
82 * Procedures exported to if_ppp.c.
84 struct compressor ppp_deflate = {
85 CI_DEFLATE, /* compress_proto */
86 z_comp_alloc, /* comp_alloc */
87 z_comp_free, /* comp_free */
88 z_comp_init, /* comp_init */
89 z_comp_reset, /* comp_reset */
90 z_compress, /* compress */
91 z_comp_stats, /* comp_stat */
92 z_decomp_alloc, /* decomp_alloc */
93 z_decomp_free, /* decomp_free */
94 z_decomp_init, /* decomp_init */
95 z_decomp_reset, /* decomp_reset */
96 z_decompress, /* decompress */
97 z_incomp, /* incomp */
98 z_comp_stats, /* decomp_stat */
102 * Space allocation and freeing routines for use by zlib routines.
105 zalloc(notused, items, size)
111 MALLOC(ptr, void *, items * size, M_DEVBUF, M_NOWAIT);
116 zfree(notused, ptr, nbytes)
125 * Allocate space for a compressor.
128 z_comp_alloc(options, opt_len)
132 struct deflate_state *state;
135 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
136 || options[1] != CILEN_DEFLATE
137 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
138 || options[3] != DEFLATE_CHK_SEQUENCE)
140 w_size = DEFLATE_SIZE(options[2]);
141 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
144 MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
149 state->strm.next_in = NULL;
150 state->strm.zalloc = zalloc;
151 state->strm.zalloc_init = zalloc;
152 state->strm.zfree = zfree;
153 if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
154 -w_size, 8, Z_DEFAULT_STRATEGY, DEFLATE_OVHD+2) != Z_OK) {
155 FREE(state, M_DEVBUF);
159 state->w_size = w_size;
160 bzero(&state->stats, sizeof(state->stats));
161 return (void *) state;
168 struct deflate_state *state = (struct deflate_state *) arg;
170 deflateEnd(&state->strm);
171 FREE(state, M_DEVBUF);
175 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
178 int opt_len, unit, hdrlen, debug;
180 struct deflate_state *state = (struct deflate_state *) arg;
182 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
183 || options[1] != CILEN_DEFLATE
184 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
185 || DEFLATE_SIZE(options[2]) != state->w_size
186 || options[3] != DEFLATE_CHK_SEQUENCE)
191 state->hdrlen = hdrlen;
192 state->debug = debug;
194 deflateReset(&state->strm);
203 struct deflate_state *state = (struct deflate_state *) arg;
206 deflateReset(&state->strm);
210 z_compress(arg, mret, mp, orig_len, maxolen)
212 struct mbuf **mret; /* compressed packet (out) */
213 struct mbuf *mp; /* uncompressed packet (in) */
214 int orig_len, maxolen;
216 struct deflate_state *state = (struct deflate_state *) arg;
218 int proto, olen, wspace, r, flush;
222 * Check that the protocol is in the range we handle.
224 rptr = mtod(mp, u_char *);
225 proto = PPP_PROTOCOL(rptr);
226 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb) {
231 /* Allocate one mbuf initially. */
232 if (maxolen > orig_len)
234 MGET(m, M_DONTWAIT, MT_DATA);
238 if (maxolen + state->hdrlen > MLEN)
239 MCLGET(m, M_DONTWAIT);
240 wspace = M_TRAILINGSPACE(m);
241 if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
242 m->m_data += state->hdrlen;
243 wspace -= state->hdrlen;
245 wptr = mtod(m, u_char *);
248 * Copy over the PPP header and store the 2-byte sequence number.
250 wptr[0] = PPP_ADDRESS(rptr);
251 wptr[1] = PPP_CONTROL(rptr);
252 wptr[2] = PPP_COMP >> 8;
255 wptr[0] = state->seqno >> 8;
256 wptr[1] = state->seqno;
258 state->strm.next_out = wptr;
259 state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
261 state->strm.next_out = NULL;
262 state->strm.avail_out = 1000000;
268 rptr += (proto > 0xff)? 2: 3; /* skip 1st proto byte if 0 */
269 state->strm.next_in = rptr;
270 state->strm.avail_in = mtod(mp, u_char *) + mp->m_len - rptr;
272 flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
275 r = deflate(&state->strm, flush);
277 printf("z_compress: deflate returned %d (%s)\n",
278 r, (state->strm.msg? state->strm.msg: ""));
281 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
282 break; /* all done */
283 if (state->strm.avail_in == 0 && mp != NULL) {
284 state->strm.next_in = mtod(mp, u_char *);
285 state->strm.avail_in = mp->m_len;
288 flush = Z_PACKET_FLUSH;
290 if (state->strm.avail_out == 0) {
294 MGET(m->m_next, M_DONTWAIT, MT_DATA);
298 if (maxolen - olen > MLEN)
299 MCLGET(m, M_DONTWAIT);
300 state->strm.next_out = mtod(m, u_char *);
301 state->strm.avail_out = wspace = M_TRAILINGSPACE(m);
305 state->strm.next_out = NULL;
306 state->strm.avail_out = 1000000;
311 olen += (m->m_len = wspace - state->strm.avail_out);
314 * See if we managed to reduce the size of the packet.
315 * If the compressor just gave us a single zero byte, it means
316 * the packet was incompressible.
318 if (m != NULL && olen < orig_len
319 && !(olen == PPP_HDRLEN + 3 && *wptr == 0)) {
320 state->stats.comp_bytes += olen;
321 state->stats.comp_packets++;
327 state->stats.inc_bytes += orig_len;
328 state->stats.inc_packets++;
331 state->stats.unc_bytes += orig_len;
332 state->stats.unc_packets++;
338 z_comp_stats(arg, stats)
340 struct compstat *stats;
342 struct deflate_state *state = (struct deflate_state *) arg;
345 *stats = state->stats;
346 stats->ratio = stats->unc_bytes;
347 out = stats->comp_bytes + stats->inc_bytes;
348 if (stats->ratio <= 0x7ffffff)
357 * Allocate space for a decompressor.
360 z_decomp_alloc(options, opt_len)
364 struct deflate_state *state;
367 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
368 || options[1] != CILEN_DEFLATE
369 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
370 || options[3] != DEFLATE_CHK_SEQUENCE)
372 w_size = DEFLATE_SIZE(options[2]);
373 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
376 MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
381 state->strm.next_out = NULL;
382 state->strm.zalloc = zalloc;
383 state->strm.zalloc_init = zalloc;
384 state->strm.zfree = zfree;
385 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
386 FREE(state, M_DEVBUF);
390 state->w_size = w_size;
391 bzero(&state->stats, sizeof(state->stats));
392 return (void *) state;
399 struct deflate_state *state = (struct deflate_state *) arg;
401 inflateEnd(&state->strm);
402 FREE(state, M_DEVBUF);
406 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
409 int opt_len, unit, hdrlen, mru, debug;
411 struct deflate_state *state = (struct deflate_state *) arg;
413 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
414 || options[1] != CILEN_DEFLATE
415 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
416 || DEFLATE_SIZE(options[2]) != state->w_size
417 || options[3] != DEFLATE_CHK_SEQUENCE)
422 state->hdrlen = hdrlen;
423 state->debug = debug;
426 inflateReset(&state->strm);
435 struct deflate_state *state = (struct deflate_state *) arg;
438 inflateReset(&state->strm);
442 * Decompress a Deflate-compressed packet.
444 * Because of patent problems, we return DECOMP_ERROR for errors
445 * found by inspecting the input data and for system problems, but
446 * DECOMP_FATALERROR for any errors which could possibly be said to
447 * be being detected "after" decompression. For DECOMP_ERROR,
448 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
449 * infringing a patent of Motorola's if we do, so we take CCP down
452 * Given that the frame has the correct sequence number and a good FCS,
453 * errors such as invalid codes in the input most likely indicate a
454 * bug, so we return DECOMP_FATALERROR for them in order to turn off
455 * compression, even though they are detected by inspecting the input.
458 z_decompress(arg, mi, mop)
460 struct mbuf *mi, **mop;
462 struct deflate_state *state = (struct deflate_state *) arg;
463 struct mbuf *mo, *mo_head;
465 int rlen, olen, ospace;
466 int seq, i, flush, r, decode_proto;
467 u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
470 rptr = mtod(mi, u_char *);
472 for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
477 rptr = mtod(mi, u_char *);
484 /* Check the sequence number. */
485 seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
486 if (seq != state->seqno) {
488 printf("z_decompress%d: bad seq # %d, expected %d\n",
489 state->unit, seq, state->seqno);
494 /* Allocate an output mbuf. */
495 MGETHDR(mo, M_DONTWAIT, MT_DATA);
501 MCLGET(mo, M_DONTWAIT);
502 ospace = M_TRAILINGSPACE(mo);
503 if (state->hdrlen + PPP_HDRLEN < ospace) {
504 mo->m_data += state->hdrlen;
505 ospace -= state->hdrlen;
509 * Fill in the first part of the PPP header. The protocol field
510 * comes from the decompressed data.
512 wptr = mtod(mo, u_char *);
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 = rlen;
525 flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
526 rlen += 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);
541 printf("z_decompress%d: inflate returned %d (%s)\n",
542 state->unit, r, (state->strm.msg? state->strm.msg: ""));
544 return DECOMP_FATALERROR;
546 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
547 break; /* all done */
548 if (state->strm.avail_in == 0 && mi != NULL) {
549 state->strm.next_in = mtod(mi, u_char *);
550 state->strm.avail_in = mi->m_len;
554 flush = Z_PACKET_FLUSH;
556 if (state->strm.avail_out == 0) {
558 state->strm.avail_out = ospace - PPP_HDRLEN;
559 if ((wptr[3] & 1) == 0) {
560 /* 2-byte protocol field */
562 --state->strm.next_out;
563 ++state->strm.avail_out;
570 MGET(mo->m_next, M_DONTWAIT, MT_DATA);
576 MCLGET(mo, M_DONTWAIT);
577 state->strm.next_out = mtod(mo, u_char *);
578 state->strm.avail_out = ospace = M_TRAILINGSPACE(mo);
586 olen += (mo->m_len = ospace - state->strm.avail_out);
588 if (olen > state->mru + PPP_HDRLEN)
589 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
590 state->unit, olen, state->mru + PPP_HDRLEN);
593 state->stats.unc_bytes += olen;
594 state->stats.unc_packets++;
595 state->stats.comp_bytes += rlen;
596 state->stats.comp_packets++;
603 * Incompressible data has arrived - add it to the history.
610 struct deflate_state *state = (struct deflate_state *) arg;
615 * Check that the protocol is one we handle.
617 rptr = mtod(mi, u_char *);
618 proto = PPP_PROTOCOL(rptr);
619 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
625 * Iterate through the mbufs, adding the characters in them
626 * to the decompressor's history. For the first mbuf, we start
627 * at the either the 1st or 2nd byte of the protocol field,
628 * depending on whether the protocol value is compressible.
631 state->strm.next_in = rptr + 3;
632 state->strm.avail_in = rlen - 3;
634 --state->strm.next_in;
635 ++state->strm.avail_in;
638 r = inflateIncomp(&state->strm);
644 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
645 state->unit, r, (state->strm.msg? state->strm.msg: ""));
651 state->strm.next_in = mtod(mi, u_char *);
652 state->strm.avail_in = mi->m_len;
659 state->stats.inc_bytes += rlen;
660 state->stats.inc_packets++;
661 state->stats.unc_bytes += rlen;
662 state->stats.unc_packets++;
665 #endif /* DO_DEFLATE */