1b838293Toomas Soome/* zutil.h -- internal interface and configuration of the compression library
2b838293Toomas Soome * Copyright (C) 1995-2016 Jean-loup Gailly, Mark Adler
3b838293Toomas Soome * For conditions of distribution and use, see copyright notice in zlib.h
4b838293Toomas Soome */
5b838293Toomas Soome
6b838293Toomas Soome/* WARNING: this file should *not* be used by applications. It is
7b838293Toomas Soome   part of the implementation of the compression library and is
8b838293Toomas Soome   subject to change. Applications should only use zlib.h.
9b838293Toomas Soome */
10b838293Toomas Soome
11b838293Toomas Soome#ifndef ZUTIL_H
12b838293Toomas Soome#define ZUTIL_H
13b838293Toomas Soome
14b838293Toomas Soome#ifdef HAVE_HIDDEN
15b838293Toomas Soome#  define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
16b838293Toomas Soome#else
17b838293Toomas Soome#  define ZLIB_INTERNAL
18b838293Toomas Soome#endif
19b838293Toomas Soome
20b838293Toomas Soome#include "zlib.h"
21b838293Toomas Soome
22b838293Toomas Soome#if defined(STDC) && !defined(Z_SOLO) && !defined(_KERNEL)
23b838293Toomas Soome#  if !(defined(_WIN32_WCE) && defined(_MSC_VER))
24b838293Toomas Soome#    include <stddef.h>
25b838293Toomas Soome#  endif
26b838293Toomas Soome#  include <string.h>
27b838293Toomas Soome#  include <stdlib.h>
28b838293Toomas Soome#endif
29b838293Toomas Soome
30b838293Toomas Soome#ifdef Z_SOLO
31b838293Toomas Soome   typedef long ptrdiff_t;  /* guess -- will be caught if guess is wrong */
32b838293Toomas Soome#endif
33b838293Toomas Soome
34b838293Toomas Soome#ifndef local
35b838293Toomas Soome#  define local static
36b838293Toomas Soome#endif
37b838293Toomas Soome/* since "static" is used to mean two completely different things in C, we
38b838293Toomas Soome   define "local" for the non-static meaning of "static", for readability
39b838293Toomas Soome   (compile with -Dlocal if your debugger can't find static symbols) */
40b838293Toomas Soome
41b838293Toomas Soometypedef unsigned char  uch;
42b838293Toomas Soometypedef uch FAR uchf;
43b838293Toomas Soometypedef unsigned short ush;
44b838293Toomas Soometypedef ush FAR ushf;
45b838293Toomas Soometypedef unsigned long  ulg;
46b838293Toomas Soome
47b838293Toomas Soomeextern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
48b838293Toomas Soome/* (size given to avoid silly warnings with Visual C++) */
49b838293Toomas Soome
50b838293Toomas Soome#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
51b838293Toomas Soome
52b838293Toomas Soome#define ERR_RETURN(strm,err) \
53b838293Toomas Soome  return (strm->msg = ERR_MSG(err), (err))
54b838293Toomas Soome/* To be used only when the state is known to be valid */
55b838293Toomas Soome
56b838293Toomas Soome        /* common constants */
57b838293Toomas Soome
58b838293Toomas Soome#ifndef DEF_WBITS
59b838293Toomas Soome#  define DEF_WBITS MAX_WBITS
60b838293Toomas Soome#endif
61b838293Toomas Soome/* default windowBits for decompression. MAX_WBITS is for compression only */
62b838293Toomas Soome
63b838293Toomas Soome#if MAX_MEM_LEVEL >= 8
64b838293Toomas Soome#  define DEF_MEM_LEVEL 8
65b838293Toomas Soome#else
66b838293Toomas Soome#  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
67b838293Toomas Soome#endif
68b838293Toomas Soome/* default memLevel */
69b838293Toomas Soome
70b838293Toomas Soome#define STORED_BLOCK 0
71b838293Toomas Soome#define STATIC_TREES 1
72b838293Toomas Soome#define DYN_TREES    2
73b838293Toomas Soome/* The three kinds of block type */
74b838293Toomas Soome
75b838293Toomas Soome#define MIN_MATCH  3
76b838293Toomas Soome#define MAX_MATCH  258
77b838293Toomas Soome/* The minimum and maximum match lengths */
78b838293Toomas Soome
79b838293Toomas Soome#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
80b838293Toomas Soome
81b838293Toomas Soome        /* target dependencies */
82b838293Toomas Soome
83b838293Toomas Soome#if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
84b838293Toomas Soome#  define OS_CODE  0x00
85b838293Toomas Soome#  ifndef Z_SOLO
86b838293Toomas Soome#    if defined(__TURBOC__) || defined(__BORLANDC__)
87b838293Toomas Soome#      if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
88b838293Toomas Soome         /* Allow compilation with ANSI keywords only enabled */
89b838293Toomas Soome         void _Cdecl farfree( void *block );
90b838293Toomas Soome         void *_Cdecl farmalloc( unsigned long nbytes );
91b838293Toomas Soome#      else
92b838293Toomas Soome#        include <alloc.h>
93b838293Toomas Soome#      endif
94b838293Toomas Soome#    else /* MSC or DJGPP */
95b838293Toomas Soome#      include <malloc.h>
96b838293Toomas Soome#    endif
97b838293Toomas Soome#  endif
98b838293Toomas Soome#endif
99b838293Toomas Soome
100b838293Toomas Soome#ifdef AMIGA
101b838293Toomas Soome#  define OS_CODE  1
102b838293Toomas Soome#endif
103b838293Toomas Soome
104b838293Toomas Soome#if defined(VAXC) || defined(VMS)
105b838293Toomas Soome#  define OS_CODE  2
106b838293Toomas Soome#  define F_OPEN(name, mode) \
107b838293Toomas Soome     fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
108b838293Toomas Soome#endif
109b838293Toomas Soome
110b838293Toomas Soome#ifdef __370__
111b838293Toomas Soome#  if __TARGET_LIB__ < 0x20000000
112b838293Toomas Soome#    define OS_CODE 4
113b838293Toomas Soome#  elif __TARGET_LIB__ < 0x40000000
114b838293Toomas Soome#    define OS_CODE 11
115b838293Toomas Soome#  else
116b838293Toomas Soome#    define OS_CODE 8
117b838293Toomas Soome#  endif
118b838293Toomas Soome#endif
119b838293Toomas Soome
120b838293Toomas Soome#if defined(ATARI) || defined(atarist)
121b838293Toomas Soome#  define OS_CODE  5
122b838293Toomas Soome#endif
123b838293Toomas Soome
124b838293Toomas Soome#ifdef OS2
125b838293Toomas Soome#  define OS_CODE  6
126b838293Toomas Soome#  if defined(M_I86) && !defined(Z_SOLO)
127b838293Toomas Soome#    include <malloc.h>
128b838293Toomas Soome#  endif
129b838293Toomas Soome#endif
130b838293Toomas Soome
131b838293Toomas Soome#if defined(MACOS) || defined(TARGET_OS_MAC)
132b838293Toomas Soome#  define OS_CODE  7
133b838293Toomas Soome#  ifndef Z_SOLO
134b838293Toomas Soome#    if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
135b838293Toomas Soome#      include <unix.h> /* for fdopen */
136b838293Toomas Soome#    else
137b838293Toomas Soome#      ifndef fdopen
138b838293Toomas Soome#        define fdopen(fd,mode) NULL /* No fdopen() */
139b838293Toomas Soome#      endif
140b838293Toomas Soome#    endif
141b838293Toomas Soome#  endif
142b838293Toomas Soome#endif
143b838293Toomas Soome
144b838293Toomas Soome#ifdef __acorn
145b838293Toomas Soome#  define OS_CODE 13
146b838293Toomas Soome#endif
147b838293Toomas Soome
148b838293Toomas Soome#if defined(WIN32) && !defined(__CYGWIN__)
149b838293Toomas Soome#  define OS_CODE  10
150b838293Toomas Soome#endif
151b838293Toomas Soome
152b838293Toomas Soome#ifdef _BEOS_
153b838293Toomas Soome#  define OS_CODE  16
154b838293Toomas Soome#endif
155b838293Toomas Soome
156b838293Toomas Soome#ifdef __TOS_OS400__
157b838293Toomas Soome#  define OS_CODE 18
158b838293Toomas Soome#endif
159b838293Toomas Soome
160b838293Toomas Soome#ifdef __APPLE__
161b838293Toomas Soome#  define OS_CODE 19
162b838293Toomas Soome#endif
163b838293Toomas Soome
164b838293Toomas Soome#if defined(_BEOS_) || defined(RISCOS)
165b838293Toomas Soome#  define fdopen(fd,mode) NULL /* No fdopen() */
166b838293Toomas Soome#endif
167b838293Toomas Soome
168b838293Toomas Soome#if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX
169b838293Toomas Soome#  if defined(_WIN32_WCE)
170b838293Toomas Soome#    define fdopen(fd,mode) NULL /* No fdopen() */
171b838293Toomas Soome#    ifndef _PTRDIFF_T_DEFINED
172b838293Toomas Soome       typedef int ptrdiff_t;
173b838293Toomas Soome#      define _PTRDIFF_T_DEFINED
174b838293Toomas Soome#    endif
175b838293Toomas Soome#  else
176b838293Toomas Soome#    define fdopen(fd,type)  _fdopen(fd,type)
177b838293Toomas Soome#  endif
178b838293Toomas Soome#endif
179b838293Toomas Soome
180b838293Toomas Soome#if defined(__BORLANDC__) && !defined(MSDOS)
181b838293Toomas Soome  #pragma warn -8004
182b838293Toomas Soome  #pragma warn -8008
183b838293Toomas Soome  #pragma warn -8066
184b838293Toomas Soome#endif
185b838293Toomas Soome
186b838293Toomas Soome/* provide prototypes for these when building zlib without LFS */
187b838293Toomas Soome#if !defined(_WIN32) && \
188b838293Toomas Soome    (!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0)
189b838293Toomas Soome    ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
190b838293Toomas Soome    ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
191b838293Toomas Soome#endif
192b838293Toomas Soome
193b838293Toomas Soome        /* common defaults */
194b838293Toomas Soome
195b838293Toomas Soome#ifndef OS_CODE
196b838293Toomas Soome#  define OS_CODE  3     /* assume Unix */
197b838293Toomas Soome#endif
198b838293Toomas Soome
199b838293Toomas Soome#ifndef F_OPEN
200b838293Toomas Soome#  define F_OPEN(name, mode) fopen((name), (mode))
201b838293Toomas Soome#endif
202b838293Toomas Soome
203b838293Toomas Soome         /* functions */
204b838293Toomas Soome
205b838293Toomas Soome#if defined(pyr) || defined(Z_SOLO)
206b838293Toomas Soome#  define NO_MEMCPY
207b838293Toomas Soome#endif
208b838293Toomas Soome#if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
209b838293Toomas Soome /* Use our own functions for small and medium model with MSC <= 5.0.
210b838293Toomas Soome  * You may have to use the same strategy for Borland C (untested).
211b838293Toomas Soome  * The __SC__ check is for Symantec.
212b838293Toomas Soome  */
213b838293Toomas Soome#  define NO_MEMCPY
214b838293Toomas Soome#endif
215b838293Toomas Soome#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
216b838293Toomas Soome#  define HAVE_MEMCPY
217b838293Toomas Soome#endif
218b838293Toomas Soome#ifdef HAVE_MEMCPY
219b838293Toomas Soome#  ifdef SMALL_MEDIUM /* MSDOS small or medium model */
220b838293Toomas Soome#    define zmemcpy _fmemcpy
221b838293Toomas Soome#    define zmemcmp _fmemcmp
222b838293Toomas Soome#    define zmemzero(dest, len) _fmemset(dest, 0, len)
223b838293Toomas Soome#  else
224b838293Toomas Soome#    define zmemcpy memcpy
225b838293Toomas Soome#    define zmemcmp memcmp
226b838293Toomas Soome#    define zmemzero(dest, len) memset(dest, 0, len)
227b838293Toomas Soome#  endif
228b838293Toomas Soome#else
229b838293Toomas Soome   void ZLIB_INTERNAL zmemcpy OF((Bytef* dest, const Bytef* source, uInt len));
230b838293Toomas Soome   int ZLIB_INTERNAL zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len));
231b838293Toomas Soome   void ZLIB_INTERNAL zmemzero OF((Bytef* dest, uInt len));
232b838293Toomas Soome#endif
233b838293Toomas Soome
234b838293Toomas Soome/* Diagnostic functions */
235b838293Toomas Soome#ifdef ZLIB_DEBUG
236b838293Toomas Soome#  include <stdio.h>
237b838293Toomas Soome   extern int ZLIB_INTERNAL z_verbose;
238b838293Toomas Soome   extern void ZLIB_INTERNAL z_error OF((char *m));
239b838293Toomas Soome#  define Assert(cond,msg) {if(!(cond)) z_error(msg);}
240b838293Toomas Soome#  define Trace(x) {if (z_verbose>=0) fprintf x ;}
241b838293Toomas Soome#  define Tracev(x) {if (z_verbose>0) fprintf x ;}
242b838293Toomas Soome#  define Tracevv(x) {if (z_verbose>1) fprintf x ;}
243b838293Toomas Soome#  define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
244b838293Toomas Soome#  define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
245b838293Toomas Soome#else
246b838293Toomas Soome#  define Assert(cond,msg)
247b838293Toomas Soome#  define Trace(x)
248b838293Toomas Soome#  define Tracev(x)
249b838293Toomas Soome#  define Tracevv(x)
250b838293Toomas Soome#  define Tracec(c,x)
251b838293Toomas Soome#  define Tracecv(c,x)
252b838293Toomas Soome#endif
253b838293Toomas Soome
254b838293Toomas Soome#ifndef Z_SOLO
255b838293Toomas Soome   voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items,
256b838293Toomas Soome                                    unsigned size));
257b838293Toomas Soome   void ZLIB_INTERNAL zcfree  OF((voidpf opaque, voidpf ptr));
258b838293Toomas Soome#endif
259b838293Toomas Soome
260b838293Toomas Soome#define ZALLOC(strm, items, size) \
261b838293Toomas Soome           (*((strm)->zalloc))((strm)->opaque, (items), (size))
262b838293Toomas Soome#define ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
263b838293Toomas Soome#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
264b838293Toomas Soome
265b838293Toomas Soome/* Reverse the bytes in a 32-bit value */
266b838293Toomas Soome#define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
267b838293Toomas Soome                    (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
268b838293Toomas Soome
269b838293Toomas Soome#endif /* ZUTIL_H */
270