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