/*
* This version is for use with mbufs on Ultrix systems.
*
- * $Id: bsd-comp.c,v 1.1 1994/11/21 05:02:49 paulus Exp $
+ * $Id: bsd-comp.c,v 1.5 1995/05/02 02:48:14 paulus Exp $
*/
#include "../h/param.h"
#include "ppp_defs.h"
#include "if_ppp.h"
-#define PACKET struct mbuf
+#define PACKETPTR struct mbuf *
#include "ppp-comp.h"
+#if DO_BSD_COMPRESS
+
+#define BSD_LITTLE_ENDIAN /* all Ultrix machines are little-endian */
+
/*
* PPP "BSD compress" compression
* The differences between this compression and the classic BSD LZW
* compression is not going well.
*/
-/*
- * Macros to extract protocol version and number of bits
- * from the third byte of the BSD Compress CCP configuration option.
- */
-#define BSD_VERSION(x) ((x) >> 5)
-#define BSD_NBITS(x) ((x) & 0x1F)
-
-#define BSD_CURRENT_VERSION 1
-
/*
* A dictionary for doing BSD compress.
*/
u_char debug;
u_char unit;
u_short seqno; /* sequence # 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 */
union { /* hash value */
u_int32_t fcode;
struct {
-#if BYTE_ORDER == LITTLE_ENDIAN
+#ifdef BSD_LITTLE_ENDIAN
u_short prefix; /* preceding code */
u_char suffix; /* last character of new code */
u_char pad;
};
#define BSD_OVHD 2 /* BSD compress overhead/packet */
-#define MIN_BSD_BITS 9
-#define BSD_INIT_BITS MIN_BSD_BITS
-#define MAX_BSD_BITS 15
+#define BSD_INIT_BITS BSD_MIN_BITS
static void *bsd_comp_alloc __P((u_char *options, int opt_len));
static void *bsd_decomp_alloc __P((u_char *options, int opt_len));
static void bsd_free __P((void *state));
static int bsd_comp_init __P((void *state, u_char *options, int opt_len,
- int unit, int debug));
+ int unit, int hdrlen, int debug));
static int bsd_decomp_init __P((void *state, u_char *options, int opt_len,
- int unit, int mru, int debug));
+ int unit, int hdrlen, int mru, int debug));
static int bsd_compress __P((void *state, struct mbuf **mret,
struct mbuf *mp, int slen, int maxolen));
static void bsd_incomp __P((void *state, struct mbuf *dmsg));
* Procedures exported to if_ppp.c.
*/
struct compressor ppp_bsd_compress = {
- 0x21, /* compress_proto */
+ CI_BSD_COMPRESS, /* compress_proto */
bsd_comp_alloc, /* comp_alloc */
bsd_free, /* comp_free */
bsd_comp_init, /* comp_init */
#define LAST 255
#define MAXCODE(b) ((1 << (b)) - 1)
-#define BADCODEM1 MAXCODE(MAX_BSD_BITS);
+#define BADCODEM1 MAXCODE(BSD_MAX_BITS)
#define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \
^ (u_int32_t)(prefix))
* clear the dictionary
*/
static void
-bsd_clear(struct bsd_db *db)
+bsd_clear(db)
+ struct bsd_db *db;
{
db->clear_count++;
db->max_ent = FIRST-1;
* must compute the same ratio.
*/
static int /* 1=output CLEAR */
-bsd_check(struct bsd_db *db)
+bsd_check(db)
+ struct bsd_db *db;
{
u_int new_ratio;
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_packets = db->comp_count;
stats->inc_bytes = db->incomp_bytes;
stats->inc_packets = db->incomp_count;
- stats->ratio = (double) db->in_count;
- if (db->bytes_out != 0)
- stats->ratio /= db->bytes_out;
+ stats->ratio = db->in_count;
+ out = db->bytes_out;
+ if (stats->ratio <= 0x7fffff)
+ stats->ratio <<= 8;
+ else
+ out >>= 8;
+ if (out != 0)
+ stats->ratio /= out;
}
/*
u_int newlen, hsize, hshift, maxmaxcode;
struct bsd_db *db;
- if (opt_len != 3 || options[0] != 0x21 || options[1] != 3
+ if (opt_len != CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
+ || options[1] != CILEN_BSD_COMPRESS
|| BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
return NULL;
bits = BSD_NBITS(options[2]);
}
maxmaxcode = MAXCODE(bits);
- newlen = sizeof(*db) + (hsize-1)*(sizeof(db->dict[0]));
- MALLOC(db, struct bsd_db *, newlen, M_DEVBUF, M_NOWAIT);
+ newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
+ KM_ALLOC(db, struct bsd_db *, newlen, KM_DEVBUF, KM_NOARG);
if (!db)
return NULL;
bzero(db, sizeof(*db) - sizeof(db->dict));
if (!decomp) {
db->lens = NULL;
} else {
- MALLOC(db->lens, u_short *, (maxmaxcode+1) * sizeof(db->lens[0]),
- M_DEVBUF, M_NOWAIT);
+ KM_ALLOC(db->lens, u_short *, (maxmaxcode+1) * sizeof(db->lens[0]),
+ KM_DEVBUF, KM_NOARG);
if (!db->lens) {
- FREE(db, M_DEVBUF);
+ KM_FREE(db, KM_DEVBUF);
return NULL;
}
}
struct bsd_db *db = (struct bsd_db *) state;
if (db->lens)
- FREE(db->lens, M_DEVBUF);
- FREE(db, M_DEVBUF);
+ KM_FREE(db->lens, KM_DEVBUF);
+ KM_FREE(db, KM_DEVBUF);
}
static void *
* Initialize the database.
*/
static int
-bsd_init(db, options, opt_len, unit, mru, debug, decomp)
+bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp)
struct bsd_db *db;
u_char *options;
- int opt_len, unit, mru, debug, decomp;
+ int opt_len, unit, hdrlen, mru, debug, decomp;
{
int i;
- if (opt_len != 3 || options[0] != 0x21 || options[1] != 3
+ 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)
}
db->unit = unit;
+ db->hdrlen = hdrlen;
db->mru = mru;
#ifndef DEBUG
if (debug)
}
static int
-bsd_comp_init(state, options, opt_len, unit, debug)
+bsd_comp_init(state, options, opt_len, unit, hdrlen, debug)
void *state;
u_char *options;
- int opt_len, unit, debug;
+ int opt_len, unit, hdrlen, debug;
{
return bsd_init((struct bsd_db *) state, options, opt_len,
- unit, 0, debug, 0);
+ unit, hdrlen, 0, debug, 0);
}
static int
-bsd_decomp_init(state, options, opt_len, unit, mru, debug)
+bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug)
void *state;
u_char *options;
- int opt_len, unit, mru, debug;
+ int opt_len, unit, hdrlen, mru, debug;
{
return bsd_init((struct bsd_db *) state, options, opt_len,
- unit, mru, debug, 1);
+ unit, hdrlen, mru, debug, 1);
}
* One change from the BSD compress command is that when the
* code size expands, we do not output a bunch of padding.
*/
-int /* new slen */
+static int /* new slen */
bsd_compress(state, mret, mp, slen, maxolen)
void *state;
struct mbuf **mret; /* return compressed mbuf chain here */
MGET(m->m_next, M_DONTWAIT, MT_DATA); \
m = m->m_next; \
if (m) { \
- m->m_len = 0; \
- if (maxolen - olen > MLEN) \
+ if (maxolen - olen > MLEN) { \
MCLGET(m, clp); \
+ } \
+ m->m_len = 0; \
wptr = mtod(m, u_char *); \
cp_end = wptr + M_TRAILINGSPACE(m); \
} else \
MGET(m, M_DONTWAIT, MT_DATA);
*mret = m;
if (m != NULL) {
- m->m_len = 0;
- if (maxolen > MLEN)
+ if (maxolen + db->hdrlen > MLEN) {
MCLGET(m, clp);
+ }
+ m->m_off += db->hdrlen;
+ m->m_len = 0;
wptr = mtod(m, u_char *);
cp_end = wptr + M_TRAILINGSPACE(m);
} else
OUTPUT(ent); /* output the last code */
db->bytes_out += olen;
db->in_count += ilen;
-/* if (bitno < 32)
+ if (bitno < 32)
++db->bytes_out; /* count complete bytes */
if (bsd_check(db))
db->uncomp_bytes += ilen;
++db->uncomp_count;
- if (olen + PPP_HDRLEN + BSD_OVHD > maxolen && *mret != NULL) {
+ if (olen + PPP_HDRLEN + BSD_OVHD > maxolen) {
/* throw away the compressed stuff if it is longer than uncompressed */
- m_freem(*mret);
- *mret = NULL;
+ if (*mret != NULL) {
+ m_freem(*mret);
+ *mret = NULL;
+ }
++db->incomp_count;
db->incomp_bytes += ilen;
} else {
db->in_count += ilen;
(void)bsd_check(db);
- ++db->comp_count;
- db->comp_bytes += bitno / 8;
++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.
/*
- * Decompress "BSD Compress"
+ * 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.
*/
-int
+static int
bsd_decompress(state, cmp, dmpp)
void *state;
struct mbuf *cmp, **dmpp;
* Check the sequence number and give up if it differs from
* the value we're expecting.
*/
- if (seq != db->seqno++) {
+ 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;
}
+ ++db->seqno;
/*
* Allocate one mbuf to start with.
if (dmp == NULL)
return DECOMP_ERROR;
mret = dmp;
- dmp->m_len = 0;
dmp->m_next = NULL;
MCLGET(dmp, clp);
+ dmp->m_len = 0;
+ dmp->m_off += db->hdrlen;
wptr = mtod(dmp, u_char *);
space = M_TRAILINGSPACE(dmp) - PPP_HDRLEN + 1;
}
}
bsd_clear(db);
- explen = 0;
+ explen = ilen = 0;
break;
}
m_freem(mret);
return DECOMP_ERROR;
}
- m->m_len = 0;
m->m_next = NULL;
dmp->m_next = m;
MCLGET(m, clp);
+ m->m_len = 0;
space = M_TRAILINGSPACE(m) - (codelen + extra);
if (space < 0) {
/* now that's what I call *compression*. */
return DECOMP_FATALERROR;
#endif /* DEBUG */
}
+#endif /* DO_BSD_COMPRESS */