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