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