Got rid of setgroups() caveat for OSX -- we fixed that a while back.
[rsync.git] / zlib / zutil.c
blob91f0ad42bd1a05e02faa00f342dbb5c7da74861c
1 /* zutil.c -- target dependent utility functions for the compression library
2 * Copyright (C) 1995-2002 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
6 /* @(#) $Id$ */
8 #include "zutil.h"
10 struct internal_state {int dummy;}; /* for buggy compilers */
12 #ifndef STDC
13 extern void exit OF((int));
14 #endif
16 const char *z_errmsg[10] = {
17 "need dictionary", /* Z_NEED_DICT 2 */
18 "stream end", /* Z_STREAM_END 1 */
19 "", /* Z_OK 0 */
20 "file error", /* Z_ERRNO (-1) */
21 "stream error", /* Z_STREAM_ERROR (-2) */
22 "data error", /* Z_DATA_ERROR (-3) */
23 "insufficient memory", /* Z_MEM_ERROR (-4) */
24 "buffer error", /* Z_BUF_ERROR (-5) */
25 "incompatible version",/* Z_VERSION_ERROR (-6) */
26 ""};
29 const char * ZEXPORT zlibVersion()
31 return ZLIB_VERSION;
34 #ifdef DEBUG
36 # ifndef verbose
37 # define verbose 0
38 # endif
39 int z_verbose = verbose;
41 void z_error (m)
42 char *m;
44 fprintf(stderr, "%s\n", m);
45 exit(1);
47 #endif
49 /* exported to allow conversion of error code to string for compress() and
50 * uncompress()
52 const char * ZEXPORT zError(err)
53 int err;
55 return ERR_MSG(err);
59 #ifndef HAVE_MEMCPY
61 void zmemcpy(dest, source, len)
62 Bytef* dest;
63 const Bytef* source;
64 uInt len;
66 if (len == 0) return;
67 do {
68 *dest++ = *source++; /* ??? to be unrolled */
69 } while (--len != 0);
72 int zmemcmp(s1, s2, len)
73 const Bytef* s1;
74 const Bytef* s2;
75 uInt len;
77 uInt j;
79 for (j = 0; j < len; j++) {
80 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
82 return 0;
85 void zmemzero(dest, len)
86 Bytef* dest;
87 uInt len;
89 if (len == 0) return;
90 do {
91 *dest++ = 0; /* ??? to be unrolled */
92 } while (--len != 0);
94 #endif
96 #ifdef __TURBOC__
97 #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
98 /* Small and medium model in Turbo C are for now limited to near allocation
99 * with reduced MAX_WBITS and MAX_MEM_LEVEL
101 # define MY_ZCALLOC
103 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
104 * and farmalloc(64K) returns a pointer with an offset of 8, so we
105 * must fix the pointer. Warning: the pointer must be put back to its
106 * original form in order to free it, use zcfree().
109 #define MAX_PTR 10
110 /* 10*64K = 640K */
112 local int next_ptr = 0;
114 typedef struct ptr_table_s {
115 voidpf org_ptr;
116 voidpf new_ptr;
117 } ptr_table;
119 local ptr_table table[MAX_PTR];
120 /* This table is used to remember the original form of pointers
121 * to large buffers (64K). Such pointers are normalized with a zero offset.
122 * Since MSDOS is not a preemptive multitasking OS, this table is not
123 * protected from concurrent access. This hack doesn't work anyway on
124 * a protected system like OS/2. Use Microsoft C instead.
127 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
129 voidpf buf = opaque; /* just to make some compilers happy */
130 ulg bsize = (ulg)items*size;
132 /* If we allocate less than 65520 bytes, we assume that farmalloc
133 * will return a usable pointer which doesn't have to be normalized.
135 if (bsize < 65520L) {
136 buf = farmalloc(bsize);
137 if (*(ush*)&buf != 0) return buf;
138 } else {
139 buf = farmalloc(bsize + 16L);
141 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
142 table[next_ptr].org_ptr = buf;
144 /* Normalize the pointer to seg:0 */
145 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
146 *(ush*)&buf = 0;
147 table[next_ptr++].new_ptr = buf;
148 return buf;
151 void zcfree (voidpf opaque, voidpf ptr)
153 int n;
154 if (*(ush*)&ptr != 0) { /* object < 64K */
155 farfree(ptr);
156 return;
158 /* Find the original pointer */
159 for (n = 0; n < next_ptr; n++) {
160 if (ptr != table[n].new_ptr) continue;
162 farfree(table[n].org_ptr);
163 while (++n < next_ptr) {
164 table[n-1] = table[n];
166 next_ptr--;
167 return;
169 ptr = opaque; /* just to make some compilers happy */
170 Assert(0, "zcfree: ptr not found");
172 #endif
173 #endif /* __TURBOC__ */
176 #if defined(M_I86) && !defined(__32BIT__)
177 /* Microsoft C in 16-bit mode */
179 # define MY_ZCALLOC
181 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
182 # define _halloc halloc
183 # define _hfree hfree
184 #endif
186 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
188 if (opaque) opaque = 0; /* to make compiler happy */
189 return _halloc((long)items, size);
192 void zcfree (voidpf opaque, voidpf ptr)
194 if (opaque) opaque = 0; /* to make compiler happy */
195 _hfree(ptr);
198 #endif /* MSC */
201 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
203 voidpf zcalloc (opaque, items, size)
204 voidpf opaque;
205 unsigned items;
206 unsigned size;
208 if (opaque) items += size - size; /* make compiler happy */
209 return (voidpf)calloc(items, size);
212 void zcfree (opaque, ptr)
213 voidpf opaque;
214 voidpf ptr;
216 free(ptr);
217 if (opaque) return; /* make compiler happy */
220 #endif /* MY_ZCALLOC */