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