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(void *, u_int items
, u_int size
);
82 static void z_free(void *, void *ptr
);
83 static void *z_comp_alloc(u_char
*options
, int opt_len
);
84 static void *z_decomp_alloc(u_char
*options
, int opt_len
);
85 static void z_comp_free(void *state
);
86 static void z_decomp_free(void *state
);
87 static int z_comp_init(void *state
, u_char
*options
, int opt_len
,
88 int unit
, int hdrlen
, int debug
);
89 static int z_decomp_init(void *state
, u_char
*options
, int opt_len
,
90 int unit
, int hdrlen
, int mru
, int debug
);
91 static int z_compress(void *state
, struct mbuf
**mret
,
92 struct mbuf
*mp
, int slen
, int maxolen
);
93 static void z_incomp(void *state
, struct mbuf
*dmsg
);
94 static int z_decompress(void *state
, struct mbuf
*cmp
, struct mbuf
**dmpp
);
95 static void z_comp_reset(void *state
);
96 static void z_decomp_reset(void *state
);
97 static void z_comp_stats(void *state
, struct compstat
*stats
);
100 * Procedures exported to if_ppp.c.
102 struct compressor ppp_deflate
= {
103 CI_DEFLATE
, /* compress_proto */
104 z_comp_alloc
, /* comp_alloc */
105 z_comp_free
, /* comp_free */
106 z_comp_init
, /* comp_init */
107 z_comp_reset
, /* comp_reset */
108 z_compress
, /* compress */
109 z_comp_stats
, /* comp_stat */
110 z_decomp_alloc
, /* decomp_alloc */
111 z_decomp_free
, /* decomp_free */
112 z_decomp_init
, /* decomp_init */
113 z_decomp_reset
, /* decomp_reset */
114 z_decompress
, /* decompress */
115 z_incomp
, /* incomp */
116 z_comp_stats
, /* decomp_stat */
119 struct compressor ppp_deflate_draft
= {
120 CI_DEFLATE_DRAFT
, /* compress_proto */
121 z_comp_alloc
, /* comp_alloc */
122 z_comp_free
, /* comp_free */
123 z_comp_init
, /* comp_init */
124 z_comp_reset
, /* comp_reset */
125 z_compress
, /* compress */
126 z_comp_stats
, /* comp_stat */
127 z_decomp_alloc
, /* decomp_alloc */
128 z_decomp_free
, /* decomp_free */
129 z_decomp_init
, /* decomp_init */
130 z_decomp_reset
, /* decomp_reset */
131 z_decompress
, /* decompress */
132 z_incomp
, /* incomp */
133 z_comp_stats
, /* decomp_stat */
137 * Space allocation and freeing routines for use by zlib routines.
140 z_alloc(notused
, items
, size
)
146 MALLOC(ptr
, void *, items
* size
, M_DEVBUF
, M_NOWAIT
);
159 * Allocate space for a compressor.
162 z_comp_alloc(options
, opt_len
)
166 struct deflate_state
*state
;
169 if (opt_len
!= CILEN_DEFLATE
170 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
171 || options
[1] != CILEN_DEFLATE
172 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
173 || options
[3] != DEFLATE_CHK_SEQUENCE
)
175 w_size
= DEFLATE_SIZE(options
[2]);
176 if (w_size
< DEFLATE_MIN_SIZE
|| w_size
> DEFLATE_MAX_SIZE
)
179 MALLOC(state
, struct deflate_state
*, sizeof(struct deflate_state
),
184 state
->strm
.next_in
= NULL
;
185 state
->strm
.zalloc
= z_alloc
;
186 state
->strm
.zfree
= z_free
;
187 if (deflateInit2(&state
->strm
, Z_DEFAULT_COMPRESSION
, DEFLATE_METHOD_VAL
,
188 -w_size
, 8, Z_DEFAULT_STRATEGY
) != Z_OK
) {
189 FREE(state
, M_DEVBUF
);
193 state
->w_size
= w_size
;
194 bzero(&state
->stats
, sizeof(state
->stats
));
195 return (void *) state
;
202 struct deflate_state
*state
= (struct deflate_state
*) arg
;
204 deflateEnd(&state
->strm
);
205 FREE(state
, M_DEVBUF
);
209 z_comp_init(arg
, options
, opt_len
, unit
, hdrlen
, debug
)
212 int opt_len
, unit
, hdrlen
, debug
;
214 struct deflate_state
*state
= (struct deflate_state
*) arg
;
216 if (opt_len
< CILEN_DEFLATE
217 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
218 || options
[1] != CILEN_DEFLATE
219 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
220 || DEFLATE_SIZE(options
[2]) != state
->w_size
221 || options
[3] != DEFLATE_CHK_SEQUENCE
)
226 state
->hdrlen
= hdrlen
;
227 state
->debug
= debug
;
229 deflateReset(&state
->strm
);
238 struct deflate_state
*state
= (struct deflate_state
*) arg
;
241 deflateReset(&state
->strm
);
245 z_compress(arg
, mret
, mp
, orig_len
, maxolen
)
247 struct mbuf
**mret
; /* compressed packet (out) */
248 struct mbuf
*mp
; /* uncompressed packet (in) */
249 int orig_len
, maxolen
;
251 struct deflate_state
*state
= (struct deflate_state
*) arg
;
253 int proto
, olen
, wspace
, r
, flush
;
257 * Check that the protocol is in the range we handle.
259 rptr
= mtod(mp
, u_char
*);
260 proto
= PPP_PROTOCOL(rptr
);
261 if (proto
> 0x3fff || proto
== 0xfd || proto
== 0xfb) {
266 /* Allocate one mbuf initially. */
267 if (maxolen
> orig_len
)
269 MGET(m
, M_DONTWAIT
, MT_DATA
);
273 if (maxolen
+ state
->hdrlen
> MLEN
)
274 MCLGET(m
, M_DONTWAIT
);
275 wspace
= M_TRAILINGSPACE(m
);
276 if (state
->hdrlen
+ PPP_HDRLEN
+ 2 < wspace
) {
277 m
->m_data
+= state
->hdrlen
;
278 wspace
-= state
->hdrlen
;
280 wptr
= mtod(m
, u_char
*);
283 * Copy over the PPP header and store the 2-byte sequence number.
285 wptr
[0] = PPP_ADDRESS(rptr
);
286 wptr
[1] = PPP_CONTROL(rptr
);
287 wptr
[2] = PPP_COMP
>> 8;
290 wptr
[0] = state
->seqno
>> 8;
291 wptr
[1] = state
->seqno
;
293 state
->strm
.next_out
= wptr
;
294 state
->strm
.avail_out
= wspace
- (PPP_HDRLEN
+ 2);
296 state
->strm
.next_out
= NULL
;
297 state
->strm
.avail_out
= 1000000;
303 rptr
+= (proto
> 0xff)? 2: 3; /* skip 1st proto byte if 0 */
304 state
->strm
.next_in
= rptr
;
305 state
->strm
.avail_in
= mtod(mp
, u_char
*) + mp
->m_len
- rptr
;
307 flush
= (mp
== NULL
)? Z_PACKET_FLUSH
: Z_NO_FLUSH
;
310 r
= deflate(&state
->strm
, flush
);
312 printf("z_compress: deflate returned %d (%s)\n",
313 r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
316 if (flush
!= Z_NO_FLUSH
&& state
->strm
.avail_out
!= 0)
317 break; /* all done */
318 if (state
->strm
.avail_in
== 0 && mp
!= NULL
) {
319 state
->strm
.next_in
= mtod(mp
, u_char
*);
320 state
->strm
.avail_in
= mp
->m_len
;
323 flush
= Z_PACKET_FLUSH
;
325 if (state
->strm
.avail_out
== 0) {
329 MGET(m
->m_next
, M_DONTWAIT
, MT_DATA
);
333 if (maxolen
- olen
> MLEN
)
334 MCLGET(m
, M_DONTWAIT
);
335 state
->strm
.next_out
= mtod(m
, u_char
*);
336 state
->strm
.avail_out
= wspace
= M_TRAILINGSPACE(m
);
340 state
->strm
.next_out
= NULL
;
341 state
->strm
.avail_out
= 1000000;
346 olen
+= (m
->m_len
= wspace
- state
->strm
.avail_out
);
349 * See if we managed to reduce the size of the packet.
351 if (m
!= NULL
&& olen
< orig_len
) {
352 state
->stats
.comp_bytes
+= olen
;
353 state
->stats
.comp_packets
++;
359 state
->stats
.inc_bytes
+= orig_len
;
360 state
->stats
.inc_packets
++;
363 state
->stats
.unc_bytes
+= orig_len
;
364 state
->stats
.unc_packets
++;
370 z_comp_stats(arg
, stats
)
372 struct compstat
*stats
;
374 struct deflate_state
*state
= (struct deflate_state
*) arg
;
377 *stats
= state
->stats
;
378 stats
->ratio
= stats
->unc_bytes
;
379 out
= stats
->comp_bytes
+ stats
->inc_bytes
;
380 if (stats
->ratio
<= 0x7ffffff)
389 * Allocate space for a decompressor.
392 z_decomp_alloc(options
, opt_len
)
396 struct deflate_state
*state
;
399 if (opt_len
!= CILEN_DEFLATE
400 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
401 || options
[1] != CILEN_DEFLATE
402 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
403 || options
[3] != DEFLATE_CHK_SEQUENCE
)
405 w_size
= DEFLATE_SIZE(options
[2]);
406 if (w_size
< DEFLATE_MIN_SIZE
|| w_size
> DEFLATE_MAX_SIZE
)
409 MALLOC(state
, struct deflate_state
*, sizeof(struct deflate_state
),
414 state
->strm
.next_out
= NULL
;
415 state
->strm
.zalloc
= z_alloc
;
416 state
->strm
.zfree
= z_free
;
417 if (inflateInit2(&state
->strm
, -w_size
) != Z_OK
) {
418 FREE(state
, M_DEVBUF
);
422 state
->w_size
= w_size
;
423 bzero(&state
->stats
, sizeof(state
->stats
));
424 return (void *) state
;
431 struct deflate_state
*state
= (struct deflate_state
*) arg
;
433 inflateEnd(&state
->strm
);
434 FREE(state
, M_DEVBUF
);
438 z_decomp_init(arg
, options
, opt_len
, unit
, hdrlen
, mru
, debug
)
441 int opt_len
, unit
, hdrlen
, mru
, debug
;
443 struct deflate_state
*state
= (struct deflate_state
*) arg
;
445 if (opt_len
< CILEN_DEFLATE
446 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
447 || options
[1] != CILEN_DEFLATE
448 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
449 || DEFLATE_SIZE(options
[2]) != state
->w_size
450 || options
[3] != DEFLATE_CHK_SEQUENCE
)
455 state
->hdrlen
= hdrlen
;
456 state
->debug
= debug
;
459 inflateReset(&state
->strm
);
468 struct deflate_state
*state
= (struct deflate_state
*) arg
;
471 inflateReset(&state
->strm
);
475 * Decompress a Deflate-compressed packet.
477 * Because of patent problems, we return DECOMP_ERROR for errors
478 * found by inspecting the input data and for system problems, but
479 * DECOMP_FATALERROR for any errors which could possibly be said to
480 * be being detected "after" decompression. For DECOMP_ERROR,
481 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
482 * infringing a patent of Motorola's if we do, so we take CCP down
485 * Given that the frame has the correct sequence number and a good FCS,
486 * errors such as invalid codes in the input most likely indicate a
487 * bug, so we return DECOMP_FATALERROR for them in order to turn off
488 * compression, even though they are detected by inspecting the input.
491 z_decompress(arg
, mi
, mop
)
493 struct mbuf
*mi
, **mop
;
495 struct deflate_state
*state
= (struct deflate_state
*) arg
;
496 struct mbuf
*mo
, *mo_head
;
498 int rlen
, olen
, ospace
;
499 int seq
, i
, flush
, r
, decode_proto
;
500 u_char hdr
[PPP_HDRLEN
+ DEFLATE_OVHD
];
503 rptr
= mtod(mi
, u_char
*);
505 for (i
= 0; i
< PPP_HDRLEN
+ DEFLATE_OVHD
; ++i
) {
510 rptr
= mtod(mi
, u_char
*);
517 /* Check the sequence number. */
518 seq
= (hdr
[PPP_HDRLEN
] << 8) + hdr
[PPP_HDRLEN
+1];
519 if (seq
!= state
->seqno
) {
521 printf("z_decompress%d: bad seq # %d, expected %d\n",
522 state
->unit
, seq
, state
->seqno
);
527 /* Allocate an output mbuf. */
528 MGETHDR(mo
, M_DONTWAIT
, MT_DATA
);
534 MCLGET(mo
, M_DONTWAIT
);
535 ospace
= M_TRAILINGSPACE(mo
);
536 if (state
->hdrlen
+ PPP_HDRLEN
< ospace
) {
537 mo
->m_data
+= state
->hdrlen
;
538 ospace
-= state
->hdrlen
;
542 * Fill in the first part of the PPP header. The protocol field
543 * comes from the decompressed data.
545 wptr
= mtod(mo
, u_char
*);
546 wptr
[0] = PPP_ADDRESS(hdr
);
547 wptr
[1] = PPP_CONTROL(hdr
);
551 * Set up to call inflate. We set avail_out to 1 initially so we can
552 * look at the first byte of the output and decide whether we have
553 * a 1-byte or 2-byte protocol field.
555 state
->strm
.next_in
= rptr
;
556 state
->strm
.avail_in
= rlen
;
558 flush
= (mi
== NULL
)? Z_PACKET_FLUSH
: Z_NO_FLUSH
;
559 rlen
+= PPP_HDRLEN
+ DEFLATE_OVHD
;
560 state
->strm
.next_out
= wptr
+ 3;
561 state
->strm
.avail_out
= 1;
566 * Call inflate, supplying more input or output as needed.
569 r
= inflate(&state
->strm
, flush
);
574 printf("z_decompress%d: inflate returned %d (%s)\n",
575 state
->unit
, r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
577 return DECOMP_FATALERROR
;
579 if (flush
!= Z_NO_FLUSH
&& state
->strm
.avail_out
!= 0)
580 break; /* all done */
581 if (state
->strm
.avail_in
== 0 && mi
!= NULL
) {
582 state
->strm
.next_in
= mtod(mi
, u_char
*);
583 state
->strm
.avail_in
= mi
->m_len
;
587 flush
= Z_PACKET_FLUSH
;
589 if (state
->strm
.avail_out
== 0) {
591 state
->strm
.avail_out
= ospace
- PPP_HDRLEN
;
592 if ((wptr
[3] & 1) == 0) {
593 /* 2-byte protocol field */
595 --state
->strm
.next_out
;
596 ++state
->strm
.avail_out
;
603 MGET(mo
->m_next
, M_DONTWAIT
, MT_DATA
);
609 MCLGET(mo
, M_DONTWAIT
);
610 state
->strm
.next_out
= mtod(mo
, u_char
*);
611 state
->strm
.avail_out
= ospace
= M_TRAILINGSPACE(mo
);
619 olen
+= (mo
->m_len
= ospace
- state
->strm
.avail_out
);
621 if (state
->debug
&& olen
> state
->mru
+ PPP_HDRLEN
)
622 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
623 state
->unit
, olen
, state
->mru
+ PPP_HDRLEN
);
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
);
677 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
678 state
->unit
, r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
684 state
->strm
.next_in
= mtod(mi
, u_char
*);
685 state
->strm
.avail_in
= mi
->m_len
;
692 state
->stats
.inc_bytes
+= rlen
;
693 state
->stats
.inc_packets
++;
694 state
->stats
.unc_bytes
+= rlen
;
695 state
->stats
.unc_packets
++;
698 #endif /* DO_DEFLATE */