Deleted Added
full compact
pred.c (31343) pred.c (31514)
1/*
2 * pred.c -- Test program for Dave Rand's rendition of the
3 * predictor algorithm
4 * Updated by: iand@labtam.labtam.oz.au (Ian Donaldson)
5 * Updated by: Carsten Bormann <cabo@cs.tu-berlin.de>
6 * Original : Dave Rand <dlr@bungi.com>/<dave_rand@novell.com>
7 *
1/*
2 * pred.c -- Test program for Dave Rand's rendition of the
3 * predictor algorithm
4 * Updated by: iand@labtam.labtam.oz.au (Ian Donaldson)
5 * Updated by: Carsten Bormann <cabo@cs.tu-berlin.de>
6 * Original : Dave Rand <dlr@bungi.com>/<dave_rand@novell.com>
7 *
8 * $Id: pred.c,v 1.16 1997/11/09 06:22:46 brian Exp $
8 * $Id: pred.c,v 1.17 1997/11/22 03:37:44 brian Exp $
9 *
10 */
11
9 *
10 */
11
12#include <sys/types.h>
12#include <sys/param.h>
13#include <netinet/in.h>
14
15#include <stdio.h>
13#include <netinet/in.h>
14
15#include <stdio.h>
16#include <stdlib.h>
16#include <string.h>
17
18#include "command.h"
19#include "mbuf.h"
20#include "log.h"
21#include "defs.h"
17#include <string.h>
18
19#include "command.h"
20#include "mbuf.h"
21#include "log.h"
22#include "defs.h"
23#include "loadalias.h"
24#include "vars.h"
22#include "timer.h"
23#include "fsm.h"
24#include "hdlc.h"
25#include "lcpproto.h"
25#include "timer.h"
26#include "fsm.h"
27#include "hdlc.h"
28#include "lcpproto.h"
29#include "lcp.h"
26#include "ccp.h"
27#include "pred.h"
28
29/* The following hash code is the heart of the algorithm:
30 * It builds a sliding hash sum of the previous 3-and-a-bit characters
31 * which will be used to index the guess table.
32 * A better hash function would result in additional compression,
33 * at the expense of time.
34 */
35#define IHASH(x) do {iHash = (iHash << 4) ^ (x);} while(0)
36#define OHASH(x) do {oHash = (oHash << 4) ^ (x);} while(0)
30#include "ccp.h"
31#include "pred.h"
32
33/* The following hash code is the heart of the algorithm:
34 * It builds a sliding hash sum of the previous 3-and-a-bit characters
35 * which will be used to index the guess table.
36 * A better hash function would result in additional compression,
37 * at the expense of time.
38 */
39#define IHASH(x) do {iHash = (iHash << 4) ^ (x);} while(0)
40#define OHASH(x) do {oHash = (oHash << 4) ^ (x);} while(0)
41#define GUESS_TABLE_SIZE 65536
37
38static unsigned short int iHash, oHash;
42
43static unsigned short int iHash, oHash;
39static unsigned char InputGuessTable[65536];
40static unsigned char OutputGuessTable[65536];
44static unsigned char *InputGuessTable;
45static unsigned char *OutputGuessTable;
41
42static int
43compress(u_char * source, u_char * dest, int len)
44{
45 int i, bitmask;
46 unsigned char *flagdest, flags, *orgdest;
47
48 orgdest = dest;

--- 48 unchanged lines hidden (view full) ---

97 len--;
98 }
99 IHASH(*dest++);
100 }
101 }
102 return (dest - orgdest);
103}
104
46
47static int
48compress(u_char * source, u_char * dest, int len)
49{
50 int i, bitmask;
51 unsigned char *flagdest, flags, *orgdest;
52
53 orgdest = dest;

--- 48 unchanged lines hidden (view full) ---

102 len--;
103 }
104 IHASH(*dest++);
105 }
106 }
107 return (dest - orgdest);
108}
109
105void
106Pred1Init(int direction)
110static void
111Pred1TermInput(void)
107{
112{
108 if (direction & 1) { /* Input part */
109 iHash = 0;
110 memset(InputGuessTable, '\0', sizeof(InputGuessTable));
113 if (InputGuessTable != NULL) {
114 free(InputGuessTable);
115 InputGuessTable = NULL;
111 }
116 }
112 if (direction & 2) { /* Output part */
113 oHash = 0;
114 memset(OutputGuessTable, '\0', sizeof(OutputGuessTable));
117}
118
119static void
120Pred1TermOutput(void)
121{
122 if (OutputGuessTable != NULL) {
123 free(OutputGuessTable);
124 OutputGuessTable = NULL;
115 }
116}
117
125 }
126}
127
118void
128static void
129Pred1ResetInput(void)
130{
131 iHash = 0;
132 memset(InputGuessTable, '\0', GUESS_TABLE_SIZE);
133 LogPrintf(LogCCP, "Predictor1: Input channel reset\n");
134}
135
136static void
137Pred1ResetOutput(void)
138{
139 oHash = 0;
140 memset(OutputGuessTable, '\0', GUESS_TABLE_SIZE);
141 LogPrintf(LogCCP, "Predictor1: Output channel reset\n");
142}
143
144static int
145Pred1InitInput(void)
146{
147 if (InputGuessTable == NULL)
148 if ((InputGuessTable = malloc(GUESS_TABLE_SIZE)) == NULL)
149 return 0;
150 Pred1ResetInput();
151 return 1;
152}
153
154static int
155Pred1InitOutput(void)
156{
157 if (OutputGuessTable == NULL)
158 if ((OutputGuessTable = malloc(GUESS_TABLE_SIZE)) == NULL)
159 return 0;
160 Pred1ResetOutput();
161 return 1;
162}
163
164static int
119Pred1Output(int pri, u_short proto, struct mbuf * bp)
120{
121 struct mbuf *mwp;
122 u_char *cp, *wp, *hp;
123 int orglen, len;
124 u_char bufp[MAX_MTU + 2];
125 u_short fcs;
126

--- 6 unchanged lines hidden (view full) ---

133 *cp++ = proto >> 8;
134 *cp++ = proto & 0377;
135 mbread(bp, cp, orglen - 2);
136 fcs = HdlcFcs(INITFCS, bufp, 2 + orglen);
137 fcs = ~fcs;
138
139 len = compress(bufp + 2, wp, orglen);
140 LogPrintf(LogDEBUG, "Pred1Output: orglen (%d) --> len (%d)\n", orglen, len);
165Pred1Output(int pri, u_short proto, struct mbuf * bp)
166{
167 struct mbuf *mwp;
168 u_char *cp, *wp, *hp;
169 int orglen, len;
170 u_char bufp[MAX_MTU + 2];
171 u_short fcs;
172

--- 6 unchanged lines hidden (view full) ---

179 *cp++ = proto >> 8;
180 *cp++ = proto & 0377;
181 mbread(bp, cp, orglen - 2);
182 fcs = HdlcFcs(INITFCS, bufp, 2 + orglen);
183 fcs = ~fcs;
184
185 len = compress(bufp + 2, wp, orglen);
186 LogPrintf(LogDEBUG, "Pred1Output: orglen (%d) --> len (%d)\n", orglen, len);
141 CcpInfo.orgout += orglen;
187 CcpInfo.uncompout += orglen;
142 if (len < orglen) {
143 *hp |= 0x80;
144 wp += len;
145 CcpInfo.compout += len;
146 } else {
147 memcpy(wp, bufp + 2, orglen);
148 wp += orglen;
149 CcpInfo.compout += orglen;
150 }
151
152 *wp++ = fcs & 0377;
153 *wp++ = fcs >> 8;
154 mwp->cnt = wp - MBUF_CTOP(mwp);
155 HdlcOutput(PRI_NORMAL, PROTO_COMPD, mwp);
188 if (len < orglen) {
189 *hp |= 0x80;
190 wp += len;
191 CcpInfo.compout += len;
192 } else {
193 memcpy(wp, bufp + 2, orglen);
194 wp += orglen;
195 CcpInfo.compout += orglen;
196 }
197
198 *wp++ = fcs & 0377;
199 *wp++ = fcs >> 8;
200 mwp->cnt = wp - MBUF_CTOP(mwp);
201 HdlcOutput(PRI_NORMAL, PROTO_COMPD, mwp);
202 return 1;
156}
157
203}
204
158void
159Pred1Input(struct mbuf * bp)
205static struct mbuf *
206Pred1Input(u_short *proto, struct mbuf *bp)
160{
161 u_char *cp, *pp;
162 int len, olen, len1;
163 struct mbuf *wp;
164 u_char *bufp;
207{
208 u_char *cp, *pp;
209 int len, olen, len1;
210 struct mbuf *wp;
211 u_char *bufp;
165 u_short fcs, proto;
212 u_short fcs;
166
167 wp = mballoc(MAX_MTU + 2, MB_IPIN);
168 cp = MBUF_CTOP(bp);
169 olen = plength(bp);
170 pp = bufp = MBUF_CTOP(wp);
171 *pp++ = *cp & 0177;
172 len = *cp++ << 8;
173 *pp++ = *cp;
174 len += *cp++;
213
214 wp = mballoc(MAX_MTU + 2, MB_IPIN);
215 cp = MBUF_CTOP(bp);
216 olen = plength(bp);
217 pp = bufp = MBUF_CTOP(wp);
218 *pp++ = *cp & 0177;
219 len = *cp++ << 8;
220 *pp++ = *cp;
221 len += *cp++;
175 CcpInfo.orgin += len & 0x7fff;
222 CcpInfo.uncompin += len & 0x7fff;
176 if (len & 0x8000) {
177 len1 = decompress(cp, pp, olen - 4);
178 CcpInfo.compin += olen;
179 len &= 0x7fff;
180 if (len != len1) { /* Error is detected. Send reset request */
223 if (len & 0x8000) {
224 len1 = decompress(cp, pp, olen - 4);
225 CcpInfo.compin += olen;
226 len &= 0x7fff;
227 if (len != len1) { /* Error is detected. Send reset request */
181 LogPrintf(LogLCP, "%s: Length Error\n", CcpFsm.name);
228 LogPrintf(LogCCP, "Pred1: Length error\n");
182 CcpSendResetReq(&CcpFsm);
183 pfree(bp);
184 pfree(wp);
229 CcpSendResetReq(&CcpFsm);
230 pfree(bp);
231 pfree(wp);
185 return;
232 return NULL;
186 }
187 cp += olen - 4;
188 pp += len1;
189 } else {
190 CcpInfo.compin += len;
191 SyncTable(cp, pp, len);
192 cp += len;
193 pp += len;

--- 4 unchanged lines hidden (view full) ---

198 if (fcs != GOODFCS)
199 LogPrintf(LogDEBUG, "Pred1Input: fcs = 0x%04x (%s), len = 0x%x,"
200 " olen = 0x%x\n", fcs, (fcs == GOODFCS) ? "good" : "bad",
201 len, olen);
202 if (fcs == GOODFCS) {
203 wp->offset += 2; /* skip length */
204 wp->cnt -= 4; /* skip length & CRC */
205 pp = MBUF_CTOP(wp);
233 }
234 cp += olen - 4;
235 pp += len1;
236 } else {
237 CcpInfo.compin += len;
238 SyncTable(cp, pp, len);
239 cp += len;
240 pp += len;

--- 4 unchanged lines hidden (view full) ---

245 if (fcs != GOODFCS)
246 LogPrintf(LogDEBUG, "Pred1Input: fcs = 0x%04x (%s), len = 0x%x,"
247 " olen = 0x%x\n", fcs, (fcs == GOODFCS) ? "good" : "bad",
248 len, olen);
249 if (fcs == GOODFCS) {
250 wp->offset += 2; /* skip length */
251 wp->cnt -= 4; /* skip length & CRC */
252 pp = MBUF_CTOP(wp);
206 proto = *pp++;
207 if (proto & 1) {
253 *proto = *pp++;
254 if (*proto & 1) {
208 wp->offset++;
209 wp->cnt--;
210 } else {
211 wp->offset += 2;
212 wp->cnt -= 2;
255 wp->offset++;
256 wp->cnt--;
257 } else {
258 wp->offset += 2;
259 wp->cnt -= 2;
213 proto = (proto << 8) | *pp++;
260 *proto = (*proto << 8) | *pp++;
214 }
261 }
215 DecodePacket(proto, wp);
262 return wp;
216 } else {
217 LogDumpBp(LogHDLC, "Bad FCS", wp);
218 CcpSendResetReq(&CcpFsm);
219 pfree(wp);
220 }
221 pfree(bp);
263 } else {
264 LogDumpBp(LogHDLC, "Bad FCS", wp);
265 CcpSendResetReq(&CcpFsm);
266 pfree(wp);
267 }
268 pfree(bp);
269 return NULL;
222}
270}
271
272static void
273Pred1DictSetup(u_short proto, struct mbuf * bp)
274{
275}
276
277static const char *
278Pred1DispOpts(struct lcp_opt *o)
279{
280 return NULL;
281}
282
283static void
284Pred1GetOpts(struct lcp_opt *o)
285{
286 o->id = TY_PRED1;
287 o->len = 2;
288}
289
290static int
291Pred1SetOpts(struct lcp_opt *o)
292{
293 if (o->id != TY_PRED1 || o->len != 2) {
294 Pred1GetOpts(o);
295 return MODE_NAK;
296 }
297 return MODE_ACK;
298}
299
300const struct ccp_algorithm Pred1Algorithm = {
301 TY_PRED1,
302 ConfPred1,
303 Pred1DispOpts,
304 {
305 Pred1GetOpts,
306 Pred1SetOpts,
307 Pred1InitInput,
308 Pred1TermInput,
309 Pred1ResetInput,
310 Pred1Input,
311 Pred1DictSetup
312 },
313 {
314 Pred1GetOpts,
315 Pred1SetOpts,
316 Pred1InitOutput,
317 Pred1TermOutput,
318 Pred1ResetOutput,
319 Pred1Output
320 },
321};