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