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