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