1 /* $NetBSD: zutil.c,v 1.2 2006/01/16 17:02:29 christos Exp $ */
3 /* zutil.c -- target dependent utility functions for the compression library
4 * Copyright (C) 1995-2005 Jean-loup Gailly.
5 * For conditions of distribution and use, see copyright notice in zlib.h
13 struct internal_state
{int dummy
;}; /* for buggy compilers */
16 const char * const z_errmsg
[10] = {
17 "need dictionary", /* Z_NEED_DICT 2 */
18 "stream end", /* Z_STREAM_END 1 */
20 "file error", /* Z_ERRNO (-1) */
21 "stream error", /* Z_STREAM_ERROR (-2) */
22 "data error", /* Z_DATA_ERROR (-3) */
23 "insufficient memory", /* Z_MEM_ERROR (-4) */
24 "buffer error", /* Z_BUF_ERROR (-5) */
25 "incompatible version",/* Z_VERSION_ERROR (-6) */
29 const char * ZEXPORT
zlibVersion()
34 uLong ZEXPORT
zlibCompileFlags()
39 switch (sizeof(uInt
)) {
41 case 4: flags
+= 1; break;
42 case 8: flags
+= 2; break;
45 switch (sizeof(uLong
)) {
47 case 4: flags
+= 1 << 2; break;
48 case 8: flags
+= 2 << 2; break;
49 default: flags
+= 3 << 2;
51 switch (sizeof(voidpf
)) {
53 case 4: flags
+= 1 << 4; break;
54 case 8: flags
+= 2 << 4; break;
55 default: flags
+= 3 << 4;
57 switch (sizeof(z_off_t
)) {
59 case 4: flags
+= 1 << 6; break;
60 case 8: flags
+= 2 << 6; break;
61 default: flags
+= 3 << 6;
66 #if defined(ASMV) || defined(ASMINF)
75 #ifdef DYNAMIC_CRC_TABLE
84 #ifdef PKZIP_BUG_WORKAROUND
93 # ifdef HAS_vsprintf_void
97 # ifdef HAS_vsnprintf_void
105 # ifdef HAS_sprintf_void
109 # ifdef HAS_snprintf_void
122 int z_verbose
= verbose
;
127 fprintf(stderr
, "%s\n", m
);
132 /* exported to allow conversion of error code to string for compress() and
135 const char * ZEXPORT
zError(err
)
141 #if defined(_WIN32_WCE)
142 /* The Microsoft C Run-Time Library for Windows CE doesn't have
143 * errno. We define it as a global variable to simplify porting.
144 * Its value is always 0 and should not be used.
151 void zmemcpy(dest
, source
, len
)
156 if (len
== 0) return;
158 *dest
++ = *source
++; /* ??? to be unrolled */
159 } while (--len
!= 0);
162 int zmemcmp(s1
, s2
, len
)
169 for (j
= 0; j
< len
; j
++) {
170 if (s1
[j
] != s2
[j
]) return 2*(s1
[j
] > s2
[j
])-1;
175 void zmemzero(dest
, len
)
179 if (len
== 0) return;
181 *dest
++ = 0; /* ??? to be unrolled */
182 } while (--len
!= 0);
190 /* Turbo C in 16-bit mode */
194 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
195 * and farmalloc(64K) returns a pointer with an offset of 8, so we
196 * must fix the pointer. Warning: the pointer must be put back to its
197 * original form in order to free it, use zcfree().
203 local
int next_ptr
= 0;
205 typedef struct ptr_table_s
{
210 local ptr_table table
[MAX_PTR
];
211 /* This table is used to remember the original form of pointers
212 * to large buffers (64K). Such pointers are normalized with a zero offset.
213 * Since MSDOS is not a preemptive multitasking OS, this table is not
214 * protected from concurrent access. This hack doesn't work anyway on
215 * a protected system like OS/2. Use Microsoft C instead.
218 voidpf
zcalloc (voidpf opaque
, unsigned items
, unsigned size
)
220 voidpf buf
= opaque
; /* just to make some compilers happy */
221 ulg bsize
= (ulg
)items
*size
;
223 /* If we allocate less than 65520 bytes, we assume that farmalloc
224 * will return a usable pointer which doesn't have to be normalized.
226 if (bsize
< 65520L) {
227 buf
= farmalloc(bsize
);
228 if (*(ush
*)&buf
!= 0) return buf
;
230 buf
= farmalloc(bsize
+ 16L);
232 if (buf
== NULL
|| next_ptr
>= MAX_PTR
) return NULL
;
233 table
[next_ptr
].org_ptr
= buf
;
235 /* Normalize the pointer to seg:0 */
236 *((ush
*)&buf
+1) += ((ush
)((uch
*)buf
-0) + 15) >> 4;
238 table
[next_ptr
++].new_ptr
= buf
;
242 void zcfree (voidpf opaque
, voidpf ptr
)
245 if (*(ush
*)&ptr
!= 0) { /* object < 64K */
249 /* Find the original pointer */
250 for (n
= 0; n
< next_ptr
; n
++) {
251 if (ptr
!= table
[n
].new_ptr
) continue;
253 farfree(table
[n
].org_ptr
);
254 while (++n
< next_ptr
) {
255 table
[n
-1] = table
[n
];
260 ptr
= opaque
; /* just to make some compilers happy */
261 Assert(0, "zcfree: ptr not found");
264 #endif /* __TURBOC__ */
268 /* Microsoft C in 16-bit mode */
272 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
273 # define _halloc halloc
274 # define _hfree hfree
277 voidpf
zcalloc (voidpf opaque
, unsigned items
, unsigned size
)
279 if (opaque
) opaque
= 0; /* to make compiler happy */
280 return _halloc((long)items
, size
);
283 void zcfree (voidpf opaque
, voidpf ptr
)
285 if (opaque
) opaque
= 0; /* to make compiler happy */
291 #endif /* SYS16BIT */
294 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
297 extern voidp malloc
OF((uInt size
));
298 extern voidp calloc
OF((uInt items
, uInt size
));
299 extern void free
OF((voidpf ptr
));
302 voidpf
zcalloc (opaque
, items
, size
)
307 if (opaque
) items
+= size
- size
; /* make compiler happy */
308 return sizeof(uInt
) > 2 ? (voidpf
)malloc(items
* size
) :
309 (voidpf
)calloc(items
, size
);
312 void zcfree (opaque
, ptr
)
317 if (opaque
) return; /* make compiler happy */
320 #endif /* MY_ZCALLOC */