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