xref: /illumos-gate/usr/src/contrib/zlib/zutil.c (revision 55d6cb5d63bcf69dfa47b8c41c770a2d34f169b0)
1 /* zutil.c -- target dependent utility functions for the compression library
2  * Copyright (C) 1995-2017 Jean-loup Gailly
3  * For conditions of distribution and use, see copyright notice in zlib.h
4  */
5 
6 #include "zutil.h"
7 #if !defined(Z_SOLO) && !defined(_KERNEL)
8 #  include "gzguts.h"
9 #endif
10 
11 z_const char * const z_errmsg[10] = {
12     (z_const char *)"need dictionary",     /* Z_NEED_DICT       2  */
13     (z_const char *)"stream end",          /* Z_STREAM_END      1  */
14     (z_const char *)"",                    /* Z_OK              0  */
15     (z_const char *)"file error",          /* Z_ERRNO         (-1) */
16     (z_const char *)"stream error",        /* Z_STREAM_ERROR  (-2) */
17     (z_const char *)"data error",          /* Z_DATA_ERROR    (-3) */
18     (z_const char *)"insufficient memory", /* Z_MEM_ERROR     (-4) */
19     (z_const char *)"buffer error",        /* Z_BUF_ERROR     (-5) */
20     (z_const char *)"incompatible version",/* Z_VERSION_ERROR (-6) */
21     (z_const char *)""
22 };
23 
24 
25 const char * ZEXPORT zlibVersion(void)
26 {
27     return ZLIB_VERSION;
28 }
29 
30 uLong ZEXPORT zlibCompileFlags(void)
31 {
32     uLong flags;
33 
34     flags = 0;
35     switch ((int)(sizeof(uInt))) {
36     case 2:     break;
37     case 4:     flags += 1;     break;
38     case 8:     flags += 2;     break;
39     default:    flags += 3;
40     }
41     switch ((int)(sizeof(uLong))) {
42     case 2:     break;
43     case 4:     flags += 1 << 2;        break;
44     case 8:     flags += 2 << 2;        break;
45     default:    flags += 3 << 2;
46     }
47     switch ((int)(sizeof(voidpf))) {
48     case 2:     break;
49     case 4:     flags += 1 << 4;        break;
50     case 8:     flags += 2 << 4;        break;
51     default:    flags += 3 << 4;
52     }
53     switch ((int)(sizeof(z_off_t))) {
54     case 2:     break;
55     case 4:     flags += 1 << 6;        break;
56     case 8:     flags += 2 << 6;        break;
57     default:    flags += 3 << 6;
58     }
59 #ifdef ZLIB_DEBUG
60     flags += 1 << 8;
61 #endif
62 #if defined(ASMV) || defined(ASMINF)
63     flags += 1 << 9;
64 #endif
65 #ifdef ZLIB_WINAPI
66     flags += 1 << 10;
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_GZCOMPRESS
75     flags += 1L << 16;
76 #endif
77 #ifdef NO_GZIP
78     flags += 1L << 17;
79 #endif
80 #ifdef PKZIP_BUG_WORKAROUND
81     flags += 1L << 20;
82 #endif
83 #ifdef FASTEST
84     flags += 1L << 21;
85 #endif
86 #if defined(STDC) || defined(Z_HAVE_STDARG_H)
87 #  ifdef NO_vsnprintf
88     flags += 1L << 25;
89 #    ifdef HAS_vsprintf_void
90     flags += 1L << 26;
91 #    endif
92 #  else
93 #    ifdef HAS_vsnprintf_void
94     flags += 1L << 26;
95 #    endif
96 #  endif
97 #else
98     flags += 1L << 24;
99 #  ifdef NO_snprintf
100     flags += 1L << 25;
101 #    ifdef HAS_sprintf_void
102     flags += 1L << 26;
103 #    endif
104 #  else
105 #    ifdef HAS_snprintf_void
106     flags += 1L << 26;
107 #    endif
108 #  endif
109 #endif
110     return flags;
111 }
112 
113 #ifdef ZLIB_DEBUG
114 #include <stdlib.h>
115 #  ifndef verbose
116 #    define verbose 0
117 #  endif
118 int ZLIB_INTERNAL z_verbose = verbose;
119 
120 void ZLIB_INTERNAL 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(int err)
132 {
133     return ERR_MSG(err);
134 }
135 
136 #if defined(_WIN32_WCE)
137     /* The Microsoft C Run-Time Library for Windows CE doesn't have
138      * errno.  We define it as a global variable to simplify porting.
139      * Its value is always 0 and should not be used.
140      */
141     int errno = 0;
142 #endif
143 
144 #if !defined(HAVE_MEMCPY) && !defined(_KERNEL)
145 
146 void ZLIB_INTERNAL zmemcpy(dest, source, len)
147     Bytef* dest;
148     const Bytef* source;
149     uInt  len;
150 {
151     if (len == 0) return;
152     do {
153         *dest++ = *source++; /* ??? to be unrolled */
154     } while (--len != 0);
155 }
156 
157 int ZLIB_INTERNAL zmemcmp(s1, s2, len)
158     const Bytef* s1;
159     const Bytef* s2;
160     uInt  len;
161 {
162     uInt j;
163 
164     for (j = 0; j < len; j++) {
165         if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
166     }
167     return 0;
168 }
169 
170 void ZLIB_INTERNAL zmemzero(dest, len)
171     Bytef* dest;
172     uInt  len;
173 {
174     if (len == 0) return;
175     do {
176         *dest++ = 0;  /* ??? to be unrolled */
177     } while (--len != 0);
178 }
179 #endif
180 
181 #ifndef Z_SOLO
182 
183 #ifdef SYS16BIT
184 
185 #ifdef __TURBOC__
186 /* Turbo C in 16-bit mode */
187 
188 #  define MY_ZCALLOC
189 
190 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
191  * and farmalloc(64K) returns a pointer with an offset of 8, so we
192  * must fix the pointer. Warning: the pointer must be put back to its
193  * original form in order to free it, use zcfree().
194  */
195 
196 #define MAX_PTR 10
197 /* 10*64K = 640K */
198 
199 local int next_ptr = 0;
200 
201 typedef struct ptr_table_s {
202     voidpf org_ptr;
203     voidpf new_ptr;
204 } ptr_table;
205 
206 local ptr_table table[MAX_PTR];
207 /* This table is used to remember the original form of pointers
208  * to large buffers (64K). Such pointers are normalized with a zero offset.
209  * Since MSDOS is not a preemptive multitasking OS, this table is not
210  * protected from concurrent access. This hack doesn't work anyway on
211  * a protected system like OS/2. Use Microsoft C instead.
212  */
213 
214 voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
215 {
216     voidpf buf;
217     ulg bsize = (ulg)items*size;
218 
219     (void)opaque;
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 
240 void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
241 {
242     int n;
243 
244     (void)opaque;
245 
246     if (*(ush*)&ptr != 0) { /* object < 64K */
247         farfree(ptr);
248         return;
249     }
250     /* Find the original pointer */
251     for (n = 0; n < next_ptr; n++) {
252         if (ptr != table[n].new_ptr) continue;
253 
254         farfree(table[n].org_ptr);
255         while (++n < next_ptr) {
256             table[n-1] = table[n];
257         }
258         next_ptr--;
259         return;
260     }
261     Assert(0, "zcfree: ptr not found");
262 }
263 
264 #endif /* __TURBOC__ */
265 
266 
267 #ifdef M_I86
268 /* Microsoft C in 16-bit mode */
269 
270 #  define MY_ZCALLOC
271 
272 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
273 #  define _halloc  halloc
274 #  define _hfree   hfree
275 #endif
276 
277 voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
278 {
279     (void)opaque;
280     return _halloc((long)items, size);
281 }
282 
283 void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
284 {
285     (void)opaque;
286     _hfree(ptr);
287 }
288 
289 #endif /* M_I86 */
290 
291 #endif /* SYS16BIT */
292 
293 
294 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
295 
296 #ifndef STDC
297 extern voidp  malloc OF((uInt size));
298 extern voidp  calloc OF((uInt items, uInt size));
299 extern void   free   OF((voidpf ptr));
300 #endif
301 
302 voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
303 {
304     (void)opaque;
305     return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
306                               (voidpf)calloc(items, size);
307 }
308 
309 void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
310 {
311     (void)opaque;
312     free(ptr);
313 }
314 
315 #endif /* MY_ZCALLOC */
316 
317 #endif /* !Z_SOLO */
318