199767f8919635c4928607450d9e0abb932109ceToomas Soome/* gzguts.h -- zlib internal header definitions for gz* operations
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler
199767f8919635c4928607450d9e0abb932109ceToomas Soome * For conditions of distribution and use, see copyright notice in zlib.h
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef _LARGEFILE64_SOURCE
199767f8919635c4928607450d9e0abb932109ceToomas Soome# ifndef _LARGEFILE_SOURCE
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define _LARGEFILE_SOURCE 1
199767f8919635c4928607450d9e0abb932109ceToomas Soome# endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome# ifdef _FILE_OFFSET_BITS
199767f8919635c4928607450d9e0abb932109ceToomas Soome# undef _FILE_OFFSET_BITS
199767f8919635c4928607450d9e0abb932109ceToomas Soome# endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef HAVE_HIDDEN
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define ZLIB_INTERNAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stdio.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "zlib.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef STDC
199767f8919635c4928607450d9e0abb932109ceToomas Soome# include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome# include <stdlib.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome# include <limits.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <fcntl.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef _WIN32
199767f8919635c4928607450d9e0abb932109ceToomas Soome# include <stddef.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32)
199767f8919635c4928607450d9e0abb932109ceToomas Soome# include <io.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef WINAPI_FAMILY
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define open _open
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define read _read
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define write _write
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define close _close
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NO_DEFLATE /* for compatibility with old definition */
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define NO_GZCOMPRESS
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
199767f8919635c4928607450d9e0abb932109ceToomas Soome# ifndef HAVE_VSNPRINTF
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define HAVE_VSNPRINTF
199767f8919635c4928607450d9e0abb932109ceToomas Soome# endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(__CYGWIN__)
199767f8919635c4928607450d9e0abb932109ceToomas Soome# ifndef HAVE_VSNPRINTF
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define HAVE_VSNPRINTF
199767f8919635c4928607450d9e0abb932109ceToomas Soome# endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410)
199767f8919635c4928607450d9e0abb932109ceToomas Soome# ifndef HAVE_VSNPRINTF
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define HAVE_VSNPRINTF
199767f8919635c4928607450d9e0abb932109ceToomas Soome# endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef HAVE_VSNPRINTF
199767f8919635c4928607450d9e0abb932109ceToomas Soome# ifdef MSDOS
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
199767f8919635c4928607450d9e0abb932109ceToomas Soome but for now we just assume it doesn't. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define NO_vsnprintf
199767f8919635c4928607450d9e0abb932109ceToomas Soome# endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome# ifdef __TURBOC__
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define NO_vsnprintf
199767f8919635c4928607450d9e0abb932109ceToomas Soome# endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome# ifdef WIN32
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome# if !defined(vsnprintf) && !defined(NO_vsnprintf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome# if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 )
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define vsnprintf _vsnprintf
199767f8919635c4928607450d9e0abb932109ceToomas Soome# endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome# endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome# endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome# ifdef __SASC
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define NO_vsnprintf
199767f8919635c4928607450d9e0abb932109ceToomas Soome# endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome# ifdef VMS
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define NO_vsnprintf
199767f8919635c4928607450d9e0abb932109ceToomas Soome# endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome# ifdef __OS400__
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define NO_vsnprintf
199767f8919635c4928607450d9e0abb932109ceToomas Soome# endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome# ifdef __MVS__
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define NO_vsnprintf
199767f8919635c4928607450d9e0abb932109ceToomas Soome# endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* unlike snprintf (which is required in C99, yet still not supported by
199767f8919635c4928607450d9e0abb932109ceToomas Soome Microsoft more than a decade later!), _snprintf does not guarantee null
199767f8919635c4928607450d9e0abb932109ceToomas Soome termination of the result -- however this is only used in gzlib.c where
199767f8919635c4928607450d9e0abb932109ceToomas Soome the result is assured to fit in the space provided */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef _MSC_VER
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define snprintf _snprintf
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef local
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define local static
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* compile with -Dlocal if your debugger can't find static symbols */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* gz* functions always use library allocation functions */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef STDC
199767f8919635c4928607450d9e0abb932109ceToomas Soome extern voidp malloc OF((uInt size));
199767f8919635c4928607450d9e0abb932109ceToomas Soome extern void free OF((voidpf ptr));
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* get errno and strerror definition */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined UNDER_CE
199767f8919635c4928607450d9e0abb932109ceToomas Soome# include <windows.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define zstrerror() gz_strwinerror((DWORD)GetLastError())
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome# ifndef NO_STRERROR
199767f8919635c4928607450d9e0abb932109ceToomas Soome# include <errno.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define zstrerror() strerror(errno)
199767f8919635c4928607450d9e0abb932109ceToomas Soome# else
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define zstrerror() "stdio error (consult errno)"
199767f8919635c4928607450d9e0abb932109ceToomas Soome# endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* provide prototypes for these when building zlib without LFS */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0
199767f8919635c4928607450d9e0abb932109ceToomas Soome ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
199767f8919635c4928607450d9e0abb932109ceToomas Soome ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
199767f8919635c4928607450d9e0abb932109ceToomas Soome ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
199767f8919635c4928607450d9e0abb932109ceToomas Soome ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* default memLevel */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if MAX_MEM_LEVEL >= 8
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define DEF_MEM_LEVEL 8
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define DEF_MEM_LEVEL MAX_MEM_LEVEL
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* default i/o buffer size -- double this for output when reading (this and
199767f8919635c4928607450d9e0abb932109ceToomas Soome twice this must be able to fit in an unsigned type) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define GZBUFSIZE 8192
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* gzip modes, also provide a little integrity check on the passed structure */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define GZ_NONE 0
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define GZ_READ 7247
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define GZ_WRITE 31153
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* values for gz_state how */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define LOOK 0 /* look for a gzip header */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define COPY 1 /* copy input directly */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define GZIP 2 /* decompress a gzip stream */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* internal gzip file state data structure */
199767f8919635c4928607450d9e0abb932109ceToomas Soometypedef struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* exposed contents for gzgetc() macro */
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct gzFile_s x; /* "x" for exposed */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* x.have: number of bytes available at x.next */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* x.next: next output data to deliver or write */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* x.pos: current position in uncompressed data */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* used for both reading and writing */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int mode; /* see gzip modes above */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int fd; /* file descriptor */
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *path; /* path or fd for error messages */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned size; /* buffer size, zero if not allocated yet */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned want; /* requested buffer size, default is GZBUFSIZE */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned char *in; /* input buffer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned char *out; /* output buffer (double-sized when reading) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int direct; /* 0 if processing gzip, 1 if transparent */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* just for reading */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int how; /* 0: get header, 1: copy, 2: decompress */
199767f8919635c4928607450d9e0abb932109ceToomas Soome z_off64_t start; /* where the gzip data started, for rewinding */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int eof; /* true if end of input file reached */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int past; /* true if read requested past end */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* just for writing */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int level; /* compression level */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int strategy; /* compression strategy */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* seek request */
199767f8919635c4928607450d9e0abb932109ceToomas Soome z_off64_t skip; /* amount to skip (already rewound if backwards) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int seek; /* true if seek request pending */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* error information */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err; /* error code */
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *msg; /* error message */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* zlib inflate or deflate stream */
199767f8919635c4928607450d9e0abb932109ceToomas Soome z_stream strm; /* stream structure in-place (not a pointer) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome} gz_state;
199767f8919635c4928607450d9e0abb932109ceToomas Soometypedef gz_state FAR *gz_statep;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* shared functions */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *));
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined UNDER_CE
199767f8919635c4928607450d9e0abb932109ceToomas Soomechar ZLIB_INTERNAL *gz_strwinerror OF((DWORD error));
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t
199767f8919635c4928607450d9e0abb932109ceToomas Soome value -- needed when comparing unsigned to z_off64_t, which is signed
199767f8919635c4928607450d9e0abb932109ceToomas Soome (possible z_off64_t types off_t, off64_t, and long are all signed) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef INT_MAX
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soomeunsigned ZLIB_INTERNAL gz_intmax OF((void));
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax())
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif