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