1 /* $Id: ppp-deflate.c,v 1.4 1996/10/08 04:38:06 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>
45 #define inflate inflate_ppp /* FreeBSD already has an inflate :-( */
48 #define DEFLATE_DEBUG 1
51 * State for a Deflate (de)compressor.
53 struct deflate_state {
61 struct compstat stats;
64 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
66 static void *zalloc __P((void *, u_int items, u_int size));
67 static void zfree __P((void *, void *ptr, u_int nb));
68 static void *z_comp_alloc __P((u_char *options, int opt_len));
69 static void *z_decomp_alloc __P((u_char *options, int opt_len));
70 static void z_comp_free __P((void *state));
71 static void z_decomp_free __P((void *state));
72 static int z_comp_init __P((void *state, u_char *options, int opt_len,
73 int unit, int hdrlen, int debug));
74 static int z_decomp_init __P((void *state, u_char *options, int opt_len,
75 int unit, int hdrlen, int mru, int debug));
76 static int z_compress __P((void *state, struct mbuf **mret,
77 struct mbuf *mp, int slen, int maxolen));
78 static void z_incomp __P((void *state, struct mbuf *dmsg));
79 static int z_decompress __P((void *state, struct mbuf *cmp,
81 static void z_comp_reset __P((void *state));
82 static void z_decomp_reset __P((void *state));
83 static void z_comp_stats __P((void *state, struct compstat *stats));
86 * Procedures exported to if_ppp.c.
88 struct compressor ppp_deflate = {
89 CI_DEFLATE, /* compress_proto */
90 z_comp_alloc, /* comp_alloc */
91 z_comp_free, /* comp_free */
92 z_comp_init, /* comp_init */
93 z_comp_reset, /* comp_reset */
94 z_compress, /* compress */
95 z_comp_stats, /* comp_stat */
96 z_decomp_alloc, /* decomp_alloc */
97 z_decomp_free, /* decomp_free */
98 z_decomp_init, /* decomp_init */
99 z_decomp_reset, /* decomp_reset */
100 z_decompress, /* decompress */
101 z_incomp, /* incomp */
102 z_comp_stats, /* decomp_stat */
106 * Space allocation and freeing routines for use by zlib routines.
109 zalloc(notused, items, size)
115 MALLOC(ptr, void *, items * size, M_DEVBUF, M_NOWAIT);
120 zfree(notused, ptr, nbytes)
129 * Allocate space for a compressor.
132 z_comp_alloc(options, opt_len)
136 struct deflate_state *state;
139 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
140 || options[1] != CILEN_DEFLATE
141 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
142 || options[3] != DEFLATE_CHK_SEQUENCE)
144 w_size = DEFLATE_SIZE(options[2]);
145 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
148 MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
153 state->strm.next_in = NULL;
154 state->strm.zalloc = zalloc;
155 state->strm.zfree = zfree;
156 if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
157 -w_size, 8, Z_DEFAULT_STRATEGY, DEFLATE_OVHD+2) != Z_OK) {
158 FREE(state, M_DEVBUF);
162 state->w_size = w_size;
163 bzero(&state->stats, sizeof(state->stats));
164 return (void *) state;
171 struct deflate_state *state = (struct deflate_state *) arg;
173 deflateEnd(&state->strm);
174 FREE(state, M_DEVBUF);
178 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
181 int opt_len, unit, hdrlen, debug;
183 struct deflate_state *state = (struct deflate_state *) arg;
185 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
186 || options[1] != CILEN_DEFLATE
187 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
188 || DEFLATE_SIZE(options[2]) != state->w_size
189 || options[3] != DEFLATE_CHK_SEQUENCE)
194 state->hdrlen = hdrlen;
195 state->debug = debug;
197 deflateReset(&state->strm);
206 struct deflate_state *state = (struct deflate_state *) arg;
209 deflateReset(&state->strm);
213 z_compress(arg, mret, mp, orig_len, maxolen)
215 struct mbuf **mret; /* compressed packet (out) */
216 struct mbuf *mp; /* uncompressed packet (in) */
217 int orig_len, maxolen;
219 struct deflate_state *state = (struct deflate_state *) arg;
221 int proto, olen, wspace, r, flush;
225 * Check that the protocol is in the range we handle.
227 rptr = mtod(mp, u_char *);
228 proto = PPP_PROTOCOL(rptr);
229 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb) {
234 /* Allocate one mbuf initially. */
235 if (maxolen > orig_len)
237 MGET(m, M_DONTWAIT, MT_DATA);
241 if (maxolen + state->hdrlen > MLEN)
242 MCLGET(m, M_DONTWAIT);
243 wspace = M_TRAILINGSPACE(m);
244 if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
245 m->m_data += state->hdrlen;
246 wspace -= state->hdrlen;
248 wptr = mtod(m, u_char *);
251 * Copy over the PPP header and store the 2-byte sequence number.
253 wptr[0] = PPP_ADDRESS(rptr);
254 wptr[1] = PPP_CONTROL(rptr);
255 wptr[2] = PPP_COMP >> 8;
258 wptr[0] = state->seqno >> 8;
259 wptr[1] = state->seqno;
261 state->strm.next_out = wptr;
262 state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
264 state->strm.next_out = NULL;
265 state->strm.avail_out = 1000000;
271 rptr += (proto > 0xff)? 2: 3; /* skip 1st proto byte if 0 */
272 state->strm.next_in = rptr;
273 state->strm.avail_in = mtod(mp, u_char *) + mp->m_len - rptr;
275 flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
278 r = deflate(&state->strm, flush);
280 printf("z_compress: deflate returned %d (%s)\n",
281 r, (state->strm.msg? state->strm.msg: ""));
284 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
285 break; /* all done */
286 if (state->strm.avail_in == 0 && mp != NULL) {
287 state->strm.next_in = mtod(mp, u_char *);
288 state->strm.avail_in = mp->m_len;
291 flush = Z_PACKET_FLUSH;
293 if (state->strm.avail_out == 0) {
297 MGET(m->m_next, M_DONTWAIT, MT_DATA);
301 if (maxolen - olen > MLEN)
302 MCLGET(m, M_DONTWAIT);
303 state->strm.next_out = mtod(m, u_char *);
304 state->strm.avail_out = wspace = M_TRAILINGSPACE(m);
308 state->strm.next_out = NULL;
309 state->strm.avail_out = 1000000;
314 olen += (m->m_len = wspace - state->strm.avail_out);
317 * See if we managed to reduce the size of the packet.
318 * If the compressor just gave us a single zero byte, it means
319 * the packet was incompressible.
321 if (m != NULL && olen < orig_len
322 && !(olen == PPP_HDRLEN + 3 && *wptr == 0)) {
323 state->stats.comp_bytes += olen;
324 state->stats.comp_packets++;
330 state->stats.inc_bytes += orig_len;
331 state->stats.inc_packets++;
334 state->stats.unc_bytes += orig_len;
335 state->stats.unc_packets++;
341 z_comp_stats(arg, stats)
343 struct compstat *stats;
345 struct deflate_state *state = (struct deflate_state *) arg;
348 *stats = state->stats;
349 stats->ratio = stats->unc_bytes;
350 out = stats->comp_bytes + stats->inc_bytes;
351 if (stats->ratio <= 0x7ffffff)
360 * Allocate space for a decompressor.
363 z_decomp_alloc(options, opt_len)
367 struct deflate_state *state;
370 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
371 || options[1] != CILEN_DEFLATE
372 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
373 || options[3] != DEFLATE_CHK_SEQUENCE)
375 w_size = DEFLATE_SIZE(options[2]);
376 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
379 MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
384 state->strm.next_out = NULL;
385 state->strm.zalloc = zalloc;
386 state->strm.zfree = zfree;
387 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
388 FREE(state, M_DEVBUF);
392 state->w_size = w_size;
393 bzero(&state->stats, sizeof(state->stats));
394 return (void *) state;
401 struct deflate_state *state = (struct deflate_state *) arg;
403 inflateEnd(&state->strm);
404 FREE(state, M_DEVBUF);
408 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
411 int opt_len, unit, hdrlen, mru, debug;
413 struct deflate_state *state = (struct deflate_state *) arg;
415 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
416 || options[1] != CILEN_DEFLATE
417 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
418 || DEFLATE_SIZE(options[2]) != state->w_size
419 || options[3] != DEFLATE_CHK_SEQUENCE)
424 state->hdrlen = hdrlen;
425 state->debug = debug;
428 inflateReset(&state->strm);
437 struct deflate_state *state = (struct deflate_state *) arg;
440 inflateReset(&state->strm);
444 * Decompress a Deflate-compressed packet.
446 * Because of patent problems, we return DECOMP_ERROR for errors
447 * found by inspecting the input data and for system problems, but
448 * DECOMP_FATALERROR for any errors which could possibly be said to
449 * be being detected "after" decompression. For DECOMP_ERROR,
450 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
451 * infringing a patent of Motorola's if we do, so we take CCP down
454 * Given that the frame has the correct sequence number and a good FCS,
455 * errors such as invalid codes in the input most likely indicate a
456 * bug, so we return DECOMP_FATALERROR for them in order to turn off
457 * compression, even though they are detected by inspecting the input.
460 z_decompress(arg, mi, mop)
462 struct mbuf *mi, **mop;
464 struct deflate_state *state = (struct deflate_state *) arg;
465 struct mbuf *mo, *mo_head;
467 int rlen, olen, ospace;
468 int seq, i, flush, r, decode_proto;
469 u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
472 rptr = mtod(mi, u_char *);
474 for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
479 rptr = mtod(mi, u_char *);
486 /* Check the sequence number. */
487 seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
488 if (seq != state->seqno) {
490 printf("z_decompress%d: bad seq # %d, expected %d\n",
491 state->unit, seq, state->seqno);
496 /* Allocate an output mbuf. */
497 MGETHDR(mo, M_DONTWAIT, MT_DATA);
503 MCLGET(mo, M_DONTWAIT);
504 ospace = M_TRAILINGSPACE(mo);
505 if (state->hdrlen + PPP_HDRLEN < ospace) {
506 mo->m_data += state->hdrlen;
507 ospace -= state->hdrlen;
511 * Fill in the first part of the PPP header. The protocol field
512 * comes from the decompressed data.
514 wptr = mtod(mo, u_char *);
515 wptr[0] = PPP_ADDRESS(hdr);
516 wptr[1] = PPP_CONTROL(hdr);
520 * Set up to call inflate. We set avail_out to 1 initially so we can
521 * look at the first byte of the output and decide whether we have
522 * a 1-byte or 2-byte protocol field.
524 state->strm.next_in = rptr;
525 state->strm.avail_in = rlen;
527 flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
528 rlen += PPP_HDRLEN + DEFLATE_OVHD;
529 state->strm.next_out = wptr + 3;
530 state->strm.avail_out = 1;
535 * Call inflate, supplying more input or output as needed.
538 r = inflate(&state->strm, flush);
543 printf("z_decompress%d: inflate returned %d (%s)\n",
544 state->unit, r, (state->strm.msg? state->strm.msg: ""));
546 return DECOMP_FATALERROR;
548 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
549 break; /* all done */
550 if (state->strm.avail_in == 0 && mi != NULL) {
551 state->strm.next_in = mtod(mi, u_char *);
552 state->strm.avail_in = mi->m_len;
556 flush = Z_PACKET_FLUSH;
558 if (state->strm.avail_out == 0) {
560 state->strm.avail_out = ospace - PPP_HDRLEN;
561 if ((wptr[3] & 1) == 0) {
562 /* 2-byte protocol field */
564 --state->strm.next_out;
565 ++state->strm.avail_out;
572 MGET(mo->m_next, M_DONTWAIT, MT_DATA);
578 MCLGET(mo, M_DONTWAIT);
579 state->strm.next_out = mtod(mo, u_char *);
580 state->strm.avail_out = ospace = M_TRAILINGSPACE(mo);
588 olen += (mo->m_len = ospace - state->strm.avail_out);
590 if (olen > state->mru + PPP_HDRLEN)
591 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
592 state->unit, olen, state->mru + PPP_HDRLEN);
595 state->stats.unc_bytes += olen;
596 state->stats.unc_packets++;
597 state->stats.comp_bytes += rlen;
598 state->stats.comp_packets++;
605 * Incompressible data has arrived - add it to the history.
612 struct deflate_state *state = (struct deflate_state *) arg;
617 * Check that the protocol is one we handle.
619 rptr = mtod(mi, u_char *);
620 proto = PPP_PROTOCOL(rptr);
621 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
627 * Iterate through the mbufs, adding the characters in them
628 * to the decompressor's history. For the first mbuf, we start
629 * at the either the 1st or 2nd byte of the protocol field,
630 * depending on whether the protocol value is compressible.
633 state->strm.next_in = rptr + 3;
634 state->strm.avail_in = rlen - 3;
636 --state->strm.next_in;
637 ++state->strm.avail_in;
640 r = inflateIncomp(&state->strm);
646 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
647 state->unit, r, (state->strm.msg? state->strm.msg: ""));
653 state->strm.next_in = mtod(mi, u_char *);
654 state->strm.avail_in = mi->m_len;
661 state->stats.inc_bytes += rlen;
662 state->stats.inc_packets++;
663 state->stats.unc_bytes += rlen;
664 state->stats.unc_packets++;
667 #endif /* DO_DEFLATE */