Watt-32 tcp/ip  2.2 dev-rel.10
zutil.c
1 /* zutil.c -- target dependent utility functions for the compression library
2  * Copyright (C) 1995-2003 Jean-loup Gailly.
3  * For conditions of distribution and use, see copyright notice in zlib.h
4  */
5 
6 /* @(#) $Id$ */
7 
8 #include "wattcp.h"
9 #include "zutil.h"
10 
11 #if defined(USE_GZIP)
12 
13 #ifndef NO_DUMMY_DECL
14 struct internal_state {int dummy;}; /* for buggy compilers */
15 #endif
16 
17 #ifndef STDC
18 extern void exit OF((int));
19 #endif
20 
21 const char * const z_errmsg[10] = {
22 "need dictionary", /* Z_NEED_DICT 2 */
23 "stream end", /* Z_STREAM_END 1 */
24 "", /* Z_OK 0 */
25 "file error", /* Z_ERRNO (-1) */
26 "stream error", /* Z_STREAM_ERROR (-2) */
27 "data error", /* Z_DATA_ERROR (-3) */
28 "insufficient memory", /* Z_MEM_ERROR (-4) */
29 "buffer error", /* Z_BUF_ERROR (-5) */
30 "incompatible version",/* Z_VERSION_ERROR (-6) */
31 ""};
32 
33 
34 const char * ZEXPORT zlibVersion()
35 {
36  return ZLIB_VERSION;
37 }
38 
39 uLong ZEXPORT zlibCompileFlags()
40 {
41  uLong flags;
42 
43  flags = 0;
44  switch (sizeof(uInt)) {
45  case 2: break;
46  case 4: flags += 1; break;
47  case 8: flags += 2; break;
48  default: flags += 3;
49  }
50  switch (sizeof(uLong)) {
51  case 2: break;
52  case 4: flags += 1 << 2; break;
53  case 8: flags += 2 << 2; break;
54  default: flags += 3 << 2;
55  }
56  switch (sizeof(voidpf)) {
57  case 2: break;
58  case 4: flags += 1 << 4; break;
59  case 8: flags += 2 << 4; break;
60  default: flags += 3 << 4;
61  }
62  switch (sizeof(z_off_t)) {
63  case 2: break;
64  case 4: flags += 1 << 6; break;
65  case 8: flags += 2 << 6; break;
66  default: flags += 3 << 6;
67  }
68 #ifdef DEBUG
69  flags += 1 << 8;
70 #endif
71 #if defined(ASMV) || defined(ASMINF)
72  flags += 1 << 9;
73 #endif
74 #ifdef ZLIB_WINAPI
75  flags += 1 << 10;
76 #endif
77 #ifdef BUILDFIXED
78  flags += 1 << 12;
79 #endif
80 #ifdef DYNAMIC_CRC_TABLE
81  flags += 1 << 13;
82 #endif
83 #ifdef NO_GZCOMPRESS
84  flags += 1 << 16;
85 #endif
86 #ifdef NO_GZIP
87  flags += 1 << 17;
88 #endif
89 #ifdef PKZIP_BUG_WORKAROUND
90  flags += 1 << 20;
91 #endif
92 #ifdef FASTEST
93  flags += 1 << 21;
94 #endif
95 #ifdef STDC
96 # ifdef NO_vsnprintf
97  flags += 1 << 25;
98 # ifdef HAS_vsprintf_void
99  flags += 1 << 26;
100 # endif
101 # else
102 # ifdef HAS_vsnprintf_void
103  flags += 1 << 26;
104 # endif
105 # endif
106 #else
107  flags += 1 << 24;
108 # ifdef NO_snprintf
109  flags += 1 << 25;
110 # ifdef HAS_sprintf_void
111  flags += 1 << 26;
112 # endif
113 # else
114 # ifdef HAS_snprintf_void
115  flags += 1 << 26;
116 # endif
117 # endif
118 #endif
119  return flags;
120 }
121 
122 #ifdef DEBUG
123 
124 # ifndef verbose
125 # define verbose 0
126 # endif
127 int z_verbose = verbose;
128 
129 void z_error (m)
130  char *m;
131 {
132  fprintf(stderr, "%s\n", m);
133  exit(1);
134 }
135 #endif
136 
137 /* exported to allow conversion of error code to string for compress() and
138  * uncompress()
139  */
140 const char * ZEXPORT zError(err)
141  int err;
142 {
143  return ERR_MSG(err);
144 }
145 
146 #if defined(_WIN32_WCE)
147  /* does not exist on WCE */
148  int errno = 0;
149 #endif
150 
151 #ifndef HAVE_MEMCPY
152 
153 void zmemcpy(dest, source, len)
154  Bytef* dest;
155  const Bytef* source;
156  uInt len;
157 {
158  if (len == 0) return;
159  do {
160  *dest++ = *source++; /* ??? to be unrolled */
161  } while (--len != 0);
162 }
163 
164 int zmemcmp(s1, s2, len)
165  const Bytef* s1;
166  const Bytef* s2;
167  uInt len;
168 {
169  uInt j;
170 
171  for (j = 0; j < len; j++) {
172  if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
173  }
174  return 0;
175 }
176 
177 void zmemzero(dest, len)
178  Bytef* dest;
179  uInt len;
180 {
181  if (len == 0) return;
182  do {
183  *dest++ = 0; /* ??? to be unrolled */
184  } while (--len != 0);
185 }
186 #endif
187 
188 
189 #ifdef SYS16BIT
190 
191 #ifdef __TURBOC__
192 /* Turbo C in 16-bit mode */
193 
194 # define MY_ZCALLOC
195 
196 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
197  * and farmalloc(64K) returns a pointer with an offset of 8, so we
198  * must fix the pointer. Warning: the pointer must be put back to its
199  * original form in order to free it, use zcfree().
200  */
201 
202 #define MAX_PTR 10
203 /* 10*64K = 640K */
204 
205 local int next_ptr = 0;
206 
207 typedef struct ptr_table_s {
208  voidpf org_ptr;
209  voidpf new_ptr;
210 } ptr_table;
211 
212 local ptr_table table[MAX_PTR];
213 /* This table is used to remember the original form of pointers
214  * to large buffers (64K). Such pointers are normalized with a zero offset.
215  * Since MSDOS is not a preemptive multitasking OS, this table is not
216  * protected from concurrent access. This hack doesn't work anyway on
217  * a protected system like OS/2. Use Microsoft C instead.
218  */
219 
220 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
221 {
222  voidpf buf = opaque; /* just to make some compilers happy */
223  ulg bsize = (ulg)items*size;
224 
225  /* If we allocate less than 65520 bytes, we assume that farmalloc
226  * will return a usable pointer which doesn't have to be normalized.
227  */
228  if (bsize < 65520L) {
229  buf = farmalloc(bsize);
230  if (*(ush*)&buf != 0) return buf;
231  } else {
232  buf = farmalloc(bsize + 16L);
233  }
234  if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
235  table[next_ptr].org_ptr = buf;
236 
237  /* Normalize the pointer to seg:0 */
238  *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
239  *(ush*)&buf = 0;
240  table[next_ptr++].new_ptr = buf;
241  return buf;
242 }
243 
244 void zcfree (voidpf opaque, voidpf ptr)
245 {
246  int n;
247  if (*(ush*)&ptr != 0) { /* object < 64K */
248  farfree(ptr);
249  return;
250  }
251  /* Find the original pointer */
252  for (n = 0; n < next_ptr; n++) {
253  if (ptr != table[n].new_ptr) continue;
254 
255  farfree(table[n].org_ptr);
256  while (++n < next_ptr) {
257  table[n-1] = table[n];
258  }
259  next_ptr--;
260  return;
261  }
262  ptr = opaque; /* just to make some compilers happy */
263  Assert(0, "zcfree: ptr not found");
264 }
265 
266 #endif /* __TURBOC__ */
267 
268 
269 #ifdef M_I86
270 /* Microsoft C in 16-bit mode */
271 
272 # define MY_ZCALLOC
273 
274 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
275 # define _halloc halloc
276 # define _hfree hfree
277 #endif
278 
279 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
280 {
281  if (opaque) opaque = 0; /* to make compiler happy */
282  return _halloc((long)items, size);
283 }
284 
285 void zcfree (voidpf opaque, voidpf ptr)
286 {
287  if (opaque) opaque = 0; /* to make compiler happy */
288  _hfree(ptr);
289 }
290 
291 #endif /* M_I86 */
292 
293 #endif /* SYS16BIT */
294 
295 
296 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
297 
298 #ifndef STDC
299 extern voidp malloc OF((uInt size));
300 extern voidp calloc OF((uInt items, uInt size));
301 extern void free OF((voidpf ptr));
302 #endif
303 
304 voidpf zcalloc (opaque, items, size)
305  voidpf opaque;
306  unsigned items;
307  unsigned size;
308 {
309  if (opaque) items += size - size; /* make compiler happy */
310  return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
311  (voidpf)calloc(items, size);
312 }
313 
314 void zcfree (opaque, ptr)
315  voidpf opaque;
316  voidpf ptr;
317 {
318  free(ptr);
319  if (opaque) return; /* make compiler happy */
320 }
321 #endif /* MY_ZCALLOC */
322 #endif /* USE_GZIP */
323 
Core definitions.