Adding upstream version 3.50~pre5.
[syslinux-debian/hramrach.git] / com32 / lib / zlib / zutil.c
blob19654060d3a6ed320dab08bdaa8949d7ffe79cea
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
4 */
7 #include "zutil.h"
9 #ifndef NO_DUMMY_DECL
10 struct internal_state {int dummy;}; /* for buggy compilers */
11 #endif
13 #ifndef STDC
14 extern void exit OF((int));
15 #endif
17 const char * const z_errmsg[10] = {
18 "need dictionary", /* Z_NEED_DICT 2 */
19 "stream end", /* Z_STREAM_END 1 */
20 "", /* Z_OK 0 */
21 "file error", /* Z_ERRNO (-1) */
22 "stream error", /* Z_STREAM_ERROR (-2) */
23 "data error", /* Z_DATA_ERROR (-3) */
24 "insufficient memory", /* Z_MEM_ERROR (-4) */
25 "buffer error", /* Z_BUF_ERROR (-5) */
26 "incompatible version",/* Z_VERSION_ERROR (-6) */
27 ""};
30 const char * ZEXPORT zlibVersion()
32 return ZLIB_VERSION;
35 uLong ZEXPORT zlibCompileFlags()
37 uLong flags;
39 flags = 0;
40 switch (sizeof(uInt)) {
41 case 2: break;
42 case 4: flags += 1; break;
43 case 8: flags += 2; break;
44 default: flags += 3;
46 switch (sizeof(uLong)) {
47 case 2: break;
48 case 4: flags += 1 << 2; break;
49 case 8: flags += 2 << 2; break;
50 default: flags += 3 << 2;
52 switch (sizeof(voidpf)) {
53 case 2: break;
54 case 4: flags += 1 << 4; break;
55 case 8: flags += 2 << 4; break;
56 default: flags += 3 << 4;
58 switch (sizeof(z_off_t)) {
59 case 2: break;
60 case 4: flags += 1 << 6; break;
61 case 8: flags += 2 << 6; break;
62 default: flags += 3 << 6;
64 #ifdef DEBUG
65 flags += 1 << 8;
66 #endif
67 #if defined(ASMV) || defined(ASMINF)
68 flags += 1 << 9;
69 #endif
70 #ifdef ZLIB_WINAPI
71 flags += 1 << 10;
72 #endif
73 #ifdef BUILDFIXED
74 flags += 1 << 12;
75 #endif
76 #ifdef DYNAMIC_CRC_TABLE
77 flags += 1 << 13;
78 #endif
79 #ifdef NO_GZCOMPRESS
80 flags += 1 << 16;
81 #endif
82 #ifdef NO_GZIP
83 flags += 1 << 17;
84 #endif
85 #ifdef PKZIP_BUG_WORKAROUND
86 flags += 1 << 20;
87 #endif
88 #ifdef FASTEST
89 flags += 1 << 21;
90 #endif
91 #ifdef STDC
92 # ifdef NO_vsnprintf
93 flags += 1 << 25;
94 # ifdef HAS_vsprintf_void
95 flags += 1 << 26;
96 # endif
97 # else
98 # ifdef HAS_vsnprintf_void
99 flags += 1 << 26;
100 # endif
101 # endif
102 #else
103 flags += 1 << 24;
104 # ifdef NO_snprintf
105 flags += 1 << 25;
106 # ifdef HAS_sprintf_void
107 flags += 1 << 26;
108 # endif
109 # else
110 # ifdef HAS_snprintf_void
111 flags += 1 << 26;
112 # endif
113 # endif
114 #endif
115 return flags;
118 #ifdef DEBUG
120 # ifndef verbose
121 # define verbose 0
122 # endif
123 int z_verbose = verbose;
125 void z_error (m)
126 char *m;
128 fprintf(stderr, "%s\n", m);
129 exit(1);
131 #endif
133 /* exported to allow conversion of error code to string for compress() and
134 * uncompress()
136 const char * ZEXPORT zError(err)
137 int err;
139 return ERR_MSG(err);
142 #if defined(_WIN32_WCE)
143 /* does not exist on WCE */
144 int errno = 0;
145 #endif
147 #ifndef HAVE_MEMCPY
149 void zmemcpy(dest, source, len)
150 Bytef* dest;
151 const Bytef* source;
152 uInt len;
154 if (len == 0) return;
155 do {
156 *dest++ = *source++; /* ??? to be unrolled */
157 } while (--len != 0);
160 int zmemcmp(s1, s2, len)
161 const Bytef* s1;
162 const Bytef* s2;
163 uInt len;
165 uInt j;
167 for (j = 0; j < len; j++) {
168 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
170 return 0;
173 void zmemzero(dest, len)
174 Bytef* dest;
175 uInt len;
177 if (len == 0) return;
178 do {
179 *dest++ = 0; /* ??? to be unrolled */
180 } while (--len != 0);
182 #endif
185 #ifdef SYS16BIT
187 #ifdef __TURBOC__
188 /* Turbo C in 16-bit mode */
190 # define MY_ZCALLOC
192 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
193 * and farmalloc(64K) returns a pointer with an offset of 8, so we
194 * must fix the pointer. Warning: the pointer must be put back to its
195 * original form in order to free it, use zcfree().
198 #define MAX_PTR 10
199 /* 10*64K = 640K */
201 local int next_ptr = 0;
203 typedef struct ptr_table_s {
204 voidpf org_ptr;
205 voidpf new_ptr;
206 } ptr_table;
208 local ptr_table table[MAX_PTR];
209 /* This table is used to remember the original form of pointers
210 * to large buffers (64K). Such pointers are normalized with a zero offset.
211 * Since MSDOS is not a preemptive multitasking OS, this table is not
212 * protected from concurrent access. This hack doesn't work anyway on
213 * a protected system like OS/2. Use Microsoft C instead.
216 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
218 voidpf buf = opaque; /* just to make some compilers happy */
219 ulg bsize = (ulg)items*size;
221 /* If we allocate less than 65520 bytes, we assume that farmalloc
222 * will return a usable pointer which doesn't have to be normalized.
224 if (bsize < 65520L) {
225 buf = farmalloc(bsize);
226 if (*(ush*)&buf != 0) return buf;
227 } else {
228 buf = farmalloc(bsize + 16L);
230 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
231 table[next_ptr].org_ptr = buf;
233 /* Normalize the pointer to seg:0 */
234 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
235 *(ush*)&buf = 0;
236 table[next_ptr++].new_ptr = buf;
237 return buf;
240 void zcfree (voidpf opaque, voidpf ptr)
242 int n;
243 if (*(ush*)&ptr != 0) { /* object < 64K */
244 farfree(ptr);
245 return;
247 /* Find the original pointer */
248 for (n = 0; n < next_ptr; n++) {
249 if (ptr != table[n].new_ptr) continue;
251 farfree(table[n].org_ptr);
252 while (++n < next_ptr) {
253 table[n-1] = table[n];
255 next_ptr--;
256 return;
258 ptr = opaque; /* just to make some compilers happy */
259 Assert(0, "zcfree: ptr not found");
262 #endif /* __TURBOC__ */
265 #ifdef M_I86
266 /* Microsoft C in 16-bit mode */
268 # define MY_ZCALLOC
270 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
271 # define _halloc halloc
272 # define _hfree hfree
273 #endif
275 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
277 if (opaque) opaque = 0; /* to make compiler happy */
278 return _halloc((long)items, size);
281 void zcfree (voidpf opaque, voidpf ptr)
283 if (opaque) opaque = 0; /* to make compiler happy */
284 _hfree(ptr);
287 #endif /* M_I86 */
289 #endif /* SYS16BIT */
292 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
294 #ifndef STDC
295 extern voidp malloc OF((uInt size));
296 extern voidp calloc OF((uInt items, uInt size));
297 extern void free OF((voidpf ptr));
298 #endif
300 voidpf zcalloc (opaque, items, size)
301 voidpf opaque;
302 unsigned items;
303 unsigned size;
305 if (opaque) items += size - size; /* make compiler happy */
306 return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
307 (voidpf)calloc(items, size);
310 void zcfree (opaque, ptr)
311 voidpf opaque;
312 voidpf ptr;
314 free(ptr);
315 if (opaque) return; /* make compiler happy */
318 #endif /* MY_ZCALLOC */