1 /* zutil.c -- target dependent utility functions for the compression library
2 * Copyright (C) 1995-2005, 2010, 2011 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
11 struct internal_state {int dummy;}; /* for buggy compilers */
14 const char * const z_errmsg[10] = {
15 "need dictionary", /* Z_NEED_DICT 2 */
16 "stream end", /* Z_STREAM_END 1 */
18 "file error", /* Z_ERRNO (-1) */
19 "stream error", /* Z_STREAM_ERROR (-2) */
20 "data error", /* Z_DATA_ERROR (-3) */
21 "insufficient memory", /* Z_MEM_ERROR (-4) */
22 "buffer error", /* Z_BUF_ERROR (-5) */
23 "incompatible version",/* Z_VERSION_ERROR (-6) */
27 const char * ZEXPORT zlibVersion()
32 uLong ZEXPORT zlibCompileFlags()
37 switch ((int)(sizeof(uInt))) {
39 case 4: flags += 1; break;
40 case 8: flags += 2; break;
43 switch ((int)(sizeof(uLong))) {
45 case 4: flags += 1 << 2; break;
46 case 8: flags += 2 << 2; break;
47 default: flags += 3 << 2;
49 switch ((int)(sizeof(voidpf))) {
51 case 4: flags += 1 << 4; break;
52 case 8: flags += 2 << 4; break;
53 default: flags += 3 << 4;
55 switch ((int)(sizeof(z_off_t))) {
57 case 4: flags += 1 << 6; break;
58 case 8: flags += 2 << 6; break;
59 default: flags += 3 << 6;
64 #if defined(ASMV) || defined(ASMINF)
73 #ifdef DYNAMIC_CRC_TABLE
82 #ifdef PKZIP_BUG_WORKAROUND
88 return flags + gzflags();
96 int ZLIB_INTERNAL z_verbose = verbose;
98 void ZLIB_INTERNAL z_error (m)
101 fprintf(stderr, "%s\n", m);
106 /* exported to allow conversion of error code to string for compress() and
109 const char * ZEXPORT zError(err)
115 #if defined(_WIN32_WCE)
116 /* The Microsoft C Run-Time Library for Windows CE doesn't have
117 * errno. We define it as a global variable to simplify porting.
118 * Its value is always 0 and should not be used.
125 void ZLIB_INTERNAL zmemcpy(dest, source, len)
130 if (len == 0) return;
132 *dest++ = *source++; /* ??? to be unrolled */
133 } while (--len != 0);
136 int ZLIB_INTERNAL zmemcmp(s1, s2, len)
143 for (j = 0; j < len; j++) {
144 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
149 void ZLIB_INTERNAL zmemzero(dest, len)
153 if (len == 0) return;
155 *dest++ = 0; /* ??? to be unrolled */
156 } while (--len != 0);
164 /* Turbo C in 16-bit mode */
168 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
169 * and farmalloc(64K) returns a pointer with an offset of 8, so we
170 * must fix the pointer. Warning: the pointer must be put back to its
171 * original form in order to free it, use zcfree().
177 local int next_ptr = 0;
179 typedef struct ptr_table_s {
184 local ptr_table table[MAX_PTR];
185 /* This table is used to remember the original form of pointers
186 * to large buffers (64K). Such pointers are normalized with a zero offset.
187 * Since MSDOS is not a preemptive multitasking OS, this table is not
188 * protected from concurrent access. This hack doesn't work anyway on
189 * a protected system like OS/2. Use Microsoft C instead.
192 voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
194 voidpf buf = opaque; /* just to make some compilers happy */
195 ulg bsize = (ulg)items*size;
197 /* If we allocate less than 65520 bytes, we assume that farmalloc
198 * will return a usable pointer which doesn't have to be normalized.
200 if (bsize < 65520L) {
201 buf = farmalloc(bsize);
202 if (*(ush*)&buf != 0) return buf;
204 buf = farmalloc(bsize + 16L);
206 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
207 table[next_ptr].org_ptr = buf;
209 /* Normalize the pointer to seg:0 */
210 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
212 table[next_ptr++].new_ptr = buf;
216 void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
219 if (*(ush*)&ptr != 0) { /* object < 64K */
223 /* Find the original pointer */
224 for (n = 0; n < next_ptr; n++) {
225 if (ptr != table[n].new_ptr) continue;
227 farfree(table[n].org_ptr);
228 while (++n < next_ptr) {
229 table[n-1] = table[n];
234 ptr = opaque; /* just to make some compilers happy */
235 Assert(0, "zcfree: ptr not found");
238 #endif /* __TURBOC__ */
242 /* Microsoft C in 16-bit mode */
246 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
247 # define _halloc halloc
248 # define _hfree hfree
251 voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
253 if (opaque) opaque = 0; /* to make compiler happy */
254 return _halloc((long)items, size);
257 void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
259 if (opaque) opaque = 0; /* to make compiler happy */
265 #endif /* SYS16BIT */
268 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
271 extern voidp malloc OF((uInt size));
272 extern voidp calloc OF((uInt items, uInt size));
273 extern void free OF((voidpf ptr));
276 voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
281 if (opaque) items += size - size; /* make compiler happy */
282 return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
283 (voidpf)calloc(items, size);
286 void ZLIB_INTERNAL zcfree (opaque, ptr)
291 if (opaque) return; /* make compiler happy */
294 #endif /* MY_ZCALLOC */