nuclear@0: /* gzio.c -- IO on .gz files nuclear@0: * Copyright (C) 1995-2005 Jean-loup Gailly. nuclear@0: * For conditions of distribution and use, see copyright notice in zlib.h nuclear@0: * nuclear@0: * Compile this file with -DNO_GZCOMPRESS to avoid the compression code. nuclear@0: */ nuclear@0: nuclear@0: /* @(#) $Id$ */ nuclear@0: nuclear@0: #include nuclear@0: nuclear@0: #include "zutil.h" nuclear@0: nuclear@0: #ifdef NO_DEFLATE /* for compatibility with old definition */ nuclear@0: # define NO_GZCOMPRESS nuclear@0: #endif nuclear@0: nuclear@0: #ifndef NO_DUMMY_DECL nuclear@0: struct internal_state {int dummy;}; /* for buggy compilers */ nuclear@0: #endif nuclear@0: nuclear@0: #ifndef Z_BUFSIZE nuclear@0: # ifdef MAXSEG_64K nuclear@0: # define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */ nuclear@0: # else nuclear@0: # define Z_BUFSIZE 16384 nuclear@0: # endif nuclear@0: #endif nuclear@0: #ifndef Z_PRINTF_BUFSIZE nuclear@0: # define Z_PRINTF_BUFSIZE 4096 nuclear@0: #endif nuclear@0: nuclear@0: #ifdef __MVS__ nuclear@0: # pragma map (fdopen , "\174\174FDOPEN") nuclear@0: FILE *fdopen(int, const char *); nuclear@0: #endif nuclear@0: nuclear@0: #ifndef STDC nuclear@0: extern voidp malloc OF((uInt size)); nuclear@0: extern void free OF((voidpf ptr)); nuclear@0: #endif nuclear@0: nuclear@0: #define ALLOC(size) malloc(size) nuclear@0: #define TRYFREE(p) {if (p) free(p);} nuclear@0: nuclear@0: static int const gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */ nuclear@0: nuclear@0: /* gzip flag byte */ nuclear@0: #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ nuclear@0: #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */ nuclear@0: #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ nuclear@0: #define ORIG_NAME 0x08 /* bit 3 set: original file name present */ nuclear@0: #define COMMENT 0x10 /* bit 4 set: file comment present */ nuclear@0: #define RESERVED 0xE0 /* bits 5..7: reserved */ nuclear@0: nuclear@0: typedef struct gz_stream { nuclear@0: z_stream stream; nuclear@0: int z_err; /* error code for last stream operation */ nuclear@0: int z_eof; /* set if end of input file */ nuclear@0: FILE *file; /* .gz file */ nuclear@0: Byte *inbuf; /* input buffer */ nuclear@0: Byte *outbuf; /* output buffer */ nuclear@0: uLong crc; /* crc32 of uncompressed data */ nuclear@0: char *msg; /* error message */ nuclear@0: char *path; /* path name for debugging only */ nuclear@0: int transparent; /* 1 if input file is not a .gz file */ nuclear@0: char mode; /* 'w' or 'r' */ nuclear@0: z_off_t start; /* start of compressed data in file (header skipped) */ nuclear@0: z_off_t in; /* bytes into deflate or inflate */ nuclear@0: z_off_t out; /* bytes out of deflate or inflate */ nuclear@0: int back; /* one character push-back */ nuclear@0: int last; /* true if push-back is last character */ nuclear@0: } gz_stream; nuclear@0: nuclear@0: nuclear@0: local gzFile gz_open OF((const char *path, const char *mode, int fd)); nuclear@0: local int do_flush OF((gzFile file, int flush)); nuclear@0: local int get_byte OF((gz_stream *s)); nuclear@0: local void check_header OF((gz_stream *s)); nuclear@0: local int destroy OF((gz_stream *s)); nuclear@0: local void putLong OF((FILE *file, uLong x)); nuclear@0: local uLong getLong OF((gz_stream *s)); nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Opens a gzip (.gz) file for reading or writing. The mode parameter nuclear@0: is as in fopen ("rb" or "wb"). The file is given either by file descriptor nuclear@0: or path name (if fd == -1). nuclear@0: gz_open returns NULL if the file could not be opened or if there was nuclear@0: insufficient memory to allocate the (de)compression state; errno nuclear@0: can be checked to distinguish the two cases (if errno is zero, the nuclear@0: zlib error is Z_MEM_ERROR). nuclear@0: */ nuclear@0: local gzFile gz_open (path, mode, fd) nuclear@0: const char *path; nuclear@0: const char *mode; nuclear@0: int fd; nuclear@0: { nuclear@0: int err; nuclear@0: int level = Z_DEFAULT_COMPRESSION; /* compression level */ nuclear@0: int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */ nuclear@0: char *p = (char*)mode; nuclear@0: gz_stream *s; nuclear@0: char fmode[80]; /* copy of mode, without the compression level */ nuclear@0: char *m = fmode; nuclear@0: nuclear@0: if (!path || !mode) return Z_NULL; nuclear@0: nuclear@0: s = (gz_stream *)ALLOC(sizeof(gz_stream)); nuclear@0: if (!s) return Z_NULL; nuclear@0: nuclear@0: s->stream.zalloc = (alloc_func)0; nuclear@0: s->stream.zfree = (free_func)0; nuclear@0: s->stream.opaque = (voidpf)0; nuclear@0: s->stream.next_in = s->inbuf = Z_NULL; nuclear@0: s->stream.next_out = s->outbuf = Z_NULL; nuclear@0: s->stream.avail_in = s->stream.avail_out = 0; nuclear@0: s->file = NULL; nuclear@0: s->z_err = Z_OK; nuclear@0: s->z_eof = 0; nuclear@0: s->in = 0; nuclear@0: s->out = 0; nuclear@0: s->back = EOF; nuclear@0: s->crc = crc32(0L, Z_NULL, 0); nuclear@0: s->msg = NULL; nuclear@0: s->transparent = 0; nuclear@0: nuclear@0: s->path = (char*)ALLOC(strlen(path)+1); nuclear@0: if (s->path == NULL) { nuclear@0: return destroy(s), (gzFile)Z_NULL; nuclear@0: } nuclear@0: strcpy(s->path, path); /* do this early for debugging */ nuclear@0: nuclear@0: s->mode = '\0'; nuclear@0: do { nuclear@0: if (*p == 'r') s->mode = 'r'; nuclear@0: if (*p == 'w' || *p == 'a') s->mode = 'w'; nuclear@0: if (*p >= '0' && *p <= '9') { nuclear@0: level = *p - '0'; nuclear@0: } else if (*p == 'f') { nuclear@0: strategy = Z_FILTERED; nuclear@0: } else if (*p == 'h') { nuclear@0: strategy = Z_HUFFMAN_ONLY; nuclear@0: } else if (*p == 'R') { nuclear@0: strategy = Z_RLE; nuclear@0: } else { nuclear@0: *m++ = *p; /* copy the mode */ nuclear@0: } nuclear@0: } while (*p++ && m != fmode + sizeof(fmode)); nuclear@0: if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL; nuclear@0: nuclear@0: if (s->mode == 'w') { nuclear@0: #ifdef NO_GZCOMPRESS nuclear@0: err = Z_STREAM_ERROR; nuclear@0: #else nuclear@0: err = deflateInit2(&(s->stream), level, nuclear@0: Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy); nuclear@0: /* windowBits is passed < 0 to suppress zlib header */ nuclear@0: nuclear@0: s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE); nuclear@0: #endif nuclear@0: if (err != Z_OK || s->outbuf == Z_NULL) { nuclear@0: return destroy(s), (gzFile)Z_NULL; nuclear@0: } nuclear@0: } else { nuclear@0: s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); nuclear@0: nuclear@0: err = inflateInit2(&(s->stream), -MAX_WBITS); nuclear@0: /* windowBits is passed < 0 to tell that there is no zlib header. nuclear@0: * Note that in this case inflate *requires* an extra "dummy" byte nuclear@0: * after the compressed stream in order to complete decompression and nuclear@0: * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are nuclear@0: * present after the compressed stream. nuclear@0: */ nuclear@0: if (err != Z_OK || s->inbuf == Z_NULL) { nuclear@0: return destroy(s), (gzFile)Z_NULL; nuclear@0: } nuclear@0: } nuclear@0: s->stream.avail_out = Z_BUFSIZE; nuclear@0: nuclear@0: errno = 0; nuclear@0: s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode); nuclear@0: nuclear@0: if (s->file == NULL) { nuclear@0: return destroy(s), (gzFile)Z_NULL; nuclear@0: } nuclear@0: if (s->mode == 'w') { nuclear@0: /* Write a very simple .gz header: nuclear@0: */ nuclear@0: fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1], nuclear@0: Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE); nuclear@0: s->start = 10L; nuclear@0: /* We use 10L instead of ftell(s->file) to because ftell causes an nuclear@0: * fflush on some systems. This version of the library doesn't use nuclear@0: * start anyway in write mode, so this initialization is not nuclear@0: * necessary. nuclear@0: */ nuclear@0: } else { nuclear@0: check_header(s); /* skip the .gz header */ nuclear@0: s->start = ftell(s->file) - s->stream.avail_in; nuclear@0: } nuclear@0: nuclear@0: return (gzFile)s; nuclear@0: } nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Opens a gzip (.gz) file for reading or writing. nuclear@0: */ nuclear@0: gzFile ZEXPORT gzopen (path, mode) nuclear@0: const char *path; nuclear@0: const char *mode; nuclear@0: { nuclear@0: return gz_open (path, mode, -1); nuclear@0: } nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Associate a gzFile with the file descriptor fd. fd is not dup'ed here nuclear@0: to mimic the behavio(u)r of fdopen. nuclear@0: */ nuclear@0: gzFile ZEXPORT gzdopen (fd, mode) nuclear@0: int fd; nuclear@0: const char *mode; nuclear@0: { nuclear@0: char name[46]; /* allow for up to 128-bit integers */ nuclear@0: nuclear@0: if (fd < 0) return (gzFile)Z_NULL; nuclear@0: sprintf(name, "", fd); /* for debugging */ nuclear@0: nuclear@0: return gz_open (name, mode, fd); nuclear@0: } nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: * Update the compression level and strategy nuclear@0: */ nuclear@0: int ZEXPORT gzsetparams (file, level, strategy) nuclear@0: gzFile file; nuclear@0: int level; nuclear@0: int strategy; nuclear@0: { nuclear@0: gz_stream *s = (gz_stream*)file; nuclear@0: nuclear@0: if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR; nuclear@0: nuclear@0: /* Make room to allow flushing */ nuclear@0: if (s->stream.avail_out == 0) { nuclear@0: nuclear@0: s->stream.next_out = s->outbuf; nuclear@0: if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) { nuclear@0: s->z_err = Z_ERRNO; nuclear@0: } nuclear@0: s->stream.avail_out = Z_BUFSIZE; nuclear@0: } nuclear@0: nuclear@0: return deflateParams (&(s->stream), level, strategy); nuclear@0: } nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Read a byte from a gz_stream; update next_in and avail_in. Return EOF nuclear@0: for end of file. nuclear@0: IN assertion: the stream s has been sucessfully opened for reading. nuclear@0: */ nuclear@0: local int get_byte(s) nuclear@0: gz_stream *s; nuclear@0: { nuclear@0: if (s->z_eof) return EOF; nuclear@0: if (s->stream.avail_in == 0) { nuclear@0: errno = 0; nuclear@0: s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file); nuclear@0: if (s->stream.avail_in == 0) { nuclear@0: s->z_eof = 1; nuclear@0: if (ferror(s->file)) s->z_err = Z_ERRNO; nuclear@0: return EOF; nuclear@0: } nuclear@0: s->stream.next_in = s->inbuf; nuclear@0: } nuclear@0: s->stream.avail_in--; nuclear@0: return *(s->stream.next_in)++; nuclear@0: } nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Check the gzip header of a gz_stream opened for reading. Set the stream nuclear@0: mode to transparent if the gzip magic header is not present; set s->err nuclear@0: to Z_DATA_ERROR if the magic header is present but the rest of the header nuclear@0: is incorrect. nuclear@0: IN assertion: the stream s has already been created sucessfully; nuclear@0: s->stream.avail_in is zero for the first time, but may be non-zero nuclear@0: for concatenated .gz files. nuclear@0: */ nuclear@0: local void check_header(s) nuclear@0: gz_stream *s; nuclear@0: { nuclear@0: int method; /* method byte */ nuclear@0: int flags; /* flags byte */ nuclear@0: uInt len; nuclear@0: int c; nuclear@0: nuclear@0: /* Assure two bytes in the buffer so we can peek ahead -- handle case nuclear@0: where first byte of header is at the end of the buffer after the last nuclear@0: gzip segment */ nuclear@0: len = s->stream.avail_in; nuclear@0: if (len < 2) { nuclear@0: if (len) s->inbuf[0] = s->stream.next_in[0]; nuclear@0: errno = 0; nuclear@0: len = (uInt)fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file); nuclear@0: if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO; nuclear@0: s->stream.avail_in += len; nuclear@0: s->stream.next_in = s->inbuf; nuclear@0: if (s->stream.avail_in < 2) { nuclear@0: s->transparent = s->stream.avail_in; nuclear@0: return; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: /* Peek ahead to check the gzip magic header */ nuclear@0: if (s->stream.next_in[0] != gz_magic[0] || nuclear@0: s->stream.next_in[1] != gz_magic[1]) { nuclear@0: s->transparent = 1; nuclear@0: return; nuclear@0: } nuclear@0: s->stream.avail_in -= 2; nuclear@0: s->stream.next_in += 2; nuclear@0: nuclear@0: /* Check the rest of the gzip header */ nuclear@0: method = get_byte(s); nuclear@0: flags = get_byte(s); nuclear@0: if (method != Z_DEFLATED || (flags & RESERVED) != 0) { nuclear@0: s->z_err = Z_DATA_ERROR; nuclear@0: return; nuclear@0: } nuclear@0: nuclear@0: /* Discard time, xflags and OS code: */ nuclear@0: for (len = 0; len < 6; len++) (void)get_byte(s); nuclear@0: nuclear@0: if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */ nuclear@0: len = (uInt)get_byte(s); nuclear@0: len += ((uInt)get_byte(s))<<8; nuclear@0: /* len is garbage if EOF but the loop below will quit anyway */ nuclear@0: while (len-- != 0 && get_byte(s) != EOF) ; nuclear@0: } nuclear@0: if ((flags & ORIG_NAME) != 0) { /* skip the original file name */ nuclear@0: while ((c = get_byte(s)) != 0 && c != EOF) ; nuclear@0: } nuclear@0: if ((flags & COMMENT) != 0) { /* skip the .gz file comment */ nuclear@0: while ((c = get_byte(s)) != 0 && c != EOF) ; nuclear@0: } nuclear@0: if ((flags & HEAD_CRC) != 0) { /* skip the header crc */ nuclear@0: for (len = 0; len < 2; len++) (void)get_byte(s); nuclear@0: } nuclear@0: s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK; nuclear@0: } nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: * Cleanup then free the given gz_stream. Return a zlib error code. nuclear@0: Try freeing in the reverse order of allocations. nuclear@0: */ nuclear@0: local int destroy (s) nuclear@0: gz_stream *s; nuclear@0: { nuclear@0: int err = Z_OK; nuclear@0: nuclear@0: if (!s) return Z_STREAM_ERROR; nuclear@0: nuclear@0: TRYFREE(s->msg); nuclear@0: nuclear@0: if (s->stream.state != NULL) { nuclear@0: if (s->mode == 'w') { nuclear@0: #ifdef NO_GZCOMPRESS nuclear@0: err = Z_STREAM_ERROR; nuclear@0: #else nuclear@0: err = deflateEnd(&(s->stream)); nuclear@0: #endif nuclear@0: } else if (s->mode == 'r') { nuclear@0: err = inflateEnd(&(s->stream)); nuclear@0: } nuclear@0: } nuclear@0: if (s->file != NULL && fclose(s->file)) { nuclear@0: #ifdef ESPIPE nuclear@0: if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */ nuclear@0: #endif nuclear@0: err = Z_ERRNO; nuclear@0: } nuclear@0: if (s->z_err < 0) err = s->z_err; nuclear@0: nuclear@0: TRYFREE(s->inbuf); nuclear@0: TRYFREE(s->outbuf); nuclear@0: TRYFREE(s->path); nuclear@0: TRYFREE(s); nuclear@0: return err; nuclear@0: } nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Reads the given number of uncompressed bytes from the compressed file. nuclear@0: gzread returns the number of bytes actually read (0 for end of file). nuclear@0: */ nuclear@0: int ZEXPORT gzread (file, buf, len) nuclear@0: gzFile file; nuclear@0: voidp buf; nuclear@0: unsigned len; nuclear@0: { nuclear@0: gz_stream *s = (gz_stream*)file; nuclear@0: Bytef *start = (Bytef*)buf; /* starting point for crc computation */ nuclear@0: Byte *next_out; /* == stream.next_out but not forced far (for MSDOS) */ nuclear@0: nuclear@0: if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR; nuclear@0: nuclear@0: if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1; nuclear@0: if (s->z_err == Z_STREAM_END) return 0; /* EOF */ nuclear@0: nuclear@0: next_out = (Byte*)buf; nuclear@0: s->stream.next_out = (Bytef*)buf; nuclear@0: s->stream.avail_out = len; nuclear@0: nuclear@0: if (s->stream.avail_out && s->back != EOF) { nuclear@0: *next_out++ = s->back; nuclear@0: s->stream.next_out++; nuclear@0: s->stream.avail_out--; nuclear@0: s->back = EOF; nuclear@0: s->out++; nuclear@0: start++; nuclear@0: if (s->last) { nuclear@0: s->z_err = Z_STREAM_END; nuclear@0: return 1; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: while (s->stream.avail_out != 0) { nuclear@0: nuclear@0: if (s->transparent) { nuclear@0: /* Copy first the lookahead bytes: */ nuclear@0: uInt n = s->stream.avail_in; nuclear@0: if (n > s->stream.avail_out) n = s->stream.avail_out; nuclear@0: if (n > 0) { nuclear@0: zmemcpy(s->stream.next_out, s->stream.next_in, n); nuclear@0: next_out += n; nuclear@0: s->stream.next_out = next_out; nuclear@0: s->stream.next_in += n; nuclear@0: s->stream.avail_out -= n; nuclear@0: s->stream.avail_in -= n; nuclear@0: } nuclear@0: if (s->stream.avail_out > 0) { nuclear@0: s->stream.avail_out -= nuclear@0: (uInt)fread(next_out, 1, s->stream.avail_out, s->file); nuclear@0: } nuclear@0: len -= s->stream.avail_out; nuclear@0: s->in += len; nuclear@0: s->out += len; nuclear@0: if (len == 0) s->z_eof = 1; nuclear@0: return (int)len; nuclear@0: } nuclear@0: if (s->stream.avail_in == 0 && !s->z_eof) { nuclear@0: nuclear@0: errno = 0; nuclear@0: s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file); nuclear@0: if (s->stream.avail_in == 0) { nuclear@0: s->z_eof = 1; nuclear@0: if (ferror(s->file)) { nuclear@0: s->z_err = Z_ERRNO; nuclear@0: break; nuclear@0: } nuclear@0: } nuclear@0: s->stream.next_in = s->inbuf; nuclear@0: } nuclear@0: s->in += s->stream.avail_in; nuclear@0: s->out += s->stream.avail_out; nuclear@0: s->z_err = inflate(&(s->stream), Z_NO_FLUSH); nuclear@0: s->in -= s->stream.avail_in; nuclear@0: s->out -= s->stream.avail_out; nuclear@0: nuclear@0: if (s->z_err == Z_STREAM_END) { nuclear@0: /* Check CRC and original size */ nuclear@0: s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start)); nuclear@0: start = s->stream.next_out; nuclear@0: nuclear@0: if (getLong(s) != s->crc) { nuclear@0: s->z_err = Z_DATA_ERROR; nuclear@0: } else { nuclear@0: (void)getLong(s); nuclear@0: /* The uncompressed length returned by above getlong() may be nuclear@0: * different from s->out in case of concatenated .gz files. nuclear@0: * Check for such files: nuclear@0: */ nuclear@0: check_header(s); nuclear@0: if (s->z_err == Z_OK) { nuclear@0: inflateReset(&(s->stream)); nuclear@0: s->crc = crc32(0L, Z_NULL, 0); nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: if (s->z_err != Z_OK || s->z_eof) break; nuclear@0: } nuclear@0: s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start)); nuclear@0: nuclear@0: if (len == s->stream.avail_out && nuclear@0: (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO)) nuclear@0: return -1; nuclear@0: return (int)(len - s->stream.avail_out); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Reads one byte from the compressed file. gzgetc returns this byte nuclear@0: or -1 in case of end of file or error. nuclear@0: */ nuclear@0: int ZEXPORT gzgetc(file) nuclear@0: gzFile file; nuclear@0: { nuclear@0: unsigned char c; nuclear@0: nuclear@0: return gzread(file, &c, 1) == 1 ? c : -1; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Push one byte back onto the stream. nuclear@0: */ nuclear@0: int ZEXPORT gzungetc(c, file) nuclear@0: int c; nuclear@0: gzFile file; nuclear@0: { nuclear@0: gz_stream *s = (gz_stream*)file; nuclear@0: nuclear@0: if (s == NULL || s->mode != 'r' || c == EOF || s->back != EOF) return EOF; nuclear@0: s->back = c; nuclear@0: s->out--; nuclear@0: s->last = (s->z_err == Z_STREAM_END); nuclear@0: if (s->last) s->z_err = Z_OK; nuclear@0: s->z_eof = 0; nuclear@0: return c; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Reads bytes from the compressed file until len-1 characters are nuclear@0: read, or a newline character is read and transferred to buf, or an nuclear@0: end-of-file condition is encountered. The string is then terminated nuclear@0: with a null character. nuclear@0: gzgets returns buf, or Z_NULL in case of error. nuclear@0: nuclear@0: The current implementation is not optimized at all. nuclear@0: */ nuclear@0: char * ZEXPORT gzgets(file, buf, len) nuclear@0: gzFile file; nuclear@0: char *buf; nuclear@0: int len; nuclear@0: { nuclear@0: char *b = buf; nuclear@0: if (buf == Z_NULL || len <= 0) return Z_NULL; nuclear@0: nuclear@0: while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ; nuclear@0: *buf = '\0'; nuclear@0: return b == buf && len > 0 ? Z_NULL : b; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: #ifndef NO_GZCOMPRESS nuclear@0: /* =========================================================================== nuclear@0: Writes the given number of uncompressed bytes into the compressed file. nuclear@0: gzwrite returns the number of bytes actually written (0 in case of error). nuclear@0: */ nuclear@0: int ZEXPORT gzwrite (file, buf, len) nuclear@0: gzFile file; nuclear@0: voidpc buf; nuclear@0: unsigned len; nuclear@0: { nuclear@0: gz_stream *s = (gz_stream*)file; nuclear@0: nuclear@0: if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR; nuclear@0: nuclear@0: s->stream.next_in = (Bytef*)buf; nuclear@0: s->stream.avail_in = len; nuclear@0: nuclear@0: while (s->stream.avail_in != 0) { nuclear@0: nuclear@0: if (s->stream.avail_out == 0) { nuclear@0: nuclear@0: s->stream.next_out = s->outbuf; nuclear@0: if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) { nuclear@0: s->z_err = Z_ERRNO; nuclear@0: break; nuclear@0: } nuclear@0: s->stream.avail_out = Z_BUFSIZE; nuclear@0: } nuclear@0: s->in += s->stream.avail_in; nuclear@0: s->out += s->stream.avail_out; nuclear@0: s->z_err = deflate(&(s->stream), Z_NO_FLUSH); nuclear@0: s->in -= s->stream.avail_in; nuclear@0: s->out -= s->stream.avail_out; nuclear@0: if (s->z_err != Z_OK) break; nuclear@0: } nuclear@0: s->crc = crc32(s->crc, (const Bytef *)buf, len); nuclear@0: nuclear@0: return (int)(len - s->stream.avail_in); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Converts, formats, and writes the args to the compressed file under nuclear@0: control of the format string, as in fprintf. gzprintf returns the number of nuclear@0: uncompressed bytes actually written (0 in case of error). nuclear@0: */ nuclear@0: #ifdef STDC nuclear@0: #include nuclear@0: nuclear@0: int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...) nuclear@0: { nuclear@0: char buf[Z_PRINTF_BUFSIZE]; nuclear@0: va_list va; nuclear@0: int len; nuclear@0: nuclear@0: buf[sizeof(buf) - 1] = 0; nuclear@0: va_start(va, format); nuclear@0: #ifdef NO_vsnprintf nuclear@0: # ifdef HAS_vsprintf_void nuclear@0: (void)vsprintf(buf, format, va); nuclear@0: va_end(va); nuclear@0: for (len = 0; len < sizeof(buf); len++) nuclear@0: if (buf[len] == 0) break; nuclear@0: # else nuclear@0: len = vsprintf(buf, format, va); nuclear@0: va_end(va); nuclear@0: # endif nuclear@0: #else nuclear@0: # ifdef HAS_vsnprintf_void nuclear@0: (void)vsnprintf(buf, sizeof(buf), format, va); nuclear@0: va_end(va); nuclear@0: len = strlen(buf); nuclear@0: # else nuclear@0: len = vsnprintf(buf, sizeof(buf), format, va); nuclear@0: va_end(va); nuclear@0: # endif nuclear@0: #endif nuclear@0: if (len <= 0 || len >= (int)sizeof(buf) || buf[sizeof(buf) - 1] != 0) nuclear@0: return 0; nuclear@0: return gzwrite(file, buf, (unsigned)len); nuclear@0: } nuclear@0: #else /* not ANSI C */ nuclear@0: nuclear@0: int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, nuclear@0: a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) nuclear@0: gzFile file; nuclear@0: const char *format; nuclear@0: int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, nuclear@0: a11, a12, a13, a14, a15, a16, a17, a18, a19, a20; nuclear@0: { nuclear@0: char buf[Z_PRINTF_BUFSIZE]; nuclear@0: int len; nuclear@0: nuclear@0: buf[sizeof(buf) - 1] = 0; nuclear@0: #ifdef NO_snprintf nuclear@0: # ifdef HAS_sprintf_void nuclear@0: sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8, nuclear@0: a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); nuclear@0: for (len = 0; len < sizeof(buf); len++) nuclear@0: if (buf[len] == 0) break; nuclear@0: # else nuclear@0: len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8, nuclear@0: a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); nuclear@0: # endif nuclear@0: #else nuclear@0: # ifdef HAS_snprintf_void nuclear@0: snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8, nuclear@0: a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); nuclear@0: len = strlen(buf); nuclear@0: # else nuclear@0: len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8, nuclear@0: a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); nuclear@0: # endif nuclear@0: #endif nuclear@0: if (len <= 0 || len >= sizeof(buf) || buf[sizeof(buf) - 1] != 0) nuclear@0: return 0; nuclear@0: return gzwrite(file, buf, len); nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Writes c, converted to an unsigned char, into the compressed file. nuclear@0: gzputc returns the value that was written, or -1 in case of error. nuclear@0: */ nuclear@0: int ZEXPORT gzputc(file, c) nuclear@0: gzFile file; nuclear@0: int c; nuclear@0: { nuclear@0: unsigned char cc = (unsigned char) c; /* required for big endian systems */ nuclear@0: nuclear@0: return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Writes the given null-terminated string to the compressed file, excluding nuclear@0: the terminating null character. nuclear@0: gzputs returns the number of characters written, or -1 in case of error. nuclear@0: */ nuclear@0: int ZEXPORT gzputs(file, s) nuclear@0: gzFile file; nuclear@0: const char *s; nuclear@0: { nuclear@0: return gzwrite(file, (char*)s, (unsigned)strlen(s)); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Flushes all pending output into the compressed file. The parameter nuclear@0: flush is as in the deflate() function. nuclear@0: */ nuclear@0: local int do_flush (file, flush) nuclear@0: gzFile file; nuclear@0: int flush; nuclear@0: { nuclear@0: uInt len; nuclear@0: int done = 0; nuclear@0: gz_stream *s = (gz_stream*)file; nuclear@0: nuclear@0: if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR; nuclear@0: nuclear@0: s->stream.avail_in = 0; /* should be zero already anyway */ nuclear@0: nuclear@0: for (;;) { nuclear@0: len = Z_BUFSIZE - s->stream.avail_out; nuclear@0: nuclear@0: if (len != 0) { nuclear@0: if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) { nuclear@0: s->z_err = Z_ERRNO; nuclear@0: return Z_ERRNO; nuclear@0: } nuclear@0: s->stream.next_out = s->outbuf; nuclear@0: s->stream.avail_out = Z_BUFSIZE; nuclear@0: } nuclear@0: if (done) break; nuclear@0: s->out += s->stream.avail_out; nuclear@0: s->z_err = deflate(&(s->stream), flush); nuclear@0: s->out -= s->stream.avail_out; nuclear@0: nuclear@0: /* Ignore the second of two consecutive flushes: */ nuclear@0: if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK; nuclear@0: nuclear@0: /* deflate has finished flushing only when it hasn't used up nuclear@0: * all the available space in the output buffer: nuclear@0: */ nuclear@0: done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END); nuclear@0: nuclear@0: if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break; nuclear@0: } nuclear@0: return s->z_err == Z_STREAM_END ? Z_OK : s->z_err; nuclear@0: } nuclear@0: nuclear@0: int ZEXPORT gzflush (file, flush) nuclear@0: gzFile file; nuclear@0: int flush; nuclear@0: { nuclear@0: gz_stream *s = (gz_stream*)file; nuclear@0: int err = do_flush (file, flush); nuclear@0: nuclear@0: if (err) return err; nuclear@0: fflush(s->file); nuclear@0: return s->z_err == Z_STREAM_END ? Z_OK : s->z_err; nuclear@0: } nuclear@0: #endif /* NO_GZCOMPRESS */ nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Sets the starting position for the next gzread or gzwrite on the given nuclear@0: compressed file. The offset represents a number of bytes in the nuclear@0: gzseek returns the resulting offset location as measured in bytes from nuclear@0: the beginning of the uncompressed stream, or -1 in case of error. nuclear@0: SEEK_END is not implemented, returns error. nuclear@0: In this version of the library, gzseek can be extremely slow. nuclear@0: */ nuclear@0: z_off_t ZEXPORT gzseek (file, offset, whence) nuclear@0: gzFile file; nuclear@0: z_off_t offset; nuclear@0: int whence; nuclear@0: { nuclear@0: gz_stream *s = (gz_stream*)file; nuclear@0: nuclear@0: if (s == NULL || whence == SEEK_END || nuclear@0: s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) { nuclear@0: return -1L; nuclear@0: } nuclear@0: nuclear@0: if (s->mode == 'w') { nuclear@0: #ifdef NO_GZCOMPRESS nuclear@0: return -1L; nuclear@0: #else nuclear@0: if (whence == SEEK_SET) { nuclear@0: offset -= s->in; nuclear@0: } nuclear@0: if (offset < 0) return -1L; nuclear@0: nuclear@0: /* At this point, offset is the number of zero bytes to write. */ nuclear@0: if (s->inbuf == Z_NULL) { nuclear@0: s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */ nuclear@0: if (s->inbuf == Z_NULL) return -1L; nuclear@0: zmemzero(s->inbuf, Z_BUFSIZE); nuclear@0: } nuclear@0: while (offset > 0) { nuclear@0: uInt size = Z_BUFSIZE; nuclear@0: if (offset < Z_BUFSIZE) size = (uInt)offset; nuclear@0: nuclear@0: size = gzwrite(file, s->inbuf, size); nuclear@0: if (size == 0) return -1L; nuclear@0: nuclear@0: offset -= size; nuclear@0: } nuclear@0: return s->in; nuclear@0: #endif nuclear@0: } nuclear@0: /* Rest of function is for reading only */ nuclear@0: nuclear@0: /* compute absolute position */ nuclear@0: if (whence == SEEK_CUR) { nuclear@0: offset += s->out; nuclear@0: } nuclear@0: if (offset < 0) return -1L; nuclear@0: nuclear@0: if (s->transparent) { nuclear@0: /* map to fseek */ nuclear@0: s->back = EOF; nuclear@0: s->stream.avail_in = 0; nuclear@0: s->stream.next_in = s->inbuf; nuclear@0: if (fseek(s->file, offset, SEEK_SET) < 0) return -1L; nuclear@0: nuclear@0: s->in = s->out = offset; nuclear@0: return offset; nuclear@0: } nuclear@0: nuclear@0: /* For a negative seek, rewind and use positive seek */ nuclear@0: if (offset >= s->out) { nuclear@0: offset -= s->out; nuclear@0: } else if (gzrewind(file) < 0) { nuclear@0: return -1L; nuclear@0: } nuclear@0: /* offset is now the number of bytes to skip. */ nuclear@0: nuclear@0: if (offset != 0 && s->outbuf == Z_NULL) { nuclear@0: s->outbuf = (Byte*)ALLOC(Z_BUFSIZE); nuclear@0: if (s->outbuf == Z_NULL) return -1L; nuclear@0: } nuclear@0: if (offset && s->back != EOF) { nuclear@0: s->back = EOF; nuclear@0: s->out++; nuclear@0: offset--; nuclear@0: if (s->last) s->z_err = Z_STREAM_END; nuclear@0: } nuclear@0: while (offset > 0) { nuclear@0: int size = Z_BUFSIZE; nuclear@0: if (offset < Z_BUFSIZE) size = (int)offset; nuclear@0: nuclear@0: size = gzread(file, s->outbuf, (uInt)size); nuclear@0: if (size <= 0) return -1L; nuclear@0: offset -= size; nuclear@0: } nuclear@0: return s->out; nuclear@0: } nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Rewinds input file. nuclear@0: */ nuclear@0: int ZEXPORT gzrewind (file) nuclear@0: gzFile file; nuclear@0: { nuclear@0: gz_stream *s = (gz_stream*)file; nuclear@0: nuclear@0: if (s == NULL || s->mode != 'r') return -1; nuclear@0: nuclear@0: s->z_err = Z_OK; nuclear@0: s->z_eof = 0; nuclear@0: s->back = EOF; nuclear@0: s->stream.avail_in = 0; nuclear@0: s->stream.next_in = s->inbuf; nuclear@0: s->crc = crc32(0L, Z_NULL, 0); nuclear@0: if (!s->transparent) (void)inflateReset(&s->stream); nuclear@0: s->in = 0; nuclear@0: s->out = 0; nuclear@0: return fseek(s->file, s->start, SEEK_SET); nuclear@0: } nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Returns the starting position for the next gzread or gzwrite on the nuclear@0: given compressed file. This position represents a number of bytes in the nuclear@0: uncompressed data stream. nuclear@0: */ nuclear@0: z_off_t ZEXPORT gztell (file) nuclear@0: gzFile file; nuclear@0: { nuclear@0: return gzseek(file, 0L, SEEK_CUR); nuclear@0: } nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Returns 1 when EOF has previously been detected reading the given nuclear@0: input stream, otherwise zero. nuclear@0: */ nuclear@0: int ZEXPORT gzeof (file) nuclear@0: gzFile file; nuclear@0: { nuclear@0: gz_stream *s = (gz_stream*)file; nuclear@0: nuclear@0: /* With concatenated compressed files that can have embedded nuclear@0: * crc trailers, z_eof is no longer the only/best indicator of EOF nuclear@0: * on a gz_stream. Handle end-of-stream error explicitly here. nuclear@0: */ nuclear@0: if (s == NULL || s->mode != 'r') return 0; nuclear@0: if (s->z_eof) return 1; nuclear@0: return s->z_err == Z_STREAM_END; nuclear@0: } nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Returns 1 if reading and doing so transparently, otherwise zero. nuclear@0: */ nuclear@0: int ZEXPORT gzdirect (file) nuclear@0: gzFile file; nuclear@0: { nuclear@0: gz_stream *s = (gz_stream*)file; nuclear@0: nuclear@0: if (s == NULL || s->mode != 'r') return 0; nuclear@0: return s->transparent; nuclear@0: } nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Outputs a long in LSB order to the given file nuclear@0: */ nuclear@0: local void putLong (file, x) nuclear@0: FILE *file; nuclear@0: uLong x; nuclear@0: { nuclear@0: int n; nuclear@0: for (n = 0; n < 4; n++) { nuclear@0: fputc((int)(x & 0xff), file); nuclear@0: x >>= 8; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Reads a long in LSB order from the given gz_stream. Sets z_err in case nuclear@0: of error. nuclear@0: */ nuclear@0: local uLong getLong (s) nuclear@0: gz_stream *s; nuclear@0: { nuclear@0: uLong x = (uLong)get_byte(s); nuclear@0: int c; nuclear@0: nuclear@0: x += ((uLong)get_byte(s))<<8; nuclear@0: x += ((uLong)get_byte(s))<<16; nuclear@0: c = get_byte(s); nuclear@0: if (c == EOF) s->z_err = Z_DATA_ERROR; nuclear@0: x += ((uLong)c)<<24; nuclear@0: return x; nuclear@0: } nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Flushes all pending output if necessary, closes the compressed file nuclear@0: and deallocates all the (de)compression state. nuclear@0: */ nuclear@0: int ZEXPORT gzclose (file) nuclear@0: gzFile file; nuclear@0: { nuclear@0: gz_stream *s = (gz_stream*)file; nuclear@0: nuclear@0: if (s == NULL) return Z_STREAM_ERROR; nuclear@0: nuclear@0: if (s->mode == 'w') { nuclear@0: #ifdef NO_GZCOMPRESS nuclear@0: return Z_STREAM_ERROR; nuclear@0: #else nuclear@0: if (do_flush (file, Z_FINISH) != Z_OK) nuclear@0: return destroy((gz_stream*)file); nuclear@0: nuclear@0: putLong (s->file, s->crc); nuclear@0: putLong (s->file, (uLong)(s->in & 0xffffffff)); nuclear@0: #endif nuclear@0: } nuclear@0: return destroy((gz_stream*)file); nuclear@0: } nuclear@0: nuclear@0: #ifdef STDC nuclear@0: # define zstrerror(errnum) strerror(errnum) nuclear@0: #else nuclear@0: # define zstrerror(errnum) "" nuclear@0: #endif nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Returns the error message for the last error which occurred on the nuclear@0: given compressed file. errnum is set to zlib error number. If an nuclear@0: error occurred in the file system and not in the compression library, nuclear@0: errnum is set to Z_ERRNO and the application may consult errno nuclear@0: to get the exact error code. nuclear@0: */ nuclear@0: const char * ZEXPORT gzerror (file, errnum) nuclear@0: gzFile file; nuclear@0: int *errnum; nuclear@0: { nuclear@0: char *m; nuclear@0: gz_stream *s = (gz_stream*)file; nuclear@0: nuclear@0: if (s == NULL) { nuclear@0: *errnum = Z_STREAM_ERROR; nuclear@0: return (const char*)ERR_MSG(Z_STREAM_ERROR); nuclear@0: } nuclear@0: *errnum = s->z_err; nuclear@0: if (*errnum == Z_OK) return (const char*)""; nuclear@0: nuclear@0: m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg); nuclear@0: nuclear@0: if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err); nuclear@0: nuclear@0: TRYFREE(s->msg); nuclear@0: s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3); nuclear@0: if (s->msg == Z_NULL) return (const char*)ERR_MSG(Z_MEM_ERROR); nuclear@0: strcpy(s->msg, s->path); nuclear@0: strcat(s->msg, ": "); nuclear@0: strcat(s->msg, m); nuclear@0: return (const char*)s->msg; nuclear@0: } nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Clear the error and end-of-file flags, and do the same for the real file. nuclear@0: */ nuclear@0: void ZEXPORT gzclearerr (file) nuclear@0: gzFile file; nuclear@0: { nuclear@0: gz_stream *s = (gz_stream*)file; nuclear@0: nuclear@0: if (s == NULL) return; nuclear@0: if (s->z_err != Z_STREAM_END) s->z_err = Z_OK; nuclear@0: s->z_eof = 0; nuclear@0: clearerr(s->file); nuclear@0: }