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