pred.c revision 46686
131921Sbrian/*- 231921Sbrian * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org> 331921Sbrian * Ian Donaldson <iand@labtam.labtam.oz.au> 431921Sbrian * Carsten Bormann <cabo@cs.tu-berlin.de> 531921Sbrian * Dave Rand <dlr@bungi.com>/<dave_rand@novell.com> 631921Sbrian * All rights reserved. 730715Sbrian * 831921Sbrian * Redistribution and use in source and binary forms, with or without 931921Sbrian * modification, are permitted provided that the following conditions 1031921Sbrian * are met: 1131921Sbrian * 1. Redistributions of source code must retain the above copyright 1231921Sbrian * notice, this list of conditions and the following disclaimer. 1331921Sbrian * 2. Redistributions in binary form must reproduce the above copyright 1431921Sbrian * notice, this list of conditions and the following disclaimer in the 1531921Sbrian * documentation and/or other materials provided with the distribution. 1630715Sbrian * 1731921Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1831921Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1931921Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2031921Sbrian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2131921Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2231921Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2331921Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2431921Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2531921Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2631921Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2731921Sbrian * SUCH DAMAGE. 2831921Sbrian * 2946686Sbrian * $Id: pred.c,v 1.24 1999/03/16 01:24:23 brian Exp $ 306059Samurai */ 316059Samurai 3236285Sbrian#include <sys/types.h> 3330715Sbrian 3431514Sbrian#include <stdlib.h> 3530715Sbrian#include <string.h> 3646686Sbrian#include <termios.h> 3730715Sbrian 3838174Sbrian#include "defs.h" 3946686Sbrian#include "layer.h" 4030715Sbrian#include "mbuf.h" 4130715Sbrian#include "log.h" 4230715Sbrian#include "timer.h" 4330715Sbrian#include "fsm.h" 4436285Sbrian#include "lqr.h" 4530715Sbrian#include "hdlc.h" 4631514Sbrian#include "lcp.h" 4730715Sbrian#include "ccp.h" 4844650Sbrian#include "throughput.h" 4944650Sbrian#include "link.h" 5030715Sbrian#include "pred.h" 5130715Sbrian 526059Samurai/* The following hash code is the heart of the algorithm: 536059Samurai * It builds a sliding hash sum of the previous 3-and-a-bit characters 546059Samurai * which will be used to index the guess table. 556059Samurai * A better hash function would result in additional compression, 566059Samurai * at the expense of time. 576059Samurai */ 5836285Sbrian#define HASH(state, x) state->hash = (state->hash << 4) ^ (x) 5931514Sbrian#define GUESS_TABLE_SIZE 65536 606059Samurai 6136285Sbrianstruct pred1_state { 6236285Sbrian u_short hash; 6336285Sbrian u_char dict[GUESS_TABLE_SIZE]; 6436285Sbrian}; 656059Samurai 666059Samuraistatic int 6736285Sbriancompress(struct pred1_state *state, u_char *source, u_char *dest, int len) 686059Samurai{ 6928679Sbrian int i, bitmask; 7028679Sbrian unsigned char *flagdest, flags, *orgdest; 716059Samurai 7228679Sbrian orgdest = dest; 7328679Sbrian while (len) { 7428679Sbrian flagdest = dest++; 7528679Sbrian flags = 0; /* All guess wrong initially */ 7628679Sbrian for (bitmask = 1, i = 0; i < 8 && len; i++, bitmask <<= 1) { 7736285Sbrian if (state->dict[state->hash] == *source) { 7828679Sbrian flags |= bitmask; /* Guess was right - don't output */ 7928679Sbrian } else { 8036285Sbrian state->dict[state->hash] = *source; 8128679Sbrian *dest++ = *source; /* Guess wrong, output char */ 8228679Sbrian } 8336285Sbrian HASH(state, *source++); 8428679Sbrian len--; 856059Samurai } 8628679Sbrian *flagdest = flags; 8728679Sbrian } 8828679Sbrian return (dest - orgdest); 896059Samurai} 906059Samurai 916059Samuraistatic void 9244792SbrianSyncTable(struct pred1_state *state, u_char *source, u_char *dest, int len) 936059Samurai{ 9428679Sbrian while (len--) { 9544792Sbrian *dest++ = state->dict[state->hash] = *source; 9644792Sbrian HASH(state, *source++); 9728679Sbrian } 986059Samurai} 996059Samurai 1006059Samuraistatic int 10144792Sbriandecompress(struct pred1_state *state, u_char *source, u_char *dest, int len) 1026059Samurai{ 10328679Sbrian int i, bitmask; 10428679Sbrian unsigned char flags, *orgdest; 1056059Samurai 10628679Sbrian orgdest = dest; 10728679Sbrian while (len) { 10828679Sbrian flags = *source++; 10928679Sbrian len--; 11028679Sbrian for (i = 0, bitmask = 1; i < 8; i++, bitmask <<= 1) { 11128679Sbrian if (flags & bitmask) { 11236285Sbrian *dest = state->dict[state->hash]; /* Guess correct */ 11328679Sbrian } else { 11428679Sbrian if (!len) 11528679Sbrian break; /* we seem to be really done -- cabo */ 11636285Sbrian state->dict[state->hash] = *source; /* Guess wrong */ 11728679Sbrian *dest = *source++; /* Read from source */ 11828679Sbrian len--; 11928679Sbrian } 12036285Sbrian HASH(state, *dest++); 1216059Samurai } 12228679Sbrian } 12328679Sbrian return (dest - orgdest); 1246059Samurai} 1256059Samurai 12631514Sbrianstatic void 12736285SbrianPred1Term(void *v) 1286059Samurai{ 12936285Sbrian struct pred1_state *state = (struct pred1_state *)v; 13036285Sbrian free(state); 13131514Sbrian} 13231514Sbrian 13331514Sbrianstatic void 13436285SbrianPred1ResetInput(void *v) 13531514Sbrian{ 13636285Sbrian struct pred1_state *state = (struct pred1_state *)v; 13736285Sbrian state->hash = 0; 13836285Sbrian memset(state->dict, '\0', sizeof state->dict); 13936285Sbrian log_Printf(LogCCP, "Predictor1: Input channel reset\n"); 1406059Samurai} 1416059Samurai 14231514Sbrianstatic void 14336285SbrianPred1ResetOutput(void *v) 14431514Sbrian{ 14536285Sbrian struct pred1_state *state = (struct pred1_state *)v; 14636285Sbrian state->hash = 0; 14736285Sbrian memset(state->dict, '\0', sizeof state->dict); 14836285Sbrian log_Printf(LogCCP, "Predictor1: Output channel reset\n"); 14931514Sbrian} 15031514Sbrian 15136285Sbrianstatic void * 15236285SbrianPred1InitInput(struct lcp_opt *o) 15331514Sbrian{ 15436285Sbrian struct pred1_state *state; 15536285Sbrian state = (struct pred1_state *)malloc(sizeof(struct pred1_state)); 15636285Sbrian if (state != NULL) 15736285Sbrian Pred1ResetInput(state); 15836285Sbrian return state; 15931514Sbrian} 16031514Sbrian 16136285Sbrianstatic void * 16236285SbrianPred1InitOutput(struct lcp_opt *o) 16331514Sbrian{ 16436285Sbrian struct pred1_state *state; 16536285Sbrian state = (struct pred1_state *)malloc(sizeof(struct pred1_state)); 16636285Sbrian if (state != NULL) 16736285Sbrian Pred1ResetOutput(state); 16836285Sbrian return state; 16931514Sbrian} 17031514Sbrian 17146686Sbrianstatic struct mbuf * 17246686SbrianPred1Output(void *v, struct ccp *ccp, struct link *l, int pri, u_short *proto, 17336285Sbrian struct mbuf *bp) 17431514Sbrian{ 17536285Sbrian struct pred1_state *state = (struct pred1_state *)v; 1766059Samurai struct mbuf *mwp; 1776059Samurai u_char *cp, *wp, *hp; 1786059Samurai int orglen, len; 17928679Sbrian u_char bufp[MAX_MTU + 2]; 1806059Samurai u_short fcs; 1816059Samurai 18236285Sbrian orglen = mbuf_Length(bp) + 2; /* add count of proto */ 18336285Sbrian mwp = mbuf_Alloc((orglen + 2) / 8 * 9 + 12, MB_HDLCOUT); 1846059Samurai hp = wp = MBUF_CTOP(mwp); 1856059Samurai cp = bufp; 1866059Samurai *wp++ = *cp++ = orglen >> 8; 1876059Samurai *wp++ = *cp++ = orglen & 0377; 18846686Sbrian *cp++ = *proto >> 8; 18946686Sbrian *cp++ = *proto & 0377; 19036285Sbrian mbuf_Read(bp, cp, orglen - 2); 19146686Sbrian fcs = hdlc_Fcs(bufp, 2 + orglen); 1926059Samurai fcs = ~fcs; 1936059Samurai 19436285Sbrian len = compress(state, bufp + 2, wp, orglen); 19536285Sbrian log_Printf(LogDEBUG, "Pred1Output: orglen (%d) --> len (%d)\n", orglen, len); 19636285Sbrian ccp->uncompout += orglen; 1976059Samurai if (len < orglen) { 1986059Samurai *hp |= 0x80; 1996059Samurai wp += len; 20036285Sbrian ccp->compout += len; 2016059Samurai } else { 20230715Sbrian memcpy(wp, bufp + 2, orglen); 2036059Samurai wp += orglen; 20436285Sbrian ccp->compout += orglen; 2056059Samurai } 2066059Samurai 2076059Samurai *wp++ = fcs & 0377; 2086059Samurai *wp++ = fcs >> 8; 2096059Samurai mwp->cnt = wp - MBUF_CTOP(mwp); 21046686Sbrian *proto = ccp_Proto(ccp); 21146686Sbrian return mwp; 2126059Samurai} 2136059Samurai 21431514Sbrianstatic struct mbuf * 21536285SbrianPred1Input(void *v, struct ccp *ccp, u_short *proto, struct mbuf *bp) 2166059Samurai{ 21736285Sbrian struct pred1_state *state = (struct pred1_state *)v; 2186059Samurai u_char *cp, *pp; 2196059Samurai int len, olen, len1; 2206059Samurai struct mbuf *wp; 2216059Samurai u_char *bufp; 22231514Sbrian u_short fcs; 2236059Samurai 22444792Sbrian wp = mbuf_Alloc(MAX_MRU + 2, MB_IPIN); 2256059Samurai cp = MBUF_CTOP(bp); 22636285Sbrian olen = mbuf_Length(bp); 2276059Samurai pp = bufp = MBUF_CTOP(wp); 2286059Samurai *pp++ = *cp & 0177; 2296059Samurai len = *cp++ << 8; 2306059Samurai *pp++ = *cp; 2316059Samurai len += *cp++; 23236285Sbrian ccp->uncompin += len & 0x7fff; 2336059Samurai if (len & 0x8000) { 23436285Sbrian len1 = decompress(state, cp, pp, olen - 4); 23536285Sbrian ccp->compin += olen; 2366059Samurai len &= 0x7fff; 23728679Sbrian if (len != len1) { /* Error is detected. Send reset request */ 23844650Sbrian log_Printf(LogCCP, "Pred1: Length error (got %d, not %d)\n", len1, len); 23944650Sbrian fsm_Reopen(&ccp->fsm); 24036285Sbrian mbuf_Free(bp); 24136285Sbrian mbuf_Free(wp); 24231514Sbrian return NULL; 2436059Samurai } 2446059Samurai cp += olen - 4; 2456059Samurai pp += len1; 24644792Sbrian } else if (len + 4 != olen) { 24744792Sbrian log_Printf(LogCCP, "Pred1: Length error (got %d, not %d)\n", len + 4, olen); 24844792Sbrian fsm_Reopen(&ccp->fsm); 24944792Sbrian mbuf_Free(wp); 25044792Sbrian mbuf_Free(bp); 25144792Sbrian return NULL; 2526059Samurai } else { 25336285Sbrian ccp->compin += len; 25436285Sbrian SyncTable(state, cp, pp, len); 2556059Samurai cp += len; 2566059Samurai pp += len; 2576059Samurai } 25828679Sbrian *pp++ = *cp++; /* CRC */ 2596059Samurai *pp++ = *cp++; 26046686Sbrian fcs = hdlc_Fcs(bufp, wp->cnt = pp - bufp); 2616059Samurai if (fcs == GOODFCS) { 2626059Samurai wp->offset += 2; /* skip length */ 2636059Samurai wp->cnt -= 4; /* skip length & CRC */ 2646059Samurai pp = MBUF_CTOP(wp); 26531514Sbrian *proto = *pp++; 26631514Sbrian if (*proto & 1) { 2676059Samurai wp->offset++; 2686059Samurai wp->cnt--; 2696059Samurai } else { 2706059Samurai wp->offset += 2; 2716059Samurai wp->cnt -= 2; 27231514Sbrian *proto = (*proto << 8) | *pp++; 2736059Samurai } 27436285Sbrian mbuf_Free(bp); 27531514Sbrian return wp; 27628679Sbrian } else { 27744792Sbrian const char *pre = *MBUF_CTOP(bp) & 0x80 ? "" : "un"; 27844792Sbrian log_Printf(LogDEBUG, "Pred1Input: fcs = 0x%04x (%scompressed), len = 0x%x," 27944792Sbrian " olen = 0x%x\n", fcs, pre, len, olen); 28044792Sbrian log_Printf(LogCCP, "%s: Bad %scompressed CRC-16\n", 28144792Sbrian ccp->fsm.link->name, pre); 28244650Sbrian fsm_Reopen(&ccp->fsm); 28336285Sbrian mbuf_Free(wp); 2846059Samurai } 28536285Sbrian mbuf_Free(bp); 28631514Sbrian return NULL; 2876059Samurai} 28831514Sbrian 28931514Sbrianstatic void 29036285SbrianPred1DictSetup(void *v, struct ccp *ccp, u_short proto, struct mbuf * bp) 29131514Sbrian{ 29231514Sbrian} 29331514Sbrian 29431514Sbrianstatic const char * 29531514SbrianPred1DispOpts(struct lcp_opt *o) 29631514Sbrian{ 29731514Sbrian return NULL; 29831514Sbrian} 29931514Sbrian 30031514Sbrianstatic void 30136285SbrianPred1InitOptsOutput(struct lcp_opt *o, const struct ccp_config *cfg) 30231514Sbrian{ 30331514Sbrian o->len = 2; 30431514Sbrian} 30531514Sbrian 30631514Sbrianstatic int 30736285SbrianPred1SetOptsOutput(struct lcp_opt *o) 30831514Sbrian{ 30936285Sbrian if (o->len != 2) { 31036285Sbrian o->len = 2; 31131514Sbrian return MODE_NAK; 31231514Sbrian } 31331514Sbrian return MODE_ACK; 31431514Sbrian} 31531514Sbrian 31636285Sbrianstatic int 31736285SbrianPred1SetOptsInput(struct lcp_opt *o, const struct ccp_config *cfg) 31836285Sbrian{ 31936285Sbrian return Pred1SetOptsOutput(o); 32036285Sbrian} 32136285Sbrian 32231514Sbrianconst struct ccp_algorithm Pred1Algorithm = { 32331514Sbrian TY_PRED1, 32436285Sbrian CCP_NEG_PRED1, 32531514Sbrian Pred1DispOpts, 32631514Sbrian { 32736285Sbrian Pred1SetOptsInput, 32831514Sbrian Pred1InitInput, 32936285Sbrian Pred1Term, 33031514Sbrian Pred1ResetInput, 33131514Sbrian Pred1Input, 33231514Sbrian Pred1DictSetup 33331514Sbrian }, 33431514Sbrian { 33536285Sbrian Pred1InitOptsOutput, 33636285Sbrian Pred1SetOptsOutput, 33731514Sbrian Pred1InitOutput, 33836285Sbrian Pred1Term, 33931514Sbrian Pred1ResetOutput, 34031514Sbrian Pred1Output 34131514Sbrian }, 34231514Sbrian}; 343