ppp-deflate.c revision 1.3 1 1.3 christos /* $NetBSD: ppp-deflate.c,v 1.3 1996/10/13 02:11:08 christos Exp $ */
2 1.1 paulus
3 1.1 paulus /*
4 1.1 paulus * ppp_deflate.c - interface the zlib procedures for Deflate compression
5 1.1 paulus * and decompression (as used by gzip) to the PPP code.
6 1.1 paulus * This version is for use with mbufs on BSD-derived systems.
7 1.1 paulus *
8 1.1 paulus * Copyright (c) 1994 The Australian National University.
9 1.1 paulus * All rights reserved.
10 1.1 paulus *
11 1.1 paulus * Permission to use, copy, modify, and distribute this software and its
12 1.1 paulus * documentation is hereby granted, provided that the above copyright
13 1.1 paulus * notice appears in all copies. This software is provided without any
14 1.1 paulus * warranty, express or implied. The Australian National University
15 1.1 paulus * makes no representations about the suitability of this software for
16 1.1 paulus * any purpose.
17 1.1 paulus *
18 1.1 paulus * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
19 1.1 paulus * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
20 1.1 paulus * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
21 1.1 paulus * THE AUSTRALIAN NATIONAL UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY
22 1.1 paulus * OF SUCH DAMAGE.
23 1.1 paulus *
24 1.1 paulus * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
25 1.1 paulus * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
26 1.1 paulus * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
27 1.1 paulus * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
28 1.1 paulus * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
29 1.1 paulus * OR MODIFICATIONS.
30 1.1 paulus */
31 1.1 paulus
32 1.1 paulus #include <sys/param.h>
33 1.1 paulus #include <sys/types.h>
34 1.1 paulus #include <sys/systm.h>
35 1.1 paulus #include <sys/mbuf.h>
36 1.1 paulus #include <net/ppp_defs.h>
37 1.1 paulus #include <net/zlib.h>
38 1.1 paulus
39 1.1 paulus #define PACKETPTR struct mbuf *
40 1.1 paulus #include <net/ppp-comp.h>
41 1.1 paulus
42 1.1 paulus #if DO_DEFLATE
43 1.1 paulus
44 1.1 paulus /*
45 1.1 paulus * State for a Deflate (de)compressor.
46 1.1 paulus */
47 1.1 paulus struct deflate_state {
48 1.1 paulus int seqno;
49 1.1 paulus int w_size;
50 1.1 paulus int unit;
51 1.1 paulus int hdrlen;
52 1.1 paulus int mru;
53 1.1 paulus int debug;
54 1.1 paulus z_stream strm;
55 1.1 paulus struct compstat stats;
56 1.1 paulus };
57 1.1 paulus
58 1.1 paulus #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
59 1.1 paulus
60 1.1 paulus static void *zalloc __P((void *, u_int items, u_int size));
61 1.1 paulus static void zfree __P((void *, void *ptr, u_int nb));
62 1.1 paulus static void *z_comp_alloc __P((u_char *options, int opt_len));
63 1.1 paulus static void *z_decomp_alloc __P((u_char *options, int opt_len));
64 1.1 paulus static void z_comp_free __P((void *state));
65 1.1 paulus static void z_decomp_free __P((void *state));
66 1.1 paulus static int z_comp_init __P((void *state, u_char *options, int opt_len,
67 1.1 paulus int unit, int hdrlen, int debug));
68 1.1 paulus static int z_decomp_init __P((void *state, u_char *options, int opt_len,
69 1.1 paulus int unit, int hdrlen, int mru, int debug));
70 1.1 paulus static int z_compress __P((void *state, struct mbuf **mret,
71 1.1 paulus struct mbuf *mp, int slen, int maxolen));
72 1.1 paulus static void z_incomp __P((void *state, struct mbuf *dmsg));
73 1.1 paulus static int z_decompress __P((void *state, struct mbuf *cmp,
74 1.1 paulus struct mbuf **dmpp));
75 1.1 paulus static void z_comp_reset __P((void *state));
76 1.1 paulus static void z_decomp_reset __P((void *state));
77 1.1 paulus static void z_comp_stats __P((void *state, struct compstat *stats));
78 1.1 paulus
79 1.1 paulus /*
80 1.1 paulus * Procedures exported to if_ppp.c.
81 1.1 paulus */
82 1.1 paulus struct compressor ppp_deflate = {
83 1.1 paulus CI_DEFLATE, /* compress_proto */
84 1.1 paulus z_comp_alloc, /* comp_alloc */
85 1.1 paulus z_comp_free, /* comp_free */
86 1.1 paulus z_comp_init, /* comp_init */
87 1.1 paulus z_comp_reset, /* comp_reset */
88 1.1 paulus z_compress, /* compress */
89 1.1 paulus z_comp_stats, /* comp_stat */
90 1.1 paulus z_decomp_alloc, /* decomp_alloc */
91 1.1 paulus z_decomp_free, /* decomp_free */
92 1.1 paulus z_decomp_init, /* decomp_init */
93 1.1 paulus z_decomp_reset, /* decomp_reset */
94 1.1 paulus z_decompress, /* decompress */
95 1.1 paulus z_incomp, /* incomp */
96 1.1 paulus z_comp_stats, /* decomp_stat */
97 1.1 paulus };
98 1.1 paulus
99 1.1 paulus /*
100 1.1 paulus * Space allocation and freeing routines for use by zlib routines.
101 1.1 paulus */
102 1.1 paulus void *
103 1.1 paulus zalloc(notused, items, size)
104 1.1 paulus void *notused;
105 1.1 paulus u_int items, size;
106 1.1 paulus {
107 1.1 paulus void *ptr;
108 1.1 paulus
109 1.1 paulus MALLOC(ptr, void *, items * size, M_DEVBUF, M_NOWAIT);
110 1.1 paulus return ptr;
111 1.1 paulus }
112 1.1 paulus
113 1.1 paulus void
114 1.1 paulus zfree(notused, ptr, nbytes)
115 1.1 paulus void *notused;
116 1.1 paulus void *ptr;
117 1.1 paulus u_int nbytes;
118 1.1 paulus {
119 1.1 paulus FREE(ptr, M_DEVBUF);
120 1.1 paulus }
121 1.1 paulus
122 1.1 paulus /*
123 1.1 paulus * Allocate space for a compressor.
124 1.1 paulus */
125 1.1 paulus static void *
126 1.1 paulus z_comp_alloc(options, opt_len)
127 1.1 paulus u_char *options;
128 1.1 paulus int opt_len;
129 1.1 paulus {
130 1.1 paulus struct deflate_state *state;
131 1.1 paulus int w_size;
132 1.1 paulus
133 1.1 paulus if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
134 1.1 paulus || options[1] != CILEN_DEFLATE
135 1.1 paulus || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
136 1.1 paulus || options[3] != DEFLATE_CHK_SEQUENCE)
137 1.1 paulus return NULL;
138 1.1 paulus w_size = DEFLATE_SIZE(options[2]);
139 1.1 paulus if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
140 1.1 paulus return NULL;
141 1.1 paulus
142 1.1 paulus MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
143 1.1 paulus M_DEVBUF, M_NOWAIT);
144 1.1 paulus if (state == NULL)
145 1.1 paulus return NULL;
146 1.1 paulus
147 1.1 paulus state->strm.next_in = NULL;
148 1.1 paulus state->strm.zalloc = zalloc;
149 1.1 paulus state->strm.zfree = zfree;
150 1.1 paulus if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
151 1.1 paulus -w_size, 8, Z_DEFAULT_STRATEGY, DEFLATE_OVHD+2) != Z_OK) {
152 1.1 paulus FREE(state, M_DEVBUF);
153 1.1 paulus return NULL;
154 1.1 paulus }
155 1.1 paulus
156 1.1 paulus state->w_size = w_size;
157 1.1 paulus bzero(&state->stats, sizeof(state->stats));
158 1.1 paulus return (void *) state;
159 1.1 paulus }
160 1.1 paulus
161 1.1 paulus static void
162 1.1 paulus z_comp_free(arg)
163 1.1 paulus void *arg;
164 1.1 paulus {
165 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
166 1.1 paulus
167 1.1 paulus deflateEnd(&state->strm);
168 1.1 paulus FREE(state, M_DEVBUF);
169 1.1 paulus }
170 1.1 paulus
171 1.1 paulus static int
172 1.1 paulus z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
173 1.1 paulus void *arg;
174 1.1 paulus u_char *options;
175 1.1 paulus int opt_len, unit, hdrlen, debug;
176 1.1 paulus {
177 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
178 1.1 paulus
179 1.1 paulus if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
180 1.1 paulus || options[1] != CILEN_DEFLATE
181 1.1 paulus || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
182 1.1 paulus || DEFLATE_SIZE(options[2]) != state->w_size
183 1.1 paulus || options[3] != DEFLATE_CHK_SEQUENCE)
184 1.1 paulus return 0;
185 1.1 paulus
186 1.1 paulus state->seqno = 0;
187 1.1 paulus state->unit = unit;
188 1.1 paulus state->hdrlen = hdrlen;
189 1.1 paulus state->debug = debug;
190 1.1 paulus
191 1.1 paulus deflateReset(&state->strm);
192 1.1 paulus
193 1.1 paulus return 1;
194 1.1 paulus }
195 1.1 paulus
196 1.1 paulus static void
197 1.1 paulus z_comp_reset(arg)
198 1.1 paulus void *arg;
199 1.1 paulus {
200 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
201 1.1 paulus
202 1.1 paulus state->seqno = 0;
203 1.1 paulus deflateReset(&state->strm);
204 1.1 paulus }
205 1.1 paulus
206 1.1 paulus int
207 1.1 paulus z_compress(arg, mret, mp, orig_len, maxolen)
208 1.1 paulus void *arg;
209 1.1 paulus struct mbuf **mret; /* compressed packet (out) */
210 1.1 paulus struct mbuf *mp; /* uncompressed packet (in) */
211 1.1 paulus int orig_len, maxolen;
212 1.1 paulus {
213 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
214 1.1 paulus u_char *rptr, *wptr;
215 1.1 paulus int proto, olen, wspace, r, flush;
216 1.1 paulus struct mbuf *m;
217 1.1 paulus
218 1.1 paulus /*
219 1.1 paulus * Check that the protocol is in the range we handle.
220 1.1 paulus */
221 1.1 paulus rptr = mtod(mp, u_char *);
222 1.1 paulus proto = PPP_PROTOCOL(rptr);
223 1.1 paulus if (proto > 0x3fff || proto == 0xfd || proto == 0xfb) {
224 1.1 paulus *mret = NULL;
225 1.1 paulus return orig_len;
226 1.1 paulus }
227 1.1 paulus
228 1.1 paulus /* Allocate one mbuf initially. */
229 1.1 paulus if (maxolen > orig_len)
230 1.1 paulus maxolen = orig_len;
231 1.1 paulus MGET(m, M_DONTWAIT, MT_DATA);
232 1.1 paulus *mret = m;
233 1.1 paulus if (m != NULL) {
234 1.1 paulus m->m_len = 0;
235 1.1 paulus if (maxolen + state->hdrlen > MLEN)
236 1.1 paulus MCLGET(m, M_DONTWAIT);
237 1.1 paulus wspace = M_TRAILINGSPACE(m);
238 1.1 paulus if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
239 1.1 paulus m->m_data += state->hdrlen;
240 1.1 paulus wspace -= state->hdrlen;
241 1.1 paulus }
242 1.1 paulus wptr = mtod(m, u_char *);
243 1.1 paulus
244 1.1 paulus /*
245 1.1 paulus * Copy over the PPP header and store the 2-byte sequence number.
246 1.1 paulus */
247 1.1 paulus wptr[0] = PPP_ADDRESS(rptr);
248 1.1 paulus wptr[1] = PPP_CONTROL(rptr);
249 1.1 paulus wptr[2] = PPP_COMP >> 8;
250 1.1 paulus wptr[3] = PPP_COMP;
251 1.1 paulus wptr += PPP_HDRLEN;
252 1.1 paulus wptr[0] = state->seqno >> 8;
253 1.1 paulus wptr[1] = state->seqno;
254 1.1 paulus wptr += 2;
255 1.1 paulus state->strm.next_out = wptr;
256 1.1 paulus state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
257 1.1 paulus } else {
258 1.1 paulus state->strm.next_out = NULL;
259 1.1 paulus state->strm.avail_out = 1000000;
260 1.1 paulus wptr = NULL;
261 1.1 paulus wspace = 0;
262 1.1 paulus }
263 1.1 paulus ++state->seqno;
264 1.1 paulus
265 1.1 paulus rptr += (proto > 0xff)? 2: 3; /* skip 1st proto byte if 0 */
266 1.1 paulus state->strm.next_in = rptr;
267 1.1 paulus state->strm.avail_in = mtod(mp, u_char *) + mp->m_len - rptr;
268 1.1 paulus mp = mp->m_next;
269 1.1 paulus flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
270 1.1 paulus olen = 0;
271 1.1 paulus for (;;) {
272 1.1 paulus r = deflate(&state->strm, flush);
273 1.1 paulus if (r != Z_OK) {
274 1.3 christos printf("z_compress: deflate returned %d (%s)\n",
275 1.2 christos r, (state->strm.msg? state->strm.msg: ""));
276 1.1 paulus break;
277 1.1 paulus }
278 1.1 paulus if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
279 1.1 paulus break; /* all done */
280 1.1 paulus if (state->strm.avail_in == 0 && mp != NULL) {
281 1.1 paulus state->strm.next_in = mtod(mp, u_char *);
282 1.1 paulus state->strm.avail_in = mp->m_len;
283 1.1 paulus mp = mp->m_next;
284 1.1 paulus if (mp == NULL)
285 1.1 paulus flush = Z_PACKET_FLUSH;
286 1.1 paulus }
287 1.1 paulus if (state->strm.avail_out == 0) {
288 1.1 paulus if (m != NULL) {
289 1.1 paulus m->m_len = wspace;
290 1.1 paulus olen += wspace;
291 1.1 paulus MGET(m->m_next, M_DONTWAIT, MT_DATA);
292 1.1 paulus m = m->m_next;
293 1.1 paulus if (m != NULL) {
294 1.1 paulus m->m_len = 0;
295 1.1 paulus if (maxolen - olen > MLEN)
296 1.1 paulus MCLGET(m, M_DONTWAIT);
297 1.1 paulus state->strm.next_out = mtod(m, u_char *);
298 1.1 paulus state->strm.avail_out = wspace = M_TRAILINGSPACE(m);
299 1.1 paulus }
300 1.1 paulus }
301 1.1 paulus if (m == NULL) {
302 1.1 paulus state->strm.next_out = NULL;
303 1.1 paulus state->strm.avail_out = 1000000;
304 1.1 paulus }
305 1.1 paulus }
306 1.1 paulus }
307 1.1 paulus if (m != NULL)
308 1.1 paulus olen += (m->m_len = wspace - state->strm.avail_out);
309 1.1 paulus
310 1.1 paulus /*
311 1.1 paulus * See if we managed to reduce the size of the packet.
312 1.1 paulus * If the compressor just gave us a single zero byte, it means
313 1.1 paulus * the packet was incompressible.
314 1.1 paulus */
315 1.1 paulus if (m != NULL && olen < orig_len
316 1.1 paulus && !(olen == PPP_HDRLEN + 3 && *wptr == 0)) {
317 1.1 paulus state->stats.comp_bytes += olen;
318 1.1 paulus state->stats.comp_packets++;
319 1.1 paulus } else {
320 1.1 paulus if (*mret != NULL) {
321 1.1 paulus m_freem(*mret);
322 1.1 paulus *mret = NULL;
323 1.1 paulus }
324 1.1 paulus state->stats.inc_bytes += orig_len;
325 1.1 paulus state->stats.inc_packets++;
326 1.1 paulus olen = orig_len;
327 1.1 paulus }
328 1.1 paulus state->stats.unc_bytes += orig_len;
329 1.1 paulus state->stats.unc_packets++;
330 1.1 paulus
331 1.1 paulus return olen;
332 1.1 paulus }
333 1.1 paulus
334 1.1 paulus static void
335 1.1 paulus z_comp_stats(arg, stats)
336 1.1 paulus void *arg;
337 1.1 paulus struct compstat *stats;
338 1.1 paulus {
339 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
340 1.1 paulus u_int out;
341 1.1 paulus
342 1.1 paulus *stats = state->stats;
343 1.1 paulus stats->ratio = stats->unc_bytes;
344 1.1 paulus out = stats->comp_bytes + stats->inc_bytes;
345 1.1 paulus if (stats->ratio <= 0x7ffffff)
346 1.1 paulus stats->ratio <<= 8;
347 1.1 paulus else
348 1.1 paulus out >>= 8;
349 1.1 paulus if (out != 0)
350 1.1 paulus stats->ratio /= out;
351 1.1 paulus }
352 1.1 paulus
353 1.1 paulus /*
354 1.1 paulus * Allocate space for a decompressor.
355 1.1 paulus */
356 1.1 paulus static void *
357 1.1 paulus z_decomp_alloc(options, opt_len)
358 1.1 paulus u_char *options;
359 1.1 paulus int opt_len;
360 1.1 paulus {
361 1.1 paulus struct deflate_state *state;
362 1.1 paulus int w_size;
363 1.1 paulus
364 1.1 paulus if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
365 1.1 paulus || options[1] != CILEN_DEFLATE
366 1.1 paulus || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
367 1.1 paulus || options[3] != DEFLATE_CHK_SEQUENCE)
368 1.1 paulus return NULL;
369 1.1 paulus w_size = DEFLATE_SIZE(options[2]);
370 1.1 paulus if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
371 1.1 paulus return NULL;
372 1.1 paulus
373 1.1 paulus MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
374 1.1 paulus M_DEVBUF, M_NOWAIT);
375 1.1 paulus if (state == NULL)
376 1.1 paulus return NULL;
377 1.1 paulus
378 1.1 paulus state->strm.next_out = NULL;
379 1.1 paulus state->strm.zalloc = zalloc;
380 1.1 paulus state->strm.zfree = zfree;
381 1.1 paulus if (inflateInit2(&state->strm, -w_size) != Z_OK) {
382 1.1 paulus FREE(state, M_DEVBUF);
383 1.1 paulus return NULL;
384 1.1 paulus }
385 1.1 paulus
386 1.1 paulus state->w_size = w_size;
387 1.1 paulus bzero(&state->stats, sizeof(state->stats));
388 1.1 paulus return (void *) state;
389 1.1 paulus }
390 1.1 paulus
391 1.1 paulus static void
392 1.1 paulus z_decomp_free(arg)
393 1.1 paulus void *arg;
394 1.1 paulus {
395 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
396 1.1 paulus
397 1.1 paulus inflateEnd(&state->strm);
398 1.1 paulus FREE(state, M_DEVBUF);
399 1.1 paulus }
400 1.1 paulus
401 1.1 paulus static int
402 1.1 paulus z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
403 1.1 paulus void *arg;
404 1.1 paulus u_char *options;
405 1.1 paulus int opt_len, unit, hdrlen, mru, debug;
406 1.1 paulus {
407 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
408 1.1 paulus
409 1.1 paulus if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
410 1.1 paulus || options[1] != CILEN_DEFLATE
411 1.1 paulus || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
412 1.1 paulus || DEFLATE_SIZE(options[2]) != state->w_size
413 1.1 paulus || options[3] != DEFLATE_CHK_SEQUENCE)
414 1.1 paulus return 0;
415 1.1 paulus
416 1.1 paulus state->seqno = 0;
417 1.1 paulus state->unit = unit;
418 1.1 paulus state->hdrlen = hdrlen;
419 1.1 paulus state->debug = debug;
420 1.1 paulus state->mru = mru;
421 1.1 paulus
422 1.1 paulus inflateReset(&state->strm);
423 1.1 paulus
424 1.1 paulus return 1;
425 1.1 paulus }
426 1.1 paulus
427 1.1 paulus static void
428 1.1 paulus z_decomp_reset(arg)
429 1.1 paulus void *arg;
430 1.1 paulus {
431 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
432 1.1 paulus
433 1.1 paulus state->seqno = 0;
434 1.1 paulus inflateReset(&state->strm);
435 1.1 paulus }
436 1.1 paulus
437 1.1 paulus /*
438 1.1 paulus * Decompress a Deflate-compressed packet.
439 1.1 paulus *
440 1.1 paulus * Because of patent problems, we return DECOMP_ERROR for errors
441 1.1 paulus * found by inspecting the input data and for system problems, but
442 1.1 paulus * DECOMP_FATALERROR for any errors which could possibly be said to
443 1.1 paulus * be being detected "after" decompression. For DECOMP_ERROR,
444 1.1 paulus * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
445 1.1 paulus * infringing a patent of Motorola's if we do, so we take CCP down
446 1.1 paulus * instead.
447 1.1 paulus *
448 1.1 paulus * Given that the frame has the correct sequence number and a good FCS,
449 1.1 paulus * errors such as invalid codes in the input most likely indicate a
450 1.1 paulus * bug, so we return DECOMP_FATALERROR for them in order to turn off
451 1.1 paulus * compression, even though they are detected by inspecting the input.
452 1.1 paulus */
453 1.1 paulus int
454 1.1 paulus z_decompress(arg, mi, mop)
455 1.1 paulus void *arg;
456 1.1 paulus struct mbuf *mi, **mop;
457 1.1 paulus {
458 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
459 1.1 paulus struct mbuf *mo, *mo_head;
460 1.1 paulus u_char *rptr, *wptr;
461 1.1 paulus int rlen, olen, ospace;
462 1.1 paulus int seq, i, flush, r, decode_proto;
463 1.1 paulus u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
464 1.1 paulus
465 1.1 paulus *mop = NULL;
466 1.1 paulus rptr = mtod(mi, u_char *);
467 1.1 paulus rlen = mi->m_len;
468 1.1 paulus for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
469 1.1 paulus while (rlen <= 0) {
470 1.1 paulus mi = mi->m_next;
471 1.1 paulus if (mi == NULL)
472 1.1 paulus return DECOMP_ERROR;
473 1.1 paulus rptr = mtod(mi, u_char *);
474 1.1 paulus rlen = mi->m_len;
475 1.1 paulus }
476 1.1 paulus hdr[i] = *rptr++;
477 1.1 paulus --rlen;
478 1.1 paulus }
479 1.1 paulus
480 1.1 paulus /* Check the sequence number. */
481 1.1 paulus seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
482 1.1 paulus if (seq != state->seqno) {
483 1.1 paulus if (state->debug)
484 1.3 christos printf("z_decompress%d: bad seq # %d, expected %d\n",
485 1.2 christos state->unit, seq, state->seqno);
486 1.1 paulus return DECOMP_ERROR;
487 1.1 paulus }
488 1.1 paulus ++state->seqno;
489 1.1 paulus
490 1.1 paulus /* Allocate an output mbuf. */
491 1.1 paulus MGETHDR(mo, M_DONTWAIT, MT_DATA);
492 1.1 paulus if (mo == NULL)
493 1.1 paulus return DECOMP_ERROR;
494 1.1 paulus mo_head = mo;
495 1.1 paulus mo->m_len = 0;
496 1.1 paulus mo->m_next = NULL;
497 1.1 paulus MCLGET(mo, M_DONTWAIT);
498 1.1 paulus ospace = M_TRAILINGSPACE(mo);
499 1.1 paulus if (state->hdrlen + PPP_HDRLEN < ospace) {
500 1.1 paulus mo->m_data += state->hdrlen;
501 1.1 paulus ospace -= state->hdrlen;
502 1.1 paulus }
503 1.1 paulus
504 1.1 paulus /*
505 1.1 paulus * Fill in the first part of the PPP header. The protocol field
506 1.1 paulus * comes from the decompressed data.
507 1.1 paulus */
508 1.1 paulus wptr = mtod(mo, u_char *);
509 1.1 paulus wptr[0] = PPP_ADDRESS(hdr);
510 1.1 paulus wptr[1] = PPP_CONTROL(hdr);
511 1.1 paulus wptr[2] = 0;
512 1.1 paulus
513 1.1 paulus /*
514 1.1 paulus * Set up to call inflate. We set avail_out to 1 initially so we can
515 1.1 paulus * look at the first byte of the output and decide whether we have
516 1.1 paulus * a 1-byte or 2-byte protocol field.
517 1.1 paulus */
518 1.1 paulus state->strm.next_in = rptr;
519 1.1 paulus state->strm.avail_in = rlen;
520 1.1 paulus mi = mi->m_next;
521 1.1 paulus flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
522 1.1 paulus rlen += PPP_HDRLEN + DEFLATE_OVHD;
523 1.1 paulus state->strm.next_out = wptr + 3;
524 1.1 paulus state->strm.avail_out = 1;
525 1.1 paulus decode_proto = 1;
526 1.1 paulus olen = PPP_HDRLEN;
527 1.1 paulus
528 1.1 paulus /*
529 1.1 paulus * Call inflate, supplying more input or output as needed.
530 1.1 paulus */
531 1.1 paulus for (;;) {
532 1.1 paulus r = inflate(&state->strm, flush);
533 1.1 paulus if (r != Z_OK) {
534 1.1 paulus if (state->debug)
535 1.3 christos printf("z_decompress%d: inflate returned %d (%s)\n",
536 1.2 christos state->unit, r, (state->strm.msg? state->strm.msg: ""));
537 1.1 paulus m_freem(mo_head);
538 1.1 paulus return DECOMP_FATALERROR;
539 1.1 paulus }
540 1.1 paulus if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
541 1.1 paulus break; /* all done */
542 1.1 paulus if (state->strm.avail_in == 0 && mi != NULL) {
543 1.1 paulus state->strm.next_in = mtod(mi, u_char *);
544 1.1 paulus state->strm.avail_in = mi->m_len;
545 1.1 paulus rlen += mi->m_len;
546 1.1 paulus mi = mi->m_next;
547 1.1 paulus if (mi == NULL)
548 1.1 paulus flush = Z_PACKET_FLUSH;
549 1.1 paulus }
550 1.1 paulus if (state->strm.avail_out == 0) {
551 1.1 paulus if (decode_proto) {
552 1.1 paulus state->strm.avail_out = ospace - PPP_HDRLEN;
553 1.1 paulus if ((wptr[3] & 1) == 0) {
554 1.1 paulus /* 2-byte protocol field */
555 1.1 paulus wptr[2] = wptr[3];
556 1.1 paulus --state->strm.next_out;
557 1.1 paulus ++state->strm.avail_out;
558 1.1 paulus --olen;
559 1.1 paulus }
560 1.1 paulus decode_proto = 0;
561 1.1 paulus } else {
562 1.1 paulus mo->m_len = ospace;
563 1.1 paulus olen += ospace;
564 1.1 paulus MGET(mo->m_next, M_DONTWAIT, MT_DATA);
565 1.1 paulus mo = mo->m_next;
566 1.1 paulus if (mo == NULL) {
567 1.1 paulus m_freem(mo_head);
568 1.1 paulus return DECOMP_ERROR;
569 1.1 paulus }
570 1.1 paulus MCLGET(mo, M_DONTWAIT);
571 1.1 paulus state->strm.next_out = mtod(mo, u_char *);
572 1.1 paulus state->strm.avail_out = ospace = M_TRAILINGSPACE(mo);
573 1.1 paulus }
574 1.1 paulus }
575 1.1 paulus }
576 1.1 paulus if (decode_proto) {
577 1.1 paulus m_freem(mo_head);
578 1.1 paulus return DECOMP_ERROR;
579 1.1 paulus }
580 1.1 paulus olen += (mo->m_len = ospace - state->strm.avail_out);
581 1.1 paulus
582 1.1 paulus state->stats.unc_bytes += olen;
583 1.1 paulus state->stats.unc_packets++;
584 1.1 paulus state->stats.comp_bytes += rlen;
585 1.1 paulus state->stats.comp_packets++;
586 1.1 paulus
587 1.1 paulus *mop = mo_head;
588 1.1 paulus return DECOMP_OK;
589 1.1 paulus }
590 1.1 paulus
591 1.1 paulus /*
592 1.1 paulus * Incompressible data has arrived - add it to the history.
593 1.1 paulus */
594 1.1 paulus static void
595 1.1 paulus z_incomp(arg, mi)
596 1.1 paulus void *arg;
597 1.1 paulus struct mbuf *mi;
598 1.1 paulus {
599 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
600 1.1 paulus u_char *rptr;
601 1.1 paulus int rlen, proto, r;
602 1.1 paulus
603 1.1 paulus /*
604 1.1 paulus * Check that the protocol is one we handle.
605 1.1 paulus */
606 1.1 paulus rptr = mtod(mi, u_char *);
607 1.1 paulus proto = PPP_PROTOCOL(rptr);
608 1.1 paulus if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
609 1.1 paulus return;
610 1.1 paulus
611 1.1 paulus ++state->seqno;
612 1.1 paulus
613 1.1 paulus /*
614 1.1 paulus * Iterate through the mbufs, adding the characters in them
615 1.1 paulus * to the decompressor's history. For the first mbuf, we start
616 1.1 paulus * at the either the 1st or 2nd byte of the protocol field,
617 1.1 paulus * depending on whether the protocol value is compressible.
618 1.1 paulus */
619 1.1 paulus rlen = mi->m_len;
620 1.1 paulus state->strm.next_in = rptr + 3;
621 1.1 paulus state->strm.avail_in = rlen - 3;
622 1.1 paulus if (proto > 0xff) {
623 1.1 paulus --state->strm.next_in;
624 1.1 paulus ++state->strm.avail_in;
625 1.1 paulus }
626 1.1 paulus for (;;) {
627 1.1 paulus r = inflateIncomp(&state->strm);
628 1.1 paulus if (r != Z_OK) {
629 1.1 paulus /* gak! */
630 1.1 paulus if (state->debug) {
631 1.3 christos printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
632 1.2 christos state->unit, r, (state->strm.msg? state->strm.msg: ""));
633 1.1 paulus }
634 1.1 paulus return;
635 1.1 paulus }
636 1.1 paulus mi = mi->m_next;
637 1.1 paulus if (mi == NULL)
638 1.1 paulus break;
639 1.1 paulus state->strm.next_in = mtod(mi, u_char *);
640 1.1 paulus state->strm.avail_in = mi->m_len;
641 1.1 paulus rlen += mi->m_len;
642 1.1 paulus }
643 1.1 paulus
644 1.1 paulus /*
645 1.1 paulus * Update stats.
646 1.1 paulus */
647 1.1 paulus state->stats.inc_bytes += rlen;
648 1.1 paulus state->stats.inc_packets++;
649 1.1 paulus state->stats.unc_bytes += rlen;
650 1.1 paulus state->stats.unc_packets++;
651 1.1 paulus }
652 1.1 paulus
653 1.1 paulus #endif /* DO_DEFLATE */
654