| 1 |
/* zutil.h -- internal interface and configuration of the compression library |
| 2 |
* Copyright (C) 1995-2026 Jean-loup Gailly, Mark Adler |
| 3 |
* For conditions of distribution and use, see copyright notice in zlib.h |
| 4 |
*/ |
| 5 |
|
| 6 |
/* WARNING: this file should *not* be used by applications. It is |
| 7 |
part of the implementation of the compression library and is |
| 8 |
subject to change. Applications should only use zlib.h. |
| 9 |
*/ |
| 10 |
|
| 11 |
/* @(#) $Id$ */ |
| 12 |
|
| 13 |
#ifndef ZUTIL_H |
| 14 |
#define ZUTIL_H |
| 15 |
|
| 16 |
#ifdef HAVE_HIDDEN |
| 17 |
# define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) |
| 18 |
#else |
| 19 |
# define ZLIB_INTERNAL |
| 20 |
#endif |
| 21 |
|
| 22 |
#include "zlib.h" |
| 23 |
|
| 24 |
#if defined(STDC) && !defined(Z_SOLO) |
| 25 |
# if !(defined(_WIN32_WCE) && defined(_MSC_VER)) |
| 26 |
# include <stddef.h> |
| 27 |
# endif |
| 28 |
# include <string.h> |
| 29 |
# include <stdlib.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#ifndef local |
| 33 |
# define local static |
| 34 |
#endif |
| 35 |
/* since "static" is used to mean two completely different things in C, we |
| 36 |
define "local" for the non-static meaning of "static", for readability |
| 37 |
(compile with -Dlocal if your debugger can't find static symbols) */ |
| 38 |
|
| 39 |
extern const char deflate_copyright[]; |
| 40 |
extern const char inflate_copyright[]; |
| 41 |
extern const char inflate9_copyright[]; |
| 42 |
|
| 43 |
typedef unsigned char uch; |
| 44 |
typedef uch FAR uchf; |
| 45 |
typedef unsigned short ush; |
| 46 |
typedef ush FAR ushf; |
| 47 |
typedef unsigned long ulg; |
| 48 |
|
| 49 |
#if !defined(Z_U8) && !defined(Z_SOLO) && defined(STDC) |
| 50 |
# include <limits.h> |
| 51 |
# if (ULONG_MAX == 0xffffffffffffffff) |
| 52 |
# define Z_U8 unsigned long |
| 53 |
# elif (ULLONG_MAX == 0xffffffffffffffff) |
| 54 |
# define Z_U8 unsigned long long |
| 55 |
# elif (ULONG_LONG_MAX == 0xffffffffffffffff) |
| 56 |
# define Z_U8 unsigned long long |
| 57 |
# elif (UINT_MAX == 0xffffffffffffffff) |
| 58 |
# define Z_U8 unsigned |
| 59 |
# endif |
| 60 |
#endif |
| 61 |
|
| 62 |
extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ |
| 63 |
/* (size given to avoid silly warnings with Visual C++) */ |
| 64 |
|
| 65 |
#define ERR_MSG(err) z_errmsg[(err) < -6 || (err) > 2 ? 9 : 2 - (err)] |
| 66 |
|
| 67 |
#define ERR_RETURN(strm,err) \ |
| 68 |
return (strm->msg = ERR_MSG(err), (err)) |
| 69 |
/* To be used only when the state is known to be valid */ |
| 70 |
|
| 71 |
/* common constants */ |
| 72 |
#if MAX_WBITS < 9 || MAX_WBITS > 15 |
| 73 |
# error MAX_WBITS must be in 9..15 |
| 74 |
#endif |
| 75 |
#ifndef DEF_WBITS |
| 76 |
# define DEF_WBITS MAX_WBITS |
| 77 |
#endif |
| 78 |
/* default windowBits for decompression. MAX_WBITS is for compression only */ |
| 79 |
|
| 80 |
#if MAX_MEM_LEVEL >= 8 |
| 81 |
# define DEF_MEM_LEVEL 8 |
| 82 |
#else |
| 83 |
# define DEF_MEM_LEVEL MAX_MEM_LEVEL |
| 84 |
#endif |
| 85 |
/* default memLevel */ |
| 86 |
|
| 87 |
#define STORED_BLOCK 0 |
| 88 |
#define STATIC_TREES 1 |
| 89 |
#define DYN_TREES 2 |
| 90 |
/* The three kinds of block type */ |
| 91 |
|
| 92 |
#define MIN_MATCH 3 |
| 93 |
#define MAX_MATCH 258 |
| 94 |
/* The minimum and maximum match lengths */ |
| 95 |
|
| 96 |
#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */ |
| 97 |
|
| 98 |
/* target dependencies */ |
| 99 |
|
| 100 |
#if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32)) |
| 101 |
# define OS_CODE 0x00 |
| 102 |
# ifndef Z_SOLO |
| 103 |
# if defined(__TURBOC__) || defined(__BORLANDC__) |
| 104 |
# if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__)) |
| 105 |
/* Allow compilation with ANSI keywords only enabled */ |
| 106 |
void _Cdecl farfree( void *block ); |
| 107 |
void *_Cdecl farmalloc( unsigned long nbytes ); |
| 108 |
# else |
| 109 |
# include <alloc.h> |
| 110 |
# endif |
| 111 |
# else /* MSC or DJGPP */ |
| 112 |
# include <malloc.h> |
| 113 |
# endif |
| 114 |
# endif |
| 115 |
#endif |
| 116 |
|
| 117 |
#ifdef AMIGA |
| 118 |
# define OS_CODE 1 |
| 119 |
#endif |
| 120 |
|
| 121 |
#if defined(VAXC) || defined(VMS) |
| 122 |
# define OS_CODE 2 |
| 123 |
# define F_OPEN(name, mode) \ |
| 124 |
fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512") |
| 125 |
#endif |
| 126 |
|
| 127 |
#ifdef __370__ |
| 128 |
# if __TARGET_LIB__ < 0x20000000 |
| 129 |
# define OS_CODE 4 |
| 130 |
# elif __TARGET_LIB__ < 0x40000000 |
| 131 |
# define OS_CODE 11 |
| 132 |
# else |
| 133 |
# define OS_CODE 8 |
| 134 |
# endif |
| 135 |
#endif |
| 136 |
|
| 137 |
#if defined(ATARI) || defined(atarist) |
| 138 |
# define OS_CODE 5 |
| 139 |
#endif |
| 140 |
|
| 141 |
#ifdef OS2 |
| 142 |
# define OS_CODE 6 |
| 143 |
# if defined(M_I86) && !defined(Z_SOLO) |
| 144 |
# include <malloc.h> |
| 145 |
# endif |
| 146 |
#endif |
| 147 |
|
| 148 |
#if defined(MACOS) |
| 149 |
# define OS_CODE 7 |
| 150 |
#endif |
| 151 |
|
| 152 |
#if defined(__acorn) || defined(__riscos) |
| 153 |
# define OS_CODE 13 |
| 154 |
#endif |
| 155 |
|
| 156 |
#if defined(WIN32) && !defined(__CYGWIN__) |
| 157 |
# define OS_CODE 10 |
| 158 |
#endif |
| 159 |
|
| 160 |
#ifdef _BEOS_ |
| 161 |
# define OS_CODE 16 |
| 162 |
#endif |
| 163 |
|
| 164 |
#ifdef __TOS_OS400__ |
| 165 |
# define OS_CODE 18 |
| 166 |
#endif |
| 167 |
|
| 168 |
#ifdef __APPLE__ |
| 169 |
# define OS_CODE 19 |
| 170 |
#endif |
| 171 |
|
| 172 |
#if defined(__BORLANDC__) && !defined(MSDOS) |
| 173 |
#pragma warn -8004 |
| 174 |
#pragma warn -8008 |
| 175 |
#pragma warn -8066 |
| 176 |
#endif |
| 177 |
|
| 178 |
/* provide prototypes for these when building zlib without LFS */ |
| 179 |
#ifndef Z_LARGE64 |
| 180 |
ZEXTERN uLong ZEXPORT adler32_combine64(uLong, uLong, z_off64_t); |
| 181 |
ZEXTERN uLong ZEXPORT crc32_combine64(uLong, uLong, z_off64_t); |
| 182 |
ZEXTERN uLong ZEXPORT crc32_combine_gen64(z_off64_t); |
| 183 |
#endif |
| 184 |
|
| 185 |
/* common defaults */ |
| 186 |
|
| 187 |
#ifndef OS_CODE |
| 188 |
# define OS_CODE 3 /* assume Unix */ |
| 189 |
#endif |
| 190 |
|
| 191 |
#ifndef F_OPEN |
| 192 |
# define F_OPEN(name, mode) fopen((name), (mode)) |
| 193 |
#endif |
| 194 |
|
| 195 |
/* functions */ |
| 196 |
|
| 197 |
#if defined(pyr) || defined(Z_SOLO) |
| 198 |
# define NO_MEMCPY |
| 199 |
#endif |
| 200 |
#if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__) |
| 201 |
/* Use our own functions for small and medium model with MSC <= 5.0. |
| 202 |
* You may have to use the same strategy for Borland C (untested). |
| 203 |
* The __SC__ check is for Symantec. |
| 204 |
*/ |
| 205 |
# define NO_MEMCPY |
| 206 |
#endif |
| 207 |
#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY) |
| 208 |
# define HAVE_MEMCPY |
| 209 |
#endif |
| 210 |
#ifdef HAVE_MEMCPY |
| 211 |
# ifdef SMALL_MEDIUM /* MSDOS small or medium model */ |
| 212 |
# define zmemcpy _fmemcpy |
| 213 |
# define zmemcmp _fmemcmp |
| 214 |
# define zmemzero(dest, len) _fmemset(dest, 0, len) |
| 215 |
# else |
| 216 |
# define zmemcpy memcpy |
| 217 |
# define zmemcmp memcmp |
| 218 |
# define zmemzero(dest, len) memset(dest, 0, len) |
| 219 |
# endif |
| 220 |
#else |
| 221 |
void ZLIB_INTERNAL zmemcpy(void FAR *, const void FAR *, z_size_t); |
| 222 |
int ZLIB_INTERNAL zmemcmp(const void FAR *, const void FAR *, z_size_t); |
| 223 |
void ZLIB_INTERNAL zmemzero(void FAR *, z_size_t); |
| 224 |
#endif |
| 225 |
|
| 226 |
/* Diagnostic functions */ |
| 227 |
#ifdef ZLIB_DEBUG |
| 228 |
# include <stdio.h> |
| 229 |
extern int ZLIB_INTERNAL z_verbose; |
| 230 |
extern void ZLIB_INTERNAL z_error(char *m); |
| 231 |
# define Assert(cond,msg) {if(!(cond)) z_error(msg);} |
| 232 |
# define Trace(x) {if (z_verbose>=0) fprintf x ;} |
| 233 |
# define Tracev(x) {if (z_verbose>0) fprintf x ;} |
| 234 |
# define Tracevv(x) {if (z_verbose>1) fprintf x ;} |
| 235 |
# define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;} |
| 236 |
# define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;} |
| 237 |
#else |
| 238 |
# define Assert(cond,msg) |
| 239 |
# define Trace(x) |
| 240 |
# define Tracev(x) |
| 241 |
# define Tracevv(x) |
| 242 |
# define Tracec(c,x) |
| 243 |
# define Tracecv(c,x) |
| 244 |
#endif |
| 245 |
|
| 246 |
#ifndef Z_SOLO |
| 247 |
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, |
| 248 |
unsigned size); |
| 249 |
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr); |
| 250 |
#endif |
| 251 |
|
| 252 |
#define ZALLOC(strm, items, size) \ |
| 253 |
(*((strm)->zalloc))((strm)->opaque, (items), (size)) |
| 254 |
#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr)) |
| 255 |
#define TRY_FREE(s, p) {if (p) ZFREE(s, p);} |
| 256 |
|
| 257 |
/* Reverse the bytes in a 32-bit value */ |
| 258 |
#define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ |
| 259 |
(((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) |
| 260 |
|
| 261 |
#ifdef Z_ONCE |
| 262 |
/* |
| 263 |
Create a local z_once() function depending on the availability of atomics. |
| 264 |
*/ |
| 265 |
|
| 266 |
/* Check for the availability of atomics. */ |
| 267 |
#if defined(__STDC__) && __STDC_VERSION__ >= 201112L && \ |
| 268 |
!defined(__STDC_NO_ATOMICS__) |
| 269 |
|
| 270 |
#include <stdatomic.h> |
| 271 |
typedef struct { |
| 272 |
atomic_flag begun; |
| 273 |
atomic_int done; |
| 274 |
} z_once_t; |
| 275 |
#define Z_ONCE_INIT {ATOMIC_FLAG_INIT, 0} |
| 276 |
|
| 277 |
/* |
| 278 |
Run the provided init() function exactly once, even if multiple threads |
| 279 |
invoke once() at the same time. The state must be a once_t initialized with |
| 280 |
Z_ONCE_INIT. |
| 281 |
*/ |
| 282 |
local void z_once(z_once_t *state, void (*init)(void)) { |
| 283 |
if (!atomic_load(&state->done)) { |
| 284 |
if (atomic_flag_test_and_set(&state->begun)) |
| 285 |
while (!atomic_load(&state->done)) |
| 286 |
; |
| 287 |
else { |
| 288 |
init(); |
| 289 |
atomic_store(&state->done, 1); |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
#else /* no atomics */ |
| 295 |
|
| 296 |
#warning zlib not thread-safe |
| 297 |
|
| 298 |
typedef struct z_once_s { |
| 299 |
volatile int begun; |
| 300 |
volatile int done; |
| 301 |
} z_once_t; |
| 302 |
#define Z_ONCE_INIT {0, 0} |
| 303 |
|
| 304 |
/* Test and set. Alas, not atomic, but tries to limit the period of |
| 305 |
vulnerability. */ |
| 306 |
local int test_and_set(int volatile *flag) { |
| 307 |
int was; |
| 308 |
|
| 309 |
was = *flag; |
| 310 |
*flag = 1; |
| 311 |
return was; |
| 312 |
} |
| 313 |
|
| 314 |
/* Run the provided init() function once. This is not thread-safe. */ |
| 315 |
local void z_once(z_once_t *state, void (*init)(void)) { |
| 316 |
if (!state->done) { |
| 317 |
if (test_and_set(&state->begun)) |
| 318 |
while (!state->done) |
| 319 |
; |
| 320 |
else { |
| 321 |
init(); |
| 322 |
state->done = 1; |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
#endif /* ?atomics */ |
| 328 |
|
| 329 |
#endif /* Z_ONCE */ |
| 330 |
|
| 331 |
#endif /* ZUTIL_H */ |