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