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