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