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