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