]>
git.saurik.com Git - apple/xnu.git/blob - libkern/zlib/zutil.c
2 * Copyright (c) 2008-2016 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
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.
177 zmemcpy(Bytef
* dest
, const Bytef
* source
, uInt len
)
179 if (len
== 0) return;
181 *dest
++ = *source
++; /* ??? to be unrolled */
182 } while (--len
!= 0);
186 zmemcmp(const Bytef
* s1
, const Bytef
* s2
, uInt len
)
190 for (j
= 0; j
< len
; j
++) {
191 if (s1
[j
] != s2
[j
]) return 2*(s1
[j
] > s2
[j
])-1;
197 zmemzero(Bytef
* dest
, uInt len
)
199 if (len
== 0) return;
201 *dest
++ = 0; /* ??? to be unrolled */
202 } while (--len
!= 0);
211 /* Turbo C in 16-bit mode */
215 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
216 * and farmalloc(64K) returns a pointer with an offset of 8, so we
217 * must fix the pointer. Warning: the pointer must be put back to its
218 * original form in order to free it, use zcfree().
224 local
int next_ptr
= 0;
226 typedef struct ptr_table_s
{
231 local ptr_table table
[MAX_PTR
];
232 /* This table is used to remember the original form of pointers
233 * to large buffers (64K). Such pointers are normalized with a zero offset.
234 * Since MSDOS is not a preemptive multitasking OS, this table is not
235 * protected from concurrent access. This hack doesn't work anyway on
236 * a protected system like OS/2. Use Microsoft C instead.
240 zcalloc(voidpf opaque
, unsigned items
, unsigned size
)
242 voidpf buf
= opaque
; /* just to make some compilers happy */
243 ulg bsize
= (ulg
)items
*size
;
245 /* If we allocate less than 65520 bytes, we assume that farmalloc
246 * will return a usable pointer which doesn't have to be normalized.
248 if (bsize
< 65520L) {
249 buf
= farmalloc(bsize
);
250 if (*(ush
*)&buf
!= 0) return buf
;
252 buf
= farmalloc(bsize
+ 16L);
254 if (buf
== NULL
|| next_ptr
>= MAX_PTR
) return NULL
;
255 table
[next_ptr
].org_ptr
= buf
;
257 /* Normalize the pointer to seg:0 */
258 *((ush
*)&buf
+1) += ((ush
)((uch
*)buf
-0) + 15) >> 4;
260 table
[next_ptr
++].new_ptr
= buf
;
265 zcfree(voidpf opaque
, voidpf ptr
)
268 if (*(ush
*)&ptr
!= 0) { /* object < 64K */
272 /* Find the original pointer */
273 for (n
= 0; n
< next_ptr
; n
++) {
274 if (ptr
!= table
[n
].new_ptr
) continue;
276 farfree(table
[n
].org_ptr
);
277 while (++n
< next_ptr
) {
278 table
[n
-1] = table
[n
];
283 ptr
= opaque
; /* just to make some compilers happy */
284 Assert(0, "zcfree: ptr not found");
287 #endif /* __TURBOC__ */
291 /* Microsoft C in 16-bit mode */
295 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
296 # define _halloc halloc
297 # define _hfree hfree
301 zcalloc(voidpf opaque
, unsigned items
, unsigned size
)
303 if (opaque
) opaque
= 0; /* to make compiler happy */
304 return _halloc((long)items
, size
);
308 zcfree(voidpf opaque
, voidpf ptr
)
310 if (opaque
) opaque
= 0; /* to make compiler happy */
316 #endif /* SYS16BIT */
319 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
322 extern voidp malloc
OF((uInt size
));
323 extern voidp calloc
OF((uInt items
, uInt size
));
324 extern void free
OF((voidpf ptr
));
328 zcalloc(voidpf opaque
, unsigned items
, unsigned size
)
330 if (opaque
) items
+= size
- size
; /* make compiler happy */
331 if (sizeof(uInt
) > 2) {
333 to prevent use of uninitialized memory, malloc and bzero
335 voidpf p
= malloc(items
* size
);
336 bzero(p
, items
* size
);
339 return (voidpf
)calloc(items
, size
);
343 zcfree(voidpf opaque
, voidpf ptr
)
346 if (opaque
) return; /* make compiler happy */
349 #endif /* MY_ZCALLOC */
351 #endif /* NO_CZFUNCS */