1/*
2 * Copyright (c) 2008-2016 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
38struct internal_state {int dummy;}; /* for buggy compilers */
39#endif
40
41const 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
54const char * ZEXPORT zlibVersion()
55{
56 return ZLIB_VERSION;
57}
58
59uLong 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
147int z_verbose = verbose;
148
149void
150z_error(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 */
160const char * ZEXPORT
161zError(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
176void
177zmemcpy(Bytef* dest, const Bytef* source, uInt len)
178{
179 if (len == 0) return;
180 do {
181 *dest++ = *source++; /* ??? to be unrolled */
182 } while (--len != 0);
183}
184
185int
186zmemcmp(const Bytef* s1, const Bytef* s2, uInt len)
187{
188 uInt j;
189
190 for (j = 0; j < len; j++) {
191 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
192 }
193 return 0;
194}
195
196void
197zmemzero(Bytef* dest, uInt len)
198{
199 if (len == 0) return;
200 do {
201 *dest++ = 0; /* ??? to be unrolled */
202 } while (--len != 0);
203}
204#endif
205
206#ifndef NO_ZCFUNCS
207
208#ifdef SYS16BIT
209
210#ifdef __TURBOC__
211/* Turbo C in 16-bit mode */
212
213# define MY_ZCALLOC
214
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().
219 */
220
221#define MAX_PTR 10
222/* 10*64K = 640K */
223
224local int next_ptr = 0;
225
226typedef struct ptr_table_s {
227 voidpf org_ptr;
228 voidpf new_ptr;
229} ptr_table;
230
231local 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.
237 */
238
239voidpf
240zcalloc(voidpf opaque, unsigned items, unsigned size)
241{
242 voidpf buf = opaque; /* just to make some compilers happy */
243 ulg bsize = (ulg)items*size;
244
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.
247 */
248 if (bsize < 65520L) {
249 buf = farmalloc(bsize);
250 if (*(ush*)&buf != 0) return buf;
251 } else {
252 buf = farmalloc(bsize + 16L);
253 }
254 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
255 table[next_ptr].org_ptr = buf;
256
257 /* Normalize the pointer to seg:0 */
258 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
259 *(ush*)&buf = 0;
260 table[next_ptr++].new_ptr = buf;
261 return buf;
262}
263
264void
265zcfree(voidpf opaque, voidpf ptr)
266{
267 int n;
268 if (*(ush*)&ptr != 0) { /* object < 64K */
269 farfree(ptr);
270 return;
271 }
272 /* Find the original pointer */
273 for (n = 0; n < next_ptr; n++) {
274 if (ptr != table[n].new_ptr) continue;
275
276 farfree(table[n].org_ptr);
277 while (++n < next_ptr) {
278 table[n-1] = table[n];
279 }
280 next_ptr--;
281 return;
282 }
283 ptr = opaque; /* just to make some compilers happy */
284 Assert(0, "zcfree: ptr not found");
285}
286
287#endif /* __TURBOC__ */
288
289
290#ifdef M_I86
291/* Microsoft C in 16-bit mode */
292
293# define MY_ZCALLOC
294
295#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
296# define _halloc halloc
297# define _hfree hfree
298#endif
299
300voidpf
301zcalloc(voidpf opaque, unsigned items, unsigned size)
302{
303 if (opaque) opaque = 0; /* to make compiler happy */
304 return _halloc((long)items, size);
305}
306
307void
308zcfree(voidpf opaque, voidpf ptr)
309{
310 if (opaque) opaque = 0; /* to make compiler happy */
311 _hfree(ptr);
312}
313
314#endif /* M_I86 */
315
316#endif /* SYS16BIT */
317
318
319#ifndef MY_ZCALLOC /* Any system without a special alloc function */
320
321#ifndef STDC
322extern voidp malloc OF((uInt size));
323extern voidp calloc OF((uInt items, uInt size));
324extern void free OF((voidpf ptr));
325#endif
326
327voidpf
328zcalloc(voidpf opaque, unsigned items, unsigned size)
329{
330 if (opaque) items += size - size; /* make compiler happy */
331 if (sizeof(uInt) > 2) {
332 /*
333 to prevent use of uninitialized memory, malloc and bzero
334 */
335 voidpf p = malloc(items * size);
336 bzero(p, items * size);
337 return p;
338 } else
339 return (voidpf)calloc(items, size);
340}
341
342void
343zcfree(voidpf opaque, voidpf ptr)
344{
345 free(ptr);
346 if (opaque) return; /* make compiler happy */
347}
348
349#endif /* MY_ZCALLOC */
350
351#endif /* NO_CZFUNCS */
352