pppdump: Remove support for decompressing compressed packets

This simplifies the code and reduces its attack surface, in response
to some deficiencies being found in the zlib code.  This should be OK
since probably no-one uses compression on PPP links any more, and in
any case, the code still exists in git if anyone wants it.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
This commit is contained in:
Paul Mackerras 2024-08-20 17:24:34 +10:00
parent f8b00fb34b
commit 2fdc5692ef
7 changed files with 4 additions and 3921 deletions

View File

@ -1,8 +1,4 @@
sbin_PROGRAMS = pppdump
dist_man8_MANS = pppdump.8
pppdump_SOURCES = pppdump.c bsd-comp.c deflate.c zlib.c
noinst_HEADERS = \
ppp-comp.h \
zlib.h
pppdump_SOURCES = pppdump.c

View File

@ -1,733 +0,0 @@
/* Because this code is derived from the 4.3BSD compress source:
*
*
* Copyright (c) 1985, 1986 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* James A. Woods, derived from original work by Spencer Thomas
* and Joseph Orost.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* $Id: bsd-comp.c,v 1.4 2004/01/17 05:47:55 carlsonj Exp $
*/
#include <sys/types.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "ppp-comp.h"
#if DO_BSD_COMPRESS
/*
* PPP "BSD compress" compression
* The differences between this compression and the classic BSD LZW
* source are obvious from the requirement that the classic code worked
* with files while this handles arbitrarily long streams that
* are broken into packets. They are:
*
* When the code size expands, a block of junk is not emitted by
* the compressor and not expected by the decompressor.
*
* New codes are not necessarily assigned every time an old
* code is output by the compressor. This is because a packet
* end forces a code to be emitted, but does not imply that a
* new sequence has been seen.
*
* The compression ratio is checked at the first end of a packet
* after the appropriate gap. Besides simplifying and speeding
* things up, this makes it more likely that the transmitter
* and receiver will agree when the dictionary is cleared when
* compression is not going well.
*/
/*
* A dictionary for doing BSD compress.
*/
struct bsd_db {
int totlen; /* length of this structure */
u_int hsize; /* size of the hash table */
u_char hshift; /* used in hash function */
u_char n_bits; /* current bits/code */
u_char maxbits;
u_char debug;
u_char unit;
u_short seqno; /* sequence number of next packet */
u_int hdrlen; /* header length to preallocate */
u_int mru;
u_int maxmaxcode; /* largest valid code */
u_int max_ent; /* largest code in use */
u_int in_count; /* uncompressed bytes, aged */
u_int bytes_out; /* compressed bytes, aged */
u_int ratio; /* recent compression ratio */
u_int checkpoint; /* when to next check the ratio */
u_int clear_count; /* times dictionary cleared */
u_int incomp_count; /* incompressible packets */
u_int incomp_bytes; /* incompressible bytes */
u_int uncomp_count; /* uncompressed packets */
u_int uncomp_bytes; /* uncompressed bytes */
u_int comp_count; /* compressed packets */
u_int comp_bytes; /* compressed bytes */
u_short *lens; /* array of lengths of codes */
struct bsd_dict {
union { /* hash value */
u_int32_t fcode;
struct {
#ifdef BSD_LITTLE_ENDIAN
u_short prefix; /* preceding code */
u_char suffix; /* last character of new code */
u_char pad;
#else
u_char pad;
u_char suffix; /* last character of new code */
u_short prefix; /* preceding code */
#endif
} hs;
} f;
u_short codem1; /* output of hash table -1 */
u_short cptr; /* map code to hash table entry */
} dict[1];
};
#define BSD_OVHD 2 /* BSD compress overhead/packet */
#define BSD_INIT_BITS BSD_MIN_BITS
static void *bsd_decomp_alloc(u_char *options, int opt_len);
static void bsd_free(void *state);
static int bsd_decomp_init(void *state, u_char *options, int opt_len,
int unit, int hdrlen, int mru, int debug);
static void bsd_incomp(void *state, u_char *dmsg, int len);
static int bsd_decompress(void *state, u_char *cmp, int inlen,
u_char *dmp, int *outlen);
static void bsd_reset(void *state);
static void bsd_comp_stats(void *state, struct compstat *stats);
/*
* Exported procedures.
*/
struct compressor ppp_bsd_compress = {
CI_BSD_COMPRESS, /* compress_proto */
bsd_decomp_alloc, /* decomp_alloc */
bsd_free, /* decomp_free */
bsd_decomp_init, /* decomp_init */
bsd_reset, /* decomp_reset */
bsd_decompress, /* decompress */
bsd_incomp, /* incomp */
bsd_comp_stats, /* decomp_stat */
};
/*
* the next two codes should not be changed lightly, as they must not
* lie within the contiguous general code space.
*/
#define CLEAR 256 /* table clear output code */
#define FIRST 257 /* first free entry */
#define LAST 255
#define MAXCODE(b) ((1 << (b)) - 1)
#define BADCODEM1 MAXCODE(BSD_MAX_BITS)
#define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
^ (u_int32_t)(prefix))
#define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \
+ (u_int32_t)(prefix))
#define CHECK_GAP 10000 /* Ratio check interval */
#define RATIO_SCALE_LOG 8
#define RATIO_SCALE (1<<RATIO_SCALE_LOG)
#define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
/*
* clear the dictionary
*/
static void
bsd_clear(struct bsd_db *db)
{
db->clear_count++;
db->max_ent = FIRST-1;
db->n_bits = BSD_INIT_BITS;
db->ratio = 0;
db->bytes_out = 0;
db->in_count = 0;
db->checkpoint = CHECK_GAP;
}
/*
* If the dictionary is full, then see if it is time to reset it.
*
* Compute the compression ratio using fixed-point arithmetic
* with 8 fractional bits.
*
* Since we have an infinite stream instead of a single file,
* watch only the local compression ratio.
*
* Since both peers must reset the dictionary at the same time even in
* the absence of CLEAR codes (while packets are incompressible), they
* must compute the same ratio.
*/
static int /* 1=output CLEAR */
bsd_check(struct bsd_db *db)
{
u_int new_ratio;
if (db->in_count >= db->checkpoint) {
/* age the ratio by limiting the size of the counts */
if (db->in_count >= RATIO_MAX
|| db->bytes_out >= RATIO_MAX) {
db->in_count -= db->in_count/4;
db->bytes_out -= db->bytes_out/4;
}
db->checkpoint = db->in_count + CHECK_GAP;
if (db->max_ent >= db->maxmaxcode) {
/* Reset the dictionary only if the ratio is worse,
* or if it looks as if it has been poisoned
* by incompressible data.
*
* This does not overflow, because
* db->in_count <= RATIO_MAX.
*/
new_ratio = db->in_count << RATIO_SCALE_LOG;
if (db->bytes_out != 0)
new_ratio /= db->bytes_out;
if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
bsd_clear(db);
return 1;
}
db->ratio = new_ratio;
}
}
return 0;
}
/*
* Return statistics.
*/
static void
bsd_comp_stats(void *state, struct compstat *stats)
{
struct bsd_db *db = (struct bsd_db *) state;
u_int out;
stats->unc_bytes = db->uncomp_bytes;
stats->unc_packets = db->uncomp_count;
stats->comp_bytes = db->comp_bytes;
stats->comp_packets = db->comp_count;
stats->inc_bytes = db->incomp_bytes;
stats->inc_packets = db->incomp_count;
u_int ratio = db->in_count;
out = db->bytes_out;
if (ratio <= 0x7fffff)
ratio <<= 8;
else
out >>= 8;
if (out != 0)
stats->ratio = ratio / out;
}
/*
* Reset state, as on a CCP ResetReq.
*/
static void
bsd_reset(void *state)
{
struct bsd_db *db = (struct bsd_db *) state;
db->seqno = 0;
bsd_clear(db);
db->clear_count = 0;
}
/*
* Allocate space for a (de) compressor.
*/
static void *
bsd_alloc(u_char *options, int opt_len, int decomp)
{
int bits;
u_int newlen, hsize, hshift, maxmaxcode;
struct bsd_db *db;
if (opt_len != 3 || options[0] != CI_BSD_COMPRESS || options[1] != 3
|| BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
return NULL;
bits = BSD_NBITS(options[2]);
switch (bits) {
case 9: /* needs 82152 for both directions */
case 10: /* needs 84144 */
case 11: /* needs 88240 */
case 12: /* needs 96432 */
hsize = 5003;
hshift = 4;
break;
case 13: /* needs 176784 */
hsize = 9001;
hshift = 5;
break;
case 14: /* needs 353744 */
hsize = 18013;
hshift = 6;
break;
case 15: /* needs 691440 */
hsize = 35023;
hshift = 7;
break;
case 16: /* needs 1366160--far too much, */
/* hsize = 69001; */ /* and 69001 is too big for cptr */
/* hshift = 8; */ /* in struct bsd_db */
/* break; */
default:
return NULL;
}
maxmaxcode = MAXCODE(bits);
newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
db = (struct bsd_db *) malloc(newlen);
if (!db)
return NULL;
memset(db, 0, sizeof(*db) - sizeof(db->dict));
if (!decomp) {
db->lens = NULL;
} else {
db->lens = (u_short *) malloc((maxmaxcode+1) * sizeof(db->lens[0]));
if (!db->lens) {
free(db);
return NULL;
}
}
db->totlen = newlen;
db->hsize = hsize;
db->hshift = hshift;
db->maxmaxcode = maxmaxcode;
db->maxbits = bits;
return (void *) db;
}
static void
bsd_free(void *state)
{
struct bsd_db *db = (struct bsd_db *) state;
if (db->lens)
free(db->lens);
free(db);
}
static void *
bsd_decomp_alloc(u_char *options, int opt_len)
{
return bsd_alloc(options, opt_len, 1);
}
/*
* Initialize the database.
*/
static int
bsd_init(struct bsd_db *db, u_char *options, int opt_len, int unit,
int hdrlen, int mru, int debug, int decomp)
{
int i;
if (opt_len < CILEN_BSD_COMPRESS
|| options[0] != CI_BSD_COMPRESS || options[1] != CILEN_BSD_COMPRESS
|| BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
|| BSD_NBITS(options[2]) != db->maxbits
|| (decomp && db->lens == NULL))
return 0;
if (decomp) {
i = LAST+1;
while (i != 0)
db->lens[--i] = 1;
}
i = db->hsize;
while (i != 0) {
db->dict[--i].codem1 = BADCODEM1;
db->dict[i].cptr = 0;
}
db->unit = unit;
db->hdrlen = hdrlen;
db->mru = mru;
if (debug)
db->debug = 1;
bsd_reset(db);
return 1;
}
static int
bsd_decomp_init(void *state, u_char *options, int opt_len,
int unit, int hdrlen, int mru, int debug)
{
return bsd_init((struct bsd_db *) state, options, opt_len,
unit, hdrlen, mru, debug, 1);
}
/*
* Update the "BSD Compress" dictionary on the receiver for
* incompressible data by pretending to compress the incoming data.
*/
static void
bsd_incomp(void *state, u_char *dmsg, int mlen)
{
struct bsd_db *db = (struct bsd_db *) state;
u_int hshift = db->hshift;
u_int max_ent = db->max_ent;
u_int n_bits = db->n_bits;
struct bsd_dict *dictp;
u_int32_t fcode;
u_char c;
long hval, disp;
int slen, ilen;
u_int bitno = 7;
u_char *rptr;
u_int ent;
rptr = dmsg;
ent = rptr[0]; /* get the protocol */
if (ent == 0) {
++rptr;
--mlen;
ent = rptr[0];
}
if ((ent & 1) == 0 || ent < 0x21 || ent > 0xf9)
return;
db->seqno++;
ilen = 1; /* count the protocol as 1 byte */
++rptr;
slen = dmsg + mlen - rptr;
ilen += slen;
for (; slen > 0; --slen) {
c = *rptr++;
fcode = BSD_KEY(ent, c);
hval = BSD_HASH(ent, c, hshift);
dictp = &db->dict[hval];
/* validate and then check the entry */
if (dictp->codem1 >= max_ent)
goto nomatch;
if (dictp->f.fcode == fcode) {
ent = dictp->codem1+1;
continue; /* found (prefix,suffix) */
}
/* continue probing until a match or invalid entry */
disp = (hval == 0) ? 1 : hval;
do {
hval += disp;
if (hval >= db->hsize)
hval -= db->hsize;
dictp = &db->dict[hval];
if (dictp->codem1 >= max_ent)
goto nomatch;
} while (dictp->f.fcode != fcode);
ent = dictp->codem1+1;
continue; /* finally found (prefix,suffix) */
nomatch: /* output (count) the prefix */
bitno += n_bits;
/* code -> hashtable */
if (max_ent < db->maxmaxcode) {
struct bsd_dict *dictp2;
/* expand code size if needed */
if (max_ent >= MAXCODE(n_bits))
db->n_bits = ++n_bits;
/* Invalidate previous hash table entry
* assigned this code, and then take it over.
*/
dictp2 = &db->dict[max_ent+1];
if (db->dict[dictp2->cptr].codem1 == max_ent)
db->dict[dictp2->cptr].codem1 = BADCODEM1;
dictp2->cptr = hval;
dictp->codem1 = max_ent;
dictp->f.fcode = fcode;
db->max_ent = ++max_ent;
db->lens[max_ent] = db->lens[ent]+1;
}
ent = c;
}
bitno += n_bits; /* output (count) the last code */
db->bytes_out += bitno/8;
db->in_count += ilen;
(void)bsd_check(db);
++db->incomp_count;
db->incomp_bytes += ilen;
++db->uncomp_count;
db->uncomp_bytes += ilen;
/* Increase code size if we would have without the packet
* boundary and as the decompressor will.
*/
if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
db->n_bits++;
}
/*
* Decompress "BSD Compress"
*
* Because of patent problems, we return DECOMP_ERROR for errors
* found by inspecting the input data and for system problems, but
* DECOMP_FATALERROR for any errors which could possibly be said to
* be being detected "after" decompression. For DECOMP_ERROR,
* we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
* infringing a patent of Motorola's if we do, so we take CCP down
* instead.
*
* Given that the frame has the correct sequence number and a good FCS,
* errors such as invalid codes in the input most likely indicate a
* bug, so we return DECOMP_FATALERROR for them in order to turn off
* compression, even though they are detected by inspecting the input.
*/
static int
bsd_decompress(void *state, u_char *cmsg, int inlen, u_char *dmp, int *outlenp)
{
struct bsd_db *db = (struct bsd_db *) state;
u_int max_ent = db->max_ent;
u_int32_t accm = 0;
u_int bitno = 32; /* 1st valid bit in accm */
u_int n_bits = db->n_bits;
u_int tgtbitno = 32-n_bits; /* bitno when we have a code */
struct bsd_dict *dictp;
int explen, seq, len;
u_int incode, oldcode, finchar;
u_char *p, *rptr, *wptr;
int ilen;
int codelen, extra;
rptr = cmsg;
if (*rptr == 0)
++rptr;
++rptr; /* skip protocol (assumed 0xfd) */
seq = (rptr[0] << 8) + rptr[1];
rptr += BSD_OVHD;
ilen = len = cmsg + inlen - rptr;
/*
* Check the sequence number and give up if it is not what we expect.
*/
if (seq != db->seqno++) {
if (db->debug)
printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
db->unit, seq, db->seqno - 1);
return DECOMP_ERROR;
}
wptr = dmp + db->hdrlen;
oldcode = CLEAR;
explen = 0;
while (len > 0) {
/*
* Accumulate bytes until we have a complete code.
* Then get the next code, relying on the 32-bit,
* unsigned accm to mask the result.
*/
bitno -= 8;
accm |= *rptr++ << bitno;
--len;
if (tgtbitno < bitno)
continue;
incode = accm >> tgtbitno;
accm <<= n_bits;
bitno += n_bits;
if (incode == CLEAR) {
/*
* The dictionary must only be cleared at
* the end of a packet. But there could be an
* empty message block at the end.
*/
if (len > 0) {
if (db->debug)
printf("bsd_decomp%d: bad CLEAR\n", db->unit);
return DECOMP_FATALERROR;
}
bsd_clear(db);
explen = ilen = 0;
break;
}
if (incode > max_ent + 2 || incode > db->maxmaxcode
|| (incode > max_ent && oldcode == CLEAR)) {
if (db->debug) {
printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
db->unit, incode, oldcode);
printf("max_ent=0x%x seqno=%d\n",
max_ent, db->seqno);
}
return DECOMP_FATALERROR; /* probably a bug */
}
/* Special case for KwKwK string. */
if (incode > max_ent) {
finchar = oldcode;
extra = 1;
} else {
finchar = incode;
extra = 0;
}
codelen = db->lens[finchar];
explen += codelen + extra;
if (explen > db->mru + 1) {
if (db->debug)
printf("bsd_decomp%d: ran out of mru\n", db->unit);
return DECOMP_FATALERROR;
}
/*
* Decode this code and install it in the decompressed buffer.
*/
p = (wptr += codelen);
while (finchar > LAST) {
dictp = &db->dict[db->dict[finchar].cptr];
#ifdef DEBUG
--codelen;
if (codelen <= 0) {
printf("bsd_decomp%d: fell off end of chain ", db->unit);
printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
incode, finchar, db->dict[finchar].cptr, max_ent);
return DECOMP_FATALERROR;
}
if (dictp->codem1 != finchar-1) {
printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
db->unit, incode, finchar);
printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
db->dict[finchar].cptr, dictp->codem1);
return DECOMP_FATALERROR;
}
#endif
*--p = dictp->f.hs.suffix;
finchar = dictp->f.hs.prefix;
}
*--p = finchar;
#ifdef DEBUG
if (--codelen != 0)
printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
db->unit, codelen, incode, max_ent);
#endif
if (extra) /* the KwKwK case again */
*wptr++ = finchar;
/*
* If not first code in a packet, and
* if not out of code space, then allocate a new code.
*
* Keep the hash table correct so it can be used
* with uncompressed packets.
*/
if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
struct bsd_dict *dictp2;
u_int32_t fcode;
int hval, disp;
fcode = BSD_KEY(oldcode,finchar);
hval = BSD_HASH(oldcode,finchar,db->hshift);
dictp = &db->dict[hval];
/* look for a free hash table entry */
if (dictp->codem1 < max_ent) {
disp = (hval == 0) ? 1 : hval;
do {
hval += disp;
if (hval >= db->hsize)
hval -= db->hsize;
dictp = &db->dict[hval];
} while (dictp->codem1 < max_ent);
}
/*
* Invalidate previous hash table entry
* assigned this code, and then take it over
*/
dictp2 = &db->dict[max_ent+1];
if (db->dict[dictp2->cptr].codem1 == max_ent) {
db->dict[dictp2->cptr].codem1 = BADCODEM1;
}
dictp2->cptr = hval;
dictp->codem1 = max_ent;
dictp->f.fcode = fcode;
db->max_ent = ++max_ent;
db->lens[max_ent] = db->lens[oldcode]+1;
/* Expand code size if needed. */
if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
db->n_bits = ++n_bits;
tgtbitno = 32-n_bits;
}
}
oldcode = incode;
}
*outlenp = wptr - (dmp + db->hdrlen);
/*
* Keep the checkpoint right so that incompressible packets
* clear the dictionary at the right times.
*/
db->bytes_out += ilen;
db->in_count += explen;
if (bsd_check(db) && db->debug) {
printf("bsd_decomp%d: peer should have cleared dictionary\n",
db->unit);
}
++db->comp_count;
db->comp_bytes += ilen + BSD_OVHD;
++db->uncomp_count;
db->uncomp_bytes += explen;
return DECOMP_OK;
}
#endif /* DO_BSD_COMPRESS */

View File

@ -1,336 +0,0 @@
/*
* ppp_deflate.c - interface the zlib procedures for Deflate compression
* and decompression (as used by gzip) to the PPP code.
*
* Copyright (c) 1994 Paul Mackerras. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The name(s) of the authors of this software must not be used to
* endorse or promote products derived from this software without
* prior written permission.
*
* 4. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by Paul Mackerras
* <paulus@ozlabs.org>".
*
* THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* $Id: deflate.c,v 1.5 2004/01/17 05:47:55 carlsonj Exp $
*/
#include <sys/types.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "ppp-comp.h"
#include "zlib.h"
#if DO_DEFLATE
#define DEFLATE_DEBUG 1
/*
* State for a Deflate (de)compressor.
*/
struct deflate_state {
int seqno;
int w_size;
int unit;
int hdrlen;
int mru;
int debug;
z_stream strm;
struct compstat stats;
};
#define DEFLATE_OVHD 2 /* Deflate overhead/packet */
static void *z_alloc(void *, u_int items, u_int size);
static void z_free(void *, void *ptr, u_int nb);
static void *z_decomp_alloc(u_char *options, int opt_len);
static void z_decomp_free(void *state);
static int z_decomp_init(void *state, u_char *options, int opt_len,
int unit, int hdrlen, int mru, int debug);
static void z_incomp(void *state, u_char *dmsg, int len);
static int z_decompress(void *state, u_char *cmp, int inlen,
u_char *dmp, int *outlenp);
static void z_decomp_reset(void *state);
static void z_comp_stats(void *state, struct compstat *stats);
/*
* Procedures exported to if_ppp.c.
*/
struct compressor ppp_deflate = {
CI_DEFLATE, /* compress_proto */
z_decomp_alloc, /* decomp_alloc */
z_decomp_free, /* decomp_free */
z_decomp_init, /* decomp_init */
z_decomp_reset, /* decomp_reset */
z_decompress, /* decompress */
z_incomp, /* incomp */
z_comp_stats, /* decomp_stat */
};
/*
* Space allocation and freeing routines for use by zlib routines.
*/
static void *
z_alloc(void *notused, u_int items, u_int size)
{
return malloc(items * size);
}
static void
z_free(void *notused, void *ptr, u_int nbytes)
{
free(ptr);
}
static void
z_comp_stats(void *arg, struct compstat *stats)
{
struct deflate_state *state = (struct deflate_state *) arg;
u_int out;
*stats = state->stats;
stats->ratio = stats->unc_bytes;
out = stats->comp_bytes + stats->unc_bytes;
u_int ratio = stats->ratio;
if (ratio <= 0x7ffffff)
ratio <<= 8;
else
out >>= 8;
if (out != 0)
stats->ratio = ratio / out;
}
/*
* Allocate space for a decompressor.
*/
static void *
z_decomp_alloc(u_char *options, int opt_len)
{
struct deflate_state *state;
int w_size;
if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
|| options[1] != CILEN_DEFLATE
|| DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
|| options[3] != DEFLATE_CHK_SEQUENCE)
return NULL;
w_size = DEFLATE_SIZE(options[2]);
if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
return NULL;
state = (struct deflate_state *) malloc(sizeof(*state));
if (state == NULL)
return NULL;
state->strm.next_out = NULL;
state->strm.zalloc = (alloc_func) z_alloc;
state->strm.zfree = (free_func) z_free;
if (inflateInit2(&state->strm, -w_size) != Z_OK) {
free(state);
return NULL;
}
state->w_size = w_size;
memset(&state->stats, 0, sizeof(state->stats));
return (void *) state;
}
static void
z_decomp_free(void *arg)
{
struct deflate_state *state = (struct deflate_state *) arg;
inflateEnd(&state->strm);
free(state);
}
static int
z_decomp_init(void *arg, u_char *options, int opt_len,
int unit, int hdrlen, int mru, int debug)
{
struct deflate_state *state = (struct deflate_state *) arg;
if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
|| options[1] != CILEN_DEFLATE
|| DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
|| DEFLATE_SIZE(options[2]) != state->w_size
|| options[3] != DEFLATE_CHK_SEQUENCE)
return 0;
state->seqno = 0;
state->unit = unit;
state->hdrlen = hdrlen;
state->debug = debug;
state->mru = mru;
inflateReset(&state->strm);
return 1;
}
static void
z_decomp_reset(void *arg)
{
struct deflate_state *state = (struct deflate_state *) arg;
state->seqno = 0;
inflateReset(&state->strm);
}
/*
* Decompress a Deflate-compressed packet.
*
* Because of patent problems, we return DECOMP_ERROR for errors
* found by inspecting the input data and for system problems, but
* DECOMP_FATALERROR for any errors which could possibly be said to
* be being detected "after" decompression. For DECOMP_ERROR,
* we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
* infringing a patent of Motorola's if we do, so we take CCP down
* instead.
*
* Given that the frame has the correct sequence number and a good FCS,
* errors such as invalid codes in the input most likely indicate a
* bug, so we return DECOMP_FATALERROR for them in order to turn off
* compression, even though they are detected by inspecting the input.
*/
static int
z_decompress(void *arg, u_char *mi, int inlen, u_char *mo, int *outlenp)
{
struct deflate_state *state = (struct deflate_state *) arg;
u_char *rptr, *wptr;
int rlen, olen;
int seq, r;
rptr = mi;
if (*rptr == 0)
++rptr;
++rptr;
/* Check the sequence number. */
seq = (rptr[0] << 8) + rptr[1];
rptr += 2;
if (seq != state->seqno) {
#if !DEFLATE_DEBUG
if (state->debug)
#endif
printf("z_decompress%d: bad seq # %d, expected %d\n",
state->unit, seq, state->seqno);
return DECOMP_ERROR;
}
++state->seqno;
/*
* Set up to call inflate.
*/
wptr = mo;
state->strm.next_in = rptr;
state->strm.avail_in = mi + inlen - rptr;
rlen = state->strm.avail_in + PPP_HDRLEN + DEFLATE_OVHD;
state->strm.next_out = wptr;
state->strm.avail_out = state->mru + 2;
r = inflate(&state->strm, Z_PACKET_FLUSH);
if (r != Z_OK) {
#if !DEFLATE_DEBUG
if (state->debug)
#endif
printf("z_decompress%d: inflate returned %d (%s)\n",
state->unit, r, (state->strm.msg? state->strm.msg: ""));
return DECOMP_FATALERROR;
}
olen = state->mru + 2 - state->strm.avail_out;
*outlenp = olen;
if ((wptr[0] & 1) != 0)
++olen; /* for suppressed protocol high byte */
olen += 2; /* for address, control */
#if DEFLATE_DEBUG
if (olen > state->mru + PPP_HDRLEN)
printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
state->unit, olen, state->mru + PPP_HDRLEN);
#endif
state->stats.unc_bytes += olen;
state->stats.unc_packets++;
state->stats.comp_bytes += rlen;
state->stats.comp_packets++;
return DECOMP_OK;
}
/*
* Incompressible data has arrived - add it to the history.
*/
static void
z_incomp(void *arg, u_char *mi, int mlen)
{
struct deflate_state *state = (struct deflate_state *) arg;
u_char *rptr;
int rlen, proto, r;
/*
* Check that the protocol is one we handle.
*/
rptr = mi;
proto = rptr[0];
if ((proto & 1) == 0)
proto = (proto << 8) + rptr[1];
if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
return;
++state->seqno;
if (rptr[0] == 0)
++rptr;
rlen = mi + mlen - rptr;
state->strm.next_in = rptr;
state->strm.avail_in = rlen;
r = inflateIncomp(&state->strm);
if (r != Z_OK) {
/* gak! */
#if !DEFLATE_DEBUG
if (state->debug)
#endif
printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
state->unit, r, (state->strm.msg? state->strm.msg: ""));
return;
}
/*
* Update stats.
*/
if (proto <= 0xff)
++rlen;
rlen += 2;
state->stats.inc_bytes += rlen;
state->stats.inc_packets++;
state->stats.unc_bytes += rlen;
state->stats.unc_packets++;
}
#endif /* DO_DEFLATE */

View File

@ -1,164 +0,0 @@
/*
* ppp-comp.h - Definitions for doing PPP packet compression.
*
* Copyright (c) 1994 Paul Mackerras. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The name(s) of the authors of this software must not be used to
* endorse or promote products derived from this software without
* prior written permission.
*
* 4. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by Paul Mackerras
* <paulus@ozlabs.org>".
*
* THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* $Id: ppp-comp.h,v 1.2 2002/12/06 09:49:16 paulus Exp $
*/
#ifndef _NET_PPP_COMP_H
#define _NET_PPP_COMP_H
/*
* The following symbols control whether we include code for
* various compression methods.
*/
#ifndef DO_BSD_COMPRESS
#define DO_BSD_COMPRESS 1 /* by default, include BSD-Compress */
#endif
#ifndef DO_DEFLATE
#define DO_DEFLATE 1 /* by default, include Deflate */
#endif
#define DO_PREDICTOR_1 0
#define DO_PREDICTOR_2 0
#if defined(SOL2)
#include <net/ppp_defs.h>
#else
#include <linux/ppp_defs.h>
#endif
/*
* Structure giving methods for compression/decompression.
*/
struct compressor {
int compress_proto; /* CCP compression protocol number */
/* Allocate space for a decompressor (receive side) */
void *(*decomp_alloc)(u_char *options, int opt_len);
/* Free space used by a decompressor */
void (*decomp_free)(void *state);
/* Initialize a decompressor */
int (*decomp_init)(void *state, u_char *options, int opt_len,
int unit, int hdrlen, int mru, int debug);
/* Reset a decompressor */
void (*decomp_reset)(void *state);
/* Decompress a packet. */
int (*decompress)(void *state, u_char *mp, int inlen,
u_char *dmp, int *outlen);
/* Update state for an incompressible packet received */
void (*incomp)(void *state, u_char *mp, int len);
/* Return decompression statistics */
void (*decomp_stat)(void *state, struct compstat *stats);
};
/*
* Return values for decompress routine.
* We need to make these distinctions so that we can disable certain
* useful functionality, namely sending a CCP reset-request as a result
* of an error detected after decompression. This is to avoid infringing
* a patent held by Motorola.
* Don't you just lurve software patents.
*/
#define DECOMP_OK 0 /* everything went OK */
#define DECOMP_ERROR 1 /* error detected before decomp. */
#define DECOMP_FATALERROR 2 /* error detected after decomp. */
/*
* CCP codes.
*/
#define CCP_CONFREQ 1
#define CCP_CONFACK 2
#define CCP_CONFNAK 3
#define CCP_CONFREJ 4
#define CCP_TERMREQ 5
#define CCP_TERMACK 6
#define CCP_RESETREQ 14
#define CCP_RESETACK 15
/*
* Max # bytes for a CCP option
*/
#define CCP_MAX_OPTION_LENGTH 32
/*
* Parts of a CCP packet.
*/
#define CCP_CODE(dp) ((dp)[0])
#define CCP_ID(dp) ((dp)[1])
#define CCP_LENGTH(dp) (((dp)[2] << 8) + (dp)[3])
#define CCP_HDRLEN 4
#define CCP_OPT_CODE(dp) ((dp)[0])
#define CCP_OPT_LENGTH(dp) ((dp)[1])
#define CCP_OPT_MINLEN 2
/*
* Definitions for BSD-Compress.
*/
#define CI_BSD_COMPRESS 21 /* config. option for BSD-Compress */
#define CILEN_BSD_COMPRESS 3 /* length of config. option */
/* Macros for handling the 3rd byte of the BSD-Compress config option. */
#define BSD_NBITS(x) ((x) & 0x1F) /* number of bits requested */
#define BSD_VERSION(x) ((x) >> 5) /* version of option format */
#define BSD_CURRENT_VERSION 1 /* current version number */
#define BSD_MAKE_OPT(v, n) (((v) << 5) | (n))
#define BSD_MIN_BITS 9 /* smallest code size supported */
#define BSD_MAX_BITS 15 /* largest code size supported */
/*
* Definitions for Deflate.
*/
#define CI_DEFLATE 26 /* config option for Deflate */
#define CI_DEFLATE_DRAFT 24 /* value used in original draft RFC */
#define CILEN_DEFLATE 4 /* length of its config option */
#define DEFLATE_MIN_SIZE 8
#define DEFLATE_MAX_SIZE 15
#define DEFLATE_METHOD_VAL 8
#define DEFLATE_SIZE(x) (((x) >> 4) + DEFLATE_MIN_SIZE)
#define DEFLATE_METHOD(x) ((x) & 0x0F)
#define DEFLATE_MAKE_OPT(w) ((((w) - DEFLATE_MIN_SIZE) << 4) \
+ DEFLATE_METHOD_VAL)
#define DEFLATE_CHK_SEQUENCE 0
/*
* Definitions for other, as yet unsupported, compression methods.
*/
#define CI_PREDICTOR_1 1 /* config option for Predictor-1 */
#define CILEN_PREDICTOR_1 2 /* length of its config option */
#define CI_PREDICTOR_2 2 /* config option for Predictor-2 */
#define CILEN_PREDICTOR_2 2 /* length of its config option */
#endif /* _NET_PPP_COMP_H */

View File

@ -39,12 +39,9 @@
#include <time.h>
#include <sys/types.h>
#include "ppp-comp.h"
int hexmode;
int pppmode;
int reverse;
int decompress;
int mru = 1500;
int abs_times;
time_t start_time;
@ -57,7 +54,6 @@ extern char *optarg;
void dumplog();
void dumpppp();
void show_time();
void handle_ccp();
int
main(ac, av)
@ -79,9 +75,6 @@ main(ac, av)
case 'r':
reverse = 1;
break;
case 'd':
decompress = 1;
break;
case 'm':
mru = atoi(optarg);
break;
@ -235,6 +228,9 @@ static u_short fcstab[256] = {
};
#define PPP_FCS(fcs, c) (((fcs) >> 8) ^ fcstab[((fcs) ^ (c)) & 0xff])
#define PPP_INITFCS 0xffff /* Initial FCS value */
#define PPP_GOODFCS 0xf0b8 /* Good final FCS value */
struct pkt {
int cnt;
int esc;
@ -327,51 +323,6 @@ dumpppp(f)
if (endp - r > mru)
printf(" ERROR: length (%zd) > MRU (%d)\n",
endp - r, mru);
if (decompress && fcs == PPP_GOODFCS) {
/* See if this is a CCP or compressed packet */
d = dbuf;
r = p;
if (r[0] == 0xff && r[1] == 3) {
*d++ = *r++;
*d++ = *r++;
}
proto = r[0];
if ((proto & 1) == 0)
proto = (proto << 8) + r[1];
if (proto == PPP_CCP) {
handle_ccp(pkt, r + 2, endp - r - 2);
} else if (proto == PPP_COMP) {
if ((pkt->flags & CCP_ISUP)
&& (pkt->flags & CCP_DECOMP_RUN)
&& pkt->state
&& (pkt->flags & CCP_ERR) == 0) {
rv = pkt->comp->decompress(pkt->state, r,
endp - r, d, &dn);
switch (rv) {
case DECOMP_OK:
p = dbuf;
nb = d + dn - p;
if ((d[0] & 1) == 0)
--dn;
--dn;
if (dn > mru)
printf(" ERROR: decompressed length (%d) > MRU (%d)\n", dn, mru);
break;
case DECOMP_ERROR:
printf(" DECOMPRESSION ERROR\n");
pkt->flags |= CCP_ERROR;
break;
case DECOMP_FATALERROR:
printf(" FATAL DECOMPRESSION ERROR\n");
pkt->flags |= CCP_FATALERROR;
break;
}
}
} else if (pkt->state
&& (pkt->flags & CCP_DECOMP_RUN)) {
pkt->comp->incomp(pkt->state, r, endp - r);
}
}
do {
nl = nb < 16? nb: 16;
printf("%s ", q);
@ -432,75 +383,6 @@ dumpppp(f)
}
}
extern struct compressor ppp_bsd_compress, ppp_deflate;
struct compressor *compressors[] = {
#if DO_BSD_COMPRESS
&ppp_bsd_compress,
#endif
#if DO_DEFLATE
&ppp_deflate,
#endif
NULL
};
void
handle_ccp(cp, dp, len)
struct pkt *cp;
u_char *dp;
int len;
{
int clen;
struct compressor **comp;
if (len < CCP_HDRLEN)
return;
clen = CCP_LENGTH(dp);
if (clen > len)
return;
switch (CCP_CODE(dp)) {
case CCP_CONFACK:
cp->flags &= ~(CCP_DECOMP_RUN | CCP_ISUP);
if (clen < CCP_HDRLEN + CCP_OPT_MINLEN
|| clen < CCP_HDRLEN + CCP_OPT_LENGTH(dp + CCP_HDRLEN))
break;
dp += CCP_HDRLEN;
clen -= CCP_HDRLEN;
for (comp = compressors; *comp != NULL; ++comp) {
if ((*comp)->compress_proto == dp[0]) {
if (cp->state != NULL) {
(*cp->comp->decomp_free)(cp->state);
cp->state = NULL;
}
cp->comp = *comp;
cp->state = (*comp)->decomp_alloc(dp, CCP_OPT_LENGTH(dp));
cp->flags |= CCP_ISUP;
if (cp->state != NULL
&& (*cp->comp->decomp_init)
(cp->state, dp, clen, 0, 0, 8192, 1))
cp->flags = (cp->flags & ~CCP_ERR) | CCP_DECOMP_RUN;
break;
}
}
break;
case CCP_CONFNAK:
case CCP_CONFREJ:
cp->flags &= ~(CCP_DECOMP_RUN | CCP_ISUP);
break;
case CCP_RESETACK:
if (cp->flags & CCP_ISUP) {
if (cp->state && (cp->flags & CCP_DECOMP_RUN)) {
(*cp->comp->decomp_reset)(cp->state);
cp->flags &= ~CCP_ERROR;
}
}
break;
}
}
void
show_time(f, c)
FILE *f;

File diff suppressed because it is too large Load Diff

View File

@ -1,413 +0,0 @@
/* $Id: zlib.h,v 1.1 1999/03/23 03:21:58 paulus Exp $ */
/*
* This file is derived from zlib.h and zconf.h from the zlib-0.95
* distribution by Jean-loup Gailly and Mark Adler, with some additions
* by Paul Mackerras to aid in implementing Deflate compression and
* decompression for PPP packets.
*/
/* zlib.h -- interface of the 'zlib' general purpose compression library
version 0.95, Aug 16th, 1995.
Copyright (C) 1995 Jean-loup Gailly and Mark Adler
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jean-loup Gailly Mark Adler
gzip@prep.ai.mit.edu madler@alumni.caltech.edu
*/
#ifndef _ZLIB_H
#define _ZLIB_H
/* #include "zconf.h" */ /* included directly here */
/* zconf.h -- configuration of the zlib compression library
* Copyright (C) 1995 Jean-loup Gailly.
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* From: zconf.h,v 1.12 1995/05/03 17:27:12 jloup Exp */
/*
The library does not install any signal handler. It is recommended to
add at least a handler for SIGSEGV when decompressing; the library checks
the consistency of the input data whenever possible but may go nuts
for some forms of corrupted input.
*/
/*
* Compile with -DMAXSEG_64K if the alloc function cannot allocate more
* than 64k bytes at a time (needed on systems with 16-bit int).
* Compile with -DUNALIGNED_OK if it is OK to access shorts or ints
* at addresses which are not a multiple of their size.
* Under DOS, -DFAR=far or -DFAR=__far may be needed.
*/
#ifndef STDC
# if defined(MSDOS) || defined(__STDC__) || defined(__cplusplus)
# define STDC
# endif
#endif
#ifdef __MWERKS__ /* Metrowerks CodeWarrior declares fileno() in unix.h */
# include <unix.h>
#endif
#ifndef FAR
# define FAR
#endif
/* Maximum value for windowBits in deflateInit2 and inflateInit2 */
#ifndef MAX_WBITS
# define MAX_WBITS 15 /* 32K LZ77 window */
#endif
/*
The memory requirements for inflate are (in bytes) 1 << windowBits
that is, 32K for windowBits=15 (default value) plus a few kilobytes
for small objects.
*/
/* Type declarations */
#ifndef OF /* function prototypes */
# ifdef STDC
# define OF(args) args
# else
# define OF(args) ()
# endif
#endif
typedef unsigned char Byte; /* 8 bits */
typedef unsigned int uInt; /* 16 bits or more */
typedef unsigned long uLong; /* 32 bits or more */
typedef Byte FAR Bytef;
typedef char FAR charf;
typedef int FAR intf;
typedef uInt FAR uIntf;
typedef uLong FAR uLongf;
#ifdef STDC
typedef void FAR *voidpf;
typedef void *voidp;
#else
typedef Byte FAR *voidpf;
typedef Byte *voidp;
#endif
/* end of original zconf.h */
#define ZLIB_VERSION "0.95P"
/*
The 'zlib' compression library provides in-memory compression and
decompression functions, including integrity checks of the uncompressed
data. This version of the library supports only one compression method
(deflation) but other algorithms may be added later and will have the same
stream interface.
For compression the application must provide the output buffer and
may optionally provide the input buffer for optimization. For decompression,
the application must provide the input buffer and may optionally provide
the output buffer for optimization.
Compression can be done in a single step if the buffers are large
enough (for example if an input file is mmap'ed), or can be done by
repeated calls of the compression function. In the latter case, the
application must provide more input and/or consume the output
(providing more output space) before each call.
*/
typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
typedef void (*free_func) OF((voidpf opaque, voidpf address, uInt nbytes));
struct internal_state;
typedef struct z_stream_s {
Bytef *next_in; /* next input byte */
uInt avail_in; /* number of bytes available at next_in */
uLong total_in; /* total nb of input bytes read so far */
Bytef *next_out; /* next output byte should be put there */
uInt avail_out; /* remaining free space at next_out */
uLong total_out; /* total nb of bytes output so far */
char *msg; /* last error message, NULL if no error */
struct internal_state FAR *state; /* not visible by applications */
alloc_func zalloc; /* used to allocate the internal state */
free_func zfree; /* used to free the internal state */
voidp opaque; /* private data object passed to zalloc and zfree */
Byte data_type; /* best guess about the data type: ascii or binary */
} z_stream;
/*
The application must update next_in and avail_in when avail_in has
dropped to zero. It must update next_out and avail_out when avail_out
has dropped to zero. The application must initialize zalloc, zfree and
opaque before calling the init function. All other fields are set by the
compression library and must not be updated by the application.
The opaque value provided by the application will be passed as the first
parameter for calls of zalloc and zfree. This can be useful for custom
memory management. The compression library attaches no meaning to the
opaque value.
zalloc must return Z_NULL if there is not enough memory for the object.
On 16-bit systems, the functions zalloc and zfree must be able to allocate
exactly 65536 bytes, but will not be required to allocate more than this
if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
pointers returned by zalloc for objects of exactly 65536 bytes *must*
have their offset normalized to zero. The default allocation function
provided by this library ensures this (see zutil.c). To reduce memory
requirements and avoid any allocation of 64K objects, at the expense of
compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
The fields total_in and total_out can be used for statistics or
progress reports. After compression, total_in holds the total size of
the uncompressed data and may be saved for use in the decompressor
(particularly if the decompressor wants to decompress everything in
a single step).
*/
/* constants */
#define Z_NO_FLUSH 0
#define Z_PARTIAL_FLUSH 1
#define Z_FULL_FLUSH 2
#define Z_SYNC_FLUSH 3 /* experimental: partial_flush + byte align */
#define Z_FINISH 4
#define Z_PACKET_FLUSH 5
/* See deflate() below for the usage of these constants */
#define Z_OK 0
#define Z_STREAM_END 1
#define Z_ERRNO (-1)
#define Z_STREAM_ERROR (-2)
#define Z_DATA_ERROR (-3)
#define Z_MEM_ERROR (-4)
#define Z_BUF_ERROR (-5)
/* error codes for the compression/decompression functions */
#define Z_BEST_SPEED 1
#define Z_BEST_COMPRESSION 9
#define Z_DEFAULT_COMPRESSION (-1)
/* compression levels */
#define Z_FILTERED 1
#define Z_HUFFMAN_ONLY 2
#define Z_DEFAULT_STRATEGY 0
#define Z_BINARY 0
#define Z_ASCII 1
#define Z_UNKNOWN 2
/* Used to set the data_type field */
#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
extern char *zlib_version;
/* The application can compare zlib_version and ZLIB_VERSION for consistency.
If the first character differs, the library code actually used is
not compatible with the zlib.h header file used by the application.
*/
/* basic functions */
extern int inflateInit OF((z_stream *strm));
/*
Initializes the internal stream state for decompression. The fields
zalloc and zfree must be initialized before by the caller. If zalloc and
zfree are set to Z_NULL, inflateInit updates them to use default allocation
functions.
inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
enough memory. msg is set to null if there is no error message.
inflateInit does not perform any decompression: this will be done by
inflate().
*/
extern int inflate OF((z_stream *strm, int flush));
/*
Performs one or both of the following actions:
- Decompress more input starting at next_in and update next_in and avail_in
accordingly. If not all input can be processed (because there is not
enough room in the output buffer), next_in is updated and processing
will resume at this point for the next call of inflate().
- Provide more output starting at next_out and update next_out and avail_out
accordingly. inflate() always provides as much output as possible
(until there is no more input data or no more space in the output buffer).
Before the call of inflate(), the application should ensure that at least
one of the actions is possible, by providing more input and/or consuming
more output, and updating the next_* and avail_* values accordingly.
The application can consume the uncompressed output when it wants, for
example when the output buffer is full (avail_out == 0), or after each
call of inflate().
If the parameter flush is set to Z_PARTIAL_FLUSH or Z_PACKET_FLUSH,
inflate flushes as much output as possible to the output buffer. The
flushing behavior of inflate is not specified for values of the flush
parameter other than Z_PARTIAL_FLUSH, Z_PACKET_FLUSH or Z_FINISH, but the
current implementation actually flushes as much output as possible
anyway. For Z_PACKET_FLUSH, inflate checks that once all the input data
has been consumed, it is expecting to see the length field of a stored
block; if not, it returns Z_DATA_ERROR.
inflate() should normally be called until it returns Z_STREAM_END or an
error. However if all decompression is to be performed in a single step
(a single call of inflate), the parameter flush should be set to
Z_FINISH. In this case all pending input is processed and all pending
output is flushed; avail_out must be large enough to hold all the
uncompressed data. (The size of the uncompressed data may have been saved
by the compressor for this purpose.) The next operation on this stream must
be inflateEnd to deallocate the decompression state. The use of Z_FINISH
is never required, but can be used to inform inflate that a faster routine
may be used for the single inflate() call.
inflate() returns Z_OK if some progress has been made (more input
processed or more output produced), Z_STREAM_END if the end of the
compressed data has been reached and all uncompressed output has been
produced, Z_DATA_ERROR if the input data was corrupted, Z_STREAM_ERROR if
the stream structure was inconsistent (for example if next_in or next_out
was NULL), Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if no
progress is possible or if there was not enough room in the output buffer
when Z_FINISH is used. In the Z_DATA_ERROR case, the application may then
call inflateSync to look for a good compression block. */
extern int inflateEnd OF((z_stream *strm));
/*
All dynamically allocated data structures for this stream are freed.
This function discards any unprocessed input and does not flush any
pending output.
inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
was inconsistent. In the error case, msg may be set but then points to a
static string (which must not be deallocated).
*/
/* advanced functions */
/*
The following functions are needed only in some special applications.
*/
extern int inflateInit2 OF((z_stream *strm,
int windowBits));
/*
This is another version of inflateInit with more compression options. The
fields next_out, zalloc and zfree must be initialized before by the caller.
The windowBits parameter is the base two logarithm of the maximum window
size (the size of the history buffer). It should be in the range 8..15 for
this version of the library (the value 16 will be allowed soon). The
default value is 15 if inflateInit is used instead. If a compressed stream
with a larger window size is given as input, inflate() will return with
the error code Z_DATA_ERROR instead of trying to allocate a larger window.
If next_out is not null, the library will use this buffer for the history
buffer; the buffer must either be large enough to hold the entire output
data, or have at least 1<<windowBits bytes. If next_out is null, the
library will allocate its own buffer (and leave next_out null). next_in
need not be provided here but must be provided by the application for the
next call of inflate().
If the history buffer is provided by the application, next_out must
never be changed by the application since the decompressor maintains
history information inside this buffer from call to call; the application
can only reset next_out to the beginning of the history buffer when
avail_out is zero and all output has been consumed.
inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was
not enough memory, Z_STREAM_ERROR if a parameter is invalid (such as
windowBits < 8). msg is set to null if there is no error message.
inflateInit2 does not perform any decompression: this will be done by
inflate().
*/
extern int inflateSync OF((z_stream *strm));
/*
Skips invalid compressed data until the special marker (see deflate()
above) can be found, or until all available input is skipped. No output
is provided.
inflateSync returns Z_OK if the special marker has been found, Z_BUF_ERROR
if no more input was provided, Z_DATA_ERROR if no marker has been found,
or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
case, the application may save the current current value of total_in which
indicates where valid compressed data was found. In the error case, the
application may repeatedly call inflateSync, providing more input each time,
until success or end of the input data.
*/
extern int inflateReset OF((z_stream *strm));
/*
This function is equivalent to inflateEnd followed by inflateInit,
but does not free and reallocate all the internal decompression state.
The stream will keep attributes that may have been set by inflateInit2.
inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
stream state was inconsistent (such as zalloc or state being NULL).
*/
extern int inflateIncomp OF((z_stream *strm));
/*
This function adds the data at next_in (avail_in bytes) to the output
history without performing any output. There must be no pending output,
and the decompressor must be expecting to see the start of a block.
Calling this function is equivalent to decompressing a stored block
containing the data at next_in (except that the data is not output).
*/
/* checksum functions */
/*
This function is not related to compression but is exported
anyway because it might be useful in applications using the
compression library.
*/
extern uLong adler32 OF((uLong adler, Bytef *buf, uInt len));
/*
Update a running Adler-32 checksum with the bytes buf[0..len-1] and
return the updated checksum. If buf is NULL, this function returns
the required initial value for the checksum.
An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
much faster. Usage example:
uLong adler = adler32(0L, Z_NULL, 0);
while (read_buffer(buffer, length) != EOF) {
adler = adler32(adler, buffer, length);
}
if (adler != original_adler) error();
*/
#ifndef _Z_UTIL_H
struct internal_state {int dummy;}; /* hack for buggy compilers */
#endif
#endif /* _ZLIB_H */