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
11 #pragma ident "%Z%%M% %I% %E% SMI"
16 struct internal_state
{int dummy
;}; /* for buggy compilers */
19 const char * const z_errmsg
[10] = {
20 "need dictionary", /* Z_NEED_DICT 2 */
21 "stream end", /* Z_STREAM_END 1 */
23 "file error", /* Z_ERRNO (-1) */
24 "stream error", /* Z_STREAM_ERROR (-2) */
25 "data error", /* Z_DATA_ERROR (-3) */
26 "insufficient memory", /* Z_MEM_ERROR (-4) */
27 "buffer error", /* Z_BUF_ERROR (-5) */
28 "incompatible version",/* Z_VERSION_ERROR (-6) */
32 const char * ZEXPORT
zlibVersion()
37 uLong ZEXPORT
zlibCompileFlags()
42 switch (sizeof(uInt
)) {
44 case 4: flags
+= 1; break;
45 case 8: flags
+= 2; break;
48 switch (sizeof(uLong
)) {
50 case 4: flags
+= 1 << 2; break;
51 case 8: flags
+= 2 << 2; break;
52 default: flags
+= 3 << 2;
54 switch (sizeof(voidpf
)) {
56 case 4: flags
+= 1 << 4; break;
57 case 8: flags
+= 2 << 4; break;
58 default: flags
+= 3 << 4;
60 switch (sizeof(z_off_t
)) {
62 case 4: flags
+= 1 << 6; break;
63 case 8: flags
+= 2 << 6; break;
64 default: flags
+= 3 << 6;
69 #if defined(ASMV) || defined(ASMINF)
78 #ifdef DYNAMIC_CRC_TABLE
87 #ifdef PKZIP_BUG_WORKAROUND
96 # ifdef HAS_vsprintf_void
100 # ifdef HAS_vsnprintf_void
108 # ifdef HAS_sprintf_void
112 # ifdef HAS_snprintf_void
125 int z_verbose
= verbose
;
130 fprintf(stderr
, "%s\n", m
);
135 /* exported to allow conversion of error code to string for compress() and
138 const char * ZEXPORT
zError(err
)
144 #if defined(_WIN32_WCE)
145 /* The Microsoft C Run-Time Library for Windows CE doesn't have
146 * errno. We define it as a global variable to simplify porting.
147 * Its value is always 0 and should not be used.
155 void zmemcpy(dest
, source
, len
)
160 if (len
== 0) return;
162 *dest
++ = *source
++; /* ??? to be unrolled */
163 } while (--len
!= 0);
166 int zmemcmp(s1
, s2
, len
)
173 for (j
= 0; j
< len
; j
++) {
174 if (s1
[j
] != s2
[j
]) return 2*(s1
[j
] > s2
[j
])-1;
179 void zmemzero(dest
, len
)
183 if (len
== 0) return;
185 *dest
++ = 0; /* ??? to be unrolled */
186 } while (--len
!= 0);
194 /* Turbo C in 16-bit mode */
198 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
199 * and farmalloc(64K) returns a pointer with an offset of 8, so we
200 * must fix the pointer. Warning: the pointer must be put back to its
201 * original form in order to free it, use zcfree().
207 local
int next_ptr
= 0;
209 typedef struct ptr_table_s
{
214 local ptr_table table
[MAX_PTR
];
215 /* This table is used to remember the original form of pointers
216 * to large buffers (64K). Such pointers are normalized with a zero offset.
217 * Since MSDOS is not a preemptive multitasking OS, this table is not
218 * protected from concurrent access. This hack doesn't work anyway on
219 * a protected system like OS/2. Use Microsoft C instead.
222 voidpf
zcalloc (voidpf opaque
, unsigned items
, unsigned size
)
224 voidpf buf
= opaque
; /* just to make some compilers happy */
225 ulg bsize
= (ulg
)items
*size
;
227 /* If we allocate less than 65520 bytes, we assume that farmalloc
228 * will return a usable pointer which doesn't have to be normalized.
230 if (bsize
< 65520L) {
231 buf
= farmalloc(bsize
);
232 if (*(ush
*)&buf
!= 0) return buf
;
234 buf
= farmalloc(bsize
+ 16L);
236 if (buf
== NULL
|| next_ptr
>= MAX_PTR
) return NULL
;
237 table
[next_ptr
].org_ptr
= buf
;
239 /* Normalize the pointer to seg:0 */
240 *((ush
*)&buf
+1) += ((ush
)((uch
*)buf
-0) + 15) >> 4;
242 table
[next_ptr
++].new_ptr
= buf
;
246 void zcfree (voidpf opaque
, voidpf ptr
)
249 if (*(ush
*)&ptr
!= 0) { /* object < 64K */
253 /* Find the original pointer */
254 for (n
= 0; n
< next_ptr
; n
++) {
255 if (ptr
!= table
[n
].new_ptr
) continue;
257 farfree(table
[n
].org_ptr
);
258 while (++n
< next_ptr
) {
259 table
[n
-1] = table
[n
];
264 ptr
= opaque
; /* just to make some compilers happy */
265 Assert(0, "zcfree: ptr not found");
268 #endif /* __TURBOC__ */
272 /* Microsoft C in 16-bit mode */
276 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
277 # define _halloc halloc
278 # define _hfree hfree
281 voidpf
zcalloc (voidpf opaque
, unsigned items
, unsigned size
)
283 if (opaque
) opaque
= 0; /* to make compiler happy */
284 return _halloc((long)items
, size
);
287 void zcfree (voidpf opaque
, voidpf ptr
)
289 if (opaque
) opaque
= 0; /* to make compiler happy */
295 #endif /* SYS16BIT */
298 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
301 extern voidp malloc
OF((uInt size
));
302 extern voidp calloc
OF((uInt items
, uInt size
));
303 extern void free
OF((voidpf ptr
));
306 voidpf
zcalloc (opaque
, items
, size
)
311 if (opaque
) items
+= size
- size
; /* make compiler happy */
312 return sizeof(uInt
) > 2 ? (voidpf
)malloc(items
* size
) :
313 (voidpf
)calloc(items
, size
);
316 void zcfree (opaque
, ptr
)
321 if (opaque
) return; /* make compiler happy */
324 #endif /* MY_ZCALLOC */