xref: /xnu-8796.141.3/libkern/zlib/zutil.h (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1 /*
2  * Copyright (c) 2008 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /* zutil.h -- internal interface and configuration of the compression library
29  * Copyright (C) 1995-2005 Jean-loup Gailly.
30  * For conditions of distribution and use, see copyright notice in zlib.h
31  */
32 
33 /* WARNING: this file should *not* be used by applications. It is
34    part of the implementation of the compression library and is
35    subject to change. Applications should only use zlib.h.
36  */
37 
38 /* @(#) $Id$ */
39 
40 #ifndef ZUTIL_H
41 #define ZUTIL_H
42 
43 #define ZLIB_INTERNAL
44 
45 #if KERNEL
46 #  include <libkern/zlib.h>
47 #  include <string.h>
48 #  ifndef NO_ERRNO_H
49 #    define NO_ERRNO_H 1
50 #  endif
51 #  ifndef NO_ZCFUNCS
52 #    define NO_ZCFUNCS 1
53 #  endif
54 #else
55 #  include "zlib.h"
56 #  ifdef STDC
57 #    ifndef _WIN32_WCE
58 #      include <stddef.h>
59 #    endif
60 #    include <string.h>
61 #    include <stdlib.h>
62 #  endif
63 #endif /* KERNEL */
64 
65 #ifdef NO_ERRNO_H
66 #   ifdef _WIN32_WCE
67       /* The Microsoft C Run-Time Library for Windows CE doesn't have
68        * errno.  We define it as a global variable to simplify porting.
69        * Its value is always 0 and should not be used.  We rename it to
70        * avoid conflict with other libraries that use the same workaround.
71        */
72 #     define errno z_errno
73 #   endif
74     extern int errno;
75 #else
76 #  ifndef _WIN32_WCE
77 #    include <errno.h>
78 #  endif
79 #endif
80 
81 #ifndef local
82 #  define local static
83 #endif
84 /* compile with -Dlocal if your debugger can't find static symbols */
85 
86 typedef unsigned char  uch;
87 typedef uch FAR uchf;
88 typedef unsigned short ush;
89 typedef ush FAR ushf;
90 typedef unsigned long  ulg;
91 
92 extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
93 /* (size given to avoid silly warnings with Visual C++) */
94 
95 #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
96 
97 #define ERR_RETURN(strm,err) \
98   return (strm->msg = (char*)ERR_MSG(err), (err))
99 /* To be used only when the state is known to be valid */
100 
101         /* common constants */
102 
103 #ifndef DEF_WBITS
104 #  define DEF_WBITS MAX_WBITS
105 #endif
106 /* default windowBits for decompression. MAX_WBITS is for compression only */
107 
108 #if MAX_MEM_LEVEL >= 8
109 #  define DEF_MEM_LEVEL 8
110 #else
111 #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
112 #endif
113 /* default memLevel */
114 
115 #define STORED_BLOCK 0
116 #define STATIC_TREES 1
117 #define DYN_TREES    2
118 /* The three kinds of block type */
119 
120 #define MIN_MATCH  3
121 #define MAX_MATCH  258
122 /* The minimum and maximum match lengths */
123 
124 #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
125 
126         /* target dependencies */
127 
128 #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
129 #  define OS_CODE  0x00
130 #  if defined(__TURBOC__) || defined(__BORLANDC__)
131 #    if(__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
132        /* Allow compilation with ANSI keywords only enabled */
133        void _Cdecl farfree( void *block );
134        void *_Cdecl farmalloc( unsigned long nbytes );
135 #    else
136 #      include <alloc.h>
137 #    endif
138 #  else /* MSC or DJGPP */
139 #    include <malloc.h>
140 #  endif
141 #endif
142 
143 #ifdef AMIGA
144 #  define OS_CODE  0x01
145 #endif
146 
147 #if defined(VAXC) || defined(VMS)
148 #  define OS_CODE  0x02
149 #  define F_OPEN(name, mode) \
150      fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
151 #endif
152 
153 #if defined(ATARI) || defined(atarist)
154 #  define OS_CODE  0x05
155 #endif
156 
157 #ifdef OS2
158 #  define OS_CODE  0x06
159 #  ifdef M_I86
160      #include <malloc.h>
161 #  endif
162 #endif
163 
164 #if defined(MACOS) || defined(TARGET_OS_MAC)
165 #  define OS_CODE  0x07
166 #  if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
167 #    include <unix.h> /* for fdopen */
168 #  else
169 #    ifndef fdopen
170 #      define fdopen(fd,mode) NULL /* No fdopen() */
171 #    endif
172 #  endif
173 #endif
174 
175 #ifdef TOPS20
176 #  define OS_CODE  0x0a
177 #endif
178 
179 #ifdef WIN32
180 #  ifndef __CYGWIN__  /* Cygwin is Unix, not Win32 */
181 #    define OS_CODE  0x0b
182 #  endif
183 #endif
184 
185 #ifdef __50SERIES /* Prime/PRIMOS */
186 #  define OS_CODE  0x0f
187 #endif
188 
189 #if defined(_BEOS_) || defined(RISCOS)
190 #  define fdopen(fd,mode) NULL /* No fdopen() */
191 #endif
192 
193 #if KERNEL
194     typedef long ptrdiff_t;
195 #elif (defined(_MSC_VER) && (_MSC_VER > 600))
196 #  if defined(_WIN32_WCE)
197 #    define fdopen(fd,mode) NULL /* No fdopen() */
198 #    ifndef _PTRDIFF_T_DEFINED
199        typedef int ptrdiff_t;
200 #      define _PTRDIFF_T_DEFINED
201 #    endif
202 #  else
203 #    define fdopen(fd,type)  _fdopen(fd,type)
204 #  endif
205 #endif
206 
207 
208         /* common defaults */
209 
210 #ifndef OS_CODE
211 #  define OS_CODE  0x03  /* assume Unix */
212 #endif
213 
214 #ifndef F_OPEN
215 #  define F_OPEN(name, mode) fopen((name), (mode))
216 #endif
217 
218          /* functions */
219 
220 #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
221 #  ifndef HAVE_VSNPRINTF
222 #    define HAVE_VSNPRINTF
223 #  endif
224 #endif
225 #if defined(__CYGWIN__)
226 #  ifndef HAVE_VSNPRINTF
227 #    define HAVE_VSNPRINTF
228 #  endif
229 #endif
230 #ifndef HAVE_VSNPRINTF
231 #  ifdef MSDOS
232      /* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
233         but for now we just assume it doesn't. */
234 #    define NO_vsnprintf
235 #  endif
236 #  ifdef __TURBOC__
237 #    define NO_vsnprintf
238 #  endif
239 #  ifdef WIN32
240      /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
241 #    if !defined(vsnprintf) && !defined(NO_vsnprintf)
242 #      define vsnprintf _vsnprintf
243 #    endif
244 #  endif
245 #  ifdef __SASC
246 #    define NO_vsnprintf
247 #  endif
248 #endif
249 #ifdef VMS
250 #  define NO_vsnprintf
251 #endif
252 
253 #if defined(pyr)
254 #  define NO_MEMCPY
255 #endif
256 #if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
257  /* Use our own functions for small and medium model with MSC <= 5.0.
258   * You may have to use the same strategy for Borland C (untested).
259   * The __SC__ check is for Symantec.
260   */
261 #  define NO_MEMCPY
262 #endif
263 #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
264 #  define HAVE_MEMCPY
265 #endif
266 #ifdef HAVE_MEMCPY
267 #  ifdef SMALL_MEDIUM /* MSDOS small or medium model */
268 #    define zmemcpy _fmemcpy
269 #    define zmemcmp _fmemcmp
270 #    define zmemzero(dest, len) _fmemset(dest, 0, len)
271 #  else
272 #    define zmemcpy memcpy
273 #    define zmemcmp memcmp
274 #    define zmemzero(dest, len) memset(dest, 0, len)
275 #  endif
276 #else
277    extern void zmemcpy  OF((Bytef* dest, const Bytef* source, uInt len));
278    extern int  zmemcmp  OF((const Bytef* s1, const Bytef* s2, uInt len));
279    extern void zmemzero OF((Bytef* dest, uInt len));
280 #endif
281 
282 /* Diagnostic functions */
283 #if defined(DEBUG) && !defined(KERNEL)
284 #  include <stdio.h>
285    extern int z_verbose;
286    extern void z_error    OF((char *m));
287 #  define Assert(cond,msg) {if(!(cond)) z_error(msg);}
288 #  define Trace(x) {if (z_verbose>=0) fprintf x ;}
289 #  define Tracev(x) {if (z_verbose>0) fprintf x ;}
290 #  define Tracevv(x) {if (z_verbose>1) fprintf x ;}
291 #  define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
292 #  define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
293 #else
294 #  define Assert(cond,msg)
295 #  define Trace(x)
296 #  define Tracev(x)
297 #  define Tracevv(x)
298 #  define Tracec(c,x)
299 #  define Tracecv(c,x)
300 #endif
301 #undef DEBUG
302 
303 #ifndef NO_ZCFUNCS
304 voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
305 void   zcfree  OF((voidpf opaque, voidpf ptr));
306 #endif /* NO_ZCFUNCS */
307 
308 #define ZALLOC(strm, items, size) \
309            (*((strm)->zalloc))((strm)->opaque, (items), (size))
310 #define ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
311 #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
312 
313 #endif /* ZUTIL_H */
314