michael@0: /* zutil.c -- target dependent utility functions for the compression library michael@0: * Copyright (C) 1995-2005, 2010 Jean-loup Gailly. michael@0: * For conditions of distribution and use, see copyright notice in zlib.h michael@0: */ michael@0: michael@0: /* @(#) $Id$ */ michael@0: michael@0: #include "zutil.h" michael@0: michael@0: #ifndef NO_DUMMY_DECL michael@0: struct internal_state {int dummy;}; /* for buggy compilers */ michael@0: #endif michael@0: michael@0: const char * const z_errmsg[10] = { michael@0: "need dictionary", /* Z_NEED_DICT 2 */ michael@0: "stream end", /* Z_STREAM_END 1 */ michael@0: "", /* Z_OK 0 */ michael@0: "file error", /* Z_ERRNO (-1) */ michael@0: "stream error", /* Z_STREAM_ERROR (-2) */ michael@0: "data error", /* Z_DATA_ERROR (-3) */ michael@0: "insufficient memory", /* Z_MEM_ERROR (-4) */ michael@0: "buffer error", /* Z_BUF_ERROR (-5) */ michael@0: "incompatible version",/* Z_VERSION_ERROR (-6) */ michael@0: ""}; michael@0: michael@0: michael@0: const char * ZEXPORT zlibVersion() michael@0: { michael@0: return ZLIB_VERSION; michael@0: } michael@0: michael@0: uLong ZEXPORT zlibCompileFlags() michael@0: { michael@0: uLong flags; michael@0: michael@0: flags = 0; michael@0: switch ((int)(sizeof(uInt))) { michael@0: case 2: break; michael@0: case 4: flags += 1; break; michael@0: case 8: flags += 2; break; michael@0: default: flags += 3; michael@0: } michael@0: switch ((int)(sizeof(uLong))) { michael@0: case 2: break; michael@0: case 4: flags += 1 << 2; break; michael@0: case 8: flags += 2 << 2; break; michael@0: default: flags += 3 << 2; michael@0: } michael@0: switch ((int)(sizeof(voidpf))) { michael@0: case 2: break; michael@0: case 4: flags += 1 << 4; break; michael@0: case 8: flags += 2 << 4; break; michael@0: default: flags += 3 << 4; michael@0: } michael@0: switch ((int)(sizeof(z_off_t))) { michael@0: case 2: break; michael@0: case 4: flags += 1 << 6; break; michael@0: case 8: flags += 2 << 6; break; michael@0: default: flags += 3 << 6; michael@0: } michael@0: #ifdef DEBUG michael@0: flags += 1 << 8; michael@0: #endif michael@0: #if defined(ASMV) || defined(ASMINF) michael@0: flags += 1 << 9; michael@0: #endif michael@0: #ifdef ZLIB_WINAPI michael@0: flags += 1 << 10; michael@0: #endif michael@0: #ifdef BUILDFIXED michael@0: flags += 1 << 12; michael@0: #endif michael@0: #ifdef DYNAMIC_CRC_TABLE michael@0: flags += 1 << 13; michael@0: #endif michael@0: #ifdef NO_GZCOMPRESS michael@0: flags += 1L << 16; michael@0: #endif michael@0: #ifdef NO_GZIP michael@0: flags += 1L << 17; michael@0: #endif michael@0: #ifdef PKZIP_BUG_WORKAROUND michael@0: flags += 1L << 20; michael@0: #endif michael@0: #ifdef FASTEST michael@0: flags += 1L << 21; michael@0: #endif michael@0: #ifdef STDC michael@0: # ifdef NO_vsnprintf michael@0: flags += 1L << 25; michael@0: # ifdef HAS_vsprintf_void michael@0: flags += 1L << 26; michael@0: # endif michael@0: # else michael@0: # ifdef HAS_vsnprintf_void michael@0: flags += 1L << 26; michael@0: # endif michael@0: # endif michael@0: #else michael@0: flags += 1L << 24; michael@0: # ifdef NO_snprintf michael@0: flags += 1L << 25; michael@0: # ifdef HAS_sprintf_void michael@0: flags += 1L << 26; michael@0: # endif michael@0: # else michael@0: # ifdef HAS_snprintf_void michael@0: flags += 1L << 26; michael@0: # endif michael@0: # endif michael@0: #endif michael@0: return flags; michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: michael@0: # ifndef verbose michael@0: # define verbose 0 michael@0: # endif michael@0: int ZLIB_INTERNAL z_verbose = verbose; michael@0: michael@0: void ZLIB_INTERNAL z_error (m) michael@0: char *m; michael@0: { michael@0: fprintf(stderr, "%s\n", m); michael@0: exit(1); michael@0: } michael@0: #endif michael@0: michael@0: /* exported to allow conversion of error code to string for compress() and michael@0: * uncompress() michael@0: */ michael@0: const char * ZEXPORT zError(err) michael@0: int err; michael@0: { michael@0: return ERR_MSG(err); michael@0: } michael@0: michael@0: #if defined(_WIN32_WCE) michael@0: /* The Microsoft C Run-Time Library for Windows CE doesn't have michael@0: * errno. We define it as a global variable to simplify porting. michael@0: * Its value is always 0 and should not be used. michael@0: */ michael@0: int errno = 0; michael@0: #endif michael@0: michael@0: #ifndef HAVE_MEMCPY michael@0: michael@0: void ZLIB_INTERNAL zmemcpy(dest, source, len) michael@0: Bytef* dest; michael@0: const Bytef* source; michael@0: uInt len; michael@0: { michael@0: if (len == 0) return; michael@0: do { michael@0: *dest++ = *source++; /* ??? to be unrolled */ michael@0: } while (--len != 0); michael@0: } michael@0: michael@0: int ZLIB_INTERNAL zmemcmp(s1, s2, len) michael@0: const Bytef* s1; michael@0: const Bytef* s2; michael@0: uInt len; michael@0: { michael@0: uInt j; michael@0: michael@0: for (j = 0; j < len; j++) { michael@0: if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: void ZLIB_INTERNAL zmemzero(dest, len) michael@0: Bytef* dest; michael@0: uInt len; michael@0: { michael@0: if (len == 0) return; michael@0: do { michael@0: *dest++ = 0; /* ??? to be unrolled */ michael@0: } while (--len != 0); michael@0: } michael@0: #endif michael@0: michael@0: michael@0: #ifdef SYS16BIT michael@0: michael@0: #ifdef __TURBOC__ michael@0: /* Turbo C in 16-bit mode */ michael@0: michael@0: # define MY_ZCALLOC michael@0: michael@0: /* Turbo C malloc() does not allow dynamic allocation of 64K bytes michael@0: * and farmalloc(64K) returns a pointer with an offset of 8, so we michael@0: * must fix the pointer. Warning: the pointer must be put back to its michael@0: * original form in order to free it, use zcfree(). michael@0: */ michael@0: michael@0: #define MAX_PTR 10 michael@0: /* 10*64K = 640K */ michael@0: michael@0: local int next_ptr = 0; michael@0: michael@0: typedef struct ptr_table_s { michael@0: voidpf org_ptr; michael@0: voidpf new_ptr; michael@0: } ptr_table; michael@0: michael@0: local ptr_table table[MAX_PTR]; michael@0: /* This table is used to remember the original form of pointers michael@0: * to large buffers (64K). Such pointers are normalized with a zero offset. michael@0: * Since MSDOS is not a preemptive multitasking OS, this table is not michael@0: * protected from concurrent access. This hack doesn't work anyway on michael@0: * a protected system like OS/2. Use Microsoft C instead. michael@0: */ michael@0: michael@0: voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size) michael@0: { michael@0: voidpf buf = opaque; /* just to make some compilers happy */ michael@0: ulg bsize = (ulg)items*size; michael@0: michael@0: /* If we allocate less than 65520 bytes, we assume that farmalloc michael@0: * will return a usable pointer which doesn't have to be normalized. michael@0: */ michael@0: if (bsize < 65520L) { michael@0: buf = farmalloc(bsize); michael@0: if (*(ush*)&buf != 0) return buf; michael@0: } else { michael@0: buf = farmalloc(bsize + 16L); michael@0: } michael@0: if (buf == NULL || next_ptr >= MAX_PTR) return NULL; michael@0: table[next_ptr].org_ptr = buf; michael@0: michael@0: /* Normalize the pointer to seg:0 */ michael@0: *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; michael@0: *(ush*)&buf = 0; michael@0: table[next_ptr++].new_ptr = buf; michael@0: return buf; michael@0: } michael@0: michael@0: void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) michael@0: { michael@0: int n; michael@0: if (*(ush*)&ptr != 0) { /* object < 64K */ michael@0: farfree(ptr); michael@0: return; michael@0: } michael@0: /* Find the original pointer */ michael@0: for (n = 0; n < next_ptr; n++) { michael@0: if (ptr != table[n].new_ptr) continue; michael@0: michael@0: farfree(table[n].org_ptr); michael@0: while (++n < next_ptr) { michael@0: table[n-1] = table[n]; michael@0: } michael@0: next_ptr--; michael@0: return; michael@0: } michael@0: ptr = opaque; /* just to make some compilers happy */ michael@0: Assert(0, "zcfree: ptr not found"); michael@0: } michael@0: michael@0: #endif /* __TURBOC__ */ michael@0: michael@0: michael@0: #ifdef M_I86 michael@0: /* Microsoft C in 16-bit mode */ michael@0: michael@0: # define MY_ZCALLOC michael@0: michael@0: #if (!defined(_MSC_VER) || (_MSC_VER <= 600)) michael@0: # define _halloc halloc michael@0: # define _hfree hfree michael@0: #endif michael@0: michael@0: voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size) michael@0: { michael@0: if (opaque) opaque = 0; /* to make compiler happy */ michael@0: return _halloc((long)items, size); michael@0: } michael@0: michael@0: void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) michael@0: { michael@0: if (opaque) opaque = 0; /* to make compiler happy */ michael@0: _hfree(ptr); michael@0: } michael@0: michael@0: #endif /* M_I86 */ michael@0: michael@0: #endif /* SYS16BIT */ michael@0: michael@0: michael@0: #ifndef MY_ZCALLOC /* Any system without a special alloc function */ michael@0: michael@0: #ifndef STDC michael@0: extern voidp malloc OF((uInt size)); michael@0: extern voidp calloc OF((uInt items, uInt size)); michael@0: extern void free OF((voidpf ptr)); michael@0: #endif michael@0: michael@0: voidpf ZLIB_INTERNAL zcalloc (opaque, items, size) michael@0: voidpf opaque; michael@0: unsigned items; michael@0: unsigned size; michael@0: { michael@0: if (opaque) items += size - size; /* make compiler happy */ michael@0: return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : michael@0: (voidpf)calloc(items, size); michael@0: } michael@0: michael@0: void ZLIB_INTERNAL zcfree (opaque, ptr) michael@0: voidpf opaque; michael@0: voidpf ptr; michael@0: { michael@0: free(ptr); michael@0: if (opaque) return; /* make compiler happy */ michael@0: } michael@0: michael@0: #endif /* MY_ZCALLOC */