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