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