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
42typedef unsigned char uch; /* Included for compatibility with external code only */
43typedef uint16_t ush; /* Included for compatibility with external code only */
44typedef unsigned long ulg;
45
46extern 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#define ZLIB_WRAPLEN 6 /* zlib format overhead */
82#define GZIP_WRAPLEN 18 /* gzip format overhead */
83
84#define DEFLATE_HEADER_BITS 3
85#define DEFLATE_EOBS_BITS 15
86#define DEFLATE_PAD_BITS 6
87#define DEFLATE_BLOCK_OVERHEAD ((DEFLATE_HEADER_BITS + DEFLATE_EOBS_BITS + DEFLATE_PAD_BITS) >> 3)
88/* deflate block overhead: 3 bits for block start + 15 bits for block end + padding to nearest byte */
89
90#define DEFLATE_QUICK_LIT_MAX_BITS 9
91#define DEFLATE_QUICK_OVERHEAD(x) ((x * (DEFLATE_QUICK_LIT_MAX_BITS - 8) + 7) >> 3)
92/* deflate_quick worst-case overhead: 9 bits per literal, round up to next byte (+7) */
93
94
95#define ZLIB_WRAPLEN 6 /* zlib format overhead */
96
97 /* target dependencies */
98
99#ifdef AMIGA
100# define OS_CODE 1
101#endif
102
103#ifdef __370__
104# if __TARGET_LIB__ < 0x20000000
105# define OS_CODE 4
106# elif __TARGET_LIB__ < 0x40000000
107# define OS_CODE 11
108# else
109# define OS_CODE 8
110# endif
111#endif
112
113#if defined(ATARI) || defined(atarist)
114# define OS_CODE 5
115#endif
116
117#ifdef OS2
118# define OS_CODE 6
119#endif
120
121#if defined(MACOS) || defined(TARGET_OS_MAC)
122# define OS_CODE 7
123#endif
124
125#ifdef __acorn
126# define OS_CODE 13
127#endif
128
129#if defined(_WIN32) && !defined(__CYGWIN__)
130# define OS_CODE 10
131#endif
132
133#ifdef __APPLE__
134# define OS_CODE 19
135#endif
136
137#if (defined(_MSC_VER) && (_MSC_VER > 600))
138# define fdopen(fd, type) _fdopen(fd, type)
139#endif
140
141/* MS Visual Studio does not allow inline in C, only C++.
142 But it provides __inline instead, so use that. */
143#if defined(_MSC_VER) && !defined(inline) && !defined(__cplusplus)
144# define inline __inline
145#endif
146
147 /* common defaults */
148
149#ifndef OS_CODE
150# define OS_CODE 3 /* assume Unix */
151#endif
152
153 /* functions */
154
155/* Diagnostic functions */
156#ifdef ZLIB_DEBUG
157# include <stdio.h>
158 extern int Z_INTERNAL z_verbose;
159 extern void Z_INTERNAL z_error(char *m);
160# define Assert(cond, msg) {if (!(cond)) z_error(msg);}
161# define Trace(x) {if (z_verbose >= 0) fprintf x;}
162# define Tracev(x) {if (z_verbose > 0) fprintf x;}
163# define Tracevv(x) {if (z_verbose > 1) fprintf x;}
164# define Tracec(c, x) {if (z_verbose > 0 && (c)) fprintf x;}
165# define Tracecv(c, x) {if (z_verbose > 1 && (c)) fprintf x;}
166#else
167# define Assert(cond, msg)
168# define Trace(x)
169# define Tracev(x)
170# define Tracevv(x)
171# define Tracec(c, x)
172# define Tracecv(c, x)
173#endif
174
175void Z_INTERNAL *zng_calloc(void *opaque, unsigned items, unsigned size);
176void Z_INTERNAL zng_cfree(void *opaque, void *ptr);
177
178#define ZALLOC(strm, items, size) (*((strm)->zalloc))((strm)->opaque, (items), (size))
179#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (void *)(addr))
180#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
181
182/* Reverse the bytes in a value. Use compiler intrinsics when
183 possible to take advantage of hardware implementations. */
184#if defined(_MSC_VER) && (_MSC_VER >= 1300)
185# pragma intrinsic(_byteswap_ulong)
186# define ZSWAP16(q) _byteswap_ushort(q)
187# define ZSWAP32(q) _byteswap_ulong(q)
188# define ZSWAP64(q) _byteswap_uint64(q)
189
190#elif defined(__Clang__) || (defined(__GNUC__) && \
191 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))
192# define ZSWAP16(q) __builtin_bswap16(q)
193# define ZSWAP32(q) __builtin_bswap32(q)
194# define ZSWAP64(q) __builtin_bswap64(q)
195
196#elif defined(__GNUC__) && (__GNUC__ >= 2) && defined(__linux__)
197# include <byteswap.h>
198# define ZSWAP16(q) bswap_16(q)
199# define ZSWAP32(q) bswap_32(q)
200# define ZSWAP64(q) bswap_64(q)
201
202#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
203# include <sys/endian.h>
204# define ZSWAP16(q) bswap16(q)
205# define ZSWAP32(q) bswap32(q)
206# define ZSWAP64(q) bswap64(q)
207#elif defined(__OpenBSD__)
208# include <sys/endian.h>
209# define ZSWAP16(q) swap16(q)
210# define ZSWAP32(q) swap32(q)
211# define ZSWAP64(q) swap64(q)
212#elif defined(__INTEL_COMPILER)
213/* ICC does not provide a two byte swap. */
214# define ZSWAP16(q) ((((q) & 0xff) << 8) | (((q) & 0xff00) >> 8))
215# define ZSWAP32(q) _bswap(q)
216# define ZSWAP64(q) _bswap64(q)
217
218#else
219# define ZSWAP16(q) ((((q) & 0xff) << 8) | (((q) & 0xff00) >> 8))
220# define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
221 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
222# define ZSWAP64(q) \
223 (((q & 0xFF00000000000000u) >> 56u) | \
224 ((q & 0x00FF000000000000u) >> 40u) | \
225 ((q & 0x0000FF0000000000u) >> 24u) | \
226 ((q & 0x000000FF00000000u) >> 8u) | \
227 ((q & 0x00000000FF000000u) << 8u) | \
228 ((q & 0x0000000000FF0000u) << 24u) | \
229 ((q & 0x000000000000FF00u) << 40u) | \
230 ((q & 0x00000000000000FFu) << 56u))
231#endif
232
233/* Only enable likely/unlikely if the compiler is known to support it */
234#if (defined(__GNUC__) && (__GNUC__ >= 3)) || defined(__INTEL_COMPILER) || defined(__Clang__)
235# define LIKELY_NULL(x) __builtin_expect((x) != 0, 0)
236# define LIKELY(x) __builtin_expect(!!(x), 1)
237# define UNLIKELY(x) __builtin_expect(!!(x), 0)
238# define PREFETCH_L1(addr) __builtin_prefetch(addr, 0, 3)
239# define PREFETCH_L2(addr) __builtin_prefetch(addr, 0, 2)
240# define PREFETCH_RW(addr) __builtin_prefetch(addr, 1, 2)
241#elif defined(__WIN__)
242# include <xmmintrin.h>
243# define LIKELY_NULL(x) x
244# define LIKELY(x) x
245# define UNLIKELY(x) x
246# define PREFETCH_L1(addr) _mm_prefetch((char *) addr, _MM_HINT_T0)
247# define PREFETCH_L2(addr) _mm_prefetch((char *) addr, _MM_HINT_T1)
248# define PREFETCH_RW(addr) _mm_prefetch((char *) addr, _MM_HINT_T1)
249#else
250# define LIKELY_NULL(x) x
251# define LIKELY(x) x
252# define UNLIKELY(x) x
253# define PREFETCH_L1(addr) addr
254# define PREFETCH_L2(addr) addr
255# define PREFETCH_RW(addr) addr
256#endif /* (un)likely */
257
258#if defined(_MSC_VER)
259# define ALIGNED_(x) __declspec(align(x))
260#else
261# if defined(__GNUC__)
262# define ALIGNED_(x) __attribute__ ((aligned(x)))
263# endif
264#endif
265
266#if defined(X86_FEATURES)
267# include "arch/x86/x86.h"
268#elif defined(ARM_FEATURES)
269# include "arch/arm/arm.h"
270#elif defined(POWER_FEATURES)
271# include "arch/power/power.h"
272#endif
273
274#endif /* ZUTIL_H_ */
275