1 /* $Id: ppp-deflate.c,v 1.6 2002/12/06 09:49:16 paulus Exp $ */
4 * ppp_deflate.c - interface the zlib procedures for Deflate compression
5 * and decompression (as used by gzip) to the PPP code.
6 * This version is for use with mbufs on BSD-derived systems.
8 * Copyright (c) 1994 Paul Mackerras. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
22 * 3. The name(s) of the authors of this software must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission.
26 * 4. Redistributions of any form whatsoever must retain the following
28 * "This product includes software developed by Paul Mackerras
29 * <paulus@samba.org>".
31 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
32 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
33 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
34 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
36 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
37 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
40 #include "../h/param.h"
41 #include "../h/types.h"
42 #include "../h/mbuf.h"
43 #include "../h/socket.h"
44 #include "../net/net/if.h"
48 #define PACKETPTR struct mbuf *
54 * State for a Deflate (de)compressor.
56 struct deflate_state
{
64 struct compstat stats
;
67 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
69 static void *zalloc
__P((void *, u_int items
, u_int size
));
70 static void zfree
__P((void *, void *ptr
));
71 static void *z_comp_alloc
__P((u_char
*options
, int opt_len
));
72 static void *z_decomp_alloc
__P((u_char
*options
, int opt_len
));
73 static void z_comp_free
__P((void *state
));
74 static void z_decomp_free
__P((void *state
));
75 static int z_comp_init
__P((void *state
, u_char
*options
, int opt_len
,
76 int unit
, int hdrlen
, int debug
));
77 static int z_decomp_init
__P((void *state
, u_char
*options
, int opt_len
,
78 int unit
, int hdrlen
, int mru
, int debug
));
79 static int z_compress
__P((void *state
, struct mbuf
**mret
,
80 struct mbuf
*mp
, int slen
, int maxolen
));
81 static void z_incomp
__P((void *state
, struct mbuf
*dmsg
));
82 static int z_decompress
__P((void *state
, struct mbuf
*cmp
,
84 static void z_comp_reset
__P((void *state
));
85 static void z_decomp_reset
__P((void *state
));
86 static void z_comp_stats
__P((void *state
, struct compstat
*stats
));
89 * Procedures exported to if_ppp.c.
91 struct compressor ppp_deflate
= {
92 CI_DEFLATE
, /* compress_proto */
93 z_comp_alloc
, /* comp_alloc */
94 z_comp_free
, /* comp_free */
95 z_comp_init
, /* comp_init */
96 z_comp_reset
, /* comp_reset */
97 z_compress
, /* compress */
98 z_comp_stats
, /* comp_stat */
99 z_decomp_alloc
, /* decomp_alloc */
100 z_decomp_free
, /* decomp_free */
101 z_decomp_init
, /* decomp_init */
102 z_decomp_reset
, /* decomp_reset */
103 z_decompress
, /* decompress */
104 z_incomp
, /* incomp */
105 z_comp_stats
, /* decomp_stat */
108 struct compressor ppp_deflate_draft
= {
109 CI_DEFLATE_DRAFT
, /* compress_proto */
110 z_comp_alloc
, /* comp_alloc */
111 z_comp_free
, /* comp_free */
112 z_comp_init
, /* comp_init */
113 z_comp_reset
, /* comp_reset */
114 z_compress
, /* compress */
115 z_comp_stats
, /* comp_stat */
116 z_decomp_alloc
, /* decomp_alloc */
117 z_decomp_free
, /* decomp_free */
118 z_decomp_init
, /* decomp_init */
119 z_decomp_reset
, /* decomp_reset */
120 z_decompress
, /* decompress */
121 z_incomp
, /* incomp */
122 z_comp_stats
, /* decomp_stat */
126 * Some useful mbuf macros not in mbuf.h.
128 #define M_IS_CLUSTER(m) ((m)->m_off > MMAXOFF)
130 #define M_TRAILINGSPACE(m) \
131 ((M_IS_CLUSTER(m) ? (u_int)(m)->m_clptr + M_CLUSTERSZ : MSIZE) \
132 - ((m)->m_off + (m)->m_len))
135 * Space allocation and freeing routines for use by zlib routines.
138 zalloc(notused
, items
, size
)
144 KM_ALLOC(ptr
, void *, items
* size
, KM_DEVBUF
, KM_NOARG
);
153 KM_FREE(ptr
, KM_DEVBUF
);
157 * Allocate space for a compressor.
160 z_comp_alloc(options
, opt_len
)
164 struct deflate_state
*state
;
167 if (opt_len
!= CILEN_DEFLATE
168 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
169 || options
[1] != CILEN_DEFLATE
170 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
171 || options
[3] != DEFLATE_CHK_SEQUENCE
)
173 w_size
= DEFLATE_SIZE(options
[2]);
174 if (w_size
< DEFLATE_MIN_SIZE
|| w_size
> DEFLATE_MAX_SIZE
)
177 KM_ALLOC(state
, struct deflate_state
*, sizeof(struct deflate_state
),
178 KM_DEVBUF
, KM_NOARG
);
181 bzero(state
, sizeof(struct deflate_state
));
183 state
->strm
.next_in
= NULL
;
184 state
->strm
.zalloc
= (alloc_func
) zalloc
;
185 state
->strm
.zfree
= (free_func
) zfree
;
186 if (deflateInit2(&state
->strm
, Z_DEFAULT_COMPRESSION
, DEFLATE_METHOD_VAL
,
187 -w_size
, 8, Z_DEFAULT_STRATEGY
) != Z_OK
) {
188 KM_FREE(state
, KM_DEVBUF
);
192 state
->w_size
= w_size
;
193 bzero(&state
->stats
, sizeof(state
->stats
));
194 return (void *) state
;
201 struct deflate_state
*state
= (struct deflate_state
*) arg
;
203 deflateEnd(&state
->strm
);
204 KM_FREE(state
, KM_DEVBUF
);
208 z_comp_init(arg
, options
, opt_len
, unit
, hdrlen
, debug
)
211 int opt_len
, unit
, hdrlen
, debug
;
213 struct deflate_state
*state
= (struct deflate_state
*) arg
;
215 if (opt_len
< CILEN_DEFLATE
216 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
217 || options
[1] != CILEN_DEFLATE
218 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
219 || DEFLATE_SIZE(options
[2]) != state
->w_size
220 || options
[3] != DEFLATE_CHK_SEQUENCE
)
225 state
->hdrlen
= hdrlen
;
226 state
->debug
= debug
;
228 deflateReset(&state
->strm
);
237 struct deflate_state
*state
= (struct deflate_state
*) arg
;
240 deflateReset(&state
->strm
);
244 z_compress(arg
, mret
, mp
, orig_len
, maxolen
)
246 struct mbuf
**mret
; /* compressed packet (out) */
247 struct mbuf
*mp
; /* uncompressed packet (in) */
248 int orig_len
, maxolen
;
250 struct deflate_state
*state
= (struct deflate_state
*) arg
;
252 int proto
, olen
, wspace
, r
, flush
;
253 struct mbuf
*m
, *clp
;
256 * Check that the protocol is in the range we handle.
258 rptr
= mtod(mp
, u_char
*);
259 proto
= PPP_PROTOCOL(rptr
);
260 if (proto
> 0x3fff || proto
== 0xfd || proto
== 0xfb) {
265 /* Allocate one mbuf initially. */
266 if (maxolen
> orig_len
)
268 MGET(m
, M_DONTWAIT
, MT_DATA
);
271 if (maxolen
+ state
->hdrlen
> MLEN
) {
272 /* MCLGET is not a single statement!!! */
276 wspace
= M_TRAILINGSPACE(m
);
277 if (state
->hdrlen
> 0 && state
->hdrlen
+ PPP_HDRLEN
+ 2 < wspace
) {
278 m
->m_off
+= state
->hdrlen
;
279 wspace
-= state
->hdrlen
;
281 wptr
= mtod(m
, u_char
*);
284 * Copy over the PPP header and store the 2-byte sequence number.
286 wptr
[0] = PPP_ADDRESS(rptr
);
287 wptr
[1] = PPP_CONTROL(rptr
);
288 wptr
[2] = PPP_COMP
>> 8;
291 wptr
[0] = state
->seqno
>> 8;
292 wptr
[1] = state
->seqno
;
294 state
->strm
.next_out
= wptr
;
295 state
->strm
.avail_out
= wspace
- (PPP_HDRLEN
+ 2);
297 state
->strm
.next_out
= NULL
;
298 state
->strm
.avail_out
= 1000000;
304 rptr
+= (proto
> 0xff)? 2: 3; /* skip 1st proto byte if 0 */
305 state
->strm
.next_in
= rptr
;
306 state
->strm
.avail_in
= mtod(mp
, u_char
*) + mp
->m_len
- rptr
;
308 flush
= (mp
== NULL
)? Z_PACKET_FLUSH
: Z_NO_FLUSH
;
311 r
= deflate(&state
->strm
, flush
);
313 printf("z_compress: deflate returned %d (%s)\n",
314 r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
317 if (flush
!= Z_NO_FLUSH
&& state
->strm
.avail_out
!= 0)
318 break; /* all done */
319 if (state
->strm
.avail_in
== 0 && mp
!= NULL
) {
320 state
->strm
.next_in
= mtod(mp
, u_char
*);
321 state
->strm
.avail_in
= mp
->m_len
;
324 flush
= Z_PACKET_FLUSH
;
326 if (state
->strm
.avail_out
== 0) {
330 MGET(m
->m_next
, M_DONTWAIT
, MT_DATA
);
333 if (maxolen
- olen
> MLEN
) {
337 state
->strm
.next_out
= mtod(m
, u_char
*);
338 state
->strm
.avail_out
= wspace
= M_TRAILINGSPACE(m
);
342 state
->strm
.next_out
= NULL
;
343 state
->strm
.avail_out
= 1000000;
348 olen
+= (m
->m_len
= wspace
- state
->strm
.avail_out
);
351 * See if we managed to reduce the size of the packet.
352 * If the compressor just gave us a single zero byte, it means
353 * the packet was incompressible.
355 if (m
!= NULL
&& olen
< orig_len
356 && !(olen
== PPP_HDRLEN
+ 3 && *wptr
== 0)) {
357 state
->stats
.comp_bytes
+= olen
;
358 state
->stats
.comp_packets
++;
364 state
->stats
.inc_bytes
+= orig_len
;
365 state
->stats
.inc_packets
++;
368 state
->stats
.unc_bytes
+= orig_len
;
369 state
->stats
.unc_packets
++;
375 z_comp_stats(arg
, stats
)
377 struct compstat
*stats
;
379 struct deflate_state
*state
= (struct deflate_state
*) arg
;
382 *stats
= state
->stats
;
383 stats
->ratio
= stats
->unc_bytes
;
384 out
= stats
->comp_bytes
+ stats
->inc_bytes
;
385 if (stats
->ratio
<= 0x7ffffff)
394 * Allocate space for a decompressor.
397 z_decomp_alloc(options
, opt_len
)
401 struct deflate_state
*state
;
404 if (opt_len
!= CILEN_DEFLATE
405 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
406 || options
[1] != CILEN_DEFLATE
407 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
408 || options
[3] != DEFLATE_CHK_SEQUENCE
)
410 w_size
= DEFLATE_SIZE(options
[2]);
411 if (w_size
< DEFLATE_MIN_SIZE
|| w_size
> DEFLATE_MAX_SIZE
)
414 KM_ALLOC(state
, struct deflate_state
*, sizeof(struct deflate_state
),
415 KM_DEVBUF
, KM_NOARG
);
418 bzero(state
, sizeof(struct deflate_state
));
420 state
->strm
.next_out
= NULL
;
421 state
->strm
.zalloc
= (alloc_func
) zalloc
;
422 state
->strm
.zfree
= (free_func
) zfree
;
423 if (inflateInit2(&state
->strm
, -w_size
) != Z_OK
) {
424 KM_FREE(state
, KM_DEVBUF
);
428 state
->w_size
= w_size
;
429 bzero(&state
->stats
, sizeof(state
->stats
));
430 return (void *) state
;
437 struct deflate_state
*state
= (struct deflate_state
*) arg
;
439 inflateEnd(&state
->strm
);
440 KM_FREE(state
, KM_DEVBUF
);
444 z_decomp_init(arg
, options
, opt_len
, unit
, hdrlen
, mru
, debug
)
447 int opt_len
, unit
, hdrlen
, mru
, debug
;
449 struct deflate_state
*state
= (struct deflate_state
*) arg
;
451 if (opt_len
< CILEN_DEFLATE
452 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
453 || options
[1] != CILEN_DEFLATE
454 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
455 || DEFLATE_SIZE(options
[2]) != state
->w_size
456 || options
[3] != DEFLATE_CHK_SEQUENCE
)
461 state
->hdrlen
= hdrlen
;
462 state
->debug
= debug
;
465 inflateReset(&state
->strm
);
474 struct deflate_state
*state
= (struct deflate_state
*) arg
;
477 inflateReset(&state
->strm
);
481 * Decompress a Deflate-compressed packet.
483 * Because of patent problems, we return DECOMP_ERROR for errors
484 * found by inspecting the input data and for system problems, but
485 * DECOMP_FATALERROR for any errors which could possibly be said to
486 * be being detected "after" decompression. For DECOMP_ERROR,
487 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
488 * infringing a patent of Motorola's if we do, so we take CCP down
491 * Given that the frame has the correct sequence number and a good FCS,
492 * errors such as invalid codes in the input most likely indicate a
493 * bug, so we return DECOMP_FATALERROR for them in order to turn off
494 * compression, even though they are detected by inspecting the input.
497 z_decompress(arg
, mi
, mop
)
499 struct mbuf
*mi
, **mop
;
501 struct deflate_state
*state
= (struct deflate_state
*) arg
;
502 struct mbuf
*mo
, *mo_head
, *clp
;
504 int rlen
, olen
, ospace
;
505 int seq
, i
, flush
, r
, decode_proto
;
506 u_char hdr
[PPP_HDRLEN
+ DEFLATE_OVHD
];
509 rptr
= mtod(mi
, u_char
*);
511 for (i
= 0; i
< PPP_HDRLEN
+ DEFLATE_OVHD
; ++i
) {
516 rptr
= mtod(mi
, u_char
*);
523 /* Check the sequence number. */
524 seq
= (hdr
[PPP_HDRLEN
] << 8) + hdr
[PPP_HDRLEN
+1];
525 if (seq
!= state
->seqno
) {
527 printf("z_decompress%d: bad seq # %d, expected %d\n",
528 state
->unit
, seq
, state
->seqno
);
533 /* Allocate an output mbuf. */
534 MGET(mo
, M_DONTWAIT
, MT_DATA
);
541 ospace
= M_TRAILINGSPACE(mo
);
542 if (state
->hdrlen
> 0 && state
->hdrlen
+ PPP_HDRLEN
< ospace
) {
543 mo
->m_off
+= state
->hdrlen
;
544 ospace
-= state
->hdrlen
;
548 * Fill in the first part of the PPP header. The protocol field
549 * comes from the decompressed data.
551 wptr
= mtod(mo
, u_char
*);
552 wptr
[0] = PPP_ADDRESS(hdr
);
553 wptr
[1] = PPP_CONTROL(hdr
);
557 * Set up to call inflate. We set avail_out to 1 initially so we can
558 * look at the first byte of the output and decide whether we have
559 * a 1-byte or 2-byte protocol field.
561 state
->strm
.next_in
= rptr
;
562 state
->strm
.avail_in
= rlen
;
564 flush
= (mi
== NULL
)? Z_PACKET_FLUSH
: Z_NO_FLUSH
;
565 rlen
+= PPP_HDRLEN
+ DEFLATE_OVHD
;
566 state
->strm
.next_out
= wptr
+ 3;
567 state
->strm
.avail_out
= 1;
572 * Call inflate, supplying more input or output as needed.
575 r
= inflate(&state
->strm
, flush
);
578 printf("z_decompress%d: inflate returned %d (%s)\n",
579 state
->unit
, r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
581 return DECOMP_FATALERROR
;
583 if (flush
!= Z_NO_FLUSH
&& state
->strm
.avail_out
!= 0)
584 break; /* all done */
585 if (state
->strm
.avail_in
== 0 && mi
!= NULL
) {
586 state
->strm
.next_in
= mtod(mi
, u_char
*);
587 state
->strm
.avail_in
= mi
->m_len
;
591 flush
= Z_PACKET_FLUSH
;
593 if (state
->strm
.avail_out
== 0) {
595 state
->strm
.avail_out
= ospace
- PPP_HDRLEN
;
596 if ((wptr
[3] & 1) == 0) {
597 /* 2-byte protocol field */
599 --state
->strm
.next_out
;
600 ++state
->strm
.avail_out
;
607 MGET(mo
->m_next
, M_DONTWAIT
, MT_DATA
);
615 state
->strm
.next_out
= mtod(mo
, u_char
*);
616 state
->strm
.avail_out
= ospace
= M_TRAILINGSPACE(mo
);
624 olen
+= (mo
->m_len
= ospace
- state
->strm
.avail_out
);
626 state
->stats
.unc_bytes
+= olen
;
627 state
->stats
.unc_packets
++;
628 state
->stats
.comp_bytes
+= rlen
;
629 state
->stats
.comp_packets
++;
636 * Incompressible data has arrived - add it to the history.
643 struct deflate_state
*state
= (struct deflate_state
*) arg
;
648 * Check that the protocol is one we handle.
650 rptr
= mtod(mi
, u_char
*);
651 proto
= PPP_PROTOCOL(rptr
);
652 if (proto
> 0x3fff || proto
== 0xfd || proto
== 0xfb)
658 * Iterate through the mbufs, adding the characters in them
659 * to the decompressor's history. For the first mbuf, we start
660 * at the either the 1st or 2nd byte of the protocol field,
661 * depending on whether the protocol value is compressible.
664 state
->strm
.next_in
= rptr
+ 3;
665 state
->strm
.avail_in
= rlen
- 3;
667 --state
->strm
.next_in
;
668 ++state
->strm
.avail_in
;
671 r
= inflateIncomp(&state
->strm
);
675 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
676 state
->unit
, r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
683 state
->strm
.next_in
= mtod(mi
, u_char
*);
684 state
->strm
.avail_in
= mi
->m_len
;
691 state
->stats
.inc_bytes
+= rlen
;
692 state
->stats
.inc_packets
++;
693 state
->stats
.unc_bytes
+= rlen
;
694 state
->stats
.unc_packets
++;
697 #endif /* DO_DEFLATE */