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