1 /* zutil.c -- target dependent utility functions for the compression library
2 * Copyright (C) 1995-1996 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
12 struct internal_state {int dummy;}; /* for buggy compilers */
15 extern void exit OF((int));
18 const char *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 *zlibVersion()
40 fprintf(stderr, "%s\n", m);
47 void zmemcpy(dest, source, len)
54 *dest++ = *source++; /* ??? to be unrolled */
58 int zmemcmp(s1, s2, len)
65 for (j = 0; j < len; j++) {
66 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
71 void zmemzero(dest, len)
77 *dest++ = 0; /* ??? to be unrolled */
83 #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
84 /* Small and medium model in Turbo C are for now limited to near allocation
85 * with reduced MAX_WBITS and MAX_MEM_LEVEL
89 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
90 * and farmalloc(64K) returns a pointer with an offset of 8, so we
91 * must fix the pointer. Warning: the pointer must be put back to its
92 * original form in order to free it, use zcfree().
98 local int next_ptr = 0;
100 typedef struct ptr_table_s {
105 local ptr_table table[MAX_PTR];
106 /* This table is used to remember the original form of pointers
107 * to large buffers (64K). Such pointers are normalized with a zero offset.
108 * Since MSDOS is not a preemptive multitasking OS, this table is not
109 * protected from concurrent access. This hack doesn't work anyway on
110 * a protected system like OS/2. Use Microsoft C instead.
113 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
115 voidpf buf = opaque; /* just to make some compilers happy */
116 ulg bsize = (ulg)items*size;
118 /* If we allocate less than 65520 bytes, we assume that farmalloc
119 * will return a usable pointer which doesn't have to be normalized.
121 if (bsize < 65520L) {
122 buf = farmalloc(bsize);
123 if (*(ush*)&buf != 0) return buf;
125 buf = farmalloc(bsize + 16L);
127 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
128 table[next_ptr].org_ptr = buf;
130 /* Normalize the pointer to seg:0 */
131 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
133 table[next_ptr++].new_ptr = buf;
137 void zcfree (voidpf opaque, voidpf ptr)
140 if (*(ush*)&ptr != 0) { /* object < 64K */
144 /* Find the original pointer */
145 for (n = 0; n < next_ptr; n++) {
146 if (ptr != table[n].new_ptr) continue;
148 farfree(table[n].org_ptr);
149 while (++n < next_ptr) {
150 table[n-1] = table[n];
155 ptr = opaque; /* just to make some compilers happy */
156 Assert(0, "zcfree: ptr not found");
159 #endif /* __TURBOC__ */
162 #if defined(M_I86) && !defined(__32BIT__)
163 /* Microsoft C in 16-bit mode */
167 #if (!defined(_MSC_VER) || (_MSC_VER < 600))
168 # define _halloc halloc
169 # define _hfree hfree
172 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
174 if (opaque) opaque = 0; /* to make compiler happy */
175 return _halloc((long)items, size);
178 void zcfree (voidpf opaque, voidpf ptr)
180 if (opaque) opaque = 0; /* to make compiler happy */
187 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
190 extern voidp calloc OF((uInt items, uInt size));
191 extern void free OF((voidpf ptr));
194 voidpf zcalloc (opaque, items, size)
199 if (opaque) items += size - size; /* make compiler happy */
200 return (voidpf)calloc(items, size);
203 void zcfree (opaque, ptr)
208 if (opaque) return; /* make compiler happy */
211 #endif /* MY_ZCALLOC */