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