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