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