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. Sun designates this |
|
7 * particular file as subject to the "Classpath" exception as provided |
|
8 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
21 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
22 * have any questions. |
|
23 */ |
|
24 |
|
25 /* |
|
26 * This file is available under and governed by the GNU General Public |
|
27 * License version 2 only, as published by the Free Software Foundation. |
|
28 * However, the following notice accompanied the original version of this |
|
29 * file and, per its terms, should not be removed: |
|
30 * |
|
31 * zutil.c -- target dependent utility functions for the compression library |
|
32 * Copyright (C) 1995-1998 Jean-loup Gailly. |
|
33 * For conditions of distribution and use, see copyright notice in zlib.h |
|
34 */ |
|
35 |
|
36 #include "zutil.h" |
|
37 |
|
38 struct internal_state {int dummy;}; /* for buggy compilers */ |
|
39 |
|
40 #ifndef STDC |
|
41 extern void exit OF((int)); |
|
42 #endif |
|
43 |
|
44 const char *z_errmsg[10] = { |
|
45 "need dictionary", /* Z_NEED_DICT 2 */ |
|
46 "stream end", /* Z_STREAM_END 1 */ |
|
47 "", /* Z_OK 0 */ |
|
48 "file error", /* Z_ERRNO (-1) */ |
|
49 "stream error", /* Z_STREAM_ERROR (-2) */ |
|
50 "data error", /* Z_DATA_ERROR (-3) */ |
|
51 "insufficient memory", /* Z_MEM_ERROR (-4) */ |
|
52 "buffer error", /* Z_BUF_ERROR (-5) */ |
|
53 "incompatible version",/* Z_VERSION_ERROR (-6) */ |
|
54 ""}; |
|
55 |
|
56 |
|
57 const char * ZEXPORT zlibVersion() |
|
58 { |
|
59 return ZLIB_VERSION; |
|
60 } |
|
61 |
|
62 #ifdef DEBUG |
|
63 |
|
64 # ifndef verbose |
|
65 # define verbose 0 |
|
66 # endif |
|
67 int z_verbose = verbose; |
|
68 |
|
69 void z_error (m) |
|
70 char *m; |
|
71 { |
|
72 fprintf(stderr, "%s\n", m); |
|
73 exit(1); |
|
74 } |
|
75 #endif |
|
76 |
|
77 /* exported to allow conversion of error code to string for compress() and |
|
78 * uncompress() |
|
79 */ |
|
80 const char * ZEXPORT zError(err) |
|
81 int err; |
|
82 { |
|
83 return ERR_MSG(err); |
|
84 } |
|
85 |
|
86 |
|
87 #ifndef HAVE_MEMCPY |
|
88 |
|
89 void zmemcpy(dest, source, len) |
|
90 Bytef* dest; |
|
91 const Bytef* source; |
|
92 uInt len; |
|
93 { |
|
94 if (len == 0) return; |
|
95 do { |
|
96 *dest++ = *source++; /* ??? to be unrolled */ |
|
97 } while (--len != 0); |
|
98 } |
|
99 |
|
100 int zmemcmp(s1, s2, len) |
|
101 const Bytef* s1; |
|
102 const Bytef* s2; |
|
103 uInt len; |
|
104 { |
|
105 uInt j; |
|
106 |
|
107 for (j = 0; j < len; j++) { |
|
108 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; |
|
109 } |
|
110 return 0; |
|
111 } |
|
112 |
|
113 void zmemzero(dest, len) |
|
114 Bytef* dest; |
|
115 uInt len; |
|
116 { |
|
117 if (len == 0) return; |
|
118 do { |
|
119 *dest++ = 0; /* ??? to be unrolled */ |
|
120 } while (--len != 0); |
|
121 } |
|
122 #endif |
|
123 |
|
124 #ifdef __TURBOC__ |
|
125 #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__) |
|
126 /* Small and medium model in Turbo C are for now limited to near allocation |
|
127 * with reduced MAX_WBITS and MAX_MEM_LEVEL |
|
128 */ |
|
129 # define MY_ZCALLOC |
|
130 |
|
131 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes |
|
132 * and farmalloc(64K) returns a pointer with an offset of 8, so we |
|
133 * must fix the pointer. Warning: the pointer must be put back to its |
|
134 * original form in order to free it, use zcfree(). |
|
135 */ |
|
136 |
|
137 #define MAX_PTR 10 |
|
138 /* 10*64K = 640K */ |
|
139 |
|
140 local int next_ptr = 0; |
|
141 |
|
142 typedef struct ptr_table_s { |
|
143 voidpf org_ptr; |
|
144 voidpf new_ptr; |
|
145 } ptr_table; |
|
146 |
|
147 local ptr_table table[MAX_PTR]; |
|
148 /* This table is used to remember the original form of pointers |
|
149 * to large buffers (64K). Such pointers are normalized with a zero offset. |
|
150 * Since MSDOS is not a preemptive multitasking OS, this table is not |
|
151 * protected from concurrent access. This hack doesn't work anyway on |
|
152 * a protected system like OS/2. Use Microsoft C instead. |
|
153 */ |
|
154 |
|
155 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) |
|
156 { |
|
157 voidpf buf = opaque; /* just to make some compilers happy */ |
|
158 ulg bsize = (ulg)items*size; |
|
159 |
|
160 /* If we allocate less than 65520 bytes, we assume that farmalloc |
|
161 * will return a usable pointer which doesn't have to be normalized. |
|
162 */ |
|
163 if (bsize < 65520L) { |
|
164 buf = farmalloc(bsize); |
|
165 if (*(ush*)&buf != 0) return buf; |
|
166 } else { |
|
167 buf = farmalloc(bsize + 16L); |
|
168 } |
|
169 if (buf == NULL || next_ptr >= MAX_PTR) return NULL; |
|
170 table[next_ptr].org_ptr = buf; |
|
171 |
|
172 /* Normalize the pointer to seg:0 */ |
|
173 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; |
|
174 *(ush*)&buf = 0; |
|
175 table[next_ptr++].new_ptr = buf; |
|
176 return buf; |
|
177 } |
|
178 |
|
179 void zcfree (voidpf opaque, voidpf ptr) |
|
180 { |
|
181 int n; |
|
182 if (*(ush*)&ptr != 0) { /* object < 64K */ |
|
183 farfree(ptr); |
|
184 return; |
|
185 } |
|
186 /* Find the original pointer */ |
|
187 for (n = 0; n < next_ptr; n++) { |
|
188 if (ptr != table[n].new_ptr) continue; |
|
189 |
|
190 farfree(table[n].org_ptr); |
|
191 while (++n < next_ptr) { |
|
192 table[n-1] = table[n]; |
|
193 } |
|
194 next_ptr--; |
|
195 return; |
|
196 } |
|
197 ptr = opaque; /* just to make some compilers happy */ |
|
198 Assert(0, "zcfree: ptr not found"); |
|
199 } |
|
200 #endif |
|
201 #endif /* __TURBOC__ */ |
|
202 |
|
203 |
|
204 #if defined(M_I86) && !defined(__32BIT__) |
|
205 /* Microsoft C in 16-bit mode */ |
|
206 |
|
207 # define MY_ZCALLOC |
|
208 |
|
209 #if (!defined(_MSC_VER) || (_MSC_VER <= 600)) |
|
210 # define _halloc halloc |
|
211 # define _hfree hfree |
|
212 #endif |
|
213 |
|
214 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) |
|
215 { |
|
216 if (opaque) opaque = 0; /* to make compiler happy */ |
|
217 return _halloc((long)items, size); |
|
218 } |
|
219 |
|
220 void zcfree (voidpf opaque, voidpf ptr) |
|
221 { |
|
222 if (opaque) opaque = 0; /* to make compiler happy */ |
|
223 _hfree(ptr); |
|
224 } |
|
225 |
|
226 #endif /* MSC */ |
|
227 |
|
228 |
|
229 #ifndef MY_ZCALLOC /* Any system without a special alloc function */ |
|
230 |
|
231 #ifndef STDC |
|
232 extern voidp calloc OF((uInt items, uInt size)); |
|
233 extern void free OF((voidpf ptr)); |
|
234 #endif |
|
235 |
|
236 voidpf zcalloc (opaque, items, size) |
|
237 voidpf opaque; |
|
238 unsigned items; |
|
239 unsigned size; |
|
240 { |
|
241 if (opaque) items += size - size; /* make compiler happy */ |
|
242 return (voidpf)calloc(items, size); |
|
243 } |
|
244 |
|
245 void zcfree (opaque, ptr) |
|
246 voidpf opaque; |
|
247 voidpf ptr; |
|
248 { |
|
249 free(ptr); |
|
250 if (opaque) return; /* make compiler happy */ |
|
251 } |
|
252 |
|
253 #endif /* MY_ZCALLOC */ |
|