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