Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef _ZBUILD_H |
2 | | #define _ZBUILD_H |
3 | | |
4 | | #define _POSIX_SOURCE 1 /* fileno */ |
5 | | #ifndef _POSIX_C_SOURCE |
6 | | # define _POSIX_C_SOURCE 200809L /* snprintf, posix_memalign, strdup */ |
7 | | #endif |
8 | | #ifndef _ISOC11_SOURCE |
9 | | # define _ISOC11_SOURCE 1 /* aligned_alloc */ |
10 | | #endif |
11 | | #ifdef __OpenBSD__ |
12 | | # define _BSD_SOURCE 1 |
13 | | #endif |
14 | | |
15 | | #include <stddef.h> |
16 | | #include <string.h> |
17 | | #include <stdlib.h> |
18 | | #include <stdint.h> |
19 | | |
20 | | /* Determine compiler version of C Standard */ |
21 | | #ifdef __STDC_VERSION__ |
22 | | # if __STDC_VERSION__ >= 199901L |
23 | | # ifndef STDC99 |
24 | | # define STDC99 |
25 | | # endif |
26 | | # endif |
27 | | # if __STDC_VERSION__ >= 201112L |
28 | | # ifndef STDC11 |
29 | | # define STDC11 |
30 | | # endif |
31 | | # endif |
32 | | #endif |
33 | | |
34 | | #ifndef Z_HAS_ATTRIBUTE |
35 | | # if defined(__has_attribute) |
36 | | # define Z_HAS_ATTRIBUTE(a) __has_attribute(a) |
37 | | # else |
38 | | # define Z_HAS_ATTRIBUTE(a) 0 |
39 | | # endif |
40 | | #endif |
41 | | |
42 | | #ifndef Z_FALLTHROUGH |
43 | | # if Z_HAS_ATTRIBUTE(__fallthrough__) || (defined(__GNUC__) && (__GNUC__ >= 7)) |
44 | 10.5M | # define Z_FALLTHROUGH __attribute__((__fallthrough__)) |
45 | | # else |
46 | | # define Z_FALLTHROUGH do {} while(0) /* fallthrough */ |
47 | | # endif |
48 | | #endif |
49 | | |
50 | | #ifndef Z_TARGET |
51 | | # if Z_HAS_ATTRIBUTE(__target__) |
52 | | # define Z_TARGET(x) __attribute__((__target__(x))) |
53 | | # else |
54 | | # define Z_TARGET(x) |
55 | | # endif |
56 | | #endif |
57 | | |
58 | | /* This has to be first include that defines any types */ |
59 | | #if defined(_MSC_VER) |
60 | | # if defined(_WIN64) |
61 | | typedef __int64 ssize_t; |
62 | | # else |
63 | | typedef long ssize_t; |
64 | | # endif |
65 | | |
66 | | # if defined(_WIN64) |
67 | | #define SSIZE_MAX _I64_MAX |
68 | | # else |
69 | | #define SSIZE_MAX LONG_MAX |
70 | | # endif |
71 | | #endif |
72 | | |
73 | | /* A forced inline decorator */ |
74 | | #if defined(_MSC_VER) |
75 | | # define Z_FORCEINLINE __forceinline |
76 | | #elif defined(__GNUC__) |
77 | | # define Z_FORCEINLINE inline __attribute__((always_inline)) |
78 | | #else |
79 | | /* It won't actually force inlining but it will suggest it */ |
80 | | # define Z_FORCEINLINE inline |
81 | | #endif |
82 | | |
83 | | /* MS Visual Studio does not allow inline in C, only C++. |
84 | | But it provides __inline instead, so use that. */ |
85 | | #if defined(_MSC_VER) && !defined(inline) && !defined(__cplusplus) |
86 | | # define inline __inline |
87 | | #endif |
88 | | |
89 | | #if defined(ZLIB_COMPAT) |
90 | | # define PREFIX(x) x |
91 | | # define PREFIX2(x) ZLIB_ ## x |
92 | | # define PREFIX3(x) z_ ## x |
93 | | # define PREFIX4(x) x ## 64 |
94 | | # define zVersion zlibVersion |
95 | | #else |
96 | 5.57k | # define PREFIX(x) zng_ ## x |
97 | 0 | # define PREFIX2(x) ZLIBNG_ ## x |
98 | 5.57k | # define PREFIX3(x) zng_ ## x |
99 | | # define PREFIX4(x) zng_ ## x |
100 | | # define zVersion zlibng_version |
101 | 0 | # define z_size_t size_t |
102 | | #endif |
103 | | |
104 | | /* In zlib-compat some functions and types use unsigned long, but zlib-ng use size_t */ |
105 | | #if defined(ZLIB_COMPAT) |
106 | | # define z_uintmax_t unsigned long |
107 | | #else |
108 | 2.78k | # define z_uintmax_t size_t |
109 | | #endif |
110 | | |
111 | | /* Minimum of a and b. */ |
112 | 587M | #define MIN(a, b) ((a) > (b) ? (b) : (a)) |
113 | | /* Maximum of a and b. */ |
114 | 20.3k | #define MAX(a, b) ((a) < (b) ? (b) : (a)) |
115 | | /* Ignore unused variable warning */ |
116 | 97.6M | #define Z_UNUSED(var) (void)(var) |
117 | | |
118 | | #if defined(HAVE_VISIBILITY_INTERNAL) |
119 | | # define Z_INTERNAL __attribute__((visibility ("internal"))) |
120 | | #elif defined(HAVE_VISIBILITY_HIDDEN) |
121 | | # define Z_INTERNAL __attribute__((visibility ("hidden"))) |
122 | | #else |
123 | | # define Z_INTERNAL |
124 | | #endif |
125 | | |
126 | | /* Symbol versioning helpers, allowing multiple versions of a function to exist. |
127 | | * Functions using this must also be added to zlib-ng.map for each version. |
128 | | * Double @@ means this is the default for newly compiled applications to link against. |
129 | | * Single @ means this is kept for backwards compatibility. |
130 | | * This is only used for Zlib-ng native API, and only on platforms supporting this. |
131 | | */ |
132 | | #if defined(HAVE_SYMVER) |
133 | | # define ZSYMVER(func,alias,ver) __asm__(".symver " func ", " alias "@ZLIB_NG_" ver); |
134 | | # define ZSYMVER_DEF(func,alias,ver) __asm__(".symver " func ", " alias "@@ZLIB_NG_" ver); |
135 | | #else |
136 | | # define ZSYMVER(func,alias,ver) |
137 | | # define ZSYMVER_DEF(func,alias,ver) |
138 | | #endif |
139 | | |
140 | | #ifndef __cplusplus |
141 | 75.1M | # define Z_REGISTER register |
142 | | #else |
143 | | # define Z_REGISTER |
144 | | #endif |
145 | | |
146 | | /* Reverse the bytes in a value. Use compiler intrinsics when |
147 | | possible to take advantage of hardware implementations. */ |
148 | | #if defined(_MSC_VER) && (_MSC_VER >= 1300) |
149 | | # include <stdlib.h> |
150 | | # pragma intrinsic(_byteswap_ulong) |
151 | | # define ZSWAP16(q) _byteswap_ushort(q) |
152 | | # define ZSWAP32(q) _byteswap_ulong(q) |
153 | | # define ZSWAP64(q) _byteswap_uint64(q) |
154 | | |
155 | | #elif defined(__clang__) || (defined(__GNUC__) && \ |
156 | | (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))) |
157 | 2.78k | # define ZSWAP16(q) __builtin_bswap16(q) |
158 | 2.78k | # define ZSWAP32(q) __builtin_bswap32(q) |
159 | | # define ZSWAP64(q) __builtin_bswap64(q) |
160 | | |
161 | | #elif defined(__GNUC__) && (__GNUC__ >= 2) && defined(__linux__) |
162 | | # include <byteswap.h> |
163 | | # define ZSWAP16(q) bswap_16(q) |
164 | | # define ZSWAP32(q) bswap_32(q) |
165 | | # define ZSWAP64(q) bswap_64(q) |
166 | | |
167 | | #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) |
168 | | # include <sys/endian.h> |
169 | | # define ZSWAP16(q) bswap16(q) |
170 | | # define ZSWAP32(q) bswap32(q) |
171 | | # define ZSWAP64(q) bswap64(q) |
172 | | #elif defined(__OpenBSD__) |
173 | | # include <sys/endian.h> |
174 | | # define ZSWAP16(q) swap16(q) |
175 | | # define ZSWAP32(q) swap32(q) |
176 | | # define ZSWAP64(q) swap64(q) |
177 | | #elif defined(__INTEL_COMPILER) |
178 | | /* ICC does not provide a two byte swap. */ |
179 | | # define ZSWAP16(q) ((((q) & 0xff) << 8) | (((q) & 0xff00) >> 8)) |
180 | | # define ZSWAP32(q) _bswap(q) |
181 | | # define ZSWAP64(q) _bswap64(q) |
182 | | |
183 | | #else |
184 | | # define ZSWAP16(q) ((((q) & 0xff) << 8) | (((q) & 0xff00) >> 8)) |
185 | | # define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ |
186 | | (((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) |
187 | | # define ZSWAP64(q) \ |
188 | | (((q & 0xFF00000000000000u) >> 56u) | \ |
189 | | ((q & 0x00FF000000000000u) >> 40u) | \ |
190 | | ((q & 0x0000FF0000000000u) >> 24u) | \ |
191 | | ((q & 0x000000FF00000000u) >> 8u) | \ |
192 | | ((q & 0x00000000FF000000u) << 8u) | \ |
193 | | ((q & 0x0000000000FF0000u) << 24u) | \ |
194 | | ((q & 0x000000000000FF00u) << 40u) | \ |
195 | | ((q & 0x00000000000000FFu) << 56u)) |
196 | | #endif |
197 | | |
198 | | /* Only enable likely/unlikely if the compiler is known to support it */ |
199 | | #if (defined(__GNUC__) && (__GNUC__ >= 3)) || defined(__INTEL_COMPILER) || defined(__clang__) |
200 | | # define LIKELY_NULL(x) __builtin_expect((x) != 0, 0) |
201 | 502M | # define LIKELY(x) __builtin_expect(!!(x), 1) |
202 | 801M | # define UNLIKELY(x) __builtin_expect(!!(x), 0) |
203 | | #else |
204 | | # define LIKELY_NULL(x) x |
205 | | # define LIKELY(x) x |
206 | | # define UNLIKELY(x) x |
207 | | #endif /* (un)likely */ |
208 | | |
209 | | #if defined(HAVE_ATTRIBUTE_ALIGNED) |
210 | 150M | # define ALIGNED_(x) __attribute__ ((aligned(x))) |
211 | | #elif defined(_MSC_VER) |
212 | | # define ALIGNED_(x) __declspec(align(x)) |
213 | | #else |
214 | | /* TODO: Define ALIGNED_ for your compiler */ |
215 | | # define ALIGNED_(x) |
216 | | #endif |
217 | | |
218 | | #ifdef HAVE_BUILTIN_ASSUME_ALIGNED |
219 | 25.0k | # define HINT_ALIGNED(p,n) __builtin_assume_aligned((void *)(p),(n)) |
220 | | #else |
221 | | # define HINT_ALIGNED(p,n) (p) |
222 | | #endif |
223 | 2.78k | #define HINT_ALIGNED_16(p) HINT_ALIGNED((p),16) |
224 | 22.2k | #define HINT_ALIGNED_64(p) HINT_ALIGNED((p),64) |
225 | | #define HINT_ALIGNED_4096(p) HINT_ALIGNED((p),4096) |
226 | | |
227 | | /* PADSZ returns needed bytes to pad bpos to pad size |
228 | | * PAD_NN calculates pad size and adds it to bpos, returning the result. |
229 | | * All take an integer or a pointer as bpos input. |
230 | | */ |
231 | 30.6k | #define PADSZ(bpos, pad) (((pad) - ((uintptr_t)(bpos) % (pad))) % (pad)) |
232 | 5.57k | #define PAD_16(bpos) ((bpos) + PADSZ((bpos),16)) |
233 | 25.0k | #define PAD_64(bpos) ((bpos) + PADSZ((bpos),64)) |
234 | | #define PAD_4096(bpos) ((bpos) + PADSZ((bpos),4096)) |
235 | | |
236 | | /* Diagnostic functions */ |
237 | | #ifdef ZLIB_DEBUG |
238 | | # include <stdio.h> |
239 | | extern int Z_INTERNAL z_verbose; |
240 | | extern void Z_INTERNAL z_error(const char *m); |
241 | | # define Assert(cond, msg) {int _cond = (cond); if (!_cond) z_error(msg);} |
242 | | # define Trace(x) {if (z_verbose >= 0) fprintf x;} |
243 | | # define Tracev(x) {if (z_verbose > 0) fprintf x;} |
244 | | # define Tracevv(x) {if (z_verbose > 1) fprintf x;} |
245 | | # define Tracec(c, x) {if (z_verbose > 0 && (c)) fprintf x;} |
246 | | # define Tracecv(c, x) {if (z_verbose > 1 && (c)) fprintf x;} |
247 | | #else |
248 | | # define Assert(cond, msg) |
249 | | # define Trace(x) |
250 | | # define Tracev(x) |
251 | | # define Tracevv(x) |
252 | | # define Tracec(c, x) |
253 | | # define Tracecv(c, x) |
254 | | #endif |
255 | | |
256 | | /* OPTIMAL_CMP values determine the comparison width: |
257 | | * 64: Best for 64-bit architectures with unaligned access |
258 | | * 32: Best for 32-bit architectures with unaligned access |
259 | | * 16: Safe default for unknown architectures |
260 | | * 8: Safe fallback for architectures without unaligned access |
261 | | * Note: The unaligned access mentioned is cpu-support, this allows compiler or |
262 | | * separate unaligned intrinsics to utilize safe unaligned access, without |
263 | | * utilizing unaligned C pointers that are known to have undefined behavior. |
264 | | */ |
265 | | #if !defined(OPTIMAL_CMP) |
266 | | # if defined(__x86_64__) || defined(_M_X64) || defined(__amd64__) || defined(_M_AMD64) |
267 | | # define OPTIMAL_CMP 64 |
268 | | # elif defined(__i386__) || defined(__i486__) || defined(__i586__) || \ |
269 | | defined(__i686__) || defined(_X86_) || defined(_M_IX86) |
270 | | # define OPTIMAL_CMP 32 |
271 | | # elif defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC) |
272 | | # if defined(__ARM_FEATURE_UNALIGNED) || defined(_WIN32) |
273 | | # define OPTIMAL_CMP 64 |
274 | | # else |
275 | | # define OPTIMAL_CMP 8 |
276 | | # endif |
277 | | # elif defined(__arm__) || defined(_M_ARM) |
278 | | # if defined(__ARM_FEATURE_UNALIGNED) || defined(_WIN32) |
279 | | # define OPTIMAL_CMP 32 |
280 | | # else |
281 | | # define OPTIMAL_CMP 8 |
282 | | # endif |
283 | | # elif defined(__powerpc64__) || defined(__ppc64__) |
284 | | # define OPTIMAL_CMP 64 |
285 | | # elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) |
286 | | # define OPTIMAL_CMP 32 |
287 | | # endif |
288 | | #endif |
289 | | #if !defined(OPTIMAL_CMP) |
290 | | # define OPTIMAL_CMP 16 |
291 | | #endif |
292 | | |
293 | | #if defined(__has_feature) |
294 | | # if __has_feature(address_sanitizer) |
295 | | # define Z_ADDRESS_SANITIZER 1 |
296 | | # endif |
297 | | #elif defined(__SANITIZE_ADDRESS__) |
298 | | # define Z_ADDRESS_SANITIZER 1 |
299 | | #endif |
300 | | |
301 | | /* |
302 | | * __asan_loadN() and __asan_storeN() calls are inserted by compilers in order to check memory accesses. |
303 | | * They can be called manually too, with the following caveats: |
304 | | * gcc says: "warning: implicit declaration of function '...'" |
305 | | * g++ says: "error: new declaration '...' ambiguates built-in declaration '...'" |
306 | | * Accommodate both. |
307 | | */ |
308 | | #ifdef Z_ADDRESS_SANITIZER |
309 | | #ifndef __cplusplus |
310 | | void __asan_loadN(void *, long); |
311 | | void __asan_storeN(void *, long); |
312 | | #endif |
313 | | #else |
314 | | # define __asan_loadN(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0) |
315 | | # define __asan_storeN(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0) |
316 | | #endif |
317 | | |
318 | | #if defined(__has_feature) |
319 | | # if __has_feature(memory_sanitizer) |
320 | | # define Z_MEMORY_SANITIZER 1 |
321 | | # include <sanitizer/msan_interface.h> |
322 | | # endif |
323 | | #endif |
324 | | |
325 | | #ifndef Z_MEMORY_SANITIZER |
326 | | # define __msan_check_mem_is_initialized(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0) |
327 | 0 | # define __msan_unpoison(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0) |
328 | | #endif |
329 | | |
330 | | /* Notify sanitizer runtime about an upcoming read access. */ |
331 | | #define instrument_read(a, size) do { \ |
332 | | void *__a = (void *)(a); \ |
333 | | long __size = size; \ |
334 | | __asan_loadN(__a, __size); \ |
335 | | __msan_check_mem_is_initialized(__a, __size); \ |
336 | | } while (0) |
337 | | |
338 | | /* Notify sanitizer runtime about an upcoming write access. */ |
339 | | #define instrument_write(a, size) do { \ |
340 | | void *__a = (void *)(a); \ |
341 | | long __size = size; \ |
342 | | __asan_storeN(__a, __size); \ |
343 | | } while (0) |
344 | | |
345 | | /* Notify sanitizer runtime about an upcoming read/write access. */ |
346 | | #define instrument_read_write(a, size) do { \ |
347 | | void *__a = (void *)(a); \ |
348 | | long __size = size; \ |
349 | | __asan_storeN(__a, __size); \ |
350 | | __msan_check_mem_is_initialized(__a, __size); \ |
351 | | } while (0) |
352 | | |
353 | | #endif |