blob: ed3c1782606711b70d238a86fa09c9bea4b90f7d [file] [log] [blame]
Przemyslaw Skibinskif0d7da72016-11-29 18:02:34 +01001/* gzread.c contains minimal changes required to be compiled with zlibWrapper:
Yann Collet32fb4072017-08-18 16:52:05 -07002 * - gz_statep was converted to union to work with -Wstrict-aliasing=1 */
3
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +01004 /* gzread.c -- zlib functions for reading gzip files
5 * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013, 2016 Mark Adler
Danielle Rozenblit4dffc352022-12-14 06:58:35 -08006 * For conditions of distribution and use, see https://www.zlib.net/zlib_license.html
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +01007 */
8
9#include "gzguts.h"
10
Yann Collet42a22af2019-10-24 15:19:05 -070011/* 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
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010019/* Local functions */
orbea1e09cff2022-07-29 12:22:10 -070020local int gz_load _Z_OF((gz_statep, unsigned char *, unsigned, unsigned *));
21local int gz_avail _Z_OF((gz_statep));
22local int gz_look _Z_OF((gz_statep));
23local int gz_decomp _Z_OF((gz_statep));
24local int gz_fetch _Z_OF((gz_statep));
25local int gz_skip _Z_OF((gz_statep, z_off64_t));
26local z_size_t gz_read _Z_OF((gz_statep, voidp, z_size_t));
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010027
28/* Use read() to load a buffer -- return -1 on error, otherwise 0. Read from
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +010029 state.state->fd, and update state.state->eof, state.state->err, and state.state->msg as appropriate.
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010030 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. */
Ed Maste2ce02902023-12-13 19:54:29 -050032local int gz_load(gz_statep state, unsigned char *buf, unsigned len,
33 unsigned *have) {
Przemyslaw Skibinskic3a04de2017-01-18 14:36:10 +010034 ssize_t ret;
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +010035 unsigned get, max = ((unsigned)-1 >> 2) + 1;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010036
37 *have = 0;
38 do {
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +010039 get = len - *have;
40 if (get > max)
41 get = max;
42 ret = read(state.state->fd, buf + *have, get);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010043 if (ret <= 0)
44 break;
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +010045 *have += (unsigned)ret;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010046 } while (*have < len);
47 if (ret < 0) {
48 gz_error(state, Z_ERRNO, zstrerror());
49 return -1;
50 }
51 if (ret == 0)
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010052 state.state->eof = 1;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010053 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. */
Ed Maste2ce02902023-12-13 19:54:29 -050063local int gz_avail(gz_statep state)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010064{
65 unsigned got;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010066 z_streamp strm = &(state.state->strm);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010067
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010068 if (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010069 return -1;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010070 if (state.state->eof == 0) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010071 if (strm->avail_in) { /* copy what's there to the start */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010072 unsigned char *p = state.state->in;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010073 unsigned const char *q = strm->next_in;
74 unsigned n = strm->avail_in;
75 do {
76 *p++ = *q++;
77 } while (--n);
78 }
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010079 if (gz_load(state, state.state->in + strm->avail_in,
80 state.state->size - strm->avail_in, &got) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010081 return -1;
82 strm->avail_in += got;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010083 strm->next_in = state.state->in;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010084 }
85 return 0;
86}
87
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +010088/* 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
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010090 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. */
Ed Maste2ce02902023-12-13 19:54:29 -050097local int gz_look(gz_statep state) {
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010098 z_streamp strm = &(state.state->strm);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010099
100 /* allocate read buffers and inflate memory */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100101 if (state.state->size == 0) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100102 /* allocate buffers */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100103 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) {
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100106 free(state.state->out);
107 free(state.state->in);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100108 gz_error(state, Z_MEM_ERROR, "out of memory");
109 return -1;
110 }
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100111 state.state->size = state.state->want;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100112
113 /* allocate inflate memory */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100114 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;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100123 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) */
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100143 if (strm->avail_in > 1 &&
Przemyslaw Skibinskidc2fe752016-12-01 11:56:20 +0100144 ((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 */
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100146 inflateReset(strm);
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100147 state.state->how = GZIP;
148 state.state->direct = 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100149 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. */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100154 if (state.state->direct == 0) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100155 strm->avail_in = 0;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100156 state.state->eof = 1;
157 state.state->x.have = 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100158 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() */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100164 state.state->x.next = state.state->out;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100165 if (strm->avail_in) {
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100166 memcpy(state.state->x.next, strm->next_in, strm->avail_in);
167 state.state->x.have = strm->avail_in;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100168 strm->avail_in = 0;
169 }
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100170 state.state->how = COPY;
171 state.state->direct = 1;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100172 return 0;
173}
174
175/* Decompress from input to the provided next_out and avail_out in the state.
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100176 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
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100179 on success, -1 on failure. */
Ed Maste2ce02902023-12-13 19:54:29 -0500180local int gz_decomp(gz_statep state) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100181 int ret = Z_OK;
182 unsigned had;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100183 z_streamp strm = &(state.state->strm);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100184
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 */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100215 state.state->x.have = had - strm->avail_out;
216 state.state->x.next = strm->next_out - state.state->x.have;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100217
218 /* if the gzip stream completed successfully, look for another */
219 if (ret == Z_STREAM_END)
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100220 state.state->how = LOOK;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100221
222 /* good decompression */
223 return 0;
224}
225
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100226/* Fetch data and put it in the output buffer. Assumes state.state->x.have is 0.
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100227 Data is either copied from the input file or decompressed from the input
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100228 file depending on state.state->how. If state.state->how is LOOK, then a gzip header is
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100229 looked for to determine whether to copy or decompress. Returns -1 on error,
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100230 otherwise 0. gz_fetch() will leave state.state->how as COPY or GZIP unless the
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100231 end of the input file has been reached and all data has been processed. */
Ed Maste2ce02902023-12-13 19:54:29 -0500232local int gz_fetch(gz_statep state) {
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100233 z_streamp strm = &(state.state->strm);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100234
235 do {
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100236 switch(state.state->how) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100237 case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */
238 if (gz_look(state) == -1)
239 return -1;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100240 if (state.state->how == LOOK)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100241 return 0;
242 break;
243 case COPY: /* -> COPY */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100244 if (gz_load(state, state.state->out, state.state->size << 1, &(state.state->x.have))
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100245 == -1)
246 return -1;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100247 state.state->x.next = state.state->out;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100248 return 0;
249 case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100250 strm->avail_out = state.state->size << 1;
251 strm->next_out = state.state->out;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100252 if (gz_decomp(state) == -1)
253 return -1;
254 }
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100255 } while (state.state->x.have == 0 && (!state.state->eof || strm->avail_in));
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100256 return 0;
257}
258
259/* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */
Ed Maste2ce02902023-12-13 19:54:29 -0500260local int gz_skip(gz_statep state, z_off64_t len) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100261 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 */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100266 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;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100272 len -= n;
273 }
274
275 /* output buffer empty -- return if we're at the end of the input */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100276 else if (state.state->eof && state.state->strm.avail_in == 0)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100277 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
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100288/* 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. */
Ed Maste2ce02902023-12-13 19:54:29 -0500292local z_size_t gz_read(gz_statep state, voidp buf, z_size_t len) {
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100293 z_size_t got;
294 unsigned n;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100295
296 /* if len is zero, avoid unnecessary operations */
297 if (len == 0)
298 return 0;
299
300 /* process a skip request */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100301 if (state.state->seek) {
302 state.state->seek = 0;
303 if (gz_skip(state, state.state->skip) == -1)
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100304 return 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100305 }
306
307 /* get len bytes to buf, or less than len if at the end */
308 got = 0;
309 do {
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100310 /* set n to the maximum amount of len that fits in an unsigned int */
311 n = -1;
312 if (n > len)
Przemyslaw Skibinskic3a04de2017-01-18 14:36:10 +0100313 n = (unsigned)len;
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100314
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100315 /* first just try copying data from the output buffer */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100316 if (state.state->x.have) {
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100317 if (state.state->x.have < n)
318 n = state.state->x.have;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100319 memcpy(buf, state.state->x.next, n);
320 state.state->x.next += n;
321 state.state->x.have -= n;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100322 }
323
324 /* output buffer empty -- return if we're at the end of the input */
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100325 else if (state.state->eof && state.state->strm.avail_in == 0) {
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100326 state.state->past = 1; /* tried to read past end */
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100327 break;
328 }
329
330 /* need output data -- for small len or new stream load up our output
331 buffer */
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100332 else if (state.state->how == LOOK || n < (state.state->size << 1)) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100333 /* get more output, looking for header if required */
334 if (gz_fetch(state) == -1)
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100335 return 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100336 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 */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100342 else if (state.state->how == COPY) { /* read directly */
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100343 if (gz_load(state, (unsigned char *)buf, n, &n) == -1)
344 return 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100345 }
346
347 /* large len -- decompress directly into user buffer */
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100348 else { /* state.state->how == GZIP */
349 state.state->strm.avail_out = n;
350 state.state->strm.next_out = (unsigned char *)buf;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100351 if (gz_decomp(state) == -1)
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100352 return 0;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100353 n = state.state->x.have;
354 state.state->x.have = 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100355 }
356
357 /* update progress */
358 len -= n;
359 buf = (char *)buf + n;
360 got += n;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100361 state.state->x.pos += n;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100362 } while (len);
363
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100364 /* return number of bytes read into user buffer */
365 return got;
366}
367
368/* -- see zlib.h -- */
Ed Maste2ce02902023-12-13 19:54:29 -0500369int ZEXPORT gzread(gzFile file, voidp buf, unsigned len) {
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100370 gz_statep state;
371
372 /* get internal structure */
373 if (file == NULL)
374 return -1;
Yann Colletcb18fff2019-09-24 17:50:58 -0700375 state.file = file;
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100376
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 */
Przemyslaw Skibinskic3a04de2017-01-18 14:36:10 +0100390 len = (unsigned)gz_read(state, buf, len);
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100391
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 -- */
Ed Maste2ce02902023-12-13 19:54:29 -0500401z_size_t ZEXPORT gzfread(voidp buf, z_size_t size, z_size_t nitems,
402 gzFile file) {
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100403 z_size_t len;
404 gz_statep state;
405
406 /* get internal structure */
407 if (file == NULL)
408 return 0;
Yann Colletcb18fff2019-09-24 17:50:58 -0700409 state.file = file;
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100410
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;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100425}
426
427/* -- see zlib.h -- */
Przemyslaw Skibinskic77befe2016-11-28 14:09:26 +0100428#if ZLIB_VERNUM >= 0x1261
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100429#ifdef Z_PREFIX_SET
430# undef z_gzgetc
431#else
432# undef gzgetc
433#endif
Przemyslaw Skibinskic77befe2016-11-28 14:09:26 +0100434#endif
435
436#if ZLIB_VERNUM == 0x1260
437# undef gzgetc
438#endif
439
440#if ZLIB_VERNUM <= 0x1250
orbea1e09cff2022-07-29 12:22:10 -0700441ZEXTERN int ZEXPORT gzgetc _Z_OF((gzFile file));
442ZEXTERN int ZEXPORT gzgetc_ _Z_OF((gzFile file));
Przemyslaw Skibinskic77befe2016-11-28 14:09:26 +0100443#endif
444
Ed Maste2ce02902023-12-13 19:54:29 -0500445int ZEXPORT gzgetc(gzFile file) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100446 int ret;
447 unsigned char buf[1];
448 gz_statep state;
449
450 /* get internal structure */
451 if (file == NULL)
452 return -1;
Yann Colletcb18fff2019-09-24 17:50:58 -0700453 state.file = file;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100454
455 /* check that we're reading and that there's no (serious) error */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100456 if (state.state->mode != GZ_READ ||
457 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100458 return -1;
459
460 /* try output buffer (no need to check for skip request) */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100461 if (state.state->x.have) {
462 state.state->x.have--;
463 state.state->x.pos++;
464 return *(state.state->x.next)++;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100465 }
466
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100467 /* nothing there -- try gz_read() */
Yann Colletcb18fff2019-09-24 17:50:58 -0700468 ret = (int)gz_read(state, buf, 1);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100469 return ret < 1 ? -1 : buf[0];
470}
471
Ed Maste2ce02902023-12-13 19:54:29 -0500472int ZEXPORT gzgetc_(gzFile file) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100473 return gzgetc(file);
474}
475
476/* -- see zlib.h -- */
Ed Maste2ce02902023-12-13 19:54:29 -0500477int ZEXPORT gzungetc(int c, gzFile file) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100478 gz_statep state;
479
480 /* get internal structure */
481 if (file == NULL)
482 return -1;
Yann Colletcb18fff2019-09-24 17:50:58 -0700483 state.file = file;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100484
485 /* check that we're reading and that there's no (serious) error */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100486 if (state.state->mode != GZ_READ ||
487 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100488 return -1;
489
490 /* process a skip request */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100491 if (state.state->seek) {
492 state.state->seek = 0;
493 if (gz_skip(state, state.state->skip) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100494 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) */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100502 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;
Yann Collet9ceb49e2016-12-22 15:26:33 +0100505 state.state->x.next[0] = (unsigned char)c;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100506 state.state->x.pos--;
507 state.state->past = 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100508 return c;
509 }
510
511 /* if no room, give up (must have already done a gzungetc()) */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100512 if (state.state->x.have == (state.state->size << 1)) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100513 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 */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100518 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)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100522 *--dest = *--src;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100523 state.state->x.next = dest;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100524 }
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100525 state.state->x.have++;
526 state.state->x.next--;
Yann Collet9ceb49e2016-12-22 15:26:33 +0100527 state.state->x.next[0] = (unsigned char)c;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100528 state.state->x.pos--;
529 state.state->past = 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100530 return c;
531}
532
533/* -- see zlib.h -- */
Ed Maste2ce02902023-12-13 19:54:29 -0500534char * ZEXPORT gzgets(gzFile file, char *buf, int len) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100535 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;
Yann Colletcb18fff2019-09-24 17:50:58 -0700543 state.file = file;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100544
545 /* check that we're reading and that there's no (serious) error */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100546 if (state.state->mode != GZ_READ ||
547 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100548 return NULL;
549
550 /* process a skip request */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100551 if (state.state->seek) {
552 state.state->seek = 0;
553 if (gz_skip(state, state.state->skip) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100554 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 */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100564 if (state.state->x.have == 0 && gz_fetch(state) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100565 return NULL; /* error */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100566 if (state.state->x.have == 0) { /* end of file */
567 state.state->past = 1; /* read past end */
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100568 break; /* return what we have */
569 }
570
571 /* look for end-of-line in current output buffer */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100572 n = state.state->x.have > left ? left : state.state->x.have;
573 eol = (unsigned char *)memchr(state.state->x.next, '\n', n);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100574 if (eol != NULL)
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100575 n = (unsigned)(eol - state.state->x.next) + 1;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100576
577 /* copy through end-of-line, or remainder if not found */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100578 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;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100582 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 -- */
Ed Maste2ce02902023-12-13 19:54:29 -0500594int ZEXPORT gzdirect(gzFile file) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100595 gz_statep state;
596
597 /* get internal structure */
598 if (file == NULL)
599 return 0;
Yann Colletcb18fff2019-09-24 17:50:58 -0700600 state.file = file;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100601
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()) */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100604 if (state.state->mode == GZ_READ && state.state->how == LOOK && state.state->x.have == 0)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100605 (void)gz_look(state);
606
607 /* return 1 if transparent, 0 if processing a gzip stream */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100608 return state.state->direct;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100609}
610
611/* -- see zlib.h -- */
Ed Maste2ce02902023-12-13 19:54:29 -0500612int ZEXPORT gzclose_r(gzFile file) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100613 int ret, err;
614 gz_statep state;
615
616 /* get internal structure */
617 if (file == NULL)
618 return Z_STREAM_ERROR;
Yann Colletcb18fff2019-09-24 17:50:58 -0700619 state.file = file;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100620
621 /* check that we're reading */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100622 if (state.state->mode != GZ_READ)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100623 return Z_STREAM_ERROR;
624
625 /* free memory and close file */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100626 if (state.state->size) {
627 inflateEnd(&(state.state->strm));
628 free(state.state->out);
629 free(state.state->in);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100630 }
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100631 err = state.state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100632 gz_error(state, Z_OK, NULL);
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100633 free(state.state->path);
634 ret = close(state.state->fd);
635 free(state.state);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100636 return ret ? Z_ERRNO : err;
637}