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