2 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
3 * Ian Donaldson <iand@labtam.labtam.oz.au>
4 * Carsten Bormann <cabo@cs.tu-berlin.de>
5 * Dave Rand <dlr@bungi.com>/<dave_rand@novell.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/types.h>
48 #include "throughput.h"
52 /* The following hash code is the heart of the algorithm:
53 * It builds a sliding hash sum of the previous 3-and-a-bit characters
54 * which will be used to index the guess table.
55 * A better hash function would result in additional compression,
56 * at the expense of time.
58 #define HASH(state, x) state->hash = (state->hash << 4) ^ (x)
59 #define GUESS_TABLE_SIZE 65536
63 u_char dict[GUESS_TABLE_SIZE];
67 compress(struct pred1_state *state, u_char *source, u_char *dest, int len)
70 unsigned char *flagdest, flags, *orgdest;
75 flags = 0; /* All guess wrong initially */
76 for (bitmask = 1, i = 0; i < 8 && len; i++, bitmask <<= 1) {
77 if (state->dict[state->hash] == *source) {
78 flags |= bitmask; /* Guess was right - don't output */
80 state->dict[state->hash] = *source;
81 *dest++ = *source; /* Guess wrong, output char */
83 HASH(state, *source++);
88 return (dest - orgdest);
92 SyncTable(struct pred1_state *state, u_char *source, u_char *dest, int len)
95 *dest++ = state->dict[state->hash] = *source;
96 HASH(state, *source++);
101 decompress(struct pred1_state *state, u_char *source, u_char *dest, int len)
104 unsigned char flags, *orgdest;
110 for (i = 0, bitmask = 1; i < 8; i++, bitmask <<= 1) {
111 if (flags & bitmask) {
112 *dest = state->dict[state->hash]; /* Guess correct */
115 break; /* we seem to be really done -- cabo */
116 state->dict[state->hash] = *source; /* Guess wrong */
117 *dest = *source++; /* Read from source */
120 HASH(state, *dest++);
123 return (dest - orgdest);
129 struct pred1_state *state = (struct pred1_state *)v;
134 Pred1ResetInput(void *v)
136 struct pred1_state *state = (struct pred1_state *)v;
138 memset(state->dict, '\0', sizeof state->dict);
139 log_Printf(LogCCP, "Predictor1: Input channel reset\n");
143 Pred1ResetOutput(void *v)
145 struct pred1_state *state = (struct pred1_state *)v;
147 memset(state->dict, '\0', sizeof state->dict);
148 log_Printf(LogCCP, "Predictor1: Output channel reset\n");
150 return 1; /* Ask FSM to ACK */
154 Pred1InitInput(struct bundle *bundle __unused, struct fsm_opt *o __unused)
156 struct pred1_state *state;
157 state = (struct pred1_state *)malloc(sizeof(struct pred1_state));
159 Pred1ResetInput(state);
164 Pred1InitOutput(struct bundle *bundle __unused, struct fsm_opt *o __unused)
166 struct pred1_state *state;
167 state = (struct pred1_state *)malloc(sizeof(struct pred1_state));
169 Pred1ResetOutput(state);
174 Pred1Output(void *v, struct ccp *ccp, struct link *l __unused,
175 int pri __unused, u_short *proto, struct mbuf *bp)
177 struct pred1_state *state = (struct pred1_state *)v;
179 u_char *cp, *wp, *hp;
181 u_char bufp[MAX_MTU + 2];
184 orglen = m_length(bp) + 2; /* add count of proto */
185 mwp = m_get((orglen + 2) / 8 * 9 + 12, MB_CCPOUT);
186 hp = wp = MBUF_CTOP(mwp);
188 *wp++ = *cp++ = orglen >> 8;
189 *wp++ = *cp++ = orglen & 0377;
191 *cp++ = *proto & 0377;
192 mbuf_Read(bp, cp, orglen - 2);
193 fcs = hdlc_Fcs(bufp, 2 + orglen);
196 len = compress(state, bufp + 2, wp, orglen);
197 log_Printf(LogDEBUG, "Pred1Output: orglen (%d) --> len (%d)\n", orglen, len);
198 ccp->uncompout += orglen;
204 memcpy(wp, bufp + 2, orglen);
206 ccp->compout += orglen;
211 mwp->m_len = wp - MBUF_CTOP(mwp);
212 *proto = ccp_Proto(ccp);
217 Pred1Input(void *v, struct ccp *ccp, u_short *proto, struct mbuf *bp)
219 struct pred1_state *state = (struct pred1_state *)v;
226 wp = m_get(MAX_MRU + 2, MB_CCPIN);
229 pp = bufp = MBUF_CTOP(wp);
234 ccp->uncompin += len & 0x7fff;
236 len1 = decompress(state, cp, pp, olen - 4);
239 if (len != len1) { /* Error is detected. Send reset request */
240 log_Printf(LogCCP, "Pred1: Length error (got %d, not %d)\n", len1, len);
241 fsm_Reopen(&ccp->fsm);
248 } else if (len + 4 != olen) {
249 log_Printf(LogCCP, "Pred1: Length error (got %d, not %d)\n", len + 4, olen);
250 fsm_Reopen(&ccp->fsm);
256 SyncTable(state, cp, pp, len);
260 *pp++ = *cp++; /* CRC */
262 fcs = hdlc_Fcs(bufp, wp->m_len = pp - bufp);
263 if (fcs == GOODFCS) {
264 wp->m_offset += 2; /* skip length */
265 wp->m_len -= 4; /* skip length & CRC */
274 *proto = (*proto << 8) | *pp++;
279 const char *pre = *MBUF_CTOP(bp) & 0x80 ? "" : "un";
280 log_Printf(LogDEBUG, "Pred1Input: fcs = 0x%04x (%scompressed), len = 0x%x,"
281 " olen = 0x%x\n", fcs, pre, len, olen);
282 log_Printf(LogCCP, "%s: Bad %scompressed CRC-16\n",
283 ccp->fsm.link->name, pre);
284 fsm_Reopen(&ccp->fsm);
292 Pred1DictSetup(void *v __unused, struct ccp *ccp __unused,
293 u_short proto __unused, struct mbuf *bp __unused)
295 /* Nothing to see here */
299 Pred1DispOpts(struct fsm_opt *o __unused)
305 Pred1InitOptsOutput(struct bundle *bundle __unused, struct fsm_opt *o,
306 const struct ccp_config *cfg __unused)
312 Pred1SetOpts(struct bundle *bundle __unused, struct fsm_opt *o,
313 const struct ccp_config *cfg __unused)
315 if (o->hdr.len != 2) {
322 const struct ccp_algorithm Pred1Algorithm = {