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