1 /* $Id: ppp-deflate.c,v 1.5 1997/03/04 03:33:28 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.zfree = zfree;
152 if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
153 -w_size, 8, Z_DEFAULT_STRATEGY, DEFLATE_OVHD+2) != Z_OK) {
154 FREE(state, M_DEVBUF);
158 state->w_size = w_size;
159 bzero(&state->stats, sizeof(state->stats));
160 return (void *) state;
167 struct deflate_state *state = (struct deflate_state *) arg;
169 deflateEnd(&state->strm);
170 FREE(state, M_DEVBUF);
174 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
177 int opt_len, unit, hdrlen, debug;
179 struct deflate_state *state = (struct deflate_state *) arg;
181 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
182 || options[1] != CILEN_DEFLATE
183 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
184 || DEFLATE_SIZE(options[2]) != state->w_size
185 || options[3] != DEFLATE_CHK_SEQUENCE)
190 state->hdrlen = hdrlen;
191 state->debug = debug;
193 deflateReset(&state->strm);
202 struct deflate_state *state = (struct deflate_state *) arg;
205 deflateReset(&state->strm);
209 z_compress(arg, mret, mp, orig_len, maxolen)
211 struct mbuf **mret; /* compressed packet (out) */
212 struct mbuf *mp; /* uncompressed packet (in) */
213 int orig_len, maxolen;
215 struct deflate_state *state = (struct deflate_state *) arg;
217 int proto, olen, wspace, r, flush;
221 * Check that the protocol is in the range we handle.
223 rptr = mtod(mp, u_char *);
224 proto = PPP_PROTOCOL(rptr);
225 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb) {
230 /* Allocate one mbuf initially. */
231 if (maxolen > orig_len)
233 MGET(m, M_DONTWAIT, MT_DATA);
237 if (maxolen + state->hdrlen > MLEN)
238 MCLGET(m, M_DONTWAIT);
239 wspace = M_TRAILINGSPACE(m);
240 if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
241 m->m_data += state->hdrlen;
242 wspace -= state->hdrlen;
244 wptr = mtod(m, u_char *);
247 * Copy over the PPP header and store the 2-byte sequence number.
249 wptr[0] = PPP_ADDRESS(rptr);
250 wptr[1] = PPP_CONTROL(rptr);
251 wptr[2] = PPP_COMP >> 8;
254 wptr[0] = state->seqno >> 8;
255 wptr[1] = state->seqno;
257 state->strm.next_out = wptr;
258 state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
260 state->strm.next_out = NULL;
261 state->strm.avail_out = 1000000;
267 rptr += (proto > 0xff)? 2: 3; /* skip 1st proto byte if 0 */
268 state->strm.next_in = rptr;
269 state->strm.avail_in = mtod(mp, u_char *) + mp->m_len - rptr;
271 flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
274 r = deflate(&state->strm, flush);
276 printf("z_compress: deflate returned %d (%s)\n",
277 r, (state->strm.msg? state->strm.msg: ""));
280 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
281 break; /* all done */
282 if (state->strm.avail_in == 0 && mp != NULL) {
283 state->strm.next_in = mtod(mp, u_char *);
284 state->strm.avail_in = mp->m_len;
287 flush = Z_PACKET_FLUSH;
289 if (state->strm.avail_out == 0) {
293 MGET(m->m_next, M_DONTWAIT, MT_DATA);
297 if (maxolen - olen > MLEN)
298 MCLGET(m, M_DONTWAIT);
299 state->strm.next_out = mtod(m, u_char *);
300 state->strm.avail_out = wspace = M_TRAILINGSPACE(m);
304 state->strm.next_out = NULL;
305 state->strm.avail_out = 1000000;
310 olen += (m->m_len = wspace - state->strm.avail_out);
313 * See if we managed to reduce the size of the packet.
314 * If the compressor just gave us a single zero byte, it means
315 * the packet was incompressible.
317 if (m != NULL && olen < orig_len
318 && !(olen == PPP_HDRLEN + 3 && *wptr == 0)) {
319 state->stats.comp_bytes += olen;
320 state->stats.comp_packets++;
326 state->stats.inc_bytes += orig_len;
327 state->stats.inc_packets++;
330 state->stats.unc_bytes += orig_len;
331 state->stats.unc_packets++;
337 z_comp_stats(arg, stats)
339 struct compstat *stats;
341 struct deflate_state *state = (struct deflate_state *) arg;
344 *stats = state->stats;
345 stats->ratio = stats->unc_bytes;
346 out = stats->comp_bytes + stats->inc_bytes;
347 if (stats->ratio <= 0x7ffffff)
356 * Allocate space for a decompressor.
359 z_decomp_alloc(options, opt_len)
363 struct deflate_state *state;
366 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
367 || options[1] != CILEN_DEFLATE
368 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
369 || options[3] != DEFLATE_CHK_SEQUENCE)
371 w_size = DEFLATE_SIZE(options[2]);
372 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
375 MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
380 state->strm.next_out = NULL;
381 state->strm.zalloc = zalloc;
382 state->strm.zfree = zfree;
383 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
384 FREE(state, M_DEVBUF);
388 state->w_size = w_size;
389 bzero(&state->stats, sizeof(state->stats));
390 return (void *) state;
397 struct deflate_state *state = (struct deflate_state *) arg;
399 inflateEnd(&state->strm);
400 FREE(state, M_DEVBUF);
404 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
407 int opt_len, unit, hdrlen, mru, debug;
409 struct deflate_state *state = (struct deflate_state *) arg;
411 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
412 || options[1] != CILEN_DEFLATE
413 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
414 || DEFLATE_SIZE(options[2]) != state->w_size
415 || options[3] != DEFLATE_CHK_SEQUENCE)
420 state->hdrlen = hdrlen;
421 state->debug = debug;
424 inflateReset(&state->strm);
433 struct deflate_state *state = (struct deflate_state *) arg;
436 inflateReset(&state->strm);
440 * Decompress a Deflate-compressed packet.
442 * Because of patent problems, we return DECOMP_ERROR for errors
443 * found by inspecting the input data and for system problems, but
444 * DECOMP_FATALERROR for any errors which could possibly be said to
445 * be being detected "after" decompression. For DECOMP_ERROR,
446 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
447 * infringing a patent of Motorola's if we do, so we take CCP down
450 * Given that the frame has the correct sequence number and a good FCS,
451 * errors such as invalid codes in the input most likely indicate a
452 * bug, so we return DECOMP_FATALERROR for them in order to turn off
453 * compression, even though they are detected by inspecting the input.
456 z_decompress(arg, mi, mop)
458 struct mbuf *mi, **mop;
460 struct deflate_state *state = (struct deflate_state *) arg;
461 struct mbuf *mo, *mo_head;
463 int rlen, olen, ospace;
464 int seq, i, flush, r, decode_proto;
465 u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
468 rptr = mtod(mi, u_char *);
470 for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
475 rptr = mtod(mi, u_char *);
482 /* Check the sequence number. */
483 seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
484 if (seq != state->seqno) {
486 printf("z_decompress%d: bad seq # %d, expected %d\n",
487 state->unit, seq, state->seqno);
492 /* Allocate an output mbuf. */
493 MGETHDR(mo, M_DONTWAIT, MT_DATA);
499 MCLGET(mo, M_DONTWAIT);
500 ospace = M_TRAILINGSPACE(mo);
501 if (state->hdrlen + PPP_HDRLEN < ospace) {
502 mo->m_data += state->hdrlen;
503 ospace -= state->hdrlen;
507 * Fill in the first part of the PPP header. The protocol field
508 * comes from the decompressed data.
510 wptr = mtod(mo, u_char *);
511 wptr[0] = PPP_ADDRESS(hdr);
512 wptr[1] = PPP_CONTROL(hdr);
516 * Set up to call inflate. We set avail_out to 1 initially so we can
517 * look at the first byte of the output and decide whether we have
518 * a 1-byte or 2-byte protocol field.
520 state->strm.next_in = rptr;
521 state->strm.avail_in = rlen;
523 flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
524 rlen += PPP_HDRLEN + DEFLATE_OVHD;
525 state->strm.next_out = wptr + 3;
526 state->strm.avail_out = 1;
531 * Call inflate, supplying more input or output as needed.
534 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 = mtod(mi, u_char *);
548 state->strm.avail_in = mi->m_len;
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;
568 MGET(mo->m_next, M_DONTWAIT, MT_DATA);
574 MCLGET(mo, M_DONTWAIT);
575 state->strm.next_out = mtod(mo, u_char *);
576 state->strm.avail_out = ospace = M_TRAILINGSPACE(mo);
584 olen += (mo->m_len = ospace - state->strm.avail_out);
586 if (olen > state->mru + PPP_HDRLEN)
587 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
588 state->unit, olen, state->mru + PPP_HDRLEN);
591 state->stats.unc_bytes += olen;
592 state->stats.unc_packets++;
593 state->stats.comp_bytes += rlen;
594 state->stats.comp_packets++;
601 * Incompressible data has arrived - add it to the history.
608 struct deflate_state *state = (struct deflate_state *) arg;
613 * Check that the protocol is one we handle.
615 rptr = mtod(mi, u_char *);
616 proto = PPP_PROTOCOL(rptr);
617 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
623 * Iterate through the mbufs, adding the characters in them
624 * to the decompressor's history. For the first mbuf, we start
625 * at the either the 1st or 2nd byte of the protocol field,
626 * depending on whether the protocol value is compressible.
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);
642 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
643 state->unit, r, (state->strm.msg? state->strm.msg: ""));
649 state->strm.next_in = mtod(mi, u_char *);
650 state->strm.avail_in = mi->m_len;
657 state->stats.inc_bytes += rlen;
658 state->stats.inc_packets++;
659 state->stats.unc_bytes += rlen;
660 state->stats.unc_packets++;
663 #endif /* DO_DEFLATE */