10c16b537SWarner Losh /* gzguts.h contains minimal changes required to be compiled with zlibWrapper: 20c16b537SWarner Losh * - #include "zlib.h" was changed to #include "zstd_zlibwrapper.h" 30c16b537SWarner Losh * - gz_statep was converted to union to work with -Wstrict-aliasing=1 */ 40c16b537SWarner Losh 50c16b537SWarner Losh /* gzguts.h -- zlib internal header definitions for gz* operations 60c16b537SWarner Losh * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013, 2016 Mark Adler 70c16b537SWarner Losh * For conditions of distribution and use, see http://www.zlib.net/zlib_license.html 80c16b537SWarner Losh */ 90c16b537SWarner Losh 100c16b537SWarner Losh #ifdef _LARGEFILE64_SOURCE 110c16b537SWarner Losh # ifndef _LARGEFILE_SOURCE 120c16b537SWarner Losh # define _LARGEFILE_SOURCE 1 130c16b537SWarner Losh # endif 140c16b537SWarner Losh # ifdef _FILE_OFFSET_BITS 150c16b537SWarner Losh # undef _FILE_OFFSET_BITS 160c16b537SWarner Losh # endif 170c16b537SWarner Losh #endif 180c16b537SWarner Losh 190c16b537SWarner Losh #ifdef HAVE_HIDDEN 200c16b537SWarner Losh # define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) 210c16b537SWarner Losh #else 220c16b537SWarner Losh # define ZLIB_INTERNAL 230c16b537SWarner Losh #endif 240c16b537SWarner Losh 250c16b537SWarner Losh #include <stdio.h> 260c16b537SWarner Losh #include "zstd_zlibwrapper.h" 270c16b537SWarner Losh #include "gzcompatibility.h" 280c16b537SWarner Losh #ifdef STDC 290c16b537SWarner Losh # include <string.h> 300c16b537SWarner Losh # include <stdlib.h> 310c16b537SWarner Losh # include <limits.h> 320c16b537SWarner Losh #endif 330c16b537SWarner Losh 340c16b537SWarner Losh #ifndef _POSIX_SOURCE 350c16b537SWarner Losh # define _POSIX_SOURCE 360c16b537SWarner Losh #endif 370c16b537SWarner Losh #include <fcntl.h> 380c16b537SWarner Losh 390c16b537SWarner Losh #ifdef _WIN32 400c16b537SWarner Losh # include <stddef.h> 41*2b9c00cbSConrad Meyer #else 42*2b9c00cbSConrad Meyer # include <unistd.h> 430c16b537SWarner Losh #endif 440c16b537SWarner Losh 450c16b537SWarner Losh #if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32) 460c16b537SWarner Losh # include <io.h> 470c16b537SWarner Losh #endif 480c16b537SWarner Losh 490f743729SConrad Meyer #if defined(_WIN32) 500c16b537SWarner Losh # define WIDECHAR 510c16b537SWarner Losh #endif 520c16b537SWarner Losh 530c16b537SWarner Losh #ifdef WINAPI_FAMILY 540c16b537SWarner Losh # define open _open 550c16b537SWarner Losh # define read _read 560c16b537SWarner Losh # define write _write 570c16b537SWarner Losh # define close _close 580c16b537SWarner Losh #endif 590c16b537SWarner Losh 600c16b537SWarner Losh #ifdef NO_DEFLATE /* for compatibility with old definition */ 610c16b537SWarner Losh # define NO_GZCOMPRESS 620c16b537SWarner Losh #endif 630c16b537SWarner Losh 640c16b537SWarner Losh #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550) 650c16b537SWarner Losh # ifndef HAVE_VSNPRINTF 660c16b537SWarner Losh # define HAVE_VSNPRINTF 670c16b537SWarner Losh # endif 680c16b537SWarner Losh #endif 690c16b537SWarner Losh 700c16b537SWarner Losh #if defined(__CYGWIN__) 710c16b537SWarner Losh # ifndef HAVE_VSNPRINTF 720c16b537SWarner Losh # define HAVE_VSNPRINTF 730c16b537SWarner Losh # endif 740c16b537SWarner Losh #endif 750c16b537SWarner Losh 760c16b537SWarner Losh #if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410) 770c16b537SWarner Losh # ifndef HAVE_VSNPRINTF 780c16b537SWarner Losh # define HAVE_VSNPRINTF 790c16b537SWarner Losh # endif 800c16b537SWarner Losh #endif 810c16b537SWarner Losh 820c16b537SWarner Losh #ifndef HAVE_VSNPRINTF 830c16b537SWarner Losh # ifdef MSDOS 840c16b537SWarner Losh /* vsnprintf may exist on some MS-DOS compilers (DJGPP?), 850c16b537SWarner Losh but for now we just assume it doesn't. */ 860c16b537SWarner Losh # define NO_vsnprintf 870c16b537SWarner Losh # endif 880c16b537SWarner Losh # ifdef __TURBOC__ 890c16b537SWarner Losh # define NO_vsnprintf 900c16b537SWarner Losh # endif 910c16b537SWarner Losh # ifdef WIN32 920c16b537SWarner Losh /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */ 930c16b537SWarner Losh # if !defined(vsnprintf) && !defined(NO_vsnprintf) 940c16b537SWarner Losh # if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 ) 950c16b537SWarner Losh # define vsnprintf _vsnprintf 960c16b537SWarner Losh # endif 970c16b537SWarner Losh # endif 980c16b537SWarner Losh # endif 990c16b537SWarner Losh # ifdef __SASC 1000c16b537SWarner Losh # define NO_vsnprintf 1010c16b537SWarner Losh # endif 1020c16b537SWarner Losh # ifdef VMS 1030c16b537SWarner Losh # define NO_vsnprintf 1040c16b537SWarner Losh # endif 1050c16b537SWarner Losh # ifdef __OS400__ 1060c16b537SWarner Losh # define NO_vsnprintf 1070c16b537SWarner Losh # endif 1080c16b537SWarner Losh # ifdef __MVS__ 1090c16b537SWarner Losh # define NO_vsnprintf 1100c16b537SWarner Losh # endif 1110c16b537SWarner Losh #endif 1120c16b537SWarner Losh 1130c16b537SWarner Losh /* unlike snprintf (which is required in C99), _snprintf does not guarantee 1140c16b537SWarner Losh null termination of the result -- however this is only used in gzlib.c where 1150c16b537SWarner Losh the result is assured to fit in the space provided */ 1160c16b537SWarner Losh #if defined(_MSC_VER) && _MSC_VER < 1900 1170c16b537SWarner Losh # define snprintf _snprintf 1180c16b537SWarner Losh #endif 1190c16b537SWarner Losh 1200c16b537SWarner Losh #ifndef local 1210c16b537SWarner Losh # define local static 1220c16b537SWarner Losh #endif 1230c16b537SWarner Losh /* since "static" is used to mean two completely different things in C, we 1240c16b537SWarner Losh define "local" for the non-static meaning of "static", for readability 1250c16b537SWarner Losh (compile with -Dlocal if your debugger can't find static symbols) */ 1260c16b537SWarner Losh 1270c16b537SWarner Losh /* gz* functions always use library allocation functions */ 1280c16b537SWarner Losh #ifndef STDC 1290c16b537SWarner Losh extern voidp malloc OF((uInt size)); 1300c16b537SWarner Losh extern void free OF((voidpf ptr)); 1310c16b537SWarner Losh #endif 1320c16b537SWarner Losh 1330c16b537SWarner Losh /* get errno and strerror definition */ 1340c16b537SWarner Losh #if defined UNDER_CE 1350c16b537SWarner Losh # include <windows.h> 1360c16b537SWarner Losh # define zstrerror() gz_strwinerror((DWORD)GetLastError()) 1370c16b537SWarner Losh #else 1380c16b537SWarner Losh # ifndef NO_STRERROR 1390c16b537SWarner Losh # include <errno.h> 1400c16b537SWarner Losh # define zstrerror() strerror(errno) 1410c16b537SWarner Losh # else 1420c16b537SWarner Losh # define zstrerror() "stdio error (consult errno)" 1430c16b537SWarner Losh # endif 1440c16b537SWarner Losh #endif 1450c16b537SWarner Losh 1460c16b537SWarner Losh /* provide prototypes for these when building zlib without LFS */ 1470c16b537SWarner Losh #if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0 1480c16b537SWarner Losh ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); 1490c16b537SWarner Losh ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int)); 1500c16b537SWarner Losh ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile)); 1510c16b537SWarner Losh ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile)); 1520c16b537SWarner Losh #endif 1530c16b537SWarner Losh 1540c16b537SWarner Losh /* default memLevel */ 1550c16b537SWarner Losh #if MAX_MEM_LEVEL >= 8 1560c16b537SWarner Losh # define DEF_MEM_LEVEL 8 1570c16b537SWarner Losh #else 1580c16b537SWarner Losh # define DEF_MEM_LEVEL MAX_MEM_LEVEL 1590c16b537SWarner Losh #endif 1600c16b537SWarner Losh 1610c16b537SWarner Losh /* default i/o buffer size -- double this for output when reading (this and 1620c16b537SWarner Losh twice this must be able to fit in an unsigned type) */ 1630c16b537SWarner Losh #define GZBUFSIZE 8192 1640c16b537SWarner Losh 1650c16b537SWarner Losh /* gzip modes, also provide a little integrity check on the passed structure */ 1660c16b537SWarner Losh #define GZ_NONE 0 1670c16b537SWarner Losh #define GZ_READ 7247 1680c16b537SWarner Losh #define GZ_WRITE 31153 1690c16b537SWarner Losh #define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */ 1700c16b537SWarner Losh 1710c16b537SWarner Losh /* values for gz_state how */ 1720c16b537SWarner Losh #define LOOK 0 /* look for a gzip header */ 1730c16b537SWarner Losh #define COPY 1 /* copy input directly */ 1740c16b537SWarner Losh #define GZIP 2 /* decompress a gzip stream */ 1750c16b537SWarner Losh 1760c16b537SWarner Losh /* internal gzip file state data structure */ 1770c16b537SWarner Losh typedef struct { 1780c16b537SWarner Losh /* exposed contents for gzgetc() macro */ 1790c16b537SWarner Losh struct gzFile_s x; /* "x" for exposed */ 1800c16b537SWarner Losh /* x.have: number of bytes available at x.next */ 1810c16b537SWarner Losh /* x.next: next output data to deliver or write */ 1820c16b537SWarner Losh /* x.pos: current position in uncompressed data */ 1830c16b537SWarner Losh /* used for both reading and writing */ 1840c16b537SWarner Losh int mode; /* see gzip modes above */ 1850c16b537SWarner Losh int fd; /* file descriptor */ 1860c16b537SWarner Losh char *path; /* path or fd for error messages */ 1870c16b537SWarner Losh unsigned size; /* buffer size, zero if not allocated yet */ 1880c16b537SWarner Losh unsigned want; /* requested buffer size, default is GZBUFSIZE */ 1890c16b537SWarner Losh unsigned char *in; /* input buffer (double-sized when writing) */ 1900c16b537SWarner Losh unsigned char *out; /* output buffer (double-sized when reading) */ 1910c16b537SWarner Losh int direct; /* 0 if processing gzip, 1 if transparent */ 1920c16b537SWarner Losh /* just for reading */ 1930c16b537SWarner Losh int how; /* 0: get header, 1: copy, 2: decompress */ 1940c16b537SWarner Losh z_off64_t start; /* where the gzip data started, for rewinding */ 1950c16b537SWarner Losh int eof; /* true if end of input file reached */ 1960c16b537SWarner Losh int past; /* true if read requested past end */ 1970c16b537SWarner Losh /* just for writing */ 1980c16b537SWarner Losh int level; /* compression level */ 1990c16b537SWarner Losh int strategy; /* compression strategy */ 2000c16b537SWarner Losh /* seek request */ 2010c16b537SWarner Losh z_off64_t skip; /* amount to skip (already rewound if backwards) */ 2020c16b537SWarner Losh int seek; /* true if seek request pending */ 2030c16b537SWarner Losh /* error information */ 2040c16b537SWarner Losh int err; /* error code */ 2050c16b537SWarner Losh char *msg; /* error message */ 2060c16b537SWarner Losh /* zlib inflate or deflate stream */ 2070c16b537SWarner Losh z_stream strm; /* stream structure in-place (not a pointer) */ 2080c16b537SWarner Losh } gz_state; 2090c16b537SWarner Losh 2100c16b537SWarner Losh typedef union { 2110c16b537SWarner Losh gz_state FAR *state; 2120c16b537SWarner Losh gzFile file; 2130c16b537SWarner Losh } gz_statep; 2140c16b537SWarner Losh 2150c16b537SWarner Losh /* shared functions */ 2160c16b537SWarner Losh void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *)); 2170c16b537SWarner Losh #if defined UNDER_CE 2180c16b537SWarner Losh char ZLIB_INTERNAL *gz_strwinerror OF((DWORD error)); 2190c16b537SWarner Losh #endif 2200c16b537SWarner Losh 2210c16b537SWarner Losh /* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t 2220c16b537SWarner Losh value -- needed when comparing unsigned to z_off64_t, which is signed 2230c16b537SWarner Losh (possible z_off64_t types off_t, off64_t, and long are all signed) */ 2240c16b537SWarner Losh #ifdef INT_MAX 2250c16b537SWarner Losh # define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX) 2260c16b537SWarner Losh #else 2270c16b537SWarner Losh unsigned ZLIB_INTERNAL gz_intmax OF((void)); 2280c16b537SWarner Losh # define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax()) 2290c16b537SWarner Losh #endif 230