1 /* zutil.c -- target dependent utility functions for the compression library
\r
2 * Copyright (C) 1995-2005 Jean-loup Gailly.
\r
3 * For conditions of distribution and use, see copyright notice in zlib.h
\r
10 #ifndef NO_DUMMY_DECL
\r
11 struct internal_state {int dummy;}; /* for buggy compilers */
\r
14 const char * const z_errmsg[10] = {
\r
15 "need dictionary", /* Z_NEED_DICT 2 */
\r
16 "stream end", /* Z_STREAM_END 1 */
\r
18 "file error", /* Z_ERRNO (-1) */
\r
19 "stream error", /* Z_STREAM_ERROR (-2) */
\r
20 "data error", /* Z_DATA_ERROR (-3) */
\r
21 "insufficient memory", /* Z_MEM_ERROR (-4) */
\r
22 "buffer error", /* Z_BUF_ERROR (-5) */
\r
23 "incompatible version",/* Z_VERSION_ERROR (-6) */
\r
27 const char * ZEXPORT zlibVersion()
\r
29 return ZLIB_VERSION;
\r
32 uLong ZEXPORT zlibCompileFlags()
\r
37 switch (sizeof(uInt)) {
\r
39 case 4: flags += 1; break;
\r
40 case 8: flags += 2; break;
\r
41 default: flags += 3;
\r
43 switch (sizeof(uLong)) {
\r
45 case 4: flags += 1 << 2; break;
\r
46 case 8: flags += 2 << 2; break;
\r
47 default: flags += 3 << 2;
\r
49 switch (sizeof(voidpf)) {
\r
51 case 4: flags += 1 << 4; break;
\r
52 case 8: flags += 2 << 4; break;
\r
53 default: flags += 3 << 4;
\r
55 switch (sizeof(z_off_t)) {
\r
57 case 4: flags += 1 << 6; break;
\r
58 case 8: flags += 2 << 6; break;
\r
59 default: flags += 3 << 6;
\r
64 #if defined(ASMV) || defined(ASMINF)
\r
73 #ifdef DYNAMIC_CRC_TABLE
\r
76 #ifdef NO_GZCOMPRESS
\r
82 #ifdef PKZIP_BUG_WORKAROUND
\r
89 # ifdef NO_vsnprintf
\r
91 # ifdef HAS_vsprintf_void
\r
95 # ifdef HAS_vsnprintf_void
\r
101 # ifdef NO_snprintf
\r
103 # ifdef HAS_sprintf_void
\r
107 # ifdef HAS_snprintf_void
\r
120 int z_verbose = verbose;
\r
125 fprintf(stderr, "%s\n", m);
\r
130 /* exported to allow conversion of error code to string for compress() and
\r
133 const char * ZEXPORT zError(err)
\r
136 return ERR_MSG(err);
\r
139 #if defined(_WIN32_WCE)
\r
140 /* The Microsoft C Run-Time Library for Windows CE doesn't have
\r
141 * errno. We define it as a global variable to simplify porting.
\r
142 * Its value is always 0 and should not be used.
\r
147 #ifndef HAVE_MEMCPY
\r
149 void zmemcpy(dest, source, len)
\r
151 const Bytef* source;
\r
154 if (len == 0) return;
\r
156 *dest++ = *source++; /* ??? to be unrolled */
\r
157 } while (--len != 0);
\r
160 int zmemcmp(s1, s2, len)
\r
167 for (j = 0; j < len; j++) {
\r
168 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
\r
173 void zmemzero(dest, len)
\r
177 if (len == 0) return;
\r
179 *dest++ = 0; /* ??? to be unrolled */
\r
180 } while (--len != 0);
\r
188 /* Turbo C in 16-bit mode */
\r
190 # define MY_ZCALLOC
\r
192 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
\r
193 * and farmalloc(64K) returns a pointer with an offset of 8, so we
\r
194 * must fix the pointer. Warning: the pointer must be put back to its
\r
195 * original form in order to free it, use zcfree().
\r
199 /* 10*64K = 640K */
\r
201 local int next_ptr = 0;
\r
203 typedef struct ptr_table_s {
\r
208 local ptr_table table[MAX_PTR];
\r
209 /* This table is used to remember the original form of pointers
\r
210 * to large buffers (64K). Such pointers are normalized with a zero offset.
\r
211 * Since MSDOS is not a preemptive multitasking OS, this table is not
\r
212 * protected from concurrent access. This hack doesn't work anyway on
\r
213 * a protected system like OS/2. Use Microsoft C instead.
\r
216 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
\r
218 voidpf buf = opaque; /* just to make some compilers happy */
\r
219 ulg bsize = (ulg)items*size;
\r
221 /* If we allocate less than 65520 bytes, we assume that farmalloc
\r
222 * will return a usable pointer which doesn't have to be normalized.
\r
224 if (bsize < 65520L) {
\r
225 buf = farmalloc(bsize);
\r
226 if (*(ush*)&buf != 0) return buf;
\r
228 buf = farmalloc(bsize + 16L);
\r
230 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
\r
231 table[next_ptr].org_ptr = buf;
\r
233 /* Normalize the pointer to seg:0 */
\r
234 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
\r
236 table[next_ptr++].new_ptr = buf;
\r
240 void zcfree (voidpf opaque, voidpf ptr)
\r
243 if (*(ush*)&ptr != 0) { /* object < 64K */
\r
247 /* Find the original pointer */
\r
248 for (n = 0; n < next_ptr; n++) {
\r
249 if (ptr != table[n].new_ptr) continue;
\r
251 farfree(table[n].org_ptr);
\r
252 while (++n < next_ptr) {
\r
253 table[n-1] = table[n];
\r
258 ptr = opaque; /* just to make some compilers happy */
\r
259 Assert(0, "zcfree: ptr not found");
\r
262 #endif /* __TURBOC__ */
\r
266 /* Microsoft C in 16-bit mode */
\r
268 # define MY_ZCALLOC
\r
270 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
\r
271 # define _halloc halloc
\r
272 # define _hfree hfree
\r
275 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
\r
277 if (opaque) opaque = 0; /* to make compiler happy */
\r
278 return _halloc((long)items, size);
\r
281 void zcfree (voidpf opaque, voidpf ptr)
\r
283 if (opaque) opaque = 0; /* to make compiler happy */
\r
289 #endif /* SYS16BIT */
\r
292 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
\r
295 extern voidp malloc OF((uInt size));
\r
296 extern voidp calloc OF((uInt items, uInt size));
\r
297 extern void free OF((voidpf ptr));
\r
300 voidpf zcalloc (opaque, items, size)
\r
305 if (opaque) items += size - size; /* make compiler happy */
\r
306 return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
\r
307 (voidpf)calloc(items, size);
\r
310 void zcfree (opaque, ptr)
\r
315 if (opaque) return; /* make compiler happy */
\r
318 #endif /* MY_ZCALLOC */
\r