1fe927c51SPeter Avalos /* gzguts.h -- zlib internal header definitions for gz* operations 2*53ddf67cSJohn Marino * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler 3fe927c51SPeter Avalos * For conditions of distribution and use, see copyright notice in zlib.h 4fe927c51SPeter Avalos */ 5fe927c51SPeter Avalos 6fe927c51SPeter Avalos #ifdef _LARGEFILE64_SOURCE 7fe927c51SPeter Avalos # ifndef _LARGEFILE_SOURCE 8fe927c51SPeter Avalos # define _LARGEFILE_SOURCE 1 9fe927c51SPeter Avalos # endif 10fe927c51SPeter Avalos # ifdef _FILE_OFFSET_BITS 11fe927c51SPeter Avalos # undef _FILE_OFFSET_BITS 12fe927c51SPeter Avalos # endif 13fe927c51SPeter Avalos #endif 14fe927c51SPeter Avalos 15712f98b7SJohn Marino #ifdef HAVE_HIDDEN 16fe927c51SPeter Avalos # define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) 17fe927c51SPeter Avalos #else 18fe927c51SPeter Avalos # define ZLIB_INTERNAL 19fe927c51SPeter Avalos #endif 20fe927c51SPeter Avalos 21fe927c51SPeter Avalos #include <stdio.h> 22fe927c51SPeter Avalos #include "zlib.h" 23fe927c51SPeter Avalos #ifdef STDC 24fe927c51SPeter Avalos # include <string.h> 25fe927c51SPeter Avalos # include <stdlib.h> 26fe927c51SPeter Avalos # include <limits.h> 27fe927c51SPeter Avalos #endif 28fe927c51SPeter Avalos #include <fcntl.h> 29fe927c51SPeter Avalos 30712f98b7SJohn Marino #ifdef _WIN32 31712f98b7SJohn Marino # include <stddef.h> 32712f98b7SJohn Marino #endif 33712f98b7SJohn Marino 34712f98b7SJohn Marino #if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32) 35712f98b7SJohn Marino # include <io.h> 36712f98b7SJohn Marino #endif 37712f98b7SJohn Marino 38*53ddf67cSJohn Marino #ifdef WINAPI_FAMILY 39*53ddf67cSJohn Marino # define open _open 40*53ddf67cSJohn Marino # define read _read 41*53ddf67cSJohn Marino # define write _write 42*53ddf67cSJohn Marino # define close _close 43*53ddf67cSJohn Marino #endif 44*53ddf67cSJohn Marino 45fe927c51SPeter Avalos #ifdef NO_DEFLATE /* for compatibility with old definition */ 46fe927c51SPeter Avalos # define NO_GZCOMPRESS 47fe927c51SPeter Avalos #endif 48fe927c51SPeter Avalos 49712f98b7SJohn Marino #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550) 50712f98b7SJohn Marino # ifndef HAVE_VSNPRINTF 51712f98b7SJohn Marino # define HAVE_VSNPRINTF 52712f98b7SJohn Marino # endif 53712f98b7SJohn Marino #endif 54712f98b7SJohn Marino 55712f98b7SJohn Marino #if defined(__CYGWIN__) 56712f98b7SJohn Marino # ifndef HAVE_VSNPRINTF 57712f98b7SJohn Marino # define HAVE_VSNPRINTF 58712f98b7SJohn Marino # endif 59712f98b7SJohn Marino #endif 60712f98b7SJohn Marino 61712f98b7SJohn Marino #if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410) 62712f98b7SJohn Marino # ifndef HAVE_VSNPRINTF 63712f98b7SJohn Marino # define HAVE_VSNPRINTF 64712f98b7SJohn Marino # endif 65712f98b7SJohn Marino #endif 66712f98b7SJohn Marino 67712f98b7SJohn Marino #ifndef HAVE_VSNPRINTF 68712f98b7SJohn Marino # ifdef MSDOS 69712f98b7SJohn Marino /* vsnprintf may exist on some MS-DOS compilers (DJGPP?), 70712f98b7SJohn Marino but for now we just assume it doesn't. */ 71712f98b7SJohn Marino # define NO_vsnprintf 72712f98b7SJohn Marino # endif 73712f98b7SJohn Marino # ifdef __TURBOC__ 74712f98b7SJohn Marino # define NO_vsnprintf 75712f98b7SJohn Marino # endif 76712f98b7SJohn Marino # ifdef WIN32 77712f98b7SJohn Marino /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */ 78712f98b7SJohn Marino # if !defined(vsnprintf) && !defined(NO_vsnprintf) 79712f98b7SJohn Marino # if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 ) 80fe927c51SPeter Avalos # define vsnprintf _vsnprintf 81fe927c51SPeter Avalos # endif 82712f98b7SJohn Marino # endif 83712f98b7SJohn Marino # endif 84712f98b7SJohn Marino # ifdef __SASC 85712f98b7SJohn Marino # define NO_vsnprintf 86712f98b7SJohn Marino # endif 87712f98b7SJohn Marino # ifdef VMS 88712f98b7SJohn Marino # define NO_vsnprintf 89712f98b7SJohn Marino # endif 90712f98b7SJohn Marino # ifdef __OS400__ 91712f98b7SJohn Marino # define NO_vsnprintf 92712f98b7SJohn Marino # endif 93712f98b7SJohn Marino # ifdef __MVS__ 94712f98b7SJohn Marino # define NO_vsnprintf 95712f98b7SJohn Marino # endif 96712f98b7SJohn Marino #endif 97fe927c51SPeter Avalos 98*53ddf67cSJohn Marino /* unlike snprintf (which is required in C99, yet still not supported by 99*53ddf67cSJohn Marino Microsoft more than a decade later!), _snprintf does not guarantee null 100*53ddf67cSJohn Marino termination of the result -- however this is only used in gzlib.c where 101*53ddf67cSJohn Marino the result is assured to fit in the space provided */ 102*53ddf67cSJohn Marino #ifdef _MSC_VER 103*53ddf67cSJohn Marino # define snprintf _snprintf 104*53ddf67cSJohn Marino #endif 105*53ddf67cSJohn Marino 106fe927c51SPeter Avalos #ifndef local 107fe927c51SPeter Avalos # define local static 108fe927c51SPeter Avalos #endif 109fe927c51SPeter Avalos /* compile with -Dlocal if your debugger can't find static symbols */ 110fe927c51SPeter Avalos 111fe927c51SPeter Avalos /* gz* functions always use library allocation functions */ 112fe927c51SPeter Avalos #ifndef STDC 113fe927c51SPeter Avalos extern voidp malloc OF((uInt size)); 114fe927c51SPeter Avalos extern void free OF((voidpf ptr)); 115fe927c51SPeter Avalos #endif 116fe927c51SPeter Avalos 117fe927c51SPeter Avalos /* get errno and strerror definition */ 118fe927c51SPeter Avalos #if defined UNDER_CE 119fe927c51SPeter Avalos # include <windows.h> 120fe927c51SPeter Avalos # define zstrerror() gz_strwinerror((DWORD)GetLastError()) 121fe927c51SPeter Avalos #else 122712f98b7SJohn Marino # ifndef NO_STRERROR 123fe927c51SPeter Avalos # include <errno.h> 124fe927c51SPeter Avalos # define zstrerror() strerror(errno) 125fe927c51SPeter Avalos # else 126fe927c51SPeter Avalos # define zstrerror() "stdio error (consult errno)" 127fe927c51SPeter Avalos # endif 128fe927c51SPeter Avalos #endif 129fe927c51SPeter Avalos 130fe927c51SPeter Avalos /* provide prototypes for these when building zlib without LFS */ 131fe927c51SPeter Avalos #if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0 132fe927c51SPeter Avalos ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); 133fe927c51SPeter Avalos ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int)); 134fe927c51SPeter Avalos ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile)); 135fe927c51SPeter Avalos ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile)); 136fe927c51SPeter Avalos #endif 137fe927c51SPeter Avalos 138712f98b7SJohn Marino /* default memLevel */ 139712f98b7SJohn Marino #if MAX_MEM_LEVEL >= 8 140712f98b7SJohn Marino # define DEF_MEM_LEVEL 8 141712f98b7SJohn Marino #else 142712f98b7SJohn Marino # define DEF_MEM_LEVEL MAX_MEM_LEVEL 143712f98b7SJohn Marino #endif 144712f98b7SJohn Marino 145*53ddf67cSJohn Marino /* default i/o buffer size -- double this for output when reading (this and 146*53ddf67cSJohn Marino twice this must be able to fit in an unsigned type) */ 147fe927c51SPeter Avalos #define GZBUFSIZE 8192 148fe927c51SPeter Avalos 149fe927c51SPeter Avalos /* gzip modes, also provide a little integrity check on the passed structure */ 150fe927c51SPeter Avalos #define GZ_NONE 0 151fe927c51SPeter Avalos #define GZ_READ 7247 152fe927c51SPeter Avalos #define GZ_WRITE 31153 153fe927c51SPeter Avalos #define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */ 154fe927c51SPeter Avalos 155fe927c51SPeter Avalos /* values for gz_state how */ 156fe927c51SPeter Avalos #define LOOK 0 /* look for a gzip header */ 157fe927c51SPeter Avalos #define COPY 1 /* copy input directly */ 158fe927c51SPeter Avalos #define GZIP 2 /* decompress a gzip stream */ 159fe927c51SPeter Avalos 160fe927c51SPeter Avalos /* internal gzip file state data structure */ 161fe927c51SPeter Avalos typedef struct { 162712f98b7SJohn Marino /* exposed contents for gzgetc() macro */ 163712f98b7SJohn Marino struct gzFile_s x; /* "x" for exposed */ 164712f98b7SJohn Marino /* x.have: number of bytes available at x.next */ 165712f98b7SJohn Marino /* x.next: next output data to deliver or write */ 166712f98b7SJohn Marino /* x.pos: current position in uncompressed data */ 167fe927c51SPeter Avalos /* used for both reading and writing */ 168fe927c51SPeter Avalos int mode; /* see gzip modes above */ 169fe927c51SPeter Avalos int fd; /* file descriptor */ 170fe927c51SPeter Avalos char *path; /* path or fd for error messages */ 171fe927c51SPeter Avalos unsigned size; /* buffer size, zero if not allocated yet */ 172fe927c51SPeter Avalos unsigned want; /* requested buffer size, default is GZBUFSIZE */ 173fe927c51SPeter Avalos unsigned char *in; /* input buffer */ 174fe927c51SPeter Avalos unsigned char *out; /* output buffer (double-sized when reading) */ 175712f98b7SJohn Marino int direct; /* 0 if processing gzip, 1 if transparent */ 176fe927c51SPeter Avalos /* just for reading */ 177fe927c51SPeter Avalos int how; /* 0: get header, 1: copy, 2: decompress */ 178712f98b7SJohn Marino z_off64_t start; /* where the gzip data started, for rewinding */ 179712f98b7SJohn Marino int eof; /* true if end of input file reached */ 180712f98b7SJohn Marino int past; /* true if read requested past end */ 181fe927c51SPeter Avalos /* just for writing */ 182fe927c51SPeter Avalos int level; /* compression level */ 183fe927c51SPeter Avalos int strategy; /* compression strategy */ 184fe927c51SPeter Avalos /* seek request */ 185fe927c51SPeter Avalos z_off64_t skip; /* amount to skip (already rewound if backwards) */ 186fe927c51SPeter Avalos int seek; /* true if seek request pending */ 187fe927c51SPeter Avalos /* error information */ 188fe927c51SPeter Avalos int err; /* error code */ 189fe927c51SPeter Avalos char *msg; /* error message */ 190fe927c51SPeter Avalos /* zlib inflate or deflate stream */ 191fe927c51SPeter Avalos z_stream strm; /* stream structure in-place (not a pointer) */ 192fe927c51SPeter Avalos } gz_state; 193fe927c51SPeter Avalos typedef gz_state FAR *gz_statep; 194fe927c51SPeter Avalos 195fe927c51SPeter Avalos /* shared functions */ 196fe927c51SPeter Avalos void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *)); 197fe927c51SPeter Avalos #if defined UNDER_CE 198fe927c51SPeter Avalos char ZLIB_INTERNAL *gz_strwinerror OF((DWORD error)); 199fe927c51SPeter Avalos #endif 200fe927c51SPeter Avalos 201fe927c51SPeter Avalos /* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t 202fe927c51SPeter Avalos value -- needed when comparing unsigned to z_off64_t, which is signed 203fe927c51SPeter Avalos (possible z_off64_t types off_t, off64_t, and long are all signed) */ 204fe927c51SPeter Avalos #ifdef INT_MAX 205fe927c51SPeter Avalos # define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX) 206fe927c51SPeter Avalos #else 207fe927c51SPeter Avalos unsigned ZLIB_INTERNAL gz_intmax OF((void)); 208fe927c51SPeter Avalos # define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax()) 209fe927c51SPeter Avalos #endif 210