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 #ifndef Z_FREETYPE
14
15 z_const char * const z_errmsg[10] = {
16 (z_const char *)"need dictionary", /* Z_NEED_DICT 2 */
17 (z_const char *)"stream end", /* Z_STREAM_END 1 */
18 (z_const char *)"", /* Z_OK 0 */
19 (z_const char *)"file error", /* Z_ERRNO (-1) */
20 (z_const char *)"stream error", /* Z_STREAM_ERROR (-2) */
21 (z_const char *)"data error", /* Z_DATA_ERROR (-3) */
22 (z_const char *)"insufficient memory", /* Z_MEM_ERROR (-4) */
23 (z_const char *)"buffer error", /* Z_BUF_ERROR (-5) */
24 (z_const char *)"incompatible version",/* Z_VERSION_ERROR (-6) */
25 (z_const char *)""
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(char * m)126 void ZLIB_INTERNAL z_error(
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(int err)137 const char * ZEXPORT zError(
138 int err)
139 {
140 return ERR_MSG(err);
141 }
142
143 #endif /* !Z_FREETYPE */
144
145 #if defined(_WIN32_WCE) && _WIN32_WCE < 0x800
146 /* The older Microsoft C Run-Time Library for Windows CE doesn't have
147 * errno. We define it as a global variable to simplify porting.
148 * Its value is always 0 and should not be used.
149 */
150 int errno = 0;
151 #endif
152
153 #ifndef HAVE_MEMCPY
154
zmemcpy(Bytef * dest,const Bytef * source,uInt len)155 void ZLIB_INTERNAL zmemcpy(
156 Bytef* dest,
157 const Bytef* source,
158 uInt len)
159 {
160 if (len == 0) return;
161 do {
162 *dest++ = *source++; /* ??? to be unrolled */
163 } while (--len != 0);
164 }
165
166 #ifndef Z_FREETYPE
167
zmemcmp(const Bytef * s1,const Bytef * s2,uInt len)168 int ZLIB_INTERNAL zmemcmp(
169 const Bytef* s1,
170 const Bytef* s2,
171 uInt len)
172 {
173 uInt j;
174
175 for (j = 0; j < len; j++) {
176 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
177 }
178 return 0;
179 }
180
zmemzero(Bytef * dest,uInt len)181 void ZLIB_INTERNAL zmemzero(
182 Bytef* dest,
183 uInt len)
184 {
185 if (len == 0) return;
186 do {
187 *dest++ = 0; /* ??? to be unrolled */
188 } while (--len != 0);
189 }
190 #endif /* !Z_FREETYPE */
191 #endif
192
193 #ifndef Z_SOLO
194
195 #ifdef SYS16BIT
196
197 #ifdef __TURBOC__
198 /* Turbo C in 16-bit mode */
199
200 # define MY_ZCALLOC
201
202 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
203 * and farmalloc(64K) returns a pointer with an offset of 8, so we
204 * must fix the pointer. Warning: the pointer must be put back to its
205 * original form in order to free it, use zcfree().
206 */
207
208 #define MAX_PTR 10
209 /* 10*64K = 640K */
210
211 local int next_ptr = 0;
212
213 typedef struct ptr_table_s {
214 voidpf org_ptr;
215 voidpf new_ptr;
216 } ptr_table;
217
218 local ptr_table table[MAX_PTR];
219 /* This table is used to remember the original form of pointers
220 * to large buffers (64K). Such pointers are normalized with a zero offset.
221 * Since MSDOS is not a preemptive multitasking OS, this table is not
222 * protected from concurrent access. This hack doesn't work anyway on
223 * a protected system like OS/2. Use Microsoft C instead.
224 */
225
zcalloc(voidpf opaque,unsigned items,unsigned size)226 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size)
227 {
228 voidpf buf;
229 ulg bsize = (ulg)items*size;
230
231 (void)opaque;
232
233 /* If we allocate less than 65520 bytes, we assume that farmalloc
234 * will return a usable pointer which doesn't have to be normalized.
235 */
236 if (bsize < 65520L) {
237 buf = farmalloc(bsize);
238 if (*(ush*)&buf != 0) return buf;
239 } else {
240 buf = farmalloc(bsize + 16L);
241 }
242 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
243 table[next_ptr].org_ptr = buf;
244
245 /* Normalize the pointer to seg:0 */
246 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
247 *(ush*)&buf = 0;
248 table[next_ptr++].new_ptr = buf;
249 return buf;
250 }
251
zcfree(voidpf opaque,voidpf ptr)252 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
253 {
254 int n;
255
256 (void)opaque;
257
258 if (*(ush*)&ptr != 0) { /* object < 64K */
259 farfree(ptr);
260 return;
261 }
262 /* Find the original pointer */
263 for (n = 0; n < next_ptr; n++) {
264 if (ptr != table[n].new_ptr) continue;
265
266 farfree(table[n].org_ptr);
267 while (++n < next_ptr) {
268 table[n-1] = table[n];
269 }
270 next_ptr--;
271 return;
272 }
273 Assert(0, "zcfree: ptr not found");
274 }
275
276 #endif /* __TURBOC__ */
277
278
279 #ifdef M_I86
280 /* Microsoft C in 16-bit mode */
281
282 # define MY_ZCALLOC
283
284 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
285 # define _halloc halloc
286 # define _hfree hfree
287 #endif
288
zcalloc(voidpf opaque,uInt items,uInt size)289 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size)
290 {
291 (void)opaque;
292 return _halloc((long)items, size);
293 }
294
zcfree(voidpf opaque,voidpf ptr)295 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
296 {
297 (void)opaque;
298 _hfree(ptr);
299 }
300
301 #endif /* M_I86 */
302
303 #endif /* SYS16BIT */
304
305
306 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
307
308 #ifndef STDC
309 extern voidp malloc OF((uInt size));
310 extern voidp calloc OF((uInt items, uInt size));
311 extern void free OF((voidpf ptr));
312 #endif
313
zcalloc(voidpf opaque,unsigned items,unsigned size)314 voidpf ZLIB_INTERNAL zcalloc(
315 voidpf opaque,
316 unsigned items,
317 unsigned size)
318 {
319 (void)opaque;
320 return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
321 (voidpf)calloc(items, size);
322 }
323
zcfree(voidpf opaque,voidpf ptr)324 void ZLIB_INTERNAL zcfree(
325 voidpf opaque,
326 voidpf ptr)
327 {
328 (void)opaque;
329 free(ptr);
330 }
331
332 #endif /* MY_ZCALLOC */
333
334 #endif /* !Z_SOLO */
335