git subrepo pull (merge) --force deps/libchdr
[pcsx_rearmed.git] / deps / libchdr / deps / zstd-1.5.6 / zlibWrapper / gzread.c
CommitLineData
648db22b 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
b24e7fce 7 */
8
9#include "gzguts.h"
10
648db22b 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
b24e7fce 19/* Local functions */
648db22b 20local 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));
b24e7fce 27
28/* Use read() to load a buffer -- return -1 on error, otherwise 0. Read from
648db22b 29 state.state->fd, and update state.state->eof, state.state->err, and state.state->msg as appropriate.
b24e7fce 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. */
f535537f 32local int gz_load(gz_statep state, unsigned char *buf, unsigned len,
33 unsigned *have) {
648db22b 34 ssize_t ret;
b24e7fce 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;
648db22b 42 ret = read(state.state->fd, buf + *have, get);
b24e7fce 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)
648db22b 52 state.state->eof = 1;
b24e7fce 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. */
f535537f 63local int gz_avail(gz_statep state)
b24e7fce 64{
65 unsigned got;
648db22b 66 z_streamp strm = &(state.state->strm);
b24e7fce 67
648db22b 68 if (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR)
b24e7fce 69 return -1;
648db22b 70 if (state.state->eof == 0) {
b24e7fce 71 if (strm->avail_in) { /* copy what's there to the start */
648db22b 72 unsigned char *p = state.state->in;
b24e7fce 73 unsigned const char *q = strm->next_in;
74 unsigned n = strm->avail_in;
75 do {
76 *p++ = *q++;
77 } while (--n);
78 }
648db22b 79 if (gz_load(state, state.state->in + strm->avail_in,
80 state.state->size - strm->avail_in, &got) == -1)
b24e7fce 81 return -1;
82 strm->avail_in += got;
648db22b 83 strm->next_in = state.state->in;
b24e7fce 84 }
85 return 0;
86}
87
648db22b 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
b24e7fce 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. */
f535537f 97local int gz_look(gz_statep state) {
648db22b 98 z_streamp strm = &(state.state->strm);
b24e7fce 99
100 /* allocate read buffers and inflate memory */
648db22b 101 if (state.state->size == 0) {
b24e7fce 102 /* allocate buffers */
648db22b 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);
b24e7fce 108 gz_error(state, Z_MEM_ERROR, "out of memory");
109 return -1;
110 }
648db22b 111 state.state->size = state.state->want;
b24e7fce 112
113 /* allocate inflate memory */
648db22b 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;
b24e7fce 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 &&
648db22b 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 */
b24e7fce 146 inflateReset(strm);
648db22b 147 state.state->how = GZIP;
148 state.state->direct = 0;
b24e7fce 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. */
648db22b 154 if (state.state->direct == 0) {
b24e7fce 155 strm->avail_in = 0;
648db22b 156 state.state->eof = 1;
157 state.state->x.have = 0;
b24e7fce 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() */
648db22b 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;
b24e7fce 172 return 0;
173}
174
175/* Decompress from input to the provided next_out and avail_out in the state.
648db22b 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
b24e7fce 179 on success, -1 on failure. */
f535537f 180local int gz_decomp(gz_statep state) {
b24e7fce 181 int ret = Z_OK;
182 unsigned had;
648db22b 183 z_streamp strm = &(state.state->strm);
b24e7fce 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 */
648db22b 215 state.state->x.have = had - strm->avail_out;
216 state.state->x.next = strm->next_out - state.state->x.have;
b24e7fce 217
218 /* if the gzip stream completed successfully, look for another */
219 if (ret == Z_STREAM_END)
648db22b 220 state.state->how = LOOK;
b24e7fce 221
222 /* good decompression */
223 return 0;
224}
225
648db22b 226/* Fetch data and put it in the output buffer. Assumes state.state->x.have is 0.
b24e7fce 227 Data is either copied from the input file or decompressed from the input
648db22b 228 file depending on state.state->how. If state.state->how is LOOK, then a gzip header is
b24e7fce 229 looked for to determine whether to copy or decompress. Returns -1 on error,
648db22b 230 otherwise 0. gz_fetch() will leave state.state->how as COPY or GZIP unless the
b24e7fce 231 end of the input file has been reached and all data has been processed. */
f535537f 232local int gz_fetch(gz_statep state) {
648db22b 233 z_streamp strm = &(state.state->strm);
b24e7fce 234
235 do {
648db22b 236 switch(state.state->how) {
b24e7fce 237 case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */
238 if (gz_look(state) == -1)
239 return -1;
648db22b 240 if (state.state->how == LOOK)
b24e7fce 241 return 0;
242 break;
243 case COPY: /* -> COPY */
648db22b 244 if (gz_load(state, state.state->out, state.state->size << 1, &(state.state->x.have))
b24e7fce 245 == -1)
246 return -1;
648db22b 247 state.state->x.next = state.state->out;
b24e7fce 248 return 0;
249 case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */
648db22b 250 strm->avail_out = state.state->size << 1;
251 strm->next_out = state.state->out;
b24e7fce 252 if (gz_decomp(state) == -1)
253 return -1;
254 }
648db22b 255 } while (state.state->x.have == 0 && (!state.state->eof || strm->avail_in));
b24e7fce 256 return 0;
257}
258
259/* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */
f535537f 260local int gz_skip(gz_statep state, z_off64_t len) {
b24e7fce 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 */
648db22b 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;
b24e7fce 272 len -= n;
273 }
274
275 /* output buffer empty -- return if we're at the end of the input */
648db22b 276 else if (state.state->eof && state.state->strm.avail_in == 0)
b24e7fce 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
648db22b 290 end of file was reached, or there was an error. state.state->err must be
b24e7fce 291 consulted in that case to determine which. */
f535537f 292local z_size_t gz_read(gz_statep state, voidp buf, z_size_t len) {
b24e7fce 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 */
648db22b 301 if (state.state->seek) {
302 state.state->seek = 0;
303 if (gz_skip(state, state.state->skip) == -1)
b24e7fce 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 */
648db22b 311 n = -1;
b24e7fce 312 if (n > len)
9e052883 313 n = (unsigned)len;
b24e7fce 314
315 /* first just try copying data from the output buffer */
648db22b 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;
b24e7fce 322 }
323
324 /* output buffer empty -- return if we're at the end of the input */
648db22b 325 else if (state.state->eof && state.state->strm.avail_in == 0) {
326 state.state->past = 1; /* tried to read past end */
b24e7fce 327 break;
328 }
329
330 /* need output data -- for small len or new stream load up our output
331 buffer */
648db22b 332 else if (state.state->how == LOOK || n < (state.state->size << 1)) {
b24e7fce 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 */
648db22b 342 else if (state.state->how == COPY) { /* read directly */
b24e7fce 343 if (gz_load(state, (unsigned char *)buf, n, &n) == -1)
344 return 0;
345 }
346
347 /* large len -- decompress directly into user buffer */
648db22b 348 else { /* state.state->how == GZIP */
349 state.state->strm.avail_out = n;
350 state.state->strm.next_out = (unsigned char *)buf;
b24e7fce 351 if (gz_decomp(state) == -1)
352 return 0;
648db22b 353 n = state.state->x.have;
354 state.state->x.have = 0;
b24e7fce 355 }
356
357 /* update progress */
358 len -= n;
359 buf = (char *)buf + n;
360 got += n;
648db22b 361 state.state->x.pos += n;
b24e7fce 362 } while (len);
363
364 /* return number of bytes read into user buffer */
365 return got;
366}
367
368/* -- see zlib.h -- */
f535537f 369int ZEXPORT gzread(gzFile file, voidp buf, unsigned len) {
b24e7fce 370 gz_statep state;
371
372 /* get internal structure */
373 if (file == NULL)
374 return -1;
648db22b 375 state.file = file;
b24e7fce 376
377 /* check that we're reading and that there's no (serious) error */
648db22b 378 if (state.state->mode != GZ_READ ||
379 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
b24e7fce 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 */
9e052883 390 len = (unsigned)gz_read(state, buf, len);
b24e7fce 391
392 /* check for an error */
648db22b 393 if (len == 0 && state.state->err != Z_OK && state.state->err != Z_BUF_ERROR)
b24e7fce 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 -- */
f535537f 401z_size_t ZEXPORT gzfread(voidp buf, z_size_t size, z_size_t nitems,
402 gzFile file) {
b24e7fce 403 z_size_t len;
404 gz_statep state;
405
406 /* get internal structure */
407 if (file == NULL)
408 return 0;
648db22b 409 state.file = file;
b24e7fce 410
411 /* check that we're reading and that there's no (serious) error */
648db22b 412 if (state.state->mode != GZ_READ ||
413 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
b24e7fce 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 -- */
648db22b 428#if ZLIB_VERNUM >= 0x1261
b24e7fce 429#ifdef Z_PREFIX_SET
430# undef z_gzgetc
431#else
432# undef gzgetc
433#endif
648db22b 434#endif
435
436#if ZLIB_VERNUM == 0x1260
437# undef gzgetc
438#endif
439
440#if ZLIB_VERNUM <= 0x1250
441ZEXTERN int ZEXPORT gzgetc _Z_OF((gzFile file));
442ZEXTERN int ZEXPORT gzgetc_ _Z_OF((gzFile file));
443#endif
444
f535537f 445int ZEXPORT gzgetc(gzFile file) {
648db22b 446 int ret;
b24e7fce 447 unsigned char buf[1];
448 gz_statep state;
449
450 /* get internal structure */
451 if (file == NULL)
452 return -1;
648db22b 453 state.file = file;
b24e7fce 454
455 /* check that we're reading and that there's no (serious) error */
648db22b 456 if (state.state->mode != GZ_READ ||
457 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
b24e7fce 458 return -1;
459
460 /* try output buffer (no need to check for skip request) */
648db22b 461 if (state.state->x.have) {
462 state.state->x.have--;
463 state.state->x.pos++;
464 return *(state.state->x.next)++;
b24e7fce 465 }
466
467 /* nothing there -- try gz_read() */
648db22b 468 ret = (int)gz_read(state, buf, 1);
469 return ret < 1 ? -1 : buf[0];
b24e7fce 470}
471
f535537f 472int ZEXPORT gzgetc_(gzFile file) {
b24e7fce 473 return gzgetc(file);
474}
475
476/* -- see zlib.h -- */
f535537f 477int ZEXPORT gzungetc(int c, gzFile file) {
b24e7fce 478 gz_statep state;
479
480 /* get internal structure */
481 if (file == NULL)
482 return -1;
648db22b 483 state.file = file;
b24e7fce 484
485 /* check that we're reading and that there's no (serious) error */
648db22b 486 if (state.state->mode != GZ_READ ||
487 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
b24e7fce 488 return -1;
489
490 /* process a skip request */
648db22b 491 if (state.state->seek) {
492 state.state->seek = 0;
493 if (gz_skip(state, state.state->skip) == -1)
b24e7fce 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) */
648db22b 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;
b24e7fce 508 return c;
509 }
510
511 /* if no room, give up (must have already done a gzungetc()) */
648db22b 512 if (state.state->x.have == (state.state->size << 1)) {
b24e7fce 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 */
648db22b 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)
b24e7fce 522 *--dest = *--src;
648db22b 523 state.state->x.next = dest;
b24e7fce 524 }
648db22b 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;
b24e7fce 530 return c;
531}
532
533/* -- see zlib.h -- */
f535537f 534char * ZEXPORT gzgets(gzFile file, char *buf, int len) {
b24e7fce 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;
648db22b 543 state.file = file;
b24e7fce 544
545 /* check that we're reading and that there's no (serious) error */
648db22b 546 if (state.state->mode != GZ_READ ||
547 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
b24e7fce 548 return NULL;
549
550 /* process a skip request */
648db22b 551 if (state.state->seek) {
552 state.state->seek = 0;
553 if (gz_skip(state, state.state->skip) == -1)
b24e7fce 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 */
648db22b 564 if (state.state->x.have == 0 && gz_fetch(state) == -1)
b24e7fce 565 return NULL; /* error */
648db22b 566 if (state.state->x.have == 0) { /* end of file */
567 state.state->past = 1; /* read past end */
b24e7fce 568 break; /* return what we have */
569 }
570
571 /* look for end-of-line in current output buffer */
648db22b 572 n = state.state->x.have > left ? left : state.state->x.have;
573 eol = (unsigned char *)memchr(state.state->x.next, '\n', n);
b24e7fce 574 if (eol != NULL)
648db22b 575 n = (unsigned)(eol - state.state->x.next) + 1;
b24e7fce 576
577 /* copy through end-of-line, or remainder if not found */
648db22b 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;
b24e7fce 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 -- */
f535537f 594int ZEXPORT gzdirect(gzFile file) {
b24e7fce 595 gz_statep state;
596
597 /* get internal structure */
598 if (file == NULL)
599 return 0;
648db22b 600 state.file = file;
b24e7fce 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()) */
648db22b 604 if (state.state->mode == GZ_READ && state.state->how == LOOK && state.state->x.have == 0)
b24e7fce 605 (void)gz_look(state);
606
607 /* return 1 if transparent, 0 if processing a gzip stream */
648db22b 608 return state.state->direct;
b24e7fce 609}
610
611/* -- see zlib.h -- */
f535537f 612int ZEXPORT gzclose_r(gzFile file) {
b24e7fce 613 int ret, err;
614 gz_statep state;
615
616 /* get internal structure */
617 if (file == NULL)
618 return Z_STREAM_ERROR;
648db22b 619 state.file = file;
b24e7fce 620
621 /* check that we're reading */
648db22b 622 if (state.state->mode != GZ_READ)
b24e7fce 623 return Z_STREAM_ERROR;
624
625 /* free memory and close file */
648db22b 626 if (state.state->size) {
627 inflateEnd(&(state.state->strm));
628 free(state.state->out);
629 free(state.state->in);
b24e7fce 630 }
648db22b 631 err = state.state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
b24e7fce 632 gz_error(state, Z_OK, NULL);
648db22b 633 free(state.state->path);
634 ret = close(state.state->fd);
635 free(state.state);
b24e7fce 636 return ret ? Z_ERRNO : err;
637}