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