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