1 /* gzread.c -- zlib functions for reading gzip files
2 * Copyright (C) 2004, 2005, 2010, 2011, 2012 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
9 local
int gz_load
OF((gz_statep
, unsigned char *, unsigned, unsigned *));
10 local
int gz_avail
OF((gz_statep
));
11 local
int gz_look
OF((gz_statep
));
12 local
int gz_decomp
OF((gz_statep
));
13 local
int gz_fetch
OF((gz_statep
));
14 local
int gz_skip
OF((gz_statep
, z_off64_t
));
16 /* Use read() to load a buffer -- return -1 on error, otherwise 0. Read from
17 state->fd, and update state->eof, state->err, and state->msg as appropriate.
18 This function needs to loop on read(), since read() is not guaranteed to
19 read the number of bytes requested, depending on the type of descriptor. */
20 local
int gz_load(state
, buf
, len
, have
)
30 ret
= read(state
->fd
, buf
+ *have
, len
- *have
);
34 } while (*have
< len
);
36 gz_error(state
, Z_ERRNO
, zstrerror());
44 /* Load up input buffer and set eof flag if last data loaded -- return -1 on
45 error, 0 otherwise. Note that the eof flag is set when the end of the input
46 file is reached, even though there may be unused data in the buffer. Once
47 that data has been used, no more attempts will be made to read the file.
48 If strm->avail_in != 0, then the current data is moved to the beginning of
49 the input buffer, and then the remainder of the buffer is loaded with the
50 available data from the input file. */
51 local
int gz_avail(state
)
55 z_streamp strm
= &(state
->strm
);
57 if (state
->err
!= Z_OK
&& state
->err
!= Z_BUF_ERROR
)
59 if (state
->eof
== 0) {
60 if (strm
->avail_in
) { /* copy what's there to the start */
61 unsigned char *p
= state
->in
, *q
= strm
->next_in
;
62 unsigned n
= strm
->avail_in
;
67 if (gz_load(state
, state
->in
+ strm
->avail_in
,
68 state
->size
- strm
->avail_in
, &got
) == -1)
70 strm
->avail_in
+= got
;
71 strm
->next_in
= state
->in
;
76 /* Look for gzip header, set up for inflate or copy. state->x.have must be 0.
77 If this is the first time in, allocate required memory. state->how will be
78 left unchanged if there is no more input data available, will be set to COPY
79 if there is no gzip header and direct copying will be performed, or it will
80 be set to GZIP for decompression. If direct copying, then leftover input
81 data from the input buffer will be copied to the output buffer. In that
82 case, all further file reads will be directly to either the output buffer or
83 a user buffer. If decompressing, the inflate state will be initialized.
84 gz_look() will return 0 on success or -1 on failure. */
85 local
int gz_look(state
)
88 z_streamp strm
= &(state
->strm
);
90 /* allocate read buffers and inflate memory */
91 if (state
->size
== 0) {
92 /* allocate buffers */
93 state
->in
= malloc(state
->want
);
94 state
->out
= malloc(state
->want
<< 1);
95 if (state
->in
== NULL
|| state
->out
== NULL
) {
96 if (state
->out
!= NULL
)
98 if (state
->in
!= NULL
)
100 gz_error(state
, Z_MEM_ERROR
, "out of memory");
103 state
->size
= state
->want
;
105 /* allocate inflate memory */
106 state
->strm
.zalloc
= Z_NULL
;
107 state
->strm
.zfree
= Z_NULL
;
108 state
->strm
.opaque
= Z_NULL
;
109 state
->strm
.avail_in
= 0;
110 state
->strm
.next_in
= Z_NULL
;
111 if (inflateInit2(&(state
->strm
), 15 + 16) != Z_OK
) { /* gunzip */
115 gz_error(state
, Z_MEM_ERROR
, "out of memory");
120 /* get at least the magic bytes in the input buffer */
121 if (strm
->avail_in
< 2) {
122 if (gz_avail(state
) == -1)
124 if (strm
->avail_in
== 0)
128 /* look for gzip magic bytes -- if there, do gzip decoding (note: there is
129 a logical dilemma here when considering the case of a partially written
130 gzip file, to wit, if a single 31 byte is written, then we cannot tell
131 whether this is a single-byte file, or just a partially written gzip
132 file -- for here we assume that if a gzip file is being written, then
133 the header will be written in a single operation, so that reading a
134 single byte is sufficient indication that it is not a gzip file) */
135 if (strm
->avail_in
> 1 &&
136 strm
->next_in
[0] == 31 && strm
->next_in
[1] == 139) {
143 /* no gzip header -- if we were decoding gzip before, then this is trailing
144 garbage. Ignore the trailing garbage and finish. */
145 if (state
->direct
== 0) {
152 /* doing raw i/o, copy any leftover input to output -- this assumes that
153 the output buffer is larger than the input buffer, which also assures
154 space for gzungetc() */
155 state
->x
.next
= state
->out
;
156 if (strm
->avail_in
) {
157 memcpy(state
->x
.next
, strm
->next_in
, strm
->avail_in
);
158 state
->x
.have
= strm
->avail_in
;
166 /* Decompress from input to the provided next_out and avail_out in the state.
167 On return, state->x.have and state->x.next point to the just decompressed
168 data. If the gzip stream completes, state->how is reset to LOOK to look for
169 the next gzip stream or raw data, once state->x.have is depleted. Returns 0
170 on success, -1 on failure. */
171 local
int gz_decomp(state
)
176 z_streamp strm
= &(state
->strm
);
178 /* fill output buffer up to end of deflate stream */
179 had
= strm
->avail_out
;
181 /* get more input for inflate() */
182 if (strm
->avail_in
== 0 && gz_avail(state
) == -1)
184 if (strm
->avail_in
== 0) {
185 gz_error(state
, Z_BUF_ERROR
, "unexpected end of file");
189 /* decompress and handle errors */
190 ret
= inflate(strm
, Z_NO_FLUSH
);
191 if (ret
== Z_STREAM_ERROR
|| ret
== Z_NEED_DICT
) {
192 gz_error(state
, Z_STREAM_ERROR
,
193 "internal error: inflate stream corrupt");
196 if (ret
== Z_MEM_ERROR
) {
197 gz_error(state
, Z_MEM_ERROR
, "out of memory");
200 if (ret
== Z_DATA_ERROR
) { /* deflate stream invalid */
201 gz_error(state
, Z_DATA_ERROR
,
202 strm
->msg
== NULL
? "compressed data error" : strm
->msg
);
205 } while (strm
->avail_out
&& ret
!= Z_STREAM_END
);
207 /* update available output */
208 state
->x
.have
= had
- strm
->avail_out
;
209 state
->x
.next
= strm
->next_out
- state
->x
.have
;
211 /* if the gzip stream completed successfully, look for another */
212 if (ret
== Z_STREAM_END
)
215 /* good decompression */
219 /* Fetch data and put it in the output buffer. Assumes state->x.have is 0.
220 Data is either copied from the input file or decompressed from the input
221 file depending on state->how. If state->how is LOOK, then a gzip header is
222 looked for to determine whether to copy or decompress. Returns -1 on error,
223 otherwise 0. gz_fetch() will leave state->how as COPY or GZIP unless the
224 end of the input file has been reached and all data has been processed. */
225 local
int gz_fetch(state
)
228 z_streamp strm
= &(state
->strm
);
232 case LOOK
: /* -> LOOK, COPY (only if never GZIP), or GZIP */
233 if (gz_look(state
) == -1)
235 if (state
->how
== LOOK
)
238 case COPY
: /* -> COPY */
239 if (gz_load(state
, state
->out
, state
->size
<< 1, &(state
->x
.have
))
242 state
->x
.next
= state
->out
;
244 case GZIP
: /* -> GZIP or LOOK (if end of gzip stream) */
245 strm
->avail_out
= state
->size
<< 1;
246 strm
->next_out
= state
->out
;
247 if (gz_decomp(state
) == -1)
250 } while (state
->x
.have
== 0 && (!state
->eof
|| strm
->avail_in
));
254 /* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */
255 local
int gz_skip(state
, len
)
261 /* skip over len bytes or reach end-of-file, whichever comes first */
263 /* skip over whatever is in output buffer */
265 n
= GT_OFF(state
->x
.have
) || (z_off64_t
)state
->x
.have
> len
?
266 (unsigned)len
: state
->x
.have
;
273 /* output buffer empty -- return if we're at the end of the input */
274 else if (state
->eof
&& state
->strm
.avail_in
== 0)
277 /* need more data to skip -- load up output buffer */
279 /* get more output, looking for header if required */
280 if (gz_fetch(state
) == -1)
286 /* -- see zlib.h -- */
287 int ZEXPORT
gzread(file
, buf
, len
)
296 /* get internal structure */
299 state
= (gz_statep
)file
;
300 strm
= &(state
->strm
);
302 /* check that we're reading and that there's no (serious) error */
303 if (state
->mode
!= GZ_READ
||
304 (state
->err
!= Z_OK
&& state
->err
!= Z_BUF_ERROR
))
307 /* since an int is returned, make sure len fits in one, otherwise return
308 with an error (this avoids the flaw in the interface) */
310 gz_error(state
, Z_DATA_ERROR
, "requested length does not fit in int");
314 /* if len is zero, avoid unnecessary operations */
318 /* process a skip request */
321 if (gz_skip(state
, state
->skip
) == -1)
325 /* get len bytes to buf, or less than len if at the end */
328 /* first just try copying data from the output buffer */
330 n
= state
->x
.have
> len
? len
: state
->x
.have
;
331 memcpy(buf
, state
->x
.next
, n
);
336 /* output buffer empty -- return if we're at the end of the input */
337 else if (state
->eof
&& strm
->avail_in
== 0) {
338 state
->past
= 1; /* tried to read past end */
342 /* need output data -- for small len or new stream load up our output
344 else if (state
->how
== LOOK
|| len
< (state
->size
<< 1)) {
345 /* get more output, looking for header if required */
346 if (gz_fetch(state
) == -1)
348 continue; /* no progress yet -- go back to copy above */
349 /* the copy above assures that we will leave with space in the
350 output buffer, allowing at least one gzungetc() to succeed */
353 /* large len -- read directly into user buffer */
354 else if (state
->how
== COPY
) { /* read directly */
355 if (gz_load(state
, buf
, len
, &n
) == -1)
359 /* large len -- decompress directly into user buffer */
360 else { /* state->how == GZIP */
361 strm
->avail_out
= len
;
362 strm
->next_out
= buf
;
363 if (gz_decomp(state
) == -1)
369 /* update progress */
371 buf
= (char *)buf
+ n
;
376 /* return number of bytes read into user buffer (will fit in int) */
380 /* -- see zlib.h -- */
382 int ZEXPORT
gzgetc(file
)
386 unsigned char buf
[1];
389 /* get internal structure */
392 state
= (gz_statep
)file
;
394 /* check that we're reading and that there's no (serious) error */
395 if (state
->mode
!= GZ_READ
||
396 (state
->err
!= Z_OK
&& state
->err
!= Z_BUF_ERROR
))
399 /* try output buffer (no need to check for skip request) */
403 return *(state
->x
.next
)++;
406 /* nothing there -- try gzread() */
407 ret
= gzread(file
, buf
, 1);
408 return ret
< 1 ? -1 : buf
[0];
411 int ZEXPORT
gzgetc_(file
)
417 /* -- see zlib.h -- */
418 int ZEXPORT
gzungetc(c
, file
)
424 /* get internal structure */
427 state
= (gz_statep
)file
;
429 /* check that we're reading and that there's no (serious) error */
430 if (state
->mode
!= GZ_READ
||
431 (state
->err
!= Z_OK
&& state
->err
!= Z_BUF_ERROR
))
434 /* process a skip request */
437 if (gz_skip(state
, state
->skip
) == -1)
445 /* if output buffer empty, put byte at end (allows more pushing) */
446 if (state
->x
.have
== 0) {
448 state
->x
.next
= state
->out
+ (state
->size
<< 1) - 1;
449 state
->x
.next
[0] = c
;
455 /* if no room, give up (must have already done a gzungetc()) */
456 if (state
->x
.have
== (state
->size
<< 1)) {
457 gz_error(state
, Z_DATA_ERROR
, "out of room to push characters");
461 /* slide output data if needed and insert byte before existing data */
462 if (state
->x
.next
== state
->out
) {
463 unsigned char *src
= state
->out
+ state
->x
.have
;
464 unsigned char *dest
= state
->out
+ (state
->size
<< 1);
465 while (src
> state
->out
)
467 state
->x
.next
= dest
;
471 state
->x
.next
[0] = c
;
477 /* -- see zlib.h -- */
478 char * ZEXPORT
gzgets(file
, buf
, len
)
488 /* check parameters and get internal structure */
489 if (file
== NULL
|| buf
== NULL
|| len
< 1)
491 state
= (gz_statep
)file
;
493 /* check that we're reading and that there's no (serious) error */
494 if (state
->mode
!= GZ_READ
||
495 (state
->err
!= Z_OK
&& state
->err
!= Z_BUF_ERROR
))
498 /* process a skip request */
501 if (gz_skip(state
, state
->skip
) == -1)
505 /* copy output bytes up to new line or len - 1, whichever comes first --
506 append a terminating zero to the string (we don't check for a zero in
507 the contents, let the user worry about that) */
509 left
= (unsigned)len
- 1;
511 /* assure that something is in the output buffer */
512 if (state
->x
.have
== 0 && gz_fetch(state
) == -1)
513 return NULL
; /* error */
514 if (state
->x
.have
== 0) { /* end of file */
515 state
->past
= 1; /* read past end */
516 break; /* return what we have */
519 /* look for end-of-line in current output buffer */
520 n
= state
->x
.have
> left
? left
: state
->x
.have
;
521 eol
= memchr(state
->x
.next
, '\n', n
);
523 n
= (unsigned)(eol
- state
->x
.next
) + 1;
525 /* copy through end-of-line, or remainder if not found */
526 memcpy(buf
, state
->x
.next
, n
);
532 } while (left
&& eol
== NULL
);
534 /* return terminated string, or if nothing, end of file */
541 /* -- see zlib.h -- */
542 int ZEXPORT
gzdirect(file
)
547 /* get internal structure */
550 state
= (gz_statep
)file
;
552 /* if the state is not known, but we can find out, then do so (this is
553 mainly for right after a gzopen() or gzdopen()) */
554 if (state
->mode
== GZ_READ
&& state
->how
== LOOK
&& state
->x
.have
== 0)
555 (void)gz_look(state
);
557 /* return 1 if transparent, 0 if processing a gzip stream */
558 return state
->direct
;
561 /* -- see zlib.h -- */
562 int ZEXPORT
gzclose_r(file
)
568 /* get internal structure */
570 return Z_STREAM_ERROR
;
571 state
= (gz_statep
)file
;
573 /* check that we're reading */
574 if (state
->mode
!= GZ_READ
)
575 return Z_STREAM_ERROR
;
577 /* free memory and close file */
579 inflateEnd(&(state
->strm
));
583 err
= state
->err
== Z_BUF_ERROR
? Z_BUF_ERROR
: Z_OK
;
584 gz_error(state
, Z_OK
, NULL
);
586 ret
= close(state
->fd
);
588 return ret
? Z_ERRNO
: err
;