Coverage Report

Created: 2025-10-28 06:57

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