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