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