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