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