zlib/zutil.c

326 lines
7.2 KiB
C
Raw Normal View History

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