2 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
43 struct deflate_state {
50 static char garbage[10];
51 static u_char EMPTY_BLOCK[4] = { 0x00, 0x00, 0xff, 0xff };
53 #define DEFLATE_CHUNK_LEN (1536 - sizeof(struct mbuf))
56 DeflateResetOutput(void *v)
58 struct deflate_state *state = (struct deflate_state *)v;
61 state->uncomp_rec = 0;
62 deflateReset(&state->cx);
63 log_Printf(LogCCP, "Deflate: Output channel reset\n");
65 return 1; /* Ask FSM to ACK */
69 DeflateOutput(void *v, struct ccp *ccp, struct link *l __unused,
70 int pri __unused, u_short *proto, struct mbuf *mp)
72 struct deflate_state *state = (struct deflate_state *)v;
74 int olen, ilen, len, res, flush;
75 struct mbuf *mo_head, *mo, *mi_head, *mi;
78 log_Printf(LogDEBUG, "DeflateOutput: Proto %02x (%d bytes)\n", *proto, ilen);
79 log_DumpBp(LogDEBUG, "DeflateOutput: Compress packet:", mp);
81 /* Stuff the protocol in front of the input */
82 mi_head = mi = m_get(2, MB_CCPOUT);
85 if (*proto < 0x100) { /* Compress the protocol */
86 rp[0] = *proto & 0377;
88 } else { /* Don't compress the protocol */
90 rp[1] = *proto & 0377;
94 /* Allocate the initial output mbuf */
95 mo_head = mo = m_get(DEFLATE_CHUNK_LEN, MB_CCPOUT);
98 *wp++ = state->seqno >> 8;
99 *wp++ = state->seqno & 0377;
100 log_Printf(LogDEBUG, "DeflateOutput: Seq %d\n", state->seqno);
103 /* Set up the deflation context */
104 state->cx.next_out = wp;
105 state->cx.avail_out = DEFLATE_CHUNK_LEN - 2;
106 state->cx.next_in = MBUF_CTOP(mi);
107 state->cx.avail_in = mi->m_len;
112 if ((res = deflate(&state->cx, flush)) != Z_OK) {
113 if (res == Z_STREAM_END)
115 log_Printf(LogWARN, "DeflateOutput: deflate returned %d (%s)\n",
116 res, state->cx.msg ? state->cx.msg : "");
120 return mp; /* Our dictionary's probably dead now :-( */
123 if (flush == Z_SYNC_FLUSH && state->cx.avail_out != 0)
126 if (state->cx.avail_in == 0 && mi->m_next != NULL) {
128 state->cx.next_in = MBUF_CTOP(mi);
129 state->cx.avail_in = mi->m_len;
130 if (mi->m_next == NULL)
131 flush = Z_SYNC_FLUSH;
134 if (state->cx.avail_out == 0) {
135 mo->m_next = m_get(DEFLATE_CHUNK_LEN, MB_CCPOUT);
136 olen += (mo->m_len = DEFLATE_CHUNK_LEN);
139 state->cx.next_out = MBUF_CTOP(mo);
140 state->cx.avail_out = DEFLATE_CHUNK_LEN;
144 olen += (mo->m_len = DEFLATE_CHUNK_LEN - state->cx.avail_out);
145 olen -= 4; /* exclude the trailing EMPTY_BLOCK */
148 * If the output packet (including seqno and excluding the EMPTY_BLOCK)
149 * got bigger, send the original.
154 log_Printf(LogDEBUG, "DeflateOutput: %d => %d: Uncompressible (0x%04x)\n",
156 ccp->uncompout += ilen;
157 ccp->compout += ilen; /* We measure this stuff too */
164 * Lose the last four bytes of our output.
165 * XXX: We should probably assert that these are the same as the
166 * contents of EMPTY_BLOCK.
169 for (len = mo->m_len; len < olen; mo = mo->m_next, len += mo->m_len)
171 mo->m_len -= len - olen;
172 if (mo->m_next != NULL) {
177 ccp->uncompout += ilen;
178 ccp->compout += olen;
180 log_Printf(LogDEBUG, "DeflateOutput: %d => %d bytes, proto 0x%04x\n",
183 *proto = ccp_Proto(ccp);
188 DeflateResetInput(void *v)
190 struct deflate_state *state = (struct deflate_state *)v;
193 state->uncomp_rec = 0;
194 inflateReset(&state->cx);
195 log_Printf(LogCCP, "Deflate: Input channel reset\n");
199 DeflateInput(void *v, struct ccp *ccp, u_short *proto, struct mbuf *mi)
201 struct deflate_state *state = (struct deflate_state *)v;
202 struct mbuf *mo, *mo_head, *mi_head;
205 int seq, flush, res, first;
208 log_DumpBp(LogDEBUG, "DeflateInput: Decompress packet:", mi);
209 mi_head = mi = mbuf_Read(mi, hdr, 2);
212 /* Check the sequence number. */
213 seq = (hdr[0] << 8) + hdr[1];
214 log_Printf(LogDEBUG, "DeflateInput: Seq %d\n", seq);
215 if (seq != state->seqno) {
216 if (seq <= state->uncomp_rec)
218 * So the peer's started at zero again - fine ! If we're wrong,
219 * inflate() will fail. This is better than getting into a loop
220 * trying to get a ResetReq to a busy sender.
224 log_Printf(LogCCP, "DeflateInput: Seq error: Got %d, expected %d\n",
227 ccp_SendResetReq(&ccp->fsm);
232 state->uncomp_rec = 0;
234 /* Allocate an output mbuf */
235 mo_head = mo = m_get(DEFLATE_CHUNK_LEN, MB_CCPIN);
237 /* Our proto starts with 0 if it's compressed */
242 * We set avail_out to 1 initially so we can look at the first
243 * byte of the output and decide whether we have a compressed
246 state->cx.next_in = MBUF_CTOP(mi);
247 state->cx.avail_in = mi->m_len;
248 state->cx.next_out = wp + 1;
249 state->cx.avail_out = 1;
252 flush = mi->m_next ? Z_NO_FLUSH : Z_SYNC_FLUSH;
257 if ((res = inflate(&state->cx, flush)) != Z_OK) {
258 if (res == Z_STREAM_END)
260 log_Printf(LogCCP, "DeflateInput: inflate returned %d (%s)\n",
261 res, state->cx.msg ? state->cx.msg : "");
264 ccp_SendResetReq(&ccp->fsm);
268 if (flush == Z_SYNC_FLUSH && state->cx.avail_out != 0)
271 if (state->cx.avail_in == 0 && mi && (mi = m_free(mi)) != NULL) {
273 state->cx.next_in = MBUF_CTOP(mi);
274 ilen += (state->cx.avail_in = mi->m_len);
275 if (mi->m_next == NULL)
276 flush = Z_SYNC_FLUSH;
279 if (state->cx.avail_out == 0) {
283 /* 2 byte proto, shuffle it back in output */
285 state->cx.next_out--;
286 state->cx.avail_out = DEFLATE_CHUNK_LEN-1;
288 state->cx.avail_out = DEFLATE_CHUNK_LEN-2;
291 olen += (mo->m_len = DEFLATE_CHUNK_LEN);
292 mo->m_next = m_get(DEFLATE_CHUNK_LEN, MB_CCPIN);
294 state->cx.next_out = MBUF_CTOP(mo);
295 state->cx.avail_out = DEFLATE_CHUNK_LEN;
304 log_Printf(LogCCP, "DeflateInput: Length error\n");
306 ccp_SendResetReq(&ccp->fsm);
310 olen += (mo->m_len = DEFLATE_CHUNK_LEN - state->cx.avail_out);
312 *proto = ((u_short)wp[0] << 8) | wp[1];
313 mo_head->m_offset += 2;
318 ccp->uncompin += olen;
320 log_Printf(LogDEBUG, "DeflateInput: %d => %d bytes, proto 0x%04x\n",
324 * Simulate an EMPTY_BLOCK so that our dictionary stays in sync.
325 * The peer will have silently removed this!
327 state->cx.next_out = garbage;
328 state->cx.avail_out = sizeof garbage;
329 state->cx.next_in = EMPTY_BLOCK;
330 state->cx.avail_in = sizeof EMPTY_BLOCK;
331 inflate(&state->cx, Z_SYNC_FLUSH);
337 DeflateDictSetup(void *v, struct ccp *ccp, u_short proto, struct mbuf *mi)
339 struct deflate_state *state = (struct deflate_state *)v;
340 int res, flush, expect_error;
342 struct mbuf *mi_head;
345 log_Printf(LogDEBUG, "DeflateDictSetup: Got seq %d\n", state->seqno);
348 * Stuff an ``uncompressed data'' block header followed by the
349 * protocol in front of the input
351 mi_head = m_get(7, MB_CCPOUT);
352 mi_head->m_next = mi;
356 if (proto < 0x100) { /* Compress the protocol */
357 rp[5] = proto & 0377;
360 } else { /* Don't compress the protocol */
362 rp[6] = proto & 0377;
366 rp[0] = 0x80; /* BITS: 100xxxxx */
367 rp[1] = len & 0377; /* The length */
369 rp[3] = (~len) & 0377; /* One's compliment of the length */
372 state->cx.next_in = rp;
373 state->cx.avail_in = mi->m_len;
374 state->cx.next_out = garbage;
375 state->cx.avail_out = sizeof garbage;
380 if ((res = inflate(&state->cx, flush)) != Z_OK) {
381 if (res == Z_STREAM_END)
383 if (expect_error && res == Z_BUF_ERROR)
385 log_Printf(LogCCP, "DeflateDictSetup: inflate returned %d (%s)\n",
386 res, state->cx.msg ? state->cx.msg : "");
387 log_Printf(LogCCP, "DeflateDictSetup: avail_in %d, avail_out %d\n",
388 state->cx.avail_in, state->cx.avail_out);
389 ccp_SendResetReq(&ccp->fsm);
390 m_free(mi_head); /* lose our allocated ``head'' buf */
394 if (flush == Z_SYNC_FLUSH && state->cx.avail_out != 0)
397 if (state->cx.avail_in == 0 && mi && (mi = mi->m_next) != NULL) {
399 state->cx.next_in = MBUF_CTOP(mi);
400 state->cx.avail_in = mi->m_len;
401 if (mi->m_next == NULL)
402 flush = Z_SYNC_FLUSH;
405 if (state->cx.avail_out == 0) {
406 if (state->cx.avail_in == 0)
408 * This seems to be a bug in libz ! If inflate() finished
409 * with 0 avail_in and 0 avail_out *and* this is the end of
410 * our input *and* inflate() *has* actually written all the
411 * output it's going to, it *doesn't* return Z_STREAM_END !
412 * When we subsequently call it with no more input, it gives
413 * us Z_BUF_ERROR :-( It seems pretty safe to ignore this
414 * error (the dictionary seems to stay in sync). In the worst
415 * case, we'll drop the next compressed packet and do a
420 state->cx.next_out = garbage;
421 state->cx.avail_out = sizeof garbage;
426 ccp->uncompin += len;
430 m_free(mi_head); /* lose our allocated ``head'' buf */
434 DeflateDispOpts(struct fsm_opt *o)
436 static char disp[7]; /* Must be used immediately */
438 sprintf(disp, "win %d", (o->data[0]>>4) + 8);
443 DeflateInitOptsOutput(struct bundle *bundle __unused, struct fsm_opt *o,
444 const struct ccp_config *cfg)
447 o->data[0] = ((cfg->deflate.out.winsize - 8) << 4) + 8;
452 DeflateSetOptsOutput(struct bundle *bundle __unused, struct fsm_opt *o,
453 const struct ccp_config *cfg __unused)
455 if (o->hdr.len != 4 || (o->data[0] & 15) != 8 || o->data[1] != '\0')
458 if ((o->data[0] >> 4) + 8 > 15) {
459 o->data[0] = ((15 - 8) << 4) + 8;
467 DeflateSetOptsInput(struct bundle *bundle __unused, struct fsm_opt *o,
468 const struct ccp_config *cfg)
472 if (o->hdr.len != 4 || (o->data[0] & 15) != 8 || o->data[1] != '\0')
475 want = (o->data[0] >> 4) + 8;
476 if (cfg->deflate.in.winsize == 0) {
477 if (want < 8 || want > 15) {
478 o->data[0] = ((15 - 8) << 4) + 8;
480 } else if (want != cfg->deflate.in.winsize) {
481 o->data[0] = ((cfg->deflate.in.winsize - 8) << 4) + 8;
489 DeflateInitInput(struct bundle *bundle __unused, struct fsm_opt *o)
491 struct deflate_state *state;
493 state = (struct deflate_state *)malloc(sizeof(struct deflate_state));
495 state->winsize = (o->data[0] >> 4) + 8;
496 state->cx.zalloc = NULL;
497 state->cx.opaque = NULL;
498 state->cx.zfree = NULL;
499 state->cx.next_out = NULL;
500 if (inflateInit2(&state->cx, -state->winsize) == Z_OK)
501 DeflateResetInput(state);
512 DeflateInitOutput(struct bundle *bundle __unused, struct fsm_opt *o)
514 struct deflate_state *state;
516 state = (struct deflate_state *)malloc(sizeof(struct deflate_state));
518 state->winsize = (o->data[0] >> 4) + 8;
519 state->cx.zalloc = NULL;
520 state->cx.opaque = NULL;
521 state->cx.zfree = NULL;
522 state->cx.next_in = NULL;
523 if (deflateInit2(&state->cx, Z_DEFAULT_COMPRESSION, 8,
524 -state->winsize, 8, Z_DEFAULT_STRATEGY) == Z_OK)
525 DeflateResetOutput(state);
536 DeflateTermInput(void *v)
538 struct deflate_state *state = (struct deflate_state *)v;
540 inflateEnd(&state->cx);
545 DeflateTermOutput(void *v)
547 struct deflate_state *state = (struct deflate_state *)v;
549 deflateEnd(&state->cx);
553 const struct ccp_algorithm PppdDeflateAlgorithm = {
554 TY_PPPD_DEFLATE, /* Older versions of pppd expected this ``type'' */
569 DeflateInitOptsOutput,
570 DeflateSetOptsOutput,
578 const struct ccp_algorithm DeflateAlgorithm = {
579 TY_DEFLATE, /* rfc 1979 */
594 DeflateInitOptsOutput,
595 DeflateSetOptsOutput,