1*ab9e68a2SToomas Soome /* gzguts.h -- zlib internal header definitions for gz* operations 2*ab9e68a2SToomas Soome * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013, 2016 Mark Adler 3*ab9e68a2SToomas Soome * For conditions of distribution and use, see copyright notice in zlib.h 4*ab9e68a2SToomas Soome */ 5*ab9e68a2SToomas Soome 6*ab9e68a2SToomas Soome #ifdef _LARGEFILE64_SOURCE 7*ab9e68a2SToomas Soome # ifndef _LARGEFILE_SOURCE 8*ab9e68a2SToomas Soome # define _LARGEFILE_SOURCE 1 9*ab9e68a2SToomas Soome # endif 10*ab9e68a2SToomas Soome # ifdef _FILE_OFFSET_BITS 11*ab9e68a2SToomas Soome # undef _FILE_OFFSET_BITS 12*ab9e68a2SToomas Soome # endif 13*ab9e68a2SToomas Soome #endif 14*ab9e68a2SToomas Soome 15*ab9e68a2SToomas Soome #ifdef HAVE_HIDDEN 16*ab9e68a2SToomas Soome # define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) 17*ab9e68a2SToomas Soome #else 18*ab9e68a2SToomas Soome # define ZLIB_INTERNAL 19*ab9e68a2SToomas Soome #endif 20*ab9e68a2SToomas Soome 21*ab9e68a2SToomas Soome #include <stdio.h> 22*ab9e68a2SToomas Soome #include "zlib.h" 23*ab9e68a2SToomas Soome #ifdef STDC 24*ab9e68a2SToomas Soome # include <string.h> 25*ab9e68a2SToomas Soome # include <stdlib.h> 26*ab9e68a2SToomas Soome # include <limits.h> 27*ab9e68a2SToomas Soome #endif 28*ab9e68a2SToomas Soome 29*ab9e68a2SToomas Soome #ifndef _POSIX_SOURCE 30*ab9e68a2SToomas Soome # define _POSIX_SOURCE 31*ab9e68a2SToomas Soome #endif 32*ab9e68a2SToomas Soome #include <fcntl.h> 33*ab9e68a2SToomas Soome 34*ab9e68a2SToomas Soome #ifdef _WIN32 35*ab9e68a2SToomas Soome # include <stddef.h> 36*ab9e68a2SToomas Soome #endif 37*ab9e68a2SToomas Soome 38*ab9e68a2SToomas Soome #if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32) 39*ab9e68a2SToomas Soome # include <io.h> 40*ab9e68a2SToomas Soome #endif 41*ab9e68a2SToomas Soome 42*ab9e68a2SToomas Soome #if defined(_WIN32) || defined(__CYGWIN__) 43*ab9e68a2SToomas Soome # define WIDECHAR 44*ab9e68a2SToomas Soome #endif 45*ab9e68a2SToomas Soome 46*ab9e68a2SToomas Soome #ifdef WINAPI_FAMILY 47*ab9e68a2SToomas Soome # define open _open 48*ab9e68a2SToomas Soome # define read _read 49*ab9e68a2SToomas Soome # define write _write 50*ab9e68a2SToomas Soome # define close _close 51*ab9e68a2SToomas Soome #endif 52*ab9e68a2SToomas Soome 53*ab9e68a2SToomas Soome #ifdef NO_DEFLATE /* for compatibility with old definition */ 54*ab9e68a2SToomas Soome # define NO_GZCOMPRESS 55*ab9e68a2SToomas Soome #endif 56*ab9e68a2SToomas Soome 57*ab9e68a2SToomas Soome #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550) 58*ab9e68a2SToomas Soome # ifndef HAVE_VSNPRINTF 59*ab9e68a2SToomas Soome # define HAVE_VSNPRINTF 60*ab9e68a2SToomas Soome # endif 61*ab9e68a2SToomas Soome #endif 62*ab9e68a2SToomas Soome 63*ab9e68a2SToomas Soome #if defined(__CYGWIN__) 64*ab9e68a2SToomas Soome # ifndef HAVE_VSNPRINTF 65*ab9e68a2SToomas Soome # define HAVE_VSNPRINTF 66*ab9e68a2SToomas Soome # endif 67*ab9e68a2SToomas Soome #endif 68*ab9e68a2SToomas Soome 69*ab9e68a2SToomas Soome #if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410) 70*ab9e68a2SToomas Soome # ifndef HAVE_VSNPRINTF 71*ab9e68a2SToomas Soome # define HAVE_VSNPRINTF 72*ab9e68a2SToomas Soome # endif 73*ab9e68a2SToomas Soome #endif 74*ab9e68a2SToomas Soome 75*ab9e68a2SToomas Soome #ifndef HAVE_VSNPRINTF 76*ab9e68a2SToomas Soome # ifdef MSDOS 77*ab9e68a2SToomas Soome /* vsnprintf may exist on some MS-DOS compilers (DJGPP?), 78*ab9e68a2SToomas Soome but for now we just assume it doesn't. */ 79*ab9e68a2SToomas Soome # define NO_vsnprintf 80*ab9e68a2SToomas Soome # endif 81*ab9e68a2SToomas Soome # ifdef __TURBOC__ 82*ab9e68a2SToomas Soome # define NO_vsnprintf 83*ab9e68a2SToomas Soome # endif 84*ab9e68a2SToomas Soome # ifdef WIN32 85*ab9e68a2SToomas Soome /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */ 86*ab9e68a2SToomas Soome # if !defined(vsnprintf) && !defined(NO_vsnprintf) 87*ab9e68a2SToomas Soome # if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 ) 88*ab9e68a2SToomas Soome # define vsnprintf _vsnprintf 89*ab9e68a2SToomas Soome # endif 90*ab9e68a2SToomas Soome # endif 91*ab9e68a2SToomas Soome # endif 92*ab9e68a2SToomas Soome # ifdef __SASC 93*ab9e68a2SToomas Soome # define NO_vsnprintf 94*ab9e68a2SToomas Soome # endif 95*ab9e68a2SToomas Soome # ifdef VMS 96*ab9e68a2SToomas Soome # define NO_vsnprintf 97*ab9e68a2SToomas Soome # endif 98*ab9e68a2SToomas Soome # ifdef __OS400__ 99*ab9e68a2SToomas Soome # define NO_vsnprintf 100*ab9e68a2SToomas Soome # endif 101*ab9e68a2SToomas Soome # ifdef __MVS__ 102*ab9e68a2SToomas Soome # define NO_vsnprintf 103*ab9e68a2SToomas Soome # endif 104*ab9e68a2SToomas Soome #endif 105*ab9e68a2SToomas Soome 106*ab9e68a2SToomas Soome /* unlike snprintf (which is required in C99), _snprintf does not guarantee 107*ab9e68a2SToomas Soome null termination of the result -- however this is only used in gzlib.c where 108*ab9e68a2SToomas Soome the result is assured to fit in the space provided */ 109*ab9e68a2SToomas Soome #if defined(_MSC_VER) && _MSC_VER < 1900 110*ab9e68a2SToomas Soome # define snprintf _snprintf 111*ab9e68a2SToomas Soome #endif 112*ab9e68a2SToomas Soome 113*ab9e68a2SToomas Soome #ifndef local 114*ab9e68a2SToomas Soome # define local static 115*ab9e68a2SToomas Soome #endif 116*ab9e68a2SToomas Soome /* since "static" is used to mean two completely different things in C, we 117*ab9e68a2SToomas Soome define "local" for the non-static meaning of "static", for readability 118*ab9e68a2SToomas Soome (compile with -Dlocal if your debugger can't find static symbols) */ 119*ab9e68a2SToomas Soome 120*ab9e68a2SToomas Soome /* gz* functions always use library allocation functions */ 121*ab9e68a2SToomas Soome #ifndef STDC 122*ab9e68a2SToomas Soome extern voidp malloc OF((uInt size)); 123*ab9e68a2SToomas Soome extern void free OF((voidpf ptr)); 124*ab9e68a2SToomas Soome #endif 125*ab9e68a2SToomas Soome 126*ab9e68a2SToomas Soome /* get errno and strerror definition */ 127*ab9e68a2SToomas Soome #if defined UNDER_CE 128*ab9e68a2SToomas Soome # include <windows.h> 129*ab9e68a2SToomas Soome # define zstrerror() gz_strwinerror((DWORD)GetLastError()) 130*ab9e68a2SToomas Soome #else 131*ab9e68a2SToomas Soome # ifndef NO_STRERROR 132*ab9e68a2SToomas Soome # include <errno.h> 133*ab9e68a2SToomas Soome # define zstrerror() strerror(errno) 134*ab9e68a2SToomas Soome # else 135*ab9e68a2SToomas Soome # define zstrerror() "stdio error (consult errno)" 136*ab9e68a2SToomas Soome # endif 137*ab9e68a2SToomas Soome #endif 138*ab9e68a2SToomas Soome 139*ab9e68a2SToomas Soome /* provide prototypes for these when building zlib without LFS */ 140*ab9e68a2SToomas Soome #if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0 141*ab9e68a2SToomas Soome ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); 142*ab9e68a2SToomas Soome ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int)); 143*ab9e68a2SToomas Soome ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile)); 144*ab9e68a2SToomas Soome ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile)); 145*ab9e68a2SToomas Soome #endif 146*ab9e68a2SToomas Soome 147*ab9e68a2SToomas Soome /* default memLevel */ 148*ab9e68a2SToomas Soome #if MAX_MEM_LEVEL >= 8 149*ab9e68a2SToomas Soome # define DEF_MEM_LEVEL 8 150*ab9e68a2SToomas Soome #else 151*ab9e68a2SToomas Soome # define DEF_MEM_LEVEL MAX_MEM_LEVEL 152*ab9e68a2SToomas Soome #endif 153*ab9e68a2SToomas Soome 154*ab9e68a2SToomas Soome /* default i/o buffer size -- double this for output when reading (this and 155*ab9e68a2SToomas Soome twice this must be able to fit in an unsigned type) */ 156*ab9e68a2SToomas Soome #define GZBUFSIZE 8192 157*ab9e68a2SToomas Soome 158*ab9e68a2SToomas Soome /* gzip modes, also provide a little integrity check on the passed structure */ 159*ab9e68a2SToomas Soome #define GZ_NONE 0 160*ab9e68a2SToomas Soome #define GZ_READ 7247 161*ab9e68a2SToomas Soome #define GZ_WRITE 31153 162*ab9e68a2SToomas Soome #define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */ 163*ab9e68a2SToomas Soome 164*ab9e68a2SToomas Soome /* values for gz_state how */ 165*ab9e68a2SToomas Soome #define LOOK 0 /* look for a gzip header */ 166*ab9e68a2SToomas Soome #define COPY 1 /* copy input directly */ 167*ab9e68a2SToomas Soome #define GZIP 2 /* decompress a gzip stream */ 168*ab9e68a2SToomas Soome 169*ab9e68a2SToomas Soome /* internal gzip file state data structure */ 170*ab9e68a2SToomas Soome typedef struct { 171*ab9e68a2SToomas Soome /* exposed contents for gzgetc() macro */ 172*ab9e68a2SToomas Soome struct gzFile_s x; /* "x" for exposed */ 173*ab9e68a2SToomas Soome /* x.have: number of bytes available at x.next */ 174*ab9e68a2SToomas Soome /* x.next: next output data to deliver or write */ 175*ab9e68a2SToomas Soome /* x.pos: current position in uncompressed data */ 176*ab9e68a2SToomas Soome /* used for both reading and writing */ 177*ab9e68a2SToomas Soome int mode; /* see gzip modes above */ 178*ab9e68a2SToomas Soome int fd; /* file descriptor */ 179*ab9e68a2SToomas Soome char *path; /* path or fd for error messages */ 180*ab9e68a2SToomas Soome unsigned size; /* buffer size, zero if not allocated yet */ 181*ab9e68a2SToomas Soome unsigned want; /* requested buffer size, default is GZBUFSIZE */ 182*ab9e68a2SToomas Soome unsigned char *in; /* input buffer (double-sized when writing) */ 183*ab9e68a2SToomas Soome unsigned char *out; /* output buffer (double-sized when reading) */ 184*ab9e68a2SToomas Soome int direct; /* 0 if processing gzip, 1 if transparent */ 185*ab9e68a2SToomas Soome /* just for reading */ 186*ab9e68a2SToomas Soome int how; /* 0: get header, 1: copy, 2: decompress */ 187*ab9e68a2SToomas Soome z_off64_t start; /* where the gzip data started, for rewinding */ 188*ab9e68a2SToomas Soome int eof; /* true if end of input file reached */ 189*ab9e68a2SToomas Soome int past; /* true if read requested past end */ 190*ab9e68a2SToomas Soome /* just for writing */ 191*ab9e68a2SToomas Soome int level; /* compression level */ 192*ab9e68a2SToomas Soome int strategy; /* compression strategy */ 193*ab9e68a2SToomas Soome /* seek request */ 194*ab9e68a2SToomas Soome z_off64_t skip; /* amount to skip (already rewound if backwards) */ 195*ab9e68a2SToomas Soome int seek; /* true if seek request pending */ 196*ab9e68a2SToomas Soome /* error information */ 197*ab9e68a2SToomas Soome int err; /* error code */ 198*ab9e68a2SToomas Soome char *msg; /* error message */ 199*ab9e68a2SToomas Soome /* zlib inflate or deflate stream */ 200*ab9e68a2SToomas Soome z_stream strm; /* stream structure in-place (not a pointer) */ 201*ab9e68a2SToomas Soome } gz_state; 202*ab9e68a2SToomas Soome typedef gz_state FAR *gz_statep; 203*ab9e68a2SToomas Soome 204*ab9e68a2SToomas Soome /* shared functions */ 205*ab9e68a2SToomas Soome void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *)); 206*ab9e68a2SToomas Soome #if defined UNDER_CE 207*ab9e68a2SToomas Soome char ZLIB_INTERNAL *gz_strwinerror OF((DWORD error)); 208*ab9e68a2SToomas Soome #endif 209*ab9e68a2SToomas Soome 210*ab9e68a2SToomas Soome /* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t 211*ab9e68a2SToomas Soome value -- needed when comparing unsigned to z_off64_t, which is signed 212*ab9e68a2SToomas Soome (possible z_off64_t types off_t, off64_t, and long are all signed) */ 213*ab9e68a2SToomas Soome #ifdef INT_MAX 214*ab9e68a2SToomas Soome # define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX) 215*ab9e68a2SToomas Soome #else 216*ab9e68a2SToomas Soome unsigned ZLIB_INTERNAL gz_intmax OF((void)); 217*ab9e68a2SToomas Soome # define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax()) 218*ab9e68a2SToomas Soome #endif 219