xref: /netbsd-src/common/dist/zlib/zutil.c (revision 96c3282121aac2037abbd5952fd638784deb5ab1)
1*96c32821Schristos /*	$NetBSD: zutil.c,v 1.5 2024/09/22 19:12:28 christos Exp $	*/
2aaf4ece6Schristos 
3aaf4ece6Schristos /* zutil.c -- target dependent utility functions for the compression library
43b1253e8Schristos  * Copyright (C) 1995-2017 Jean-loup Gailly
5aaf4ece6Schristos  * For conditions of distribution and use, see copyright notice in zlib.h
6aaf4ece6Schristos  */
7aaf4ece6Schristos 
83b1253e8Schristos /* @(#) Id */
9aaf4ece6Schristos 
10aaf4ece6Schristos #include "zutil.h"
116db8c6e9Schristos #ifndef Z_SOLO
126db8c6e9Schristos #  include "gzguts.h"
13aaf4ece6Schristos #endif
14aaf4ece6Schristos 
156db8c6e9Schristos z_const char * const z_errmsg[10] = {
166db8c6e9Schristos     __UNCONST("need dictionary"),     /* Z_NEED_DICT       2  */
176db8c6e9Schristos     __UNCONST("stream end"),          /* Z_STREAM_END      1  */
186db8c6e9Schristos     __UNCONST(""),                    /* Z_OK              0  */
196db8c6e9Schristos     __UNCONST("file error"),          /* Z_ERRNO         (-1) */
206db8c6e9Schristos     __UNCONST("stream error"),        /* Z_STREAM_ERROR  (-2) */
216db8c6e9Schristos     __UNCONST("data error"),          /* Z_DATA_ERROR    (-3) */
226db8c6e9Schristos     __UNCONST("insufficient memory"), /* Z_MEM_ERROR     (-4) */
236db8c6e9Schristos     __UNCONST("buffer error"),        /* Z_BUF_ERROR     (-5) */
246db8c6e9Schristos     __UNCONST("incompatible version"),/* Z_VERSION_ERROR (-6) */
256db8c6e9Schristos     __UNCONST(""),
266db8c6e9Schristos };
27aaf4ece6Schristos 
28aaf4ece6Schristos 
29*96c32821Schristos const char * ZEXPORT zlibVersion(void) {
30aaf4ece6Schristos     return ZLIB_VERSION;
31aaf4ece6Schristos }
32aaf4ece6Schristos 
33*96c32821Schristos uLong ZEXPORT zlibCompileFlags(void) {
34aaf4ece6Schristos     uLong flags;
35aaf4ece6Schristos 
36aaf4ece6Schristos     flags = 0;
376db8c6e9Schristos     switch ((int)(sizeof(uInt))) {
38aaf4ece6Schristos     case 2:     break;
39aaf4ece6Schristos     case 4:     flags += 1;     break;
40aaf4ece6Schristos     case 8:     flags += 2;     break;
41aaf4ece6Schristos     default:    flags += 3;
42aaf4ece6Schristos     }
436db8c6e9Schristos     switch ((int)(sizeof(uLong))) {
44aaf4ece6Schristos     case 2:     break;
45aaf4ece6Schristos     case 4:     flags += 1 << 2;        break;
46aaf4ece6Schristos     case 8:     flags += 2 << 2;        break;
47aaf4ece6Schristos     default:    flags += 3 << 2;
48aaf4ece6Schristos     }
496db8c6e9Schristos     switch ((int)(sizeof(voidpf))) {
50aaf4ece6Schristos     case 2:     break;
51aaf4ece6Schristos     case 4:     flags += 1 << 4;        break;
52aaf4ece6Schristos     case 8:     flags += 2 << 4;        break;
53aaf4ece6Schristos     default:    flags += 3 << 4;
54aaf4ece6Schristos     }
556db8c6e9Schristos     switch ((int)(sizeof(z_off_t))) {
56aaf4ece6Schristos     case 2:     break;
57aaf4ece6Schristos     case 4:     flags += 1 << 6;        break;
58aaf4ece6Schristos     case 8:     flags += 2 << 6;        break;
59aaf4ece6Schristos     default:    flags += 3 << 6;
60aaf4ece6Schristos     }
61a1f9f4c0Schristos #ifdef ZLIB_DEBUG
62aaf4ece6Schristos     flags += 1 << 8;
63aaf4ece6Schristos #endif
643b1253e8Schristos     /*
65aaf4ece6Schristos #if defined(ASMV) || defined(ASMINF)
66aaf4ece6Schristos     flags += 1 << 9;
67aaf4ece6Schristos #endif
683b1253e8Schristos      */
69aaf4ece6Schristos #ifdef ZLIB_WINAPI
70aaf4ece6Schristos     flags += 1 << 10;
71aaf4ece6Schristos #endif
72aaf4ece6Schristos #ifdef BUILDFIXED
73aaf4ece6Schristos     flags += 1 << 12;
74aaf4ece6Schristos #endif
75aaf4ece6Schristos #ifdef DYNAMIC_CRC_TABLE
76aaf4ece6Schristos     flags += 1 << 13;
77aaf4ece6Schristos #endif
78aaf4ece6Schristos #ifdef NO_GZCOMPRESS
79aaf4ece6Schristos     flags += 1L << 16;
80aaf4ece6Schristos #endif
81aaf4ece6Schristos #ifdef NO_GZIP
82aaf4ece6Schristos     flags += 1L << 17;
83aaf4ece6Schristos #endif
84aaf4ece6Schristos #ifdef PKZIP_BUG_WORKAROUND
85aaf4ece6Schristos     flags += 1L << 20;
86aaf4ece6Schristos #endif
87aaf4ece6Schristos #ifdef FASTEST
88aaf4ece6Schristos     flags += 1L << 21;
89aaf4ece6Schristos #endif
906db8c6e9Schristos #if defined(STDC) || defined(Z_HAVE_STDARG_H)
91aaf4ece6Schristos #  ifdef NO_vsnprintf
92aaf4ece6Schristos     flags += 1L << 25;
93aaf4ece6Schristos #    ifdef HAS_vsprintf_void
94aaf4ece6Schristos     flags += 1L << 26;
95aaf4ece6Schristos #    endif
96aaf4ece6Schristos #  else
97aaf4ece6Schristos #    ifdef HAS_vsnprintf_void
98aaf4ece6Schristos     flags += 1L << 26;
99aaf4ece6Schristos #    endif
100aaf4ece6Schristos #  endif
101aaf4ece6Schristos #else
102aaf4ece6Schristos     flags += 1L << 24;
103aaf4ece6Schristos #  ifdef NO_snprintf
104aaf4ece6Schristos     flags += 1L << 25;
105aaf4ece6Schristos #    ifdef HAS_sprintf_void
106aaf4ece6Schristos     flags += 1L << 26;
107aaf4ece6Schristos #    endif
108aaf4ece6Schristos #  else
109aaf4ece6Schristos #    ifdef HAS_snprintf_void
110aaf4ece6Schristos     flags += 1L << 26;
111aaf4ece6Schristos #    endif
112aaf4ece6Schristos #  endif
113aaf4ece6Schristos #endif
114aaf4ece6Schristos     return flags;
115aaf4ece6Schristos }
116aaf4ece6Schristos 
117a1f9f4c0Schristos #ifdef ZLIB_DEBUG
1186db8c6e9Schristos #include <stdlib.h>
119aaf4ece6Schristos #  ifndef verbose
120aaf4ece6Schristos #    define verbose 0
121aaf4ece6Schristos #  endif
1226db8c6e9Schristos int ZLIB_INTERNAL z_verbose = verbose;
123aaf4ece6Schristos 
124*96c32821Schristos void ZLIB_INTERNAL z_error(char *m) {
125aaf4ece6Schristos     fprintf(stderr, "%s\n", m);
126aaf4ece6Schristos     exit(1);
127aaf4ece6Schristos }
128aaf4ece6Schristos #endif
129aaf4ece6Schristos 
130aaf4ece6Schristos /* exported to allow conversion of error code to string for compress() and
131aaf4ece6Schristos  * uncompress()
132aaf4ece6Schristos  */
133*96c32821Schristos const char * ZEXPORT zError(int err) {
134aaf4ece6Schristos     return ERR_MSG(err);
135aaf4ece6Schristos }
136aaf4ece6Schristos 
1373b1253e8Schristos #if defined(_WIN32_WCE) && _WIN32_WCE < 0x800
1383b1253e8Schristos     /* The older Microsoft C Run-Time Library for Windows CE doesn't have
139aaf4ece6Schristos      * errno.  We define it as a global variable to simplify porting.
140aaf4ece6Schristos      * Its value is always 0 and should not be used.
141aaf4ece6Schristos      */
142aaf4ece6Schristos     int errno = 0;
143aaf4ece6Schristos #endif
144aaf4ece6Schristos 
145aaf4ece6Schristos #ifndef HAVE_MEMCPY
146aaf4ece6Schristos 
147*96c32821Schristos void ZLIB_INTERNAL zmemcpy(Bytef* dest, const Bytef* source, uInt len) {
148aaf4ece6Schristos     if (len == 0) return;
149aaf4ece6Schristos     do {
150aaf4ece6Schristos         *dest++ = *source++; /* ??? to be unrolled */
151aaf4ece6Schristos     } while (--len != 0);
152aaf4ece6Schristos }
153aaf4ece6Schristos 
154*96c32821Schristos int ZLIB_INTERNAL zmemcmp(const Bytef* s1, const Bytef* s2, uInt len) {
155aaf4ece6Schristos     uInt j;
156aaf4ece6Schristos 
157aaf4ece6Schristos     for (j = 0; j < len; j++) {
158aaf4ece6Schristos         if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
159aaf4ece6Schristos     }
160aaf4ece6Schristos     return 0;
161aaf4ece6Schristos }
162aaf4ece6Schristos 
163*96c32821Schristos void ZLIB_INTERNAL zmemzero(Bytef* dest, uInt len) {
164aaf4ece6Schristos     if (len == 0) return;
165aaf4ece6Schristos     do {
166aaf4ece6Schristos         *dest++ = 0;  /* ??? to be unrolled */
167aaf4ece6Schristos     } while (--len != 0);
168aaf4ece6Schristos }
169aaf4ece6Schristos #endif
170aaf4ece6Schristos 
1716db8c6e9Schristos #ifndef Z_SOLO
172aaf4ece6Schristos 
173aaf4ece6Schristos #ifdef SYS16BIT
174aaf4ece6Schristos 
175aaf4ece6Schristos #ifdef __TURBOC__
176aaf4ece6Schristos /* Turbo C in 16-bit mode */
177aaf4ece6Schristos 
178aaf4ece6Schristos #  define MY_ZCALLOC
179aaf4ece6Schristos 
180aaf4ece6Schristos /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
181aaf4ece6Schristos  * and farmalloc(64K) returns a pointer with an offset of 8, so we
182aaf4ece6Schristos  * must fix the pointer. Warning: the pointer must be put back to its
183aaf4ece6Schristos  * original form in order to free it, use zcfree().
184aaf4ece6Schristos  */
185aaf4ece6Schristos 
186aaf4ece6Schristos #define MAX_PTR 10
187aaf4ece6Schristos /* 10*64K = 640K */
188aaf4ece6Schristos 
189aaf4ece6Schristos local int next_ptr = 0;
190aaf4ece6Schristos 
191aaf4ece6Schristos typedef struct ptr_table_s {
192aaf4ece6Schristos     voidpf org_ptr;
193aaf4ece6Schristos     voidpf new_ptr;
194aaf4ece6Schristos } ptr_table;
195aaf4ece6Schristos 
196aaf4ece6Schristos local ptr_table table[MAX_PTR];
197aaf4ece6Schristos /* This table is used to remember the original form of pointers
198aaf4ece6Schristos  * to large buffers (64K). Such pointers are normalized with a zero offset.
199aaf4ece6Schristos  * Since MSDOS is not a preemptive multitasking OS, this table is not
200aaf4ece6Schristos  * protected from concurrent access. This hack doesn't work anyway on
201aaf4ece6Schristos  * a protected system like OS/2. Use Microsoft C instead.
202aaf4ece6Schristos  */
203aaf4ece6Schristos 
204*96c32821Schristos voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) {
2056db8c6e9Schristos     voidpf buf;
206aaf4ece6Schristos     ulg bsize = (ulg)items*size;
207aaf4ece6Schristos 
2086db8c6e9Schristos     (void)opaque;
2096db8c6e9Schristos 
210aaf4ece6Schristos     /* If we allocate less than 65520 bytes, we assume that farmalloc
211aaf4ece6Schristos      * will return a usable pointer which doesn't have to be normalized.
212aaf4ece6Schristos      */
213aaf4ece6Schristos     if (bsize < 65520L) {
214aaf4ece6Schristos         buf = farmalloc(bsize);
215aaf4ece6Schristos         if (*(ush*)&buf != 0) return buf;
216aaf4ece6Schristos     } else {
217aaf4ece6Schristos         buf = farmalloc(bsize + 16L);
218aaf4ece6Schristos     }
219aaf4ece6Schristos     if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
220aaf4ece6Schristos     table[next_ptr].org_ptr = buf;
221aaf4ece6Schristos 
222aaf4ece6Schristos     /* Normalize the pointer to seg:0 */
223aaf4ece6Schristos     *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
224aaf4ece6Schristos     *(ush*)&buf = 0;
225aaf4ece6Schristos     table[next_ptr++].new_ptr = buf;
226aaf4ece6Schristos     return buf;
227aaf4ece6Schristos }
228aaf4ece6Schristos 
229*96c32821Schristos void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {
230aaf4ece6Schristos     int n;
2316db8c6e9Schristos 
2326db8c6e9Schristos     (void)opaque;
2336db8c6e9Schristos 
234aaf4ece6Schristos     if (*(ush*)&ptr != 0) { /* object < 64K */
235aaf4ece6Schristos         farfree(ptr);
236aaf4ece6Schristos         return;
237aaf4ece6Schristos     }
238aaf4ece6Schristos     /* Find the original pointer */
239aaf4ece6Schristos     for (n = 0; n < next_ptr; n++) {
240aaf4ece6Schristos         if (ptr != table[n].new_ptr) continue;
241aaf4ece6Schristos 
242aaf4ece6Schristos         farfree(table[n].org_ptr);
243aaf4ece6Schristos         while (++n < next_ptr) {
244aaf4ece6Schristos             table[n-1] = table[n];
245aaf4ece6Schristos         }
246aaf4ece6Schristos         next_ptr--;
247aaf4ece6Schristos         return;
248aaf4ece6Schristos     }
249aaf4ece6Schristos     Assert(0, "zcfree: ptr not found");
250aaf4ece6Schristos }
251aaf4ece6Schristos 
252aaf4ece6Schristos #endif /* __TURBOC__ */
253aaf4ece6Schristos 
254aaf4ece6Schristos 
255aaf4ece6Schristos #ifdef M_I86
256aaf4ece6Schristos /* Microsoft C in 16-bit mode */
257aaf4ece6Schristos 
258aaf4ece6Schristos #  define MY_ZCALLOC
259aaf4ece6Schristos 
260aaf4ece6Schristos #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
261aaf4ece6Schristos #  define _halloc  halloc
262aaf4ece6Schristos #  define _hfree   hfree
263aaf4ece6Schristos #endif
264aaf4ece6Schristos 
265*96c32821Schristos voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size) {
2666db8c6e9Schristos     (void)opaque;
267aaf4ece6Schristos     return _halloc((long)items, size);
268aaf4ece6Schristos }
269aaf4ece6Schristos 
270*96c32821Schristos void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {
2716db8c6e9Schristos     (void)opaque;
272aaf4ece6Schristos     _hfree(ptr);
273aaf4ece6Schristos }
274aaf4ece6Schristos 
275aaf4ece6Schristos #endif /* M_I86 */
276aaf4ece6Schristos 
277aaf4ece6Schristos #endif /* SYS16BIT */
278aaf4ece6Schristos 
279aaf4ece6Schristos 
280aaf4ece6Schristos #ifndef MY_ZCALLOC /* Any system without a special alloc function */
281aaf4ece6Schristos 
282aaf4ece6Schristos #ifndef STDC
283*96c32821Schristos extern voidp malloc(uInt size);
284*96c32821Schristos extern voidp calloc(uInt items, uInt size);
285*96c32821Schristos extern void free(voidpf ptr);
286aaf4ece6Schristos #endif
287aaf4ece6Schristos 
288*96c32821Schristos voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) {
2896db8c6e9Schristos     (void)opaque;
290aaf4ece6Schristos     return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
291aaf4ece6Schristos                               (voidpf)calloc(items, size);
292aaf4ece6Schristos }
293aaf4ece6Schristos 
294*96c32821Schristos void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {
2956db8c6e9Schristos     (void)opaque;
296aaf4ece6Schristos     free(ptr);
297aaf4ece6Schristos }
298aaf4ece6Schristos 
299aaf4ece6Schristos #endif /* MY_ZCALLOC */
3006db8c6e9Schristos 
3016db8c6e9Schristos #endif /* !Z_SOLO */
302