1N/A/*
1N/A * ppp_deflate.c - interface the zlib procedures for Deflate compression
1N/A * and decompression (as used by gzip) to the PPP code.
1N/A *
1N/A * Copyright (c) 1994 The Australian National University.
1N/A * All rights reserved.
1N/A *
1N/A * Permission to use, copy, modify, and distribute this software and its
1N/A * documentation is hereby granted, provided that the above copyright
1N/A * notice appears in all copies. This software is provided without any
1N/A * warranty, express or implied. The Australian National University
1N/A * makes no representations about the suitability of this software for
1N/A * any purpose.
1N/A *
1N/A * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
1N/A * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
1N/A * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
1N/A * THE AUSTRALIAN NATIONAL UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY
1N/A * OF SUCH DAMAGE.
1N/A *
1N/A * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
1N/A * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
1N/A * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
1N/A * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
1N/A * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
1N/A * OR MODIFICATIONS.
1N/A *
1N/A * $Id: deflate.c,v 1.3 1999/04/16 11:35:59 paulus Exp $
1N/A */
1N/A
1N/A#include <sys/types.h>
1N/A#include <stddef.h>
1N/A#include <stdlib.h>
1N/A#ifdef PPP_DEFS_IN_NET
1N/A#include <net/ppp_defs.h>
1N/A#else
1N/A#include "ppp_defs.h"
1N/A#endif
1N/A#include "ppp-comp.h"
1N/A#include "zlib.h"
1N/A
1N/A#if DO_DEFLATE
1N/A
1N/A#define DEFLATE_DEBUG 1
1N/A
1N/A/*
1N/A * State for a Deflate (de)compressor.
1N/A */
1N/Astruct deflate_state {
1N/A int seqno;
1N/A int w_size;
1N/A int unit;
1N/A int hdrlen;
1N/A int mru;
1N/A int debug;
1N/A z_stream strm;
1N/A struct compstat stats;
1N/A};
1N/A
1N/A#define DEFLATE_OVHD 2 /* Deflate overhead/packet */
1N/A
1N/Astatic void *z_alloc __P((void *, u_int items, u_int size));
1N/Astatic void z_free __P((void *, void *ptr, u_int nb));
1N/Astatic void *z_decomp_alloc __P((u_char *options, int opt_len));
1N/Astatic void z_decomp_free __P((void *state));
1N/Astatic int z_decomp_init __P((void *state, u_char *options, int opt_len,
1N/A int unit, int hdrlen, int mru, int debug));
1N/Astatic void z_incomp __P((void *state, u_char *dmsg, int len));
1N/Astatic int z_decompress __P((void *state, u_char *cmp, int inlen,
1N/A u_char *dmp, int *outlenp));
1N/Astatic void z_decomp_reset __P((void *state));
1N/Astatic void z_comp_stats __P((void *state, struct compstat *stats));
1N/A
1N/A/*
1N/A * Procedures exported to if_ppp.c.
1N/A */
1N/Astruct compressor ppp_deflate = {
1N/A CI_DEFLATE, /* compress_proto */
1N/A z_decomp_alloc, /* decomp_alloc */
1N/A z_decomp_free, /* decomp_free */
1N/A z_decomp_init, /* decomp_init */
1N/A z_decomp_reset, /* decomp_reset */
1N/A z_decompress, /* decompress */
1N/A z_incomp, /* incomp */
1N/A z_comp_stats, /* decomp_stat */
1N/A};
1N/A
1N/A/*
1N/A * Space allocation and freeing routines for use by zlib routines.
1N/A */
1N/Astatic void *
1N/Az_alloc(notused, items, size)
1N/A void *notused;
1N/A u_int items, size;
1N/A{
1N/A return malloc(items * size);
1N/A}
1N/A
1N/Astatic void
1N/Az_free(notused, ptr, nbytes)
1N/A void *notused;
1N/A void *ptr;
1N/A u_int nbytes;
1N/A{
1N/A free(ptr);
1N/A}
1N/A
1N/Astatic void
1N/Az_comp_stats(arg, stats)
1N/A void *arg;
1N/A struct compstat *stats;
1N/A{
1N/A struct deflate_state *state = (struct deflate_state *) arg;
1N/A u_int out;
1N/A
1N/A *stats = state->stats;
1N/A stats->ratio = stats->unc_bytes;
1N/A out = stats->comp_bytes + stats->unc_bytes;
1N/A if (stats->ratio <= 0x7ffffff)
1N/A stats->ratio <<= 8;
1N/A else
1N/A out >>= 8;
1N/A if (out != 0)
1N/A stats->ratio /= out;
1N/A}
1N/A
1N/A/*
1N/A * Allocate space for a decompressor.
1N/A */
1N/Astatic void *
1N/Az_decomp_alloc(options, opt_len)
1N/A u_char *options;
1N/A int opt_len;
1N/A{
1N/A struct deflate_state *state;
1N/A int w_size;
1N/A
1N/A if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
1N/A || options[1] != CILEN_DEFLATE
1N/A || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
1N/A || options[3] != DEFLATE_CHK_SEQUENCE)
1N/A return NULL;
1N/A w_size = DEFLATE_SIZE(options[2]);
1N/A if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
1N/A return NULL;
1N/A
1N/A state = (struct deflate_state *) malloc(sizeof(*state));
1N/A if (state == NULL)
1N/A return NULL;
1N/A
1N/A state->strm.next_out = NULL;
1N/A state->strm.zalloc = (alloc_func) z_alloc;
1N/A state->strm.zfree = (free_func) z_free;
1N/A if (inflateInit2(&state->strm, -w_size) != Z_OK) {
1N/A free(state);
1N/A return NULL;
1N/A }
1N/A
1N/A state->w_size = w_size;
1N/A memset(&state->stats, 0, sizeof(state->stats));
1N/A return (void *) state;
1N/A}
1N/A
1N/Astatic void
1N/Az_decomp_free(arg)
1N/A void *arg;
1N/A{
1N/A struct deflate_state *state = (struct deflate_state *) arg;
1N/A
1N/A inflateEnd(&state->strm);
1N/A free(state);
1N/A}
1N/A
1N/Astatic int
1N/Az_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
1N/A void *arg;
1N/A u_char *options;
1N/A int opt_len, unit, hdrlen, mru, debug;
1N/A{
1N/A struct deflate_state *state = (struct deflate_state *) arg;
1N/A
1N/A if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
1N/A || options[1] != CILEN_DEFLATE
1N/A || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
1N/A || DEFLATE_SIZE(options[2]) != state->w_size
1N/A || options[3] != DEFLATE_CHK_SEQUENCE)
1N/A return 0;
1N/A
1N/A state->seqno = 0;
1N/A state->unit = unit;
1N/A state->hdrlen = hdrlen;
1N/A state->debug = debug;
1N/A state->mru = mru;
1N/A
1N/A inflateReset(&state->strm);
1N/A
1N/A return 1;
1N/A}
1N/A
1N/Astatic void
1N/Az_decomp_reset(arg)
1N/A void *arg;
1N/A{
1N/A struct deflate_state *state = (struct deflate_state *) arg;
1N/A
1N/A state->seqno = 0;
1N/A inflateReset(&state->strm);
1N/A}
1N/A
1N/A/*
1N/A * Decompress a Deflate-compressed packet.
1N/A *
1N/A * Because of patent problems, we return DECOMP_ERROR for errors
1N/A * found by inspecting the input data and for system problems, but
1N/A * DECOMP_FATALERROR for any errors which could possibly be said to
1N/A * be being detected "after" decompression. For DECOMP_ERROR,
1N/A * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
1N/A * infringing a patent of Motorola's if we do, so we take CCP down
1N/A * instead.
1N/A *
1N/A * Given that the frame has the correct sequence number and a good FCS,
1N/A * errors such as invalid codes in the input most likely indicate a
1N/A * bug, so we return DECOMP_FATALERROR for them in order to turn off
1N/A * compression, even though they are detected by inspecting the input.
1N/A */
1N/Astatic int
1N/Az_decompress(arg, mi, inlen, mo, outlenp)
1N/A void *arg;
1N/A u_char *mi, *mo;
1N/A int inlen, *outlenp;
1N/A{
1N/A struct deflate_state *state = (struct deflate_state *) arg;
1N/A u_char *rptr, *wptr;
1N/A int rlen, olen, ospace;
1N/A int seq, i, flush, r, decode_proto;
1N/A
1N/A rptr = mi;
1N/A if (*rptr == 0)
1N/A ++rptr;
1N/A ++rptr;
1N/A
1N/A /* Check the sequence number. */
1N/A seq = (rptr[0] << 8) + rptr[1];
1N/A rptr += 2;
1N/A if (seq != state->seqno) {
1N/A#if !DEFLATE_DEBUG
1N/A if (state->debug)
1N/A#endif
1N/A printf("z_decompress%d: bad seq # %d, expected %d\n",
1N/A state->unit, seq, state->seqno);
1N/A return DECOMP_ERROR;
1N/A }
1N/A ++state->seqno;
1N/A
1N/A /*
1N/A * Set up to call inflate.
1N/A */
1N/A wptr = mo;
1N/A state->strm.next_in = rptr;
1N/A state->strm.avail_in = mi + inlen - rptr;
1N/A rlen = state->strm.avail_in + PPP_HDRLEN + DEFLATE_OVHD;
1N/A state->strm.next_out = wptr;
1N/A state->strm.avail_out = state->mru + 2;
1N/A
1N/A r = inflate(&state->strm, Z_PACKET_FLUSH);
1N/A if (r != Z_OK) {
1N/A#if !DEFLATE_DEBUG
1N/A if (state->debug)
1N/A#endif
1N/A printf("z_decompress%d: inflate returned %d (%s)\n",
1N/A state->unit, r, (state->strm.msg? state->strm.msg: ""));
1N/A return DECOMP_FATALERROR;
1N/A }
1N/A olen = state->mru + 2 - state->strm.avail_out;
1N/A *outlenp = olen;
1N/A
1N/A if ((wptr[0] & 1) != 0)
1N/A ++olen; /* for suppressed protocol high byte */
1N/A olen += 2; /* for address, control */
1N/A
1N/A#if DEFLATE_DEBUG
1N/A if (olen > state->mru + PPP_HDRLEN)
1N/A printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
1N/A state->unit, olen, state->mru + PPP_HDRLEN);
1N/A#endif
1N/A
1N/A state->stats.unc_bytes += olen;
1N/A state->stats.unc_packets++;
1N/A state->stats.comp_bytes += rlen;
1N/A state->stats.comp_packets++;
1N/A
1N/A return DECOMP_OK;
1N/A}
1N/A
1N/A/*
1N/A * Incompressible data has arrived - add it to the history.
1N/A */
1N/Astatic void
1N/Az_incomp(arg, mi, mlen)
1N/A void *arg;
1N/A u_char *mi;
1N/A int mlen;
1N/A{
1N/A struct deflate_state *state = (struct deflate_state *) arg;
1N/A u_char *rptr;
1N/A int rlen, proto, r;
1N/A
1N/A /*
1N/A * Check that the protocol is one we handle.
1N/A */
1N/A rptr = mi;
1N/A proto = rptr[0];
1N/A if ((proto & 1) == 0)
1N/A proto = (proto << 8) + rptr[1];
1N/A if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
1N/A return;
1N/A
1N/A ++state->seqno;
1N/A
1N/A if (rptr[0] == 0)
1N/A ++rptr;
1N/A rlen = mi + mlen - rptr;
1N/A state->strm.next_in = rptr;
1N/A state->strm.avail_in = rlen;
1N/A r = inflateIncomp(&state->strm);
1N/A if (r != Z_OK) {
1N/A /* gak! */
1N/A#if !DEFLATE_DEBUG
1N/A if (state->debug)
1N/A#endif
1N/A printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
1N/A state->unit, r, (state->strm.msg? state->strm.msg: ""));
1N/A return;
1N/A }
1N/A
1N/A /*
1N/A * Update stats.
1N/A */
1N/A if (proto <= 0xff)
1N/A ++rlen;
1N/A rlen += 2;
1N/A state->stats.inc_bytes += rlen;
1N/A state->stats.inc_packets++;
1N/A state->stats.unc_bytes += rlen;
1N/A state->stats.unc_packets++;
1N/A}
1N/A
1N/A#endif /* DO_DEFLATE */