199767f8919635c4928607450d9e0abb932109ceToomas Soome/* zutil.c -- target dependent utility functions for the compression library
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (C) 1995-2005, 2010, 2011, 2012 Jean-loup Gailly.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * For conditions of distribution and use, see copyright notice in zlib.h
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* @(#) $Id$ */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct internal_state {int dummy;}; /* for buggy compilers */
199767f8919635c4928607450d9e0abb932109ceToomas Soome"incompatible version",/* Z_VERSION_ERROR (-6) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch ((int)(sizeof(uInt))) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 2: break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch ((int)(sizeof(uLong))) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 2: break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch ((int)(sizeof(voidpf))) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 2: break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch ((int)(sizeof(z_off_t))) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 2: break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* exported to allow conversion of error code to string for compress() and
199767f8919635c4928607450d9e0abb932109ceToomas Soome * uncompress()
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The Microsoft C Run-Time Library for Windows CE doesn't have
199767f8919635c4928607450d9e0abb932109ceToomas Soome * errno. We define it as a global variable to simplify porting.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Its value is always 0 and should not be used.
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len == 0) return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (--len != 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (j = 0; j < len; j++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len == 0) return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (--len != 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Turbo C in 16-bit mode */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and farmalloc(64K) returns a pointer with an offset of 8, so we
199767f8919635c4928607450d9e0abb932109ceToomas Soome * must fix the pointer. Warning: the pointer must be put back to its
199767f8919635c4928607450d9e0abb932109ceToomas Soome * original form in order to free it, use zcfree().
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* 10*64K = 640K */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* This table is used to remember the original form of pointers
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to large buffers (64K). Such pointers are normalized with a zero offset.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Since MSDOS is not a preemptive multitasking OS, this table is not
199767f8919635c4928607450d9e0abb932109ceToomas Soome * protected from concurrent access. This hack doesn't work anyway on
199767f8919635c4928607450d9e0abb932109ceToomas Soome * a protected system like OS/2. Use Microsoft C instead.
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome voidpf buf = opaque; /* just to make some compilers happy */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If we allocate less than 65520 bytes, we assume that farmalloc
199767f8919635c4928607450d9e0abb932109ceToomas Soome * will return a usable pointer which doesn't have to be normalized.
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Normalize the pointer to seg:0 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Find the original pointer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0; n < next_ptr; n++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (++n < next_ptr) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome ptr = opaque; /* just to make some compilers happy */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* __TURBOC__ */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Microsoft C in 16-bit mode */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (opaque) opaque = 0; /* to make compiler happy */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (opaque) opaque = 0; /* to make compiler happy */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* M_I86 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* SYS16BIT */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef MY_ZCALLOC /* Any system without a special alloc function */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern voidp calloc OF((uInt items, uInt size));
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (opaque) items += size - size; /* make compiler happy */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* MY_ZCALLOC */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* !Z_SOLO */