File indexing completed on 2024-04-21 03:48:30

0001 /* gzguts.h -- zlib internal header definitions for gz* operations
0002  * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler
0003  * For conditions of distribution and use, see copyright notice in zlib.h
0004  */
0005 
0006 #ifdef _LARGEFILE64_SOURCE
0007 #  ifndef _LARGEFILE_SOURCE
0008 #    define _LARGEFILE_SOURCE 1
0009 #  endif
0010 #  ifdef _FILE_OFFSET_BITS
0011 #    undef _FILE_OFFSET_BITS
0012 #  endif
0013 #endif
0014 
0015 #ifdef HAVE_HIDDEN
0016 #  define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
0017 #else
0018 #  define ZLIB_INTERNAL
0019 #endif
0020 
0021 #include <stdio.h>
0022 #include "zlib.h"
0023 #ifdef STDC
0024 #  include <string.h>
0025 #  include <stdlib.h>
0026 #  include <limits.h>
0027 #endif
0028 #include <fcntl.h>
0029 
0030 #ifdef _WIN32
0031 #  include <stddef.h>
0032 #endif
0033 
0034 #if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32)
0035 #  include <io.h>
0036 #endif
0037 
0038 #ifdef WINAPI_FAMILY
0039 #  define open _open
0040 #  define read _read
0041 #  define write _write
0042 #  define close _close
0043 #endif
0044 
0045 #ifdef NO_DEFLATE       /* for compatibility with old definition */
0046 #  define NO_GZCOMPRESS
0047 #endif
0048 
0049 #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
0050 #  ifndef HAVE_VSNPRINTF
0051 #    define HAVE_VSNPRINTF
0052 #  endif
0053 #endif
0054 
0055 #if defined(__CYGWIN__)
0056 #  ifndef HAVE_VSNPRINTF
0057 #    define HAVE_VSNPRINTF
0058 #  endif
0059 #endif
0060 
0061 #if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410)
0062 #  ifndef HAVE_VSNPRINTF
0063 #    define HAVE_VSNPRINTF
0064 #  endif
0065 #endif
0066 
0067 #ifndef HAVE_VSNPRINTF
0068 #  ifdef MSDOS
0069 /* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
0070    but for now we just assume it doesn't. */
0071 #    define NO_vsnprintf
0072 #  endif
0073 #  ifdef __TURBOC__
0074 #    define NO_vsnprintf
0075 #  endif
0076 #  ifdef WIN32
0077 /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
0078 #    if !defined(vsnprintf) && !defined(NO_vsnprintf)
0079 #      if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 )
0080 #         define vsnprintf _vsnprintf
0081 #      endif
0082 #    endif
0083 #  endif
0084 #  ifdef __SASC
0085 #    define NO_vsnprintf
0086 #  endif
0087 #  ifdef VMS
0088 #    define NO_vsnprintf
0089 #  endif
0090 #  ifdef __OS400__
0091 #    define NO_vsnprintf
0092 #  endif
0093 #  ifdef __MVS__
0094 #    define NO_vsnprintf
0095 #  endif
0096 #endif
0097 
0098 /* unlike snprintf (which is required in C99, yet still not supported by
0099    Microsoft more than a decade later!), _snprintf does not guarantee null
0100    termination of the result -- however this is only used in gzlib.c where
0101    the result is assured to fit in the space provided */
0102 #ifdef _MSC_VER
0103 #  define snprintf _snprintf
0104 #endif
0105 
0106 #ifndef local
0107 #  define local static
0108 #endif
0109 /* compile with -Dlocal if your debugger can't find static symbols */
0110 
0111 /* gz* functions always use library allocation functions */
0112 #ifndef STDC
0113   extern voidp  malloc OF((uInt size));
0114   extern void   free   OF((voidpf ptr));
0115 #endif
0116 
0117 /* get errno and strerror definition */
0118 #if defined UNDER_CE
0119 #  include <windows.h>
0120 #  define zstrerror() gz_strwinerror((DWORD)GetLastError())
0121 #else
0122 #  ifndef NO_STRERROR
0123 #    include <errno.h>
0124 #    define zstrerror() strerror(errno)
0125 #  else
0126 #    define zstrerror() "stdio error (consult errno)"
0127 #  endif
0128 #endif
0129 
0130 /* provide prototypes for these when building zlib without LFS */
0131 #if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0
0132     ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
0133     ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
0134     ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
0135     ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
0136 #endif
0137 
0138 /* default memLevel */
0139 #if MAX_MEM_LEVEL >= 8
0140 #  define DEF_MEM_LEVEL 8
0141 #else
0142 #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
0143 #endif
0144 
0145 /* default i/o buffer size -- double this for output when reading (this and
0146    twice this must be able to fit in an unsigned type) */
0147 #define GZBUFSIZE 8192
0148 
0149 /* gzip modes, also provide a little integrity check on the passed structure */
0150 #define GZ_NONE 0
0151 #define GZ_READ 7247
0152 #define GZ_WRITE 31153
0153 #define GZ_APPEND 1     /* mode set to GZ_WRITE after the file is opened */
0154 
0155 /* values for gz_state how */
0156 #define LOOK 0      /* look for a gzip header */
0157 #define COPY 1      /* copy input directly */
0158 #define GZIP 2      /* decompress a gzip stream */
0159 
0160 /* internal gzip file state data structure */
0161 typedef struct {
0162         /* exposed contents for gzgetc() macro */
0163     struct gzFile_s x;      /* "x" for exposed */
0164                             /* x.have: number of bytes available at x.next */
0165                             /* x.next: next output data to deliver or write */
0166                             /* x.pos: current position in uncompressed data */
0167         /* used for both reading and writing */
0168     int mode;               /* see gzip modes above */
0169     int fd;                 /* file descriptor */
0170     char *path;             /* path or fd for error messages */
0171     unsigned size;          /* buffer size, zero if not allocated yet */
0172     unsigned want;          /* requested buffer size, default is GZBUFSIZE */
0173     unsigned char *in;      /* input buffer */
0174     unsigned char *out;     /* output buffer (double-sized when reading) */
0175     int direct;             /* 0 if processing gzip, 1 if transparent */
0176         /* just for reading */
0177     int how;                /* 0: get header, 1: copy, 2: decompress */
0178     z_off64_t start;        /* where the gzip data started, for rewinding */
0179     int eof;                /* true if end of input file reached */
0180     int past;               /* true if read requested past end */
0181         /* just for writing */
0182     int level;              /* compression level */
0183     int strategy;           /* compression strategy */
0184         /* seek request */
0185     z_off64_t skip;         /* amount to skip (already rewound if backwards) */
0186     int seek;               /* true if seek request pending */
0187         /* error information */
0188     int err;                /* error code */
0189     char *msg;              /* error message */
0190         /* zlib inflate or deflate stream */
0191     z_stream strm;          /* stream structure in-place (not a pointer) */
0192 } gz_state;
0193 typedef gz_state FAR *gz_statep;
0194 
0195 /* shared functions */
0196 void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *));
0197 #if defined UNDER_CE
0198 char ZLIB_INTERNAL *gz_strwinerror OF((DWORD error));
0199 #endif
0200 
0201 /* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t
0202    value -- needed when comparing unsigned to z_off64_t, which is signed
0203    (possible z_off64_t types off_t, off64_t, and long are all signed) */
0204 #ifdef INT_MAX
0205 #  define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX)
0206 #else
0207 unsigned ZLIB_INTERNAL gz_intmax OF((void));
0208 #  define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax())
0209 #endif