xref: /netbsd-src/common/dist/zlib/zutil.h (revision 96c3282121aac2037abbd5952fd638784deb5ab1)
1*96c32821Schristos /*	$NetBSD: zutil.h,v 1.8 2024/09/22 19:12:28 christos Exp $	*/
2aaf4ece6Schristos 
3aaf4ece6Schristos /* zutil.h -- internal interface and configuration of the compression library
4*96c32821Schristos  * Copyright (C) 1995-2024 Jean-loup Gailly, Mark Adler
5aaf4ece6Schristos  * For conditions of distribution and use, see copyright notice in zlib.h
6aaf4ece6Schristos  */
7aaf4ece6Schristos 
8aaf4ece6Schristos /* WARNING: this file should *not* be used by applications. It is
9aaf4ece6Schristos    part of the implementation of the compression library and is
10aaf4ece6Schristos    subject to change. Applications should only use zlib.h.
11aaf4ece6Schristos  */
12aaf4ece6Schristos 
133b1253e8Schristos /* @(#) Id */
14aaf4ece6Schristos 
15aaf4ece6Schristos #ifndef ZUTIL_H
16aaf4ece6Schristos #define ZUTIL_H
17aaf4ece6Schristos 
186db8c6e9Schristos #ifdef HAVE_HIDDEN
196db8c6e9Schristos #  define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
206db8c6e9Schristos #else
21aaf4ece6Schristos #  define ZLIB_INTERNAL
226db8c6e9Schristos #endif
236db8c6e9Schristos 
24aaf4ece6Schristos #include "zlib.h"
25aaf4ece6Schristos 
266db8c6e9Schristos #if defined(STDC) && !defined(Z_SOLO)
276db8c6e9Schristos #  if defined(_KERNEL) || defined(_STANDALONE)
28c08686ceSchristos #    include <lib/libkern/libkern.h>
29c08686ceSchristos #  else
306db8c6e9Schristos #    if !(defined(_WIN32_WCE) && defined(_MSC_VER))
31aaf4ece6Schristos #      include <stddef.h>
32aaf4ece6Schristos #    endif
33aaf4ece6Schristos #    include <string.h>
34aaf4ece6Schristos #    include <stdlib.h>
35aaf4ece6Schristos #  endif
36aaf4ece6Schristos #endif
376db8c6e9Schristos 
38aaf4ece6Schristos #ifndef local
39aaf4ece6Schristos #  define local static
40aaf4ece6Schristos #endif
416db8c6e9Schristos /* since "static" is used to mean two completely different things in C, we
426db8c6e9Schristos    define "local" for the non-static meaning of "static", for readability
436db8c6e9Schristos    (compile with -Dlocal if your debugger can't find static symbols) */
44aaf4ece6Schristos 
45aaf4ece6Schristos typedef unsigned char  uch;
46aaf4ece6Schristos typedef uch FAR uchf;
47aaf4ece6Schristos typedef unsigned short ush;
48aaf4ece6Schristos typedef ush FAR ushf;
49aaf4ece6Schristos typedef unsigned long  ulg;
50aaf4ece6Schristos 
513b1253e8Schristos #if !defined(Z_U8) && !defined(Z_SOLO) && defined(STDC)
52eb31298aSchristos # if defined(_KERNEL) || defined(_STANDALONE)
53eb31298aSchristos #  ifdef _LP64
54eb31298aSchristos #   define Z_U8 unsigned long
55eb31298aSchristos #  else
56eb31298aSchristos #   define Z_U8 unsigned long long
57eb31298aSchristos #  endif
58eb31298aSchristos # else
593b1253e8Schristos #  include <limits.h>
603b1253e8Schristos #  if (ULONG_MAX == 0xffffffffffffffff)
613b1253e8Schristos #    define Z_U8 unsigned long
623b1253e8Schristos #  elif (ULLONG_MAX == 0xffffffffffffffff)
633b1253e8Schristos #    define Z_U8 unsigned long long
643b1253e8Schristos #  elif (UINT_MAX == 0xffffffffffffffff)
653b1253e8Schristos #    define Z_U8 unsigned
663b1253e8Schristos #  endif
673b1253e8Schristos # endif
68eb31298aSchristos #endif
693b1253e8Schristos 
706db8c6e9Schristos extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
71aaf4ece6Schristos /* (size given to avoid silly warnings with Visual C++) */
72aaf4ece6Schristos 
73*96c32821Schristos #define ERR_MSG(err) z_errmsg[(err) < -6 || (err) > 2 ? 9 : 2 - (err)]
74aaf4ece6Schristos 
75aaf4ece6Schristos #define ERR_RETURN(strm,err) \
76b85ba082Schristos   return (strm->msg = __UNCONST(ERR_MSG(err)), (err))
77aaf4ece6Schristos /* To be used only when the state is known to be valid */
78aaf4ece6Schristos 
79aaf4ece6Schristos         /* common constants */
80aaf4ece6Schristos 
81aaf4ece6Schristos #ifndef DEF_WBITS
82aaf4ece6Schristos #  define DEF_WBITS MAX_WBITS
83aaf4ece6Schristos #endif
84aaf4ece6Schristos /* default windowBits for decompression. MAX_WBITS is for compression only */
85aaf4ece6Schristos 
86aaf4ece6Schristos #if MAX_MEM_LEVEL >= 8
87aaf4ece6Schristos #  define DEF_MEM_LEVEL 8
88aaf4ece6Schristos #else
89aaf4ece6Schristos #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
90aaf4ece6Schristos #endif
91aaf4ece6Schristos /* default memLevel */
92aaf4ece6Schristos 
93aaf4ece6Schristos #define STORED_BLOCK 0
94aaf4ece6Schristos #define STATIC_TREES 1
95aaf4ece6Schristos #define DYN_TREES    2
96aaf4ece6Schristos /* The three kinds of block type */
97aaf4ece6Schristos 
98aaf4ece6Schristos #define MIN_MATCH  3
99aaf4ece6Schristos #define MAX_MATCH  258
100aaf4ece6Schristos /* The minimum and maximum match lengths */
101aaf4ece6Schristos 
102aaf4ece6Schristos #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
103aaf4ece6Schristos 
104aaf4ece6Schristos         /* target dependencies */
105aaf4ece6Schristos 
106aaf4ece6Schristos #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
107aaf4ece6Schristos #  define OS_CODE  0x00
1086db8c6e9Schristos #  ifndef Z_SOLO
109aaf4ece6Schristos #    if defined(__TURBOC__) || defined(__BORLANDC__)
110aaf4ece6Schristos #      if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
111aaf4ece6Schristos          /* Allow compilation with ANSI keywords only enabled */
112aaf4ece6Schristos          void _Cdecl farfree( void *block );
113aaf4ece6Schristos          void *_Cdecl farmalloc( unsigned long nbytes );
114aaf4ece6Schristos #      else
115aaf4ece6Schristos #        include <alloc.h>
116aaf4ece6Schristos #      endif
117aaf4ece6Schristos #    else /* MSC or DJGPP */
118aaf4ece6Schristos #      include <malloc.h>
119aaf4ece6Schristos #    endif
120aaf4ece6Schristos #  endif
1216db8c6e9Schristos #endif
122aaf4ece6Schristos 
123aaf4ece6Schristos #ifdef AMIGA
1246db8c6e9Schristos #  define OS_CODE  1
125aaf4ece6Schristos #endif
126aaf4ece6Schristos 
127aaf4ece6Schristos #if defined(VAXC) || defined(VMS)
1286db8c6e9Schristos #  define OS_CODE  2
129aaf4ece6Schristos #  define F_OPEN(name, mode) \
130aaf4ece6Schristos      fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
131aaf4ece6Schristos #endif
132aaf4ece6Schristos 
1336db8c6e9Schristos #ifdef __370__
1346db8c6e9Schristos #  if __TARGET_LIB__ < 0x20000000
1356db8c6e9Schristos #    define OS_CODE 4
1366db8c6e9Schristos #  elif __TARGET_LIB__ < 0x40000000
1376db8c6e9Schristos #    define OS_CODE 11
1386db8c6e9Schristos #  else
1396db8c6e9Schristos #    define OS_CODE 8
1406db8c6e9Schristos #  endif
1416db8c6e9Schristos #endif
1426db8c6e9Schristos 
143aaf4ece6Schristos #if defined(ATARI) || defined(atarist)
1446db8c6e9Schristos #  define OS_CODE  5
145aaf4ece6Schristos #endif
146aaf4ece6Schristos 
147aaf4ece6Schristos #ifdef OS2
1486db8c6e9Schristos #  define OS_CODE  6
1496db8c6e9Schristos #  if defined(M_I86) && !defined(Z_SOLO)
150aaf4ece6Schristos #    include <malloc.h>
151aaf4ece6Schristos #  endif
152aaf4ece6Schristos #endif
153aaf4ece6Schristos 
154*96c32821Schristos #if defined(MACOS)
1556db8c6e9Schristos #  define OS_CODE  7
156aaf4ece6Schristos #endif
157aaf4ece6Schristos 
1586db8c6e9Schristos #ifdef __acorn
1596db8c6e9Schristos #  define OS_CODE 13
160aaf4ece6Schristos #endif
161aaf4ece6Schristos 
1626db8c6e9Schristos #if defined(WIN32) && !defined(__CYGWIN__)
1636db8c6e9Schristos #  define OS_CODE  10
1646db8c6e9Schristos #endif
1656db8c6e9Schristos 
1666db8c6e9Schristos #ifdef _BEOS_
1676db8c6e9Schristos #  define OS_CODE  16
1686db8c6e9Schristos #endif
1696db8c6e9Schristos 
1706db8c6e9Schristos #ifdef __TOS_OS400__
1716db8c6e9Schristos #  define OS_CODE 18
1726db8c6e9Schristos #endif
1736db8c6e9Schristos 
1746db8c6e9Schristos #ifdef __APPLE__
1756db8c6e9Schristos #  define OS_CODE 19
176aaf4ece6Schristos #endif
177aaf4ece6Schristos 
1786db8c6e9Schristos #if defined(__BORLANDC__) && !defined(MSDOS)
1796db8c6e9Schristos   #pragma warn -8004
1806db8c6e9Schristos   #pragma warn -8008
1816db8c6e9Schristos   #pragma warn -8066
1826db8c6e9Schristos #endif
1836db8c6e9Schristos 
1846db8c6e9Schristos /* provide prototypes for these when building zlib without LFS */
1856db8c6e9Schristos #if !defined(_WIN32) && \
1866db8c6e9Schristos     (!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0)
187*96c32821Schristos     ZEXTERN uLong ZEXPORT adler32_combine64(uLong, uLong, z_off_t);
188*96c32821Schristos     ZEXTERN uLong ZEXPORT crc32_combine64(uLong, uLong, z_off_t);
189*96c32821Schristos     ZEXTERN uLong ZEXPORT crc32_combine_gen64(z_off_t);
1906db8c6e9Schristos #endif
1916db8c6e9Schristos 
192aaf4ece6Schristos         /* common defaults */
193aaf4ece6Schristos 
194aaf4ece6Schristos #ifndef OS_CODE
1956db8c6e9Schristos #  define OS_CODE  3     /* assume Unix */
196aaf4ece6Schristos #endif
197aaf4ece6Schristos 
198aaf4ece6Schristos #ifndef F_OPEN
199aaf4ece6Schristos #  define F_OPEN(name, mode) fopen((name), (mode))
200aaf4ece6Schristos #endif
201aaf4ece6Schristos 
202aaf4ece6Schristos          /* functions */
203aaf4ece6Schristos 
2046db8c6e9Schristos #if defined(pyr) || defined(Z_SOLO)
205aaf4ece6Schristos #  define NO_MEMCPY
206aaf4ece6Schristos #endif
207aaf4ece6Schristos #if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
208aaf4ece6Schristos  /* Use our own functions for small and medium model with MSC <= 5.0.
209aaf4ece6Schristos   * You may have to use the same strategy for Borland C (untested).
210aaf4ece6Schristos   * The __SC__ check is for Symantec.
211aaf4ece6Schristos   */
212aaf4ece6Schristos #  define NO_MEMCPY
213aaf4ece6Schristos #endif
214aaf4ece6Schristos #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
215aaf4ece6Schristos #  define HAVE_MEMCPY
216aaf4ece6Schristos #endif
217aaf4ece6Schristos #ifdef HAVE_MEMCPY
218aaf4ece6Schristos #  ifdef SMALL_MEDIUM /* MSDOS small or medium model */
219aaf4ece6Schristos #    define zmemcpy _fmemcpy
220aaf4ece6Schristos #    define zmemcmp _fmemcmp
221aaf4ece6Schristos #    define zmemzero(dest, len) _fmemset(dest, 0, len)
222aaf4ece6Schristos #  else
223aaf4ece6Schristos #    define zmemcpy memcpy
224aaf4ece6Schristos #    define zmemcmp memcmp
225aaf4ece6Schristos #    define zmemzero(dest, len) memset(dest, 0, len)
226aaf4ece6Schristos #  endif
227aaf4ece6Schristos #else
228*96c32821Schristos    void ZLIB_INTERNAL zmemcpy(Bytef* dest, const Bytef* source, uInt len);
229*96c32821Schristos    int ZLIB_INTERNAL zmemcmp(const Bytef* s1, const Bytef* s2, uInt len);
230*96c32821Schristos    void ZLIB_INTERNAL zmemzero(Bytef* dest, uInt len);
231aaf4ece6Schristos #endif
232aaf4ece6Schristos 
233aaf4ece6Schristos /* Diagnostic functions */
2346db8c6e9Schristos #ifdef ZLIB_DEBUG
235aaf4ece6Schristos #  include <stdio.h>
2366db8c6e9Schristos    extern int ZLIB_INTERNAL z_verbose;
237*96c32821Schristos    extern void ZLIB_INTERNAL z_error(char *m);
238aaf4ece6Schristos #  define Assert(cond,msg) {if(!(cond)) z_error(msg);}
239aaf4ece6Schristos #  define Trace(x) {if (z_verbose>=0) fprintf x ;}
240aaf4ece6Schristos #  define Tracev(x) {if (z_verbose>0) fprintf x ;}
241aaf4ece6Schristos #  define Tracevv(x) {if (z_verbose>1) fprintf x ;}
242aaf4ece6Schristos #  define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
243aaf4ece6Schristos #  define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
244aaf4ece6Schristos #else
245aaf4ece6Schristos #  define Assert(cond,msg)
246aaf4ece6Schristos #  define Trace(x)
247aaf4ece6Schristos #  define Tracev(x)
248aaf4ece6Schristos #  define Tracevv(x)
249aaf4ece6Schristos #  define Tracec(c,x)
250aaf4ece6Schristos #  define Tracecv(c,x)
251aaf4ece6Schristos #endif
252aaf4ece6Schristos 
2536db8c6e9Schristos #ifndef Z_SOLO
254*96c32821Schristos    voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items,
255*96c32821Schristos                                 unsigned size);
256*96c32821Schristos    void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr);
2576db8c6e9Schristos #endif
258aaf4ece6Schristos 
259aaf4ece6Schristos #define ZALLOC(strm, items, size) \
260aaf4ece6Schristos            (*((strm)->zalloc))((strm)->opaque, (items), (size))
261aaf4ece6Schristos #define ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
262aaf4ece6Schristos #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
263aaf4ece6Schristos 
2646db8c6e9Schristos /* Reverse the bytes in a 32-bit value */
2656db8c6e9Schristos #define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
2666db8c6e9Schristos                     (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
2676db8c6e9Schristos 
268aaf4ece6Schristos #endif /* ZUTIL_H */
269