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
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) */
29 const char * ZEXPORT zlibVersion()
34 uLong ZEXPORT zlibCompileFlags()
39 switch ((int)(sizeof(uInt))) {
41 case 4: flags += 1; break;
42 case 8: flags += 2; break;
45 switch ((int)(sizeof(uLong))) {
47 case 4: flags += 1 << 2; break;
48 case 8: flags += 2 << 2; break;
49 default: flags += 3 << 2;
51 switch ((int)(sizeof(voidpf))) {
53 case 4: flags += 1 << 4; break;
54 case 8: flags += 2 << 4; break;
55 default: flags += 3 << 4;
57 switch ((int)(sizeof(z_off_t))) {
59 case 4: flags += 1 << 6; break;
60 case 8: flags += 2 << 6; break;
61 default: flags += 3 << 6;
67 #if defined(ASMV) || defined(ASMINF)
77 #ifdef DYNAMIC_CRC_TABLE
86 #ifdef PKZIP_BUG_WORKAROUND
92 #if defined(STDC) || defined(Z_HAVE_STDARG_H)
95 # ifdef HAS_vsprintf_void
99 # ifdef HAS_vsnprintf_void
107 # ifdef HAS_sprintf_void
111 # ifdef HAS_snprintf_void
124 int ZLIB_INTERNAL z_verbose = verbose;
126 void ZLIB_INTERNAL z_error(
129 fprintf(stderr, "%s\n", m);
134 /* exported to allow conversion of error code to string for compress() and
137 const char * ZEXPORT zError(
143 #endif /* !Z_FREETYPE */
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.
155 void ZLIB_INTERNAL zmemcpy(
160 if (len == 0) return;
162 *dest++ = *source++; /* ??? to be unrolled */
163 } while (--len != 0);
168 int ZLIB_INTERNAL zmemcmp(
175 for (j = 0; j < len; j++) {
176 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
181 void ZLIB_INTERNAL zmemzero(
185 if (len == 0) return;
187 *dest++ = 0; /* ??? to be unrolled */
188 } while (--len != 0);
190 #endif /* !Z_FREETYPE */
198 /* Turbo C in 16-bit mode */
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().
211 local int next_ptr = 0;
213 typedef struct ptr_table_s {
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.
226 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size)
229 ulg bsize = (ulg)items*size;
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.
236 if (bsize < 65520L) {
237 buf = farmalloc(bsize);
238 if (*(ush*)&buf != 0) return buf;
240 buf = farmalloc(bsize + 16L);
242 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
243 table[next_ptr].org_ptr = buf;
245 /* Normalize the pointer to seg:0 */
246 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
248 table[next_ptr++].new_ptr = buf;
252 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
258 if (*(ush*)&ptr != 0) { /* object < 64K */
262 /* Find the original pointer */
263 for (n = 0; n < next_ptr; n++) {
264 if (ptr != table[n].new_ptr) continue;
266 farfree(table[n].org_ptr);
267 while (++n < next_ptr) {
268 table[n-1] = table[n];
273 Assert(0, "zcfree: ptr not found");
276 #endif /* __TURBOC__ */
280 /* Microsoft C in 16-bit mode */
284 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
285 # define _halloc halloc
286 # define _hfree hfree
289 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size)
292 return _halloc((long)items, size);
295 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
303 #endif /* SYS16BIT */
306 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
309 extern voidp malloc OF((uInt size));
310 extern voidp calloc OF((uInt items, uInt size));
311 extern void free OF((voidpf ptr));
314 voidpf ZLIB_INTERNAL zcalloc(
320 return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
321 (voidpf)calloc(items, size);
324 void ZLIB_INTERNAL zcfree(
332 #endif /* MY_ZCALLOC */