1 /* gzio.c -- IO on .gz files
2 * Copyright (C) 1995-2002 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
5 * Compile this file with -DNO_DEFLATE to avoid the compression code.
15 #pragma warning (disable:4996)
18 struct internal_state {int dummy;}; /* for buggy compilers */
22 # define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
24 # define Z_BUFSIZE 16384
27 #ifndef Z_PRINTF_BUFSIZE
28 # define Z_PRINTF_BUFSIZE 4096
31 #define ALLOC(size) malloc(size)
32 #define TRYFREE(p) {if (p) free(p);}
34 static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
37 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
38 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
39 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
40 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
41 #define COMMENT 0x10 /* bit 4 set: file comment present */
42 #define RESERVED 0xE0 /* bits 5..7: reserved */
44 typedef struct gz_stream {
46 int z_err; /* error code for last stream operation */
47 int z_eof; /* set if end of input file */
48 FILE *file; /* .gz file */
49 Byte *inbuf; /* input buffer */
50 Byte *outbuf; /* output buffer */
51 uLong crc; /* crc32 of uncompressed data */
52 char *msg; /* error message */
53 char *path; /* path name for debugging only */
54 int transparent; /* 1 if input file is not a .gz file */
55 char mode; /* 'w' or 'r' */
56 long startpos; /* start of compressed data in file (header skipped) */
60 local gzFile gz_open OF((const char *path, const char *mode, int fd));
61 local int do_flush OF((gzFile file, int flush));
62 local int get_byte OF((gz_stream *s));
63 local void check_header OF((gz_stream *s));
64 local int destroy OF((gz_stream *s));
65 local void putLong OF((FILE *file, uLong x));
66 local uLong getLong OF((gz_stream *s));
68 /* ===========================================================================
69 Opens a gzip (.gz) file for reading or writing. The mode parameter
70 is as in fopen ("rb" or "wb"). The file is given either by file descriptor
71 or path name (if fd == -1).
72 gz_open return NULL if the file could not be opened or if there was
73 insufficient memory to allocate the (de)compression state; errno
74 can be checked to distinguish the two cases (if errno is zero, the
75 zlib error is Z_MEM_ERROR).
77 local gzFile gz_open (path, mode, fd)
83 int level = Z_DEFAULT_COMPRESSION; /* compression level */
84 int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
85 char *p = (char*)mode;
87 char fmode[80]; /* copy of mode, without the compression level */
90 if (!path || !mode) return Z_NULL;
92 s = (gz_stream *)ALLOC(sizeof(gz_stream));
93 if (!s) return Z_NULL;
95 s->stream.zalloc = (alloc_func)0;
96 s->stream.zfree = (free_func)0;
97 s->stream.opaque = (voidpf)0;
98 s->stream.next_in = s->inbuf = Z_NULL;
99 s->stream.next_out = s->outbuf = Z_NULL;
100 s->stream.avail_in = s->stream.avail_out = 0;
104 s->crc = crc32(0L, Z_NULL, 0);
108 s->path = (char*)ALLOC(strlen(path)+1);
109 if (s->path == NULL) {
110 return destroy(s), (gzFile)Z_NULL;
112 strcpy(s->path, path); /* do this early for debugging */
116 if (*p == 'r') s->mode = 'r';
117 if (*p == 'w' || *p == 'a') s->mode = 'w';
118 if (*p >= '0' && *p <= '9') {
120 } else if (*p == 'f') {
121 strategy = Z_FILTERED;
122 } else if (*p == 'h') {
123 strategy = Z_HUFFMAN_ONLY;
125 *m++ = *p; /* copy the mode */
127 } while (*p++ && m != fmode + sizeof(fmode));
128 if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
130 if (s->mode == 'w') {
132 err = Z_STREAM_ERROR;
134 err = deflateInit2(&(s->stream), level,
135 Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
136 /* windowBits is passed < 0 to suppress zlib header */
138 s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
140 if (err != Z_OK || s->outbuf == Z_NULL) {
141 return destroy(s), (gzFile)Z_NULL;
144 s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
146 err = inflateInit2(&(s->stream), -MAX_WBITS);
147 /* windowBits is passed < 0 to tell that there is no zlib header.
148 * Note that in this case inflate *requires* an extra "dummy" byte
149 * after the compressed stream in order to complete decompression and
150 * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
151 * present after the compressed stream.
153 if (err != Z_OK || s->inbuf == Z_NULL) {
154 return destroy(s), (gzFile)Z_NULL;
157 s->stream.avail_out = Z_BUFSIZE;
160 s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
162 if (s->file == NULL) {
163 return destroy(s), (gzFile)Z_NULL;
165 if (s->mode == 'w') {
166 /* Write a very simple .gz header:
168 fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
169 Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
171 /* We use 10L instead of ftell(s->file) to because ftell causes an
172 * fflush on some systems. This version of the library doesn't use
173 * startpos anyway in write mode, so this initialization is not
177 check_header(s); /* skip the .gz header */
178 s->startpos = (ftell(s->file) - s->stream.avail_in);
184 /* ===========================================================================
185 Opens a gzip (.gz) file for reading or writing.
187 gzFile ZEXPORT gzopen (path, mode)
191 return gz_open (path, mode, -1);
194 /* ===========================================================================
195 Associate a gzFile with the file descriptor fd. fd is not dup'ed here
196 to mimic the behavio(u)r of fdopen.
198 gzFile ZEXPORT gzdopen (fd, mode)
204 if (fd < 0) return (gzFile)Z_NULL;
205 sprintf(name, "<fd:%d>", fd); /* for debugging */
207 return gz_open (name, mode, fd);
210 /* ===========================================================================
211 * Update the compression level and strategy
213 int ZEXPORT gzsetparams (file, level, strategy)
218 gz_stream *s = (gz_stream*)file;
220 if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
222 /* Make room to allow flushing */
223 if (s->stream.avail_out == 0) {
225 s->stream.next_out = s->outbuf;
226 if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
229 s->stream.avail_out = Z_BUFSIZE;
232 return deflateParams (&(s->stream), level, strategy);
235 /* ===========================================================================
236 Read a byte from a gz_stream; update next_in and avail_in. Return EOF
238 IN assertion: the stream s has been sucessfully opened for reading.
240 local int get_byte(s)
243 if (s->z_eof) return EOF;
244 if (s->stream.avail_in == 0) {
246 s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
247 if (s->stream.avail_in == 0) {
249 if (ferror(s->file)) s->z_err = Z_ERRNO;
252 s->stream.next_in = s->inbuf;
254 s->stream.avail_in--;
255 return *(s->stream.next_in)++;
258 /* ===========================================================================
259 Check the gzip header of a gz_stream opened for reading. Set the stream
260 mode to transparent if the gzip magic header is not present; set s->err
261 to Z_DATA_ERROR if the magic header is present but the rest of the header
263 IN assertion: the stream s has already been created sucessfully;
264 s->stream.avail_in is zero for the first time, but may be non-zero
265 for concatenated .gz files.
267 local void check_header(s)
270 int method; /* method byte */
271 int flags; /* flags byte */
275 /* Check the gzip magic header */
276 for (len = 0; len < 2; len++) {
278 if (c != gz_magic[len]) {
279 if (len != 0) s->stream.avail_in++, s->stream.next_in--;
281 s->stream.avail_in++, s->stream.next_in--;
284 s->z_err = s->stream.avail_in != 0 ? Z_OK : Z_STREAM_END;
288 method = get_byte(s);
290 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
291 s->z_err = Z_DATA_ERROR;
295 /* Discard time, xflags and OS code: */
296 for (len = 0; len < 6; len++) (void)get_byte(s);
298 if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
299 len = (uInt)get_byte(s);
300 len += ((uInt)get_byte(s))<<8;
301 /* len is garbage if EOF but the loop below will quit anyway */
302 while (len-- != 0 && get_byte(s) != EOF) ;
304 if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
305 while ((c = get_byte(s)) != 0 && c != EOF) ;
307 if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
308 while ((c = get_byte(s)) != 0 && c != EOF) ;
310 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
311 for (len = 0; len < 2; len++) (void)get_byte(s);
313 s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
316 /* ===========================================================================
317 * Cleanup then free the given gz_stream. Return a zlib error code.
318 Try freeing in the reverse order of allocations.
320 local int destroy (s)
325 if (!s) return Z_STREAM_ERROR;
329 if (s->stream.state != NULL) {
330 if (s->mode == 'w') {
332 err = Z_STREAM_ERROR;
334 err = deflateEnd(&(s->stream));
336 } else if (s->mode == 'r') {
337 err = inflateEnd(&(s->stream));
340 if (s->file != NULL && fclose(s->file)) {
342 if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
346 if (s->z_err < 0) err = s->z_err;
355 /* ===========================================================================
356 Reads the given number of uncompressed bytes from the compressed file.
357 gzread returns the number of bytes actually read (0 for end of file).
359 int ZEXPORT gzread (file, buf, len)
364 gz_stream *s = (gz_stream*)file;
365 Bytef *start = (Bytef*)buf; /* starting point for crc computation */
366 Byte *next_out; /* == stream.next_out but not forced far (for MSDOS) */
368 if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
370 if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
371 if (s->z_err == Z_STREAM_END) return 0; /* EOF */
373 next_out = (Byte*)buf;
374 s->stream.next_out = (Bytef*)buf;
375 s->stream.avail_out = len;
377 while (s->stream.avail_out != 0) {
379 if (s->transparent) {
380 /* Copy first the lookahead bytes: */
381 uInt n = s->stream.avail_in;
382 if (n > s->stream.avail_out) n = s->stream.avail_out;
384 zmemcpy(s->stream.next_out, s->stream.next_in, n);
386 s->stream.next_out = next_out;
387 s->stream.next_in += n;
388 s->stream.avail_out -= n;
389 s->stream.avail_in -= n;
391 if (s->stream.avail_out > 0) {
392 s->stream.avail_out -= fread(next_out, 1, s->stream.avail_out,
395 len -= s->stream.avail_out;
396 s->stream.total_in += (uLong)len;
397 s->stream.total_out += (uLong)len;
398 if (len == 0) s->z_eof = 1;
401 if (s->stream.avail_in == 0 && !s->z_eof) {
404 s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
405 if (s->stream.avail_in == 0) {
407 if (ferror(s->file)) {
412 s->stream.next_in = s->inbuf;
414 s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
416 if (s->z_err == Z_STREAM_END) {
417 /* Check CRC and original size */
418 s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
419 start = s->stream.next_out;
421 if (getLong(s) != s->crc) {
422 s->z_err = Z_DATA_ERROR;
425 /* The uncompressed length returned by above getlong() may
426 * be different from s->stream.total_out) in case of
427 * concatenated .gz files. Check for such files:
430 if (s->z_err == Z_OK) {
431 uLong total_in = s->stream.total_in;
432 uLong total_out = s->stream.total_out;
434 inflateReset(&(s->stream));
435 s->stream.total_in = total_in;
436 s->stream.total_out = total_out;
437 s->crc = crc32(0L, Z_NULL, 0);
441 if (s->z_err != Z_OK || s->z_eof) break;
443 s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
445 return (int)(len - s->stream.avail_out);
449 /* ===========================================================================
450 Reads one byte from the compressed file. gzgetc returns this byte
451 or -1 in case of end of file or error.
453 int ZEXPORT gzgetc(file)
458 return gzread(file, &c, 1) == 1 ? c : -1;
462 /* ===========================================================================
463 Reads bytes from the compressed file until len-1 characters are
464 read, or a newline character is read and transferred to buf, or an
465 end-of-file condition is encountered. The string is then terminated
466 with a null character.
467 gzgets returns buf, or Z_NULL in case of error.
469 The current implementation is not optimized at all.
471 char * ZEXPORT gzgets(file, buf, len)
477 if (buf == Z_NULL || len <= 0) return Z_NULL;
479 while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ;
481 return b == buf && len > 0 ? Z_NULL : b;
486 /* ===========================================================================
487 Writes the given number of uncompressed bytes into the compressed file.
488 gzwrite returns the number of bytes actually written (0 in case of error).
490 int ZEXPORT gzwrite (file, buf, len)
495 gz_stream *s = (gz_stream*)file;
497 if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
499 s->stream.next_in = (Bytef*)buf;
500 s->stream.avail_in = len;
502 while (s->stream.avail_in != 0) {
504 if (s->stream.avail_out == 0) {
506 s->stream.next_out = s->outbuf;
507 if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
511 s->stream.avail_out = Z_BUFSIZE;
513 s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
514 if (s->z_err != Z_OK) break;
516 s->crc = crc32(s->crc, (const Bytef *)buf, len);
518 return (int)(len - s->stream.avail_in);
521 /* ===========================================================================
522 Converts, formats, and writes the args to the compressed file under
523 control of the format string, as in fprintf. gzprintf returns the number of
524 uncompressed bytes actually written (0 in case of error).
529 int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...)
531 char buf[Z_PRINTF_BUFSIZE];
535 va_start(va, format);
537 (void)vsnprintf(buf, sizeof(buf), format, va);
539 (void)vsprintf(buf, format, va);
542 len = strlen(buf); /* some *sprintf don't return the nb of bytes written */
543 if (len <= 0) return 0;
545 return gzwrite(file, buf, (unsigned)len);
547 #else /* not ANSI C */
549 int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
550 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
553 int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
554 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
556 char buf[Z_PRINTF_BUFSIZE];
560 snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
561 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
563 sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
564 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
566 len = strlen(buf); /* old sprintf doesn't return the nb of bytes written */
567 if (len <= 0) return 0;
569 return gzwrite(file, buf, len);
573 /* ===========================================================================
574 Writes c, converted to an unsigned char, into the compressed file.
575 gzputc returns the value that was written, or -1 in case of error.
577 int ZEXPORT gzputc(file, c)
581 unsigned char cc = (unsigned char) c; /* required for big endian systems */
583 return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1;
587 /* ===========================================================================
588 Writes the given null-terminated string to the compressed file, excluding
589 the terminating null character.
590 gzputs returns the number of characters written, or -1 in case of error.
592 int ZEXPORT gzputs(file, s)
596 return gzwrite(file, (char*)s, (unsigned)strlen(s));
600 /* ===========================================================================
601 Flushes all pending output into the compressed file. The parameter
602 flush is as in the deflate() function.
604 local int do_flush (file, flush)
610 gz_stream *s = (gz_stream*)file;
612 if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
614 s->stream.avail_in = 0; /* should be zero already anyway */
617 len = Z_BUFSIZE - s->stream.avail_out;
620 if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
624 s->stream.next_out = s->outbuf;
625 s->stream.avail_out = Z_BUFSIZE;
628 s->z_err = deflate(&(s->stream), flush);
630 /* Ignore the second of two consecutive flushes: */
631 if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
633 /* deflate has finished flushing only when it hasn't used up
634 * all the available space in the output buffer:
636 done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
638 if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
640 return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
643 int ZEXPORT gzflush (file, flush)
647 gz_stream *s = (gz_stream*)file;
648 int err = do_flush (file, flush);
652 return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
654 #endif /* NO_DEFLATE */
656 /* ===========================================================================
657 Sets the starting position for the next gzread or gzwrite on the given
658 compressed file. The offset represents a number of bytes in the
659 gzseek returns the resulting offset location as measured in bytes from
660 the beginning of the uncompressed stream, or -1 in case of error.
661 SEEK_END is not implemented, returns error.
662 In this version of the library, gzseek can be extremely slow.
664 z_off_t ZEXPORT gzseek (file, offset, whence)
669 gz_stream *s = (gz_stream*)file;
671 if (s == NULL || whence == SEEK_END ||
672 s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
676 if (s->mode == 'w') {
680 if (whence == SEEK_SET) {
681 offset -= s->stream.total_in;
683 if (offset < 0) return -1L;
685 /* At this point, offset is the number of zero bytes to write. */
686 if (s->inbuf == Z_NULL) {
687 s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
688 zmemzero(s->inbuf, Z_BUFSIZE);
691 uInt size = Z_BUFSIZE;
692 if (offset < Z_BUFSIZE) size = (uInt)offset;
694 size = gzwrite(file, s->inbuf, size);
695 if (size == 0) return -1L;
699 return (z_off_t)s->stream.total_in;
702 /* Rest of function is for reading only */
704 /* compute absolute position */
705 if (whence == SEEK_CUR) {
706 offset += s->stream.total_out;
708 if (offset < 0) return -1L;
710 if (s->transparent) {
712 s->stream.avail_in = 0;
713 s->stream.next_in = s->inbuf;
714 if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
716 s->stream.total_in = s->stream.total_out = (uLong)offset;
720 /* For a negative seek, rewind and use positive seek */
721 if ((uLong)offset >= s->stream.total_out) {
722 offset -= s->stream.total_out;
723 } else if (gzrewind(file) < 0) {
726 /* offset is now the number of bytes to skip. */
728 if (offset != 0 && s->outbuf == Z_NULL) {
729 s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
732 int size = Z_BUFSIZE;
733 if (offset < Z_BUFSIZE) size = (int)offset;
735 size = gzread(file, s->outbuf, (uInt)size);
736 if (size <= 0) return -1L;
739 return (z_off_t)s->stream.total_out;
742 /* ===========================================================================
745 int ZEXPORT gzrewind (file)
748 gz_stream *s = (gz_stream*)file;
750 if (s == NULL || s->mode != 'r') return -1;
754 s->stream.avail_in = 0;
755 s->stream.next_in = s->inbuf;
756 s->crc = crc32(0L, Z_NULL, 0);
758 if (s->startpos == 0) { /* not a compressed file */
763 (void) inflateReset(&s->stream);
764 return fseek(s->file, s->startpos, SEEK_SET);
767 /* ===========================================================================
768 Returns the starting position for the next gzread or gzwrite on the
769 given compressed file. This position represents a number of bytes in the
770 uncompressed data stream.
772 z_off_t ZEXPORT gztell (file)
775 return gzseek(file, 0L, SEEK_CUR);
778 /* ===========================================================================
779 Returns 1 when EOF has previously been detected reading the given
780 input stream, otherwise zero.
782 int ZEXPORT gzeof (file)
785 gz_stream *s = (gz_stream*)file;
787 return (s == NULL || s->mode != 'r') ? 0 : s->z_eof;
790 /* ===========================================================================
791 Outputs a long in LSB order to the given file
793 local void putLong (file, x)
798 for (n = 0; n < 4; n++) {
799 fputc((int)(x & 0xff), file);
804 /* ===========================================================================
805 Reads a long in LSB order from the given gz_stream. Sets z_err in case
808 local uLong getLong (s)
811 uLong x = (uLong)get_byte(s);
814 x += ((uLong)get_byte(s))<<8;
815 x += ((uLong)get_byte(s))<<16;
817 if (c == EOF) s->z_err = Z_DATA_ERROR;
822 /* ===========================================================================
823 Flushes all pending output if necessary, closes the compressed file
824 and deallocates all the (de)compression state.
826 int ZEXPORT gzclose (file)
830 gz_stream *s = (gz_stream*)file;
832 if (s == NULL) return Z_STREAM_ERROR;
834 if (s->mode == 'w') {
836 return Z_STREAM_ERROR;
838 err = do_flush (file, Z_FINISH);
839 if (err != Z_OK) return destroy((gz_stream*)file);
841 putLong (s->file, s->crc);
842 putLong (s->file, s->stream.total_in);
845 return destroy((gz_stream*)file);
848 /* ===========================================================================
849 Returns the error message for the last error which occured on the
850 given compressed file. errnum is set to zlib error number. If an
851 error occured in the file system and not in the compression library,
852 errnum is set to Z_ERRNO and the application may consult errno
853 to get the exact error code.
855 const char* ZEXPORT gzerror (file, errnum)
860 gz_stream *s = (gz_stream*)file;
863 *errnum = Z_STREAM_ERROR;
864 return (const char*)ERR_MSG(Z_STREAM_ERROR);
867 if (*errnum == Z_OK) return (const char*)"";
869 m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
871 if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
874 s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
875 strcpy(s->msg, s->path);
876 strcat(s->msg, ": ");
878 return (const char*)s->msg;