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