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