2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/types.h>
45 struct deflate_state {
52 static char garbage[10];
53 static u_char EMPTY_BLOCK[4] = { 0x00, 0x00, 0xff, 0xff };
55 #define DEFLATE_CHUNK_LEN (1536 - sizeof(struct mbuf))
58 DeflateResetOutput(void *v)
60 struct deflate_state *state = (struct deflate_state *)v;
63 state->uncomp_rec = 0;
64 deflateReset(&state->cx);
65 log_Printf(LogCCP, "Deflate: Output channel reset\n");
67 return 1; /* Ask FSM to ACK */
71 DeflateOutput(void *v, struct ccp *ccp, struct link *l __unused,
72 int pri __unused, u_short *proto, struct mbuf *mp)
74 struct deflate_state *state = (struct deflate_state *)v;
76 int olen, ilen, len, res, flush;
77 struct mbuf *mo_head, *mo, *mi_head, *mi;
80 log_Printf(LogDEBUG, "DeflateOutput: Proto %02x (%d bytes)\n", *proto, ilen);
81 log_DumpBp(LogDEBUG, "DeflateOutput: Compress packet:", mp);
83 /* Stuff the protocol in front of the input */
84 mi_head = mi = m_get(2, MB_CCPOUT);
87 if (*proto < 0x100) { /* Compress the protocol */
88 rp[0] = *proto & 0377;
90 } else { /* Don't compress the protocol */
92 rp[1] = *proto & 0377;
96 /* Allocate the initial output mbuf */
97 mo_head = mo = m_get(DEFLATE_CHUNK_LEN, MB_CCPOUT);
100 *wp++ = state->seqno >> 8;
101 *wp++ = state->seqno & 0377;
102 log_Printf(LogDEBUG, "DeflateOutput: Seq %d\n", state->seqno);
105 /* Set up the deflation context */
106 state->cx.next_out = wp;
107 state->cx.avail_out = DEFLATE_CHUNK_LEN - 2;
108 state->cx.next_in = MBUF_CTOP(mi);
109 state->cx.avail_in = mi->m_len;
114 if ((res = deflate(&state->cx, flush)) != Z_OK) {
115 if (res == Z_STREAM_END)
117 log_Printf(LogWARN, "DeflateOutput: deflate returned %d (%s)\n",
118 res, state->cx.msg ? state->cx.msg : "");
122 return mp; /* Our dictionary's probably dead now :-( */
125 if (flush == Z_SYNC_FLUSH && state->cx.avail_out != 0)
128 if (state->cx.avail_in == 0 && mi->m_next != NULL) {
130 state->cx.next_in = MBUF_CTOP(mi);
131 state->cx.avail_in = mi->m_len;
132 if (mi->m_next == NULL)
133 flush = Z_SYNC_FLUSH;
136 if (state->cx.avail_out == 0) {
137 mo->m_next = m_get(DEFLATE_CHUNK_LEN, MB_CCPOUT);
138 olen += (mo->m_len = DEFLATE_CHUNK_LEN);
141 state->cx.next_out = MBUF_CTOP(mo);
142 state->cx.avail_out = DEFLATE_CHUNK_LEN;
146 olen += (mo->m_len = DEFLATE_CHUNK_LEN - state->cx.avail_out);
147 olen -= 4; /* exclude the trailing EMPTY_BLOCK */
150 * If the output packet (including seqno and excluding the EMPTY_BLOCK)
151 * got bigger, send the original.
156 log_Printf(LogDEBUG, "DeflateOutput: %d => %d: Uncompressible (0x%04x)\n",
158 ccp->uncompout += ilen;
159 ccp->compout += ilen; /* We measure this stuff too */
166 * Lose the last four bytes of our output.
167 * XXX: We should probably assert that these are the same as the
168 * contents of EMPTY_BLOCK.
171 for (len = mo->m_len; len < olen; mo = mo->m_next, len += mo->m_len)
173 mo->m_len -= len - olen;
174 if (mo->m_next != NULL) {
179 ccp->uncompout += ilen;
180 ccp->compout += olen;
182 log_Printf(LogDEBUG, "DeflateOutput: %d => %d bytes, proto 0x%04x\n",
185 *proto = ccp_Proto(ccp);
190 DeflateResetInput(void *v)
192 struct deflate_state *state = (struct deflate_state *)v;
195 state->uncomp_rec = 0;
196 inflateReset(&state->cx);
197 log_Printf(LogCCP, "Deflate: Input channel reset\n");
201 DeflateInput(void *v, struct ccp *ccp, u_short *proto, struct mbuf *mi)
203 struct deflate_state *state = (struct deflate_state *)v;
204 struct mbuf *mo, *mo_head, *mi_head;
207 int seq, flush, res, first;
210 log_DumpBp(LogDEBUG, "DeflateInput: Decompress packet:", mi);
211 mi_head = mi = mbuf_Read(mi, hdr, 2);
214 /* Check the sequence number. */
215 seq = (hdr[0] << 8) + hdr[1];
216 log_Printf(LogDEBUG, "DeflateInput: Seq %d\n", seq);
217 if (seq != state->seqno) {
218 if (seq <= state->uncomp_rec)
220 * So the peer's started at zero again - fine ! If we're wrong,
221 * inflate() will fail. This is better than getting into a loop
222 * trying to get a ResetReq to a busy sender.
226 log_Printf(LogCCP, "DeflateInput: Seq error: Got %d, expected %d\n",
229 ccp_SendResetReq(&ccp->fsm);
234 state->uncomp_rec = 0;
236 /* Allocate an output mbuf */
237 mo_head = mo = m_get(DEFLATE_CHUNK_LEN, MB_CCPIN);
239 /* Our proto starts with 0 if it's compressed */
244 * We set avail_out to 1 initially so we can look at the first
245 * byte of the output and decide whether we have a compressed
248 state->cx.next_in = MBUF_CTOP(mi);
249 state->cx.avail_in = mi->m_len;
250 state->cx.next_out = wp + 1;
251 state->cx.avail_out = 1;
254 flush = mi->m_next ? Z_NO_FLUSH : Z_SYNC_FLUSH;
259 if ((res = inflate(&state->cx, flush)) != Z_OK) {
260 if (res == Z_STREAM_END)
262 log_Printf(LogCCP, "DeflateInput: inflate returned %d (%s)\n",
263 res, state->cx.msg ? state->cx.msg : "");
266 ccp_SendResetReq(&ccp->fsm);
270 if (flush == Z_SYNC_FLUSH && state->cx.avail_out != 0)
273 if (state->cx.avail_in == 0 && mi && (mi = m_free(mi)) != NULL) {
275 state->cx.next_in = MBUF_CTOP(mi);
276 ilen += (state->cx.avail_in = mi->m_len);
277 if (mi->m_next == NULL)
278 flush = Z_SYNC_FLUSH;
281 if (state->cx.avail_out == 0) {
285 /* 2 byte proto, shuffle it back in output */
287 state->cx.next_out--;
288 state->cx.avail_out = DEFLATE_CHUNK_LEN-1;
290 state->cx.avail_out = DEFLATE_CHUNK_LEN-2;
293 olen += (mo->m_len = DEFLATE_CHUNK_LEN);
294 mo->m_next = m_get(DEFLATE_CHUNK_LEN, MB_CCPIN);
296 state->cx.next_out = MBUF_CTOP(mo);
297 state->cx.avail_out = DEFLATE_CHUNK_LEN;
306 log_Printf(LogCCP, "DeflateInput: Length error\n");
308 ccp_SendResetReq(&ccp->fsm);
312 olen += (mo->m_len = DEFLATE_CHUNK_LEN - state->cx.avail_out);
314 *proto = ((u_short)wp[0] << 8) | wp[1];
315 mo_head->m_offset += 2;
320 ccp->uncompin += olen;
322 log_Printf(LogDEBUG, "DeflateInput: %d => %d bytes, proto 0x%04x\n",
326 * Simulate an EMPTY_BLOCK so that our dictionary stays in sync.
327 * The peer will have silently removed this!
329 state->cx.next_out = garbage;
330 state->cx.avail_out = sizeof garbage;
331 state->cx.next_in = EMPTY_BLOCK;
332 state->cx.avail_in = sizeof EMPTY_BLOCK;
333 inflate(&state->cx, Z_SYNC_FLUSH);
339 DeflateDictSetup(void *v, struct ccp *ccp, u_short proto, struct mbuf *mi)
341 struct deflate_state *state = (struct deflate_state *)v;
342 int res, flush, expect_error;
344 struct mbuf *mi_head;
347 log_Printf(LogDEBUG, "DeflateDictSetup: Got seq %d\n", state->seqno);
350 * Stuff an ``uncompressed data'' block header followed by the
351 * protocol in front of the input
353 mi_head = m_get(7, MB_CCPOUT);
354 mi_head->m_next = mi;
358 if (proto < 0x100) { /* Compress the protocol */
359 rp[5] = proto & 0377;
362 } else { /* Don't compress the protocol */
364 rp[6] = proto & 0377;
368 rp[0] = 0x80; /* BITS: 100xxxxx */
369 rp[1] = len & 0377; /* The length */
371 rp[3] = (~len) & 0377; /* One's compliment of the length */
374 state->cx.next_in = rp;
375 state->cx.avail_in = mi->m_len;
376 state->cx.next_out = garbage;
377 state->cx.avail_out = sizeof garbage;
382 if ((res = inflate(&state->cx, flush)) != Z_OK) {
383 if (res == Z_STREAM_END)
385 if (expect_error && res == Z_BUF_ERROR)
387 log_Printf(LogCCP, "DeflateDictSetup: inflate returned %d (%s)\n",
388 res, state->cx.msg ? state->cx.msg : "");
389 log_Printf(LogCCP, "DeflateDictSetup: avail_in %d, avail_out %d\n",
390 state->cx.avail_in, state->cx.avail_out);
391 ccp_SendResetReq(&ccp->fsm);
392 m_free(mi_head); /* lose our allocated ``head'' buf */
396 if (flush == Z_SYNC_FLUSH && state->cx.avail_out != 0)
399 if (state->cx.avail_in == 0 && mi && (mi = mi->m_next) != NULL) {
401 state->cx.next_in = MBUF_CTOP(mi);
402 state->cx.avail_in = mi->m_len;
403 if (mi->m_next == NULL)
404 flush = Z_SYNC_FLUSH;
407 if (state->cx.avail_out == 0) {
408 if (state->cx.avail_in == 0)
410 * This seems to be a bug in libz ! If inflate() finished
411 * with 0 avail_in and 0 avail_out *and* this is the end of
412 * our input *and* inflate() *has* actually written all the
413 * output it's going to, it *doesn't* return Z_STREAM_END !
414 * When we subsequently call it with no more input, it gives
415 * us Z_BUF_ERROR :-( It seems pretty safe to ignore this
416 * error (the dictionary seems to stay in sync). In the worst
417 * case, we'll drop the next compressed packet and do a
422 state->cx.next_out = garbage;
423 state->cx.avail_out = sizeof garbage;
428 ccp->uncompin += len;
432 m_free(mi_head); /* lose our allocated ``head'' buf */
436 DeflateDispOpts(struct fsm_opt *o)
438 static char disp[7]; /* Must be used immediately */
440 sprintf(disp, "win %d", (o->data[0]>>4) + 8);
445 DeflateInitOptsOutput(struct bundle *bundle __unused, struct fsm_opt *o,
446 const struct ccp_config *cfg)
449 o->data[0] = ((cfg->deflate.out.winsize - 8) << 4) + 8;
454 DeflateSetOptsOutput(struct bundle *bundle __unused, struct fsm_opt *o,
455 const struct ccp_config *cfg __unused)
457 if (o->hdr.len != 4 || (o->data[0] & 15) != 8 || o->data[1] != '\0')
460 if ((o->data[0] >> 4) + 8 > 15) {
461 o->data[0] = ((15 - 8) << 4) + 8;
469 DeflateSetOptsInput(struct bundle *bundle __unused, struct fsm_opt *o,
470 const struct ccp_config *cfg)
474 if (o->hdr.len != 4 || (o->data[0] & 15) != 8 || o->data[1] != '\0')
477 want = (o->data[0] >> 4) + 8;
478 if (cfg->deflate.in.winsize == 0) {
479 if (want < 8 || want > 15) {
480 o->data[0] = ((15 - 8) << 4) + 8;
482 } else if (want != cfg->deflate.in.winsize) {
483 o->data[0] = ((cfg->deflate.in.winsize - 8) << 4) + 8;
491 DeflateInitInput(struct bundle *bundle __unused, struct fsm_opt *o)
493 struct deflate_state *state;
495 state = (struct deflate_state *)malloc(sizeof(struct deflate_state));
497 state->winsize = (o->data[0] >> 4) + 8;
498 state->cx.zalloc = NULL;
499 state->cx.opaque = NULL;
500 state->cx.zfree = NULL;
501 state->cx.next_out = NULL;
502 if (inflateInit2(&state->cx, -state->winsize) == Z_OK)
503 DeflateResetInput(state);
514 DeflateInitOutput(struct bundle *bundle __unused, struct fsm_opt *o)
516 struct deflate_state *state;
518 state = (struct deflate_state *)malloc(sizeof(struct deflate_state));
520 state->winsize = (o->data[0] >> 4) + 8;
521 state->cx.zalloc = NULL;
522 state->cx.opaque = NULL;
523 state->cx.zfree = NULL;
524 state->cx.next_in = NULL;
525 if (deflateInit2(&state->cx, Z_DEFAULT_COMPRESSION, 8,
526 -state->winsize, 8, Z_DEFAULT_STRATEGY) == Z_OK)
527 DeflateResetOutput(state);
538 DeflateTermInput(void *v)
540 struct deflate_state *state = (struct deflate_state *)v;
542 inflateEnd(&state->cx);
547 DeflateTermOutput(void *v)
549 struct deflate_state *state = (struct deflate_state *)v;
551 deflateEnd(&state->cx);
555 const struct ccp_algorithm PppdDeflateAlgorithm = {
556 TY_PPPD_DEFLATE, /* Older versions of pppd expected this ``type'' */
571 DeflateInitOptsOutput,
572 DeflateSetOptsOutput,
580 const struct ccp_algorithm DeflateAlgorithm = {
581 TY_DEFLATE, /* rfc 1979 */
596 DeflateInitOptsOutput,
597 DeflateSetOptsOutput,