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