2 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 /* zutil.c -- target dependent utility functions for the compression library
7 * Copyright (C) 1995-2005 Jean-loup Gailly.
8 * For conditions of distribution and use, see copyright notice in zlib.h
14 struct internal_state
{int dummy
;}; /* for buggy compilers */
17 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 (sizeof(uInt
)) {
42 case 4: flags
+= 1; break;
43 case 8: flags
+= 2; break;
46 switch (sizeof(uLong
)) {
48 case 4: flags
+= 1 << 2; break;
49 case 8: flags
+= 2 << 2; break;
50 default: flags
+= 3 << 2;
52 switch (sizeof(voidpf
)) {
54 case 4: flags
+= 1 << 4; break;
55 case 8: flags
+= 2 << 4; break;
56 default: flags
+= 3 << 4;
58 switch (sizeof(z_off_t
)) {
60 case 4: flags
+= 1 << 6; break;
61 case 8: flags
+= 2 << 6; break;
62 default: flags
+= 3 << 6;
67 #if defined(ASMV) || defined(ASMINF)
76 #ifdef DYNAMIC_CRC_TABLE
85 #ifdef PKZIP_BUG_WORKAROUND
94 # ifdef HAS_vsprintf_void
98 # ifdef HAS_vsnprintf_void
106 # ifdef HAS_sprintf_void
110 # ifdef HAS_snprintf_void
123 int z_verbose
= verbose
;
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.
153 void zmemcpy(dest
, source
, len
)
158 if (len
== 0) return;
160 *dest
++ = *source
++; /* ??? to be unrolled */
161 } while (--len
!= 0);
164 int zmemcmp(s1
, s2
, len
)
171 for (j
= 0; j
< len
; j
++) {
172 if (s1
[j
] != s2
[j
]) return 2*(s1
[j
] > s2
[j
])-1;
177 void zmemzero(dest
, len
)
181 if (len
== 0) return;
183 *dest
++ = 0; /* ??? to be unrolled */
184 } 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
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 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
zcalloc (voidpf opaque
, unsigned items
, unsigned size
)
281 if (opaque
) opaque
= 0; /* to make compiler happy */
282 return _halloc((long)items
, size
);
285 void 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
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 zcfree (opaque
, ptr
)
319 if (opaque
) return; /* make compiler happy */
322 #endif /* MY_ZCALLOC */