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