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