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