OLD | NEW |
1 /* zutil.c -- target dependent utility functions for the compression library | 1 /* zutil.c -- target dependent utility functions for the compression library |
2 * Copyright (C) 1995-2005 Jean-loup Gailly. | 2 * Copyright (C) 1995-2005, 2010 Jean-loup Gailly. |
3 * For conditions of distribution and use, see copyright notice in zlib.h | 3 * For conditions of distribution and use, see copyright notice in zlib.h |
4 */ | 4 */ |
5 | 5 |
6 /* @(#) $Id: zutil.c,v 3.11 2005/08/04 19:14:14 tor%cs.brown.edu Exp $ */ | 6 /* @(#) $Id$ */ |
7 | 7 |
8 #include "zutil.h" | 8 #include "zutil.h" |
9 | 9 |
10 #ifndef NO_DUMMY_DECL | 10 #ifndef NO_DUMMY_DECL |
11 struct internal_state {int dummy;}; /* for buggy compilers */ | 11 struct internal_state {int dummy;}; /* for buggy compilers */ |
12 #endif | 12 #endif |
13 | 13 |
14 const char * const z_errmsg[10] = { | 14 const char * const z_errmsg[10] = { |
15 "need dictionary", /* Z_NEED_DICT 2 */ | 15 "need dictionary", /* Z_NEED_DICT 2 */ |
16 "stream end", /* Z_STREAM_END 1 */ | 16 "stream end", /* Z_STREAM_END 1 */ |
(...skipping 10 matching lines...) Expand all Loading... |
27 const char * ZEXPORT zlibVersion() | 27 const char * ZEXPORT zlibVersion() |
28 { | 28 { |
29 return ZLIB_VERSION; | 29 return ZLIB_VERSION; |
30 } | 30 } |
31 | 31 |
32 uLong ZEXPORT zlibCompileFlags() | 32 uLong ZEXPORT zlibCompileFlags() |
33 { | 33 { |
34 uLong flags; | 34 uLong flags; |
35 | 35 |
36 flags = 0; | 36 flags = 0; |
37 switch (sizeof(uInt)) { | 37 switch ((int)(sizeof(uInt))) { |
38 case 2: break; | 38 case 2: break; |
39 case 4: flags += 1; break; | 39 case 4: flags += 1; break; |
40 case 8: flags += 2; break; | 40 case 8: flags += 2; break; |
41 default: flags += 3; | 41 default: flags += 3; |
42 } | 42 } |
43 switch (sizeof(uLong)) { | 43 switch ((int)(sizeof(uLong))) { |
44 case 2: break; | 44 case 2: break; |
45 case 4: flags += 1 << 2; break; | 45 case 4: flags += 1 << 2; break; |
46 case 8: flags += 2 << 2; break; | 46 case 8: flags += 2 << 2; break; |
47 default: flags += 3 << 2; | 47 default: flags += 3 << 2; |
48 } | 48 } |
49 switch (sizeof(voidpf)) { | 49 switch ((int)(sizeof(voidpf))) { |
50 case 2: break; | 50 case 2: break; |
51 case 4: flags += 1 << 4; break; | 51 case 4: flags += 1 << 4; break; |
52 case 8: flags += 2 << 4; break; | 52 case 8: flags += 2 << 4; break; |
53 default: flags += 3 << 4; | 53 default: flags += 3 << 4; |
54 } | 54 } |
55 switch (sizeof(z_off_t)) { | 55 switch ((int)(sizeof(z_off_t))) { |
56 case 2: break; | 56 case 2: break; |
57 case 4: flags += 1 << 6; break; | 57 case 4: flags += 1 << 6; break; |
58 case 8: flags += 2 << 6; break; | 58 case 8: flags += 2 << 6; break; |
59 default: flags += 3 << 6; | 59 default: flags += 3 << 6; |
60 } | 60 } |
61 #ifdef DEBUG | 61 #ifdef DEBUG |
62 flags += 1 << 8; | 62 flags += 1 << 8; |
63 #endif | 63 #endif |
64 #if defined(ASMV) || defined(ASMINF) | 64 #if defined(ASMV) || defined(ASMINF) |
65 flags += 1 << 9; | 65 flags += 1 << 9; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 # endif | 110 # endif |
111 #endif | 111 #endif |
112 return flags; | 112 return flags; |
113 } | 113 } |
114 | 114 |
115 #ifdef DEBUG | 115 #ifdef DEBUG |
116 | 116 |
117 # ifndef verbose | 117 # ifndef verbose |
118 # define verbose 0 | 118 # define verbose 0 |
119 # endif | 119 # endif |
120 int z_verbose = verbose; | 120 int ZLIB_INTERNAL z_verbose = verbose; |
121 | 121 |
122 void z_error (m) | 122 void ZLIB_INTERNAL z_error (m) |
123 char *m; | 123 char *m; |
124 { | 124 { |
125 fprintf(stderr, "%s\n", m); | 125 fprintf(stderr, "%s\n", m); |
126 exit(1); | 126 exit(1); |
127 } | 127 } |
128 #endif | 128 #endif |
129 | 129 |
130 /* exported to allow conversion of error code to string for compress() and | 130 /* exported to allow conversion of error code to string for compress() and |
131 * uncompress() | 131 * uncompress() |
132 */ | 132 */ |
133 const char * ZEXPORT zError(err) | 133 const char * ZEXPORT zError(err) |
134 int err; | 134 int err; |
135 { | 135 { |
136 return ERR_MSG(err); | 136 return ERR_MSG(err); |
137 } | 137 } |
138 | 138 |
139 #if defined(_WIN32_WCE) | 139 #if defined(_WIN32_WCE) |
140 /* The Microsoft C Run-Time Library for Windows CE doesn't have | 140 /* The Microsoft C Run-Time Library for Windows CE doesn't have |
141 * errno. We define it as a global variable to simplify porting. | 141 * errno. We define it as a global variable to simplify porting. |
142 * Its value is always 0 and should not be used. | 142 * Its value is always 0 and should not be used. |
143 */ | 143 */ |
144 // Google Gears modification: zutil.h defines errno as z_errno for WinCE. | 144 int errno = 0; |
145 //int errno = 0; | |
146 int z_errno = 0; | |
147 #endif | 145 #endif |
148 | 146 |
149 #ifndef HAVE_MEMCPY | 147 #ifndef HAVE_MEMCPY |
150 | 148 |
151 void zmemcpy(dest, source, len) | 149 void ZLIB_INTERNAL zmemcpy(dest, source, len) |
152 Bytef* dest; | 150 Bytef* dest; |
153 const Bytef* source; | 151 const Bytef* source; |
154 uInt len; | 152 uInt len; |
155 { | 153 { |
156 if (len == 0) return; | 154 if (len == 0) return; |
157 do { | 155 do { |
158 *dest++ = *source++; /* ??? to be unrolled */ | 156 *dest++ = *source++; /* ??? to be unrolled */ |
159 } while (--len != 0); | 157 } while (--len != 0); |
160 } | 158 } |
161 | 159 |
162 int zmemcmp(s1, s2, len) | 160 int ZLIB_INTERNAL zmemcmp(s1, s2, len) |
163 const Bytef* s1; | 161 const Bytef* s1; |
164 const Bytef* s2; | 162 const Bytef* s2; |
165 uInt len; | 163 uInt len; |
166 { | 164 { |
167 uInt j; | 165 uInt j; |
168 | 166 |
169 for (j = 0; j < len; j++) { | 167 for (j = 0; j < len; j++) { |
170 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; | 168 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; |
171 } | 169 } |
172 return 0; | 170 return 0; |
173 } | 171 } |
174 | 172 |
175 void zmemzero(dest, len) | 173 void ZLIB_INTERNAL zmemzero(dest, len) |
176 Bytef* dest; | 174 Bytef* dest; |
177 uInt len; | 175 uInt len; |
178 { | 176 { |
179 if (len == 0) return; | 177 if (len == 0) return; |
180 do { | 178 do { |
181 *dest++ = 0; /* ??? to be unrolled */ | 179 *dest++ = 0; /* ??? to be unrolled */ |
182 } while (--len != 0); | 180 } while (--len != 0); |
183 } | 181 } |
184 #endif | 182 #endif |
185 | 183 |
(...skipping 22 matching lines...) Expand all Loading... |
208 } ptr_table; | 206 } ptr_table; |
209 | 207 |
210 local ptr_table table[MAX_PTR]; | 208 local ptr_table table[MAX_PTR]; |
211 /* This table is used to remember the original form of pointers | 209 /* This table is used to remember the original form of pointers |
212 * to large buffers (64K). Such pointers are normalized with a zero offset. | 210 * to large buffers (64K). Such pointers are normalized with a zero offset. |
213 * Since MSDOS is not a preemptive multitasking OS, this table is not | 211 * Since MSDOS is not a preemptive multitasking OS, this table is not |
214 * protected from concurrent access. This hack doesn't work anyway on | 212 * protected from concurrent access. This hack doesn't work anyway on |
215 * a protected system like OS/2. Use Microsoft C instead. | 213 * a protected system like OS/2. Use Microsoft C instead. |
216 */ | 214 */ |
217 | 215 |
218 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) | 216 voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size) |
219 { | 217 { |
220 voidpf buf = opaque; /* just to make some compilers happy */ | 218 voidpf buf = opaque; /* just to make some compilers happy */ |
221 ulg bsize = (ulg)items*size; | 219 ulg bsize = (ulg)items*size; |
222 | 220 |
223 /* If we allocate less than 65520 bytes, we assume that farmalloc | 221 /* If we allocate less than 65520 bytes, we assume that farmalloc |
224 * will return a usable pointer which doesn't have to be normalized. | 222 * will return a usable pointer which doesn't have to be normalized. |
225 */ | 223 */ |
226 if (bsize < 65520L) { | 224 if (bsize < 65520L) { |
227 buf = farmalloc(bsize); | 225 buf = farmalloc(bsize); |
228 if (*(ush*)&buf != 0) return buf; | 226 if (*(ush*)&buf != 0) return buf; |
229 } else { | 227 } else { |
230 buf = farmalloc(bsize + 16L); | 228 buf = farmalloc(bsize + 16L); |
231 } | 229 } |
232 if (buf == NULL || next_ptr >= MAX_PTR) return NULL; | 230 if (buf == NULL || next_ptr >= MAX_PTR) return NULL; |
233 table[next_ptr].org_ptr = buf; | 231 table[next_ptr].org_ptr = buf; |
234 | 232 |
235 /* Normalize the pointer to seg:0 */ | 233 /* Normalize the pointer to seg:0 */ |
236 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; | 234 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; |
237 *(ush*)&buf = 0; | 235 *(ush*)&buf = 0; |
238 table[next_ptr++].new_ptr = buf; | 236 table[next_ptr++].new_ptr = buf; |
239 return buf; | 237 return buf; |
240 } | 238 } |
241 | 239 |
242 void zcfree (voidpf opaque, voidpf ptr) | 240 void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) |
243 { | 241 { |
244 int n; | 242 int n; |
245 if (*(ush*)&ptr != 0) { /* object < 64K */ | 243 if (*(ush*)&ptr != 0) { /* object < 64K */ |
246 farfree(ptr); | 244 farfree(ptr); |
247 return; | 245 return; |
248 } | 246 } |
249 /* Find the original pointer */ | 247 /* Find the original pointer */ |
250 for (n = 0; n < next_ptr; n++) { | 248 for (n = 0; n < next_ptr; n++) { |
251 if (ptr != table[n].new_ptr) continue; | 249 if (ptr != table[n].new_ptr) continue; |
252 | 250 |
(...skipping 14 matching lines...) Expand all Loading... |
267 #ifdef M_I86 | 265 #ifdef M_I86 |
268 /* Microsoft C in 16-bit mode */ | 266 /* Microsoft C in 16-bit mode */ |
269 | 267 |
270 # define MY_ZCALLOC | 268 # define MY_ZCALLOC |
271 | 269 |
272 #if (!defined(_MSC_VER) || (_MSC_VER <= 600)) | 270 #if (!defined(_MSC_VER) || (_MSC_VER <= 600)) |
273 # define _halloc halloc | 271 # define _halloc halloc |
274 # define _hfree hfree | 272 # define _hfree hfree |
275 #endif | 273 #endif |
276 | 274 |
277 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) | 275 voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size) |
278 { | 276 { |
279 if (opaque) opaque = 0; /* to make compiler happy */ | 277 if (opaque) opaque = 0; /* to make compiler happy */ |
280 return _halloc((long)items, size); | 278 return _halloc((long)items, size); |
281 } | 279 } |
282 | 280 |
283 void zcfree (voidpf opaque, voidpf ptr) | 281 void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) |
284 { | 282 { |
285 if (opaque) opaque = 0; /* to make compiler happy */ | 283 if (opaque) opaque = 0; /* to make compiler happy */ |
286 _hfree(ptr); | 284 _hfree(ptr); |
287 } | 285 } |
288 | 286 |
289 #endif /* M_I86 */ | 287 #endif /* M_I86 */ |
290 | 288 |
291 #endif /* SYS16BIT */ | 289 #endif /* SYS16BIT */ |
292 | 290 |
293 | 291 |
294 #ifndef MY_ZCALLOC /* Any system without a special alloc function */ | 292 #ifndef MY_ZCALLOC /* Any system without a special alloc function */ |
295 | 293 |
296 #ifndef STDC | 294 #ifndef STDC |
297 extern voidp malloc OF((uInt size)); | 295 extern voidp malloc OF((uInt size)); |
298 extern voidp calloc OF((uInt items, uInt size)); | 296 extern voidp calloc OF((uInt items, uInt size)); |
299 extern void free OF((voidpf ptr)); | 297 extern void free OF((voidpf ptr)); |
300 #endif | 298 #endif |
301 | 299 |
302 voidpf zcalloc (opaque, items, size) | 300 voidpf ZLIB_INTERNAL zcalloc (opaque, items, size) |
303 voidpf opaque; | 301 voidpf opaque; |
304 unsigned items; | 302 unsigned items; |
305 unsigned size; | 303 unsigned size; |
306 { | 304 { |
307 if (opaque) items += size - size; /* make compiler happy */ | 305 if (opaque) items += size - size; /* make compiler happy */ |
308 return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : | 306 return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : |
309 (voidpf)calloc(items, size); | 307 (voidpf)calloc(items, size); |
310 } | 308 } |
311 | 309 |
312 void zcfree (opaque, ptr) | 310 void ZLIB_INTERNAL zcfree (opaque, ptr) |
313 voidpf opaque; | 311 voidpf opaque; |
314 voidpf ptr; | 312 voidpf ptr; |
315 { | 313 { |
316 free(ptr); | 314 free(ptr); |
317 if (opaque) return; /* make compiler happy */ | 315 if (opaque) return; /* make compiler happy */ |
318 } | 316 } |
319 | 317 |
320 #endif /* MY_ZCALLOC */ | 318 #endif /* MY_ZCALLOC */ |
OLD | NEW |