xref: /openbsd-src/sys/lib/libz/zutil.h (revision 03f0755e35ca1a342c64c6f56a71ac596b81552d)
1 /* zutil.h -- internal interface and configuration of the compression library
2  * Copyright (C) 1995-2024 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 #ifdef _KERNEL
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 
26 #define MY_ZCALLOC
27 
28 typedef long ptrdiff_t;
29 #else
30 #ifdef _STANDALONE
31 #include <lib/libsa/stand.h>
32 #else
33 #if defined(STDC) && !defined(Z_SOLO)
34 #  if !(defined(_WIN32_WCE) && defined(_MSC_VER))
35 #    include <stddef.h>
36 #  endif
37 #  include <string.h>
38 #  include <stdlib.h>
39 #endif
40 #endif
41 #endif
42 
43 #ifdef Z_SOLO
44    typedef long ptrdiff_t;
45 #endif
46 
47 #ifndef local
48 #  define local static
49 #endif
50 /* since "static" is used to mean two completely different things in C, we
51    define "local" for the non-static meaning of "static", for readability
52    (compile with -Dlocal if your debugger can't find static symbols) */
53 
54 typedef unsigned char  uch;
55 typedef uch FAR uchf;
56 typedef unsigned short ush;
57 typedef ush FAR ushf;
58 typedef unsigned long  ulg;
59 
60 #if !defined(Z_U8) && !defined(Z_SOLO) && defined(STDC)
61 #  include <sys/limits.h>
62 #  if (ULONG_MAX == 0xffffffffffffffff)
63 #    define Z_U8 unsigned long
64 #  elif (ULLONG_MAX == 0xffffffffffffffff)
65 #    define Z_U8 unsigned long long
66 #  elif (ULONG_LONG_MAX == 0xffffffffffffffff)
67 #    define Z_U8 unsigned long long
68 #  elif (UINT_MAX == 0xffffffffffffffff)
69 #    define Z_U8 unsigned
70 #  endif
71 #endif
72 
73 extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
74 /* (size given to avoid silly warnings with Visual C++) */
75 
76 #define ERR_MSG(err) z_errmsg[(err) < -6 || (err) > 2 ? 9 : 2 - (err)]
77 
78 #define ERR_RETURN(strm,err) \
79   return (strm->msg = ERR_MSG(err), (err))
80 /* To be used only when the state is known to be valid */
81 
82         /* common constants */
83 #if MAX_WBITS < 9 || MAX_WBITS > 15
84 #  error MAX_WBITS must be in 9..15
85 #endif
86 #ifndef DEF_WBITS
87 #  define DEF_WBITS MAX_WBITS
88 #endif
89 /* default windowBits for decompression. MAX_WBITS is for compression only */
90 
91 #if MAX_MEM_LEVEL >= 8
92 #  define DEF_MEM_LEVEL 8
93 #else
94 #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
95 #endif
96 /* default memLevel */
97 
98 #define STORED_BLOCK 0
99 #define STATIC_TREES 1
100 #define DYN_TREES    2
101 /* The three kinds of block type */
102 
103 #define MIN_MATCH  3
104 #define MAX_MATCH  258
105 /* The minimum and maximum match lengths */
106 
107 #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
108 
109         /* target dependencies */
110 
111 #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
112 #  define OS_CODE  0x00
113 #  ifndef Z_SOLO
114 #    if defined(__TURBOC__) || defined(__BORLANDC__)
115 #      if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
116          /* Allow compilation with ANSI keywords only enabled */
117          void _Cdecl farfree( void *block );
118          void *_Cdecl farmalloc( unsigned long nbytes );
119 #      else
120 #        include <alloc.h>
121 #      endif
122 #    else /* MSC or DJGPP */
123 #      include <malloc.h>
124 #    endif
125 #  endif
126 #endif
127 
128 #ifdef AMIGA
129 #  define OS_CODE  1
130 #endif
131 
132 #if defined(VAXC) || defined(VMS)
133 #  define OS_CODE  2
134 #  define F_OPEN(name, mode) \
135      fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
136 #endif
137 
138 #ifdef __370__
139 #  if __TARGET_LIB__ < 0x20000000
140 #    define OS_CODE 4
141 #  elif __TARGET_LIB__ < 0x40000000
142 #    define OS_CODE 11
143 #  else
144 #    define OS_CODE 8
145 #  endif
146 #endif
147 
148 #if defined(ATARI) || defined(atarist)
149 #  define OS_CODE  5
150 #endif
151 
152 #ifdef OS2
153 #  define OS_CODE  6
154 #  if defined(M_I86) && !defined(Z_SOLO)
155 #    include <malloc.h>
156 #  endif
157 #endif
158 
159 #if defined(MACOS)
160 #  define OS_CODE  7
161 #endif
162 
163 #if defined(__acorn) || defined(__riscos)
164 #  define OS_CODE 13
165 #endif
166 
167 #if defined(WIN32) && !defined(__CYGWIN__)
168 #  define OS_CODE  10
169 #endif
170 
171 #ifdef _BEOS_
172 #  define OS_CODE  16
173 #endif
174 
175 #ifdef __TOS_OS400__
176 #  define OS_CODE 18
177 #endif
178 
179 #ifdef __APPLE__
180 #  define OS_CODE 19
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 #ifndef Z_LARGE64
191    ZEXTERN uLong ZEXPORT adler32_combine64(uLong, uLong, z_off64_t);
192    ZEXTERN uLong ZEXPORT crc32_combine64(uLong, uLong, z_off64_t);
193    ZEXTERN uLong ZEXPORT crc32_combine_gen64(z_off64_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(Bytef* dest, const Bytef* source, uInt len);
233    int ZLIB_INTERNAL zmemcmp(const Bytef* s1, const Bytef* s2, uInt len);
234    void ZLIB_INTERNAL zmemzero(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(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(voidpf opaque, unsigned items,
259                                 unsigned size);
260    void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr, unsigned size);
261 #endif
262 
263 #define ZALLOC(strm, items, size) \
264            (*((strm)->zalloc))((strm)->opaque, (items), (size))
265 #define ZFREE(strm, addr, size) \
266            (*((strm)->zfree))((strm)->opaque, (voidpf)(addr), (size))
267 #define TRY_FREE(s, p, size) ZFREE(s, p, size)
268 
269 /* Reverse the bytes in a 32-bit value */
270 #define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
271                     (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
272 
273 #endif /* ZUTIL_H */
274