]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | /* zutil.c -- target dependent utility functions for the compression library |
2 | * Copyright (C) 1995-1998 Jean-loup Gailly. | |
e6ebb514 | 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
c801d85f KB |
4 | */ |
5 | ||
6 | /* @(#) $Id$ */ | |
7 | ||
8 | #include "zutil.h" | |
9 | ||
10 | struct internal_state {int dummy;}; /* for buggy compilers */ | |
11 | ||
12 | #ifndef STDC | |
13 | extern void exit OF((int)); | |
14 | #endif | |
15 | ||
16 | const char *z_errmsg[10] = { | |
17 | "need dictionary", /* Z_NEED_DICT 2 */ | |
18 | "stream end", /* Z_STREAM_END 1 */ | |
19 | "", /* Z_OK 0 */ | |
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) */ | |
26 | ""}; | |
27 | ||
28 | ||
29 | const char * ZEXPORT zlibVersion() | |
30 | { | |
31 | return ZLIB_VERSION; | |
32 | } | |
33 | ||
c6cdbc3d | 34 | #ifdef __WXDEBUG__ |
c801d85f KB |
35 | |
36 | # ifndef verbose | |
37 | # define verbose 0 | |
38 | # endif | |
39 | int z_verbose = verbose; | |
40 | ||
e6ebb514 DW |
41 | #if defined(__VISAGECPP__) // Visual game can't handle this antiquated interface |
42 | void z_error (char* m) | |
43 | #else | |
c801d85f KB |
44 | void z_error (m) |
45 | char *m; | |
e6ebb514 | 46 | #endif |
c801d85f KB |
47 | { |
48 | fprintf(stderr, "%s\n", m); | |
49 | exit(1); | |
50 | } | |
51 | #endif | |
52 | ||
53 | /* exported to allow conversion of error code to string for compress() and | |
54 | * uncompress() | |
55 | */ | |
e6ebb514 DW |
56 | #if defined(__VISAGECPP__) // Visual game can't handle this antiquated interface |
57 | const char* ZEXPORT zError(int err) | |
58 | #else | |
c801d85f KB |
59 | const char * ZEXPORT zError(err) |
60 | int err; | |
e6ebb514 | 61 | #endif |
c801d85f KB |
62 | { |
63 | return ERR_MSG(err); | |
64 | } | |
65 | ||
66 | ||
67 | #ifndef HAVE_MEMCPY | |
68 | ||
e6ebb514 DW |
69 | #if defined(__VISAGECPP__) // Visual game can't handle this antiquated interface |
70 | void zmemcpy(Bytef* dest, Bytef* source, Uint len) | |
71 | #else | |
c801d85f KB |
72 | void zmemcpy(dest, source, len) |
73 | Bytef* dest; | |
74 | Bytef* source; | |
75 | uInt len; | |
e6ebb514 | 76 | #endif |
c801d85f KB |
77 | { |
78 | if (len == 0) return; | |
79 | do { | |
80 | *dest++ = *source++; /* ??? to be unrolled */ | |
81 | } while (--len != 0); | |
82 | } | |
83 | ||
e6ebb514 DW |
84 | #if defined(__VISAGECPP__) // Visual game can't handle this antiquated interface |
85 | int zmemcmp(Bytef* s1, Bytef* s2, int len) | |
86 | #else | |
c801d85f KB |
87 | int zmemcmp(s1, s2, len) |
88 | Bytef* s1; | |
89 | Bytef* s2; | |
90 | uInt len; | |
e6ebb514 | 91 | #endif |
c801d85f KB |
92 | { |
93 | uInt j; | |
94 | ||
95 | for (j = 0; j < len; j++) { | |
96 | if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; | |
97 | } | |
98 | return 0; | |
99 | } | |
100 | ||
e6ebb514 DW |
101 | #if defined(__VISAGECPP__) // Visual game can't handle this antiquated interface |
102 | void zmemzero(Bytef* dest, uInt len) | |
103 | #else | |
c801d85f KB |
104 | void zmemzero(dest, len) |
105 | Bytef* dest; | |
106 | uInt len; | |
e6ebb514 | 107 | #endif |
c801d85f KB |
108 | { |
109 | if (len == 0) return; | |
110 | do { | |
111 | *dest++ = 0; /* ??? to be unrolled */ | |
112 | } while (--len != 0); | |
113 | } | |
114 | #endif | |
115 | ||
116 | #ifdef __TURBOC__ | |
117 | #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__) | |
118 | /* Small and medium model in Turbo C are for now limited to near allocation | |
119 | * with reduced MAX_WBITS and MAX_MEM_LEVEL | |
120 | */ | |
121 | # define MY_ZCALLOC | |
122 | ||
123 | /* Turbo C malloc() does not allow dynamic allocation of 64K bytes | |
124 | * and farmalloc(64K) returns a pointer with an offset of 8, so we | |
125 | * must fix the pointer. Warning: the pointer must be put back to its | |
126 | * original form in order to free it, use zcfree(). | |
127 | */ | |
128 | ||
129 | #define MAX_PTR 10 | |
130 | /* 10*64K = 640K */ | |
131 | ||
132 | local int next_ptr = 0; | |
133 | ||
134 | typedef struct ptr_table_s { | |
135 | voidpf org_ptr; | |
136 | voidpf new_ptr; | |
137 | } ptr_table; | |
138 | ||
139 | local ptr_table table[MAX_PTR]; | |
140 | /* This table is used to remember the original form of pointers | |
141 | * to large buffers (64K). Such pointers are normalized with a zero offset. | |
142 | * Since MSDOS is not a preemptive multitasking OS, this table is not | |
143 | * protected from concurrent access. This hack doesn't work anyway on | |
144 | * a protected system like OS/2. Use Microsoft C instead. | |
145 | */ | |
146 | ||
147 | voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) | |
148 | { | |
149 | voidpf buf = opaque; /* just to make some compilers happy */ | |
150 | ulg bsize = (ulg)items*size; | |
151 | ||
152 | /* If we allocate less than 65520 bytes, we assume that farmalloc | |
153 | * will return a usable pointer which doesn't have to be normalized. | |
154 | */ | |
155 | if (bsize < 65520L) { | |
156 | buf = farmalloc(bsize); | |
157 | if (*(ush*)&buf != 0) return buf; | |
158 | } else { | |
159 | buf = farmalloc(bsize + 16L); | |
160 | } | |
161 | if (buf == NULL || next_ptr >= MAX_PTR) return NULL; | |
162 | table[next_ptr].org_ptr = buf; | |
163 | ||
164 | /* Normalize the pointer to seg:0 */ | |
165 | *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; | |
166 | *(ush*)&buf = 0; | |
167 | table[next_ptr++].new_ptr = buf; | |
168 | return buf; | |
169 | } | |
170 | ||
171 | void zcfree (voidpf opaque, voidpf ptr) | |
172 | { | |
173 | int n; | |
174 | if (*(ush*)&ptr != 0) { /* object < 64K */ | |
175 | farfree(ptr); | |
176 | return; | |
177 | } | |
178 | /* Find the original pointer */ | |
179 | for (n = 0; n < next_ptr; n++) { | |
180 | if (ptr != table[n].new_ptr) continue; | |
181 | ||
182 | farfree(table[n].org_ptr); | |
183 | while (++n < next_ptr) { | |
184 | table[n-1] = table[n]; | |
185 | } | |
186 | next_ptr--; | |
187 | return; | |
188 | } | |
189 | ptr = opaque; /* just to make some compilers happy */ | |
190 | Assert(0, "zcfree: ptr not found"); | |
191 | } | |
192 | #endif | |
193 | #endif /* __TURBOC__ */ | |
194 | ||
195 | ||
196 | #if defined(M_I86) && !defined(__32BIT__) | |
197 | /* Microsoft C in 16-bit mode */ | |
198 | ||
199 | # define MY_ZCALLOC | |
200 | ||
201 | #if (!defined(_MSC_VER) || (_MSC_VER < 600)) | |
202 | # define _halloc halloc | |
203 | # define _hfree hfree | |
204 | #endif | |
205 | ||
206 | voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) | |
207 | { | |
208 | if (opaque) opaque = 0; /* to make compiler happy */ | |
209 | return _halloc((long)items, size); | |
210 | } | |
211 | ||
212 | void zcfree (voidpf opaque, voidpf ptr) | |
213 | { | |
214 | if (opaque) opaque = 0; /* to make compiler happy */ | |
215 | _hfree(ptr); | |
216 | } | |
217 | ||
218 | #endif /* MSC */ | |
219 | ||
220 | ||
221 | #ifndef MY_ZCALLOC /* Any system without a special alloc function */ | |
222 | ||
223 | #ifndef STDC | |
224 | extern voidp calloc OF((uInt items, uInt size)); | |
225 | extern void free OF((voidpf ptr)); | |
226 | #endif | |
227 | ||
e6ebb514 DW |
228 | #if defined(__VISAGECPP__) // Visual game can't handle this antiquated interface |
229 | voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) | |
230 | #else | |
c801d85f KB |
231 | voidpf zcalloc (opaque, items, size) |
232 | voidpf opaque; | |
233 | unsigned items; | |
234 | unsigned size; | |
e6ebb514 | 235 | #endif |
c801d85f KB |
236 | { |
237 | if (opaque) items += size - size; /* make compiler happy */ | |
238 | return (voidpf)calloc(items, size); | |
239 | } | |
240 | ||
e6ebb514 DW |
241 | #if defined(__VISAGECPP__) // Visual game can't handle this antiquated interface |
242 | void zcfree(voidpf opaque, voidpf ptr) | |
243 | #else | |
c801d85f KB |
244 | void zcfree (opaque, ptr) |
245 | voidpf opaque; | |
246 | voidpf ptr; | |
e6ebb514 | 247 | #endif |
c801d85f KB |
248 | { |
249 | free(ptr); | |
250 | if (opaque) return; /* make compiler happy */ | |
251 | } | |
252 | ||
253 | #endif /* MY_ZCALLOC */ |