ยปCore Development>Code coverage>Modules/zlib/zutil.c

Python code coverage for Modules/zlib/zutil.c

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