zutil.c

filter: Statementstmt 70.9% 22 / 31 Branchbran 33.3% 4 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 60.4% 26 / 43 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« inflate.c ↑ Summary inffast.c »
1 /* zutil.c -- target dependent utility functions for the compression library
2 * Copyright (C) 1995-2026 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 14 const char * ZEXPORT zlibVersion(void) {
28 14 return ZLIB_VERSION;
29 }
30  
31 2 uLong ZEXPORT zlibCompileFlags(void) {
32 uLong flags;
33  
34 2 flags = 0;
35 switch ((int)(sizeof(uInt))) {
36 case 2: break;
37 2 case 4: flags += 1; break;
38 case 8: flags += 2; break;
39 default: flags += 3;
40 }
41 switch ((int)(sizeof(uLong))) {
42 case 2: break;
43 case 4: flags += 1 << 2; break;
44 2 case 8: flags += 2 << 2; break;
45 default: flags += 3 << 2;
46 }
47 switch ((int)(sizeof(voidpf))) {
48 case 2: break;
49 case 4: flags += 1 << 4; break;
50 2 case 8: flags += 2 << 4; break;
51 default: flags += 3 << 4;
52 }
53 switch ((int)(sizeof(z_off_t))) {
54 case 2: break;
55 case 4: flags += 1 << 6; break;
56 2 case 8: flags += 2 << 6; break;
57 default: flags += 3 << 6;
58 }
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 #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 # 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 2 return flags;
121 }
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);
Branch: 2 branches
BranchTrueFalseTotal
return ERR_MSG(err); 0 0 0
return ERR_MSG(err); 0 0 0
141 }
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 151250 void ZLIB_INTERNAL zmemcpy(void FAR *dst, const void FAR *src, z_size_t n) {
154 151250 uchf *p = (uchf *)dst;
155 151250 const uchf *q = (const uchf *)src;
156 1546609 ▶ while (n) {
Branch: 1 branch
BranchTrueFalseTotal
while (n) { 1395359 151250 1546609
157 1395359 *p++ = *q++;
158 1395359 n--;
159 }
160 151250 }
161  
162 0 int ZLIB_INTERNAL zmemcmp(const void FAR *s1, const void FAR *s2, z_size_t n) {
163 0 const uchf *p = (const uchf *)s1, *q = (const uchf *)s2;
164 0 ▶ while (n) {
Branch: 1 branch
BranchTrueFalseTotal
while (n) { 0 0 0
165 0 ▶ if (*p++ != *q++)
Branch: 1 branch
BranchTrueFalseTotal
if (*p++ != *q++) 0 0 0
166 0 return (int)p[-1] - (int)q[-1];
167 0 n--;
168 }
169 0 return 0;
170 }
171  
172 354 void ZLIB_INTERNAL zmemzero(void FAR *b, z_size_t len) {
173 354 uchf *p = (uchf *)b;
174 4919552 ▶ while (len) {
Branch: 1 branch
BranchTrueFalseTotal
while (len) { 4919198 354 4919552
175 4919198 *p++ = 0;
176 4919198 len--;
177 }
178 354 }
179  
180 #endif
181  
182 #ifndef Z_SOLO
183  
184 #ifdef SYS16BIT
185  
186 #ifdef __TURBOC__
187 /* Turbo C in 16-bit mode */
188  
189 # define MY_ZCALLOC
190  
191 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
192 * and farmalloc(64K) returns a pointer with an offset of 8, so we
193 * must fix the pointer. Warning: the pointer must be put back to its
194 * original form in order to free it, use zcfree().
195 */
196  
197 #define MAX_PTR 10
198 /* 10*64K = 640K */
199  
200 local int next_ptr = 0;
201  
202 typedef struct ptr_table_s {
203 voidpf org_ptr;
204 voidpf new_ptr;
205 } ptr_table;
206  
207 local ptr_table table[MAX_PTR];
208 /* This table is used to remember the original form of pointers
209 * to large buffers (64K). Such pointers are normalized with a zero offset.
210 * Since MSDOS is not a preemptive multitasking OS, this table is not
211 * protected from concurrent access. This hack doesn't work anyway on
212 * a protected system like OS/2. Use Microsoft C instead.
213 */
214  
215 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) {
216 voidpf buf;
217 ulg bsize = (ulg)items*size;
218  
219 (void)opaque;
220  
221 /* If we allocate less than 65520 bytes, we assume that farmalloc
222 * will return a usable pointer which doesn't have to be normalized.
223 */
224 if (bsize < 65520L) {
225 buf = farmalloc(bsize);
226 if (*(ush*)&buf != 0) return buf;
227 } else {
228 buf = farmalloc(bsize + 16L);
229 }
230 if (buf == NULL) return NULL;
231 if (next_ptr >= MAX_PTR) {
232 farfree(buf);
233 return NULL;
234 }
235 table[next_ptr].org_ptr = buf;
236  
237 /* Normalize the pointer to seg:0 */
238 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
239 *(ush*)&buf = 0;
240 table[next_ptr++].new_ptr = buf;
241 return buf;
242 }
243  
244 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {
245 int n;
246  
247 (void)opaque;
248  
249 if (*(ush*)&ptr != 0) { /* object < 64K */
250 farfree(ptr);
251 return;
252 }
253 /* Find the original pointer */
254 for (n = 0; n < next_ptr; n++) {
255 if (ptr != table[n].new_ptr) continue;
256  
257 farfree(table[n].org_ptr);
258 while (++n < next_ptr) {
259 table[n-1] = table[n];
260 }
261 next_ptr--;
262 return;
263 }
264 Assert(0, "zcfree: ptr not found");
265 }
266  
267 #endif /* __TURBOC__ */
268  
269  
270 #ifdef M_I86
271 /* Microsoft C in 16-bit mode */
272  
273 # define MY_ZCALLOC
274  
275 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
276 # define _halloc halloc
277 # define _hfree hfree
278 #endif
279  
280 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size) {
281 (void)opaque;
282 return _halloc((long)items, size);
283 }
284  
285 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {
286 (void)opaque;
287 _hfree(ptr);
288 }
289  
290 #endif /* M_I86 */
291  
292 #endif /* SYS16BIT */
293  
294  
295 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
296  
297 #ifndef STDC
298 extern voidp malloc(uInt size);
299 extern voidp calloc(uInt items, uInt size);
300 extern void free(voidpf ptr);
301 #endif
302  
303 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) {
304 (void)opaque;
305 return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
306 (voidpf)calloc(items, size);
307 }
308  
309 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {
310 (void)opaque;
311 free(ptr);
312 }
313  
314 #endif /* MY_ZCALLOC */
315  
316 #endif /* !Z_SOLO */
« inflate.c ↑ Summary inffast.c »