| 1 | /* inflate.c -- zlib interface to inflate modules |
| 2 | * Copyright (C) 1995-1998 Mark Adler |
| 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
| 4 | */ |
| 5 | |
| 6 | #include "zutil.h" |
| 7 | #include "infblock.h" |
| 8 | |
| 9 | struct inflate_blocks_state {int dummy;}; /* for buggy compilers */ |
| 10 | |
| 11 | typedef enum { |
| 12 | METHOD, /* waiting for method byte */ |
| 13 | FLAG, /* waiting for flag byte */ |
| 14 | DICT4, /* four dictionary check bytes to go */ |
| 15 | DICT3, /* three dictionary check bytes to go */ |
| 16 | DICT2, /* two dictionary check bytes to go */ |
| 17 | DICT1, /* one dictionary check byte to go */ |
| 18 | DICT0, /* waiting for inflateSetDictionary */ |
| 19 | BLOCKS, /* decompressing blocks */ |
| 20 | CHECK4, /* four check bytes to go */ |
| 21 | CHECK3, /* three check bytes to go */ |
| 22 | CHECK2, /* two check bytes to go */ |
| 23 | CHECK1, /* one check byte to go */ |
| 24 | DONE, /* finished check, done */ |
| 25 | BAD} /* got an error--stay here */ |
| 26 | inflate_mode; |
| 27 | |
| 28 | /* inflate private state */ |
| 29 | struct internal_state { |
| 30 | |
| 31 | /* mode */ |
| 32 | inflate_mode mode; /* current inflate mode */ |
| 33 | |
| 34 | /* mode dependent information */ |
| 35 | union { |
| 36 | uInt method; /* if FLAGS, method byte */ |
| 37 | struct { |
| 38 | uLong was; /* computed check value */ |
| 39 | uLong need; /* stream check value */ |
| 40 | } check; /* if CHECK, check values to compare */ |
| 41 | uInt marker; /* if BAD, inflateSync's marker bytes count */ |
| 42 | } sub; /* submode */ |
| 43 | |
| 44 | /* mode independent information */ |
| 45 | int nowrap; /* flag for no wrapper */ |
| 46 | uInt wbits; /* log2(window size) (8..15, defaults to 15) */ |
| 47 | inflate_blocks_statef |
| 48 | *blocks; /* current inflate_blocks state */ |
| 49 | |
| 50 | }; |
| 51 | |
| 52 | |
| 53 | #if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ |
| 54 | int ZEXPORT inflateReset(z_streamp z) |
| 55 | #else |
| 56 | int ZEXPORT inflateReset(z) |
| 57 | z_streamp z; |
| 58 | #endif |
| 59 | { |
| 60 | if (z == Z_NULL || z->state == Z_NULL) |
| 61 | return Z_STREAM_ERROR; |
| 62 | z->total_in = z->total_out = 0; |
| 63 | z->msg = Z_NULL; |
| 64 | z->state->mode = z->state->nowrap ? BLOCKS : METHOD; |
| 65 | inflate_blocks_reset(z->state->blocks, z, Z_NULL); |
| 66 | Tracev((stderr, "inflate: reset\n")); |
| 67 | return Z_OK; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | #if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ |
| 72 | int ZEXPORT inflateEnd(z_streamp z) |
| 73 | #else |
| 74 | int ZEXPORT inflateEnd(z) |
| 75 | z_streamp z; |
| 76 | #endif |
| 77 | { |
| 78 | if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL) |
| 79 | return Z_STREAM_ERROR; |
| 80 | if (z->state->blocks != Z_NULL) |
| 81 | inflate_blocks_free(z->state->blocks, z); |
| 82 | ZFREE(z, z->state); |
| 83 | z->state = Z_NULL; |
| 84 | Tracev((stderr, "inflate: end\n")); |
| 85 | return Z_OK; |
| 86 | } |
| 87 | |
| 88 | #if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ |
| 89 | int ZEXPORT inflateInit2_(z_streamp z, int w, const char* version, int stream_size) |
| 90 | #else |
| 91 | int ZEXPORT inflateInit2_(z, w, version, stream_size) |
| 92 | z_streamp z; |
| 93 | int w; |
| 94 | const char *version; |
| 95 | int stream_size; |
| 96 | #endif |
| 97 | { |
| 98 | if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || |
| 99 | stream_size != sizeof(z_stream)) |
| 100 | return Z_VERSION_ERROR; |
| 101 | |
| 102 | /* initialize state */ |
| 103 | if (z == Z_NULL) |
| 104 | return Z_STREAM_ERROR; |
| 105 | z->msg = Z_NULL; |
| 106 | if (z->zalloc == Z_NULL) |
| 107 | { |
| 108 | z->zalloc = zcalloc; |
| 109 | z->opaque = (voidpf)0; |
| 110 | } |
| 111 | if (z->zfree == Z_NULL) z->zfree = zcfree; |
| 112 | if ((z->state = (struct internal_state FAR *) |
| 113 | ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL) |
| 114 | return Z_MEM_ERROR; |
| 115 | z->state->blocks = Z_NULL; |
| 116 | |
| 117 | /* handle undocumented nowrap option (no zlib header or check) */ |
| 118 | z->state->nowrap = 0; |
| 119 | if (w < 0) |
| 120 | { |
| 121 | w = - w; |
| 122 | z->state->nowrap = 1; |
| 123 | } |
| 124 | |
| 125 | /* set window size */ |
| 126 | if (w < 8 || w > 15) |
| 127 | { |
| 128 | inflateEnd(z); |
| 129 | return Z_STREAM_ERROR; |
| 130 | } |
| 131 | z->state->wbits = (uInt)w; |
| 132 | |
| 133 | /* create inflate_blocks state */ |
| 134 | if ((z->state->blocks = |
| 135 | inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w)) |
| 136 | == Z_NULL) |
| 137 | { |
| 138 | inflateEnd(z); |
| 139 | return Z_MEM_ERROR; |
| 140 | } |
| 141 | Tracev((stderr, "inflate: allocated\n")); |
| 142 | |
| 143 | /* reset state */ |
| 144 | inflateReset(z); |
| 145 | return Z_OK; |
| 146 | } |
| 147 | |
| 148 | #if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ |
| 149 | int ZEXPORT inflateInit_(z_streamp z, const char* version, int stream_size) |
| 150 | #else |
| 151 | int ZEXPORT inflateInit_(z, version, stream_size) |
| 152 | z_streamp z; |
| 153 | const char *version; |
| 154 | int stream_size; |
| 155 | #endif |
| 156 | { |
| 157 | return inflateInit2_(z, DEF_WBITS, version, stream_size); |
| 158 | } |
| 159 | |
| 160 | #define NEEDBYTE {if(z->avail_in==0)return r;r=f;} |
| 161 | #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++) |
| 162 | |
| 163 | #if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ |
| 164 | int ZEXPORT inflate(z_streamp z, int f) |
| 165 | #else |
| 166 | int ZEXPORT inflate(z, f) |
| 167 | z_streamp z; |
| 168 | int f; |
| 169 | #endif |
| 170 | { |
| 171 | int r; |
| 172 | uInt b; |
| 173 | |
| 174 | if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL) |
| 175 | return Z_STREAM_ERROR; |
| 176 | f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK; |
| 177 | r = Z_BUF_ERROR; |
| 178 | while (1) switch (z->state->mode) |
| 179 | { |
| 180 | case METHOD: |
| 181 | NEEDBYTE |
| 182 | if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED) |
| 183 | { |
| 184 | z->state->mode = BAD; |
| 185 | z->msg = (char*)"unknown compression method"; |
| 186 | z->state->sub.marker = 5; /* can't try inflateSync */ |
| 187 | break; |
| 188 | } |
| 189 | if ((z->state->sub.method >> 4) + 8 > z->state->wbits) |
| 190 | { |
| 191 | z->state->mode = BAD; |
| 192 | z->msg = (char*)"invalid window size"; |
| 193 | z->state->sub.marker = 5; /* can't try inflateSync */ |
| 194 | break; |
| 195 | } |
| 196 | z->state->mode = FLAG; |
| 197 | case FLAG: |
| 198 | NEEDBYTE |
| 199 | b = NEXTBYTE; |
| 200 | if (((z->state->sub.method << 8) + b) % 31) |
| 201 | { |
| 202 | z->state->mode = BAD; |
| 203 | z->msg = (char*)"incorrect header check"; |
| 204 | z->state->sub.marker = 5; /* can't try inflateSync */ |
| 205 | break; |
| 206 | } |
| 207 | Tracev((stderr, "inflate: zlib header ok\n")); |
| 208 | if (!(b & PRESET_DICT)) |
| 209 | { |
| 210 | z->state->mode = BLOCKS; |
| 211 | break; |
| 212 | } |
| 213 | z->state->mode = DICT4; |
| 214 | case DICT4: |
| 215 | NEEDBYTE |
| 216 | z->state->sub.check.need = (uLong)NEXTBYTE << 24; |
| 217 | z->state->mode = DICT3; |
| 218 | case DICT3: |
| 219 | NEEDBYTE |
| 220 | z->state->sub.check.need += (uLong)NEXTBYTE << 16; |
| 221 | z->state->mode = DICT2; |
| 222 | case DICT2: |
| 223 | NEEDBYTE |
| 224 | z->state->sub.check.need += (uLong)NEXTBYTE << 8; |
| 225 | z->state->mode = DICT1; |
| 226 | case DICT1: |
| 227 | NEEDBYTE |
| 228 | z->state->sub.check.need += (uLong)NEXTBYTE; |
| 229 | z->adler = z->state->sub.check.need; |
| 230 | z->state->mode = DICT0; |
| 231 | return Z_NEED_DICT; |
| 232 | case DICT0: |
| 233 | z->state->mode = BAD; |
| 234 | z->msg = (char*)"need dictionary"; |
| 235 | z->state->sub.marker = 0; /* can try inflateSync */ |
| 236 | return Z_STREAM_ERROR; |
| 237 | case BLOCKS: |
| 238 | r = inflate_blocks(z->state->blocks, z, r); |
| 239 | if (r == Z_DATA_ERROR) |
| 240 | { |
| 241 | z->state->mode = BAD; |
| 242 | z->state->sub.marker = 0; /* can try inflateSync */ |
| 243 | break; |
| 244 | } |
| 245 | if (r == Z_OK) |
| 246 | r = f; |
| 247 | if (r != Z_STREAM_END) |
| 248 | return r; |
| 249 | r = f; |
| 250 | inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was); |
| 251 | if (z->state->nowrap) |
| 252 | { |
| 253 | z->state->mode = DONE; |
| 254 | break; |
| 255 | } |
| 256 | z->state->mode = CHECK4; |
| 257 | case CHECK4: |
| 258 | NEEDBYTE |
| 259 | z->state->sub.check.need = (uLong)NEXTBYTE << 24; |
| 260 | z->state->mode = CHECK3; |
| 261 | case CHECK3: |
| 262 | NEEDBYTE |
| 263 | z->state->sub.check.need += (uLong)NEXTBYTE << 16; |
| 264 | z->state->mode = CHECK2; |
| 265 | case CHECK2: |
| 266 | NEEDBYTE |
| 267 | z->state->sub.check.need += (uLong)NEXTBYTE << 8; |
| 268 | z->state->mode = CHECK1; |
| 269 | case CHECK1: |
| 270 | NEEDBYTE |
| 271 | z->state->sub.check.need += (uLong)NEXTBYTE; |
| 272 | |
| 273 | if (z->state->sub.check.was != z->state->sub.check.need) |
| 274 | { |
| 275 | z->state->mode = BAD; |
| 276 | z->msg = (char*)"incorrect data check"; |
| 277 | z->state->sub.marker = 5; /* can't try inflateSync */ |
| 278 | break; |
| 279 | } |
| 280 | Tracev((stderr, "inflate: zlib check ok\n")); |
| 281 | z->state->mode = DONE; |
| 282 | case DONE: |
| 283 | return Z_STREAM_END; |
| 284 | case BAD: |
| 285 | return Z_DATA_ERROR; |
| 286 | default: |
| 287 | return Z_STREAM_ERROR; |
| 288 | } |
| 289 | #ifdef NEED_DUMMY_RETURN |
| 290 | return Z_STREAM_ERROR; /* Some dumb compilers complain without this */ |
| 291 | #endif |
| 292 | } |
| 293 | |
| 294 | #if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ |
| 295 | int ZEXPORT inflateSetDictionary(z_streamp z, const Bytef* dictionary, uInt dictLength) |
| 296 | #else |
| 297 | int ZEXPORT inflateSetDictionary(z, dictionary, dictLength) |
| 298 | z_streamp z; |
| 299 | const Bytef *dictionary; |
| 300 | uInt dictLength; |
| 301 | #endif |
| 302 | { |
| 303 | uInt length = dictLength; |
| 304 | |
| 305 | if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0) |
| 306 | return Z_STREAM_ERROR; |
| 307 | |
| 308 | if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR; |
| 309 | z->adler = 1L; |
| 310 | |
| 311 | if (length >= ((uInt)1<<z->state->wbits)) |
| 312 | { |
| 313 | length = (1<<z->state->wbits)-1; |
| 314 | dictionary += dictLength - length; |
| 315 | } |
| 316 | inflate_set_dictionary(z->state->blocks, dictionary, length); |
| 317 | z->state->mode = BLOCKS; |
| 318 | return Z_OK; |
| 319 | } |
| 320 | |
| 321 | #if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ |
| 322 | int ZEXPORT inflateSync(z_streamp z) |
| 323 | #else |
| 324 | int ZEXPORT inflateSync(z) |
| 325 | z_streamp z; |
| 326 | #endif |
| 327 | { |
| 328 | uInt n; /* number of bytes to look at */ |
| 329 | Bytef *p; /* pointer to bytes */ |
| 330 | uInt m; /* number of marker bytes found in a row */ |
| 331 | uLong r, w; /* temporaries to save total_in and total_out */ |
| 332 | |
| 333 | /* set up */ |
| 334 | if (z == Z_NULL || z->state == Z_NULL) |
| 335 | return Z_STREAM_ERROR; |
| 336 | if (z->state->mode != BAD) |
| 337 | { |
| 338 | z->state->mode = BAD; |
| 339 | z->state->sub.marker = 0; |
| 340 | } |
| 341 | if ((n = z->avail_in) == 0) |
| 342 | return Z_BUF_ERROR; |
| 343 | p = z->next_in; |
| 344 | m = z->state->sub.marker; |
| 345 | |
| 346 | /* search */ |
| 347 | while (n && m < 4) |
| 348 | { |
| 349 | static const Byte mark[4] = {0, 0, 0xff, 0xff}; |
| 350 | if (*p == mark[m]) |
| 351 | m++; |
| 352 | else if (*p) |
| 353 | m = 0; |
| 354 | else |
| 355 | m = 4 - m; |
| 356 | p++, n--; |
| 357 | } |
| 358 | |
| 359 | /* restore */ |
| 360 | z->total_in += p - z->next_in; |
| 361 | z->next_in = p; |
| 362 | z->avail_in = n; |
| 363 | z->state->sub.marker = m; |
| 364 | |
| 365 | /* return no joy or set up to restart on a new block */ |
| 366 | if (m != 4) |
| 367 | return Z_DATA_ERROR; |
| 368 | r = z->total_in; w = z->total_out; |
| 369 | inflateReset(z); |
| 370 | z->total_in = r; z->total_out = w; |
| 371 | z->state->mode = BLOCKS; |
| 372 | return Z_OK; |
| 373 | } |
| 374 | |
| 375 | /* Returns true if inflate is currently at the end of a block generated |
| 376 | * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP |
| 377 | * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH |
| 378 | * but removes the length bytes of the resulting empty stored block. When |
| 379 | * decompressing, PPP checks that at the end of input packet, inflate is |
| 380 | * waiting for these length bytes. |
| 381 | */ |
| 382 | #if defined(__VISAGECPP__) /* Visualage can't handle this antiquated interface */ |
| 383 | int ZEXPORT inflateSyncPoint(z_streamp z) |
| 384 | #else |
| 385 | int ZEXPORT inflateSyncPoint(z) |
| 386 | z_streamp z; |
| 387 | #endif |
| 388 | { |
| 389 | if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL) |
| 390 | return Z_STREAM_ERROR; |
| 391 | return inflate_blocks_sync_point(z->state->blocks); |
| 392 | } |