2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * ppp_deflate.c - interface the zlib procedures for Deflate compression
24 * and decompression (as used by gzip) to the PPP code.
25 * This version is for use with mbufs on BSD-derived systems.
27 * Copyright (c) 1994 The Australian National University.
28 * All rights reserved.
30 * Permission to use, copy, modify, and distribute this software and its
31 * documentation is hereby granted, provided that the above copyright
32 * notice appears in all copies. This software is provided without any
33 * warranty, express or implied. The Australian National University
34 * makes no representations about the suitability of this software for
37 * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
38 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
39 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
40 * THE AUSTRALIAN NATIONAL UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY
43 * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
44 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
45 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
46 * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
47 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/malloc.h>
55 #include <net/ppp_defs.h>
58 #define PACKETPTR struct mbuf *
59 #include <net/ppp_comp.h>
63 #define DEFLATE_DEBUG 1
66 * State for a Deflate (de)compressor.
68 struct deflate_state
{
76 struct compstat stats
;
79 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
81 static void *z_alloc
__P((void *, u_int items
, u_int size
));
82 static void z_free
__P((void *, void *ptr
));
83 static void *z_comp_alloc
__P((u_char
*options
, int opt_len
));
84 static void *z_decomp_alloc
__P((u_char
*options
, int opt_len
));
85 static void z_comp_free
__P((void *state
));
86 static void z_decomp_free
__P((void *state
));
87 static int z_comp_init
__P((void *state
, u_char
*options
, int opt_len
,
88 int unit
, int hdrlen
, int debug
));
89 static int z_decomp_init
__P((void *state
, u_char
*options
, int opt_len
,
90 int unit
, int hdrlen
, int mru
, int debug
));
91 static int z_compress
__P((void *state
, struct mbuf
**mret
,
92 struct mbuf
*mp
, int slen
, int maxolen
));
93 static void z_incomp
__P((void *state
, struct mbuf
*dmsg
));
94 static int z_decompress
__P((void *state
, struct mbuf
*cmp
,
96 static void z_comp_reset
__P((void *state
));
97 static void z_decomp_reset
__P((void *state
));
98 static void z_comp_stats
__P((void *state
, struct compstat
*stats
));
101 * Procedures exported to if_ppp.c.
103 struct compressor ppp_deflate
= {
104 CI_DEFLATE
, /* compress_proto */
105 z_comp_alloc
, /* comp_alloc */
106 z_comp_free
, /* comp_free */
107 z_comp_init
, /* comp_init */
108 z_comp_reset
, /* comp_reset */
109 z_compress
, /* compress */
110 z_comp_stats
, /* comp_stat */
111 z_decomp_alloc
, /* decomp_alloc */
112 z_decomp_free
, /* decomp_free */
113 z_decomp_init
, /* decomp_init */
114 z_decomp_reset
, /* decomp_reset */
115 z_decompress
, /* decompress */
116 z_incomp
, /* incomp */
117 z_comp_stats
, /* decomp_stat */
120 struct compressor ppp_deflate_draft
= {
121 CI_DEFLATE_DRAFT
, /* compress_proto */
122 z_comp_alloc
, /* comp_alloc */
123 z_comp_free
, /* comp_free */
124 z_comp_init
, /* comp_init */
125 z_comp_reset
, /* comp_reset */
126 z_compress
, /* compress */
127 z_comp_stats
, /* comp_stat */
128 z_decomp_alloc
, /* decomp_alloc */
129 z_decomp_free
, /* decomp_free */
130 z_decomp_init
, /* decomp_init */
131 z_decomp_reset
, /* decomp_reset */
132 z_decompress
, /* decompress */
133 z_incomp
, /* incomp */
134 z_comp_stats
, /* decomp_stat */
138 * Space allocation and freeing routines for use by zlib routines.
141 z_alloc(notused
, items
, size
)
147 MALLOC(ptr
, void *, items
* size
, M_DEVBUF
, M_NOWAIT
);
160 * Allocate space for a compressor.
163 z_comp_alloc(options
, opt_len
)
167 struct deflate_state
*state
;
170 if (opt_len
!= CILEN_DEFLATE
171 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
172 || options
[1] != CILEN_DEFLATE
173 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
174 || options
[3] != DEFLATE_CHK_SEQUENCE
)
176 w_size
= DEFLATE_SIZE(options
[2]);
177 if (w_size
< DEFLATE_MIN_SIZE
|| w_size
> DEFLATE_MAX_SIZE
)
180 MALLOC(state
, struct deflate_state
*, sizeof(struct deflate_state
),
185 state
->strm
.next_in
= NULL
;
186 state
->strm
.zalloc
= z_alloc
;
187 state
->strm
.zfree
= z_free
;
188 if (deflateInit2(&state
->strm
, Z_DEFAULT_COMPRESSION
, DEFLATE_METHOD_VAL
,
189 -w_size
, 8, Z_DEFAULT_STRATEGY
) != Z_OK
) {
190 FREE(state
, M_DEVBUF
);
194 state
->w_size
= w_size
;
195 bzero(&state
->stats
, sizeof(state
->stats
));
196 return (void *) state
;
203 struct deflate_state
*state
= (struct deflate_state
*) arg
;
205 deflateEnd(&state
->strm
);
206 FREE(state
, M_DEVBUF
);
210 z_comp_init(arg
, options
, opt_len
, unit
, hdrlen
, debug
)
213 int opt_len
, unit
, hdrlen
, debug
;
215 struct deflate_state
*state
= (struct deflate_state
*) arg
;
217 if (opt_len
< CILEN_DEFLATE
218 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
219 || options
[1] != CILEN_DEFLATE
220 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
221 || DEFLATE_SIZE(options
[2]) != state
->w_size
222 || options
[3] != DEFLATE_CHK_SEQUENCE
)
227 state
->hdrlen
= hdrlen
;
228 state
->debug
= debug
;
230 deflateReset(&state
->strm
);
239 struct deflate_state
*state
= (struct deflate_state
*) arg
;
242 deflateReset(&state
->strm
);
246 z_compress(arg
, mret
, mp
, orig_len
, maxolen
)
248 struct mbuf
**mret
; /* compressed packet (out) */
249 struct mbuf
*mp
; /* uncompressed packet (in) */
250 int orig_len
, maxolen
;
252 struct deflate_state
*state
= (struct deflate_state
*) arg
;
254 int proto
, olen
, wspace
, r
, flush
;
258 * Check that the protocol is in the range we handle.
260 rptr
= mtod(mp
, u_char
*);
261 proto
= PPP_PROTOCOL(rptr
);
262 if (proto
> 0x3fff || proto
== 0xfd || proto
== 0xfb) {
267 /* Allocate one mbuf initially. */
268 if (maxolen
> orig_len
)
270 MGET(m
, M_DONTWAIT
, MT_DATA
);
274 if (maxolen
+ state
->hdrlen
> MLEN
)
275 MCLGET(m
, M_DONTWAIT
);
276 wspace
= M_TRAILINGSPACE(m
);
277 if (state
->hdrlen
+ PPP_HDRLEN
+ 2 < wspace
) {
278 m
->m_data
+= 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
);
334 if (maxolen
- olen
> MLEN
)
335 MCLGET(m
, M_DONTWAIT
);
336 state
->strm
.next_out
= mtod(m
, u_char
*);
337 state
->strm
.avail_out
= wspace
= M_TRAILINGSPACE(m
);
341 state
->strm
.next_out
= NULL
;
342 state
->strm
.avail_out
= 1000000;
347 olen
+= (m
->m_len
= wspace
- state
->strm
.avail_out
);
350 * See if we managed to reduce the size of the packet.
352 if (m
!= NULL
&& olen
< orig_len
) {
353 state
->stats
.comp_bytes
+= olen
;
354 state
->stats
.comp_packets
++;
360 state
->stats
.inc_bytes
+= orig_len
;
361 state
->stats
.inc_packets
++;
364 state
->stats
.unc_bytes
+= orig_len
;
365 state
->stats
.unc_packets
++;
371 z_comp_stats(arg
, stats
)
373 struct compstat
*stats
;
375 struct deflate_state
*state
= (struct deflate_state
*) arg
;
378 *stats
= state
->stats
;
379 stats
->ratio
= stats
->unc_bytes
;
380 out
= stats
->comp_bytes
+ stats
->inc_bytes
;
381 if (stats
->ratio
<= 0x7ffffff)
390 * Allocate space for a decompressor.
393 z_decomp_alloc(options
, opt_len
)
397 struct deflate_state
*state
;
400 if (opt_len
!= CILEN_DEFLATE
401 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
402 || options
[1] != CILEN_DEFLATE
403 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
404 || options
[3] != DEFLATE_CHK_SEQUENCE
)
406 w_size
= DEFLATE_SIZE(options
[2]);
407 if (w_size
< DEFLATE_MIN_SIZE
|| w_size
> DEFLATE_MAX_SIZE
)
410 MALLOC(state
, struct deflate_state
*, sizeof(struct deflate_state
),
415 state
->strm
.next_out
= NULL
;
416 state
->strm
.zalloc
= z_alloc
;
417 state
->strm
.zfree
= z_free
;
418 if (inflateInit2(&state
->strm
, -w_size
) != Z_OK
) {
419 FREE(state
, M_DEVBUF
);
423 state
->w_size
= w_size
;
424 bzero(&state
->stats
, sizeof(state
->stats
));
425 return (void *) state
;
432 struct deflate_state
*state
= (struct deflate_state
*) arg
;
434 inflateEnd(&state
->strm
);
435 FREE(state
, M_DEVBUF
);
439 z_decomp_init(arg
, options
, opt_len
, unit
, hdrlen
, mru
, debug
)
442 int opt_len
, unit
, hdrlen
, mru
, debug
;
444 struct deflate_state
*state
= (struct deflate_state
*) arg
;
446 if (opt_len
< CILEN_DEFLATE
447 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
448 || options
[1] != CILEN_DEFLATE
449 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
450 || DEFLATE_SIZE(options
[2]) != state
->w_size
451 || options
[3] != DEFLATE_CHK_SEQUENCE
)
456 state
->hdrlen
= hdrlen
;
457 state
->debug
= debug
;
460 inflateReset(&state
->strm
);
469 struct deflate_state
*state
= (struct deflate_state
*) arg
;
472 inflateReset(&state
->strm
);
476 * Decompress a Deflate-compressed packet.
478 * Because of patent problems, we return DECOMP_ERROR for errors
479 * found by inspecting the input data and for system problems, but
480 * DECOMP_FATALERROR for any errors which could possibly be said to
481 * be being detected "after" decompression. For DECOMP_ERROR,
482 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
483 * infringing a patent of Motorola's if we do, so we take CCP down
486 * Given that the frame has the correct sequence number and a good FCS,
487 * errors such as invalid codes in the input most likely indicate a
488 * bug, so we return DECOMP_FATALERROR for them in order to turn off
489 * compression, even though they are detected by inspecting the input.
492 z_decompress(arg
, mi
, mop
)
494 struct mbuf
*mi
, **mop
;
496 struct deflate_state
*state
= (struct deflate_state
*) arg
;
497 struct mbuf
*mo
, *mo_head
;
499 int rlen
, olen
, ospace
;
500 int seq
, i
, flush
, r
, decode_proto
;
501 u_char hdr
[PPP_HDRLEN
+ DEFLATE_OVHD
];
504 rptr
= mtod(mi
, u_char
*);
506 for (i
= 0; i
< PPP_HDRLEN
+ DEFLATE_OVHD
; ++i
) {
511 rptr
= mtod(mi
, u_char
*);
518 /* Check the sequence number. */
519 seq
= (hdr
[PPP_HDRLEN
] << 8) + hdr
[PPP_HDRLEN
+1];
520 if (seq
!= state
->seqno
) {
522 printf("z_decompress%d: bad seq # %d, expected %d\n",
523 state
->unit
, seq
, state
->seqno
);
528 /* Allocate an output mbuf. */
529 MGETHDR(mo
, M_DONTWAIT
, MT_DATA
);
535 MCLGET(mo
, M_DONTWAIT
);
536 ospace
= M_TRAILINGSPACE(mo
);
537 if (state
->hdrlen
+ PPP_HDRLEN
< ospace
) {
538 mo
->m_data
+= state
->hdrlen
;
539 ospace
-= state
->hdrlen
;
543 * Fill in the first part of the PPP header. The protocol field
544 * comes from the decompressed data.
546 wptr
= mtod(mo
, u_char
*);
547 wptr
[0] = PPP_ADDRESS(hdr
);
548 wptr
[1] = PPP_CONTROL(hdr
);
552 * Set up to call inflate. We set avail_out to 1 initially so we can
553 * look at the first byte of the output and decide whether we have
554 * a 1-byte or 2-byte protocol field.
556 state
->strm
.next_in
= rptr
;
557 state
->strm
.avail_in
= rlen
;
559 flush
= (mi
== NULL
)? Z_PACKET_FLUSH
: Z_NO_FLUSH
;
560 rlen
+= PPP_HDRLEN
+ DEFLATE_OVHD
;
561 state
->strm
.next_out
= wptr
+ 3;
562 state
->strm
.avail_out
= 1;
567 * Call inflate, supplying more input or output as needed.
570 r
= inflate(&state
->strm
, flush
);
575 printf("z_decompress%d: inflate returned %d (%s)\n",
576 state
->unit
, r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
578 return DECOMP_FATALERROR
;
580 if (flush
!= Z_NO_FLUSH
&& state
->strm
.avail_out
!= 0)
581 break; /* all done */
582 if (state
->strm
.avail_in
== 0 && mi
!= NULL
) {
583 state
->strm
.next_in
= mtod(mi
, u_char
*);
584 state
->strm
.avail_in
= mi
->m_len
;
588 flush
= Z_PACKET_FLUSH
;
590 if (state
->strm
.avail_out
== 0) {
592 state
->strm
.avail_out
= ospace
- PPP_HDRLEN
;
593 if ((wptr
[3] & 1) == 0) {
594 /* 2-byte protocol field */
596 --state
->strm
.next_out
;
597 ++state
->strm
.avail_out
;
604 MGET(mo
->m_next
, M_DONTWAIT
, MT_DATA
);
610 MCLGET(mo
, M_DONTWAIT
);
611 state
->strm
.next_out
= mtod(mo
, u_char
*);
612 state
->strm
.avail_out
= ospace
= M_TRAILINGSPACE(mo
);
620 olen
+= (mo
->m_len
= ospace
- state
->strm
.avail_out
);
622 if (state
->debug
&& olen
> state
->mru
+ PPP_HDRLEN
)
623 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
624 state
->unit
, olen
, state
->mru
+ PPP_HDRLEN
);
627 state
->stats
.unc_bytes
+= olen
;
628 state
->stats
.unc_packets
++;
629 state
->stats
.comp_bytes
+= rlen
;
630 state
->stats
.comp_packets
++;
637 * Incompressible data has arrived - add it to the history.
644 struct deflate_state
*state
= (struct deflate_state
*) arg
;
649 * Check that the protocol is one we handle.
651 rptr
= mtod(mi
, u_char
*);
652 proto
= PPP_PROTOCOL(rptr
);
653 if (proto
> 0x3fff || proto
== 0xfd || proto
== 0xfb)
659 * Iterate through the mbufs, adding the characters in them
660 * to the decompressor's history. For the first mbuf, we start
661 * at the either the 1st or 2nd byte of the protocol field,
662 * depending on whether the protocol value is compressible.
665 state
->strm
.next_in
= rptr
+ 3;
666 state
->strm
.avail_in
= rlen
- 3;
668 --state
->strm
.next_in
;
669 ++state
->strm
.avail_in
;
672 r
= inflateIncomp(&state
->strm
);
678 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
679 state
->unit
, r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
685 state
->strm
.next_in
= mtod(mi
, u_char
*);
686 state
->strm
.avail_in
= mi
->m_len
;
693 state
->stats
.inc_bytes
+= rlen
;
694 state
->stats
.inc_packets
++;
695 state
->stats
.unc_bytes
+= rlen
;
696 state
->stats
.unc_packets
++;
699 #endif /* DO_DEFLATE */