diff options
Diffstat (limited to '')
| -rw-r--r-- | inflate.c | 72 |
1 files changed, 45 insertions, 27 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* inflate.c -- zlib interface to inflate modules | 1 | /* inflate.c -- zlib interface to inflate modules |
| 2 | * Copyright (C) 1995-1996 Mark Adler | 2 | * Copyright (C) 1995-1998 Mark Adler |
| 3 | * For conditions of distribution and use, see copyright notice in zlib.h | 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| @@ -8,11 +8,7 @@ | |||
| 8 | 8 | ||
| 9 | struct inflate_blocks_state {int dummy;}; /* for buggy compilers */ | 9 | struct inflate_blocks_state {int dummy;}; /* for buggy compilers */ |
| 10 | 10 | ||
| 11 | /* inflate private state */ | 11 | typedef enum { |
| 12 | struct internal_state { | ||
| 13 | |||
| 14 | /* mode */ | ||
| 15 | enum { | ||
| 16 | METHOD, /* waiting for method byte */ | 12 | METHOD, /* waiting for method byte */ |
| 17 | FLAG, /* waiting for flag byte */ | 13 | FLAG, /* waiting for flag byte */ |
| 18 | DICT4, /* four dictionary check bytes to go */ | 14 | DICT4, /* four dictionary check bytes to go */ |
| @@ -27,7 +23,13 @@ struct internal_state { | |||
| 27 | CHECK1, /* one check byte to go */ | 23 | CHECK1, /* one check byte to go */ |
| 28 | DONE, /* finished check, done */ | 24 | DONE, /* finished check, done */ |
| 29 | BAD} /* got an error--stay here */ | 25 | BAD} /* got an error--stay here */ |
| 30 | mode; /* current inflate mode */ | 26 | inflate_mode; |
| 27 | |||
| 28 | /* inflate private state */ | ||
| 29 | struct internal_state { | ||
| 30 | |||
| 31 | /* mode */ | ||
| 32 | inflate_mode mode; /* current inflate mode */ | ||
| 31 | 33 | ||
| 32 | /* mode dependent information */ | 34 | /* mode dependent information */ |
| 33 | union { | 35 | union { |
| @@ -48,39 +50,35 @@ struct internal_state { | |||
| 48 | }; | 50 | }; |
| 49 | 51 | ||
| 50 | 52 | ||
| 51 | int inflateReset(z) | 53 | int EXPORT inflateReset(z) |
| 52 | z_streamp z; | 54 | z_streamp z; |
| 53 | { | 55 | { |
| 54 | uLong c; | ||
| 55 | |||
| 56 | if (z == Z_NULL || z->state == Z_NULL) | 56 | if (z == Z_NULL || z->state == Z_NULL) |
| 57 | return Z_STREAM_ERROR; | 57 | return Z_STREAM_ERROR; |
| 58 | z->total_in = z->total_out = 0; | 58 | z->total_in = z->total_out = 0; |
| 59 | z->msg = Z_NULL; | 59 | z->msg = Z_NULL; |
| 60 | z->state->mode = z->state->nowrap ? BLOCKS : METHOD; | 60 | z->state->mode = z->state->nowrap ? BLOCKS : METHOD; |
| 61 | inflate_blocks_reset(z->state->blocks, z, &c); | 61 | inflate_blocks_reset(z->state->blocks, z, Z_NULL); |
| 62 | Trace((stderr, "inflate: reset\n")); | 62 | Tracev((stderr, "inflate: reset\n")); |
| 63 | return Z_OK; | 63 | return Z_OK; |
| 64 | } | 64 | } |
| 65 | 65 | ||
| 66 | 66 | ||
| 67 | int inflateEnd(z) | 67 | int EXPORT inflateEnd(z) |
| 68 | z_streamp z; | 68 | z_streamp z; |
| 69 | { | 69 | { |
| 70 | uLong c; | ||
| 71 | |||
| 72 | if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL) | 70 | if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL) |
| 73 | return Z_STREAM_ERROR; | 71 | return Z_STREAM_ERROR; |
| 74 | if (z->state->blocks != Z_NULL) | 72 | if (z->state->blocks != Z_NULL) |
| 75 | inflate_blocks_free(z->state->blocks, z, &c); | 73 | inflate_blocks_free(z->state->blocks, z); |
| 76 | ZFREE(z, z->state); | 74 | ZFREE(z, z->state); |
| 77 | z->state = Z_NULL; | 75 | z->state = Z_NULL; |
| 78 | Trace((stderr, "inflate: end\n")); | 76 | Tracev((stderr, "inflate: end\n")); |
| 79 | return Z_OK; | 77 | return Z_OK; |
| 80 | } | 78 | } |
| 81 | 79 | ||
| 82 | 80 | ||
| 83 | int inflateInit2_(z, w, version, stream_size) | 81 | int EXPORT inflateInit2_(z, w, version, stream_size) |
| 84 | z_streamp z; | 82 | z_streamp z; |
| 85 | int w; | 83 | int w; |
| 86 | const char *version; | 84 | const char *version; |
| @@ -129,7 +127,7 @@ int stream_size; | |||
| 129 | inflateEnd(z); | 127 | inflateEnd(z); |
| 130 | return Z_MEM_ERROR; | 128 | return Z_MEM_ERROR; |
| 131 | } | 129 | } |
| 132 | Trace((stderr, "inflate: allocated\n")); | 130 | Tracev((stderr, "inflate: allocated\n")); |
| 133 | 131 | ||
| 134 | /* reset state */ | 132 | /* reset state */ |
| 135 | inflateReset(z); | 133 | inflateReset(z); |
| @@ -137,7 +135,7 @@ int stream_size; | |||
| 137 | } | 135 | } |
| 138 | 136 | ||
| 139 | 137 | ||
| 140 | int inflateInit_(z, version, stream_size) | 138 | int EXPORT inflateInit_(z, version, stream_size) |
| 141 | z_streamp z; | 139 | z_streamp z; |
| 142 | const char *version; | 140 | const char *version; |
| 143 | int stream_size; | 141 | int stream_size; |
| @@ -146,10 +144,10 @@ int stream_size; | |||
| 146 | } | 144 | } |
| 147 | 145 | ||
| 148 | 146 | ||
| 149 | #define NEEDBYTE {if(z->avail_in==0)return r;r=Z_OK;} | 147 | #define NEEDBYTE {if(z->avail_in==0)return r; if (f != Z_FINISH) r = Z_OK;} |
| 150 | #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++) | 148 | #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++) |
| 151 | 149 | ||
| 152 | int inflate(z, f) | 150 | int EXPORT inflate(z, f) |
| 153 | z_streamp z; | 151 | z_streamp z; |
| 154 | int f; | 152 | int f; |
| 155 | { | 153 | { |
| @@ -188,7 +186,7 @@ int f; | |||
| 188 | z->state->sub.marker = 5; /* can't try inflateSync */ | 186 | z->state->sub.marker = 5; /* can't try inflateSync */ |
| 189 | break; | 187 | break; |
| 190 | } | 188 | } |
| 191 | Trace((stderr, "inflate: zlib header ok\n")); | 189 | Tracev((stderr, "inflate: zlib header ok\n")); |
| 192 | if (!(b & PRESET_DICT)) | 190 | if (!(b & PRESET_DICT)) |
| 193 | { | 191 | { |
| 194 | z->state->mode = BLOCKS; | 192 | z->state->mode = BLOCKS; |
| @@ -227,7 +225,7 @@ int f; | |||
| 227 | break; | 225 | break; |
| 228 | } | 226 | } |
| 229 | if (r != Z_STREAM_END) | 227 | if (r != Z_STREAM_END) |
| 230 | return r; | 228 | return f == Z_FINISH && r == Z_OK ? Z_BUF_ERROR : r; |
| 231 | r = Z_OK; | 229 | r = Z_OK; |
| 232 | inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was); | 230 | inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was); |
| 233 | if (z->state->nowrap) | 231 | if (z->state->nowrap) |
| @@ -236,6 +234,7 @@ int f; | |||
| 236 | break; | 234 | break; |
| 237 | } | 235 | } |
| 238 | z->state->mode = CHECK4; | 236 | z->state->mode = CHECK4; |
| 237 | if (f == Z_FINISH) r = Z_BUF_ERROR; | ||
| 239 | case CHECK4: | 238 | case CHECK4: |
| 240 | NEEDBYTE | 239 | NEEDBYTE |
| 241 | z->state->sub.check.need = (uLong)NEXTBYTE << 24; | 240 | z->state->sub.check.need = (uLong)NEXTBYTE << 24; |
| @@ -259,7 +258,7 @@ int f; | |||
| 259 | z->state->sub.marker = 5; /* can't try inflateSync */ | 258 | z->state->sub.marker = 5; /* can't try inflateSync */ |
| 260 | break; | 259 | break; |
| 261 | } | 260 | } |
| 262 | Trace((stderr, "inflate: zlib check ok\n")); | 261 | Tracev((stderr, "inflate: zlib check ok\n")); |
| 263 | z->state->mode = DONE; | 262 | z->state->mode = DONE; |
| 264 | case DONE: | 263 | case DONE: |
| 265 | return Z_STREAM_END; | 264 | return Z_STREAM_END; |
| @@ -268,10 +267,13 @@ int f; | |||
| 268 | default: | 267 | default: |
| 269 | return Z_STREAM_ERROR; | 268 | return Z_STREAM_ERROR; |
| 270 | } | 269 | } |
| 270 | #ifdef NEED_DUMMY_RETURN | ||
| 271 | return Z_STREAM_ERROR; /* Some dumb compilers complain without this */ | ||
| 272 | #endif | ||
| 271 | } | 273 | } |
| 272 | 274 | ||
| 273 | 275 | ||
| 274 | int inflateSetDictionary(z, dictionary, dictLength) | 276 | int EXPORT inflateSetDictionary(z, dictionary, dictLength) |
| 275 | z_streamp z; | 277 | z_streamp z; |
| 276 | const Bytef *dictionary; | 278 | const Bytef *dictionary; |
| 277 | uInt dictLength; | 279 | uInt dictLength; |
| @@ -295,7 +297,7 @@ uInt dictLength; | |||
| 295 | } | 297 | } |
| 296 | 298 | ||
| 297 | 299 | ||
| 298 | int inflateSync(z) | 300 | int EXPORT inflateSync(z) |
| 299 | z_streamp z; | 301 | z_streamp z; |
| 300 | { | 302 | { |
| 301 | uInt n; /* number of bytes to look at */ | 303 | uInt n; /* number of bytes to look at */ |
| @@ -343,3 +345,19 @@ z_streamp z; | |||
| 343 | z->state->mode = BLOCKS; | 345 | z->state->mode = BLOCKS; |
| 344 | return Z_OK; | 346 | return Z_OK; |
| 345 | } | 347 | } |
| 348 | |||
| 349 | |||
| 350 | /* Returns true if inflate is currently at the end of a block generated | ||
| 351 | * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP | ||
| 352 | * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH | ||
| 353 | * but removes the length bytes of the resulting empty stored block. When | ||
| 354 | * decompressing, PPP checks that at the end of input packet, inflate is | ||
| 355 | * waiting for these length bytes. | ||
| 356 | */ | ||
| 357 | int EXPORT inflateSyncPoint(z) | ||
| 358 | z_streamp z; | ||
| 359 | { | ||
| 360 | if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL) | ||
| 361 | return Z_STREAM_ERROR; | ||
| 362 | return inflate_blocks_sync_point(z->state->blocks); | ||
| 363 | } | ||
