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