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