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