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