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