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