/src/assimp/contrib/zlib/zutil.c
Line | Count | Source |
1 | | /* zutil.c -- target dependent utility functions for the compression library |
2 | | * Copyright (C) 1995-2017 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 | | const char * ZEXPORT zlibVersion() |
27 | 0 | { |
28 | 0 | return ZLIB_VERSION; |
29 | 0 | } |
30 | | |
31 | | uLong ZEXPORT zlibCompileFlags() |
32 | 0 | { |
33 | 0 | uLong flags; |
34 | |
|
35 | 0 | flags = 0; |
36 | 0 | switch ((int)(sizeof(uInt))) { |
37 | 0 | case 2: break; |
38 | 0 | case 4: flags += 1; break; |
39 | 0 | case 8: flags += 2; break; |
40 | 0 | default: flags += 3; |
41 | 0 | } |
42 | 0 | switch ((int)(sizeof(uLong))) { |
43 | 0 | case 2: break; |
44 | 0 | case 4: flags += 1 << 2; break; |
45 | 0 | case 8: flags += 2 << 2; break; |
46 | 0 | default: flags += 3 << 2; |
47 | 0 | } |
48 | 0 | switch ((int)(sizeof(voidpf))) { |
49 | 0 | case 2: break; |
50 | 0 | case 4: flags += 1 << 4; break; |
51 | 0 | case 8: flags += 2 << 4; break; |
52 | 0 | default: flags += 3 << 4; |
53 | 0 | } |
54 | 0 | switch ((int)(sizeof(z_off_t))) { |
55 | 0 | case 2: break; |
56 | 0 | case 4: flags += 1 << 6; break; |
57 | 0 | case 8: flags += 2 << 6; break; |
58 | 0 | default: flags += 3 << 6; |
59 | 0 | } |
60 | | #ifdef ZLIB_DEBUG |
61 | | flags += 1 << 8; |
62 | | #endif |
63 | | /* |
64 | | #if defined(ASMV) || defined(ASMINF) |
65 | | flags += 1 << 9; |
66 | | #endif |
67 | | */ |
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 | 0 | #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 | 0 | # 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 | 0 | return flags; |
114 | 0 | } |
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 | | |
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 | | */ |
134 | | const char * ZEXPORT zError(err) |
135 | | int err; |
136 | 0 | { |
137 | 0 | return ERR_MSG(err); |
138 | 0 | } |
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 | | |
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 | | |
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 | | |
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 | | |
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 | | |
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 | | |
281 | | voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size) |
282 | | { |
283 | | (void)opaque; |
284 | | return _halloc((long)items, size); |
285 | | } |
286 | | |
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 | | |
306 | | voidpf ZLIB_INTERNAL zcalloc(opaque, items, size) |
307 | | voidpf opaque; |
308 | | unsigned items; |
309 | | unsigned size; |
310 | 65 | { |
311 | 65 | (void)opaque; |
312 | 65 | return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : |
313 | 65 | (voidpf)calloc(items, size); |
314 | 65 | } |
315 | | |
316 | | void ZLIB_INTERNAL zcfree(opaque, ptr) |
317 | | voidpf opaque; |
318 | | voidpf ptr; |
319 | 65 | { |
320 | 65 | (void)opaque; |
321 | 65 | free(ptr); |
322 | 65 | } |
323 | | |
324 | | #endif /* MY_ZCALLOC */ |
325 | | |
326 | | #endif /* !Z_SOLO */ |