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