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