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