1 /* zutil.c -- target dependent utility functions for the compression library
2 * Copyright (C) 1995-2005, 2010, 2011, 2012 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
13 char * const z_errmsg[10] = {
14 "need dictionary", /* Z_NEED_DICT 2 */
15 "stream end", /* Z_STREAM_END 1 */
17 "file error", /* Z_ERRNO (-1) */
18 "stream error", /* Z_STREAM_ERROR (-2) */
19 "data error", /* Z_DATA_ERROR (-3) */
20 "insufficient memory", /* Z_MEM_ERROR (-4) */
21 "buffer error", /* Z_BUF_ERROR (-5) */
22 "incompatible version",/* Z_VERSION_ERROR (-6) */
26 const char * ZEXPORT zlibVersion(void)
31 uLong ZEXPORT zlibCompileFlags(void)
36 switch ((int)(sizeof(uInt))) {
38 case 4: flags += 1; break;
39 case 8: flags += 2; break;
42 switch ((int)(sizeof(uLong))) {
44 case 4: flags += 1 << 2; break;
45 case 8: flags += 2 << 2; break;
46 default: flags += 3 << 2;
48 switch ((int)(sizeof(voidpf))) {
50 case 4: flags += 1 << 4; break;
51 case 8: flags += 2 << 4; break;
52 default: flags += 3 << 4;
54 switch ((int)(sizeof(z_off_t))) {
56 case 4: flags += 1 << 6; break;
57 case 8: flags += 2 << 6; break;
58 default: flags += 3 << 6;
63 #if defined(ASMV) || defined(ASMINF)
72 #ifdef DYNAMIC_CRC_TABLE
81 #ifdef PKZIP_BUG_WORKAROUND
87 #if defined(STDC) || defined(Z_HAVE_STDARG_H)
90 # ifdef HAS_vsprintf_void
94 # ifdef HAS_vsnprintf_void
102 # ifdef HAS_sprintf_void
106 # ifdef HAS_snprintf_void
119 int ZLIB_INTERNAL z_verbose = verbose;
121 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)
136 #if defined(_WIN32_WCE)
137 /* The Microsoft C Run-Time Library for Windows CE doesn't have
138 * errno. We define it as a global variable to simplify porting.
139 * Its value is always 0 and should not be used.
146 void ZLIB_INTERNAL zmemcpy(Bytef *dest, const Bytef *source, uInt len)
148 if (len == 0) return;
150 *dest++ = *source++; /* ??? to be unrolled */
151 } while (--len != 0);
154 int ZLIB_INTERNAL zmemcmp(const Bytef *s1, const Bytef *s2, uInt len)
158 for (j = 0; j < len; j++) {
159 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
164 void ZLIB_INTERNAL zmemzero(Bytef *dest, uInt len)
166 if (len == 0) return;
168 *dest++ = 0; /* ??? to be unrolled */
169 } while (--len != 0);
178 /* Turbo C in 16-bit mode */
182 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
183 * and farmalloc(64K) returns a pointer with an offset of 8, so we
184 * must fix the pointer. Warning: the pointer must be put back to its
185 * original form in order to free it, use zcfree().
191 local int next_ptr = 0;
193 typedef struct ptr_table_s {
198 local ptr_table table[MAX_PTR];
199 /* This table is used to remember the original form of pointers
200 * to large buffers (64K). Such pointers are normalized with a zero offset.
201 * Since MSDOS is not a preemptive multitasking OS, this table is not
202 * protected from concurrent access. This hack doesn't work anyway on
203 * a protected system like OS/2. Use Microsoft C instead.
206 voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
208 voidpf buf = opaque; /* just to make some compilers happy */
209 ulg bsize = (ulg)items*size;
211 /* If we allocate less than 65520 bytes, we assume that farmalloc
212 * will return a usable pointer which doesn't have to be normalized.
214 if (bsize < 65520L) {
215 buf = farmalloc(bsize);
216 if (*(ush*)&buf != 0) return buf;
218 buf = farmalloc(bsize + 16L);
220 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
221 table[next_ptr].org_ptr = buf;
223 /* Normalize the pointer to seg:0 */
224 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
226 table[next_ptr++].new_ptr = buf;
230 void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
233 if (*(ush*)&ptr != 0) { /* object < 64K */
237 /* Find the original pointer */
238 for (n = 0; n < next_ptr; n++) {
239 if (ptr != table[n].new_ptr) continue;
241 farfree(table[n].org_ptr);
242 while (++n < next_ptr) {
243 table[n-1] = table[n];
248 ptr = opaque; /* just to make some compilers happy */
249 Assert(0, "zcfree: ptr not found");
252 #endif /* __TURBOC__ */
256 /* Microsoft C in 16-bit mode */
260 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
261 # define _halloc halloc
262 # define _hfree hfree
265 voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
267 if (opaque) opaque = 0; /* to make compiler happy */
268 return _halloc((long)items, size);
271 void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
273 if (opaque) opaque = 0; /* to make compiler happy */
279 #endif /* SYS16BIT */
282 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
285 extern voidp malloc OF((uInt size));
286 extern voidp calloc OF((uInt items, uInt size));
287 extern void free OF((voidpf ptr));
290 voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
292 if (opaque) items += size - size; /* make compiler happy */
293 return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
294 (voidpf)calloc(items, size);
297 void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
300 if (opaque) return; /* make compiler happy */
303 #endif /* MY_ZCALLOC */