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