]>
git.saurik.com Git - apple/xnu.git/blob - libkern/zlib/zutil.c
f90ac37a73baf9bc33ab4647cf23cd4027ec4188
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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
38 struct internal_state
{int dummy
;}; /* for buggy compilers */
41 const char * const z_errmsg
[10] = {
42 "need dictionary", /* Z_NEED_DICT 2 */
43 "stream end", /* Z_STREAM_END 1 */
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) */
54 const char * ZEXPORT
zlibVersion()
59 uLong ZEXPORT
zlibCompileFlags()
64 switch (sizeof(uInt
)) {
66 case 4: flags
+= 1; break;
67 case 8: flags
+= 2; break;
70 switch (sizeof(uLong
)) {
72 case 4: flags
+= 1 << 2; break;
73 case 8: flags
+= 2 << 2; break;
74 default: flags
+= 3 << 2;
76 switch (sizeof(voidpf
)) {
78 case 4: flags
+= 1 << 4; break;
79 case 8: flags
+= 2 << 4; break;
80 default: flags
+= 3 << 4;
82 switch (sizeof(z_off_t
)) {
84 case 4: flags
+= 1 << 6; break;
85 case 8: flags
+= 2 << 6; break;
86 default: flags
+= 3 << 6;
91 #if defined(ASMV) || defined(ASMINF)
100 #ifdef DYNAMIC_CRC_TABLE
109 #ifdef PKZIP_BUG_WORKAROUND
118 # ifdef HAS_vsprintf_void
122 # ifdef HAS_vsnprintf_void
130 # ifdef HAS_sprintf_void
134 # ifdef HAS_snprintf_void
147 int z_verbose
= verbose
;
152 fprintf(stderr
, "%s\n", m
);
157 /* exported to allow conversion of error code to string for compress() and
160 const char * ZEXPORT
zError(err
)
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.
176 void zmemcpy(dest
, source
, len
)
181 if (len
== 0) return;
183 *dest
++ = *source
++; /* ??? to be unrolled */
184 } while (--len
!= 0);
187 int zmemcmp(s1
, s2
, len
)
194 for (j
= 0; j
< len
; j
++) {
195 if (s1
[j
] != s2
[j
]) return 2*(s1
[j
] > s2
[j
])-1;
200 void zmemzero(dest
, len
)
204 if (len
== 0) return;
206 *dest
++ = 0; /* ??? to be unrolled */
207 } while (--len
!= 0);
216 /* Turbo C in 16-bit mode */
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().
229 local
int next_ptr
= 0;
231 typedef struct ptr_table_s
{
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.
244 voidpf
zcalloc (voidpf opaque
, unsigned items
, unsigned size
)
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.
252 if (bsize
< 65520L) {
253 buf
= farmalloc(bsize
);
254 if (*(ush
*)&buf
!= 0) return buf
;
256 buf
= farmalloc(bsize
+ 16L);
258 if (buf
== NULL
|| next_ptr
>= MAX_PTR
) return NULL
;
259 table
[next_ptr
].org_ptr
= buf
;
261 /* Normalize the pointer to seg:0 */
262 *((ush
*)&buf
+1) += ((ush
)((uch
*)buf
-0) + 15) >> 4;
264 table
[next_ptr
++].new_ptr
= buf
;
268 void zcfree (voidpf opaque
, voidpf ptr
)
271 if (*(ush
*)&ptr
!= 0) { /* object < 64K */
275 /* Find the original pointer */
276 for (n
= 0; n
< next_ptr
; n
++) {
277 if (ptr
!= table
[n
].new_ptr
) continue;
279 farfree(table
[n
].org_ptr
);
280 while (++n
< next_ptr
) {
281 table
[n
-1] = table
[n
];
286 ptr
= opaque
; /* just to make some compilers happy */
287 Assert(0, "zcfree: ptr not found");
290 #endif /* __TURBOC__ */
294 /* Microsoft C in 16-bit mode */
298 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
299 # define _halloc halloc
300 # define _hfree hfree
303 voidpf
zcalloc (voidpf opaque
, unsigned items
, unsigned size
)
305 if (opaque
) opaque
= 0; /* to make compiler happy */
306 return _halloc((long)items
, size
);
309 void zcfree (voidpf opaque
, voidpf ptr
)
311 if (opaque
) opaque
= 0; /* to make compiler happy */
317 #endif /* SYS16BIT */
320 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
323 extern voidp malloc
OF((uInt size
));
324 extern voidp calloc
OF((uInt items
, uInt size
));
325 extern void free
OF((voidpf ptr
));
328 voidpf
zcalloc (opaque
, items
, size
)
333 if (opaque
) items
+= size
- size
; /* make compiler happy */
334 if (sizeof(uInt
) > 2) {
336 to prevent use of uninitialized memory, malloc and bzero
338 voidpf p
= malloc(items
* size
);
339 bzero(p
, items
* size
);
342 return (voidpf
)calloc(items
, size
);
345 void zcfree (opaque
, ptr
)
350 if (opaque
) return; /* make compiler happy */
353 #endif /* MY_ZCALLOC */
355 #endif /* NO_CZFUNCS */