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