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 */
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
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)
15 typedef SSIZE_T ssize_t;
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));
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,
35 unsigned get, max = ((unsigned)-1 >> 2) + 1;
42 ret = read(state.state->fd, buf + *have, get);
45 *have += (unsigned)ret;
46 } while (*have < len);
48 gz_error(state, Z_ERRNO, zstrerror());
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)
66 z_streamp strm = &(state.state->strm);
68 if (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR)
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;
79 if (gz_load(state, state.state->in + strm->avail_in,
80 state.state->size - strm->avail_in, &got) == -1)
82 strm->avail_in += got;
83 strm->next_in = state.state->in;
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);
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");
111 state.state->size = state.state->want;
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");
128 /* get at least the magic bytes in the input buffer */
129 if (strm->avail_in < 2) {
130 if (gz_avail(state) == -1)
132 if (strm->avail_in == 0)
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 */
147 state.state->how = GZIP;
148 state.state->direct = 0;
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) {
156 state.state->eof = 1;
157 state.state->x.have = 0;
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;
170 state.state->how = COPY;
171 state.state->direct = 1;
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) {
183 z_streamp strm = &(state.state->strm);
185 /* fill output buffer up to end of deflate stream */
186 had = strm->avail_out;
188 /* get more input for inflate() */
189 if (strm->avail_in == 0 && gz_avail(state) == -1)
191 if (strm->avail_in == 0) {
192 gz_error(state, Z_BUF_ERROR, "unexpected end of file");
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");
203 if (ret == Z_MEM_ERROR) {
204 gz_error(state, Z_MEM_ERROR, "out of memory");
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);
212 } while (strm->avail_out && ret != Z_STREAM_END);
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;
218 /* if the gzip stream completed successfully, look for another */
219 if (ret == Z_STREAM_END)
220 state.state->how = LOOK;
222 /* good decompression */
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);
236 switch(state.state->how) {
237 case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */
238 if (gz_look(state) == -1)
240 if (state.state->how == LOOK)
243 case COPY: /* -> COPY */
244 if (gz_load(state, state.state->out, state.state->size << 1, &(state.state->x.have))
247 state.state->x.next = state.state->out;
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)
255 } while (state.state->x.have == 0 && (!state.state->eof || strm->avail_in));
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) {
263 /* skip over len bytes or reach end-of-file, whichever comes first */
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;
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)
279 /* need more data to skip -- load up output buffer */
281 /* get more output, looking for header if required */
282 if (gz_fetch(state) == -1)
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) {
296 /* if len is zero, avoid unnecessary operations */
300 /* process a skip request */
301 if (state.state->seek) {
302 state.state->seek = 0;
303 if (gz_skip(state, state.state->skip) == -1)
307 /* get len bytes to buf, or less than len if at the end */
310 /* set n to the maximum amount of len that fits in an unsigned int */
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;
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 */
330 /* need output data -- for small len or new stream load up our output
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)
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 */
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)
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)
353 n = state.state->x.have;
354 state.state->x.have = 0;
357 /* update progress */
359 buf = (char *)buf + n;
361 state.state->x.pos += n;
364 /* return number of bytes read into user buffer */
368 /* -- see zlib.h -- */
369 int ZEXPORT gzread(gzFile file, voidp buf, unsigned len) {
372 /* get internal structure */
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))
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) */
385 gz_error(state, Z_STREAM_ERROR, "request does not fit in an int");
389 /* read len or fewer bytes to buf */
390 len = (unsigned)gz_read(state, buf, len);
392 /* check for an error */
393 if (len == 0 && state.state->err != Z_OK && state.state->err != Z_BUF_ERROR)
396 /* return the number of bytes read (this is assured to fit in an int) */
400 /* -- see zlib.h -- */
401 z_size_t ZEXPORT gzfread(voidp buf, z_size_t size, z_size_t nitems,
406 /* get internal structure */
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))
416 /* compute bytes to read -- error on overflow */
418 if (size && len / size != nitems) {
419 gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
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;
427 /* -- see zlib.h -- */
428 #if ZLIB_VERNUM >= 0x1261
436 #if ZLIB_VERNUM == 0x1260
440 #if ZLIB_VERNUM <= 0x1250
441 ZEXTERN int ZEXPORT gzgetc _Z_OF((gzFile file));
442 ZEXTERN int ZEXPORT gzgetc_ _Z_OF((gzFile file));
445 int ZEXPORT gzgetc(gzFile file) {
447 unsigned char buf[1];
450 /* get internal structure */
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))
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)++;
467 /* nothing there -- try gz_read() */
468 ret = (int)gz_read(state, buf, 1);
469 return ret < 1 ? -1 : buf[0];
472 int ZEXPORT gzgetc_(gzFile file) {
476 /* -- see zlib.h -- */
477 int ZEXPORT gzungetc(int c, gzFile file) {
480 /* get internal structure */
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))
490 /* process a skip request */
491 if (state.state->seek) {
492 state.state->seek = 0;
493 if (gz_skip(state, state.state->skip) == -1)
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;
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");
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)
523 state.state->x.next = dest;
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;
533 /* -- see zlib.h -- */
534 char * ZEXPORT gzgets(gzFile file, char *buf, int len) {
540 /* check parameters and get internal structure */
541 if (file == NULL || buf == NULL || len < 1)
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))
550 /* process a skip request */
551 if (state.state->seek) {
552 state.state->seek = 0;
553 if (gz_skip(state, state.state->skip) == -1)
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) */
561 left = (unsigned)len - 1;
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 */
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);
575 n = (unsigned)(eol - state.state->x.next) + 1;
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;
584 } while (left && eol == NULL);
586 /* return terminated string, or if nothing, end of file */
593 /* -- see zlib.h -- */
594 int ZEXPORT gzdirect(gzFile file) {
597 /* get internal structure */
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);
607 /* return 1 if transparent, 0 if processing a gzip stream */
608 return state.state->direct;
611 /* -- see zlib.h -- */
612 int ZEXPORT gzclose_r(gzFile file) {
616 /* get internal structure */
618 return Z_STREAM_ERROR;
621 /* check that we're reading */
622 if (state.state->mode != GZ_READ)
623 return Z_STREAM_ERROR;
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);
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);
636 return ret ? Z_ERRNO : err;