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