1 /* zutil.c -- target dependent utility functions for the compression library
\r
2 * Copyright (C) 1995-2002 Jean-loup Gailly.
\r
3 * For conditions of distribution and use, see copyright notice in zlib.h
\r
6 /* @(#) $Id: zutil.c,v 1.1 2003/05/23 21:05:30 steveking Exp $ */
\r
10 struct internal_state {int dummy;}; /* for buggy compilers */
\r
13 extern void exit OF((int));
\r
16 const char *z_errmsg[10] = {
\r
17 "need dictionary", /* Z_NEED_DICT 2 */
\r
18 "stream end", /* Z_STREAM_END 1 */
\r
20 "file error", /* Z_ERRNO (-1) */
\r
21 "stream error", /* Z_STREAM_ERROR (-2) */
\r
22 "data error", /* Z_DATA_ERROR (-3) */
\r
23 "insufficient memory", /* Z_MEM_ERROR (-4) */
\r
24 "buffer error", /* Z_BUF_ERROR (-5) */
\r
25 "incompatible version",/* Z_VERSION_ERROR (-6) */
\r
29 const char * ZEXPORT zlibVersion()
\r
31 return ZLIB_VERSION;
\r
39 int z_verbose = verbose;
\r
44 fprintf(stderr, "%s\n", m);
\r
49 /* exported to allow conversion of error code to string for compress() and
\r
52 const char * ZEXPORT zError(err)
\r
55 return ERR_MSG(err);
\r
61 void zmemcpy(dest, source, len)
\r
63 const Bytef* source;
\r
66 if (len == 0) return;
\r
68 *dest++ = *source++; /* ??? to be unrolled */
\r
69 } while (--len != 0);
\r
72 int zmemcmp(s1, s2, len)
\r
79 for (j = 0; j < len; j++) {
\r
80 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
\r
85 void zmemzero(dest, len)
\r
89 if (len == 0) return;
\r
91 *dest++ = 0; /* ??? to be unrolled */
\r
92 } while (--len != 0);
\r
97 #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
\r
98 /* Small and medium model in Turbo C are for now limited to near allocation
\r
99 * with reduced MAX_WBITS and MAX_MEM_LEVEL
\r
101 # define MY_ZCALLOC
\r
103 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
\r
104 * and farmalloc(64K) returns a pointer with an offset of 8, so we
\r
105 * must fix the pointer. Warning: the pointer must be put back to its
\r
106 * original form in order to free it, use zcfree().
\r
110 /* 10*64K = 640K */
\r
112 local int next_ptr = 0;
\r
114 typedef struct ptr_table_s {
\r
119 local ptr_table table[MAX_PTR];
\r
120 /* This table is used to remember the original form of pointers
\r
121 * to large buffers (64K). Such pointers are normalized with a zero offset.
\r
122 * Since MSDOS is not a preemptive multitasking OS, this table is not
\r
123 * protected from concurrent access. This hack doesn't work anyway on
\r
124 * a protected system like OS/2. Use Microsoft C instead.
\r
127 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
\r
129 voidpf buf = opaque; /* just to make some compilers happy */
\r
130 ulg bsize = (ulg)items*size;
\r
132 /* If we allocate less than 65520 bytes, we assume that farmalloc
\r
133 * will return a usable pointer which doesn't have to be normalized.
\r
135 if (bsize < 65520L) {
\r
136 buf = farmalloc(bsize);
\r
137 if (*(ush*)&buf != 0) return buf;
\r
139 buf = farmalloc(bsize + 16L);
\r
141 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
\r
142 table[next_ptr].org_ptr = buf;
\r
144 /* Normalize the pointer to seg:0 */
\r
145 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
\r
147 table[next_ptr++].new_ptr = buf;
\r
151 void zcfree (voidpf opaque, voidpf ptr)
\r
154 if (*(ush*)&ptr != 0) { /* object < 64K */
\r
158 /* Find the original pointer */
\r
159 for (n = 0; n < next_ptr; n++) {
\r
160 if (ptr != table[n].new_ptr) continue;
\r
162 farfree(table[n].org_ptr);
\r
163 while (++n < next_ptr) {
\r
164 table[n-1] = table[n];
\r
169 ptr = opaque; /* just to make some compilers happy */
\r
170 Assert(0, "zcfree: ptr not found");
\r
173 #endif /* __TURBOC__ */
\r
176 #if defined(M_I86) && !defined(__32BIT__)
\r
177 /* Microsoft C in 16-bit mode */
\r
179 # define MY_ZCALLOC
\r
181 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
\r
182 # define _halloc halloc
\r
183 # define _hfree hfree
\r
186 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
\r
188 if (opaque) opaque = 0; /* to make compiler happy */
\r
189 return _halloc((long)items, size);
\r
192 void zcfree (voidpf opaque, voidpf ptr)
\r
194 if (opaque) opaque = 0; /* to make compiler happy */
\r
201 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
\r
204 extern voidp calloc OF((uInt items, uInt size));
\r
205 extern void free OF((voidpf ptr));
\r
208 voidpf zcalloc (opaque, items, size)
\r
213 if (opaque) items += size - size; /* make compiler happy */
\r
214 return (voidpf)calloc(items, size);
\r
217 void zcfree (opaque, ptr)
\r
222 if (opaque) return; /* make compiler happy */
\r
225 #endif /* MY_ZCALLOC */
\r