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