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