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