]>
Commit | Line | Data |
---|---|---|
b0d623f7 A |
1 | /* |
2 | * Copyright (c) 2008 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
27 | */ | |
28 | /* zutil.c -- target dependent utility functions for the compression library | |
29 | * Copyright (C) 1995-2005 Jean-loup Gailly. | |
30 | * For conditions of distribution and use, see copyright notice in zlib.h | |
31 | */ | |
32 | ||
33 | /* @(#) $Id$ */ | |
34 | ||
35 | #include "zutil.h" | |
36 | ||
37 | #ifndef NO_DUMMY_DECL | |
38 | struct internal_state {int dummy;}; /* for buggy compilers */ | |
39 | #endif | |
40 | ||
41 | const char * const z_errmsg[10] = { | |
42 | "need dictionary", /* Z_NEED_DICT 2 */ | |
43 | "stream end", /* Z_STREAM_END 1 */ | |
44 | "", /* Z_OK 0 */ | |
45 | "file error", /* Z_ERRNO (-1) */ | |
46 | "stream error", /* Z_STREAM_ERROR (-2) */ | |
47 | "data error", /* Z_DATA_ERROR (-3) */ | |
48 | "insufficient memory", /* Z_MEM_ERROR (-4) */ | |
49 | "buffer error", /* Z_BUF_ERROR (-5) */ | |
50 | "incompatible version",/* Z_VERSION_ERROR (-6) */ | |
51 | ""}; | |
52 | ||
53 | ||
54 | const char * ZEXPORT zlibVersion() | |
55 | { | |
56 | return ZLIB_VERSION; | |
57 | } | |
58 | ||
59 | uLong ZEXPORT zlibCompileFlags() | |
60 | { | |
61 | uLong flags; | |
62 | ||
63 | flags = 0; | |
64 | switch (sizeof(uInt)) { | |
65 | case 2: break; | |
66 | case 4: flags += 1; break; | |
67 | case 8: flags += 2; break; | |
68 | default: flags += 3; | |
69 | } | |
70 | switch (sizeof(uLong)) { | |
71 | case 2: break; | |
72 | case 4: flags += 1 << 2; break; | |
73 | case 8: flags += 2 << 2; break; | |
74 | default: flags += 3 << 2; | |
75 | } | |
76 | switch (sizeof(voidpf)) { | |
77 | case 2: break; | |
78 | case 4: flags += 1 << 4; break; | |
79 | case 8: flags += 2 << 4; break; | |
80 | default: flags += 3 << 4; | |
81 | } | |
82 | switch (sizeof(z_off_t)) { | |
83 | case 2: break; | |
84 | case 4: flags += 1 << 6; break; | |
85 | case 8: flags += 2 << 6; break; | |
86 | default: flags += 3 << 6; | |
87 | } | |
88 | #ifdef DEBUG | |
89 | flags += 1 << 8; | |
90 | #endif | |
91 | #if defined(ASMV) || defined(ASMINF) | |
92 | flags += 1 << 9; | |
93 | #endif | |
94 | #ifdef ZLIB_WINAPI | |
95 | flags += 1 << 10; | |
96 | #endif | |
97 | #ifdef BUILDFIXED | |
98 | flags += 1 << 12; | |
99 | #endif | |
100 | #ifdef DYNAMIC_CRC_TABLE | |
101 | flags += 1 << 13; | |
102 | #endif | |
103 | #ifdef NO_GZCOMPRESS | |
104 | flags += 1L << 16; | |
105 | #endif | |
106 | #ifdef NO_GZIP | |
107 | flags += 1L << 17; | |
108 | #endif | |
109 | #ifdef PKZIP_BUG_WORKAROUND | |
110 | flags += 1L << 20; | |
111 | #endif | |
112 | #ifdef FASTEST | |
113 | flags += 1L << 21; | |
114 | #endif | |
115 | #ifdef STDC | |
116 | # ifdef NO_vsnprintf | |
117 | flags += 1L << 25; | |
118 | # ifdef HAS_vsprintf_void | |
119 | flags += 1L << 26; | |
120 | # endif | |
121 | # else | |
122 | # ifdef HAS_vsnprintf_void | |
123 | flags += 1L << 26; | |
124 | # endif | |
125 | # endif | |
126 | #else | |
127 | flags += 1L << 24; | |
128 | # ifdef NO_snprintf | |
129 | flags += 1L << 25; | |
130 | # ifdef HAS_sprintf_void | |
131 | flags += 1L << 26; | |
132 | # endif | |
133 | # else | |
134 | # ifdef HAS_snprintf_void | |
135 | flags += 1L << 26; | |
136 | # endif | |
137 | # endif | |
138 | #endif | |
139 | return flags; | |
140 | } | |
141 | ||
142 | #ifdef DEBUG | |
143 | ||
144 | # ifndef verbose | |
145 | # define verbose 0 | |
146 | # endif | |
147 | int z_verbose = verbose; | |
148 | ||
149 | void z_error (m) | |
150 | char *m; | |
151 | { | |
152 | fprintf(stderr, "%s\n", m); | |
153 | exit(1); | |
154 | } | |
155 | #endif | |
156 | ||
157 | /* exported to allow conversion of error code to string for compress() and | |
158 | * uncompress() | |
159 | */ | |
160 | const char * ZEXPORT zError(err) | |
161 | int err; | |
162 | { | |
163 | return ERR_MSG(err); | |
164 | } | |
165 | ||
166 | #if defined(_WIN32_WCE) | |
167 | /* The Microsoft C Run-Time Library for Windows CE doesn't have | |
168 | * errno. We define it as a global variable to simplify porting. | |
169 | * Its value is always 0 and should not be used. | |
170 | */ | |
171 | int errno = 0; | |
172 | #endif | |
173 | ||
174 | #ifndef HAVE_MEMCPY | |
175 | ||
176 | void zmemcpy(dest, source, len) | |
177 | Bytef* dest; | |
178 | const Bytef* source; | |
179 | uInt len; | |
180 | { | |
181 | if (len == 0) return; | |
182 | do { | |
183 | *dest++ = *source++; /* ??? to be unrolled */ | |
184 | } while (--len != 0); | |
185 | } | |
186 | ||
187 | int zmemcmp(s1, s2, len) | |
188 | const Bytef* s1; | |
189 | const Bytef* s2; | |
190 | uInt len; | |
191 | { | |
192 | uInt j; | |
193 | ||
194 | for (j = 0; j < len; j++) { | |
195 | if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; | |
196 | } | |
197 | return 0; | |
198 | } | |
199 | ||
200 | void zmemzero(dest, len) | |
201 | Bytef* dest; | |
202 | uInt len; | |
203 | { | |
204 | if (len == 0) return; | |
205 | do { | |
206 | *dest++ = 0; /* ??? to be unrolled */ | |
207 | } while (--len != 0); | |
208 | } | |
209 | #endif | |
210 | ||
211 | #ifndef NO_ZCFUNCS | |
212 | ||
213 | #ifdef SYS16BIT | |
214 | ||
215 | #ifdef __TURBOC__ | |
216 | /* Turbo C in 16-bit mode */ | |
217 | ||
218 | # define MY_ZCALLOC | |
219 | ||
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 | */ | |
225 | ||
226 | #define MAX_PTR 10 | |
227 | /* 10*64K = 640K */ | |
228 | ||
229 | local int next_ptr = 0; | |
230 | ||
231 | typedef struct ptr_table_s { | |
232 | voidpf org_ptr; | |
233 | voidpf new_ptr; | |
234 | } ptr_table; | |
235 | ||
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 | */ | |
243 | ||
244 | voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) | |
245 | { | |
246 | voidpf buf = opaque; /* just to make some compilers happy */ | |
247 | ulg bsize = (ulg)items*size; | |
248 | ||
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) return buf; | |
255 | } else { | |
256 | buf = farmalloc(bsize + 16L); | |
257 | } | |
258 | if (buf == NULL || next_ptr >= MAX_PTR) return NULL; | |
259 | table[next_ptr].org_ptr = buf; | |
260 | ||
261 | /* Normalize the pointer to seg:0 */ | |
262 | *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; | |
263 | *(ush*)&buf = 0; | |
264 | table[next_ptr++].new_ptr = buf; | |
265 | return buf; | |
266 | } | |
267 | ||
268 | void zcfree (voidpf opaque, voidpf ptr) | |
269 | { | |
270 | int n; | |
271 | if (*(ush*)&ptr != 0) { /* object < 64K */ | |
272 | farfree(ptr); | |
273 | return; | |
274 | } | |
275 | /* Find the original pointer */ | |
276 | for (n = 0; n < next_ptr; n++) { | |
277 | if (ptr != table[n].new_ptr) continue; | |
278 | ||
279 | farfree(table[n].org_ptr); | |
280 | while (++n < next_ptr) { | |
281 | table[n-1] = table[n]; | |
282 | } | |
283 | next_ptr--; | |
284 | return; | |
285 | } | |
286 | ptr = opaque; /* just to make some compilers happy */ | |
287 | Assert(0, "zcfree: ptr not found"); | |
288 | } | |
289 | ||
290 | #endif /* __TURBOC__ */ | |
291 | ||
292 | ||
293 | #ifdef M_I86 | |
294 | /* Microsoft C in 16-bit mode */ | |
295 | ||
296 | # define MY_ZCALLOC | |
297 | ||
298 | #if (!defined(_MSC_VER) || (_MSC_VER <= 600)) | |
299 | # define _halloc halloc | |
300 | # define _hfree hfree | |
301 | #endif | |
302 | ||
303 | voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) | |
304 | { | |
305 | if (opaque) opaque = 0; /* to make compiler happy */ | |
306 | return _halloc((long)items, size); | |
307 | } | |
308 | ||
309 | void zcfree (voidpf opaque, voidpf ptr) | |
310 | { | |
311 | if (opaque) opaque = 0; /* to make compiler happy */ | |
312 | _hfree(ptr); | |
313 | } | |
314 | ||
315 | #endif /* M_I86 */ | |
316 | ||
317 | #endif /* SYS16BIT */ | |
318 | ||
319 | ||
320 | #ifndef MY_ZCALLOC /* Any system without a special alloc function */ | |
321 | ||
322 | #ifndef STDC | |
323 | extern voidp malloc OF((uInt size)); | |
324 | extern voidp calloc OF((uInt items, uInt size)); | |
325 | extern void free OF((voidpf ptr)); | |
326 | #endif | |
327 | ||
328 | voidpf zcalloc (opaque, items, size) | |
329 | voidpf opaque; | |
330 | unsigned items; | |
331 | unsigned size; | |
332 | { | |
333 | if (opaque) items += size - size; /* make compiler happy */ | |
334 | return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : | |
335 | (voidpf)calloc(items, size); | |
336 | } | |
337 | ||
338 | void zcfree (opaque, ptr) | |
339 | voidpf opaque; | |
340 | voidpf ptr; | |
341 | { | |
342 | free(ptr); | |
343 | if (opaque) return; /* make compiler happy */ | |
344 | } | |
345 | ||
346 | #endif /* MY_ZCALLOC */ | |
347 | ||
348 | #endif /* NO_CZFUNCS */ |