2 * ppp_deflate.c - interface the zlib procedures for Deflate compression
3 * and decompression (as used by gzip) to the PPP code.
4 * This version is for use with STREAMS under SunOS 4.x, Solaris 2,
5 * SVR4, OSF/1 and AIX 4.x.
7 * Copyright (c) 1994 Paul Mackerras. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. The name(s) of the authors of this software must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission.
25 * 4. Redistributions of any form whatsoever must retain the following
27 * "This product includes software developed by Paul Mackerras
28 * <paulus@samba.org>".
30 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
31 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
32 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
33 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
34 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
35 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
36 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38 * $Id: deflate.c,v 1.12 2004/01/17 05:47:55 carlsonj Exp $
42 #include <net/net_globals.h>
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/stream.h>
47 #include <net/ppp_defs.h>
50 #define PACKETPTR mblk_t *
51 #include <net/ppp-comp.h>
56 #include "../common/zlib.h"
60 #include <sys/sunddi.h>
65 #define DEFLATE_DEBUG 1
68 * State for a Deflate (de)compressor.
70 struct deflate_state
{
78 struct compstat stats
;
81 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
83 static void *z_alloc
__P((void *, u_int items
, u_int size
));
84 static void *z_alloc_init
__P((void *, u_int items
, u_int size
));
85 static void z_free
__P((void *, void *ptr
));
86 static void *z_comp_alloc
__P((u_char
*options
, int opt_len
));
87 static void *z_decomp_alloc
__P((u_char
*options
, int opt_len
));
88 static void z_comp_free
__P((void *state
));
89 static void z_decomp_free
__P((void *state
));
90 static int z_comp_init
__P((void *state
, u_char
*options
, int opt_len
,
91 int unit
, int hdrlen
, int debug
));
92 static int z_decomp_init
__P((void *state
, u_char
*options
, int opt_len
,
93 int unit
, int hdrlen
, int mru
, int debug
));
94 static int z_compress
__P((void *state
, mblk_t
**mret
,
95 mblk_t
*mp
, int slen
, int maxolen
));
96 static void z_incomp
__P((void *state
, mblk_t
*dmsg
));
97 static int z_decompress
__P((void *state
, mblk_t
*cmp
,
99 static void z_comp_reset
__P((void *state
));
100 static void z_decomp_reset
__P((void *state
));
101 static void z_comp_stats
__P((void *state
, struct compstat
*stats
));
104 * Procedures exported to ppp_comp.c.
106 struct compressor ppp_deflate
= {
107 CI_DEFLATE
, /* compress_proto */
108 z_comp_alloc
, /* comp_alloc */
109 z_comp_free
, /* comp_free */
110 z_comp_init
, /* comp_init */
111 z_comp_reset
, /* comp_reset */
112 z_compress
, /* compress */
113 z_comp_stats
, /* comp_stat */
114 z_decomp_alloc
, /* decomp_alloc */
115 z_decomp_free
, /* decomp_free */
116 z_decomp_init
, /* decomp_init */
117 z_decomp_reset
, /* decomp_reset */
118 z_decompress
, /* decompress */
119 z_incomp
, /* incomp */
120 z_comp_stats
, /* decomp_stat */
123 struct compressor ppp_deflate_draft
= {
124 CI_DEFLATE_DRAFT
, /* compress_proto */
125 z_comp_alloc
, /* comp_alloc */
126 z_comp_free
, /* comp_free */
127 z_comp_init
, /* comp_init */
128 z_comp_reset
, /* comp_reset */
129 z_compress
, /* compress */
130 z_comp_stats
, /* comp_stat */
131 z_decomp_alloc
, /* decomp_alloc */
132 z_decomp_free
, /* decomp_free */
133 z_decomp_init
, /* decomp_init */
134 z_decomp_reset
, /* decomp_reset */
135 z_decompress
, /* decompress */
136 z_incomp
, /* incomp */
137 z_comp_stats
, /* decomp_stat */
140 #define DECOMP_CHUNK 512
143 * Space allocation and freeing routines for use by zlib routines.
150 #define GUARD_MAGIC 0x77a6011a
153 z_alloc_init(notused
, items
, size
)
159 size
= items
* size
+ sizeof(struct zchunk
);
161 z
= (struct zchunk
*) ALLOC_SLEEP(size
);
163 z
= (struct zchunk
*) ALLOC_NOSLEEP(size
);
166 z
->guard
= GUARD_MAGIC
;
167 return (void *) (z
+ 1);
171 z_alloc(notused
, items
, size
)
177 size
= items
* size
+ sizeof(struct zchunk
);
178 z
= (struct zchunk
*) ALLOC_NOSLEEP(size
);
180 z
->guard
= GUARD_MAGIC
;
181 return (void *) (z
+ 1);
189 struct zchunk
*z
= ((struct zchunk
*) ptr
) - 1;
191 if (z
->guard
!= GUARD_MAGIC
) {
192 printf("ppp: z_free of corrupted chunk at %x (%x, %x)\n",
193 z
, z
->size
, z
->guard
);
200 * Allocate space for a compressor.
203 z_comp_alloc(options
, opt_len
)
207 struct deflate_state
*state
;
210 if (opt_len
!= CILEN_DEFLATE
211 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
212 || options
[1] != CILEN_DEFLATE
213 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
214 || options
[3] != DEFLATE_CHK_SEQUENCE
)
216 w_size
= DEFLATE_SIZE(options
[2]);
218 * N.B. the 9 below should be DEFLATE_MIN_SIZE (8), but using
219 * 8 will cause kernel crashes because of a bug in zlib.
221 if (w_size
< 9 || w_size
> DEFLATE_MAX_SIZE
)
226 state
= (struct deflate_state
*) ALLOC_SLEEP(sizeof(*state
));
228 state
= (struct deflate_state
*) ALLOC_NOSLEEP(sizeof(*state
));
234 state
->strm
.next_in
= NULL
;
235 state
->strm
.zalloc
= (alloc_func
) z_alloc_init
;
236 state
->strm
.zfree
= (free_func
) z_free
;
237 if (deflateInit2(&state
->strm
, Z_DEFAULT_COMPRESSION
, DEFLATE_METHOD_VAL
,
238 -w_size
, 8, Z_DEFAULT_STRATEGY
) != Z_OK
) {
239 FREE(state
, sizeof(*state
));
243 state
->strm
.zalloc
= (alloc_func
) z_alloc
;
244 state
->w_size
= w_size
;
245 bzero(&state
->stats
, sizeof(state
->stats
));
246 return (void *) state
;
253 struct deflate_state
*state
= (struct deflate_state
*) arg
;
255 deflateEnd(&state
->strm
);
256 FREE(state
, sizeof(*state
));
260 z_comp_init(arg
, options
, opt_len
, unit
, hdrlen
, debug
)
263 int opt_len
, unit
, hdrlen
, debug
;
265 struct deflate_state
*state
= (struct deflate_state
*) arg
;
267 if (opt_len
< CILEN_DEFLATE
268 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
269 || options
[1] != CILEN_DEFLATE
270 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
271 || DEFLATE_SIZE(options
[2]) != state
->w_size
272 || options
[3] != DEFLATE_CHK_SEQUENCE
)
277 state
->hdrlen
= hdrlen
;
278 state
->debug
= debug
;
280 deflateReset(&state
->strm
);
289 struct deflate_state
*state
= (struct deflate_state
*) arg
;
292 deflateReset(&state
->strm
);
296 z_compress(arg
, mret
, mp
, orig_len
, maxolen
)
298 mblk_t
**mret
; /* compressed packet (out) */
299 mblk_t
*mp
; /* uncompressed packet (in) */
300 int orig_len
, maxolen
;
302 struct deflate_state
*state
= (struct deflate_state
*) arg
;
304 int proto
, olen
, wspace
, r
, flush
;
308 * Check that the protocol is in the range we handle.
312 if (rptr
+ PPP_HDRLEN
> mp
->b_wptr
) {
313 if (!pullupmsg(mp
, PPP_HDRLEN
))
317 proto
= PPP_PROTOCOL(rptr
);
318 if (proto
> 0x3fff || proto
== 0xfd || proto
== 0xfb)
321 /* Allocate one mblk initially. */
322 if (maxolen
> orig_len
)
324 if (maxolen
<= PPP_HDRLEN
+ 2) {
328 wspace
= maxolen
+ state
->hdrlen
;
331 m
= allocb(wspace
, BPRI_MED
);
335 if (state
->hdrlen
+ PPP_HDRLEN
+ 2 < wspace
) {
336 m
->b_rptr
+= state
->hdrlen
;
337 m
->b_wptr
= m
->b_rptr
;
338 wspace
-= state
->hdrlen
;
343 * Copy over the PPP header and store the 2-byte sequence number.
345 wptr
[0] = PPP_ADDRESS(rptr
);
346 wptr
[1] = PPP_CONTROL(rptr
);
347 wptr
[2] = PPP_COMP
>> 8;
350 wptr
[0] = state
->seqno
>> 8;
351 wptr
[1] = state
->seqno
;
353 state
->strm
.next_out
= wptr
;
354 state
->strm
.avail_out
= wspace
- (PPP_HDRLEN
+ 2);
356 state
->strm
.next_out
= NULL
;
357 state
->strm
.avail_out
= 1000000;
361 rptr
+= (proto
> 0xff)? 2: 3; /* skip 1st proto byte if 0 */
362 state
->strm
.next_in
= rptr
;
363 state
->strm
.avail_in
= mp
->b_wptr
- rptr
;
365 flush
= (mp
== NULL
)? Z_PACKET_FLUSH
: Z_NO_FLUSH
;
368 r
= deflate(&state
->strm
, flush
);
370 printf("z_compress: deflate returned %d (%s)\n",
371 r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
374 if (flush
!= Z_NO_FLUSH
&& state
->strm
.avail_out
!= 0)
375 break; /* all done */
376 if (state
->strm
.avail_in
== 0 && mp
!= NULL
) {
377 state
->strm
.next_in
= mp
->b_rptr
;
378 state
->strm
.avail_in
= mp
->b_wptr
- mp
->b_rptr
;
381 flush
= Z_PACKET_FLUSH
;
383 if (state
->strm
.avail_out
== 0) {
387 wspace
= maxolen
- olen
;
394 else if (wspace
> 4096)
396 m
->b_cont
= allocb(wspace
, BPRI_MED
);
400 state
->strm
.next_out
= m
->b_wptr
;
401 state
->strm
.avail_out
= wspace
;
405 state
->strm
.next_out
= NULL
;
406 state
->strm
.avail_out
= 1000000;
411 m
->b_wptr
+= wspace
- state
->strm
.avail_out
;
412 olen
+= wspace
- state
->strm
.avail_out
;
416 * See if we managed to reduce the size of the packet.
418 if (olen
< orig_len
&& m
!= NULL
) {
419 state
->stats
.comp_bytes
+= olen
;
420 state
->stats
.comp_packets
++;
426 state
->stats
.inc_bytes
+= orig_len
;
427 state
->stats
.inc_packets
++;
430 state
->stats
.unc_bytes
+= orig_len
;
431 state
->stats
.unc_packets
++;
437 z_comp_stats(arg
, stats
)
439 struct compstat
*stats
;
441 struct deflate_state
*state
= (struct deflate_state
*) arg
;
444 *stats
= state
->stats
;
445 stats
->ratio
= stats
->unc_bytes
;
446 out
= stats
->comp_bytes
+ stats
->unc_bytes
;
447 if (stats
->ratio
<= 0x7ffffff)
456 * Allocate space for a decompressor.
459 z_decomp_alloc(options
, opt_len
)
463 struct deflate_state
*state
;
466 if (opt_len
!= CILEN_DEFLATE
467 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
468 || options
[1] != CILEN_DEFLATE
469 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
470 || options
[3] != DEFLATE_CHK_SEQUENCE
)
472 w_size
= DEFLATE_SIZE(options
[2]);
474 * N.B. the 9 below should be DEFLATE_MIN_SIZE (8), but using
475 * 8 will cause kernel crashes because of a bug in zlib.
477 if (w_size
< 9 || w_size
> DEFLATE_MAX_SIZE
)
481 state
= (struct deflate_state
*) ALLOC_SLEEP(sizeof(*state
));
483 state
= (struct deflate_state
*) ALLOC_NOSLEEP(sizeof(*state
));
488 state
->strm
.next_out
= NULL
;
489 state
->strm
.zalloc
= (alloc_func
) z_alloc_init
;
490 state
->strm
.zfree
= (free_func
) z_free
;
491 if (inflateInit2(&state
->strm
, -w_size
) != Z_OK
) {
492 FREE(state
, sizeof(*state
));
496 state
->strm
.zalloc
= (alloc_func
) z_alloc
;
497 state
->w_size
= w_size
;
498 bzero(&state
->stats
, sizeof(state
->stats
));
499 return (void *) state
;
506 struct deflate_state
*state
= (struct deflate_state
*) arg
;
508 inflateEnd(&state
->strm
);
509 FREE(state
, sizeof(*state
));
513 z_decomp_init(arg
, options
, opt_len
, unit
, hdrlen
, mru
, debug
)
516 int opt_len
, unit
, hdrlen
, mru
, debug
;
518 struct deflate_state
*state
= (struct deflate_state
*) arg
;
520 if (opt_len
< CILEN_DEFLATE
521 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
522 || options
[1] != CILEN_DEFLATE
523 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
524 || DEFLATE_SIZE(options
[2]) != state
->w_size
525 || options
[3] != DEFLATE_CHK_SEQUENCE
)
530 state
->hdrlen
= hdrlen
;
531 state
->debug
= debug
;
534 inflateReset(&state
->strm
);
543 struct deflate_state
*state
= (struct deflate_state
*) arg
;
546 inflateReset(&state
->strm
);
550 * Decompress a Deflate-compressed packet.
552 * Because of patent problems, we return DECOMP_ERROR for errors
553 * found by inspecting the input data and for system problems, but
554 * DECOMP_FATALERROR for any errors which could possibly be said to
555 * be being detected "after" decompression. For DECOMP_ERROR,
556 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
557 * infringing a patent of Motorola's if we do, so we take CCP down
560 * Given that the frame has the correct sequence number and a good FCS,
561 * errors such as invalid codes in the input most likely indicate a
562 * bug, so we return DECOMP_FATALERROR for them in order to turn off
563 * compression, even though they are detected by inspecting the input.
566 z_decompress(arg
, mi
, mop
)
570 struct deflate_state
*state
= (struct deflate_state
*) arg
;
571 mblk_t
*mo
, *mo_head
;
573 int rlen
, olen
, ospace
;
574 int seq
, i
, flush
, r
, decode_proto
;
575 u_char hdr
[PPP_HDRLEN
+ DEFLATE_OVHD
];
579 for (i
= 0; i
< PPP_HDRLEN
+ DEFLATE_OVHD
; ++i
) {
580 while (rptr
>= mi
->b_wptr
) {
589 /* Check the sequence number. */
590 seq
= (hdr
[PPP_HDRLEN
] << 8) + hdr
[PPP_HDRLEN
+1];
591 if (seq
!= state
->seqno
) {
595 printf("z_decompress%d: bad seq # %d, expected %d\n",
596 state
->unit
, seq
, state
->seqno
);
601 /* Allocate an output message block. */
602 mo
= allocb(DECOMP_CHUNK
+ state
->hdrlen
, BPRI_MED
);
607 mo
->b_rptr
+= state
->hdrlen
;
608 mo
->b_wptr
= wptr
= mo
->b_rptr
;
609 ospace
= DECOMP_CHUNK
;
613 * Fill in the first part of the PPP header. The protocol field
614 * comes from the decompressed data.
616 wptr
[0] = PPP_ADDRESS(hdr
);
617 wptr
[1] = PPP_CONTROL(hdr
);
621 * Set up to call inflate. We set avail_out to 1 initially so we can
622 * look at the first byte of the output and decide whether we have
623 * a 1-byte or 2-byte protocol field.
625 state
->strm
.next_in
= rptr
;
626 state
->strm
.avail_in
= mi
->b_wptr
- rptr
;
628 flush
= (mi
== NULL
)? Z_PACKET_FLUSH
: Z_NO_FLUSH
;
629 rlen
= state
->strm
.avail_in
+ PPP_HDRLEN
+ DEFLATE_OVHD
;
630 state
->strm
.next_out
= wptr
+ 3;
631 state
->strm
.avail_out
= 1;
635 * Call inflate, supplying more input or output as needed.
638 r
= inflate(&state
->strm
, flush
);
643 printf("z_decompress%d: inflate returned %d (%s)\n",
644 state
->unit
, r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
646 return DECOMP_FATALERROR
;
648 if (flush
!= Z_NO_FLUSH
&& state
->strm
.avail_out
!= 0)
649 break; /* all done */
650 if (state
->strm
.avail_in
== 0 && mi
!= NULL
) {
651 state
->strm
.next_in
= mi
->b_rptr
;
652 state
->strm
.avail_in
= mi
->b_wptr
- mi
->b_rptr
;
653 rlen
+= state
->strm
.avail_in
;
656 flush
= Z_PACKET_FLUSH
;
658 if (state
->strm
.avail_out
== 0) {
660 state
->strm
.avail_out
= ospace
- PPP_HDRLEN
;
661 if ((wptr
[3] & 1) == 0) {
662 /* 2-byte protocol field */
664 --state
->strm
.next_out
;
665 ++state
->strm
.avail_out
;
669 mo
->b_wptr
+= ospace
;
671 mo
->b_cont
= allocb(DECOMP_CHUNK
, BPRI_MED
);
677 state
->strm
.next_out
= mo
->b_rptr
;
678 state
->strm
.avail_out
= ospace
= DECOMP_CHUNK
;
686 mo
->b_wptr
+= ospace
- state
->strm
.avail_out
;
687 olen
+= ospace
- state
->strm
.avail_out
;
690 if (olen
> state
->mru
+ PPP_HDRLEN
)
691 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
692 state
->unit
, olen
, state
->mru
+ PPP_HDRLEN
);
695 state
->stats
.unc_bytes
+= olen
;
696 state
->stats
.unc_packets
++;
697 state
->stats
.comp_bytes
+= rlen
;
698 state
->stats
.comp_packets
++;
705 * Incompressible data has arrived - add it to the history.
712 struct deflate_state
*state
= (struct deflate_state
*) arg
;
717 * Check that the protocol is one we handle.
720 if (rptr
+ PPP_HDRLEN
> mi
->b_wptr
) {
721 if (!pullupmsg(mi
, PPP_HDRLEN
))
725 proto
= PPP_PROTOCOL(rptr
);
726 if (proto
> 0x3fff || proto
== 0xfd || proto
== 0xfb)
732 * Iterate through the message blocks, adding the characters in them
733 * to the decompressor's history. For the first block, we start
734 * at the either the 1st or 2nd byte of the protocol field,
735 * depending on whether the protocol value is compressible.
737 rlen
= mi
->b_wptr
- mi
->b_rptr
;
738 state
->strm
.next_in
= rptr
+ 3;
739 state
->strm
.avail_in
= rlen
- 3;
741 --state
->strm
.next_in
;
742 ++state
->strm
.avail_in
;
745 r
= inflateIncomp(&state
->strm
);
751 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
752 state
->unit
, r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
758 state
->strm
.next_in
= mi
->b_rptr
;
759 state
->strm
.avail_in
= mi
->b_wptr
- mi
->b_rptr
;
760 rlen
+= state
->strm
.avail_in
;
766 state
->stats
.inc_bytes
+= rlen
;
767 state
->stats
.inc_packets
++;
768 state
->stats
.unc_bytes
+= rlen
;
769 state
->stats
.unc_packets
++;
772 #endif /* DO_DEFLATE */