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