1 /* zutil.c -- target dependent utility functions for the compression library
2 * Copyright (C) 1995-2003 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 */
15 extern void exit OF((int));
18 const char * const z_errmsg[10] = {
19 "need dictionary", /* Z_NEED_DICT 2 */
20 "stream end", /* Z_STREAM_END 1 */
22 "file error", /* Z_ERRNO (-1) */
23 "stream error", /* Z_STREAM_ERROR (-2) */
24 "data error", /* Z_DATA_ERROR (-3) */
25 "insufficient memory", /* Z_MEM_ERROR (-4) */
26 "buffer error", /* Z_BUF_ERROR (-5) */
27 "incompatible version",/* Z_VERSION_ERROR (-6) */
31 const char * ZEXPORT zlibVersion()
36 uLong ZEXPORT zlibCompileFlags()
41 switch (sizeof(uInt)) {
43 case 4: flags += 1; break;
44 case 8: flags += 2; break;
47 switch (sizeof(uLong)) {
49 case 4: flags += 1 << 2; break;
50 case 8: flags += 2 << 2; break;
51 default: flags += 3 << 2;
53 switch (sizeof(voidpf)) {
55 case 4: flags += 1 << 4; break;
56 case 8: flags += 2 << 4; break;
57 default: flags += 3 << 4;
59 switch (sizeof(z_off_t)) {
61 case 4: flags += 1 << 6; break;
62 case 8: flags += 2 << 6; break;
63 default: flags += 3 << 6;
71 #ifdef DYNAMIC_CRC_TABLE
80 #ifdef PKZIP_BUG_WORKAROUND
89 # ifdef HAS_vsprintf_void
93 # ifdef HAS_vsnprintf_void
101 # ifdef HAS_sprintf_void
105 # ifdef HAS_snprintf_void
118 int z_verbose = verbose;
123 fprintf(stderr, "%s\n", m);
128 /* exported to allow conversion of error code to string for compress() and
131 const char * ZEXPORT zError(err)
137 #if defined(_WIN32_WCE)
138 /* does not exist on WCE */
144 void zmemcpy(dest, source, len)
149 if (len == 0) return;
151 *dest++ = *source++; /* ??? to be unrolled */
152 } while (--len != 0);
155 int zmemcmp(s1, s2, len)
162 for (j = 0; j < len; j++) {
163 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
168 void zmemzero(dest, len)
172 if (len == 0) return;
174 *dest++ = 0; /* ??? to be unrolled */
175 } while (--len != 0);
180 #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
181 #if !defined(__linux)
182 /* Small and medium model in Turbo C are for now limited to near allocation
183 * with reduced MAX_WBITS and MAX_MEM_LEVEL
187 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
188 * and farmalloc(64K) returns a pointer with an offset of 8, so we
189 * must fix the pointer. Warning: the pointer must be put back to its
190 * original form in order to free it, use zcfree().
196 local int next_ptr = 0;
198 typedef struct ptr_table_s {
203 local ptr_table table[MAX_PTR];
204 /* This table is used to remember the original form of pointers
205 * to large buffers (64K). Such pointers are normalized with a zero offset.
206 * Since MSDOS is not a preemptive multitasking OS, this table is not
207 * protected from concurrent access. This hack doesn't work anyway on
208 * a protected system like OS/2. Use Microsoft C instead.
211 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
213 voidpf buf = opaque; /* just to make some compilers happy */
214 ulg bsize = (ulg)items*size;
216 /* If we allocate less than 65520 bytes, we assume that farmalloc
217 * will return a usable pointer which doesn't have to be normalized.
219 if (bsize < 65520L) {
220 buf = farmalloc(bsize);
221 if (*(ush*)&buf != 0) return buf;
223 buf = farmalloc(bsize + 16L);
225 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
226 table[next_ptr].org_ptr = buf;
228 /* Normalize the pointer to seg:0 */
229 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
231 table[next_ptr++].new_ptr = buf;
235 void zcfree (voidpf opaque, voidpf ptr)
238 if (*(ush*)&ptr != 0) { /* object < 64K */
242 /* Find the original pointer */
243 for (n = 0; n < next_ptr; n++) {
244 if (ptr != table[n].new_ptr) continue;
246 farfree(table[n].org_ptr);
247 while (++n < next_ptr) {
248 table[n-1] = table[n];
253 ptr = opaque; /* just to make some compilers happy */
254 Assert(0, "zcfree: ptr not found");
258 #endif /* __TURBOC__ */
261 #if defined(M_I86) && !defined(__32BIT__)
262 /* Microsoft C in 16-bit mode */
266 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
267 # define _halloc halloc
268 # define _hfree hfree
271 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
273 if (opaque) opaque = 0; /* to make compiler happy */
274 return _halloc((long)items, size);
277 void zcfree (voidpf opaque, voidpf ptr)
279 if (opaque) opaque = 0; /* to make compiler happy */
286 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
289 extern voidp malloc OF((uInt size));
290 extern voidp calloc OF((uInt items, uInt size));
291 extern void free OF((voidpf ptr));
294 voidpf zcalloc (opaque, items, size)
299 if (opaque) items += size - size; /* make compiler happy */
300 return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
301 (voidpf)calloc(items, size);
304 void zcfree (opaque, ptr)
309 if (opaque) return; /* make compiler happy */
312 #endif /* MY_ZCALLOC */