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 #endif
104 
105 #if defined(MACOS) || defined(TARGET_OS_MAC)
106 #  define OS_CODE  7
107 #endif
108 
109 #ifdef __acorn
110 #  define OS_CODE 13
111 #endif
112 
113 #if defined(_WIN32) && !defined(__CYGWIN__)
114 #  define OS_CODE  10
115 #endif
116 
117 #ifdef __APPLE__
118 #  define OS_CODE 19
119 #endif
120 
121 #if (defined(_MSC_VER) && (_MSC_VER > 600))
122 #  define fdopen(fd, type)  _fdopen(fd, type)
123 #endif
124 
125 /* MS Visual Studio does not allow inline in C, only C++.
126    But it provides __inline instead, so use that. */
127 #if defined(_MSC_VER) && !defined(inline) && !defined(__cplusplus)
128 #  define inline __inline
129 #endif
130 
131         /* common defaults */
132 
133 #ifndef OS_CODE
134 #  define OS_CODE  3  /* assume Unix */
135 #endif
136 
137          /* functions */
138 
139 /* Diagnostic functions */
140 #ifdef ZLIB_DEBUG
141 #  include <stdio.h>
142    extern int Z_INTERNAL z_verbose;
143    extern void Z_INTERNAL z_error(char *m);
144 #  define Assert(cond, msg) {if (!(cond)) z_error(msg);}
145 #  define Trace(x) {if (z_verbose >= 0) fprintf x;}
146 #  define Tracev(x) {if (z_verbose > 0) fprintf x;}
147 #  define Tracevv(x) {if (z_verbose > 1) fprintf x;}
148 #  define Tracec(c, x) {if (z_verbose > 0 && (c)) fprintf x;}
149 #  define Tracecv(c, x) {if (z_verbose > 1 && (c)) fprintf x;}
150 #else
151 #  define Assert(cond, msg)
152 #  define Trace(x)
153 #  define Tracev(x)
154 #  define Tracevv(x)
155 #  define Tracec(c, x)
156 #  define Tracecv(c, x)
157 #endif
158 
159 void Z_INTERNAL *zng_calloc(void *opaque, unsigned items, unsigned size);
160 void Z_INTERNAL   zng_cfree(void *opaque, void *ptr);
161 
162 #define ZALLOC(strm, items, size) (*((strm)->zalloc))((strm)->opaque, (items), (size))
163 #define ZFREE(strm, addr)         (*((strm)->zfree))((strm)->opaque, (void *)(addr))
164 #define TRY_FREE(s, p)            {if (p) ZFREE(s, p);}
165 
166 /* Reverse the bytes in a value. Use compiler intrinsics when
167    possible to take advantage of hardware implementations. */
168 #if defined(_MSC_VER) && (_MSC_VER >= 1300)
169 #  pragma intrinsic(_byteswap_ulong)
170 #  define ZSWAP16(q) _byteswap_ushort(q)
171 #  define ZSWAP32(q) _byteswap_ulong(q)
172 #  define ZSWAP64(q) _byteswap_uint64(q)
173 
174 #elif defined(__Clang__) || (defined(__GNUC__) && \
175         (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))
176 #  define ZSWAP16(q) __builtin_bswap16(q)
177 #  define ZSWAP32(q) __builtin_bswap32(q)
178 #  define ZSWAP64(q) __builtin_bswap64(q)
179 
180 #elif defined(__GNUC__) && (__GNUC__ >= 2) && defined(__linux__)
181 #  include <byteswap.h>
182 #  define ZSWAP16(q) bswap_16(q)
183 #  define ZSWAP32(q) bswap_32(q)
184 #  define ZSWAP64(q) bswap_64(q)
185 
186 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
187 #  include <sys/endian.h>
188 #  define ZSWAP16(q) bswap16(q)
189 #  define ZSWAP32(q) bswap32(q)
190 #  define ZSWAP64(q) bswap64(q)
191 #elif defined(__OpenBSD__)
192 #  include <sys/endian.h>
193 #  define ZSWAP16(q) swap16(q)
194 #  define ZSWAP32(q) swap32(q)
195 #  define ZSWAP64(q) swap64(q)
196 #elif defined(__INTEL_COMPILER)
197 /* ICC does not provide a two byte swap. */
198 #  define ZSWAP16(q) ((((q) & 0xff) << 8) | (((q) & 0xff00) >> 8))
199 #  define ZSWAP32(q) _bswap(q)
200 #  define ZSWAP64(q) _bswap64(q)
201 
202 #else
203 #  define ZSWAP16(q) ((((q) & 0xff) << 8) | (((q) & 0xff00) >> 8))
204 #  define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
205                      (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
206 #  define ZSWAP64(q)                           \
207           ((q & 0xFF00000000000000u) >> 56u) | \
208           ((q & 0x00FF000000000000u) >> 40u) | \
209           ((q & 0x0000FF0000000000u) >> 24u) | \
210           ((q & 0x000000FF00000000u) >> 8u)  | \
211           ((q & 0x00000000FF000000u) << 8u)  | \
212           ((q & 0x0000000000FF0000u) << 24u) | \
213           ((q & 0x000000000000FF00u) << 40u) | \
214           ((q & 0x00000000000000FFu) << 56u)
215 #endif
216 
217 /* Only enable likely/unlikely if the compiler is known to support it */
218 #if (defined(__GNUC__) && (__GNUC__ >= 3)) || defined(__INTEL_COMPILER) || defined(__Clang__)
219 #  define LIKELY_NULL(x)        __builtin_expect((x) != 0, 0)
220 #  define LIKELY(x)             __builtin_expect(!!(x), 1)
221 #  define UNLIKELY(x)           __builtin_expect(!!(x), 0)
222 #  define PREFETCH_L1(addr)     __builtin_prefetch(addr, 0, 3)
223 #  define PREFETCH_L2(addr)     __builtin_prefetch(addr, 0, 2)
224 #  define PREFETCH_RW(addr)     __builtin_prefetch(addr, 1, 2)
225 #elif defined(__WIN__)
226 #  include <xmmintrin.h>
227 #  define LIKELY_NULL(x)        x
228 #  define LIKELY(x)             x
229 #  define UNLIKELY(x)           x
230 #  define PREFETCH_L1(addr)     _mm_prefetch((char *) addr, _MM_HINT_T0)
231 #  define PREFETCH_L2(addr)     _mm_prefetch((char *) addr, _MM_HINT_T1)
232 #  define PREFETCH_RW(addr)     _mm_prefetch((char *) addr, _MM_HINT_T1)
233 #else
234 #  define LIKELY_NULL(x)        x
235 #  define LIKELY(x)             x
236 #  define UNLIKELY(x)           x
237 #  define PREFETCH_L1(addr)     addr
238 #  define PREFETCH_L2(addr)     addr
239 #  define PREFETCH_RW(addr)     addr
240 #endif /* (un)likely */
241 
242 #if defined(_MSC_VER)
243 #  define ALIGNED_(x) __declspec(align(x))
244 #else
245 #  if defined(__GNUC__)
246 #    define ALIGNED_(x) __attribute__ ((aligned(x)))
247 #  endif
248 #endif
249 
250 #if defined(X86_FEATURES)
251 #  include "arch/x86/x86.h"
252 #elif defined(ARM_FEATURES)
253 #  include "arch/arm/arm.h"
254 #elif defined(POWER_FEATURES)
255 #  include "arch/power/power.h"
256 #endif
257 
258 #endif /* ZUTIL_H_ */
259