1
/* zutil.c -- target dependent utility functions for the compression library
2
* Copyright (C) 1995-2002 Jean-loup Gailly.
3
* For conditions of distribution and use, see copyright notice in zlib.h
6
/* @(#) $Id: zutil.c,v 1.5 2004/07/10 07:48:40 mcr Exp $ */
8
#include <zlib/zutil.h>
12
struct internal_state {int dummy;}; /* for buggy compilers */
15
extern void exit OF((int));
18
const char *z_errmsg[10] = {
19
"need dictionary", /* Z_NEED_DICT 2 */
20
"stream end", /* Z_STREAM_END 1 */
22
"file error", /* Z_ERRNO (-1) */
23
"stream error", /* Z_STREAM_ERROR (-2) */
24
"data error", /* Z_DATA_ERROR (-3) */
25
"insufficient memory", /* Z_MEM_ERROR (-4) */
26
"buffer error", /* Z_BUF_ERROR (-5) */
27
"incompatible version",/* Z_VERSION_ERROR (-6) */
31
const char * ZEXPORT zlibVersion()
41
int z_verbose = verbose;
46
fprintf(stderr, "%s\n", m);
51
/* exported to allow conversion of error code to string for compress() and
54
const char * ZEXPORT zError(err)
63
void zmemcpy(dest, source, len)
70
*dest++ = *source++; /* ??? to be unrolled */
74
int zmemcmp(s1, s2, len)
81
for (j = 0; j < len; j++) {
82
if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
87
void zmemzero(dest, len)
93
*dest++ = 0; /* ??? to be unrolled */
99
#if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
100
/* Small and medium model in Turbo C are for now limited to near allocation
101
* with reduced MAX_WBITS and MAX_MEM_LEVEL
105
/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
106
* and farmalloc(64K) returns a pointer with an offset of 8, so we
107
* must fix the pointer. Warning: the pointer must be put back to its
108
* original form in order to free it, use zcfree().
114
local int next_ptr = 0;
116
typedef struct ptr_table_s {
121
local ptr_table table[MAX_PTR];
122
/* This table is used to remember the original form of pointers
123
* to large buffers (64K). Such pointers are normalized with a zero offset.
124
* Since MSDOS is not a preemptive multitasking OS, this table is not
125
* protected from concurrent access. This hack doesn't work anyway on
126
* a protected system like OS/2. Use Microsoft C instead.
129
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
131
voidpf buf = opaque; /* just to make some compilers happy */
132
ulg bsize = (ulg)items*size;
134
/* If we allocate less than 65520 bytes, we assume that farmalloc
135
* will return a usable pointer which doesn't have to be normalized.
137
if (bsize < 65520L) {
138
buf = farmalloc(bsize);
139
if (*(ush*)&buf != 0) return buf;
141
buf = farmalloc(bsize + 16L);
143
if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
144
table[next_ptr].org_ptr = buf;
146
/* Normalize the pointer to seg:0 */
147
*((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
149
table[next_ptr++].new_ptr = buf;
153
void zcfree (voidpf opaque, voidpf ptr)
156
if (*(ush*)&ptr != 0) { /* object < 64K */
160
/* Find the original pointer */
161
for (n = 0; n < next_ptr; n++) {
162
if (ptr != table[n].new_ptr) continue;
164
farfree(table[n].org_ptr);
165
while (++n < next_ptr) {
166
table[n-1] = table[n];
171
ptr = opaque; /* just to make some compilers happy */
172
Assert(0, "zcfree: ptr not found");
175
#endif /* __TURBOC__ */
178
#if defined(M_I86) && !defined(__32BIT__)
179
/* Microsoft C in 16-bit mode */
183
#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
184
# define _halloc halloc
185
# define _hfree hfree
188
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
190
if (opaque) opaque = 0; /* to make compiler happy */
191
return _halloc((long)items, size);
194
void zcfree (voidpf opaque, voidpf ptr)
196
if (opaque) opaque = 0; /* to make compiler happy */
203
#ifndef MY_ZCALLOC /* Any system without a special alloc function */
206
extern voidp calloc OF((uInt items, uInt size));
207
extern void free OF((voidpf ptr));
210
voidpf zcalloc (opaque, items, size)
215
if (opaque) items += size - size; /* make compiler happy */
216
return (voidpf)calloc(items, size);
219
void zcfree (opaque, ptr)
224
if (opaque) return; /* make compiler happy */
227
#endif /* MY_ZCALLOC */