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