=========================================================================== Check the gzip header of a gz_stream opened for reading. Set the stream mode to transparent if the gzip magic header is not present; set s->err to Z_DATA_ERROR if the magic header is present but the rest of the header is incorrect. IN assertion: the stream s has already been created sucessfully;
(s)
| 291 | for concatenated .gz files. |
| 292 | */ |
| 293 | local void check_header(s) |
| 294 | gz_stream *s; |
| 295 | { |
| 296 | int method; /* method byte */ |
| 297 | int flags; /* flags byte */ |
| 298 | uInt len; |
| 299 | int c; |
| 300 | |
| 301 | /* Assure two bytes in the buffer so we can peek ahead -- handle case |
| 302 | where first byte of header is at the end of the buffer after the last |
| 303 | gzip segment */ |
| 304 | len = s->stream.avail_in; |
| 305 | if (len < 2) { |
| 306 | if (len) s->inbuf[0] = s->stream.next_in[0]; |
| 307 | errno = 0; |
| 308 | len = (uInt)fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file); |
| 309 | if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO; |
| 310 | s->stream.avail_in += len; |
| 311 | s->stream.next_in = s->inbuf; |
| 312 | if (s->stream.avail_in < 2) { |
| 313 | s->transparent = s->stream.avail_in; |
| 314 | return; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /* Peek ahead to check the gzip magic header */ |
| 319 | if (s->stream.next_in[0] != gz_magic[0] || |
| 320 | s->stream.next_in[1] != gz_magic[1]) { |
| 321 | s->transparent = 1; |
| 322 | return; |
| 323 | } |
| 324 | s->stream.avail_in -= 2; |
| 325 | s->stream.next_in += 2; |
| 326 | |
| 327 | /* Check the rest of the gzip header */ |
| 328 | method = get_byte(s); |
| 329 | flags = get_byte(s); |
| 330 | if (method != Z_DEFLATED || (flags & RESERVED) != 0) { |
| 331 | s->z_err = Z_DATA_ERROR; |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | /* Discard time, xflags and OS code: */ |
| 336 | for (len = 0; len < 6; len++) (void)get_byte(s); |
| 337 | |
| 338 | if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */ |
| 339 | len = (uInt)get_byte(s); |
| 340 | len += ((uInt)get_byte(s))<<8; |
| 341 | /* len is garbage if EOF but the loop below will quit anyway */ |
| 342 | while (len-- != 0 && get_byte(s) != EOF) ; |
| 343 | } |
| 344 | if ((flags & ORIG_NAME) != 0) { /* skip the original file name */ |
| 345 | while ((c = get_byte(s)) != 0 && c != EOF) ; |
| 346 | } |
| 347 | if ((flags & COMMENT) != 0) { /* skip the .gz file comment */ |
| 348 | while ((c = get_byte(s)) != 0 && c != EOF) ; |
| 349 | } |
| 350 | if ((flags & HEAD_CRC) != 0) { /* skip the header crc */ |