RFC 1977
PPP BSD Compression Protocol
RFC 1977: PPP BSD Compression Protocol π
Okay bestie, let's do the vitals first β¨
- RFC number: 1977
- Title: PPP BSD Compression Protocol
- Author: V. Schryver
- Date: August 1996
- Category: Informational
- Working group: Network Working Group
- Obsoletes / Updates: the title block doesn't list anything here π
Status of This Memo π
Okay so the vibe check on status is that this memo is strictly for information π«Ά It is not an Internet standard of any kind π Distribution is unlimited too, so share it with everyone π§
Abstract π
The Point-to-Point Protocol (PPP) [1] is the go-to standard method for transporting multi-protocol datagrams over point-to-point links π¦
The PPP Compression Control Protocol [2] gives you a way to negotiate and use compression protocols over PPP encapsulated links π₯
This document is all about the Unix Compress compression protocol and how it's used to compress PPP encapsulated packets π
Table of Contents π
1. Introduction .................................... 1
1.1 Licensing ................................. 2
2. BSD Compress Packets ............................ 2
2.1 Packet Format ............................. 5
3. Configuration Option Format ..................... 6
APPENDICES ............................................. 7
A. BSD Compress Algorithm .......................... 7
SECURITY CONSIDERATIONS ................................ 24
REFERENCES ............................................. 24
ACKNOWLEDGEMENTS ....................................... 24
CHAIR'S ADDRESS ........................................ 25
AUTHOR'S ADDRESS ....................................... 25
1. Introduction β¨
Okay so UNIX compress, as embodied in the freely and widely distributed BSD source, has these features:
- dynamic table clearing when compression becomes less effective π§Ή
- automatic turning off of compression when the overall result is not smaller than the input π
- dynamic choice of code width within predetermined limits π
- heavily used for many years in networks, on modem and other point-to-point links, to transfer netnews π‘
- an effective code width requires less than 64KBytes of memory on both sender and receive πΎ
1.1. Licensing π
The BSD Unix compress command source is widely and freely available, with no additional license needed for many computer vendors π The included source code is based on the BSD compress command source and carries only the copyright of The Regents of the University of California. Use the code entirely at your own risk, because it comes with no warranties or indemnifications of any sort π Also, note that there are patents on LZW π
3. Configuration Option Format π
Description
okay so the CCP BSD Compress Configuration Option is how the two peers agree on whether BSD Compress gets used on the link π¦ by default, or if they're in ultimate disagreement π, no compression is used at all. no cap, that's giving "off by default" energy β¨
here's the layout of the option! the fields go left to right, bestie π§
0 1 2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type | Length | Vers| Dict |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Type
21 or 0x15 for BSD compress π
Length
3 πΈ
Vers
Must be the binary number 001 π«Ά
Dict
The size in bits of the largest code used. It can range from 9 to 16. A common choice is 12 π The code included below can support code sizes from 9 to 15.
It's convenient to treat the byte containing the Vers and Dict fields as a single field with legal values ranging from 0x29 to 0x30 β¨
Note that the peer receiving compressed data must use the same code size as the peer sending data. It is not practical for the receiver to use a larger dictionary or code size, because both dictionaries must be cleared at the same time, even when the data is not compressible, so that uncompressed packets are being sent, and so the receiver cannot receive LZW "CLEAR" codes. π
When a received Configure-Request specifies a smaller dictionary than the local preference, it is often best to accept it instead of using a Configure-Nak to ask the peer to specify a larger dictionary. the vibe is "don't be extra, just say yes" π
A. BSD Compress Algorithm π
okay the big reveal: this code is the core of a commercial workstation implementation π It was derived by transliterating the 4.*BSD compress command. Honestly? It's unlikely to be of direct use in any system that doesn't have the same mixture of mbufs and STREAMS buffers π It may need retuning for CPUs other than RISC's with many registers and certain addressing modes. BUT the code itself is the most accurate and unambiguous way of defining the changes to the BSD compress source required to apply it to a stream instead of a file. the receipts are all right here π§Ύπ
Note that it assumes a "short" contains 16 bits and an "int" contains at least 32 bits. Where it would matter if more than 32 bits were in an "int" or "long," __uint32_t is used instead π¦
/* 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.
*/
/* ***************** */
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 debug;
u_char unit;
u_short mru;
u_short seqno; /* # of last byte of packet */
u_int maxmaxcode; /* largest valid code */
u_int max_ent; /* largest code in use */
u_int in_count; /* uncompressed bytes */
u_int bytes_out; /* compressed bytes */
u_int ratio; /* recent compression ratio */
u_int checkpoint; /* when to next check ratio */
int clear_count; /* times dictionary cleared */
int incomp_count; /* incompressible packets */
int decomp_count; /* packets decompressed */
int overshoot; /* excess decompression buf */
int undershoot; /* insufficient decomp. buf */
u_short *lens; /* array of lengths of codes */
struct bsd_dict {
union { /* hash value */
__uint32_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 */
} dict[1];
};
#define BSD_OVHD (2+2) /* overhead/packet */
#define MIN_BSD_BITS 9
#define MAX_BSD_BITS 15 /* implementation limit */
#define BSD_VERS 1 /* when shifted */
#ifdef _KERNEL
extern struct bsd_db *pf_bsd_init(struct bsd_db*, int, int, int);
extern int pf_bsd_comp(struct bsd_db*,u_char*,int,struct mbuf*,int);
extern mblk_t* pf_bsd_decomp(struct bsd_db*, mblk_t*);
extern void pf_bsd_incomp(struct bsd_db*, mblk_t*, u_int);
#endif
/* ***************** */
/* 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.
*/
/*
* 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 BSD_INIT_BITS MIN_BSD_BITS
#define MAXCODE(b) ((1 << (b)) - 1)
#define BADCODEM1 MAXCODE(MAX_BSD_BITS);
#define BSD_HASH(prefix,suffix,hshift) ((((__uint32_t)(suffix)) \
<< (hshift)) \
^ (__uint32_t)(prefix))
#define BSD_KEY(prefix,suffix) ((((__uint32_t)(suffix)) << 16) \
+ (__uint32_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
pf_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->incomp_count = 0;
db->decomp_count = 0;
db->overshoot = 0;
db->undershoot = 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 */
pf_bsd_check(struct bsd_db *db)
{
register 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) {
pf_bsd_clear(db);
return 1;
}
db->ratio = new_ratio;
}
}
return 0;
}
/* Initialize the database.
*/
struct bsd_db *
pf_bsd_init(struct bsd_db *db, /* initialize this database */
int unit, /* for debugging */
int bits, /* size of LZW code word */
int mru) /* MRU for input, 0 for output*/
We're picking up right where the init function's body starts, so let's go π
pf_bsd_init() body π
{
register int i;
register u_short *lens;
register u_int newlen, hsize, hshift, maxmaxcode;
switch (bits) {
case 9: /* needs 82152 for both comp &*/
case 10: /* needs 84144 decomp*/
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 */
/* hshift = 8; */ /* cptr in struct bsd_db */
/* break; */
default:
if (db) {
if (db->lens)
kern_free(db->lens);
kern_free(db);
}
return 0;
}
maxmaxcode = MAXCODE(bits);
newlen = sizeof(*db) + (hsize-1)*(sizeof(db->dict[0]));
if (db) {
lens = db->lens;
if (db->totlen != newlen) {
if (lens)
kern_free(lens);
kern_free(db);
db = 0;
}
}
if (!db) {
db = (struct bsd_db*)kern_malloc(newlen);
if (!db)
return 0;
if (mru == 0) {
lens = 0;
} else {
lens = (u_short*)kern_malloc((maxmaxcode+1)
* sizeof(*lens));
if (!lens) {
kern_free(db);
return 0;
}
i = LAST+1;
while (i != 0)
lens[--i] = 1;
}
i = hsize;
while (i != 0) {
db->dict[--i].codem1 = BADCODEM1;
db->dict[i].cptr = 0;
}
}
bzero(db,sizeof(*db)-sizeof(db->dict));
db->lens = lens;
db->unit = unit;
db->mru = mru;
db->hsize = hsize;
db->hshift = hshift;
db->maxmaxcode = maxmaxcode;
db->clear_count = -1;
pf_bsd_clear(db);
return db;
}
Okay so this is the setup crew π§ The switch picks a hash table size and shift based on how many bits you asked for, and anything outside 9 through 15 hits the default: and bails with 0. ngl the case 16 comment block is giving "I was going to, but no" energy π It's literally commented out because that table would be too big for cptr in struct bsd_db.
Then it either reuses the existing db or frees it and mallocs a fresh one, sets up the lens array (only if mru isn't zero) with every entry set to 1, and marks every dictionary slot as BADCODEM1 so nothing looks valid yet. Main character energy for a clean slate β¨
pf_bsd_comp() π¦
/* compress a packet
* Assume the protocol is known to be >= 0x21 and < 0xff.
* 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 */
pf_bsd_comp(struct bsd_db *db,
u_char *cp_buf, /* compress into here */
int proto, /* this original PPP protocol */
struct mbuf *m, /* from here */
int slen)
{
register int hshift = db->hshift;
register u_int max_ent = db->max_ent;
register u_int n_bits = db->n_bits;
register u_int bitno = 32;
register __uint32_t accum = 0;
register struct bsd_dict *dictp;
register __uint32_t fcode;
register u_char c;
register int hval, disp, ent;
register u_char *rptr, *wptr;
struct mbuf *n;
#define OUTPUT(ent) { \
bitno -= n_bits; \
accum |= ((ent) << bitno); \
do { \
*wptr++ = accum>>24; \
accum <<= 8; \
bitno += 8; \
} while (bitno <= 24); \
}
/* start with the protocol byte */
ent = proto;
db->in_count++;
/* install sequence number */
cp_buf[0] = db->seqno>>8;
cp_buf[1] = db->seqno;
db->seqno++;
wptr = &cp_buf[2];
slen = m->m_len;
db->in_count += slen;
rptr = mtod(m, u_char*);
n = m->m_next;
for (;;) {
if (slen == 0) {
if (!n)
break;
slen = n->m_len;
rptr = mtod(n, u_char*);
n = n->m_next;
if (!slen)
continue; /* handle 0-length buffers*/
db->in_count += slen;
}
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; /* found (prefix,suffix) */
continue;
nomatch:
OUTPUT(ent); /* output the prefix */
/* 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 old hash table entry using
* 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;
}
ent = c;
}
OUTPUT(ent); /* output the last code */
db->bytes_out += (wptr-&cp_buf[2] /* count complete bytes */
+ (32-bitno+7)/8);
if (pf_bsd_check(db))
OUTPUT(CLEAR); /* do not count the CLEAR */
/* Pad dribble bits of last code with ones.
* Do not emit a completely useless byte of ones.
*/
if (bitno != 32)
*wptr++ = (accum | (0xff << (bitno-8))) >> 24;
/* 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++;
return (wptr - cp_buf);
#undef OUTPUT
}
Okay this one is the big compressor boss π It starts by stamping a two-byte sequence number onto the front of the output buffer, then walks every byte of the mbuf chain (skipping zero-length buffers like a pro π ββοΈ). For each byte it hashes the (prefix, suffix) pair and probes the table until it finds a match or hits an invalid slot. When it misses, it outputs the current code, adds the new pair to the dictionary, and starts over from the byte it just read. π
The OUTPUT macro is doing the bit-packing magic, shoving codes into accum and flushing whole bytes out. At the end it pads any leftover bits with ones, but it won't emit a byte that's entirely ones. Iconic, honestly πΈ
pf_bsd_incomp() π
/* Update the "BSD Compress" dictionary on the receiver for
* incompressible data by pretending to compress the incoming data.
*/
void
pf_bsd_incomp(struct bsd_db *db,
mblk_t *dmsg,
u_int ent) /* start with protocol byte */
{
register u_int hshift = db->hshift;
register u_int max_ent = db->max_ent;
register u_int n_bits = db->n_bits;
register struct bsd_dict *dictp;
register __uint32_t fcode;
register u_char c;
register int hval, disp;
register int slen;
register u_int bitno = 7;
register u_char *rptr;
db->incomp_count++;
db->in_count++; /* count protocol as 1 byte */
db->seqno++;
rptr = dmsg->b_rptr+PPP_BUF_HEAD_INFO;
for (;;) {
slen = dmsg->b_wptr - rptr;
if (slen == 0) {
dmsg = dmsg->b_cont;
if (!dmsg)
break;
rptr = dmsg->b_rptr;
continue; /* skip zero-length buffers */
}
db->in_count += slen;
do {
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 until 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; /* 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;
} while (--slen != 0);
}
bitno += n_bits; /* output (count) last code */
db->bytes_out += bitno/8;
(void)pf_bsd_check(db);
/* 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++;
}
This is the sneaky twin π¦ When the data is too spicy to compress, the receiver still has to keep its dictionary in sync with the sender, so pf_bsd_incomp() pretends to compress the incoming packet. It doesn't write any output. It just counts the bits (bitno) and bytes (bytes_out) and builds the same dictionary entries the compressor would have built. Honestly so iconic that it does all the work and nobody sees it π
pf_bsd_decomp() π
/* Decompress "BSD Compress"
*/
mblk_t* /* 0=failed, so zap CCP */
pf_bsd_decomp(struct bsd_db *db,
mblk_t *cmsg)
And that's where we're leaving things, bestie, right at the decompressor's door πͺβ¨ The header says it hands back 0 on failure, which means the CCP gets zapped, so the function body is about to get very interesting π
{
register u_int max_ent = db->max_ent;
register __uint32_t accum = 0;
register u_int bitno = 32; /* 1st valid bit in accum */
register u_int n_bits = db->n_bits;
register u_int tgtbitno = 32-n_bits; /* bitno when accum full */
register struct bsd_dict *dictp;
register int explen, i;
register u_int incode, oldcode, finchar;
register u_char *p, *rptr, *rptr9, *wptr0, *wptr;
mblk_t *dmsg, *dmsg1, *bp;
db->decomp_count++;
rptr = cmsg->b_rptr;
ASSERT(cmsg->b_wptr >= rptr+PPP_BUF_MIN);
ASSERT(PPP_BUF_ALIGN(rptr));
rptr += PPP_BUF_MIN;
/* get the sequence number */
i = 0;
explen = 2;
do {
while (rptr >= cmsg->b_wptr) {
bp = cmsg;
cmsg = cmsg->b_cont;
freeb(bp);
if (!cmsg) {
if (db->debug)
printf("bsd_decomp%d: missing"
" %d header bytes\n",
db->unit, explen);
return 0;
}
rptr = cmsg->b_rptr;
}
i = (i << 8) + *rptr++;
} while (--explen != 0);
if (i != db->seqno++) {
freemsg(cmsg);
if (db->debug)
printf("bsd_decomp%d: bad sequence number 0x%x"
" instead of 0x%x\n",
db->unit, i, db->seqno-1);
return 0;
}
/* Guess how much memory we will need. Assume this packet was
* compressed by at least 1.5X regardless of the recent ratio.
*/
if (db->ratio > (RATIO_SCALE*3)/2)
explen = (msgdsize(cmsg)*db->ratio)/RATIO_SCALE;
else
explen = (msgdsize(cmsg)*3)/2;
if (explen > db->mru)
explen = db->mru;
dmsg = dmsg1 = allocb(explen+PPP_BUF_HEAD_INFO, BPRI_HI);
if (!dmsg1) {
freemsg(cmsg);
return 0;
}
wptr = dmsg1->b_wptr;
((struct ppp_buf*)wptr)->type = BEEP_FRAME;
/* the protocol field must be compressed */
((struct ppp_buf*)wptr)->proto = 0;
wptr += PPP_BUF_HEAD_PROTO+1;
rptr9 = cmsg->b_wptr;
db->bytes_out += rptr9-rptr;
wptr0 = wptr;
explen = dmsg1->b_datap->db_lim - wptr;
oldcode = CLEAR;
for (;;) {
if (rptr >= rptr9) {
bp = cmsg;
cmsg = cmsg->b_cont;
freeb(bp);
if (!cmsg) /* quit at end of message */
break;
rptr = cmsg->b_rptr;
rptr9 = cmsg->b_wptr;
db->bytes_out += rptr9-rptr;
continue; /* handle 0-length buffers */
}
/* Accumulate bytes until we have a complete code.
* Then get the next code, relying on the 32-bit,
* unsigned accum to mask the result.
*/
bitno -= 8;
accum |= *rptr++ << bitno;
if (tgtbitno < bitno)
continue;
incode = accum >> tgtbitno;
accum <<= 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 (rptr != rptr9
|| cmsg->b_cont != 0) {
cmsg->b_rptr = rptr;
i = msgdsize(cmsg);
if (i != 0) {
freemsg(dmsg);
freemsg(cmsg);
if (db->debug)
printf("bsd_decomp%d: "
"bad CLEAR\n",
db->unit);
return 0;
}
}
pf_bsd_clear(db);
freemsg(cmsg);
wptr0 = wptr;
break;
}
/* Special case for KwKwK string. */
if (incode > max_ent) {
if (incode > max_ent+2
|| incode > db->maxmaxcode
|| oldcode == CLEAR) {
freemsg(dmsg);
freemsg(cmsg);
if (db->debug)
printf("bsd_decomp%d: bad code %x\n",
db->unit, incode);
return 0;
}
i = db->lens[oldcode];
/* do not write past end of buf */
explen -= i+1;
if (explen < 0) {
db->undershoot -= explen;
db->in_count += wptr-wptr0;
dmsg1->b_wptr = wptr;
CK_WPTR(dmsg1);
explen = MAX(64,i+1);
bp = allocb(explen, BPRI_HI);
if (!bp) {
freemsg(cmsg);
freemsg(dmsg);
return 0;
}
dmsg1->b_cont = bp;
dmsg1 = bp;
wptr0 = wptr = dmsg1->b_wptr;
explen=dmsg1->b_datap->db_lim-wptr-(i+1);
}
p = (wptr += i);
*wptr++ = finchar;
finchar = oldcode;
} else {
i = db->lens[finchar = incode];
explen -= i;
if (explen < 0) {
db->undershoot -= explen;
db->in_count += wptr-wptr0;
dmsg1->b_wptr = wptr;
CK_WPTR(dmsg1);
explen = MAX(64,i);
bp = allocb(explen, BPRI_HI);
if (!bp) {
freemsg(dmsg);
freemsg(cmsg);
return 0;
}
dmsg1->b_cont = bp;
dmsg1 = bp;
wptr0 = wptr = dmsg1->b_wptr;
explen = dmsg1->b_datap->db_lim-wptr-i;
}
p = (wptr += i);
}
/* decode code and install in decompressed buffer */
while (finchar > LAST) {
dictp = &db->dict[db->dict[finchar].cptr];
*--p = dictp->f.hs.suffix;
finchar = dictp->f.hs.prefix;
}
*--p = 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;
__uint32_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;
}
db->in_count += wptr-wptr0;
dmsg1->b_wptr = wptr;
CK_WPTR(dmsg1);
db->overshoot += explen;
/* Keep the checkpoint right so that incompressible packets
* clear the dictionary at the right times.
*/
if (pf_bsd_check(db)
&& db->debug) {
printf("bsd_decomp%d: peer should have "
"cleared dictionary\n", db->unit);
}
return dmsg;
}
Security Considerations π
Okay bestie, the security section is giving very short and sweet π₯Ί Security issues are not discussed in this memo. So don't go looking for a security vibe check in here, because this doc just doesn't cover it. π
References π
[1] Simpson, W., "The Point-to-Point Protocol (PPP)", STD 51, RFC 1661, July 1994.
[2] Rand, D., "The PPP Compression Control Protocol (CCP)", RFC 1962, June 1996.
[3] Simpson, W., "PPP LCP Extensions", RFC 1570, January 1994.
[4] Simpson, W., "PPP in HDLC-like Framing", STD 51, RFC 1662, July 1994.
Acknowledgments π«Ά
Major shoutout to William Simpson π for providing and backing the super valuable idea of not using any additional header bytes for incompressible packets. That is iconic behaviour, honestly πβ¨
Chair's Address π
If you need to reach the working group, the current chair is your girl:
Karl Fox
Ascend Communications
3518 Riverside Drive, Suite 101
Columbus, Ohio 43221
EMail: karl@ascend.com
Author's Address π
Got questions about this memo? You can also hit up: π¦
Vernon Schryver
2482 Lee Hill Drive
Boulder, Colorado 80302
EMail: vjs@rhyolite.com
π¦ where this page comes from
- ietf: rfc1977 1996-08
- π girlified by claude-haiku-5-5 on : a language model's rewrite of the original, under its terms