/* 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
* 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.3 1999/04/16 11:35:59 paulus Exp $
*/
#include <stddef.h>
#include <stdlib.h>
#ifdef PPP_DEFS_IN_NET
#include <net/ppp_defs.h>
#else
#include "ppp_defs.h"
#endif
#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 {
struct bsd_dict {
union { /* hash value */
struct {
#ifdef BSD_LITTLE_ENDIAN
#else
#endif
} hs;
} f;
};
/*
* Exported procedures.
*/
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.
*/
/*
* clear the dictionary
*/
static void
{
db->clear_count++;
}
/*
* 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 */
{
/* age the ratio by limiting the size of the counts */
}
/* 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.
*/
return 1;
}
}
}
return 0;
}
/*
* Return statistics.
*/
static void
void *state;
{
else
out >>= 8;
if (out != 0)
}
/*
* Reset state, as on a CCP ResetReq.
*/
static void
void *state;
{
db->clear_count = 0;
}
/*
* Allocate space for a (de) compressor.
*/
static void *
{
int bits;
return NULL;
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;
}
if (!db)
return NULL;
if (!decomp) {
} else {
return NULL;
}
}
return (void *) db;
}
static void
void *state;
{
}
static void *
int opt_len;
{
}
/*
* Initialize the database.
*/
static int
{
int i;
if (opt_len < CILEN_BSD_COMPRESS
return 0;
if (decomp) {
i = LAST+1;
while (i != 0)
}
while (i != 0) {
}
if (debug)
return 1;
}
static int
void *state;
{
}
/*
* Update the "BSD Compress" dictionary on the receiver for
* incompressible data by pretending to compress the incoming data.
*/
static void
void *state;
int mlen;
{
u_char c;
if (ent == 0) {
++rptr;
--mlen;
}
return;
++rptr;
c = *rptr++;
/* validate and then check the entry */
goto nomatch;
continue; /* found (prefix,suffix) */
}
/* continue probing until a match or invalid entry */
do {
goto nomatch;
continue; /* finally found (prefix,suffix) */
nomatch: /* output (count) the prefix */
/* code -> hashtable */
/* expand code size if needed */
/* Invalidate previous hash table entry
* assigned this code, and then take it over.
*/
}
ent = c;
}
++db->incomp_count;
++db->uncomp_count;
/* Increase code size if we would have without the packet
* boundary and as the decompressor will.
*/
}
/*
* 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
void *state;
{
int ilen;
if (*rptr == 0)
++rptr;
++rptr; /* skip protocol (assumed 0xfd) */
/*
* Check the sequence number and give up if it is not what we expect.
*/
printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
return DECOMP_ERROR;
}
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;
--len;
continue;
/*
* 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) {
return DECOMP_FATALERROR;
}
break;
}
printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
printf("max_ent=0x%x dlen=%d seqno=%d\n",
}
return DECOMP_FATALERROR; /* probably a bug */
}
/* Special case for KwKwK string. */
extra = 1;
} else {
extra = 0;
}
return DECOMP_FATALERROR;
}
/*
* Decode this code and install it in the decompressed buffer.
*/
#ifdef DEBUG
--codelen;
if (codelen <= 0) {
printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
return DECOMP_FATALERROR;
}
printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
return DECOMP_FATALERROR;
}
#endif
}
*--p = finchar;
#ifdef DEBUG
if (--codelen != 0)
printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
#endif
if (extra) /* the KwKwK case again */
/*
* 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.
*/
/* look for a free hash table entry */
do {
}
/*
* Invalidate previous hash table entry
* assigned this code, and then take it over
*/
}
/* Expand code size if needed. */
}
}
}
/*
* Keep the checkpoint right so that incompressible packets
* clear the dictionary at the right times.
*/
printf("bsd_decomp%d: peer should have cleared dictionary\n",
}
++db->comp_count;
++db->uncomp_count;
return DECOMP_OK;
}
#endif /* DO_BSD_COMPRESS */