1592N/A/*
1592N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1592N/A *
1592N/A * This code is free software; you can redistribute it and/or modify it
1592N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1592N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1592N/A *
1592N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1592N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1592N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1592N/A * version 2 for more details (a copy is included in the LICENSE file that
1592N/A * accompanied this code).
1592N/A *
1592N/A * You should have received a copy of the GNU General Public License version
1592N/A * 2 along with this work; if not, write to the Free Software Foundation,
1592N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1592N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1592N/A */
1592N/A
1592N/A/* zutil.c -- target dependent utility functions for the compression library
1592N/A * Copyright (C) 1995-2005 Jean-loup Gailly.
1592N/A * For conditions of distribution and use, see copyright notice in zlib.h
1592N/A */
1592N/A
1592N/A/* @(#) $Id$ */
1592N/A
1592N/A#include "zutil.h"
1592N/A
1592N/A#ifndef NO_DUMMY_DECL
1592N/Astruct internal_state {int dummy;}; /* for buggy compilers */
1592N/A#endif
1592N/A
1592N/Aconst char * const z_errmsg[10] = {
1592N/A"need dictionary", /* Z_NEED_DICT 2 */
1592N/A"stream end", /* Z_STREAM_END 1 */
1592N/A"", /* Z_OK 0 */
1592N/A"file error", /* Z_ERRNO (-1) */
1592N/A"stream error", /* Z_STREAM_ERROR (-2) */
1592N/A"data error", /* Z_DATA_ERROR (-3) */
1592N/A"insufficient memory", /* Z_MEM_ERROR (-4) */
1592N/A"buffer error", /* Z_BUF_ERROR (-5) */
1592N/A"incompatible version",/* Z_VERSION_ERROR (-6) */
1592N/A""};
1592N/A
1592N/A
1592N/Aconst char * ZEXPORT zlibVersion()
1592N/A{
1592N/A return ZLIB_VERSION;
1592N/A}
1592N/A
1592N/AuLong ZEXPORT zlibCompileFlags()
1592N/A{
1592N/A uLong flags;
1592N/A
1592N/A flags = 0;
1592N/A switch (sizeof(uInt)) {
1592N/A case 2: break;
1592N/A case 4: flags += 1; break;
1592N/A case 8: flags += 2; break;
1592N/A default: flags += 3;
1592N/A }
1592N/A switch (sizeof(uLong)) {
1592N/A case 2: break;
1592N/A case 4: flags += 1 << 2; break;
1592N/A case 8: flags += 2 << 2; break;
1592N/A default: flags += 3 << 2;
1592N/A }
1592N/A switch (sizeof(voidpf)) {
1592N/A case 2: break;
1592N/A case 4: flags += 1 << 4; break;
1592N/A case 8: flags += 2 << 4; break;
1592N/A default: flags += 3 << 4;
1592N/A }
1592N/A switch (sizeof(z_off_t)) {
1592N/A case 2: break;
1592N/A case 4: flags += 1 << 6; break;
1592N/A case 8: flags += 2 << 6; break;
1592N/A default: flags += 3 << 6;
1592N/A }
1592N/A#ifdef DEBUG
1592N/A flags += 1 << 8;
1592N/A#endif
1592N/A#if defined(ASMV) || defined(ASMINF)
1592N/A flags += 1 << 9;
1592N/A#endif
1592N/A#ifdef ZLIB_WINAPI
1592N/A flags += 1 << 10;
1592N/A#endif
1592N/A#ifdef BUILDFIXED
1592N/A flags += 1 << 12;
1592N/A#endif
1592N/A#ifdef DYNAMIC_CRC_TABLE
1592N/A flags += 1 << 13;
1592N/A#endif
1592N/A#ifdef NO_GZCOMPRESS
1592N/A flags += 1L << 16;
1592N/A#endif
1592N/A#ifdef NO_GZIP
1592N/A flags += 1L << 17;
1592N/A#endif
1592N/A#ifdef PKZIP_BUG_WORKAROUND
1592N/A flags += 1L << 20;
1592N/A#endif
1592N/A#ifdef FASTEST
1592N/A flags += 1L << 21;
1592N/A#endif
1592N/A#ifdef STDC
1592N/A# ifdef NO_vsnprintf
1592N/A flags += 1L << 25;
1592N/A# ifdef HAS_vsprintf_void
1592N/A flags += 1L << 26;
1592N/A# endif
1592N/A# else
1592N/A# ifdef HAS_vsnprintf_void
1592N/A flags += 1L << 26;
1592N/A# endif
1592N/A# endif
1592N/A#else
1592N/A flags += 1L << 24;
1592N/A# ifdef NO_snprintf
1592N/A flags += 1L << 25;
1592N/A# ifdef HAS_sprintf_void
1592N/A flags += 1L << 26;
1592N/A# endif
1592N/A# else
1592N/A# ifdef HAS_snprintf_void
1592N/A flags += 1L << 26;
1592N/A# endif
1592N/A# endif
1592N/A#endif
1592N/A return flags;
1592N/A}
1592N/A
1592N/A#ifdef DEBUG
1592N/A
1592N/A# ifndef verbose
1592N/A# define verbose 0
1592N/A# endif
1592N/Aint z_verbose = verbose;
1592N/A
1592N/Avoid z_error (m)
1592N/A char *m;
1592N/A{
1592N/A fprintf(stderr, "%s\n", m);
1592N/A exit(1);
1592N/A}
1592N/A#endif
1592N/A
1592N/A/* exported to allow conversion of error code to string for compress() and
1592N/A * uncompress()
1592N/A */
1592N/Aconst char * ZEXPORT zError(err)
1592N/A int err;
1592N/A{
1592N/A return ERR_MSG(err);
1592N/A}
1592N/A
1592N/A#if defined(_WIN32_WCE)
1592N/A /* The Microsoft C Run-Time Library for Windows CE doesn't have
1592N/A * errno. We define it as a global variable to simplify porting.
1592N/A * Its value is always 0 and should not be used.
1592N/A */
1592N/A int errno = 0;
1592N/A#endif
1592N/A
1592N/A#ifndef HAVE_MEMCPY
1592N/A
1592N/Avoid zmemcpy(dest, source, len)
1592N/A Bytef* dest;
1592N/A const Bytef* source;
1592N/A uInt len;
1592N/A{
1592N/A if (len == 0) return;
1592N/A do {
1592N/A *dest++ = *source++; /* ??? to be unrolled */
1592N/A } while (--len != 0);
1592N/A}
1592N/A
1592N/Aint zmemcmp(s1, s2, len)
1592N/A const Bytef* s1;
1592N/A const Bytef* s2;
1592N/A uInt len;
1592N/A{
1592N/A uInt j;
1592N/A
1592N/A for (j = 0; j < len; j++) {
1592N/A if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
1592N/A }
1592N/A return 0;
1592N/A}
1592N/A
1592N/Avoid zmemzero(dest, len)
1592N/A Bytef* dest;
1592N/A uInt len;
1592N/A{
1592N/A if (len == 0) return;
1592N/A do {
1592N/A *dest++ = 0; /* ??? to be unrolled */
1592N/A } while (--len != 0);
1592N/A}
1592N/A#endif
1592N/A
1592N/A
1592N/A#ifdef SYS16BIT
1592N/A
1592N/A#ifdef __TURBOC__
1592N/A/* Turbo C in 16-bit mode */
1592N/A
1592N/A# define MY_ZCALLOC
1592N/A
1592N/A/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
1592N/A * and farmalloc(64K) returns a pointer with an offset of 8, so we
1592N/A * must fix the pointer. Warning: the pointer must be put back to its
1592N/A * original form in order to free it, use zcfree().
1592N/A */
1592N/A
1592N/A#define MAX_PTR 10
1592N/A/* 10*64K = 640K */
1592N/A
1592N/Alocal int next_ptr = 0;
1592N/A
1592N/Atypedef struct ptr_table_s {
1592N/A voidpf org_ptr;
1592N/A voidpf new_ptr;
1592N/A} ptr_table;
1592N/A
1592N/Alocal ptr_table table[MAX_PTR];
1592N/A/* This table is used to remember the original form of pointers
1592N/A * to large buffers (64K). Such pointers are normalized with a zero offset.
1592N/A * Since MSDOS is not a preemptive multitasking OS, this table is not
1592N/A * protected from concurrent access. This hack doesn't work anyway on
1592N/A * a protected system like OS/2. Use Microsoft C instead.
1592N/A */
1592N/A
1592N/Avoidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
1592N/A{
1592N/A voidpf buf = opaque; /* just to make some compilers happy */
1592N/A ulg bsize = (ulg)items*size;
1592N/A
1592N/A /* If we allocate less than 65520 bytes, we assume that farmalloc
1592N/A * will return a usable pointer which doesn't have to be normalized.
1592N/A */
1592N/A if (bsize < 65520L) {
1592N/A buf = farmalloc(bsize);
1592N/A if (*(ush*)&buf != 0) return buf;
1592N/A } else {
1592N/A buf = farmalloc(bsize + 16L);
1592N/A }
1592N/A if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
1592N/A table[next_ptr].org_ptr = buf;
1592N/A
1592N/A /* Normalize the pointer to seg:0 */
1592N/A *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
1592N/A *(ush*)&buf = 0;
1592N/A table[next_ptr++].new_ptr = buf;
1592N/A return buf;
1592N/A}
1592N/A
1592N/Avoid zcfree (voidpf opaque, voidpf ptr)
1592N/A{
1592N/A int n;
1592N/A if (*(ush*)&ptr != 0) { /* object < 64K */
1592N/A farfree(ptr);
1592N/A return;
1592N/A }
1592N/A /* Find the original pointer */
1592N/A for (n = 0; n < next_ptr; n++) {
1592N/A if (ptr != table[n].new_ptr) continue;
1592N/A
1592N/A farfree(table[n].org_ptr);
1592N/A while (++n < next_ptr) {
1592N/A table[n-1] = table[n];
1592N/A }
1592N/A next_ptr--;
1592N/A return;
1592N/A }
1592N/A ptr = opaque; /* just to make some compilers happy */
1592N/A Assert(0, "zcfree: ptr not found");
1592N/A}
1592N/A
1592N/A#endif /* __TURBOC__ */
1592N/A
1592N/A
1592N/A#ifdef M_I86
1592N/A/* Microsoft C in 16-bit mode */
1592N/A
1592N/A# define MY_ZCALLOC
1592N/A
1592N/A#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
1592N/A# define _halloc halloc
1592N/A# define _hfree hfree
1592N/A#endif
1592N/A
1592N/Avoidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
1592N/A{
1592N/A if (opaque) opaque = 0; /* to make compiler happy */
1592N/A return _halloc((long)items, size);
1592N/A}
1592N/A
1592N/Avoid zcfree (voidpf opaque, voidpf ptr)
1592N/A{
1592N/A if (opaque) opaque = 0; /* to make compiler happy */
1592N/A _hfree(ptr);
1592N/A}
1592N/A
1592N/A#endif /* M_I86 */
1592N/A
1592N/A#endif /* SYS16BIT */
1592N/A
1592N/A
1592N/A#ifndef MY_ZCALLOC /* Any system without a special alloc function */
1592N/A
1592N/A#ifndef STDC
1592N/Aextern voidp malloc OF((uInt size));
1592N/Aextern voidp calloc OF((uInt items, uInt size));
1592N/Aextern void free OF((voidpf ptr));
1592N/A#endif
1592N/A
1592N/Avoidpf zcalloc (opaque, items, size)
1592N/A voidpf opaque;
1592N/A unsigned items;
1592N/A unsigned size;
1592N/A{
1592N/A if (opaque) items += size - size; /* make compiler happy */
1592N/A return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
1592N/A (voidpf)calloc(items, size);
1592N/A}
1592N/A
1592N/Avoid zcfree (opaque, ptr)
1592N/A voidpf opaque;
1592N/A voidpf ptr;
1592N/A{
1592N/A free(ptr);
1592N/A if (opaque) return; /* make compiler happy */
1592N/A}
1592N/A
1592N/A#endif /* MY_ZCALLOC */