lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ppp_deflate.c - interface the zlib procedures for Deflate compression |
| 3 | * and decompression (as used by gzip) to the PPP code. |
| 4 | * |
| 5 | * Copyright (c) 1994 Paul Mackerras. All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * |
| 19 | * 3. The name(s) of the authors of this software must not be used to |
| 20 | * endorse or promote products derived from this software without |
| 21 | * prior written permission. |
| 22 | * |
| 23 | * 4. Redistributions of any form whatsoever must retain the following |
| 24 | * acknowledgment: |
| 25 | * "This product includes software developed by Paul Mackerras |
| 26 | * <paulus@samba.org>". |
| 27 | * |
| 28 | * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO |
| 29 | * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 30 | * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
| 31 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 32 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN |
| 33 | * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
| 34 | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 35 | * |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame^] | 36 | * $Id: deflate.c,v 1.5 2004/01/17 05:47:55 carlsonj Exp $ |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 37 | */ |
| 38 | |
| 39 | #include <sys/types.h> |
| 40 | #include <stdio.h> |
| 41 | #include <stddef.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <string.h> |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame^] | 44 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 45 | #include "ppp-comp.h" |
| 46 | #include "zlib.h" |
| 47 | |
| 48 | #if DO_DEFLATE |
| 49 | |
| 50 | #define DEFLATE_DEBUG 1 |
| 51 | |
| 52 | /* |
| 53 | * State for a Deflate (de)compressor. |
| 54 | */ |
| 55 | struct deflate_state { |
| 56 | int seqno; |
| 57 | int w_size; |
| 58 | int unit; |
| 59 | int hdrlen; |
| 60 | int mru; |
| 61 | int debug; |
| 62 | z_stream strm; |
| 63 | struct compstat stats; |
| 64 | }; |
| 65 | |
| 66 | #define DEFLATE_OVHD 2 /* Deflate overhead/packet */ |
| 67 | |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame^] | 68 | static void *z_alloc(void *, u_int items, u_int size); |
| 69 | static void z_free(void *, void *ptr, u_int nb); |
| 70 | static void *z_decomp_alloc(u_char *options, int opt_len); |
| 71 | static void z_decomp_free(void *state); |
| 72 | static int z_decomp_init(void *state, u_char *options, int opt_len, |
| 73 | int unit, int hdrlen, int mru, int debug); |
| 74 | static void z_incomp(void *state, u_char *dmsg, int len); |
| 75 | static int z_decompress(void *state, u_char *cmp, int inlen, |
| 76 | u_char *dmp, int *outlenp); |
| 77 | static void z_decomp_reset(void *state); |
| 78 | static void z_comp_stats(void *state, struct compstat *stats); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 79 | |
| 80 | /* |
| 81 | * Procedures exported to if_ppp.c. |
| 82 | */ |
| 83 | struct compressor ppp_deflate = { |
| 84 | CI_DEFLATE, /* compress_proto */ |
| 85 | z_decomp_alloc, /* decomp_alloc */ |
| 86 | z_decomp_free, /* decomp_free */ |
| 87 | z_decomp_init, /* decomp_init */ |
| 88 | z_decomp_reset, /* decomp_reset */ |
| 89 | z_decompress, /* decompress */ |
| 90 | z_incomp, /* incomp */ |
| 91 | z_comp_stats, /* decomp_stat */ |
| 92 | }; |
| 93 | |
| 94 | /* |
| 95 | * Space allocation and freeing routines for use by zlib routines. |
| 96 | */ |
| 97 | static void * |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame^] | 98 | z_alloc(void *notused, u_int items, u_int size) |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 99 | { |
| 100 | return malloc(items * size); |
| 101 | } |
| 102 | |
| 103 | static void |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame^] | 104 | z_free(void *notused, void *ptr, u_int nbytes) |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 105 | { |
| 106 | free(ptr); |
| 107 | } |
| 108 | |
| 109 | static void |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame^] | 110 | z_comp_stats(void *arg, struct compstat *stats) |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 111 | { |
| 112 | struct deflate_state *state = (struct deflate_state *) arg; |
| 113 | u_int out; |
| 114 | |
| 115 | *stats = state->stats; |
| 116 | stats->ratio = stats->unc_bytes; |
| 117 | out = stats->comp_bytes + stats->unc_bytes; |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame^] | 118 | u_int ratio = stats->ratio; |
| 119 | if (ratio <= 0x7ffffff) |
| 120 | ratio <<= 8; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 121 | else |
| 122 | out >>= 8; |
| 123 | if (out != 0) |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame^] | 124 | stats->ratio = ratio / out; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Allocate space for a decompressor. |
| 129 | */ |
| 130 | static void * |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame^] | 131 | z_decomp_alloc(u_char *options, int opt_len) |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 132 | { |
| 133 | struct deflate_state *state; |
| 134 | int w_size; |
| 135 | |
| 136 | if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE |
| 137 | || options[1] != CILEN_DEFLATE |
| 138 | || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL |
| 139 | || options[3] != DEFLATE_CHK_SEQUENCE) |
| 140 | return NULL; |
| 141 | w_size = DEFLATE_SIZE(options[2]); |
| 142 | if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE) |
| 143 | return NULL; |
| 144 | |
| 145 | state = (struct deflate_state *) malloc(sizeof(*state)); |
| 146 | if (state == NULL) |
| 147 | return NULL; |
| 148 | |
| 149 | state->strm.next_out = NULL; |
| 150 | state->strm.zalloc = (alloc_func) z_alloc; |
| 151 | state->strm.zfree = (free_func) z_free; |
| 152 | if (inflateInit2(&state->strm, -w_size) != Z_OK) { |
| 153 | free(state); |
| 154 | return NULL; |
| 155 | } |
| 156 | |
| 157 | state->w_size = w_size; |
| 158 | memset(&state->stats, 0, sizeof(state->stats)); |
| 159 | return (void *) state; |
| 160 | } |
| 161 | |
| 162 | static void |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame^] | 163 | z_decomp_free(void *arg) |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 164 | { |
| 165 | struct deflate_state *state = (struct deflate_state *) arg; |
| 166 | |
| 167 | inflateEnd(&state->strm); |
| 168 | free(state); |
| 169 | } |
| 170 | |
| 171 | static int |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame^] | 172 | z_decomp_init(void *arg, u_char *options, int opt_len, |
| 173 | int unit, int hdrlen, int mru, int debug) |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 174 | { |
| 175 | struct deflate_state *state = (struct deflate_state *) arg; |
| 176 | |
| 177 | if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE |
| 178 | || options[1] != CILEN_DEFLATE |
| 179 | || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL |
| 180 | || DEFLATE_SIZE(options[2]) != state->w_size |
| 181 | || options[3] != DEFLATE_CHK_SEQUENCE) |
| 182 | return 0; |
| 183 | |
| 184 | state->seqno = 0; |
| 185 | state->unit = unit; |
| 186 | state->hdrlen = hdrlen; |
| 187 | state->debug = debug; |
| 188 | state->mru = mru; |
| 189 | |
| 190 | inflateReset(&state->strm); |
| 191 | |
| 192 | return 1; |
| 193 | } |
| 194 | |
| 195 | static void |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame^] | 196 | z_decomp_reset(void *arg) |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 197 | { |
| 198 | struct deflate_state *state = (struct deflate_state *) arg; |
| 199 | |
| 200 | state->seqno = 0; |
| 201 | inflateReset(&state->strm); |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Decompress a Deflate-compressed packet. |
| 206 | * |
| 207 | * Because of patent problems, we return DECOMP_ERROR for errors |
| 208 | * found by inspecting the input data and for system problems, but |
| 209 | * DECOMP_FATALERROR for any errors which could possibly be said to |
| 210 | * be being detected "after" decompression. For DECOMP_ERROR, |
| 211 | * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be |
| 212 | * infringing a patent of Motorola's if we do, so we take CCP down |
| 213 | * instead. |
| 214 | * |
| 215 | * Given that the frame has the correct sequence number and a good FCS, |
| 216 | * errors such as invalid codes in the input most likely indicate a |
| 217 | * bug, so we return DECOMP_FATALERROR for them in order to turn off |
| 218 | * compression, even though they are detected by inspecting the input. |
| 219 | */ |
| 220 | static int |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame^] | 221 | z_decompress(void *arg, u_char *mi, int inlen, u_char *mo, int *outlenp) |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 222 | { |
| 223 | struct deflate_state *state = (struct deflate_state *) arg; |
| 224 | u_char *rptr, *wptr; |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame^] | 225 | int rlen, olen; |
| 226 | int seq, r; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 227 | |
| 228 | rptr = mi; |
| 229 | if (*rptr == 0) |
| 230 | ++rptr; |
| 231 | ++rptr; |
| 232 | |
| 233 | /* Check the sequence number. */ |
| 234 | seq = (rptr[0] << 8) + rptr[1]; |
| 235 | rptr += 2; |
| 236 | if (seq != state->seqno) { |
| 237 | #if !DEFLATE_DEBUG |
| 238 | if (state->debug) |
| 239 | #endif |
| 240 | printf("z_decompress%d: bad seq # %d, expected %d\n", |
| 241 | state->unit, seq, state->seqno); |
| 242 | return DECOMP_ERROR; |
| 243 | } |
| 244 | ++state->seqno; |
| 245 | |
| 246 | /* |
| 247 | * Set up to call inflate. |
| 248 | */ |
| 249 | wptr = mo; |
| 250 | state->strm.next_in = rptr; |
| 251 | state->strm.avail_in = mi + inlen - rptr; |
| 252 | rlen = state->strm.avail_in + PPP_HDRLEN + DEFLATE_OVHD; |
| 253 | state->strm.next_out = wptr; |
| 254 | state->strm.avail_out = state->mru + 2; |
| 255 | |
| 256 | r = inflate(&state->strm, Z_PACKET_FLUSH); |
| 257 | if (r != Z_OK) { |
| 258 | #if !DEFLATE_DEBUG |
| 259 | if (state->debug) |
| 260 | #endif |
| 261 | printf("z_decompress%d: inflate returned %d (%s)\n", |
| 262 | state->unit, r, (state->strm.msg? state->strm.msg: "")); |
| 263 | return DECOMP_FATALERROR; |
| 264 | } |
| 265 | olen = state->mru + 2 - state->strm.avail_out; |
| 266 | *outlenp = olen; |
| 267 | |
| 268 | if ((wptr[0] & 1) != 0) |
| 269 | ++olen; /* for suppressed protocol high byte */ |
| 270 | olen += 2; /* for address, control */ |
| 271 | |
| 272 | #if DEFLATE_DEBUG |
| 273 | if (olen > state->mru + PPP_HDRLEN) |
| 274 | printf("ppp_deflate%d: exceeded mru (%d > %d)\n", |
| 275 | state->unit, olen, state->mru + PPP_HDRLEN); |
| 276 | #endif |
| 277 | |
| 278 | state->stats.unc_bytes += olen; |
| 279 | state->stats.unc_packets++; |
| 280 | state->stats.comp_bytes += rlen; |
| 281 | state->stats.comp_packets++; |
| 282 | |
| 283 | return DECOMP_OK; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * Incompressible data has arrived - add it to the history. |
| 288 | */ |
| 289 | static void |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame^] | 290 | z_incomp(void *arg, u_char *mi, int mlen) |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 291 | { |
| 292 | struct deflate_state *state = (struct deflate_state *) arg; |
| 293 | u_char *rptr; |
| 294 | int rlen, proto, r; |
| 295 | |
| 296 | /* |
| 297 | * Check that the protocol is one we handle. |
| 298 | */ |
| 299 | rptr = mi; |
| 300 | proto = rptr[0]; |
| 301 | if ((proto & 1) == 0) |
| 302 | proto = (proto << 8) + rptr[1]; |
| 303 | if (proto > 0x3fff || proto == 0xfd || proto == 0xfb) |
| 304 | return; |
| 305 | |
| 306 | ++state->seqno; |
| 307 | |
| 308 | if (rptr[0] == 0) |
| 309 | ++rptr; |
| 310 | rlen = mi + mlen - rptr; |
| 311 | state->strm.next_in = rptr; |
| 312 | state->strm.avail_in = rlen; |
| 313 | r = inflateIncomp(&state->strm); |
| 314 | if (r != Z_OK) { |
| 315 | /* gak! */ |
| 316 | #if !DEFLATE_DEBUG |
| 317 | if (state->debug) |
| 318 | #endif |
| 319 | printf("z_incomp%d: inflateIncomp returned %d (%s)\n", |
| 320 | state->unit, r, (state->strm.msg? state->strm.msg: "")); |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * Update stats. |
| 326 | */ |
| 327 | if (proto <= 0xff) |
| 328 | ++rlen; |
| 329 | rlen += 2; |
| 330 | state->stats.inc_bytes += rlen; |
| 331 | state->stats.inc_packets++; |
| 332 | state->stats.unc_bytes += rlen; |
| 333 | state->stats.unc_packets++; |
| 334 | } |
| 335 | |
| 336 | #endif /* DO_DEFLATE */ |