]> git.saurik.com Git - apple/security.git/blob - SecurityServer/MacYarrow/zlib/zutil.c
Security-28.tar.gz
[apple/security.git] / SecurityServer / MacYarrow / zlib / zutil.c
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 /* zutil.c -- target dependent utility functions for the compression library
20 * Copyright (C) 1995-1998 Jean-loup Gailly.
21 * For conditions of distribution and use, see copyright notice in zlib.h
22 */
23
24 /* @(#) $Id: zutil.c,v 1.1.1.1 2001/05/18 23:14:03 mb Exp $ */
25
26 #include "zutil.h"
27
28 struct internal_state {int dummy;}; /* for buggy compilers */
29
30 #ifndef STDC
31 extern void exit OF((int));
32 #endif
33
34 const char *z_errmsg[10] = {
35 "need dictionary", /* Z_NEED_DICT 2 */
36 "stream end", /* Z_STREAM_END 1 */
37 "", /* Z_OK 0 */
38 "file error", /* Z_ERRNO (-1) */
39 "stream error", /* Z_STREAM_ERROR (-2) */
40 "data error", /* Z_DATA_ERROR (-3) */
41 "insufficient memory", /* Z_MEM_ERROR (-4) */
42 "buffer error", /* Z_BUF_ERROR (-5) */
43 "incompatible version",/* Z_VERSION_ERROR (-6) */
44 ""};
45
46
47 const char * ZEXPORT zlibVersion()
48 {
49 return ZLIB_VERSION;
50 }
51
52 #ifdef DEBUG
53
54 # ifndef verbose
55 # define verbose 0
56 # endif
57 int z_verbose = verbose;
58
59 void z_error (m)
60 char *m;
61 {
62 fprintf(stderr, "%s\n", m);
63 exit(1);
64 }
65 #endif
66
67 /* exported to allow conversion of error code to string for compress() and
68 * uncompress()
69 */
70 const char * ZEXPORT zError(err)
71 int err;
72 {
73 return ERR_MSG(err);
74 }
75
76
77 #ifndef HAVE_MEMCPY
78
79 void zmemcpy(dest, source, len)
80 Bytef* dest;
81 const Bytef* source;
82 uInt len;
83 {
84 if (len == 0) return;
85 do {
86 *dest++ = *source++; /* ??? to be unrolled */
87 } while (--len != 0);
88 }
89
90 int zmemcmp(s1, s2, len)
91 const Bytef* s1;
92 const Bytef* s2;
93 uInt len;
94 {
95 uInt j;
96
97 for (j = 0; j < len; j++) {
98 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
99 }
100 return 0;
101 }
102
103 void zmemzero(dest, len)
104 Bytef* dest;
105 uInt len;
106 {
107 if (len == 0) return;
108 do {
109 *dest++ = 0; /* ??? to be unrolled */
110 } while (--len != 0);
111 }
112 #endif
113
114 #ifdef __TURBOC__
115 #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
116 /* Small and medium model in Turbo C are for now limited to near allocation
117 * with reduced MAX_WBITS and MAX_MEM_LEVEL
118 */
119 # define MY_ZCALLOC
120
121 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
122 * and farmalloc(64K) returns a pointer with an offset of 8, so we
123 * must fix the pointer. Warning: the pointer must be put back to its
124 * original form in order to free it, use zcfree().
125 */
126
127 #define MAX_PTR 10
128 /* 10*64K = 640K */
129
130 local int next_ptr = 0;
131
132 typedef struct ptr_table_s {
133 voidpf org_ptr;
134 voidpf new_ptr;
135 } ptr_table;
136
137 local ptr_table table[MAX_PTR];
138 /* This table is used to remember the original form of pointers
139 * to large buffers (64K). Such pointers are normalized with a zero offset.
140 * Since MSDOS is not a preemptive multitasking OS, this table is not
141 * protected from concurrent access. This hack doesn't work anyway on
142 * a protected system like OS/2. Use Microsoft C instead.
143 */
144
145 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
146 {
147 voidpf buf = opaque; /* just to make some compilers happy */
148 ulg bsize = (ulg)items*size;
149
150 /* If we allocate less than 65520 bytes, we assume that farmalloc
151 * will return a usable pointer which doesn't have to be normalized.
152 */
153 if (bsize < 65520L) {
154 buf = farmalloc(bsize);
155 if (*(ush*)&buf != 0) return buf;
156 } else {
157 buf = farmalloc(bsize + 16L);
158 }
159 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
160 table[next_ptr].org_ptr = buf;
161
162 /* Normalize the pointer to seg:0 */
163 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
164 *(ush*)&buf = 0;
165 table[next_ptr++].new_ptr = buf;
166 return buf;
167 }
168
169 void zcfree (voidpf opaque, voidpf ptr)
170 {
171 int n;
172 if (*(ush*)&ptr != 0) { /* object < 64K */
173 farfree(ptr);
174 return;
175 }
176 /* Find the original pointer */
177 for (n = 0; n < next_ptr; n++) {
178 if (ptr != table[n].new_ptr) continue;
179
180 farfree(table[n].org_ptr);
181 while (++n < next_ptr) {
182 table[n-1] = table[n];
183 }
184 next_ptr--;
185 return;
186 }
187 ptr = opaque; /* just to make some compilers happy */
188 Assert(0, "zcfree: ptr not found");
189 }
190 #endif
191 #endif /* __TURBOC__ */
192
193
194 #if defined(M_I86) && !defined(__32BIT__)
195 /* Microsoft C in 16-bit mode */
196
197 # define MY_ZCALLOC
198
199 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
200 # define _halloc halloc
201 # define _hfree hfree
202 #endif
203
204 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
205 {
206 if (opaque) opaque = 0; /* to make compiler happy */
207 return _halloc((long)items, size);
208 }
209
210 void zcfree (voidpf opaque, voidpf ptr)
211 {
212 if (opaque) opaque = 0; /* to make compiler happy */
213 _hfree(ptr);
214 }
215
216 #endif /* MSC */
217
218
219 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
220
221 #ifndef STDC
222 extern voidp calloc OF((uInt items, uInt size));
223 extern void free OF((voidpf ptr));
224 #endif
225
226 voidpf zcalloc (opaque, items, size)
227 voidpf opaque;
228 unsigned items;
229 unsigned size;
230 {
231 if (opaque) items += size - size; /* make compiler happy */
232 return (voidpf)calloc(items, size);
233 }
234
235 void zcfree (opaque, ptr)
236 voidpf opaque;
237 voidpf ptr;
238 {
239 free(ptr);
240 if (opaque) return; /* make compiler happy */
241 }
242
243 #endif /* MY_ZCALLOC */