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