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