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