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