1 /* SPDX-License-Identifier: Zlib */
2 /* zutil.h -- internal interface and configuration of the compression library
3  * Copyright (C) 1995-2022 Jean-loup Gailly, Mark Adler
4  * For conditions of distribution and use, see copyright notice in zlib.h
5  */
6 
7 /* WARNING: this file should *not* be used by applications. It is
8    part of the implementation of the compression library and is
9    subject to change. Applications should only use zlib.h.
10  */
11 
12 /* @(#) $Id$ */
13 
14 #ifndef ZUTIL_H
15 #define ZUTIL_H
16 
17 #ifdef HAVE_HIDDEN
18 #  define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
19 #else
20 #  define ZLIB_INTERNAL
21 #endif
22 
23 #include "zlib.h"
24 
25 #if defined(STDC) && !defined(Z_SOLO)
26 #  if !(defined(_WIN32_WCE) && defined(_MSC_VER))
27 #    include <stddef.h>
28 #  endif
29 #  include <string.h>
30 #  include <stdlib.h>
31 #endif
32 
33 #ifndef local
34 #  define local static
35 #endif
36 /* since "static" is used to mean two completely different things in C, we
37    define "local" for the non-static meaning of "static", for readability
38    (compile with -Dlocal if your debugger can't find static symbols) */
39 
40 typedef unsigned char  uch;
41 typedef uch FAR uchf;
42 typedef unsigned short ush;
43 typedef ush FAR ushf;
44 typedef unsigned long  ulg;
45 
46 #if !defined(Z_U8) && !defined(Z_SOLO) && defined(STDC)
47 #  include <limits.h>
48 #  if (ULONG_MAX == 0xffffffffffffffff)
49 #    define Z_U8 unsigned long
50 #  elif (ULLONG_MAX == 0xffffffffffffffff)
51 #    define Z_U8 unsigned long long
52 #  elif (UINT_MAX == 0xffffffffffffffff)
53 #    define Z_U8 unsigned
54 #  endif
55 #endif
56 
57 extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
58 /* (size given to avoid silly warnings with Visual C++) */
59 
60 #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
61 
62 #define ERR_RETURN(strm,err) \
63   return (strm->msg = ERR_MSG(err), (err))
64 /* To be used only when the state is known to be valid */
65 
66         /* common constants */
67 
68 #ifndef DEF_WBITS
69 #  define DEF_WBITS MAX_WBITS
70 #endif
71 /* default windowBits for decompression. MAX_WBITS is for compression only */
72 
73 #if MAX_MEM_LEVEL >= 8
74 #  define DEF_MEM_LEVEL 8
75 #else
76 #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
77 #endif
78 /* default memLevel */
79 
80 #define STORED_BLOCK 0
81 #define STATIC_TREES 1
82 #define DYN_TREES    2
83 /* The three kinds of block type */
84 
85 #define MIN_MATCH  3
86 #define MAX_MATCH  258
87 /* The minimum and maximum match lengths */
88 
89 #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
90 
91         /* target dependencies */
92 
93 #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
94 #  define OS_CODE  0x00
95 #  ifndef Z_SOLO
96 #    if defined(__TURBOC__) || defined(__BORLANDC__)
97 #      if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
98          /* Allow compilation with ANSI keywords only enabled */
99          void _Cdecl farfree( void *block );
100          void *_Cdecl farmalloc( unsigned long nbytes );
101 #      else
102 #        include <alloc.h>
103 #      endif
104 #    else /* MSC or DJGPP */
105 #      include <malloc.h>
106 #    endif
107 #  endif
108 #endif
109 
110 #ifdef AMIGA
111 #  define OS_CODE  1
112 #endif
113 
114 #if defined(VAXC) || defined(VMS)
115 #  define OS_CODE  2
116 #  define F_OPEN(name, mode) \
117      fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
118 #endif
119 
120 #ifdef __370__
121 #  if __TARGET_LIB__ < 0x20000000
122 #    define OS_CODE 4
123 #  elif __TARGET_LIB__ < 0x40000000
124 #    define OS_CODE 11
125 #  else
126 #    define OS_CODE 8
127 #  endif
128 #endif
129 
130 #if defined(ATARI) || defined(atarist)
131 #  define OS_CODE  5
132 #endif
133 
134 #ifdef OS2
135 #  define OS_CODE  6
136 #  if defined(M_I86) && !defined(Z_SOLO)
137 #    include <malloc.h>
138 #  endif
139 #endif
140 
141 #if defined(MACOS) || defined(TARGET_OS_MAC)
142 #  define OS_CODE  7
143 #  ifndef Z_SOLO
144 #    if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
145 #      include <unix.h> /* for fdopen */
146 #    else
147 #      ifndef fdopen
148 #        define fdopen(fd,mode) NULL /* No fdopen() */
149 #      endif
150 #    endif
151 #  endif
152 #endif
153 
154 #ifdef __acorn
155 #  define OS_CODE 13
156 #endif
157 
158 #if defined(WIN32) && !defined(__CYGWIN__)
159 #  define OS_CODE  10
160 #endif
161 
162 #ifdef _BEOS_
163 #  define OS_CODE  16
164 #endif
165 
166 #ifdef __TOS_OS400__
167 #  define OS_CODE 18
168 #endif
169 
170 #ifdef __APPLE__
171 #  define OS_CODE 19
172 #endif
173 
174 #if defined(_BEOS_) || defined(RISCOS)
175 #  define fdopen(fd,mode) NULL /* No fdopen() */
176 #endif
177 
178 #if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX
179 #  if defined(_WIN32_WCE)
180 #    define fdopen(fd,mode) NULL /* No fdopen() */
181 #  else
182 #    define fdopen(fd,type)  _fdopen(fd,type)
183 #  endif
184 #endif
185 
186 #if defined(__BORLANDC__) && !defined(MSDOS)
187   #pragma warn -8004
188   #pragma warn -8008
189   #pragma warn -8066
190 #endif
191 
192 /* provide prototypes for these when building zlib without LFS */
193 #if !defined(_WIN32) && \
194     (!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0)
195     ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
196     ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
197 #endif
198 
199         /* common defaults */
200 
201 #ifndef OS_CODE
202 #  define OS_CODE  3     /* assume Unix */
203 #endif
204 
205 #ifndef F_OPEN
206 #  define F_OPEN(name, mode) fopen((name), (mode))
207 #endif
208 
209          /* functions */
210 
211 #if defined(pyr) || defined(Z_SOLO)
212 #  define NO_MEMCPY
213 #endif
214 #if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
215  /* Use our own functions for small and medium model with MSC <= 5.0.
216   * You may have to use the same strategy for Borland C (untested).
217   * The __SC__ check is for Symantec.
218   */
219 #  define NO_MEMCPY
220 #endif
221 #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
222 #  define HAVE_MEMCPY
223 #endif
224 #ifdef HAVE_MEMCPY
225 #  ifdef SMALL_MEDIUM /* MSDOS small or medium model */
226 #    define zmemcpy _fmemcpy
227 #    define zmemcmp _fmemcmp
228 #    define zmemzero(dest, len) _fmemset(dest, 0, len)
229 #  else
230 #    define zmemcpy memcpy
231 #    define zmemcmp memcmp
232 #    define zmemzero(dest, len) memset(dest, 0, len)
233 #  endif
234 #else
235    void ZLIB_INTERNAL zmemcpy OF((Bytef* dest, const Bytef* source, uInt len));
236    int ZLIB_INTERNAL zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len));
237    void ZLIB_INTERNAL zmemzero OF((Bytef* dest, uInt len));
238 #endif
239 
240 /* Diagnostic functions */
241 #ifdef ZLIB_DEBUG
242 #  include <stdio.h>
243    extern int ZLIB_INTERNAL z_verbose;
244    extern void ZLIB_INTERNAL z_error OF((char *m));
245 #  define Assert(cond,msg) {if(!(cond)) z_error(msg);}
246 #  define Trace(x) {if (z_verbose>=0) fprintf x ;}
247 #  define Tracev(x) {if (z_verbose>0) fprintf x ;}
248 #  define Tracevv(x) {if (z_verbose>1) fprintf x ;}
249 #  define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
250 #  define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
251 #else
252 #  define Assert(cond,msg)
253 #  define Trace(x)
254 #  define Tracev(x)
255 #  define Tracevv(x)
256 #  define Tracec(c,x)
257 #  define Tracecv(c,x)
258 #endif
259 
260 #ifndef Z_SOLO
261    voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items,
262                                     unsigned size));
263    void ZLIB_INTERNAL zcfree  OF((voidpf opaque, voidpf ptr));
264 #endif
265 
266 #define ZALLOC(strm, items, size) \
267            (*((strm)->zalloc))((strm)->opaque, (items), (size))
268 #define ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
269 #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
270 
271 /* Reverse the bytes in a 32-bit value */
272 #define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
273                     (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
274 
275 #endif /* ZUTIL_H */
276