1 /* $NetBSD: ppp-deflate.c,v 1.6 1998/05/02 14:34:25 christos Exp $ */
2 /* Id: ppp-deflate.c,v 1.5 1997/03/04 03:33:28 paulus Exp */
5 * ppp_deflate.c - interface the zlib procedures for Deflate compression
6 * and decompression (as used by gzip) to the PPP code.
7 * This version is for use with mbufs on BSD-derived systems.
9 * Copyright (c) 1994 The Australian National University.
10 * All rights reserved.
12 * Permission to use, copy, modify, and distribute this software and its
13 * documentation is hereby granted, provided that the above copyright
14 * notice appears in all copies. This software is provided without any
15 * warranty, express or implied. The Australian National University
16 * makes no representations about the suitability of this software for
19 * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
20 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
21 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
22 * THE AUSTRALIAN NATIONAL UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY
25 * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
28 * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
29 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
37 #include <net/ppp_defs.h>
40 #define PACKETPTR struct mbuf *
41 #include <net/ppp-comp.h>
45 #define DEFLATE_DEBUG 1
48 * State for a Deflate (de)compressor.
50 struct deflate_state {
58 struct compstat stats;
61 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
63 static void *zalloc __P((void *, u_int items, u_int size));
64 static void zfree __P((void *, void *ptr));
65 static void *z_comp_alloc __P((u_char *options, int opt_len));
66 static void *z_decomp_alloc __P((u_char *options, int opt_len));
67 static void z_comp_free __P((void *state));
68 static void z_decomp_free __P((void *state));
69 static int z_comp_init __P((void *state, u_char *options, int opt_len,
70 int unit, int hdrlen, int debug));
71 static int z_decomp_init __P((void *state, u_char *options, int opt_len,
72 int unit, int hdrlen, int mru, int debug));
73 static int z_compress __P((void *state, struct mbuf **mret,
74 struct mbuf *mp, int slen, int maxolen));
75 static void z_incomp __P((void *state, struct mbuf *dmsg));
76 static int z_decompress __P((void *state, struct mbuf *cmp,
78 static void z_comp_reset __P((void *state));
79 static void z_decomp_reset __P((void *state));
80 static void z_comp_stats __P((void *state, struct compstat *stats));
83 * Procedures exported to if_ppp.c.
85 struct compressor ppp_deflate = {
86 CI_DEFLATE, /* compress_proto */
87 z_comp_alloc, /* comp_alloc */
88 z_comp_free, /* comp_free */
89 z_comp_init, /* comp_init */
90 z_comp_reset, /* comp_reset */
91 z_compress, /* compress */
92 z_comp_stats, /* comp_stat */
93 z_decomp_alloc, /* decomp_alloc */
94 z_decomp_free, /* decomp_free */
95 z_decomp_init, /* decomp_init */
96 z_decomp_reset, /* decomp_reset */
97 z_decompress, /* decompress */
98 z_incomp, /* incomp */
99 z_comp_stats, /* decomp_stat */
102 struct compressor ppp_deflate_draft = {
103 CI_DEFLATE_DRAFT, /* 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 * Space allocation and freeing routines for use by zlib routines.
122 zalloc(notused, items, size)
128 MALLOC(ptr, void *, items * size, M_DEVBUF, M_NOWAIT);
141 * Allocate space for a compressor.
144 z_comp_alloc(options, opt_len)
148 struct deflate_state *state;
151 if (opt_len != CILEN_DEFLATE
152 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
153 || options[1] != CILEN_DEFLATE
154 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
155 || options[3] != DEFLATE_CHK_SEQUENCE)
157 w_size = DEFLATE_SIZE(options[2]);
158 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
161 MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
166 state->strm.next_in = NULL;
167 state->strm.zalloc = zalloc;
168 state->strm.zfree = zfree;
169 if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
170 -w_size, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
171 FREE(state, M_DEVBUF);
175 state->w_size = w_size;
176 bzero(&state->stats, sizeof(state->stats));
177 return (void *) state;
184 struct deflate_state *state = (struct deflate_state *) arg;
186 deflateEnd(&state->strm);
187 FREE(state, M_DEVBUF);
191 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
194 int opt_len, unit, hdrlen, debug;
196 struct deflate_state *state = (struct deflate_state *) arg;
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 || DEFLATE_SIZE(options[2]) != state->w_size
203 || options[3] != DEFLATE_CHK_SEQUENCE)
208 state->hdrlen = hdrlen;
209 state->debug = debug;
211 deflateReset(&state->strm);
220 struct deflate_state *state = (struct deflate_state *) arg;
223 deflateReset(&state->strm);
227 z_compress(arg, mret, mp, orig_len, maxolen)
229 struct mbuf **mret; /* compressed packet (out) */
230 struct mbuf *mp; /* uncompressed packet (in) */
231 int orig_len, maxolen;
233 struct deflate_state *state = (struct deflate_state *) arg;
235 int proto, olen, wspace, r, flush;
239 * Check that the protocol is in the range we handle.
241 rptr = mtod(mp, u_char *);
242 proto = PPP_PROTOCOL(rptr);
243 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb) {
248 /* Allocate one mbuf initially. */
249 if (maxolen > orig_len)
251 MGET(m, M_DONTWAIT, MT_DATA);
255 if (maxolen + state->hdrlen > MLEN)
256 MCLGET(m, M_DONTWAIT);
257 wspace = M_TRAILINGSPACE(m);
258 if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
259 m->m_data += state->hdrlen;
260 wspace -= state->hdrlen;
262 wptr = mtod(m, u_char *);
265 * Copy over the PPP header and store the 2-byte sequence number.
267 wptr[0] = PPP_ADDRESS(rptr);
268 wptr[1] = PPP_CONTROL(rptr);
269 wptr[2] = PPP_COMP >> 8;
272 wptr[0] = state->seqno >> 8;
273 wptr[1] = state->seqno;
275 state->strm.next_out = wptr;
276 state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
278 state->strm.next_out = NULL;
279 state->strm.avail_out = 1000000;
285 rptr += (proto > 0xff)? 2: 3; /* skip 1st proto byte if 0 */
286 state->strm.next_in = rptr;
287 state->strm.avail_in = mtod(mp, u_char *) + mp->m_len - rptr;
289 flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
292 r = deflate(&state->strm, flush);
294 printf("z_compress: deflate returned %d (%s)\n",
295 r, (state->strm.msg? state->strm.msg: ""));
298 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
299 break; /* all done */
300 if (state->strm.avail_in == 0 && mp != NULL) {
301 state->strm.next_in = mtod(mp, u_char *);
302 state->strm.avail_in = mp->m_len;
305 flush = Z_PACKET_FLUSH;
307 if (state->strm.avail_out == 0) {
311 MGET(m->m_next, M_DONTWAIT, MT_DATA);
315 if (maxolen - olen > MLEN)
316 MCLGET(m, M_DONTWAIT);
317 state->strm.next_out = mtod(m, u_char *);
318 state->strm.avail_out = wspace = M_TRAILINGSPACE(m);
322 state->strm.next_out = NULL;
323 state->strm.avail_out = 1000000;
328 olen += (m->m_len = wspace - state->strm.avail_out);
331 * See if we managed to reduce the size of the packet.
333 if (m != NULL && olen < orig_len) {
334 state->stats.comp_bytes += olen;
335 state->stats.comp_packets++;
341 state->stats.inc_bytes += orig_len;
342 state->stats.inc_packets++;
345 state->stats.unc_bytes += orig_len;
346 state->stats.unc_packets++;
352 z_comp_stats(arg, stats)
354 struct compstat *stats;
356 struct deflate_state *state = (struct deflate_state *) arg;
359 *stats = state->stats;
360 stats->ratio = stats->unc_bytes;
361 out = stats->comp_bytes + stats->inc_bytes;
362 if (stats->ratio <= 0x7ffffff)
371 * Allocate space for a decompressor.
374 z_decomp_alloc(options, opt_len)
378 struct deflate_state *state;
381 if (opt_len != CILEN_DEFLATE
382 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
383 || options[1] != CILEN_DEFLATE
384 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
385 || options[3] != DEFLATE_CHK_SEQUENCE)
387 w_size = DEFLATE_SIZE(options[2]);
388 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
391 MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
396 state->strm.next_out = NULL;
397 state->strm.zalloc = zalloc;
398 state->strm.zfree = zfree;
399 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
400 FREE(state, M_DEVBUF);
404 state->w_size = w_size;
405 bzero(&state->stats, sizeof(state->stats));
406 return (void *) state;
413 struct deflate_state *state = (struct deflate_state *) arg;
415 inflateEnd(&state->strm);
416 FREE(state, M_DEVBUF);
420 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
423 int opt_len, unit, hdrlen, mru, debug;
425 struct deflate_state *state = (struct deflate_state *) arg;
427 if (opt_len < CILEN_DEFLATE
428 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
429 || options[1] != CILEN_DEFLATE
430 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
431 || DEFLATE_SIZE(options[2]) != state->w_size
432 || options[3] != DEFLATE_CHK_SEQUENCE)
437 state->hdrlen = hdrlen;
438 state->debug = debug;
441 inflateReset(&state->strm);
450 struct deflate_state *state = (struct deflate_state *) arg;
453 inflateReset(&state->strm);
457 * Decompress a Deflate-compressed packet.
459 * Because of patent problems, we return DECOMP_ERROR for errors
460 * found by inspecting the input data and for system problems, but
461 * DECOMP_FATALERROR for any errors which could possibly be said to
462 * be being detected "after" decompression. For DECOMP_ERROR,
463 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
464 * infringing a patent of Motorola's if we do, so we take CCP down
467 * Given that the frame has the correct sequence number and a good FCS,
468 * errors such as invalid codes in the input most likely indicate a
469 * bug, so we return DECOMP_FATALERROR for them in order to turn off
470 * compression, even though they are detected by inspecting the input.
473 z_decompress(arg, mi, mop)
475 struct mbuf *mi, **mop;
477 struct deflate_state *state = (struct deflate_state *) arg;
478 struct mbuf *mo, *mo_head;
480 int rlen, olen, ospace;
481 int seq, i, flush, r, decode_proto;
482 u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
485 rptr = mtod(mi, u_char *);
487 for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
492 rptr = mtod(mi, u_char *);
499 /* Check the sequence number. */
500 seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
501 if (seq != state->seqno) {
503 printf("z_decompress%d: bad seq # %d, expected %d\n",
504 state->unit, seq, state->seqno);
509 /* Allocate an output mbuf. */
510 MGETHDR(mo, M_DONTWAIT, MT_DATA);
516 MCLGET(mo, M_DONTWAIT);
517 ospace = M_TRAILINGSPACE(mo);
518 if (state->hdrlen + PPP_HDRLEN < ospace) {
519 mo->m_data += state->hdrlen;
520 ospace -= state->hdrlen;
524 * Fill in the first part of the PPP header. The protocol field
525 * comes from the decompressed data.
527 wptr = mtod(mo, u_char *);
528 wptr[0] = PPP_ADDRESS(hdr);
529 wptr[1] = PPP_CONTROL(hdr);
533 * Set up to call inflate. We set avail_out to 1 initially so we can
534 * look at the first byte of the output and decide whether we have
535 * a 1-byte or 2-byte protocol field.
537 state->strm.next_in = rptr;
538 state->strm.avail_in = rlen;
540 flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
541 rlen += PPP_HDRLEN + DEFLATE_OVHD;
542 state->strm.next_out = wptr + 3;
543 state->strm.avail_out = 1;
548 * Call inflate, supplying more input or output as needed.
551 r = inflate(&state->strm, flush);
556 printf("z_decompress%d: inflate returned %d (%s)\n",
557 state->unit, r, (state->strm.msg? state->strm.msg: ""));
559 return DECOMP_FATALERROR;
561 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
562 break; /* all done */
563 if (state->strm.avail_in == 0 && mi != NULL) {
564 state->strm.next_in = mtod(mi, u_char *);
565 state->strm.avail_in = mi->m_len;
569 flush = Z_PACKET_FLUSH;
571 if (state->strm.avail_out == 0) {
573 state->strm.avail_out = ospace - PPP_HDRLEN;
574 if ((wptr[3] & 1) == 0) {
575 /* 2-byte protocol field */
577 --state->strm.next_out;
578 ++state->strm.avail_out;
585 MGET(mo->m_next, M_DONTWAIT, MT_DATA);
591 MCLGET(mo, M_DONTWAIT);
592 state->strm.next_out = mtod(mo, u_char *);
593 state->strm.avail_out = ospace = M_TRAILINGSPACE(mo);
601 olen += (mo->m_len = ospace - state->strm.avail_out);
603 if (olen > state->mru + PPP_HDRLEN)
604 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
605 state->unit, olen, state->mru + PPP_HDRLEN);
608 state->stats.unc_bytes += olen;
609 state->stats.unc_packets++;
610 state->stats.comp_bytes += rlen;
611 state->stats.comp_packets++;
618 * Incompressible data has arrived - add it to the history.
625 struct deflate_state *state = (struct deflate_state *) arg;
630 * Check that the protocol is one we handle.
632 rptr = mtod(mi, u_char *);
633 proto = PPP_PROTOCOL(rptr);
634 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
640 * Iterate through the mbufs, adding the characters in them
641 * to the decompressor's history. For the first mbuf, we start
642 * at the either the 1st or 2nd byte of the protocol field,
643 * depending on whether the protocol value is compressible.
646 state->strm.next_in = rptr + 3;
647 state->strm.avail_in = rlen - 3;
649 --state->strm.next_in;
650 ++state->strm.avail_in;
653 r = inflateIncomp(&state->strm);
659 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
660 state->unit, r, (state->strm.msg? state->strm.msg: ""));
666 state->strm.next_in = mtod(mi, u_char *);
667 state->strm.avail_in = mi->m_len;
674 state->stats.inc_bytes += rlen;
675 state->stats.inc_packets++;
676 state->stats.unc_bytes += rlen;
677 state->stats.unc_packets++;
680 #endif /* DO_DEFLATE */