1 /* zutil.c -- target dependent utility functions for the compression library
2 * Copyright (C) 1995-2005, 2010, 2011, 2012 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
14 struct internal_state
{int dummy
;}; /* for buggy compilers */
17 z_const
char * const z_errmsg
[10] = {
18 "need dictionary", /* Z_NEED_DICT 2 */
19 "stream end", /* Z_STREAM_END 1 */
21 "file error", /* Z_ERRNO (-1) */
22 "stream error", /* Z_STREAM_ERROR (-2) */
23 "data error", /* Z_DATA_ERROR (-3) */
24 "insufficient memory", /* Z_MEM_ERROR (-4) */
25 "buffer error", /* Z_BUF_ERROR (-5) */
26 "incompatible version",/* Z_VERSION_ERROR (-6) */
30 const char * ZEXPORT
zlibVersion()
35 uLong ZEXPORT
zlibCompileFlags()
40 switch ((int)(sizeof(uInt
))) {
41 case 2: break; /* CONSTANT CONDITION */
42 case 4: flags
+= 1; break; /* CONSTANT CONDITION */
43 case 8: flags
+= 2; break; /* CONSTANT CONDITION */
46 switch ((int)(sizeof(uLong
))) {
47 case 2: break; /* CONSTANT CONDITION */
48 case 4: flags
+= 1 << 2; break; /* CONSTANT CONDITION */
49 case 8: flags
+= 2 << 2; break; /* CONSTANT CONDITION */
50 default: flags
+= 3 << 2;
52 switch ((int)(sizeof(voidpf
))) {
53 case 2: break; /* CONSTANT CONDITION */
54 case 4: flags
+= 1 << 4; break; /* CONSTANT CONDITION */
55 case 8: flags
+= 2 << 4; break; /* CONSTANT CONDITION */
56 default: flags
+= 3 << 4;
58 switch ((int)(sizeof(z_off_t
))) {
59 case 2: break; /* CONSTANT CONDITION */
60 case 4: flags
+= 1 << 6; break; /* CONSTANT CONDITION */
61 case 8: flags
+= 2 << 6; break; /* CONSTANT CONDITION */
62 default: flags
+= 3 << 6;
67 #if defined(ASMV) || defined(ASMINF)
76 #ifdef DYNAMIC_CRC_TABLE
85 #ifdef PKZIP_BUG_WORKAROUND
91 #if defined(STDC) || defined(Z_HAVE_STDARG_H)
94 # ifdef HAS_vsprintf_void
98 # ifdef HAS_vsnprintf_void
106 # ifdef HAS_sprintf_void
110 # ifdef HAS_snprintf_void
123 int ZLIB_INTERNAL z_verbose
= verbose
;
125 void ZLIB_INTERNAL
z_error (m
)
128 fprintf(stderr
, "%s\n", m
);
133 /* exported to allow conversion of error code to string for compress() and
136 const char * ZEXPORT
zError(err
)
142 #if defined(_WIN32_WCE)
143 /* The Microsoft C Run-Time Library for Windows CE doesn't have
144 * errno. We define it as a global variable to simplify porting.
145 * Its value is always 0 and should not be used.
152 void ZLIB_INTERNAL
zmemcpy(dest
, source
, len
)
157 if (len
== 0) return;
159 *dest
++ = *source
++; /* ??? to be unrolled */
160 } while (--len
!= 0);
163 int ZLIB_INTERNAL
zmemcmp(s1
, s2
, len
)
170 for (j
= 0; j
< len
; j
++) {
171 if (s1
[j
] != s2
[j
]) return 2*(s1
[j
] > s2
[j
])-1;
176 void ZLIB_INTERNAL
zmemzero(dest
, len
)
180 if (len
== 0) return;
182 *dest
++ = 0; /* ??? to be unrolled */
183 } while (--len
!= 0);
192 /* Turbo C in 16-bit mode */
196 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
197 * and farmalloc(64K) returns a pointer with an offset of 8, so we
198 * must fix the pointer. Warning: the pointer must be put back to its
199 * original form in order to free it, use zcfree().
205 local
int next_ptr
= 0;
207 typedef struct ptr_table_s
{
212 local ptr_table table
[MAX_PTR
];
213 /* This table is used to remember the original form of pointers
214 * to large buffers (64K). Such pointers are normalized with a zero offset.
215 * Since MSDOS is not a preemptive multitasking OS, this table is not
216 * protected from concurrent access. This hack doesn't work anyway on
217 * a protected system like OS/2. Use Microsoft C instead.
220 voidpf ZLIB_INTERNAL
zcalloc (voidpf opaque
, unsigned items
, unsigned size
)
222 voidpf buf
= opaque
; /* just to make some compilers happy */
223 ulg bsize
= (ulg
)items
*size
;
225 /* If we allocate less than 65520 bytes, we assume that farmalloc
226 * will return a usable pointer which doesn't have to be normalized.
228 if (bsize
< 65520L) {
229 buf
= farmalloc(bsize
);
230 if (*(ush
*)&buf
!= 0) return buf
;
232 buf
= farmalloc(bsize
+ 16L);
234 if (buf
== NULL
|| next_ptr
>= MAX_PTR
) return NULL
;
235 table
[next_ptr
].org_ptr
= buf
;
237 /* Normalize the pointer to seg:0 */
238 *((ush
*)&buf
+1) += ((ush
)((uch
*)buf
-0) + 15) >> 4;
240 table
[next_ptr
++].new_ptr
= buf
;
244 void ZLIB_INTERNAL
zcfree (voidpf opaque
, voidpf ptr
)
247 if (*(ush
*)&ptr
!= 0) { /* object < 64K */
251 /* Find the original pointer */
252 for (n
= 0; n
< next_ptr
; n
++) {
253 if (ptr
!= table
[n
].new_ptr
) continue;
255 farfree(table
[n
].org_ptr
);
256 while (++n
< next_ptr
) {
257 table
[n
-1] = table
[n
];
262 ptr
= opaque
; /* just to make some compilers happy */
263 Assert(0, "zcfree: ptr not found");
266 #endif /* __TURBOC__ */
270 /* Microsoft C in 16-bit mode */
274 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
275 # define _halloc halloc
276 # define _hfree hfree
279 voidpf ZLIB_INTERNAL
zcalloc (voidpf opaque
, uInt items
, uInt size
)
281 if (opaque
) opaque
= 0; /* to make compiler happy */
282 return _halloc((long)items
, size
);
285 void ZLIB_INTERNAL
zcfree (voidpf opaque
, voidpf ptr
)
287 if (opaque
) opaque
= 0; /* to make compiler happy */
293 #endif /* SYS16BIT */
296 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
299 extern voidp malloc
OF((uInt size
));
300 extern voidp calloc
OF((uInt items
, uInt size
));
301 extern void free
OF((voidpf ptr
));
304 voidpf ZLIB_INTERNAL
zcalloc (opaque
, items
, size
)
309 if (opaque
) items
+= size
- size
; /* make compiler happy */
310 return sizeof(uInt
) > 2 ? (voidpf
)malloc(items
* size
) :
311 (voidpf
)calloc(items
, size
);
314 void ZLIB_INTERNAL
zcfree (opaque
, ptr
)
319 if (opaque
) return; /* make compiler happy */
322 #endif /* MY_ZCALLOC */