]>
git.saurik.com Git - wxWidgets.git/blob - src/zlib/zutil.c
1 /* zutil.c -- target dependent utility functions for the compression library
2 * Copyright (C) 1995-2005 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
10 struct internal_state
{int dummy
;}; /* for buggy compilers */
13 const char * const z_errmsg
[10] = {
14 "need dictionary", /* Z_NEED_DICT 2 */
15 "stream end", /* Z_STREAM_END 1 */
17 "file error", /* Z_ERRNO (-1) */
18 "stream error", /* Z_STREAM_ERROR (-2) */
19 "data error", /* Z_DATA_ERROR (-3) */
20 "insufficient memory", /* Z_MEM_ERROR (-4) */
21 "buffer error", /* Z_BUF_ERROR (-5) */
22 "incompatible version",/* Z_VERSION_ERROR (-6) */
26 const char * ZEXPORT
zlibVersion()
31 uLong ZEXPORT
zlibCompileFlags()
36 switch (sizeof(uInt
)) {
38 case 4: flags
+= 1; break;
39 case 8: flags
+= 2; break;
42 switch (sizeof(uLong
)) {
44 case 4: flags
+= 1 << 2; break;
45 case 8: flags
+= 2 << 2; break;
46 default: flags
+= 3 << 2;
48 switch (sizeof(voidpf
)) {
50 case 4: flags
+= 1 << 4; break;
51 case 8: flags
+= 2 << 4; break;
52 default: flags
+= 3 << 4;
54 switch (sizeof(z_off_t
)) {
56 case 4: flags
+= 1 << 6; break;
57 case 8: flags
+= 2 << 6; break;
58 default: flags
+= 3 << 6;
63 #if defined(ASMV) || defined(ASMINF)
72 #ifdef DYNAMIC_CRC_TABLE
81 #ifdef PKZIP_BUG_WORKAROUND
90 # ifdef HAS_vsprintf_void
94 # ifdef HAS_vsnprintf_void
102 # ifdef HAS_sprintf_void
106 # ifdef HAS_snprintf_void
119 int z_verbose
= verbose
;
124 fprintf(stderr
, "%s\n", m
);
129 /* exported to allow conversion of error code to string for compress() and
132 const char * ZEXPORT
zError(err
)
138 #if defined(_WIN32_WCE)
139 /* The Microsoft C Run-Time Library for Windows CE doesn't have
140 * errno. We define it as a global variable to simplify porting.
141 * Its value is always 0 and should not be used.
148 void zmemcpy(dest
, source
, len
)
153 if (len
== 0) return;
155 *dest
++ = *source
++; /* ??? to be unrolled */
156 } while (--len
!= 0);
159 int zmemcmp(s1
, s2
, len
)
166 for (j
= 0; j
< len
; j
++) {
167 if (s1
[j
] != s2
[j
]) return 2*(s1
[j
] > s2
[j
])-1;
172 void zmemzero(dest
, len
)
176 if (len
== 0) return;
178 *dest
++ = 0; /* ??? to be unrolled */
179 } while (--len
!= 0);
187 /* Turbo C in 16-bit mode */
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().
200 local
int next_ptr
= 0;
202 typedef struct ptr_table_s
{
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.
215 voidpf
zcalloc (voidpf opaque
, unsigned items
, unsigned size
)
217 voidpf buf
= opaque
; /* just to make some compilers happy */
218 ulg bsize
= (ulg
)items
*size
;
220 /* If we allocate less than 65520 bytes, we assume that farmalloc
221 * will return a usable pointer which doesn't have to be normalized.
223 if (bsize
< 65520L) {
224 buf
= farmalloc(bsize
);
225 if (*(ush
*)&buf
!= 0) return buf
;
227 buf
= farmalloc(bsize
+ 16L);
229 if (buf
== NULL
|| next_ptr
>= MAX_PTR
) return NULL
;
230 table
[next_ptr
].org_ptr
= buf
;
232 /* Normalize the pointer to seg:0 */
233 *((ush
*)&buf
+1) += ((ush
)((uch
*)buf
-0) + 15) >> 4;
235 table
[next_ptr
++].new_ptr
= buf
;
239 void zcfree (voidpf opaque
, voidpf ptr
)
242 if (*(ush
*)&ptr
!= 0) { /* object < 64K */
246 /* Find the original pointer */
247 for (n
= 0; n
< next_ptr
; n
++) {
248 if (ptr
!= table
[n
].new_ptr
) continue;
250 farfree(table
[n
].org_ptr
);
251 while (++n
< next_ptr
) {
252 table
[n
-1] = table
[n
];
257 ptr
= opaque
; /* just to make some compilers happy */
258 Assert(0, "zcfree: ptr not found");
261 #endif /* __TURBOC__ */
265 /* Microsoft C in 16-bit mode */
269 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
270 # define _halloc halloc
271 # define _hfree hfree
274 voidpf
zcalloc (voidpf opaque
, unsigned items
, unsigned size
)
276 if (opaque
) opaque
= 0; /* to make compiler happy */
277 return _halloc((long)items
, size
);
280 void zcfree (voidpf opaque
, voidpf ptr
)
282 if (opaque
) opaque
= 0; /* to make compiler happy */
288 #endif /* SYS16BIT */
291 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
294 extern voidp malloc
OF((uInt size
));
295 extern voidp calloc
OF((uInt items
, uInt size
));
296 extern void free
OF((voidpf ptr
));
299 voidpf
zcalloc (opaque
, items
, size
)
304 if (opaque
) items
+= size
- size
; /* make compiler happy */
305 return sizeof(uInt
) > 2 ? (voidpf
)malloc(items
* size
) :
306 (voidpf
)calloc(items
, size
);
309 void zcfree (opaque
, ptr
)
314 if (opaque
) return; /* make compiler happy */
317 #endif /* MY_ZCALLOC */