xref: /openbsd/sys/lib/libz/zutil.h (revision 09467b48)
1 /*	$OpenBSD: zutil.h,v 1.18 2011/07/07 02:57:24 deraadt Exp $	*/
2 /* zutil.h -- internal interface and configuration of the compression library
3  * Copyright (C) 1995-2005 Jean-loup Gailly.
4  * For conditions of distribution and use, see copyright notice in zlib.h
5  */
6 
7 /* WARNING: this file should *not* be used by applications. It is
8    part of the implementation of the compression library and is
9    subject to change. Applications should only use zlib.h.
10  */
11 
12 
13 #ifndef ZUTIL_H
14 #define ZUTIL_H
15 
16 #define ZLIB_INTERNAL
17 #include "zlib.h"
18 
19 #ifdef _KERNEL
20 #include <sys/param.h>
21 #include <sys/systm.h>
22 #define MY_ZCALLOC
23 #else
24 
25 #ifdef _STANDALONE
26 #include <lib/libsa/stand.h>
27 #else
28 #ifdef STDC
29 #  ifndef _WIN32_WCE
30 #    include <stddef.h>
31 #  endif
32 #  include <string.h>
33 #  include <stdlib.h>
34 #endif
35 #ifdef NO_ERRNO_H
36 #   ifdef _WIN32_WCE
37       /* The Microsoft C Run-Time Library for Windows CE doesn't have
38        * errno.  We define it as a global variable to simplify porting.
39        * Its value is always 0 and should not be used.  We rename it to
40        * avoid conflict with other libraries that use the same workaround.
41        */
42 #     define errno z_errno
43 #   endif
44     extern int errno;
45 #else
46 #  ifndef _WIN32_WCE
47 #    include <errno.h>
48 #  endif
49 #endif
50 #endif
51 
52 #endif
53 
54 #ifndef local
55 #  define local static
56 #endif
57 /* compile with -Dlocal if your debugger can't find static symbols */
58 
59 typedef unsigned char  uch;
60 typedef uch FAR uchf;
61 typedef unsigned short ush;
62 typedef ush FAR ushf;
63 typedef unsigned long  ulg;
64 
65 extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
66 /* (size given to avoid silly warnings with Visual C++) */
67 
68 #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
69 
70 #define ERR_RETURN(strm,err) \
71   return (strm->msg = (char*)ERR_MSG(err), (err))
72 /* To be used only when the state is known to be valid */
73 
74         /* common constants */
75 
76 #ifndef DEF_WBITS
77 #  define DEF_WBITS MAX_WBITS
78 #endif
79 /* default windowBits for decompression. MAX_WBITS is for compression only */
80 
81 #if MAX_MEM_LEVEL >= 8
82 #  define DEF_MEM_LEVEL 8
83 #else
84 #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
85 #endif
86 /* default memLevel */
87 
88 #define STORED_BLOCK 0
89 #define STATIC_TREES 1
90 #define DYN_TREES    2
91 /* The three kinds of block type */
92 
93 #define MIN_MATCH  3
94 #define MAX_MATCH  258
95 /* The minimum and maximum match lengths */
96 
97 #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
98 
99         /* target dependencies */
100 
101 #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
102 #  define OS_CODE  0x00
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 
116 #ifdef AMIGA
117 #  define OS_CODE  0x01
118 #endif
119 
120 #if defined(VAXC) || defined(VMS)
121 #  define OS_CODE  0x02
122 #  define F_OPEN(name, mode) \
123      fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
124 #endif
125 
126 #if defined(ATARI) || defined(atarist)
127 #  define OS_CODE  0x05
128 #endif
129 
130 #ifdef OS2
131 #  define OS_CODE  0x06
132 #  ifdef M_I86
133      #include <malloc.h>
134 #  endif
135 #endif
136 
137 #if defined(MACOS) || defined(TARGET_OS_MAC)
138 #  define OS_CODE  0x07
139 #  if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
140 #    include <unix.h> /* for fdopen */
141 #  else
142 #    ifndef fdopen
143 #      define fdopen(fd,mode) NULL /* No fdopen() */
144 #    endif
145 #  endif
146 #endif
147 
148 #ifdef TOPS20
149 #  define OS_CODE  0x0a
150 #endif
151 
152 #ifdef WIN32
153 #  ifndef __CYGWIN__  /* Cygwin is Unix, not Win32 */
154 #    define OS_CODE  0x0b
155 #  endif
156 #endif
157 
158 #ifdef __50SERIES /* Prime/PRIMOS */
159 #  define OS_CODE  0x0f
160 #endif
161 
162 #if defined(_BEOS_) || defined(RISCOS)
163 #  define fdopen(fd,mode) NULL /* No fdopen() */
164 #endif
165 
166 #if (defined(_MSC_VER) && (_MSC_VER > 600))
167 #  if defined(_WIN32_WCE)
168 #    define fdopen(fd,mode) NULL /* No fdopen() */
169 #    ifndef _PTRDIFF_T_DEFINED
170        typedef int ptrdiff_t;
171 #      define _PTRDIFF_T_DEFINED
172 #    endif
173 #  else
174 #    define fdopen(fd,type)  _fdopen(fd,type)
175 #  endif
176 #endif
177 
178         /* common defaults */
179 
180 #ifndef OS_CODE
181 #  define OS_CODE  0x03  /* assume Unix */
182 #endif
183 
184 #ifndef F_OPEN
185 #  define F_OPEN(name, mode) fopen((name), (mode))
186 #endif
187 
188          /* functions */
189 
190 #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
191 #  ifndef HAVE_VSNPRINTF
192 #    define HAVE_VSNPRINTF
193 #  endif
194 #endif
195 #if defined(__CYGWIN__)
196 #  ifndef HAVE_VSNPRINTF
197 #    define HAVE_VSNPRINTF
198 #  endif
199 #endif
200 #ifndef HAVE_VSNPRINTF
201 #  ifdef MSDOS
202      /* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
203         but for now we just assume it doesn't. */
204 #    define NO_vsnprintf
205 #  endif
206 #  ifdef __TURBOC__
207 #    define NO_vsnprintf
208 #  endif
209 #  ifdef WIN32
210      /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
211 #    if !defined(vsnprintf) && !defined(NO_vsnprintf)
212 #      define vsnprintf _vsnprintf
213 #    endif
214 #  endif
215 #  ifdef __SASC
216 #    define NO_vsnprintf
217 #  endif
218 #endif
219 #ifdef VMS
220 #  define NO_vsnprintf
221 #endif
222 
223 #if defined(pyr)
224 #  define NO_MEMCPY
225 #endif
226 #if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
227  /* Use our own functions for small and medium model with MSC <= 5.0.
228   * You may have to use the same strategy for Borland C (untested).
229   * The __SC__ check is for Symantec.
230   */
231 #  define NO_MEMCPY
232 #endif
233 #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
234 #  define HAVE_MEMCPY
235 #endif
236 #ifdef HAVE_MEMCPY
237 #  ifdef SMALL_MEDIUM /* MSDOS small or medium model */
238 #    define zmemcpy _fmemcpy
239 #    define zmemcmp _fmemcmp
240 #    define zmemzero(dest, len) _fmemset(dest, 0, len)
241 #  else
242 #    define zmemcpy memcpy
243 #    define zmemcmp memcmp
244 #    define zmemzero(dest, len) memset(dest, 0, len)
245 #  endif
246 #else
247    extern void zmemcpy  OF((Bytef* dest, const Bytef* source, uInt len));
248    extern int  zmemcmp  OF((const Bytef* s1, const Bytef* s2, uInt len));
249    extern void zmemzero OF((Bytef* dest, uInt len));
250 #endif
251 
252 /* Diagnostic functions */
253 #ifdef DEBUG_LIBZ
254 #  include <stdio.h>
255    extern int z_verbose;
256    extern void z_error    OF((char *m));
257 #  define Assert(cond,msg) {if(!(cond)) z_error(msg);}
258 #  define Trace(x) {if (z_verbose>=0) fprintf x ;}
259 #  define Tracev(x) {if (z_verbose>0) fprintf x ;}
260 #  define Tracevv(x) {if (z_verbose>1) fprintf x ;}
261 #  define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
262 #  define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
263 #else
264 #  define Assert(cond,msg)
265 #  define Trace(x)
266 #  define Tracev(x)
267 #  define Tracevv(x)
268 #  define Tracec(c,x)
269 #  define Tracecv(c,x)
270 #endif
271 
272 
273 voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
274 void   zcfree  OF((voidpf opaque, voidpf ptr));
275 
276 #define ZALLOC(strm, items, size) \
277            (*((strm)->zalloc))((strm)->opaque, (items), (size))
278 #define ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
279 #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
280 
281 #endif /* ZUTIL_H */
282