ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
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>
12
13#include "zutil.h"
14
15
17
18
19#ifndef NO_DUMMY_DECL
20struct internal_state {int dummy;}; /* for buggy compilers */
21#endif
22
23const 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
36const char * ZEXPORT zlibVersion()
37{
38 return ZLIB_VERSION;
39}
40
42{
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
129int ZLIB_INTERNAL z_verbose = verbose;
130
131void 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 */
142const 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
157void 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
165int 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
201local int next_ptr = 0;
202
203typedef struct ptr_table_s {
204 voidpf org_ptr;
205 voidpf new_ptr;
206} ptr_table;
207
208local 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
216voidpf 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
240void 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
275voidpf 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
281void 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
295extern voidp malloc OF((uInt size));
296extern voidp calloc OF((uInt items, uInt size));
297extern void free OF((voidpf ptr));
298#endif
299
300voidpf 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
308{
309 free(ptr);
310 if (opaque) return; /* make compiler happy */
311}
312
313#endif /* MY_ZCALLOC */
314
315
316
318
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
#define local
Definition adler32.c:17
#define ZLIB_INTERNAL
Definition compress_.c:8
Definition flags.h:11
char * calloc()
VOID_HACK free()
VOID_HACK exit()
char * malloc()
Byte FAR * voidpf
Definition zconf.h:355
#define ZEXPORT
Definition zconf.h:322
unsigned int uInt
Definition zconf.h:335
#define z_off_t
Definition zconf.h:396
Byte * voidp
Definition zconf.h:356
#define OF(args)
Definition zconf.h:242
unsigned long uLong
Definition zconf.h:336
Byte FAR Bytef
Definition zconf.h:342
#define ZLIB_VERSION
Definition zlib.h:48
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
Definition zutil.c:307
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size)
Definition zutil.c:300
const char *ZEXPORT zlibVersion()
Definition zutil.c:36
uLong ZEXPORT zlibCompileFlags()
Definition zutil.c:41
void ZLIB_INTERNAL zmemzero(Bytef *dest, uInt len)
Definition zutil.c:175
int ZLIB_INTERNAL zmemcmp(const Bytef *s1, const Bytef *s2, uInt len)
Definition zutil.c:165
void ZLIB_INTERNAL zmemcpy(Bytef *dest, const Bytef *source, uInt len)
Definition zutil.c:157
const char *const z_errmsg[10]
Definition zutil.c:23
const char *ZEXPORT zError(int err)
Definition zutil.c:142
unsigned short ush
Definition zutil.h:41
#define Assert(cond, msg)
Definition zutil.h:268
#define ERR_MSG(err)
Definition zutil.h:48
unsigned long ulg
Definition zutil.h:43
unsigned char uch
Definition zutil.h:39