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