/* gzio.c -- IO on .gz files
* Copyright (C) 1995-2005 Jean-loup Gailly.
* For conditions of distribution and use, see copyright notice in zlib.h
*
* Compile this file with -DNO_GZCOMPRESS to avoid the compression code.
*/
/* @(#) $Id$ */
#if _PACKAGE_ast
#include <ast.h>
#endif
#include <stdio.h>
#include "zutil.h"
#ifdef NO_DEFLATE /* for compatibility with old definition */
# define NO_GZCOMPRESS
#endif
#ifndef NO_DUMMY_DECL
#endif
#ifndef Z_BUFSIZE
# ifdef MAXSEG_64K
# else
# endif
#endif
#ifndef Z_PRINTF_BUFSIZE
#endif
#ifdef __MVS__
#endif
#if !_PACKAGE_ast
#ifndef ZLIB_STDC
#endif
#endif
/* gzip flag byte */
typedef struct gz_stream {
#if _PACKAGE_ast
#endif
} 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 pointer
or path name (if fp == 0).
gz_open returns 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;
void *buf;
unsigned len;
{
int err;
char *p = (char*)mode;
gz_stream *s;
char *m = fmode;
if (!s) return Z_NULL;
s->z_eof = 0;
s->in = 0;
s->out = 0;
#if _PACKAGE_ast
s->fatal = 0;
s->noclose = 0;
s->nocrc = 0;
s->verified = 0;
#endif
s->transparent = 0;
}
s->mode = '\0';
do {
if (*p >= '0' && *p <= '9') {
level = *p - '0';
} else if (*p == 'f') {
} else if (*p == 'h') {
} else if (*p == 'R') {
#if _PACKAGE_ast
} else if (*p == 'n') {
s->nocrc = 1;
} else if (*p == 'o') {
s->noclose = 1;
} else if (*p == 'v') {
s->verified = 1;
#endif
} else {
*m++ = *p; /* copy the mode */
}
if (s->mode == 'w') {
#ifdef NO_GZCOMPRESS
#else
#if _PACKAGE_ast
s->nocrc = 0;
#endif
/* windowBits is passed < 0 to suppress zlib header */
#endif
}
} else {
len = 0;
if (len) {
#if 0
fprintf(stderr, "AHA gz_open inbuf=< %02x %02x > len=%d offset=%d:%d\n", s->inbuf[0], s->inbuf[1], len, (int)ftell(fp), (int)lseek(fileno(fp), 0, SEEK_CUR));
#endif
}
/* 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->start = 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
* start anyway in write mode, so this initialization is not
* necessary.
*/
} else {
#if _PACKAGE_ast && 0
#endif
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;
{
}
int fd;
const char *mode;
void *buf;
unsigned len;
{
}
/* ===========================================================================
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;
{
}
/* ===========================================================================
Associate a gzFile with the stdio stream fp.
*/
void* fp;
const char *mode;
{
if (!sp)
}
/* ===========================================================================
* 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;
#if _PACKAGE_ast
if (!s->verified)
c = get_byte(s);
if (c != EOF) {
s->transparent = 1;
}
return;
}
}
#else
/* Assure two bytes in the buffer so we can peek ahead -- handle case
where first byte of header is at the end of the buffer after the last
gzip segment */
if (len < 2) {
errno = 0;
return;
}
}
/* Peek ahead to check the gzip magic header */
s->transparent = 1;
return;
}
#endif
/* Check the rest of the gzip header */
s->z_err = Z_DATA_ERROR;
return;
}
/* Discard time, xflags and OS code: */
(void)get_byte(s);
/* 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_GZCOMPRESS
#else
#endif
} else if (s->mode == 'r') {
}
}
#if _PACKAGE_ast
#else
#endif
#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;
{
s->out++;
start++;
if (s->last) {
s->z_err = Z_STREAM_END;
return 1;
}
}
if (s->transparent) {
/* Copy first the lookahead bytes: */
if (n > 0) {
next_out += n;
}
}
return (int)len;
}
errno = 0;
s->z_eof = 1;
break;
}
}
}
if (s->z_err == Z_STREAM_END) {
/* Check CRC and original size */
#if _PACKAGE_ast
if (!s->nocrc)
#endif
#if _PACKAGE_ast
#else
#endif
s->z_err = Z_DATA_ERROR;
} else {
(void)getLong(s);
/* The uncompressed length returned by above getlong() may be
* different from s->out in case of concatenated .gz files.
* Check for such files:
*/
check_header(s);
inflateReset(&(s->stream));
#if _PACKAGE_ast
if (!s->nocrc)
#endif
#if _PACKAGE_ast
else
s->z_err = Z_STREAM_END;
#endif
}
}
}
}
#if _PACKAGE_ast
if (!s->nocrc)
#endif
return -1;
}
/* ===========================================================================
Reads one byte from the compressed file. gzgetc returns this byte
or -1 in case of end of file or error.
*/
{
unsigned char c;
}
/* ===========================================================================
Push one byte back onto the stream.
*/
int c;
{
s->back = c;
s->out--;
s->z_eof = 0;
return 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_GZCOMPRESS
/* ===========================================================================
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).
*/
#if _PACKAGE_ast
#ifdef __STDC__
#else
#endif
{
#ifndef __STDC__
char *format;
#endif
ssize_t n;
return -1;
#ifdef __STDC__
#else
#endif
if (n >= 0)
return n;
}
#else
#ifdef ZLIB_STDC
#include <stdarg.h>
{
int len;
#ifdef NO_vsnprintf
# ifdef HAS_vsprintf_void
# else
# endif
#else
# ifdef HAS_vsnprintf_void
# else
# endif
#endif
return 0;
}
#else /* not ANSI C */
const char *format;
{
int len;
#ifdef NO_snprintf
# ifdef HAS_sprintf_void
# else
# endif
#else
# ifdef HAS_snprintf_void
# else
# endif
#endif
return 0;
}
#endif
#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_GZCOMPRESS */
/* ===========================================================================
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_GZCOMPRESS
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;
}
return s->in;
#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. */
}
s->out++;
offset--;
}
while (offset > 0) {
if (size <= 0) return -1L;
}
return s->out;
}
/* ===========================================================================
Rewinds input file.
*/
{
s->z_eof = 0;
s->in = 0;
s->out = 0;
}
int set;
{
#if _PACKAGE_ast
return crc;
#else
return 0;
#endif
}
/* ===========================================================================
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.
*/
{
/* With concatenated compressed files that can have embedded
* on a gz_stream. Handle end-of-stream error explicitly here.
*/
if (s->z_eof) return 1;
return s->z_err == Z_STREAM_END;
}
/* ===========================================================================
Returns 1 if reading and doing so transparently, otherwise zero.
*/
{
return s->transparent;
}
/* ===========================================================================
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.
*/
{
if (s == NULL) return Z_STREAM_ERROR;
if (s->mode == 'w') {
#ifdef NO_GZCOMPRESS
return Z_STREAM_ERROR;
#else
#endif
}
}
#ifdef ZLIB_STDC
#else
#endif
/* ===========================================================================
Returns the error message for the last error which occurred on the
given compressed file. errnum is set to zlib error number. If an
error occurred 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;
}
/* ===========================================================================
Clear the error and end-of-file flags, and do the same for the real file.
*/
{
if (s == NULL) return;
s->z_eof = 0;
}
#if _PACKAGE_ast
#ifdef z_off64_t
#else
#endif
{
return (z_OFF_t)-1;
if (s->mode == 'w') {
return (z_OFF_t)-1;
s->fatal = 1;
return (z_OFF_t)-1;
}
}
return (z_OFF_t)-1;
return (z_OFF_t)-1;
}
#ifdef z_off64_t
{
}
#endif
#endif