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 mbufs on BSD-derived systems.
6 * Copyright (c) 1994 The Australian National University.
9 * Permission to use, copy, modify, and distribute this software and its
10 * documentation is hereby granted, provided that the above copyright
11 * notice appears in all copies. This software is provided without any
12 * warranty, express or implied. The Australian National University
13 * makes no representations about the suitability of this software for
16 * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
17 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
18 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
19 * THE AUSTRALIAN NATIONAL UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY
22 * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
23 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
25 * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
26 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
29 * $Id: ppp-deflate.c,v 1.1 1996/01/18 03:19:11 paulus Exp $
32 #include <sys/param.h>
33 #include <sys/types.h>
35 #include <net/ppp_defs.h>
37 #define PACKETPTR struct mbuf *
38 #include <net/ppp-comp.h>
39 #include "common/zlib.h"
44 * State for a Deflate (de)compressor.
46 struct deflate_state {
54 struct compstat stats;
57 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
59 static void *zalloc __P((void *, u_int items, u_int size));
60 static void zfree __P((void *, void *ptr, u_int nb));
61 static void *z_comp_alloc __P((u_char *options, int opt_len));
62 static void *z_decomp_alloc __P((u_char *options, int opt_len));
63 static void z_comp_free __P((void *state));
64 static void z_decomp_free __P((void *state));
65 static int z_comp_init __P((void *state, u_char *options, int opt_len,
66 int unit, int hdrlen, int debug));
67 static int z_decomp_init __P((void *state, u_char *options, int opt_len,
68 int unit, int hdrlen, int mru, int debug));
69 static int z_compress __P((void *state, struct mbuf **mret,
70 struct mbuf *mp, int slen, int maxolen));
71 static void z_incomp __P((void *state, struct mbuf *dmsg));
72 static int z_decompress __P((void *state, struct mbuf *cmp,
74 static void z_comp_reset __P((void *state));
75 static void z_decomp_reset __P((void *state));
76 static void z_comp_stats __P((void *state, struct compstat *stats));
79 * Procedures exported to if_ppp.c.
81 struct compressor ppp_deflate = {
82 CI_DEFLATE, /* compress_proto */
83 z_comp_alloc, /* comp_alloc */
84 z_comp_free, /* comp_free */
85 z_comp_init, /* comp_init */
86 z_comp_reset, /* comp_reset */
87 z_compress, /* compress */
88 z_comp_stats, /* comp_stat */
89 z_decomp_alloc, /* decomp_alloc */
90 z_decomp_free, /* decomp_free */
91 z_decomp_init, /* decomp_init */
92 z_decomp_reset, /* decomp_reset */
93 z_decompress, /* decompress */
94 z_incomp, /* incomp */
95 z_comp_stats, /* decomp_stat */
99 * Space allocation and freeing routines for use by zlib routines.
102 zalloc(notused, items, size)
108 MALLOC(ptr, void *, items * size, M_DEVBUF, M_NOWAIT);
113 zfree(notused, ptr, nbytes)
122 * Allocate space for a compressor.
125 z_comp_alloc(options, opt_len)
129 struct deflate_state *state;
132 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
133 || options[1] != CILEN_DEFLATE
134 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
135 || options[3] != DEFLATE_CHK_SEQUENCE)
137 w_size = DEFLATE_SIZE(options[2]);
138 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
141 MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
146 state->strm.next_in = NULL;
147 state->strm.zalloc = zalloc;
148 state->strm.zfree = zfree;
149 if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
150 -w_size, 8, Z_DEFAULT_STRATEGY, DEFLATE_OVHD+2) != Z_OK) {
151 FREE(state, M_DEVBUF);
155 state->w_size = w_size;
156 bzero(&state->stats, sizeof(state->stats));
157 return (void *) state;
164 struct deflate_state *state = (struct deflate_state *) arg;
166 deflateEnd(&state->strm);
167 FREE(state, M_DEVBUF);
171 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
174 int opt_len, unit, hdrlen, debug;
176 struct deflate_state *state = (struct deflate_state *) arg;
178 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
179 || options[1] != CILEN_DEFLATE
180 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
181 || DEFLATE_SIZE(options[2]) != state->w_size
182 || options[3] != DEFLATE_CHK_SEQUENCE)
187 state->hdrlen = hdrlen;
188 state->debug = debug;
190 deflateReset(&state->strm);
199 struct deflate_state *state = (struct deflate_state *) arg;
202 deflateReset(&state->strm);
206 z_compress(arg, mret, mp, orig_len, maxolen)
208 struct mbuf **mret; /* compressed packet (out) */
209 struct mbuf *mp; /* uncompressed packet (in) */
210 int orig_len, maxolen;
212 struct deflate_state *state = (struct deflate_state *) arg;
213 u_char *rptr, *wptr, *wp0;
214 int proto, olen, wspace, r, flush;
218 * Check that the protocol is in the range we handle.
220 rptr = mtod(mp, u_char *);
221 proto = PPP_PROTOCOL(rptr);
222 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb) {
228 printf("z_comp%d: pkt in len=%d proto=%x seq=%d\n",
229 state->unit, orig_len, proto, state->seqno);
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;
265 rptr += (proto > 0xff)? 2: 3; /* skip 1st proto byte if 0 */
266 state->strm.next_in = rptr;
267 state->strm.avail_in = mtod(mp, u_char *) + mp->m_len - rptr;
269 flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
273 wp0 = state->strm.next_in;
274 printf("z_comp%d: deflate flush=%d in=%d[%x %x %x %x]\n",
275 state->unit, flush, state->strm.avail_in,
276 wp0[0], wp0[1], wp0[2], wp0[3]);
277 wp0 = state->strm.next_out;
279 r = deflate(&state->strm, flush);
281 printf("z_comp%d: ret %d inleft=%d gen %d[%x %x %x %x]\n",
282 state->unit, r, state->strm.avail_in,
283 state->strm.next_out - wp0, wp0[0], wp0[1], wp0[2], wp0[3]);
286 printf("z_compress: deflate returned %d (%s)\n",
287 r, (state->strm.msg? state->strm.msg: ""));
290 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
291 break; /* all done */
292 if (state->strm.avail_in == 0 && mp != NULL) {
293 state->strm.next_in = mtod(mp, u_char *);
294 state->strm.avail_in = mp->m_len;
297 flush = Z_PACKET_FLUSH;
299 if (state->strm.avail_out == 0) {
303 MGET(m->m_next, M_DONTWAIT, MT_DATA);
307 if (maxolen - olen > MLEN)
308 MCLGET(m, M_DONTWAIT);
309 state->strm.next_out = mtod(m, u_char *);
310 state->strm.avail_out = wspace = M_TRAILINGSPACE(m);
314 state->strm.next_out = NULL;
315 state->strm.avail_out = 1000000;
319 olen += (m->m_len = wspace - state->strm.avail_out);
322 * See if we managed to reduce the size of the packet.
323 * If the compressor just gave us a single zero byte, it means
324 * the packet was incompressible.
326 if (olen < orig_len && m != NULL
327 && !(olen == PPP_HDRLEN + 3 && *wptr == 0)) {
328 state->stats.comp_bytes += olen;
329 state->stats.comp_packets++;
335 state->stats.inc_bytes += orig_len;
336 state->stats.inc_packets++;
339 state->stats.unc_bytes += orig_len;
340 state->stats.unc_packets++;
346 z_comp_stats(arg, stats)
348 struct compstat *stats;
350 struct deflate_state *state = (struct deflate_state *) arg;
353 *stats = state->stats;
354 stats->ratio = stats->unc_bytes;
355 out = stats->comp_bytes + stats->unc_bytes;
356 if (stats->ratio <= 0x7ffffff)
365 * Allocate space for a decompressor.
368 z_decomp_alloc(options, opt_len)
372 struct deflate_state *state;
375 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
376 || options[1] != CILEN_DEFLATE
377 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
378 || options[3] != DEFLATE_CHK_SEQUENCE)
380 w_size = DEFLATE_SIZE(options[2]);
381 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
384 MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
389 state->strm.next_out = NULL;
390 state->strm.zalloc = zalloc;
391 state->strm.zfree = zfree;
392 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
393 FREE(state, M_DEVBUF);
397 state->w_size = w_size;
398 bzero(&state->stats, sizeof(state->stats));
399 return (void *) state;
406 struct deflate_state *state = (struct deflate_state *) arg;
408 inflateEnd(&state->strm);
409 FREE(state, M_DEVBUF);
413 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
416 int opt_len, unit, hdrlen, mru, debug;
418 struct deflate_state *state = (struct deflate_state *) arg;
420 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
421 || options[1] != CILEN_DEFLATE
422 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
423 || DEFLATE_SIZE(options[2]) != state->w_size
424 || options[3] != DEFLATE_CHK_SEQUENCE)
429 state->hdrlen = hdrlen;
430 state->debug = debug;
433 inflateReset(&state->strm);
442 struct deflate_state *state = (struct deflate_state *) arg;
445 inflateReset(&state->strm);
449 * Decompress a Deflate-compressed packet.
451 * Because of patent problems, we return DECOMP_ERROR for errors
452 * found by inspecting the input data and for system problems, but
453 * DECOMP_FATALERROR for any errors which could possibly be said to
454 * be being detected "after" decompression. For DECOMP_ERROR,
455 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
456 * infringing a patent of Motorola's if we do, so we take CCP down
459 * Given that the frame has the correct sequence number and a good FCS,
460 * errors such as invalid codes in the input most likely indicate a
461 * bug, so we return DECOMP_FATALERROR for them in order to turn off
462 * compression, even though they are detected by inspecting the input.
465 z_decompress(arg, mi, mop)
467 struct mbuf *mi, **mop;
469 struct deflate_state *state = (struct deflate_state *) arg;
470 struct mbuf *mo, *mo_head;
471 u_char *rptr, *wptr, *wp0;
472 int rlen, olen, ospace;
473 int seq, i, flush, r, decode_proto;
474 u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
477 rptr = mtod(mi, u_char *);
479 for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
484 rptr = mtod(mi, u_char *);
491 /* Check the sequence number. */
492 seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
493 if (seq != state->seqno) {
495 printf("z_decompress%d: bad seq # %d, expected %d\n",
496 state->unit, seq, state->seqno);
505 for (x = mi; (x = x->m_next) != NULL; )
507 printf("z_decomp%d: pkt in len=%d seq=%d\n", state->unit, l, seq);
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.
552 wp0 = state->strm.next_in;
553 printf("z_decomp%d: inflate flush=%d in=%d[%x %x %x %x]\n",
554 state->unit, flush, state->strm.avail_in,
555 wp0[0], wp0[1], wp0[2], wp0[3]);
556 wp0 = state->strm.next_out;
558 r = inflate(&state->strm, flush);
560 printf("z_decomp%d: ret %d inleft=%d gen %d[%x %x %x %x]\n",
561 state->unit, r, state->strm.avail_in,
562 state->strm.next_out - wp0, wp0[0], wp0[1], wp0[2], wp0[3]);
566 printf("z_decompress%d: inflate returned %d (%s)\n",
567 state->unit, r, (state->strm.msg? state->strm.msg: ""));
569 return DECOMP_FATALERROR;
571 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
572 break; /* all done */
573 if (state->strm.avail_in == 0 && mi != NULL) {
574 state->strm.next_in = mtod(mi, u_char *);
575 state->strm.avail_in = mi->m_len;
579 flush = Z_PACKET_FLUSH;
581 if (state->strm.avail_out == 0) {
583 state->strm.avail_out = ospace - PPP_HDRLEN;
584 if ((wptr[3] & 1) == 0) {
585 /* 2-byte protocol field */
587 --state->strm.next_out;
588 ++state->strm.avail_out;
595 MGET(mo->m_next, M_DONTWAIT, MT_DATA);
601 MCLGET(mo, M_DONTWAIT);
602 state->strm.next_out = mtod(mo, u_char *);
603 state->strm.avail_out = ospace = M_TRAILINGSPACE(mo);
611 olen += (mo->m_len = ospace - state->strm.avail_out);
613 state->stats.unc_bytes += olen;
614 state->stats.unc_packets++;
615 state->stats.comp_bytes += rlen;
616 state->stats.comp_packets++;
623 * Incompressible data has arrived - add it to the history.
630 struct deflate_state *state = (struct deflate_state *) arg;
635 * Check that the protocol is one we handle.
637 rptr = mtod(mi, u_char *);
638 proto = PPP_PROTOCOL(rptr);
639 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
645 * Iterate through the mbufs, adding the characters in them
646 * to the decompressor's history. For the first mbuf, we start
647 * at the either the 1st or 2nd byte of the protocol field,
648 * depending on whether the protocol value is compressible.
651 state->strm.next_in = rptr + 3;
652 state->strm.avail_in = rlen - 3;
654 --state->strm.next_in;
655 ++state->strm.avail_in;
658 r = inflateIncomp(&state->strm);
662 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
663 state->unit, r, (state->strm.msg? state->strm.msg: ""));
670 state->strm.next_in = mtod(mi, u_char *);
671 state->strm.avail_in = mi->m_len;
678 state->stats.inc_bytes += rlen;
679 state->stats.inc_packets++;
680 state->stats.unc_bytes += rlen;
681 state->stats.unc_packets++;
684 #endif /* DO_DEFLATE */