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