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