1 #ifndef ZUTIL_H_ 2 #define ZUTIL_H_ 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 #if defined(HAVE_VISIBILITY_INTERNAL) 14 # define Z_INTERNAL __attribute__((visibility ("internal"))) 15 #elif defined(HAVE_VISIBILITY_HIDDEN) 16 # define Z_INTERNAL __attribute__((visibility ("hidden"))) 17 #else 18 # define Z_INTERNAL 19 #endif 20 21 #ifndef __cplusplus 22 # define Z_REGISTER register 23 #else 24 # define Z_REGISTER 25 #endif 26 27 #ifndef Z_TLS 28 # define Z_TLS 29 #endif 30 31 #include <stddef.h> 32 #include <string.h> 33 #include <stdlib.h> 34 #include <stdint.h> 35 #ifdef ZLIB_COMPAT 36 # include "zlib.h" 37 #else 38 # include "zlib-ng.h" 39 #endif 40 #include "zbuild.h" 41 42 typedef unsigned char uch; /* Included for compatibility with external code only */ 43 typedef uint16_t ush; /* Included for compatibility with external code only */ 44 typedef unsigned long ulg; 45 46 extern z_const char * const PREFIX(z_errmsg)[10]; /* indexed by 2-zlib_error */ 47 /* (size given to avoid silly warnings with Visual C++) */ 48 49 #define ERR_MSG(err) PREFIX(z_errmsg)[Z_NEED_DICT-(err)] 50 51 #define ERR_RETURN(strm, err) return (strm->msg = ERR_MSG(err), (err)) 52 /* To be used only when the state is known to be valid */ 53 54 /* common constants */ 55 56 #ifndef DEF_WBITS 57 # define DEF_WBITS MAX_WBITS 58 #endif 59 /* default windowBits for decompression. MAX_WBITS is for compression only */ 60 61 #if MAX_MEM_LEVEL >= 8 62 # define DEF_MEM_LEVEL 8 63 #else 64 # define DEF_MEM_LEVEL MAX_MEM_LEVEL 65 #endif 66 /* default memLevel */ 67 68 #define STORED_BLOCK 0 69 #define STATIC_TREES 1 70 #define DYN_TREES 2 71 /* The three kinds of block type */ 72 73 #define MIN_MATCH 3 74 #define MAX_MATCH 258 75 /* The minimum and maximum match lengths */ 76 77 #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */ 78 79 #define ADLER32_INITIAL_VALUE 1 /* initial adler-32 hash value */ 80 81 /* target dependencies */ 82 83 #ifdef AMIGA 84 # define OS_CODE 1 85 #endif 86 87 #ifdef __370__ 88 # if __TARGET_LIB__ < 0x20000000 89 # define OS_CODE 4 90 # elif __TARGET_LIB__ < 0x40000000 91 # define OS_CODE 11 92 # else 93 # define OS_CODE 8 94 # endif 95 #endif 96 97 #if defined(ATARI) || defined(atarist) 98 # define OS_CODE 5 99 #endif 100 101 #ifdef OS2 102 # define OS_CODE 6 103 # if defined(M_I86) && !defined(Z_SOLO) 104 # include <malloc.h> 105 # endif 106 #endif 107 108 #if defined(MACOS) || defined(TARGET_OS_MAC) 109 # define OS_CODE 7 110 #endif 111 112 #ifdef __acorn 113 # define OS_CODE 13 114 #endif 115 116 #if defined(_WIN32) && !defined(__CYGWIN__) 117 # define OS_CODE 10 118 #endif 119 120 #ifdef __APPLE__ 121 # define OS_CODE 19 122 #endif 123 124 #if (defined(_MSC_VER) && (_MSC_VER > 600)) 125 # define fdopen(fd, type) _fdopen(fd, type) 126 #endif 127 128 /* MS Visual Studio does not allow inline in C, only C++. 129 But it provides __inline instead, so use that. */ 130 #if defined(_MSC_VER) && !defined(inline) && !defined(__cplusplus) 131 # define inline __inline 132 #endif 133 134 /* common defaults */ 135 136 #ifndef OS_CODE 137 # define OS_CODE 3 /* assume Unix */ 138 #endif 139 140 #ifndef F_OPEN 141 # define F_OPEN(name, mode) fopen((name), (mode)) 142 #endif 143 144 /* functions */ 145 146 /* Diagnostic functions */ 147 #ifdef ZLIB_DEBUG 148 # include <stdio.h> 149 extern int Z_INTERNAL z_verbose; 150 extern void Z_INTERNAL z_error(char *m); 151 # define Assert(cond, msg) {if (!(cond)) z_error(msg);} 152 # define Trace(x) {if (z_verbose >= 0) fprintf x;} 153 # define Tracev(x) {if (z_verbose > 0) fprintf x;} 154 # define Tracevv(x) {if (z_verbose > 1) fprintf x;} 155 # define Tracec(c, x) {if (z_verbose > 0 && (c)) fprintf x;} 156 # define Tracecv(c, x) {if (z_verbose > 1 && (c)) fprintf x;} 157 #else 158 # define Assert(cond, msg) 159 # define Trace(x) 160 # define Tracev(x) 161 # define Tracevv(x) 162 # define Tracec(c, x) 163 # define Tracecv(c, x) 164 #endif 165 166 void Z_INTERNAL *zng_calloc(void *opaque, unsigned items, unsigned size); 167 void Z_INTERNAL zng_cfree(void *opaque, void *ptr); 168 169 #define ZALLOC(strm, items, size) (*((strm)->zalloc))((strm)->opaque, (items), (size)) 170 #define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (void *)(addr)) 171 #define TRY_FREE(s, p) {if (p) ZFREE(s, p);} 172 173 /* Reverse the bytes in a value. Use compiler intrinsics when 174 possible to take advantage of hardware implementations. */ 175 #if defined(_MSC_VER) && (_MSC_VER >= 1300) 176 # pragma intrinsic(_byteswap_ulong) 177 # define ZSWAP16(q) _byteswap_ushort(q) 178 # define ZSWAP32(q) _byteswap_ulong(q) 179 # define ZSWAP64(q) _byteswap_uint64(q) 180 181 #elif defined(__Clang__) || (defined(__GNUC__) && \ 182 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))) 183 # define ZSWAP16(q) __builtin_bswap16(q) 184 # define ZSWAP32(q) __builtin_bswap32(q) 185 # define ZSWAP64(q) __builtin_bswap64(q) 186 187 #elif defined(__GNUC__) && (__GNUC__ >= 2) && defined(__linux__) 188 # include <byteswap.h> 189 # define ZSWAP16(q) bswap_16(q) 190 # define ZSWAP32(q) bswap_32(q) 191 # define ZSWAP64(q) bswap_64(q) 192 193 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) 194 # include <sys/endian.h> 195 # define ZSWAP16(q) bswap16(q) 196 # define ZSWAP32(q) bswap32(q) 197 # define ZSWAP64(q) bswap64(q) 198 199 #elif defined(__INTEL_COMPILER) 200 /* ICC does not provide a two byte swap. */ 201 # define ZSWAP16(q) ((((q) & 0xff) << 8) | (((q) & 0xff00) >> 8)) 202 # define ZSWAP32(q) _bswap(q) 203 # define ZSWAP64(q) _bswap64(q) 204 205 #else 206 # define ZSWAP16(q) ((((q) & 0xff) << 8) | (((q) & 0xff00) >> 8)) 207 # define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ 208 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) 209 # define ZSWAP64(q) \ 210 ((q & 0xFF00000000000000u) >> 56u) | \ 211 ((q & 0x00FF000000000000u) >> 40u) | \ 212 ((q & 0x0000FF0000000000u) >> 24u) | \ 213 ((q & 0x000000FF00000000u) >> 8u) | \ 214 ((q & 0x00000000FF000000u) << 8u) | \ 215 ((q & 0x0000000000FF0000u) << 24u) | \ 216 ((q & 0x000000000000FF00u) << 40u) | \ 217 ((q & 0x00000000000000FFu) << 56u) 218 #endif 219 220 /* Only enable likely/unlikely if the compiler is known to support it */ 221 #if (defined(__GNUC__) && (__GNUC__ >= 3)) || defined(__INTEL_COMPILER) || defined(__Clang__) 222 # define LIKELY_NULL(x) __builtin_expect((x) != 0, 0) 223 # define LIKELY(x) __builtin_expect(!!(x), 1) 224 # define UNLIKELY(x) __builtin_expect(!!(x), 0) 225 # define PREFETCH_L1(addr) __builtin_prefetch(addr, 0, 3) 226 # define PREFETCH_L2(addr) __builtin_prefetch(addr, 0, 2) 227 # define PREFETCH_RW(addr) __builtin_prefetch(addr, 1, 2) 228 #elif defined(__WIN__) 229 # include <xmmintrin.h> 230 # define LIKELY_NULL(x) x 231 # define LIKELY(x) x 232 # define UNLIKELY(x) x 233 # define PREFETCH_L1(addr) _mm_prefetch((char *) addr, _MM_HINT_T0) 234 # define PREFETCH_L2(addr) _mm_prefetch((char *) addr, _MM_HINT_T1) 235 # define PREFETCH_RW(addr) _mm_prefetch((char *) addr, _MM_HINT_T1) 236 #else 237 # define LIKELY_NULL(x) x 238 # define LIKELY(x) x 239 # define UNLIKELY(x) x 240 # define PREFETCH_L1(addr) addr 241 # define PREFETCH_L2(addr) addr 242 # define PREFETCH_RW(addr) addr 243 #endif /* (un)likely */ 244 245 #if defined(_MSC_VER) 246 # define ALIGNED_(x) __declspec(align(x)) 247 #else 248 # if defined(__GNUC__) 249 # define ALIGNED_(x) __attribute__ ((aligned(x))) 250 # endif 251 #endif 252 253 #if defined(X86_FEATURES) 254 # include "arch/x86/x86.h" 255 #elif defined(ARM_FEATURES) 256 # include "arch/arm/arm.h" 257 #elif defined(POWER_FEATURES) 258 # include "arch/power/power.h" 259 #endif 260 261 #endif /* ZUTIL_H_ */ 262