c62d2810 |
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 |
4 | */ |
5 | |
6 | /* @(#) $Id: zutil.c,v 1.2 2002/03/13 17:45:56 xodnizel Exp $ */ |
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 | |
34 | #ifdef DEBUG |
35 | |
36 | # ifndef verbose |
37 | # define verbose 0 |
38 | # endif |
39 | int z_verbose = verbose; |
40 | |
41 | void z_error (m) |
42 | char *m; |
43 | { |
44 | fprintf(stderr, "%s\n", m); |
45 | exit(1); |
46 | } |
47 | #endif |
48 | |
49 | /* exported to allow conversion of error code to string for compress() and |
50 | * uncompress() |
51 | */ |
52 | const char * ZEXPORT zError(err) |
53 | int err; |
54 | { |
55 | return ERR_MSG(err); |
56 | } |
57 | |
58 | |
59 | #ifndef HAVE_MEMCPY |
60 | |
61 | void zmemcpy(dest, source, len) |
62 | Bytef* dest; |
63 | const Bytef* source; |
64 | uInt len; |
65 | { |
66 | if (len == 0) return; |
67 | do { |
68 | *dest++ = *source++; /* ??? to be unrolled */ |
69 | } while (--len != 0); |
70 | } |
71 | |
72 | int zmemcmp(s1, s2, len) |
73 | const Bytef* s1; |
74 | const Bytef* s2; |
75 | uInt len; |
76 | { |
77 | uInt j; |
78 | |
79 | for (j = 0; j < len; j++) { |
80 | if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; |
81 | } |
82 | return 0; |
83 | } |
84 | |
85 | void zmemzero(dest, len) |
86 | Bytef* dest; |
87 | uInt len; |
88 | { |
89 | if (len == 0) return; |
90 | do { |
91 | *dest++ = 0; /* ??? to be unrolled */ |
92 | } while (--len != 0); |
93 | } |
94 | #endif |
95 | |
96 | #ifdef __TURBOC__ |
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 |
100 | */ |
101 | # define MY_ZCALLOC |
102 | |
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(). |
107 | */ |
108 | |
109 | #define MAX_PTR 10 |
110 | /* 10*64K = 640K */ |
111 | |
112 | local int next_ptr = 0; |
113 | |
114 | typedef struct ptr_table_s { |
115 | voidpf org_ptr; |
116 | voidpf new_ptr; |
117 | } ptr_table; |
118 | |
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. |
125 | */ |
126 | |
127 | voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) |
128 | { |
129 | voidpf buf = opaque; /* just to make some compilers happy */ |
130 | ulg bsize = (ulg)items*size; |
131 | |
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. |
134 | */ |
135 | if (bsize < 65520L) { |
136 | buf = farmalloc(bsize); |
137 | if (*(ush*)&buf != 0) return buf; |
138 | } else { |
139 | buf = farmalloc(bsize + 16L); |
140 | } |
141 | if (buf == NULL || next_ptr >= MAX_PTR) return NULL; |
142 | table[next_ptr].org_ptr = buf; |
143 | |
144 | /* Normalize the pointer to seg:0 */ |
145 | *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; |
146 | *(ush*)&buf = 0; |
147 | table[next_ptr++].new_ptr = buf; |
148 | return buf; |
149 | } |
150 | |
151 | void zcfree (voidpf opaque, voidpf ptr) |
152 | { |
153 | int n; |
154 | if (*(ush*)&ptr != 0) { /* object < 64K */ |
155 | farfree(ptr); |
156 | return; |
157 | } |
158 | /* Find the original pointer */ |
159 | for (n = 0; n < next_ptr; n++) { |
160 | if (ptr != table[n].new_ptr) continue; |
161 | |
162 | farfree(table[n].org_ptr); |
163 | while (++n < next_ptr) { |
164 | table[n-1] = table[n]; |
165 | } |
166 | next_ptr--; |
167 | return; |
168 | } |
169 | ptr = opaque; /* just to make some compilers happy */ |
170 | Assert(0, "zcfree: ptr not found"); |
171 | } |
172 | #endif |
173 | #endif /* __TURBOC__ */ |
174 | |
175 | |
176 | #if defined(M_I86) && !defined(__32BIT__) |
177 | /* Microsoft C in 16-bit mode */ |
178 | |
179 | # define MY_ZCALLOC |
180 | |
181 | #if (!defined(_MSC_VER) || (_MSC_VER <= 600)) |
182 | # define _halloc halloc |
183 | # define _hfree hfree |
184 | #endif |
185 | |
186 | voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) |
187 | { |
188 | if (opaque) opaque = 0; /* to make compiler happy */ |
189 | return _halloc((long)items, size); |
190 | } |
191 | |
192 | void zcfree (voidpf opaque, voidpf ptr) |
193 | { |
194 | if (opaque) opaque = 0; /* to make compiler happy */ |
195 | _hfree(ptr); |
196 | } |
197 | |
198 | #endif /* MSC */ |
199 | |
200 | |
201 | #ifndef MY_ZCALLOC /* Any system without a special alloc function */ |
202 | |
203 | #ifndef STDC |
204 | extern voidp calloc OF((uInt items, uInt size)); |
205 | extern void free OF((voidpf ptr)); |
206 | #endif |
207 | |
208 | voidpf zcalloc (opaque, items, size) |
209 | voidpf opaque; |
210 | unsigned items; |
211 | unsigned size; |
212 | { |
213 | if (opaque) items += size - size; /* make compiler happy */ |
214 | return (voidpf)calloc(items, size); |
215 | } |
216 | |
217 | void zcfree (opaque, ptr) |
218 | voidpf opaque; |
219 | voidpf ptr; |
220 | { |
221 | free(ptr); |
222 | if (opaque) return; /* make compiler happy */ |
223 | } |
224 | |
225 | #endif /* MY_ZCALLOC */ |