1 /* zutil.c -- target dependent utility functions for the compression library
2 * Copyright (C) 1995-1998 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
10 struct internal_state
{int dummy
;}; /* for buggy compilers */
13 extern void exit
OF((int));
16 const char *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()
39 int z_verbose
= verbose
;
44 fprintf(stderr
, "%s\n", m
);
49 /* exported to allow conversion of error code to string for compress() and
52 const char * ZEXPORT
zError(err
)
61 void zmemcpy(dest
, source
, len
)
68 *dest
++ = *source
++; /* ??? to be unrolled */
72 int zmemcmp(s1
, s2
, len
)
79 for (j
= 0; j
< len
; j
++) {
80 if (s1
[j
] != s2
[j
]) return 2*(s1
[j
] > s2
[j
])-1;
85 void zmemzero(dest
, len
)
91 *dest
++ = 0; /* ??? to be unrolled */
97 #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
98 /* Small and medium model in Turbo C are for now limited to near allocation
99 * with reduced MAX_WBITS and MAX_MEM_LEVEL
103 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
104 * and farmalloc(64K) returns a pointer with an offset of 8, so we
105 * must fix the pointer. Warning: the pointer must be put back to its
106 * original form in order to free it, use zcfree().
112 local
int next_ptr
= 0;
114 typedef struct ptr_table_s
{
119 local ptr_table table
[MAX_PTR
];
120 /* This table is used to remember the original form of pointers
121 * to large buffers (64K). Such pointers are normalized with a zero offset.
122 * Since MSDOS is not a preemptive multitasking OS, this table is not
123 * protected from concurrent access. This hack doesn't work anyway on
124 * a protected system like OS/2. Use Microsoft C instead.
127 voidpf
zcalloc (voidpf opaque
, unsigned items
, unsigned size
)
129 voidpf buf
= opaque
; /* just to make some compilers happy */
130 ulg bsize
= (ulg
)items
*size
;
132 /* If we allocate less than 65520 bytes, we assume that farmalloc
133 * will return a usable pointer which doesn't have to be normalized.
135 if (bsize
< 65520L) {
136 buf
= farmalloc(bsize
);
137 if (*(ush
*)&buf
!= 0) return buf
;
139 buf
= farmalloc(bsize
+ 16L);
141 if (buf
== NULL
|| next_ptr
>= MAX_PTR
) return NULL
;
142 table
[next_ptr
].org_ptr
= buf
;
144 /* Normalize the pointer to seg:0 */
145 *((ush
*)&buf
+1) += ((ush
)((uch
*)buf
-0) + 15) >> 4;
147 table
[next_ptr
++].new_ptr
= buf
;
151 void zcfree (voidpf opaque
, voidpf ptr
)
154 if (*(ush
*)&ptr
!= 0) { /* object < 64K */
158 /* Find the original pointer */
159 for (n
= 0; n
< next_ptr
; n
++) {
160 if (ptr
!= table
[n
].new_ptr
) continue;
162 farfree(table
[n
].org_ptr
);
163 while (++n
< next_ptr
) {
164 table
[n
-1] = table
[n
];
169 ptr
= opaque
; /* just to make some compilers happy */
170 Assert(0, "zcfree: ptr not found");
173 #endif /* __TURBOC__ */
176 #if defined(M_I86) && !defined(__32BIT__)
177 /* Microsoft C in 16-bit mode */
181 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
182 # define _halloc halloc
183 # define _hfree hfree
186 voidpf
zcalloc (voidpf opaque
, unsigned items
, unsigned size
)
188 if (opaque
) opaque
= 0; /* to make compiler happy */
189 return _halloc((long)items
, size
);
192 void zcfree (voidpf opaque
, voidpf ptr
)
194 if (opaque
) opaque
= 0; /* to make compiler happy */
201 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
204 extern voidp calloc
OF((uInt items
, uInt size
));
205 extern void free
OF((voidpf ptr
));
208 voidpf
zcalloc (opaque
, items
, size
)
213 if (opaque
) items
+= size
- size
; /* make compiler happy */
214 return (voidpf
)calloc(items
, size
);
217 void zcfree (opaque
, ptr
)
222 if (opaque
) return; /* make compiler happy */
225 #endif /* MY_ZCALLOC */