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