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
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) */
27 const char * ZEXPORT zlibVersion(void) {
31 uLong ZEXPORT zlibCompileFlags(void) {
35 switch ((int)(sizeof(uInt))) {
37 case 4: flags += 1; break;
38 case 8: flags += 2; break;
41 switch ((int)(sizeof(uLong))) {
43 case 4: flags += 1 << 2; break;
44 case 8: flags += 2 << 2; break;
45 default: flags += 3 << 2;
47 switch ((int)(sizeof(voidpf))) {
49 case 4: flags += 1 << 4; break;
50 case 8: flags += 2 << 4; break;
51 default: flags += 3 << 4;
53 switch ((int)(sizeof(z_off_t))) {
55 case 4: flags += 1 << 6; break;
56 case 8: flags += 2 << 6; break;
57 default: flags += 3 << 6;
63 #if defined(ASMV) || defined(ASMINF)
73 #ifdef DYNAMIC_CRC_TABLE
82 #ifdef PKZIP_BUG_WORKAROUND
88 #if defined(STDC) || defined(Z_HAVE_STDARG_H)
91 # ifdef HAS_vsprintf_void
95 # ifdef HAS_vsnprintf_void
103 # ifdef HAS_sprintf_void
107 # ifdef HAS_snprintf_void
120 int ZLIB_INTERNAL z_verbose = verbose;
122 void ZLIB_INTERNAL z_error(char *m) {
123 fprintf(stderr, "%s\n", m);
128 /* exported to allow conversion of error code to string for compress() and
131 const char * ZEXPORT zError(int err) {
135 #if defined(_WIN32_WCE) && _WIN32_WCE < 0x800
136 /* The older Microsoft C Run-Time Library for Windows CE doesn't have
137 * errno. We define it as a global variable to simplify porting.
138 * Its value is always 0 and should not be used.
145 void ZLIB_INTERNAL zmemcpy(Bytef* dest, const Bytef* source, uInt len) {
146 if (len == 0) return;
148 *dest++ = *source++; /* ??? to be unrolled */
149 } while (--len != 0);
152 int ZLIB_INTERNAL zmemcmp(const Bytef* s1, const Bytef* s2, uInt len) {
155 for (j = 0; j < len; j++) {
156 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
161 void ZLIB_INTERNAL zmemzero(Bytef* dest, uInt len) {
162 if (len == 0) return;
164 *dest++ = 0; /* ??? to be unrolled */
165 } while (--len != 0);
174 /* Turbo C in 16-bit mode */
178 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
179 * and farmalloc(64K) returns a pointer with an offset of 8, so we
180 * must fix the pointer. Warning: the pointer must be put back to its
181 * original form in order to free it, use zcfree().
187 local int next_ptr = 0;
189 typedef struct ptr_table_s {
194 local ptr_table table[MAX_PTR];
195 /* This table is used to remember the original form of pointers
196 * to large buffers (64K). Such pointers are normalized with a zero offset.
197 * Since MSDOS is not a preemptive multitasking OS, this table is not
198 * protected from concurrent access. This hack doesn't work anyway on
199 * a protected system like OS/2. Use Microsoft C instead.
202 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) {
204 ulg bsize = (ulg)items*size;
208 /* If we allocate less than 65520 bytes, we assume that farmalloc
209 * will return a usable pointer which doesn't have to be normalized.
211 if (bsize < 65520L) {
212 buf = farmalloc(bsize);
213 if (*(ush*)&buf != 0) return buf;
215 buf = farmalloc(bsize + 16L);
217 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
218 table[next_ptr].org_ptr = buf;
220 /* Normalize the pointer to seg:0 */
221 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
223 table[next_ptr++].new_ptr = buf;
227 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {
232 if (*(ush*)&ptr != 0) { /* object < 64K */
236 /* Find the original pointer */
237 for (n = 0; n < next_ptr; n++) {
238 if (ptr != table[n].new_ptr) continue;
240 farfree(table[n].org_ptr);
241 while (++n < next_ptr) {
242 table[n-1] = table[n];
247 Assert(0, "zcfree: ptr not found");
250 #endif /* __TURBOC__ */
254 /* Microsoft C in 16-bit mode */
258 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
259 # define _halloc halloc
260 # define _hfree hfree
263 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size) {
265 return _halloc((long)items, size);
268 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {
275 #endif /* SYS16BIT */
278 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
281 extern voidp malloc(uInt size);
282 extern voidp calloc(uInt items, uInt size);
283 extern void free(voidpf ptr);
286 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) {
288 return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
289 (voidpf)calloc(items, size);
292 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {
297 #endif /* MY_ZCALLOC */