blob: 359d178891307a387b008d309037621cb967bd57 [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
Przemyslaw Skibinski957a6d52017-01-18 19:04:00 +01006 * For conditions of distribution and use, see http://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 */
20local int gz_load OF((gz_statep, unsigned char *, unsigned, unsigned *));
21local int gz_avail OF((gz_statep));
22local int gz_look OF((gz_statep));
23local int gz_decomp OF((gz_statep));
24local int gz_fetch OF((gz_statep));
25local int gz_skip OF((gz_statep, z_off64_t));
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +010026local z_size_t gz_read 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. */
32local int gz_load(state, buf, len, have)
33 gz_statep state;
34 unsigned char *buf;
35 unsigned len;
36 unsigned *have;
37{
Przemyslaw Skibinskic3a04de2017-01-18 14:36:10 +010038 ssize_t ret;
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +010039 unsigned get, max = ((unsigned)-1 >> 2) + 1;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010040
41 *have = 0;
42 do {
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +010043 get = len - *have;
44 if (get > max)
45 get = max;
46 ret = read(state.state->fd, buf + *have, get);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010047 if (ret <= 0)
48 break;
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +010049 *have += (unsigned)ret;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010050 } while (*have < len);
51 if (ret < 0) {
52 gz_error(state, Z_ERRNO, zstrerror());
53 return -1;
54 }
55 if (ret == 0)
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010056 state.state->eof = 1;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010057 return 0;
58}
59
60/* Load up input buffer and set eof flag if last data loaded -- return -1 on
61 error, 0 otherwise. Note that the eof flag is set when the end of the input
62 file is reached, even though there may be unused data in the buffer. Once
63 that data has been used, no more attempts will be made to read the file.
64 If strm->avail_in != 0, then the current data is moved to the beginning of
65 the input buffer, and then the remainder of the buffer is loaded with the
66 available data from the input file. */
67local int gz_avail(state)
68 gz_statep state;
69{
70 unsigned got;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010071 z_streamp strm = &(state.state->strm);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010072
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010073 if (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010074 return -1;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010075 if (state.state->eof == 0) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010076 if (strm->avail_in) { /* copy what's there to the start */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010077 unsigned char *p = state.state->in;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010078 unsigned const char *q = strm->next_in;
79 unsigned n = strm->avail_in;
80 do {
81 *p++ = *q++;
82 } while (--n);
83 }
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010084 if (gz_load(state, state.state->in + strm->avail_in,
85 state.state->size - strm->avail_in, &got) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010086 return -1;
87 strm->avail_in += got;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010088 strm->next_in = state.state->in;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010089 }
90 return 0;
91}
92
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +010093/* Look for gzip header, set up for inflate or copy. state.state->x.have must be 0.
94 If this is the first time in, allocate required memory. state.state->how will be
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010095 left unchanged if there is no more input data available, will be set to COPY
96 if there is no gzip header and direct copying will be performed, or it will
97 be set to GZIP for decompression. If direct copying, then leftover input
98 data from the input buffer will be copied to the output buffer. In that
99 case, all further file reads will be directly to either the output buffer or
100 a user buffer. If decompressing, the inflate state will be initialized.
101 gz_look() will return 0 on success or -1 on failure. */
102local int gz_look(state)
103 gz_statep state;
104{
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100105 z_streamp strm = &(state.state->strm);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100106
107 /* allocate read buffers and inflate memory */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100108 if (state.state->size == 0) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100109 /* allocate buffers */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100110 state.state->in = (unsigned char *)malloc(state.state->want);
111 state.state->out = (unsigned char *)malloc(state.state->want << 1);
112 if (state.state->in == NULL || state.state->out == NULL) {
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100113 free(state.state->out);
114 free(state.state->in);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100115 gz_error(state, Z_MEM_ERROR, "out of memory");
116 return -1;
117 }
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100118 state.state->size = state.state->want;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100119
120 /* allocate inflate memory */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100121 state.state->strm.zalloc = Z_NULL;
122 state.state->strm.zfree = Z_NULL;
123 state.state->strm.opaque = Z_NULL;
124 state.state->strm.avail_in = 0;
125 state.state->strm.next_in = Z_NULL;
126 if (inflateInit2(&(state.state->strm), 15 + 16) != Z_OK) { /* gunzip */
127 free(state.state->out);
128 free(state.state->in);
129 state.state->size = 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100130 gz_error(state, Z_MEM_ERROR, "out of memory");
131 return -1;
132 }
133 }
134
135 /* get at least the magic bytes in the input buffer */
136 if (strm->avail_in < 2) {
137 if (gz_avail(state) == -1)
138 return -1;
139 if (strm->avail_in == 0)
140 return 0;
141 }
142
143 /* look for gzip magic bytes -- if there, do gzip decoding (note: there is
144 a logical dilemma here when considering the case of a partially written
145 gzip file, to wit, if a single 31 byte is written, then we cannot tell
146 whether this is a single-byte file, or just a partially written gzip
147 file -- for here we assume that if a gzip file is being written, then
148 the header will be written in a single operation, so that reading a
149 single byte is sufficient indication that it is not a gzip file) */
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100150 if (strm->avail_in > 1 &&
Przemyslaw Skibinskidc2fe752016-12-01 11:56:20 +0100151 ((strm->next_in[0] == 31 && strm->next_in[1] == 139) /* gz header */
152 || (strm->next_in[0] == 40 && strm->next_in[1] == 181))) { /* zstd header */
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100153 inflateReset(strm);
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100154 state.state->how = GZIP;
155 state.state->direct = 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100156 return 0;
157 }
158
159 /* no gzip header -- if we were decoding gzip before, then this is trailing
160 garbage. Ignore the trailing garbage and finish. */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100161 if (state.state->direct == 0) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100162 strm->avail_in = 0;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100163 state.state->eof = 1;
164 state.state->x.have = 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100165 return 0;
166 }
167
168 /* doing raw i/o, copy any leftover input to output -- this assumes that
169 the output buffer is larger than the input buffer, which also assures
170 space for gzungetc() */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100171 state.state->x.next = state.state->out;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100172 if (strm->avail_in) {
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100173 memcpy(state.state->x.next, strm->next_in, strm->avail_in);
174 state.state->x.have = strm->avail_in;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100175 strm->avail_in = 0;
176 }
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100177 state.state->how = COPY;
178 state.state->direct = 1;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100179 return 0;
180}
181
182/* Decompress from input to the provided next_out and avail_out in the state.
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100183 On return, state.state->x.have and state.state->x.next point to the just decompressed
184 data. If the gzip stream completes, state.state->how is reset to LOOK to look for
185 the next gzip stream or raw data, once state.state->x.have is depleted. Returns 0
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100186 on success, -1 on failure. */
187local int gz_decomp(state)
188 gz_statep state;
189{
190 int ret = Z_OK;
191 unsigned had;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100192 z_streamp strm = &(state.state->strm);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100193
194 /* fill output buffer up to end of deflate stream */
195 had = strm->avail_out;
196 do {
197 /* get more input for inflate() */
198 if (strm->avail_in == 0 && gz_avail(state) == -1)
199 return -1;
200 if (strm->avail_in == 0) {
201 gz_error(state, Z_BUF_ERROR, "unexpected end of file");
202 break;
203 }
204
205 /* decompress and handle errors */
206 ret = inflate(strm, Z_NO_FLUSH);
207 if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
208 gz_error(state, Z_STREAM_ERROR,
209 "internal error: inflate stream corrupt");
210 return -1;
211 }
212 if (ret == Z_MEM_ERROR) {
213 gz_error(state, Z_MEM_ERROR, "out of memory");
214 return -1;
215 }
216 if (ret == Z_DATA_ERROR) { /* deflate stream invalid */
217 gz_error(state, Z_DATA_ERROR,
218 strm->msg == NULL ? "compressed data error" : strm->msg);
219 return -1;
220 }
221 } while (strm->avail_out && ret != Z_STREAM_END);
222
223 /* update available output */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100224 state.state->x.have = had - strm->avail_out;
225 state.state->x.next = strm->next_out - state.state->x.have;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100226
227 /* if the gzip stream completed successfully, look for another */
228 if (ret == Z_STREAM_END)
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100229 state.state->how = LOOK;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100230
231 /* good decompression */
232 return 0;
233}
234
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100235/* Fetch data and put it in the output buffer. Assumes state.state->x.have is 0.
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100236 Data is either copied from the input file or decompressed from the input
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100237 file depending on state.state->how. If state.state->how is LOOK, then a gzip header is
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100238 looked for to determine whether to copy or decompress. Returns -1 on error,
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100239 otherwise 0. gz_fetch() will leave state.state->how as COPY or GZIP unless the
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100240 end of the input file has been reached and all data has been processed. */
241local int gz_fetch(state)
242 gz_statep state;
243{
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100244 z_streamp strm = &(state.state->strm);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100245
246 do {
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100247 switch(state.state->how) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100248 case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */
249 if (gz_look(state) == -1)
250 return -1;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100251 if (state.state->how == LOOK)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100252 return 0;
253 break;
254 case COPY: /* -> COPY */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100255 if (gz_load(state, state.state->out, state.state->size << 1, &(state.state->x.have))
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100256 == -1)
257 return -1;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100258 state.state->x.next = state.state->out;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100259 return 0;
260 case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100261 strm->avail_out = state.state->size << 1;
262 strm->next_out = state.state->out;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100263 if (gz_decomp(state) == -1)
264 return -1;
265 }
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100266 } while (state.state->x.have == 0 && (!state.state->eof || strm->avail_in));
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100267 return 0;
268}
269
270/* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */
271local int gz_skip(state, len)
272 gz_statep state;
273 z_off64_t len;
274{
275 unsigned n;
276
277 /* skip over len bytes or reach end-of-file, whichever comes first */
278 while (len)
279 /* skip over whatever is in output buffer */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100280 if (state.state->x.have) {
281 n = GT_OFF(state.state->x.have) || (z_off64_t)state.state->x.have > len ?
282 (unsigned)len : state.state->x.have;
283 state.state->x.have -= n;
284 state.state->x.next += n;
285 state.state->x.pos += n;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100286 len -= n;
287 }
288
289 /* output buffer empty -- return if we're at the end of the input */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100290 else if (state.state->eof && state.state->strm.avail_in == 0)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100291 break;
292
293 /* need more data to skip -- load up output buffer */
294 else {
295 /* get more output, looking for header if required */
296 if (gz_fetch(state) == -1)
297 return -1;
298 }
299 return 0;
300}
301
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100302/* Read len bytes into buf from file, or less than len up to the end of the
303 input. Return the number of bytes read. If zero is returned, either the
304 end of file was reached, or there was an error. state.state->err must be
305 consulted in that case to determine which. */
306local z_size_t gz_read(state, buf, len)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100307 gz_statep state;
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100308 voidp buf;
309 z_size_t len;
310{
311 z_size_t got;
312 unsigned n;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100313
314 /* if len is zero, avoid unnecessary operations */
315 if (len == 0)
316 return 0;
317
318 /* process a skip request */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100319 if (state.state->seek) {
320 state.state->seek = 0;
321 if (gz_skip(state, state.state->skip) == -1)
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100322 return 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100323 }
324
325 /* get len bytes to buf, or less than len if at the end */
326 got = 0;
327 do {
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100328 /* set n to the maximum amount of len that fits in an unsigned int */
329 n = -1;
330 if (n > len)
Przemyslaw Skibinskic3a04de2017-01-18 14:36:10 +0100331 n = (unsigned)len;
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100332
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100333 /* first just try copying data from the output buffer */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100334 if (state.state->x.have) {
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100335 if (state.state->x.have < n)
336 n = state.state->x.have;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100337 memcpy(buf, state.state->x.next, n);
338 state.state->x.next += n;
339 state.state->x.have -= n;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100340 }
341
342 /* output buffer empty -- return if we're at the end of the input */
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100343 else if (state.state->eof && state.state->strm.avail_in == 0) {
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100344 state.state->past = 1; /* tried to read past end */
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100345 break;
346 }
347
348 /* need output data -- for small len or new stream load up our output
349 buffer */
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100350 else if (state.state->how == LOOK || n < (state.state->size << 1)) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100351 /* get more output, looking for header if required */
352 if (gz_fetch(state) == -1)
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100353 return 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100354 continue; /* no progress yet -- go back to copy above */
355 /* the copy above assures that we will leave with space in the
356 output buffer, allowing at least one gzungetc() to succeed */
357 }
358
359 /* large len -- read directly into user buffer */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100360 else if (state.state->how == COPY) { /* read directly */
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100361 if (gz_load(state, (unsigned char *)buf, n, &n) == -1)
362 return 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100363 }
364
365 /* large len -- decompress directly into user buffer */
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100366 else { /* state.state->how == GZIP */
367 state.state->strm.avail_out = n;
368 state.state->strm.next_out = (unsigned char *)buf;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100369 if (gz_decomp(state) == -1)
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100370 return 0;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100371 n = state.state->x.have;
372 state.state->x.have = 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100373 }
374
375 /* update progress */
376 len -= n;
377 buf = (char *)buf + n;
378 got += n;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100379 state.state->x.pos += n;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100380 } while (len);
381
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100382 /* return number of bytes read into user buffer */
383 return got;
384}
385
386/* -- see zlib.h -- */
387int ZEXPORT gzread(file, buf, len)
388 gzFile file;
389 voidp buf;
390 unsigned len;
391{
392 gz_statep state;
393
394 /* get internal structure */
395 if (file == NULL)
396 return -1;
Yann Colletcb18fff2019-09-24 17:50:58 -0700397 state.file = file;
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100398
399 /* check that we're reading and that there's no (serious) error */
400 if (state.state->mode != GZ_READ ||
401 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
402 return -1;
403
404 /* since an int is returned, make sure len fits in one, otherwise return
405 with an error (this avoids a flaw in the interface) */
406 if ((int)len < 0) {
407 gz_error(state, Z_STREAM_ERROR, "request does not fit in an int");
408 return -1;
409 }
410
411 /* read len or fewer bytes to buf */
Przemyslaw Skibinskic3a04de2017-01-18 14:36:10 +0100412 len = (unsigned)gz_read(state, buf, len);
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100413
414 /* check for an error */
415 if (len == 0 && state.state->err != Z_OK && state.state->err != Z_BUF_ERROR)
416 return -1;
417
418 /* return the number of bytes read (this is assured to fit in an int) */
419 return (int)len;
420}
421
422/* -- see zlib.h -- */
423z_size_t ZEXPORT gzfread(buf, size, nitems, file)
424 voidp buf;
425 z_size_t size;
426 z_size_t nitems;
427 gzFile file;
428{
429 z_size_t len;
430 gz_statep state;
431
432 /* get internal structure */
433 if (file == NULL)
434 return 0;
Yann Colletcb18fff2019-09-24 17:50:58 -0700435 state.file = file;
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100436
437 /* check that we're reading and that there's no (serious) error */
438 if (state.state->mode != GZ_READ ||
439 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
440 return 0;
441
442 /* compute bytes to read -- error on overflow */
443 len = nitems * size;
444 if (size && len / size != nitems) {
445 gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
446 return 0;
447 }
448
449 /* read len or fewer bytes to buf, return the number of full items read */
450 return len ? gz_read(state, buf, len) / size : 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100451}
452
453/* -- see zlib.h -- */
Przemyslaw Skibinskic77befe2016-11-28 14:09:26 +0100454#if ZLIB_VERNUM >= 0x1261
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100455#ifdef Z_PREFIX_SET
456# undef z_gzgetc
457#else
458# undef gzgetc
459#endif
Przemyslaw Skibinskic77befe2016-11-28 14:09:26 +0100460#endif
461
462#if ZLIB_VERNUM == 0x1260
463# undef gzgetc
464#endif
465
466#if ZLIB_VERNUM <= 0x1250
467ZEXTERN int ZEXPORT gzgetc OF((gzFile file));
468ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file));
469#endif
470
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100471int ZEXPORT gzgetc(file)
472 gzFile file;
473{
474 int ret;
475 unsigned char buf[1];
476 gz_statep state;
477
478 /* get internal structure */
479 if (file == NULL)
480 return -1;
Yann Colletcb18fff2019-09-24 17:50:58 -0700481 state.file = file;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100482
483 /* check that we're reading and that there's no (serious) error */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100484 if (state.state->mode != GZ_READ ||
485 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100486 return -1;
487
488 /* try output buffer (no need to check for skip request) */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100489 if (state.state->x.have) {
490 state.state->x.have--;
491 state.state->x.pos++;
492 return *(state.state->x.next)++;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100493 }
494
Przemyslaw Skibinski5735fd72017-01-18 12:14:01 +0100495 /* nothing there -- try gz_read() */
Yann Colletcb18fff2019-09-24 17:50:58 -0700496 ret = (int)gz_read(state, buf, 1);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100497 return ret < 1 ? -1 : buf[0];
498}
499
500int ZEXPORT gzgetc_(file)
501gzFile file;
502{
503 return gzgetc(file);
504}
505
506/* -- see zlib.h -- */
507int ZEXPORT gzungetc(c, file)
508 int c;
509 gzFile file;
510{
511 gz_statep state;
512
513 /* get internal structure */
514 if (file == NULL)
515 return -1;
Yann Colletcb18fff2019-09-24 17:50:58 -0700516 state.file = file;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100517
518 /* check that we're reading and that there's no (serious) error */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100519 if (state.state->mode != GZ_READ ||
520 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100521 return -1;
522
523 /* process a skip request */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100524 if (state.state->seek) {
525 state.state->seek = 0;
526 if (gz_skip(state, state.state->skip) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100527 return -1;
528 }
529
530 /* can't push EOF */
531 if (c < 0)
532 return -1;
533
534 /* if output buffer empty, put byte at end (allows more pushing) */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100535 if (state.state->x.have == 0) {
536 state.state->x.have = 1;
537 state.state->x.next = state.state->out + (state.state->size << 1) - 1;
Yann Collet9ceb49e2016-12-22 15:26:33 +0100538 state.state->x.next[0] = (unsigned char)c;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100539 state.state->x.pos--;
540 state.state->past = 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100541 return c;
542 }
543
544 /* if no room, give up (must have already done a gzungetc()) */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100545 if (state.state->x.have == (state.state->size << 1)) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100546 gz_error(state, Z_DATA_ERROR, "out of room to push characters");
547 return -1;
548 }
549
550 /* slide output data if needed and insert byte before existing data */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100551 if (state.state->x.next == state.state->out) {
552 unsigned char *src = state.state->out + state.state->x.have;
553 unsigned char *dest = state.state->out + (state.state->size << 1);
554 while (src > state.state->out)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100555 *--dest = *--src;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100556 state.state->x.next = dest;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100557 }
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100558 state.state->x.have++;
559 state.state->x.next--;
Yann Collet9ceb49e2016-12-22 15:26:33 +0100560 state.state->x.next[0] = (unsigned char)c;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100561 state.state->x.pos--;
562 state.state->past = 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100563 return c;
564}
565
566/* -- see zlib.h -- */
567char * ZEXPORT gzgets(file, buf, len)
568 gzFile file;
569 char *buf;
570 int len;
571{
572 unsigned left, n;
573 char *str;
574 unsigned char *eol;
575 gz_statep state;
576
577 /* check parameters and get internal structure */
578 if (file == NULL || buf == NULL || len < 1)
579 return NULL;
Yann Colletcb18fff2019-09-24 17:50:58 -0700580 state.file = file;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100581
582 /* check that we're reading and that there's no (serious) error */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100583 if (state.state->mode != GZ_READ ||
584 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100585 return NULL;
586
587 /* process a skip request */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100588 if (state.state->seek) {
589 state.state->seek = 0;
590 if (gz_skip(state, state.state->skip) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100591 return NULL;
592 }
593
594 /* copy output bytes up to new line or len - 1, whichever comes first --
595 append a terminating zero to the string (we don't check for a zero in
596 the contents, let the user worry about that) */
597 str = buf;
598 left = (unsigned)len - 1;
599 if (left) do {
600 /* assure that something is in the output buffer */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100601 if (state.state->x.have == 0 && gz_fetch(state) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100602 return NULL; /* error */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100603 if (state.state->x.have == 0) { /* end of file */
604 state.state->past = 1; /* read past end */
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100605 break; /* return what we have */
606 }
607
608 /* look for end-of-line in current output buffer */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100609 n = state.state->x.have > left ? left : state.state->x.have;
610 eol = (unsigned char *)memchr(state.state->x.next, '\n', n);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100611 if (eol != NULL)
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100612 n = (unsigned)(eol - state.state->x.next) + 1;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100613
614 /* copy through end-of-line, or remainder if not found */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100615 memcpy(buf, state.state->x.next, n);
616 state.state->x.have -= n;
617 state.state->x.next += n;
618 state.state->x.pos += n;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100619 left -= n;
620 buf += n;
621 } while (left && eol == NULL);
622
623 /* return terminated string, or if nothing, end of file */
624 if (buf == str)
625 return NULL;
626 buf[0] = 0;
627 return str;
628}
629
630/* -- see zlib.h -- */
631int ZEXPORT gzdirect(file)
632 gzFile file;
633{
634 gz_statep state;
635
636 /* get internal structure */
637 if (file == NULL)
638 return 0;
Yann Colletcb18fff2019-09-24 17:50:58 -0700639 state.file = file;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100640
641 /* if the state is not known, but we can find out, then do so (this is
642 mainly for right after a gzopen() or gzdopen()) */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100643 if (state.state->mode == GZ_READ && state.state->how == LOOK && state.state->x.have == 0)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100644 (void)gz_look(state);
645
646 /* return 1 if transparent, 0 if processing a gzip stream */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100647 return state.state->direct;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100648}
649
650/* -- see zlib.h -- */
651int ZEXPORT gzclose_r(file)
652 gzFile file;
653{
654 int ret, err;
655 gz_statep state;
656
657 /* get internal structure */
658 if (file == NULL)
659 return Z_STREAM_ERROR;
Yann Colletcb18fff2019-09-24 17:50:58 -0700660 state.file = file;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100661
662 /* check that we're reading */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100663 if (state.state->mode != GZ_READ)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100664 return Z_STREAM_ERROR;
665
666 /* free memory and close file */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100667 if (state.state->size) {
668 inflateEnd(&(state.state->strm));
669 free(state.state->out);
670 free(state.state->in);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100671 }
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100672 err = state.state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100673 gz_error(state, Z_OK, NULL);
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100674 free(state.state->path);
675 ret = close(state.state->fd);
676 free(state.state);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100677 return ret ? Z_ERRNO : err;
678}