Coverage Report

Created: 2026-01-09 07:10

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