/* gzio.c -- IO on .gz files
* Copyright (C) 1995-2002 Jean-loup Gailly.
* For conditions of distribution and use, see copyright notice in zlib.h
*
* Compile this file with -DNO_DEFLATE to avoid the compression code.
*/
#include <stdio.h>
#include "zutil.h"
#ifndef Z_BUFSIZE
# ifdef MAXSEG_64K
# else
# endif
#endif
#ifndef Z_PRINTF_BUFSIZE
#endif
/* gzip flag byte */
typedef struct gz_stream {
} gz_stream;
/* ===========================================================================
Opens a gzip (.gz) file for reading or writing. The mode parameter
is as in fopen ("rb" or "wb"). The file is given either by file descriptor
or path name (if fd == -1).
gz_open return NULL if the file could not be opened or if there was
insufficient memory to allocate the (de)compression state; errno
can be checked to distinguish the two cases (if errno is zero, the
zlib error is Z_MEM_ERROR).
*/
const char *path;
const char *mode;
int fd;
{
int err;
char *p = (char*)mode;
gz_stream *s;
char *m = fmode;
if (!s) return Z_NULL;
s->z_eof = 0;
s->transparent = 0;
}
s->mode = '\0';
do {
if (*p >= '0' && *p <= '9') {
level = *p - '0';
} else if (*p == 'f') {
} else if (*p == 'h') {
} else {
*m++ = *p; /* copy the mode */
}
if (s->mode == 'w') {
#ifdef NO_DEFLATE
#else
/* windowBits is passed < 0 to suppress zlib header */
#endif
}
} else {
/* windowBits is passed < 0 to tell that there is no zlib header.
* Note that in this case inflate *requires* an extra "dummy" byte
* after the compressed stream in order to complete decompression and
* return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
* present after the compressed stream.
*/
}
}
errno = 0;
}
if (s->mode == 'w') {
/* Write a very simple .gz header:
*/
s->startpos = 10L;
/* We use 10L instead of ftell(s->file) to because ftell causes an
* fflush on some systems. This version of the library doesn't use
* startpos anyway in write mode, so this initialization is not
* necessary.
*/
} else {
check_header(s); /* skip the .gz header */
}
return (gzFile)s;
}
/* ===========================================================================
Opens a gzip (.gz) file for reading or writing.
*/
const char *path;
const char *mode;
{
}
/* ===========================================================================
Associate a gzFile with the file descriptor fd. fd is not dup'ed here
to mimic the behavio(u)r of fdopen.
*/
int fd;
const char *mode;
{
}
/* ===========================================================================
* Update the compression level and strategy
*/
int level;
int strategy;
{
/* Make room to allow flushing */
}
}
}
/* ===========================================================================
Read a byte from a gz_stream; update next_in and avail_in. Return EOF
for end of file.
IN assertion: the stream s has been sucessfully opened for reading.
*/
gz_stream *s;
{
errno = 0;
s->z_eof = 1;
return EOF;
}
}
}
/* ===========================================================================
Check the gzip header of a gz_stream opened for reading. Set the stream
mode to transparent if the gzip magic header is not present; set s->err
to Z_DATA_ERROR if the magic header is present but the rest of the header
is incorrect.
IN assertion: the stream s has already been created sucessfully;
s->stream.avail_in is zero for the first time, but may be non-zero
for concatenated .gz files.
*/
gz_stream *s;
{
int c;
/* Check the gzip magic header */
c = get_byte(s);
if (c != EOF) {
s->transparent = 1;
}
return;
}
}
s->z_err = Z_DATA_ERROR;
return;
}
/* Discard time, xflags and OS code: */
/* len is garbage if EOF but the loop below will quit anyway */
}
}
}
}
}
/* ===========================================================================
* Cleanup then free the given gz_stream. Return a zlib error code.
Try freeing in the reverse order of allocations.
*/
gz_stream *s;
{
if (!s) return Z_STREAM_ERROR;
if (s->mode == 'w') {
#ifdef NO_DEFLATE
#else
#endif
} else if (s->mode == 'r') {
}
}
#ifdef ESPIPE
#endif
}
TRYFREE(s);
return err;
}
/* ===========================================================================
Reads the given number of uncompressed bytes from the compressed file.
gzread returns the number of bytes actually read (0 for end of file).
*/
unsigned len;
{
if (s->transparent) {
/* Copy first the lookahead bytes: */
if (n > 0) {
next_out += n;
}
s->file);
}
return (int)len;
}
errno = 0;
s->z_eof = 1;
break;
}
}
}
if (s->z_err == Z_STREAM_END) {
/* Check CRC and original size */
s->z_err = Z_DATA_ERROR;
} else {
(void)getLong(s);
/* The uncompressed length returned by above getlong() may
* be different from s->stream.total_out) in case of
* concatenated .gz files. Check for such files:
*/
check_header(s);
inflateReset(&(s->stream));
}
}
}
}
}
/* ===========================================================================
Reads one byte from the compressed file. gzgetc returns this byte
or -1 in case of end of file or error.
*/
{
unsigned char c;
}
/* ===========================================================================
Reads bytes from the compressed file until len-1 characters are
read, or a newline character is read and transferred to buf, or an
end-of-file condition is encountered. The string is then terminated
with a null character.
gzgets returns buf, or Z_NULL in case of error.
The current implementation is not optimized at all.
*/
char *buf;
int len;
{
char *b = buf;
*buf = '\0';
}
#ifndef NO_DEFLATE
/* ===========================================================================
Writes the given number of uncompressed bytes into the compressed file.
gzwrite returns the number of bytes actually written (0 in case of error).
*/
unsigned len;
{
break;
}
}
}
}
/* ===========================================================================
Converts, formats, and writes the args to the compressed file under
control of the format string, as in fprintf. gzprintf returns the number of
uncompressed bytes actually written (0 in case of error).
*/
#ifdef STDC
#include <stdarg.h>
{
int len;
#ifdef HAS_vsnprintf
#else
#endif
if (len <= 0) return 0;
}
#else /* not ANSI C */
const char *format;
{
int len;
#ifdef HAS_snprintf
#else
#endif
if (len <= 0) return 0;
}
#endif
/* ===========================================================================
Writes c, converted to an unsigned char, into the compressed file.
gzputc returns the value that was written, or -1 in case of error.
*/
int c;
{
}
/* ===========================================================================
Writes the given null-terminated string to the compressed file, excluding
the terminating null character.
gzputs returns the number of characters written, or -1 in case of error.
*/
const char *s;
{
}
/* ===========================================================================
Flushes all pending output into the compressed file. The parameter
flush is as in the deflate() function.
*/
int flush;
{
int done = 0;
for (;;) {
if (len != 0) {
return Z_ERRNO;
}
}
if (done) break;
/* Ignore the second of two consecutive flushes: */
/* deflate has finished flushing only when it hasn't used up
* all the available space in the output buffer:
*/
}
}
int flush;
{
}
#endif /* NO_DEFLATE */
/* ===========================================================================
Sets the starting position for the next gzread or gzwrite on the given
compressed file. The offset represents a number of bytes in the
gzseek returns the resulting offset location as measured in bytes from
the beginning of the uncompressed stream, or -1 in case of error.
SEEK_END is not implemented, returns error.
In this version of the library, gzseek can be extremely slow.
*/
int whence;
{
return -1L;
}
if (s->mode == 'w') {
#ifdef NO_DEFLATE
return -1L;
#else
}
if (offset < 0) return -1L;
/* At this point, offset is the number of zero bytes to write. */
}
while (offset > 0) {
if (size == 0) return -1L;
}
#endif
}
/* Rest of function is for reading only */
/* compute absolute position */
}
if (offset < 0) return -1L;
if (s->transparent) {
/* map to fseek */
return offset;
}
/* For a negative seek, rewind and use positive seek */
return -1L;
}
/* offset is now the number of bytes to skip. */
}
while (offset > 0) {
if (size <= 0) return -1L;
}
}
/* ===========================================================================
Rewinds input file.
*/
{
s->z_eof = 0;
if (s->startpos == 0) { /* not a compressed file */
return 0;
}
(void) inflateReset(&s->stream);
}
/* ===========================================================================
Returns the starting position for the next gzread or gzwrite on the
given compressed file. This position represents a number of bytes in the
uncompressed data stream.
*/
{
}
/* ===========================================================================
Returns 1 when EOF has previously been detected reading the given
input stream, otherwise zero.
*/
{
}
/* ===========================================================================
Outputs a long in LSB order to the given file
*/
uLong x;
{
int n;
for (n = 0; n < 4; n++) {
x >>= 8;
}
}
/* ===========================================================================
Reads a long in LSB order from the given gz_stream. Sets z_err in case
of error.
*/
gz_stream *s;
{
int c;
c = get_byte(s);
x += ((uLong)c)<<24;
return x;
}
/* ===========================================================================
Flushes all pending output if necessary, closes the compressed file
and deallocates all the (de)compression state.
*/
{
int err;
if (s == NULL) return Z_STREAM_ERROR;
if (s->mode == 'w') {
#ifdef NO_DEFLATE
return Z_STREAM_ERROR;
#else
#endif
}
}
/* ===========================================================================
Returns the error message for the last error which occured on the
given compressed file. errnum is set to zlib error number. If an
error occured in the file system and not in the compression library,
errnum is set to Z_ERRNO and the application may consult errno
to get the exact error code.
*/
int *errnum;
{
char *m;
if (s == NULL) {
*errnum = Z_STREAM_ERROR;
return (const char*)ERR_MSG(Z_STREAM_ERROR);
}
return (const char*)s->msg;
}