1 /* gzread.c contains minimal changes required to be compiled with zlibWrapper: 2 * - gz_statep was converted to union to work with -Wstrict-aliasing=1 */ 3 4 /* gzread.c -- zlib functions for reading gzip files 5 * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013, 2016 Mark Adler 6 * For conditions of distribution and use, see https://www.zlib.net/zlib_license.html 7 */ 8 9 #include "gzguts.h" 10 11 /* fix for Visual Studio, which doesn't support ssize_t type. 12 * see https://github.com/facebook/zstd/issues/1800#issuecomment-545945050 */ 13 #if defined(_MSC_VER) && !defined(ssize_t) 14 # include <BaseTsd.h> 15 typedef SSIZE_T ssize_t; 16 #endif 17 18 19 /* Local functions */ 20 local int gz_load _Z_OF((gz_statep, unsigned char *, unsigned, unsigned *)); 21 local int gz_avail _Z_OF((gz_statep)); 22 local int gz_look _Z_OF((gz_statep)); 23 local int gz_decomp _Z_OF((gz_statep)); 24 local int gz_fetch _Z_OF((gz_statep)); 25 local int gz_skip _Z_OF((gz_statep, z_off64_t)); 26 local z_size_t gz_read _Z_OF((gz_statep, voidp, z_size_t)); 27 28 /* Use read() to load a buffer -- return -1 on error, otherwise 0. Read from 29 state.state->fd, and update state.state->eof, state.state->err, and state.state->msg as appropriate. 30 This function needs to loop on read(), since read() is not guaranteed to 31 read the number of bytes requested, depending on the type of descriptor. */ 32 local int gz_load(gz_statep state, unsigned char *buf, unsigned len, 33 unsigned *have) { 34 ssize_t ret; 35 unsigned get, max = ((unsigned)-1 >> 2) + 1; 36 37 *have = 0; 38 do { 39 get = len - *have; 40 if (get > max) 41 get = max; 42 ret = read(state.state->fd, buf + *have, get); 43 if (ret <= 0) 44 break; 45 *have += (unsigned)ret; 46 } while (*have < len); 47 if (ret < 0) { 48 gz_error(state, Z_ERRNO, zstrerror()); 49 return -1; 50 } 51 if (ret == 0) 52 state.state->eof = 1; 53 return 0; 54 } 55 56 /* Load up input buffer and set eof flag if last data loaded -- return -1 on 57 error, 0 otherwise. Note that the eof flag is set when the end of the input 58 file is reached, even though there may be unused data in the buffer. Once 59 that data has been used, no more attempts will be made to read the file. 60 If strm->avail_in != 0, then the current data is moved to the beginning of 61 the input buffer, and then the remainder of the buffer is loaded with the 62 available data from the input file. */ 63 local int gz_avail(gz_statep state) 64 { 65 unsigned got; 66 z_streamp strm = &(state.state->strm); 67 68 if (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR) 69 return -1; 70 if (state.state->eof == 0) { 71 if (strm->avail_in) { /* copy what's there to the start */ 72 unsigned char *p = state.state->in; 73 unsigned const char *q = strm->next_in; 74 unsigned n = strm->avail_in; 75 do { 76 *p++ = *q++; 77 } while (--n); 78 } 79 if (gz_load(state, state.state->in + strm->avail_in, 80 state.state->size - strm->avail_in, &got) == -1) 81 return -1; 82 strm->avail_in += got; 83 strm->next_in = state.state->in; 84 } 85 return 0; 86 } 87 88 /* Look for gzip header, set up for inflate or copy. state.state->x.have must be 0. 89 If this is the first time in, allocate required memory. state.state->how will be 90 left unchanged if there is no more input data available, will be set to COPY 91 if there is no gzip header and direct copying will be performed, or it will 92 be set to GZIP for decompression. If direct copying, then leftover input 93 data from the input buffer will be copied to the output buffer. In that 94 case, all further file reads will be directly to either the output buffer or 95 a user buffer. If decompressing, the inflate state will be initialized. 96 gz_look() will return 0 on success or -1 on failure. */ 97 local int gz_look(gz_statep state) { 98 z_streamp strm = &(state.state->strm); 99 100 /* allocate read buffers and inflate memory */ 101 if (state.state->size == 0) { 102 /* allocate buffers */ 103 state.state->in = (unsigned char *)malloc(state.state->want); 104 state.state->out = (unsigned char *)malloc(state.state->want << 1); 105 if (state.state->in == NULL || state.state->out == NULL) { 106 free(state.state->out); 107 free(state.state->in); 108 gz_error(state, Z_MEM_ERROR, "out of memory"); 109 return -1; 110 } 111 state.state->size = state.state->want; 112 113 /* allocate inflate memory */ 114 state.state->strm.zalloc = Z_NULL; 115 state.state->strm.zfree = Z_NULL; 116 state.state->strm.opaque = Z_NULL; 117 state.state->strm.avail_in = 0; 118 state.state->strm.next_in = Z_NULL; 119 if (inflateInit2(&(state.state->strm), 15 + 16) != Z_OK) { /* gunzip */ 120 free(state.state->out); 121 free(state.state->in); 122 state.state->size = 0; 123 gz_error(state, Z_MEM_ERROR, "out of memory"); 124 return -1; 125 } 126 } 127 128 /* get at least the magic bytes in the input buffer */ 129 if (strm->avail_in < 2) { 130 if (gz_avail(state) == -1) 131 return -1; 132 if (strm->avail_in == 0) 133 return 0; 134 } 135 136 /* look for gzip magic bytes -- if there, do gzip decoding (note: there is 137 a logical dilemma here when considering the case of a partially written 138 gzip file, to wit, if a single 31 byte is written, then we cannot tell 139 whether this is a single-byte file, or just a partially written gzip 140 file -- for here we assume that if a gzip file is being written, then 141 the header will be written in a single operation, so that reading a 142 single byte is sufficient indication that it is not a gzip file) */ 143 if (strm->avail_in > 1 && 144 ((strm->next_in[0] == 31 && strm->next_in[1] == 139) /* gz header */ 145 || (strm->next_in[0] == 40 && strm->next_in[1] == 181))) { /* zstd header */ 146 inflateReset(strm); 147 state.state->how = GZIP; 148 state.state->direct = 0; 149 return 0; 150 } 151 152 /* no gzip header -- if we were decoding gzip before, then this is trailing 153 garbage. Ignore the trailing garbage and finish. */ 154 if (state.state->direct == 0) { 155 strm->avail_in = 0; 156 state.state->eof = 1; 157 state.state->x.have = 0; 158 return 0; 159 } 160 161 /* doing raw i/o, copy any leftover input to output -- this assumes that 162 the output buffer is larger than the input buffer, which also assures 163 space for gzungetc() */ 164 state.state->x.next = state.state->out; 165 if (strm->avail_in) { 166 memcpy(state.state->x.next, strm->next_in, strm->avail_in); 167 state.state->x.have = strm->avail_in; 168 strm->avail_in = 0; 169 } 170 state.state->how = COPY; 171 state.state->direct = 1; 172 return 0; 173 } 174 175 /* Decompress from input to the provided next_out and avail_out in the state. 176 On return, state.state->x.have and state.state->x.next point to the just decompressed 177 data. If the gzip stream completes, state.state->how is reset to LOOK to look for 178 the next gzip stream or raw data, once state.state->x.have is depleted. Returns 0 179 on success, -1 on failure. */ 180 local int gz_decomp(gz_statep state) { 181 int ret = Z_OK; 182 unsigned had; 183 z_streamp strm = &(state.state->strm); 184 185 /* fill output buffer up to end of deflate stream */ 186 had = strm->avail_out; 187 do { 188 /* get more input for inflate() */ 189 if (strm->avail_in == 0 && gz_avail(state) == -1) 190 return -1; 191 if (strm->avail_in == 0) { 192 gz_error(state, Z_BUF_ERROR, "unexpected end of file"); 193 break; 194 } 195 196 /* decompress and handle errors */ 197 ret = inflate(strm, Z_NO_FLUSH); 198 if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) { 199 gz_error(state, Z_STREAM_ERROR, 200 "internal error: inflate stream corrupt"); 201 return -1; 202 } 203 if (ret == Z_MEM_ERROR) { 204 gz_error(state, Z_MEM_ERROR, "out of memory"); 205 return -1; 206 } 207 if (ret == Z_DATA_ERROR) { /* deflate stream invalid */ 208 gz_error(state, Z_DATA_ERROR, 209 strm->msg == NULL ? "compressed data error" : strm->msg); 210 return -1; 211 } 212 } while (strm->avail_out && ret != Z_STREAM_END); 213 214 /* update available output */ 215 state.state->x.have = had - strm->avail_out; 216 state.state->x.next = strm->next_out - state.state->x.have; 217 218 /* if the gzip stream completed successfully, look for another */ 219 if (ret == Z_STREAM_END) 220 state.state->how = LOOK; 221 222 /* good decompression */ 223 return 0; 224 } 225 226 /* Fetch data and put it in the output buffer. Assumes state.state->x.have is 0. 227 Data is either copied from the input file or decompressed from the input 228 file depending on state.state->how. If state.state->how is LOOK, then a gzip header is 229 looked for to determine whether to copy or decompress. Returns -1 on error, 230 otherwise 0. gz_fetch() will leave state.state->how as COPY or GZIP unless the 231 end of the input file has been reached and all data has been processed. */ 232 local int gz_fetch(gz_statep state) { 233 z_streamp strm = &(state.state->strm); 234 235 do { 236 switch(state.state->how) { 237 case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */ 238 if (gz_look(state) == -1) 239 return -1; 240 if (state.state->how == LOOK) 241 return 0; 242 break; 243 case COPY: /* -> COPY */ 244 if (gz_load(state, state.state->out, state.state->size << 1, &(state.state->x.have)) 245 == -1) 246 return -1; 247 state.state->x.next = state.state->out; 248 return 0; 249 case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */ 250 strm->avail_out = state.state->size << 1; 251 strm->next_out = state.state->out; 252 if (gz_decomp(state) == -1) 253 return -1; 254 } 255 } while (state.state->x.have == 0 && (!state.state->eof || strm->avail_in)); 256 return 0; 257 } 258 259 /* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */ 260 local int gz_skip(gz_statep state, z_off64_t len) { 261 unsigned n; 262 263 /* skip over len bytes or reach end-of-file, whichever comes first */ 264 while (len) 265 /* skip over whatever is in output buffer */ 266 if (state.state->x.have) { 267 n = GT_OFF(state.state->x.have) || (z_off64_t)state.state->x.have > len ? 268 (unsigned)len : state.state->x.have; 269 state.state->x.have -= n; 270 state.state->x.next += n; 271 state.state->x.pos += n; 272 len -= n; 273 } 274 275 /* output buffer empty -- return if we're at the end of the input */ 276 else if (state.state->eof && state.state->strm.avail_in == 0) 277 break; 278 279 /* need more data to skip -- load up output buffer */ 280 else { 281 /* get more output, looking for header if required */ 282 if (gz_fetch(state) == -1) 283 return -1; 284 } 285 return 0; 286 } 287 288 /* Read len bytes into buf from file, or less than len up to the end of the 289 input. Return the number of bytes read. If zero is returned, either the 290 end of file was reached, or there was an error. state.state->err must be 291 consulted in that case to determine which. */ 292 local z_size_t gz_read(gz_statep state, voidp buf, z_size_t len) { 293 z_size_t got; 294 unsigned n; 295 296 /* if len is zero, avoid unnecessary operations */ 297 if (len == 0) 298 return 0; 299 300 /* process a skip request */ 301 if (state.state->seek) { 302 state.state->seek = 0; 303 if (gz_skip(state, state.state->skip) == -1) 304 return 0; 305 } 306 307 /* get len bytes to buf, or less than len if at the end */ 308 got = 0; 309 do { 310 /* set n to the maximum amount of len that fits in an unsigned int */ 311 n = -1; 312 if (n > len) 313 n = (unsigned)len; 314 315 /* first just try copying data from the output buffer */ 316 if (state.state->x.have) { 317 if (state.state->x.have < n) 318 n = state.state->x.have; 319 memcpy(buf, state.state->x.next, n); 320 state.state->x.next += n; 321 state.state->x.have -= n; 322 } 323 324 /* output buffer empty -- return if we're at the end of the input */ 325 else if (state.state->eof && state.state->strm.avail_in == 0) { 326 state.state->past = 1; /* tried to read past end */ 327 break; 328 } 329 330 /* need output data -- for small len or new stream load up our output 331 buffer */ 332 else if (state.state->how == LOOK || n < (state.state->size << 1)) { 333 /* get more output, looking for header if required */ 334 if (gz_fetch(state) == -1) 335 return 0; 336 continue; /* no progress yet -- go back to copy above */ 337 /* the copy above assures that we will leave with space in the 338 output buffer, allowing at least one gzungetc() to succeed */ 339 } 340 341 /* large len -- read directly into user buffer */ 342 else if (state.state->how == COPY) { /* read directly */ 343 if (gz_load(state, (unsigned char *)buf, n, &n) == -1) 344 return 0; 345 } 346 347 /* large len -- decompress directly into user buffer */ 348 else { /* state.state->how == GZIP */ 349 state.state->strm.avail_out = n; 350 state.state->strm.next_out = (unsigned char *)buf; 351 if (gz_decomp(state) == -1) 352 return 0; 353 n = state.state->x.have; 354 state.state->x.have = 0; 355 } 356 357 /* update progress */ 358 len -= n; 359 buf = (char *)buf + n; 360 got += n; 361 state.state->x.pos += n; 362 } while (len); 363 364 /* return number of bytes read into user buffer */ 365 return got; 366 } 367 368 /* -- see zlib.h -- */ 369 int ZEXPORT gzread(gzFile file, voidp buf, unsigned len) { 370 gz_statep state; 371 372 /* get internal structure */ 373 if (file == NULL) 374 return -1; 375 state.file = file; 376 377 /* check that we're reading and that there's no (serious) error */ 378 if (state.state->mode != GZ_READ || 379 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR)) 380 return -1; 381 382 /* since an int is returned, make sure len fits in one, otherwise return 383 with an error (this avoids a flaw in the interface) */ 384 if ((int)len < 0) { 385 gz_error(state, Z_STREAM_ERROR, "request does not fit in an int"); 386 return -1; 387 } 388 389 /* read len or fewer bytes to buf */ 390 len = (unsigned)gz_read(state, buf, len); 391 392 /* check for an error */ 393 if (len == 0 && state.state->err != Z_OK && state.state->err != Z_BUF_ERROR) 394 return -1; 395 396 /* return the number of bytes read (this is assured to fit in an int) */ 397 return (int)len; 398 } 399 400 /* -- see zlib.h -- */ 401 z_size_t ZEXPORT gzfread(voidp buf, z_size_t size, z_size_t nitems, 402 gzFile file) { 403 z_size_t len; 404 gz_statep state; 405 406 /* get internal structure */ 407 if (file == NULL) 408 return 0; 409 state.file = file; 410 411 /* check that we're reading and that there's no (serious) error */ 412 if (state.state->mode != GZ_READ || 413 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR)) 414 return 0; 415 416 /* compute bytes to read -- error on overflow */ 417 len = nitems * size; 418 if (size && len / size != nitems) { 419 gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t"); 420 return 0; 421 } 422 423 /* read len or fewer bytes to buf, return the number of full items read */ 424 return len ? gz_read(state, buf, len) / size : 0; 425 } 426 427 /* -- see zlib.h -- */ 428 #if ZLIB_VERNUM >= 0x1261 429 #ifdef Z_PREFIX_SET 430 # undef z_gzgetc 431 #else 432 # undef gzgetc 433 #endif 434 #endif 435 436 #if ZLIB_VERNUM == 0x1260 437 # undef gzgetc 438 #endif 439 440 #if ZLIB_VERNUM <= 0x1250 441 ZEXTERN int ZEXPORT gzgetc _Z_OF((gzFile file)); 442 ZEXTERN int ZEXPORT gzgetc_ _Z_OF((gzFile file)); 443 #endif 444 445 int ZEXPORT gzgetc(gzFile file) { 446 int ret; 447 unsigned char buf[1]; 448 gz_statep state; 449 450 /* get internal structure */ 451 if (file == NULL) 452 return -1; 453 state.file = file; 454 455 /* check that we're reading and that there's no (serious) error */ 456 if (state.state->mode != GZ_READ || 457 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR)) 458 return -1; 459 460 /* try output buffer (no need to check for skip request) */ 461 if (state.state->x.have) { 462 state.state->x.have--; 463 state.state->x.pos++; 464 return *(state.state->x.next)++; 465 } 466 467 /* nothing there -- try gz_read() */ 468 ret = (int)gz_read(state, buf, 1); 469 return ret < 1 ? -1 : buf[0]; 470 } 471 472 int ZEXPORT gzgetc_(gzFile file) { 473 return gzgetc(file); 474 } 475 476 /* -- see zlib.h -- */ 477 int ZEXPORT gzungetc(int c, gzFile file) { 478 gz_statep state; 479 480 /* get internal structure */ 481 if (file == NULL) 482 return -1; 483 state.file = file; 484 485 /* check that we're reading and that there's no (serious) error */ 486 if (state.state->mode != GZ_READ || 487 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR)) 488 return -1; 489 490 /* process a skip request */ 491 if (state.state->seek) { 492 state.state->seek = 0; 493 if (gz_skip(state, state.state->skip) == -1) 494 return -1; 495 } 496 497 /* can't push EOF */ 498 if (c < 0) 499 return -1; 500 501 /* if output buffer empty, put byte at end (allows more pushing) */ 502 if (state.state->x.have == 0) { 503 state.state->x.have = 1; 504 state.state->x.next = state.state->out + (state.state->size << 1) - 1; 505 state.state->x.next[0] = (unsigned char)c; 506 state.state->x.pos--; 507 state.state->past = 0; 508 return c; 509 } 510 511 /* if no room, give up (must have already done a gzungetc()) */ 512 if (state.state->x.have == (state.state->size << 1)) { 513 gz_error(state, Z_DATA_ERROR, "out of room to push characters"); 514 return -1; 515 } 516 517 /* slide output data if needed and insert byte before existing data */ 518 if (state.state->x.next == state.state->out) { 519 unsigned char *src = state.state->out + state.state->x.have; 520 unsigned char *dest = state.state->out + (state.state->size << 1); 521 while (src > state.state->out) 522 *--dest = *--src; 523 state.state->x.next = dest; 524 } 525 state.state->x.have++; 526 state.state->x.next--; 527 state.state->x.next[0] = (unsigned char)c; 528 state.state->x.pos--; 529 state.state->past = 0; 530 return c; 531 } 532 533 /* -- see zlib.h -- */ 534 char * ZEXPORT gzgets(gzFile file, char *buf, int len) { 535 unsigned left, n; 536 char *str; 537 unsigned char *eol; 538 gz_statep state; 539 540 /* check parameters and get internal structure */ 541 if (file == NULL || buf == NULL || len < 1) 542 return NULL; 543 state.file = file; 544 545 /* check that we're reading and that there's no (serious) error */ 546 if (state.state->mode != GZ_READ || 547 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR)) 548 return NULL; 549 550 /* process a skip request */ 551 if (state.state->seek) { 552 state.state->seek = 0; 553 if (gz_skip(state, state.state->skip) == -1) 554 return NULL; 555 } 556 557 /* copy output bytes up to new line or len - 1, whichever comes first -- 558 append a terminating zero to the string (we don't check for a zero in 559 the contents, let the user worry about that) */ 560 str = buf; 561 left = (unsigned)len - 1; 562 if (left) do { 563 /* assure that something is in the output buffer */ 564 if (state.state->x.have == 0 && gz_fetch(state) == -1) 565 return NULL; /* error */ 566 if (state.state->x.have == 0) { /* end of file */ 567 state.state->past = 1; /* read past end */ 568 break; /* return what we have */ 569 } 570 571 /* look for end-of-line in current output buffer */ 572 n = state.state->x.have > left ? left : state.state->x.have; 573 eol = (unsigned char *)memchr(state.state->x.next, '\n', n); 574 if (eol != NULL) 575 n = (unsigned)(eol - state.state->x.next) + 1; 576 577 /* copy through end-of-line, or remainder if not found */ 578 memcpy(buf, state.state->x.next, n); 579 state.state->x.have -= n; 580 state.state->x.next += n; 581 state.state->x.pos += n; 582 left -= n; 583 buf += n; 584 } while (left && eol == NULL); 585 586 /* return terminated string, or if nothing, end of file */ 587 if (buf == str) 588 return NULL; 589 buf[0] = 0; 590 return str; 591 } 592 593 /* -- see zlib.h -- */ 594 int ZEXPORT gzdirect(gzFile file) { 595 gz_statep state; 596 597 /* get internal structure */ 598 if (file == NULL) 599 return 0; 600 state.file = file; 601 602 /* if the state is not known, but we can find out, then do so (this is 603 mainly for right after a gzopen() or gzdopen()) */ 604 if (state.state->mode == GZ_READ && state.state->how == LOOK && state.state->x.have == 0) 605 (void)gz_look(state); 606 607 /* return 1 if transparent, 0 if processing a gzip stream */ 608 return state.state->direct; 609 } 610 611 /* -- see zlib.h -- */ 612 int ZEXPORT gzclose_r(gzFile file) { 613 int ret, err; 614 gz_statep state; 615 616 /* get internal structure */ 617 if (file == NULL) 618 return Z_STREAM_ERROR; 619 state.file = file; 620 621 /* check that we're reading */ 622 if (state.state->mode != GZ_READ) 623 return Z_STREAM_ERROR; 624 625 /* free memory and close file */ 626 if (state.state->size) { 627 inflateEnd(&(state.state->strm)); 628 free(state.state->out); 629 free(state.state->in); 630 } 631 err = state.state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK; 632 gz_error(state, Z_OK, NULL); 633 free(state.state->path); 634 ret = close(state.state->fd); 635 free(state.state); 636 return ret ? Z_ERRNO : err; 637 } 638