Coverage Report

Created: 2025-08-29 06:28

/src/zlib-ng/zutil.c
Line
Count
Source (jump to first uncovered line)
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 "zbuild.h"
7
#include "zutil_p.h"
8
#include "zutil.h"
9
10
z_const char * const PREFIX(z_errmsg)[10] = {
11
    (z_const char *)"need dictionary",     /* Z_NEED_DICT       2  */
12
    (z_const char *)"stream end",          /* Z_STREAM_END      1  */
13
    (z_const char *)"",                    /* Z_OK              0  */
14
    (z_const char *)"file error",          /* Z_ERRNO         (-1) */
15
    (z_const char *)"stream error",        /* Z_STREAM_ERROR  (-2) */
16
    (z_const char *)"data error",          /* Z_DATA_ERROR    (-3) */
17
    (z_const char *)"insufficient memory", /* Z_MEM_ERROR     (-4) */
18
    (z_const char *)"buffer error",        /* Z_BUF_ERROR     (-5) */
19
    (z_const char *)"incompatible version",/* Z_VERSION_ERROR (-6) */
20
    (z_const char *)""
21
};
22
23
const char PREFIX3(vstring)[] =
24
    " zlib-ng 2.3.0-beta";
25
26
#ifdef ZLIB_COMPAT
27
const char * Z_EXPORT zlibVersion(void) {
28
    return ZLIB_VERSION;
29
}
30
#else
31
0
const char * Z_EXPORT zlibng_version(void) {
32
0
    return ZLIBNG_VERSION;
33
0
}
34
#endif
35
36
0
unsigned long Z_EXPORT PREFIX(zlibCompileFlags)(void) {
37
0
    unsigned long flags;
38
39
0
    flags = 0;
40
0
    switch ((int)(sizeof(unsigned int))) {
41
0
    case 2:     break;
42
0
    case 4:     flags += 1;     break;
43
0
    case 8:     flags += 2;     break;
44
0
    default:    flags += 3;
45
0
    }
46
0
    switch ((int)(sizeof(unsigned long))) {
47
0
    case 2:     break;
48
0
    case 4:     flags += 1 << 2;        break;
49
0
    case 8:     flags += 2 << 2;        break;
50
0
    default:    flags += 3 << 2;
51
0
    }
52
0
    switch ((int)(sizeof(void *))) {
53
0
    case 2:     break;
54
0
    case 4:     flags += 1 << 4;        break;
55
0
    case 8:     flags += 2 << 4;        break;
56
0
    default:    flags += 3 << 4;
57
0
    }
58
0
    switch ((int)(sizeof(z_off_t))) {
59
0
    case 2:     break;
60
0
    case 4:     flags += 1 << 6;        break;
61
0
    case 8:     flags += 2 << 6;        break;
62
0
    default:    flags += 3 << 6;
63
0
    }
64
#ifdef ZLIB_DEBUG
65
    flags += 1 << 8;
66
#endif
67
#ifdef ZLIB_WINAPI
68
    flags += 1 << 10;
69
#endif
70
    /* Bit 13 reserved for DYNAMIC_CRC_TABLE */
71
#ifdef NO_GZCOMPRESS
72
    flags += 1L << 16;
73
#endif
74
#ifdef NO_GZIP
75
    flags += 1L << 17;
76
#endif
77
#ifdef PKZIP_BUG_WORKAROUND
78
    flags += 1L << 20;
79
#endif
80
0
    return flags;
81
0
}
82
83
#ifdef ZLIB_DEBUG
84
#  include <stdlib.h>
85
#  ifndef verbose
86
#    define verbose 0
87
#  endif
88
int Z_INTERNAL z_verbose = verbose;
89
90
void Z_INTERNAL z_error(const char *m) {
91
    fprintf(stderr, "%s\n", m);
92
    exit(1);
93
}
94
#endif
95
96
/* exported to allow conversion of error code to string for compress() and
97
 * uncompress()
98
 */
99
0
const char * Z_EXPORT PREFIX(zError)(int err) {
100
0
    return ERR_MSG(err);
101
0
}
102
103
// Zlib-ng's default alloc/free implementation, used unless
104
// application supplies its own alloc/free functions.
105
10.9k
void Z_INTERNAL *PREFIX(zcalloc)(void *opaque, unsigned items, unsigned size) {
106
10.9k
    Z_UNUSED(opaque);
107
10.9k
    return zng_alloc((size_t)items * (size_t)size);
108
10.9k
}
109
110
10.9k
void Z_INTERNAL PREFIX(zcfree)(void *opaque, void *ptr) {
111
10.9k
    Z_UNUSED(opaque);
112
10.9k
    zng_free(ptr);
113
10.9k
}
114
115
/* Provide aligned allocations, only used by gz* code */
116
10.9k
void Z_INTERNAL *zng_alloc_aligned(unsigned size, unsigned align) {
117
10.9k
    uintptr_t return_ptr, original_ptr;
118
10.9k
    uint32_t alloc_size, align_diff;
119
10.9k
    void *ptr;
120
121
    /* Allocate enough memory for proper alignment and to store the original memory pointer */
122
10.9k
    alloc_size = sizeof(void *) + size + align;
123
10.9k
    ptr = zng_alloc(alloc_size);
124
10.9k
    if (!ptr)
125
0
        return NULL;
126
127
    /* Calculate return pointer address with space enough to store original pointer */
128
10.9k
    align_diff = align - ((uintptr_t)ptr % align);
129
10.9k
    return_ptr = (uintptr_t)ptr + align_diff;
130
10.9k
    if (align_diff < sizeof(void *))
131
0
        return_ptr += align;
132
133
    /* Store the original pointer for free() */
134
10.9k
    original_ptr = return_ptr - sizeof(void *);
135
10.9k
    memcpy((void *)original_ptr, &ptr, sizeof(void *));
136
137
    /* Return properly aligned pointer in allocation */
138
10.9k
    return (void *)return_ptr;
139
10.9k
}
140
141
10.9k
void Z_INTERNAL zng_free_aligned(void *ptr) {
142
10.9k
    if (!ptr)
143
0
        return;
144
145
    /* Calculate offset to original memory allocation pointer */
146
10.9k
    void *original_ptr = (void *)((uintptr_t)ptr - sizeof(void *));
147
10.9k
    void *free_ptr = *(void **)original_ptr;
148
149
    /* Free original memory allocation */
150
10.9k
    zng_free(free_ptr);
151
10.9k
}