1 /* zutil.c -- target dependent utility functions for the compression library
2 * Copyright (C) 1995-2005, 2010 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
6 /* @(#) $Id$ */
8 #include "zutil.h"
10 #ifndef NO_DUMMY_DECL
11 struct internal_state {
12 int dummy;
13 }; /* for buggy compilers */
14 #endif
16 const char *const z_errmsg[10] = {
17 "need dictionary", /* Z_NEED_DICT 2 */
18 "stream end", /* Z_STREAM_END 1 */
19 "", /* Z_OK 0 */
20 "file error", /* Z_ERRNO (-1) */
21 "stream error", /* Z_STREAM_ERROR (-2) */
22 "data error", /* Z_DATA_ERROR (-3) */
23 "insufficient memory", /* Z_MEM_ERROR (-4) */
24 "buffer error", /* Z_BUF_ERROR (-5) */
25 "incompatible version", /* Z_VERSION_ERROR (-6) */
26 ""
27 };
29 const char *ZEXPORT zlibVersion()
30 {
31 return ZLIB_VERSION;
32 }
34 uLong ZEXPORT zlibCompileFlags()
35 {
36 uLong flags;
38 flags = 0;
39 switch ((int)(sizeof(uInt))) {
40 case 2:
41 break;
42 case 4:
43 flags += 1;
44 break;
45 case 8:
46 flags += 2;
47 break;
48 default:
49 flags += 3;
50 }
51 switch ((int)(sizeof(uLong))) {
52 case 2:
53 break;
54 case 4:
55 flags += 1 << 2;
56 break;
57 case 8:
58 flags += 2 << 2;
59 break;
60 default:
61 flags += 3 << 2;
62 }
63 switch ((int)(sizeof(voidpf))) {
64 case 2:
65 break;
66 case 4:
67 flags += 1 << 4;
68 break;
69 case 8:
70 flags += 2 << 4;
71 break;
72 default:
73 flags += 3 << 4;
74 }
75 switch ((int)(sizeof(z_off_t))) {
76 case 2:
77 break;
78 case 4:
79 flags += 1 << 6;
80 break;
81 case 8:
82 flags += 2 << 6;
83 break;
84 default:
85 flags += 3 << 6;
86 }
87 #ifdef DEBUG
88 flags += 1 << 8;
89 #endif
90 #if defined(ASMV) || defined(ASMINF)
91 flags += 1 << 9;
92 #endif
93 #ifdef ZLIB_WINAPI
94 flags += 1 << 10;
95 #endif
96 #ifdef BUILDFIXED
97 flags += 1 << 12;
98 #endif
99 #ifdef DYNAMIC_CRC_TABLE
100 flags += 1 << 13;
101 #endif
102 #ifdef NO_GZCOMPRESS
103 flags += 1L << 16;
104 #endif
105 #ifdef NO_GZIP
106 flags += 1L << 17;
107 #endif
108 #ifdef PKZIP_BUG_WORKAROUND
109 flags += 1L << 20;
110 #endif
111 #ifdef FASTEST
112 flags += 1L << 21;
113 #endif
114 #ifdef STDC
115 #ifdef NO_vsnprintf
116 flags += 1L << 25;
117 #ifdef HAS_vsprintf_void
118 flags += 1L << 26;
119 #endif
120 #else
121 #ifdef HAS_vsnprintf_void
122 flags += 1L << 26;
123 #endif
124 #endif
125 #else
126 flags += 1L << 24;
127 #ifdef NO_snprintf
128 flags += 1L << 25;
129 #ifdef HAS_sprintf_void
130 flags += 1L << 26;
131 #endif
132 #else
133 #ifdef HAS_snprintf_void
134 flags += 1L << 26;
135 #endif
136 #endif
137 #endif
138 return flags;
139 }
141 #ifdef DEBUG
143 #ifndef verbose
144 #define verbose 0
145 #endif
146 int ZLIB_INTERNAL z_verbose = verbose;
148 void ZLIB_INTERNAL z_error(m)
149 char *m;
150 {
151 fprintf(stderr, "%s\n", m);
152 exit(1);
153 }
154 #endif
156 /* exported to allow conversion of error code to string for compress() and
157 * uncompress()
158 */
159 const char *ZEXPORT zError(err)
160 int err;
161 {
162 return ERR_MSG(err);
163 }
165 #if defined(_WIN32_WCE)
166 /* The Microsoft C Run-Time Library for Windows CE doesn't have
167 * errno. We define it as a global variable to simplify porting.
168 * Its value is always 0 and should not be used.
169 */
170 int errno = 0;
171 #endif
173 #ifndef HAVE_MEMCPY
175 void ZLIB_INTERNAL zmemcpy(dest, source, len)
176 Bytef *dest;
177 const Bytef *source;
178 uInt len;
179 {
180 if (len == 0)
181 return;
182 do {
183 *dest++ = *source++; /* ??? to be unrolled */
184 } while (--len != 0);
185 }
187 int ZLIB_INTERNAL zmemcmp(s1, s2, len)
188 const Bytef *s1;
189 const Bytef *s2;
190 uInt len;
191 {
192 uInt j;
194 for (j = 0; j < len; j++) {
195 if (s1[j] != s2[j])
196 return 2 * (s1[j] > s2[j]) - 1;
197 }
198 return 0;
199 }
201 void ZLIB_INTERNAL zmemzero(dest, len)
202 Bytef *dest;
203 uInt len;
204 {
205 if (len == 0)
206 return;
207 do {
208 *dest++ = 0; /* ??? to be unrolled */
209 } while (--len != 0);
210 }
211 #endif
213 #ifdef SYS16BIT
215 #ifdef __TURBOC__
216 /* Turbo C in 16-bit mode */
218 #define MY_ZCALLOC
220 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
221 * and farmalloc(64K) returns a pointer with an offset of 8, so we
222 * must fix the pointer. Warning: the pointer must be put back to its
223 * original form in order to free it, use zcfree().
224 */
226 #define MAX_PTR 10
227 /* 10*64K = 640K */
229 local int next_ptr = 0;
231 typedef struct ptr_table_s {
232 voidpf org_ptr;
233 voidpf new_ptr;
234 } ptr_table;
236 local ptr_table table[MAX_PTR];
237 /* This table is used to remember the original form of pointers
238 * to large buffers (64K). Such pointers are normalized with a zero offset.
239 * Since MSDOS is not a preemptive multitasking OS, this table is not
240 * protected from concurrent access. This hack doesn't work anyway on
241 * a protected system like OS/2. Use Microsoft C instead.
242 */
244 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size)
245 {
246 voidpf buf = opaque; /* just to make some compilers happy */
247 ulg bsize = (ulg) items * size;
249 /* If we allocate less than 65520 bytes, we assume that farmalloc
250 * will return a usable pointer which doesn't have to be normalized.
251 */
252 if (bsize < 65520L) {
253 buf = farmalloc(bsize);
254 if (*(ush *) & buf != 0)
255 return buf;
256 } else {
257 buf = farmalloc(bsize + 16L);
258 }
259 if (buf == NULL || next_ptr >= MAX_PTR)
260 return NULL;
261 table[next_ptr].org_ptr = buf;
263 /* Normalize the pointer to seg:0 */
264 *((ush *) & buf + 1) += ((ush) ((uch *) buf - 0) + 15) >> 4;
265 *(ush *) & buf = 0;
266 table[next_ptr++].new_ptr = buf;
267 return buf;
268 }
270 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
271 {
272 int n;
273 if (*(ush *) & ptr != 0) { /* object < 64K */
274 farfree(ptr);
275 return;
276 }
277 /* Find the original pointer */
278 for (n = 0; n < next_ptr; n++) {
279 if (ptr != table[n].new_ptr)
280 continue;
282 farfree(table[n].org_ptr);
283 while (++n < next_ptr) {
284 table[n - 1] = table[n];
285 }
286 next_ptr--;
287 return;
288 }
289 ptr = opaque; /* just to make some compilers happy */
290 Assert(0, "zcfree: ptr not found");
291 }
293 #endif /* __TURBOC__ */
295 #ifdef M_I86
296 /* Microsoft C in 16-bit mode */
298 #define MY_ZCALLOC
300 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
301 #define _halloc halloc
302 #define _hfree hfree
303 #endif
305 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size)
306 {
307 if (opaque)
308 opaque = 0; /* to make compiler happy */
309 return _halloc((long)items, size);
310 }
312 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
313 {
314 if (opaque)
315 opaque = 0; /* to make compiler happy */
316 _hfree(ptr);
317 }
319 #endif /* M_I86 */
321 #endif /* SYS16BIT */
323 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
325 #ifndef STDC
326 extern voidp malloc OF((uInt size));
327 extern voidp calloc OF((uInt items, uInt size));
328 extern void free OF((voidpf ptr));
329 #endif
331 voidpf ZLIB_INTERNAL zcalloc(opaque, items, size)
332 voidpf opaque;
333 unsigned items;
334 unsigned size;
335 {
336 if (opaque)
337 items += size - size; /* make compiler happy */
338 return sizeof(uInt) > 2 ? (voidpf) malloc(items * size) :
339 (voidpf) calloc(items, size);
340 }
342 void ZLIB_INTERNAL zcfree(opaque, ptr)
343 voidpf opaque;
344 voidpf ptr;
345 {
346 free(ptr);
347 if (opaque)
348 return; /* make compiler happy */
349 }
351 #endif /* MY_ZCALLOC */