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