Coverage Report

Created: 2026-01-10 06:20

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