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
73 value 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 1 if
96 there is no gzip header and direct copying will be performned, or it will be
97 set to 2 for decompression, and the gzip header will be skipped so that the
98 next available input data is the raw deflate stream. If direct copying,
99 then leftover input data from the input buffer will be copied to the output
100 buffer. In that case, all further file reads will be directly to either the
101 output buffer or a user buffer. If decompressing, the inflate state and the
102 check value will be initialized. gz_head() will return 0 on success or -1
103 on failure. Failures may include read errors or gzip header errors. */
104 local int gz_head(state)
107 z_streamp strm = &(state->strm);
111 /* allocate read buffers and inflate memory */
112 if (state->size == 0) {
113 /* allocate buffers */
114 state->in = malloc(state->want);
115 state->out = malloc(state->want << 1);
116 if (state->in == NULL || state->out == NULL) {
117 if (state->out != NULL)
119 if (state->in != NULL)
121 gz_error(state, Z_MEM_ERROR, "out of memory");
124 state->size = state->want;
126 /* allocate inflate memory */
127 state->strm.zalloc = Z_NULL;
128 state->strm.zfree = Z_NULL;
129 state->strm.opaque = Z_NULL;
130 state->strm.avail_in = 0;
131 state->strm.next_in = Z_NULL;
132 if (inflateInit2(&(state->strm), -15) != Z_OK) { /* raw inflate */
136 gz_error(state, Z_MEM_ERROR, "out of memory");
141 /* get some data in the input buffer */
142 if (strm->avail_in == 0) {
143 if (gz_avail(state) == -1)
145 if (strm->avail_in == 0)
149 /* look for the gzip magic header bytes 31 and 139 */
150 if (strm->next_in[0] == 31) {
153 if (strm->avail_in == 0 && gz_avail(state) == -1)
155 if (strm->avail_in && strm->next_in[0] == 139) {
156 /* we have a gzip header, woo hoo! */
160 /* skip rest of header */
161 if (NEXT() != 8) { /* compression method */
162 gz_error(state, Z_DATA_ERROR, "unknown compression method");
166 if (flags & 0xe0) { /* reserved flag bits */
167 gz_error(state, Z_DATA_ERROR, "unknown header flags set");
170 NEXT(); /* modification time */
174 NEXT(); /* extra flags */
175 NEXT(); /* operating system */
176 if (flags & 4) { /* extra field */
177 len = (unsigned)NEXT();
178 len += (unsigned)NEXT() << 8;
183 if (flags & 8) /* file name */
186 if (flags & 16) /* comment */
189 if (flags & 2) { /* header crc */
194 /* set up for decompression */
196 strm->adler = crc32(0L, Z_NULL, 0);
201 /* not a gzip file -- save first byte (31) and fall to raw i/o */
207 /* doing raw i/o, save start of raw data for seeking, copy any leftover
208 input to output -- this assumes that the output buffer is larger than
210 state->raw = state->pos;
211 state->next = state->out;
212 if (strm->avail_in) {
213 memcpy(state->next + state->have, strm->next_in, strm->avail_in);
214 state->have += strm->avail_in;
221 /* Decompress from input to the provided next_out and avail_out in the state.
222 If the end of the compressed data is reached, then verify the gzip trailer
223 check value and length (modulo 2^32). state->have and state->next are
224 set to point to the just decompressed data, and the crc is updated. If the
225 trailer is verified, state->how is reset to zero to look for the next gzip
226 stream or raw data, once state->have is depleted. Returns 0 on success, -1
227 on failure. Failures may include invalid compressed data or a failed gzip
228 trailer verification. */
229 local int gz_decomp(state)
234 unsigned long crc, len;
235 z_streamp strm = &(state->strm);
237 /* fill output buffer up to end of deflate stream */
238 had = strm->avail_out;
240 /* get more input for inflate() */
241 if (strm->avail_in == 0 && gz_avail(state) == -1)
243 if (strm->avail_in == 0) {
244 gz_error(state, Z_DATA_ERROR, "unexpected end of file");
248 /* decompress and handle errors */
249 ret = inflate(strm, Z_NO_FLUSH);
250 if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
251 gz_error(state, Z_STREAM_ERROR,
252 "internal error: inflate stream corrupt");
255 if (ret == Z_MEM_ERROR) {
256 gz_error(state, Z_MEM_ERROR, "out of memory");
259 if (ret == Z_DATA_ERROR) { /* deflate stream invalid */
260 gz_error(state, Z_DATA_ERROR,
261 strm->msg == NULL ? "compressed data error" : strm->msg);
264 } while (strm->avail_out && ret != Z_STREAM_END);
266 /* update available output and crc check value */
267 state->have = had - strm->avail_out;
268 state->next = strm->next_out - state->have;
269 strm->adler = crc32(strm->adler, state->next, state->have);
271 /* check gzip trailer if at end of deflate stream */
272 if (ret == Z_STREAM_END) {
273 if (gz_next4(state, &crc) == -1 || gz_next4(state, &len) == -1) {
274 gz_error(state, Z_DATA_ERROR, "unexpected end of file");
277 if (crc != strm->adler) {
278 gz_error(state, Z_DATA_ERROR, "incorrect data check");
281 if (len != (strm->total_out & 0xffffffffL)) {
282 gz_error(state, Z_DATA_ERROR, "incorrect length check");
285 state->how = 0; /* ready for next stream, once have is 0 */
288 /* good decompression */
292 /* Make data and put in the output buffer. Assumes that state->have == 0.
293 Data is either copied from the input file or decompressed from the input
294 file depending on state->how. If state->how is zero, then a gzip header is
295 looked for (and skipped if found) to determine wither to copy or decompress.
296 Returns -1 on error, otherwise 0. gz_make() will leave state->have non-zero
297 unless the end of the input file has been reached and all data has been
299 local int gz_make(state)
302 z_streamp strm = &(state->strm);
304 if (state->how == 0) { /* look for gzip header */
305 if (gz_head(state) == -1)
307 if (state->have) /* got some data from gz_head() */
310 if (state->how == 1) { /* straight copy */
311 if (gz_load(state, state->out, state->size << 1, &(state->have)) == -1)
313 state->next = state->out;
315 else if (state->how == 2) { /* decompress */
316 strm->avail_out = state->size << 1;
317 strm->next_out = state->out;
318 if (gz_decomp(state) == -1)
324 /* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */
325 local int gz_skip(state, len)
331 /* skip over len bytes or reach end-of-file, whichever comes first */
333 /* skip over whatever is in output buffer */
335 n = state->have > len ? (unsigned)len : state->have;
342 /* output buffer empty -- return if we're at the end of the input */
343 else if (state->eof && state->strm.avail_in == 0)
346 /* need more data to skip -- load up output buffer */
348 /* get more output, looking for header if required */
349 if (gz_make(state) == -1)
355 /* -- see zlib.h -- */
356 int ZEXPORT gzread(file, buf, len)
365 /* get internal structure */
368 state = (gz_statep)file;
369 strm = &(state->strm);
371 /* check that we're reading and that there's no error */
372 if (state->mode != GZ_READ || state->err != Z_OK)
375 /* process a skip request */
378 if (gz_skip(state, state->skip) == -1)
382 /* get len bytes to buf, or less than len if at the end */
386 /* first just try copying data from the output buffer */
388 n = state->have > len ? len : state->have;
389 memcpy(buf, state->next, n);
394 /* output buffer empty -- return if we're at the end of the input */
395 else if (state->eof && strm->avail_in == 0)
398 /* need output data -- for small len or new stream load up our output
400 else if (state->how == 0 || len < (state->size << 1)) {
401 /* get more output, looking for header if required */
402 if (gz_make(state) == -1)
404 continue; /* no progress yet -- go back to memcpy() above */
407 /* large len -- read directly into user buffer */
408 else if (state->how == 1) { /* read directly */
409 if (gz_load(state, buf, len, &n) == -1)
413 /* large len -- decompress directly into user buffer */
414 else { /* state->how == 2 */
415 strm->avail_out = len;
416 strm->next_out = buf;
417 if (gz_decomp(state) == -1)
423 /* update progress */
430 /* return number of bytes read into user buffer */
431 return (int)got; /* len had better fit in int -- interface flaw */
434 /* -- see zlib.h -- */
435 int ZEXPORT gzgetc(file)
439 unsigned char buf[1];
442 /* get internal structure */
445 state = (gz_statep)file;
447 /* check that we're reading and that there's no error */
448 if (state->mode != GZ_READ || state->err != Z_OK)
451 /* try output buffer */
455 return *(state->next)++;
458 /* nothing there -- try gzread() */
459 ret = gzread(file, buf, 1);
460 return ret < 1 ? -1 : buf[0];
463 /* -- see zlib.h -- */
464 int ZEXPORT gzungetc(c, file)
470 /* get internal structure */
473 state = (gz_statep)file;
475 /* check that we're reading and that there's no error */
476 if (state->mode != GZ_READ || state->err != Z_OK)
479 /* process a skip request */
482 if (gz_skip(state, state->skip) == -1)
490 /* if output buffer empty, put byte at end (allows more pushing) */
491 if (state->have == 0) {
493 state->next = state->out + (state->size << 1) - 1;
499 /* if no room, give up (must have already done a gz_ungetc()) */
500 if (state->have == (state->size << 1))
503 /* slide output data if needed and insert byte before existing data */
504 if (state->next == state->out) {
505 unsigned char *src = state->out + state->have;
506 unsigned char *dest = state->out + (state->size << 1);
507 while (src > state->out)
518 /* -- see zlib.h -- */
519 char * ZEXPORT gzgets(file, buf, len)
529 /* get internal structure */
532 state = (gz_statep)file;
534 /* check that we're reading and that there's no error */
535 if (state->mode != GZ_READ || state->err != Z_OK)
538 /* process a skip request */
541 if (gz_skip(state, state->skip) == -1)
545 /* check for a dumb length */
549 /* copy output bytes up to new line or len - 1, whichever comes first --
550 append a terminating zero to the string (we don't check for a zero in
551 the contents, let the user worry about that) */
553 left = (unsigned)len - 1;
555 /* assure that something is in the output buffer */
556 if (state->have == 0) {
557 if (gz_make(state) == -1)
558 return NULL; /* error */
559 if (state->have == 0) { /* end of file */
560 if (buf == str) /* got bupkus */
562 break; /* got something -- return it */
566 /* look for end-of-line in current output buffer */
567 n = state->have > left ? left : state->have;
568 eol = memchr(state->next, '\n', n);
570 n = (eol - state->next) + 1;
572 /* copy through end-of-line, or remainder if not found */
573 memcpy(buf, state->next, n);
579 } while (left && eol == NULL);
581 /* found end-of-line or out of space -- terminate string and return it */
586 /* -- see zlib.h -- */
587 int ZEXPORT gzdirect(file)
592 /* get internal structure */
595 state = (gz_statep)file;
597 /* check that we're reading */
598 if (state->mode != GZ_READ)
601 /* return true if reading without decompression */
602 return state->how == 1;
605 /* -- see zlib.h -- */
606 int ZEXPORT gzclose_r(file)
611 /* get internal structure */
613 return Z_STREAM_ERROR;
614 state = (gz_statep)file;
616 /* check that we're reading */
617 if (state->mode != GZ_READ)
618 return Z_STREAM_ERROR;
620 /* free memory and close file */
622 inflateEnd(&(state->strm));
626 gz_error(state, Z_OK, NULL);
632 #endif /* !OLD_GZIO */