1 /* gzread.c -- zlib functions for reading gzip files
2 * Copyright (C) 2004, 2005, 2010 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
11 local int gz_load OF((gz_statep, unsigned char *, unsigned, unsigned *));
12 local int gz_avail OF((gz_statep));
13 local int gz_next4 OF((gz_statep, unsigned long *));
14 local int gz_head OF((gz_statep));
15 local int gz_decomp OF((gz_statep));
16 local int gz_make OF((gz_statep));
17 local int gz_skip OF((gz_statep, z_off_t));
19 /* Use read() to load a buffer -- return -1 on error, otherwise 0. Read from
20 state->fd, and update state->eof, state->err, and state->msg as appropriate.
21 This function needs to loop on read(), since read() is not guaranteed to
22 read the number of bytes requested, depending on the type of descriptor. */
23 local int gz_load(state, buf, len, have)
33 ret = read(state->fd, buf + *have, len - *have);
37 } while (*have < len);
39 gz_error(state, Z_ERRNO, zstrerror());
47 /* Load up input buffer and set eof flag if last data loaded -- return -1 on
48 error, 0 otherwise. Note that the eof flag is set when the end of the input
49 file is reached, even though there may be unused data in the buffer. Once
50 that data has been used, no more attempts will be made to read the file.
51 gz_avail() assumes that strm->avail_in == 0. */
52 local int gz_avail(state)
55 z_streamp strm = &(state->strm);
57 if (state->err != Z_OK)
59 if (state->eof == 0) {
60 if (gz_load(state, state->in, state->size, &(strm->avail_in)) == -1)
62 strm->next_in = state->in;
67 /* Get next byte from input, or -1 if end or error. */
68 #define NEXT() ((strm->avail_in == 0 && gz_avail(state) == -1) ? -1 : \
69 (strm->avail_in == 0 ? -1 : \
70 (strm->avail_in--, *(strm->next_in)++)))
72 /* Get a four-byte little-endian integer and return 0 on success and the value
73 in *ret. Otherwise -1 is returned and *ret is not modified. */
74 local int gz_next4(state, ret)
80 z_streamp strm = &(state->strm);
83 val += (unsigned)NEXT() << 8;
84 val += (unsigned long)NEXT() << 16;
88 val += (unsigned long)ch << 24;
93 /* Look for gzip header, set up for inflate or copy. state->have must be zero.
94 If this is the first time in, allocate required memory. state->how will be
95 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, and the gzip header will be skipped so
98 that the next available input data is the raw deflate stream. If direct
99 copying, then leftover input data from the input buffer will be copied to
100 the output buffer. In that case, all further file reads will be directly to
101 either the output buffer or a user buffer. If decompressing, the inflate
102 state and the check value will be initialized. gz_head() will return 0 on
103 success or -1 on failure. Failures may include read errors or gzip header
105 local int gz_head(state)
108 z_streamp strm = &(state->strm);
112 /* allocate read buffers and inflate memory */
113 if (state->size == 0) {
114 /* allocate buffers */
115 state->in = malloc(state->want);
116 state->out = malloc(state->want << 1);
117 if (state->in == NULL || state->out == NULL) {
118 if (state->out != NULL)
120 if (state->in != NULL)
122 gz_error(state, Z_MEM_ERROR, "out of memory");
125 state->size = state->want;
127 /* allocate inflate memory */
128 state->strm.zalloc = Z_NULL;
129 state->strm.zfree = Z_NULL;
130 state->strm.opaque = Z_NULL;
131 state->strm.avail_in = 0;
132 state->strm.next_in = Z_NULL;
133 if (inflateInit2(&(state->strm), -15) != Z_OK) { /* raw inflate */
137 gz_error(state, Z_MEM_ERROR, "out of memory");
142 /* get some data in the input buffer */
143 if (strm->avail_in == 0) {
144 if (gz_avail(state) == -1)
146 if (strm->avail_in == 0)
150 /* look for the gzip magic header bytes 31 and 139 */
151 if (strm->next_in[0] == 31) {
154 if (strm->avail_in == 0 && gz_avail(state) == -1)
156 if (strm->avail_in && strm->next_in[0] == 139) {
157 /* we have a gzip header, woo hoo! */
161 /* skip rest of header */
162 if (NEXT() != 8) { /* compression method */
163 gz_error(state, Z_DATA_ERROR, "unknown compression method");
167 if (flags & 0xe0) { /* reserved flag bits */
168 gz_error(state, Z_DATA_ERROR, "unknown header flags set");
171 NEXT(); /* modification time */
175 NEXT(); /* extra flags */
176 NEXT(); /* operating system */
177 if (flags & 4) { /* extra field */
178 len = (unsigned)NEXT();
179 len += (unsigned)NEXT() << 8;
184 if (flags & 8) /* file name */
187 if (flags & 16) /* comment */
190 if (flags & 2) { /* header crc */
194 /* an unexpected end of file is not checked for here -- it will be
195 noticed on the first request for uncompressed data */
197 /* set up for decompression */
199 strm->adler = crc32(0L, Z_NULL, 0);
205 /* not a gzip file -- save first byte (31) and fall to raw i/o */
211 /* doing raw i/o, save start of raw data for seeking, copy any leftover
212 input to output -- this assumes that the output buffer is larger than
213 the input buffer, which also assures space for gzungetc() */
214 state->raw = state->pos;
215 state->next = state->out;
216 if (strm->avail_in) {
217 memcpy(state->next + state->have, strm->next_in, strm->avail_in);
218 state->have += strm->avail_in;
226 /* Decompress from input to the provided next_out and avail_out in the state.
227 If the end of the compressed data is reached, then verify the gzip trailer
228 check value and length (modulo 2^32). state->have and state->next are set
229 to point to the just decompressed data, and the crc is updated. If the
230 trailer is verified, state->how is reset to LOOK to look for the next gzip
231 stream or raw data, once state->have is depleted. Returns 0 on success, -1
232 on failure. Failures may include invalid compressed data or a failed gzip
233 trailer verification. */
234 local int gz_decomp(state)
239 unsigned long crc, len;
240 z_streamp strm = &(state->strm);
242 /* fill output buffer up to end of deflate stream */
243 had = strm->avail_out;
245 /* get more input for inflate() */
246 if (strm->avail_in == 0 && gz_avail(state) == -1)
248 if (strm->avail_in == 0) {
249 gz_error(state, Z_DATA_ERROR, "unexpected end of file");
253 /* decompress and handle errors */
254 ret = inflate(strm, Z_NO_FLUSH);
255 if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
256 gz_error(state, Z_STREAM_ERROR,
257 "internal error: inflate stream corrupt");
260 if (ret == Z_MEM_ERROR) {
261 gz_error(state, Z_MEM_ERROR, "out of memory");
264 if (ret == Z_DATA_ERROR) { /* deflate stream invalid */
265 gz_error(state, Z_DATA_ERROR,
266 strm->msg == NULL ? "compressed data error" : strm->msg);
269 } while (strm->avail_out && ret != Z_STREAM_END);
271 /* update available output and crc check value */
272 state->have = had - strm->avail_out;
273 state->next = strm->next_out - state->have;
274 strm->adler = crc32(strm->adler, state->next, state->have);
276 /* check gzip trailer if at end of deflate stream */
277 if (ret == Z_STREAM_END) {
278 if (gz_next4(state, &crc) == -1 || gz_next4(state, &len) == -1) {
279 gz_error(state, Z_DATA_ERROR, "unexpected end of file");
282 if (crc != strm->adler) {
283 gz_error(state, Z_DATA_ERROR, "incorrect data check");
286 if (len != (strm->total_out & 0xffffffffL)) {
287 gz_error(state, Z_DATA_ERROR, "incorrect length check");
290 state->how = LOOK; /* ready for next stream, once have is 0 (leave
291 state->direct unchanged to remember how) */
294 /* good decompression */
298 /* Make data and put in the output buffer. Assumes that state->have == 0.
299 Data is either copied from the input file or decompressed from the input
300 file depending on state->how. If state->how is LOOK, then a gzip header is
301 looked for (and skipped if found) to determine wither to copy or decompress.
302 Returns -1 on error, otherwise 0. gz_make() will leave state->have as COPY
303 or GZIP unless the end of the input file has been reached and all data has
305 local int gz_make(state)
308 z_streamp strm = &(state->strm);
310 if (state->how == LOOK) { /* look for gzip header */
311 if (gz_head(state) == -1)
313 if (state->have) /* got some data from gz_head() */
316 if (state->how == COPY) { /* straight copy */
317 if (gz_load(state, state->out, state->size << 1, &(state->have)) == -1)
319 state->next = state->out;
321 else if (state->how == GZIP) { /* decompress */
322 strm->avail_out = state->size << 1;
323 strm->next_out = state->out;
324 if (gz_decomp(state) == -1)
330 /* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */
331 local int gz_skip(state, len)
337 /* skip over len bytes or reach end-of-file, whichever comes first */
339 /* skip over whatever is in output buffer */
341 n = state->have > len ? (unsigned)len : state->have;
348 /* output buffer empty -- return if we're at the end of the input */
349 else if (state->eof && state->strm.avail_in == 0)
352 /* need more data to skip -- load up output buffer */
354 /* get more output, looking for header if required */
355 if (gz_make(state) == -1)
361 /* -- see zlib.h -- */
362 int ZEXPORT gzread(file, buf, len)
371 /* get internal structure */
374 state = (gz_statep)file;
375 strm = &(state->strm);
377 /* check that we're reading and that there's no error */
378 if (state->mode != GZ_READ || state->err != Z_OK)
381 /* since an int is returned, make sure len fits in one, otherwise return
382 with an error (this avoids the flaw in the interface) */
384 gz_error(state, Z_BUF_ERROR, "requested length does not fit in int");
388 /* if len is zero, avoid unnecessary operations */
392 /* process a skip request */
395 if (gz_skip(state, state->skip) == -1)
399 /* get len bytes to buf, or less than len if at the end */
402 /* first just try copying data from the output buffer */
404 n = state->have > len ? len : state->have;
405 memcpy(buf, state->next, n);
410 /* output buffer empty -- return if we're at the end of the input */
411 else if (state->eof && strm->avail_in == 0)
414 /* need output data -- for small len or new stream load up our output
416 else if (state->how == LOOK || len < (state->size << 1)) {
417 /* get more output, looking for header if required */
418 if (gz_make(state) == -1)
420 continue; /* no progress yet -- go back to memcpy() above */
421 /* the copy above assures that we will leave with space in the
422 output buffer, allowing at least one gzungetc() to succeed */
425 /* large len -- read directly into user buffer */
426 else if (state->how == COPY) { /* read directly */
427 if (gz_load(state, buf, len, &n) == -1)
431 /* large len -- decompress directly into user buffer */
432 else { /* state->how == GZIP */
433 strm->avail_out = len;
434 strm->next_out = buf;
435 if (gz_decomp(state) == -1)
441 /* update progress */
443 buf = (char *)buf + n;
448 /* return number of bytes read into user buffer (will fit in int) */
452 /* -- see zlib.h -- */
453 int ZEXPORT gzgetc(file)
457 unsigned char buf[1];
460 /* get internal structure */
463 state = (gz_statep)file;
465 /* check that we're reading and that there's no error */
466 if (state->mode != GZ_READ || state->err != Z_OK)
469 /* try output buffer (no need to check for skip request) */
473 return *(state->next)++;
476 /* nothing there -- try gzread() */
477 ret = gzread(file, buf, 1);
478 return ret < 1 ? -1 : buf[0];
481 /* -- see zlib.h -- */
482 int ZEXPORT gzungetc(c, file)
488 /* get internal structure */
491 state = (gz_statep)file;
493 /* check that we're reading and that there's no error */
494 if (state->mode != GZ_READ || state->err != Z_OK)
497 /* process a skip request */
500 if (gz_skip(state, state->skip) == -1)
508 /* if output buffer empty, put byte at end (allows more pushing) */
509 if (state->have == 0) {
511 state->next = state->out + (state->size << 1) - 1;
517 /* if no room, give up (must have already done a gzungetc()) */
518 if (state->have == (state->size << 1)) {
519 gz_error(state, Z_BUF_ERROR, "out of room to push characters");
523 /* slide output data if needed and insert byte before existing data */
524 if (state->next == state->out) {
525 unsigned char *src = state->out + state->have;
526 unsigned char *dest = state->out + (state->size << 1);
527 while (src > state->out)
538 /* -- see zlib.h -- */
539 char * ZEXPORT gzgets(file, buf, len)
549 /* get internal structure */
552 state = (gz_statep)file;
554 /* check that we're reading and that there's no error */
555 if (state->mode != GZ_READ || state->err != Z_OK)
558 /* process a skip request */
561 if (gz_skip(state, state->skip) == -1)
565 /* check for a dumb length */
569 /* copy output bytes up to new line or len - 1, whichever comes first --
570 append a terminating zero to the string (we don't check for a zero in
571 the contents, let the user worry about that) */
573 left = (unsigned)len - 1;
575 /* assure that something is in the output buffer */
576 if (state->have == 0) {
577 if (gz_make(state) == -1)
578 return NULL; /* error */
579 if (state->have == 0) { /* end of file */
580 if (buf == str) /* got bupkus */
582 break; /* got something -- return it */
586 /* look for end-of-line in current output buffer */
587 n = state->have > left ? left : state->have;
588 eol = memchr(state->next, '\n', n);
590 n = (eol - state->next) + 1;
592 /* copy through end-of-line, or remainder if not found */
593 memcpy(buf, state->next, n);
599 } while (left && eol == NULL);
601 /* found end-of-line or out of space -- terminate string and return it */
606 /* -- see zlib.h -- */
607 int ZEXPORT gzdirect(file)
612 /* get internal structure */
615 state = (gz_statep)file;
617 /* check that we're reading */
618 if (state->mode != GZ_READ)
621 /* if the state is not known, but we can find out, then do so (this is
622 mainly for right after a gzopen() or gzdopen()) */
623 if (state->how == LOOK && state->have == 0)
624 (void)gz_head(state);
626 /* return 1 if reading direct, 0 if decompressing a gzip stream */
627 return state->direct;
630 /* -- see zlib.h -- */
631 int ZEXPORT gzclose_r(file)
637 /* get internal structure */
639 return Z_STREAM_ERROR;
640 state = (gz_statep)file;
642 /* check that we're reading */
643 if (state->mode != GZ_READ)
644 return Z_STREAM_ERROR;
646 /* free memory and close file */
648 inflateEnd(&(state->strm));
652 gz_error(state, Z_OK, NULL);
653 ret = close(state->fd);
655 return ret ? Z_ERRNO : Z_OK;
658 #endif /* !OLD_GZIO */