|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 * |
|
4 * This code is free software; you can redistribute it and/or modify it |
|
5 * under the terms of the GNU General Public License version 2 only, as |
|
6 * published by the Free Software Foundation. Oracle designates this |
|
7 * particular file as subject to the "Classpath" exception as provided |
|
8 * by Oracle in the LICENSE file that accompanied this code. |
|
9 * |
|
10 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 * version 2 for more details (a copy is included in the LICENSE file that |
|
14 * accompanied this code). |
|
15 * |
|
16 * You should have received a copy of the GNU General Public License version |
|
17 * 2 along with this work; if not, write to the Free Software Foundation, |
|
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 * |
|
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
21 * or visit www.oracle.com if you need additional information or have any |
|
22 * questions. |
|
23 */ |
|
24 |
|
25 /* zutil.c -- target dependent utility functions for the compression library |
|
26 * Copyright (C) 1995-2005, 2010 Jean-loup Gailly. |
|
27 * For conditions of distribution and use, see copyright notice in zlib.h |
|
28 */ |
|
29 |
|
30 /* @(#) $Id$ */ |
|
31 |
|
32 #include "zutil.h" |
|
33 |
|
34 #ifndef NO_DUMMY_DECL |
|
35 struct internal_state {int dummy;}; /* for buggy compilers */ |
|
36 #endif |
|
37 |
|
38 const char * const z_errmsg[10] = { |
|
39 "need dictionary", /* Z_NEED_DICT 2 */ |
|
40 "stream end", /* Z_STREAM_END 1 */ |
|
41 "", /* Z_OK 0 */ |
|
42 "file error", /* Z_ERRNO (-1) */ |
|
43 "stream error", /* Z_STREAM_ERROR (-2) */ |
|
44 "data error", /* Z_DATA_ERROR (-3) */ |
|
45 "insufficient memory", /* Z_MEM_ERROR (-4) */ |
|
46 "buffer error", /* Z_BUF_ERROR (-5) */ |
|
47 "incompatible version",/* Z_VERSION_ERROR (-6) */ |
|
48 ""}; |
|
49 |
|
50 |
|
51 const char * ZEXPORT zlibVersion() |
|
52 { |
|
53 return ZLIB_VERSION; |
|
54 } |
|
55 |
|
56 uLong ZEXPORT zlibCompileFlags() |
|
57 { |
|
58 uLong flags; |
|
59 |
|
60 flags = 0; |
|
61 switch ((int)(sizeof(uInt))) { |
|
62 case 2: break; |
|
63 case 4: flags += 1; break; |
|
64 case 8: flags += 2; break; |
|
65 default: flags += 3; |
|
66 } |
|
67 switch ((int)(sizeof(uLong))) { |
|
68 case 2: break; |
|
69 case 4: flags += 1 << 2; break; |
|
70 case 8: flags += 2 << 2; break; |
|
71 default: flags += 3 << 2; |
|
72 } |
|
73 switch ((int)(sizeof(voidpf))) { |
|
74 case 2: break; |
|
75 case 4: flags += 1 << 4; break; |
|
76 case 8: flags += 2 << 4; break; |
|
77 default: flags += 3 << 4; |
|
78 } |
|
79 switch ((int)(sizeof(z_off_t))) { |
|
80 case 2: break; |
|
81 case 4: flags += 1 << 6; break; |
|
82 case 8: flags += 2 << 6; break; |
|
83 default: flags += 3 << 6; |
|
84 } |
|
85 #ifdef DEBUG |
|
86 flags += 1 << 8; |
|
87 #endif |
|
88 #if defined(ASMV) || defined(ASMINF) |
|
89 flags += 1 << 9; |
|
90 #endif |
|
91 #ifdef ZLIB_WINAPI |
|
92 flags += 1 << 10; |
|
93 #endif |
|
94 #ifdef BUILDFIXED |
|
95 flags += 1 << 12; |
|
96 #endif |
|
97 #ifdef DYNAMIC_CRC_TABLE |
|
98 flags += 1 << 13; |
|
99 #endif |
|
100 #ifdef NO_GZCOMPRESS |
|
101 flags += 1L << 16; |
|
102 #endif |
|
103 #ifdef NO_GZIP |
|
104 flags += 1L << 17; |
|
105 #endif |
|
106 #ifdef PKZIP_BUG_WORKAROUND |
|
107 flags += 1L << 20; |
|
108 #endif |
|
109 #ifdef FASTEST |
|
110 flags += 1L << 21; |
|
111 #endif |
|
112 #ifdef STDC |
|
113 # ifdef NO_vsnprintf |
|
114 flags += 1L << 25; |
|
115 # ifdef HAS_vsprintf_void |
|
116 flags += 1L << 26; |
|
117 # endif |
|
118 # else |
|
119 # ifdef HAS_vsnprintf_void |
|
120 flags += 1L << 26; |
|
121 # endif |
|
122 # endif |
|
123 #else |
|
124 flags += 1L << 24; |
|
125 # ifdef NO_snprintf |
|
126 flags += 1L << 25; |
|
127 # ifdef HAS_sprintf_void |
|
128 flags += 1L << 26; |
|
129 # endif |
|
130 # else |
|
131 # ifdef HAS_snprintf_void |
|
132 flags += 1L << 26; |
|
133 # endif |
|
134 # endif |
|
135 #endif |
|
136 return flags; |
|
137 } |
|
138 |
|
139 #ifdef DEBUG |
|
140 |
|
141 # ifndef verbose |
|
142 # define verbose 0 |
|
143 # endif |
|
144 int ZLIB_INTERNAL z_verbose = verbose; |
|
145 |
|
146 void ZLIB_INTERNAL z_error (m) |
|
147 char *m; |
|
148 { |
|
149 fprintf(stderr, "%s\n", m); |
|
150 exit(1); |
|
151 } |
|
152 #endif |
|
153 |
|
154 /* exported to allow conversion of error code to string for compress() and |
|
155 * uncompress() |
|
156 */ |
|
157 const char * ZEXPORT zError(err) |
|
158 int err; |
|
159 { |
|
160 return ERR_MSG(err); |
|
161 } |
|
162 |
|
163 #if defined(_WIN32_WCE) |
|
164 /* The Microsoft C Run-Time Library for Windows CE doesn't have |
|
165 * errno. We define it as a global variable to simplify porting. |
|
166 * Its value is always 0 and should not be used. |
|
167 */ |
|
168 int errno = 0; |
|
169 #endif |
|
170 |
|
171 #ifndef HAVE_MEMCPY |
|
172 |
|
173 void ZLIB_INTERNAL zmemcpy(dest, source, len) |
|
174 Bytef* dest; |
|
175 const Bytef* source; |
|
176 uInt len; |
|
177 { |
|
178 if (len == 0) return; |
|
179 do { |
|
180 *dest++ = *source++; /* ??? to be unrolled */ |
|
181 } while (--len != 0); |
|
182 } |
|
183 |
|
184 int ZLIB_INTERNAL zmemcmp(s1, s2, len) |
|
185 const Bytef* s1; |
|
186 const Bytef* s2; |
|
187 uInt len; |
|
188 { |
|
189 uInt j; |
|
190 |
|
191 for (j = 0; j < len; j++) { |
|
192 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; |
|
193 } |
|
194 return 0; |
|
195 } |
|
196 |
|
197 void ZLIB_INTERNAL zmemzero(dest, len) |
|
198 Bytef* dest; |
|
199 uInt len; |
|
200 { |
|
201 if (len == 0) return; |
|
202 do { |
|
203 *dest++ = 0; /* ??? to be unrolled */ |
|
204 } while (--len != 0); |
|
205 } |
|
206 #endif |
|
207 |
|
208 |
|
209 #ifdef SYS16BIT |
|
210 |
|
211 #ifdef __TURBOC__ |
|
212 /* Turbo C in 16-bit mode */ |
|
213 |
|
214 # define MY_ZCALLOC |
|
215 |
|
216 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes |
|
217 * and farmalloc(64K) returns a pointer with an offset of 8, so we |
|
218 * must fix the pointer. Warning: the pointer must be put back to its |
|
219 * original form in order to free it, use zcfree(). |
|
220 */ |
|
221 |
|
222 #define MAX_PTR 10 |
|
223 /* 10*64K = 640K */ |
|
224 |
|
225 local int next_ptr = 0; |
|
226 |
|
227 typedef struct ptr_table_s { |
|
228 voidpf org_ptr; |
|
229 voidpf new_ptr; |
|
230 } ptr_table; |
|
231 |
|
232 local ptr_table table[MAX_PTR]; |
|
233 /* This table is used to remember the original form of pointers |
|
234 * to large buffers (64K). Such pointers are normalized with a zero offset. |
|
235 * Since MSDOS is not a preemptive multitasking OS, this table is not |
|
236 * protected from concurrent access. This hack doesn't work anyway on |
|
237 * a protected system like OS/2. Use Microsoft C instead. |
|
238 */ |
|
239 |
|
240 voidpf ZLIB_INTERNAL zcalloc (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 |
|
264 void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) |
|
265 { |
|
266 int n; |
|
267 if (*(ush*)&ptr != 0) { /* object < 64K */ |
|
268 farfree(ptr); |
|
269 return; |
|
270 } |
|
271 /* Find the original pointer */ |
|
272 for (n = 0; n < next_ptr; n++) { |
|
273 if (ptr != table[n].new_ptr) continue; |
|
274 |
|
275 farfree(table[n].org_ptr); |
|
276 while (++n < next_ptr) { |
|
277 table[n-1] = table[n]; |
|
278 } |
|
279 next_ptr--; |
|
280 return; |
|
281 } |
|
282 ptr = opaque; /* just to make some compilers happy */ |
|
283 Assert(0, "zcfree: ptr not found"); |
|
284 } |
|
285 |
|
286 #endif /* __TURBOC__ */ |
|
287 |
|
288 |
|
289 #ifdef M_I86 |
|
290 /* Microsoft C in 16-bit mode */ |
|
291 |
|
292 # define MY_ZCALLOC |
|
293 |
|
294 #if (!defined(_MSC_VER) || (_MSC_VER <= 600)) |
|
295 # define _halloc halloc |
|
296 # define _hfree hfree |
|
297 #endif |
|
298 |
|
299 voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size) |
|
300 { |
|
301 if (opaque) opaque = 0; /* to make compiler happy */ |
|
302 return _halloc((long)items, size); |
|
303 } |
|
304 |
|
305 void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) |
|
306 { |
|
307 if (opaque) opaque = 0; /* to make compiler happy */ |
|
308 _hfree(ptr); |
|
309 } |
|
310 |
|
311 #endif /* M_I86 */ |
|
312 |
|
313 #endif /* SYS16BIT */ |
|
314 |
|
315 |
|
316 #ifndef MY_ZCALLOC /* Any system without a special alloc function */ |
|
317 |
|
318 #ifndef STDC |
|
319 extern voidp malloc OF((uInt size)); |
|
320 extern voidp calloc OF((uInt items, uInt size)); |
|
321 extern void free OF((voidpf ptr)); |
|
322 #endif |
|
323 |
|
324 voidpf ZLIB_INTERNAL zcalloc (opaque, items, size) |
|
325 voidpf opaque; |
|
326 unsigned items; |
|
327 unsigned size; |
|
328 { |
|
329 if (opaque) items += size - size; /* make compiler happy */ |
|
330 return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : |
|
331 (voidpf)calloc(items, size); |
|
332 } |
|
333 |
|
334 void ZLIB_INTERNAL zcfree (opaque, ptr) |
|
335 voidpf opaque; |
|
336 voidpf ptr; |
|
337 { |
|
338 free(ptr); |
|
339 if (opaque) return; /* make compiler happy */ |
|
340 } |
|
341 |
|
342 #endif /* MY_ZCALLOC */ |