2014-02-10 02:10:30 +01:00
|
|
|
/* zutil.c -- target dependent utility functions for the compression library
|
2017-01-16 17:58:34 +01:00
|
|
|
* Copyright (C) 1995-2017 Jean-loup Gailly
|
2014-02-10 02:10:30 +01:00
|
|
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* @(#) $Id$ */
|
|
|
|
|
|
|
|
#include "zutil.h"
|
|
|
|
#ifndef Z_SOLO
|
|
|
|
# include "gzguts.h"
|
|
|
|
#endif
|
|
|
|
|
2016-07-23 13:06:54 +02:00
|
|
|
z_const char * const z_errmsg[10] = {
|
2017-01-05 22:30:02 +01: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 *)""
|
|
|
|
};
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
|
2023-08-29 11:02:34 +02:00
|
|
|
const char * ZEXPORT zlibVersion(void) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return ZLIB_VERSION;
|
|
|
|
}
|
|
|
|
|
2023-08-29 11:02:34 +02:00
|
|
|
uLong ZEXPORT zlibCompileFlags(void) {
|
2014-02-10 02:10:30 +01:00
|
|
|
uLong flags;
|
|
|
|
|
|
|
|
flags = 0;
|
|
|
|
switch ((int)(sizeof(uInt))) {
|
|
|
|
case 2: break;
|
|
|
|
case 4: flags += 1; break;
|
|
|
|
case 8: flags += 2; break;
|
|
|
|
default: flags += 3;
|
|
|
|
}
|
|
|
|
switch ((int)(sizeof(uLong))) {
|
|
|
|
case 2: break;
|
|
|
|
case 4: flags += 1 << 2; break;
|
|
|
|
case 8: flags += 2 << 2; break;
|
|
|
|
default: flags += 3 << 2;
|
|
|
|
}
|
|
|
|
switch ((int)(sizeof(voidpf))) {
|
|
|
|
case 2: break;
|
|
|
|
case 4: flags += 1 << 4; break;
|
|
|
|
case 8: flags += 2 << 4; break;
|
|
|
|
default: flags += 3 << 4;
|
|
|
|
}
|
|
|
|
switch ((int)(sizeof(z_off_t))) {
|
|
|
|
case 2: break;
|
|
|
|
case 4: flags += 1 << 6; break;
|
|
|
|
case 8: flags += 2 << 6; break;
|
|
|
|
default: flags += 3 << 6;
|
|
|
|
}
|
2017-01-05 22:30:02 +01:00
|
|
|
#ifdef ZLIB_DEBUG
|
2014-02-10 02:10:30 +01:00
|
|
|
flags += 1 << 8;
|
|
|
|
#endif
|
2022-11-30 15:16:31 +01:00
|
|
|
/*
|
2014-02-10 02:10:30 +01:00
|
|
|
#if defined(ASMV) || defined(ASMINF)
|
|
|
|
flags += 1 << 9;
|
|
|
|
#endif
|
2022-11-30 15:16:31 +01:00
|
|
|
*/
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifdef ZLIB_WINAPI
|
|
|
|
flags += 1 << 10;
|
|
|
|
#endif
|
|
|
|
#ifdef BUILDFIXED
|
|
|
|
flags += 1 << 12;
|
|
|
|
#endif
|
|
|
|
#ifdef DYNAMIC_CRC_TABLE
|
|
|
|
flags += 1 << 13;
|
|
|
|
#endif
|
|
|
|
#ifdef NO_GZCOMPRESS
|
|
|
|
flags += 1L << 16;
|
|
|
|
#endif
|
|
|
|
#ifdef NO_GZIP
|
|
|
|
flags += 1L << 17;
|
|
|
|
#endif
|
|
|
|
#ifdef PKZIP_BUG_WORKAROUND
|
|
|
|
flags += 1L << 20;
|
|
|
|
#endif
|
|
|
|
#ifdef FASTEST
|
|
|
|
flags += 1L << 21;
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2017-01-05 22:30:02 +01:00
|
|
|
#ifdef ZLIB_DEBUG
|
|
|
|
#include <stdlib.h>
|
2014-02-10 02:10:30 +01:00
|
|
|
# ifndef verbose
|
|
|
|
# define verbose 0
|
|
|
|
# endif
|
|
|
|
int ZLIB_INTERNAL z_verbose = verbose;
|
|
|
|
|
2023-08-29 11:02:34 +02:00
|
|
|
void ZLIB_INTERNAL z_error(char *m) {
|
2014-02-10 02:10:30 +01:00
|
|
|
fprintf(stderr, "%s\n", m);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* exported to allow conversion of error code to string for compress() and
|
|
|
|
* uncompress()
|
|
|
|
*/
|
2023-08-29 11:02:34 +02:00
|
|
|
const char * ZEXPORT zError(int err) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return ERR_MSG(err);
|
|
|
|
}
|
|
|
|
|
2022-03-31 14:14:05 +02:00
|
|
|
#if defined(_WIN32_WCE) && _WIN32_WCE < 0x800
|
|
|
|
/* The older Microsoft C Run-Time Library for Windows CE doesn't have
|
2014-02-10 02:10:30 +01:00
|
|
|
* errno. We define it as a global variable to simplify porting.
|
|
|
|
* Its value is always 0 and should not be used.
|
|
|
|
*/
|
|
|
|
int errno = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_MEMCPY
|
|
|
|
|
2023-08-29 11:02:34 +02:00
|
|
|
void ZLIB_INTERNAL zmemcpy(Bytef* dest, const Bytef* source, uInt len) {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (len == 0) return;
|
|
|
|
do {
|
|
|
|
*dest++ = *source++; /* ??? to be unrolled */
|
|
|
|
} while (--len != 0);
|
|
|
|
}
|
|
|
|
|
2023-08-29 11:02:34 +02:00
|
|
|
int ZLIB_INTERNAL zmemcmp(const Bytef* s1, const Bytef* s2, uInt len) {
|
2014-02-10 02:10:30 +01:00
|
|
|
uInt j;
|
|
|
|
|
|
|
|
for (j = 0; j < len; j++) {
|
|
|
|
if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-29 11:02:34 +02:00
|
|
|
void ZLIB_INTERNAL zmemzero(Bytef* dest, uInt len) {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (len == 0) return;
|
|
|
|
do {
|
|
|
|
*dest++ = 0; /* ??? to be unrolled */
|
|
|
|
} while (--len != 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef Z_SOLO
|
|
|
|
|
|
|
|
#ifdef SYS16BIT
|
|
|
|
|
|
|
|
#ifdef __TURBOC__
|
|
|
|
/* Turbo C in 16-bit mode */
|
|
|
|
|
|
|
|
# define MY_ZCALLOC
|
|
|
|
|
|
|
|
/* 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 {
|
|
|
|
voidpf org_ptr;
|
|
|
|
voidpf new_ptr;
|
|
|
|
} 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.
|
|
|
|
*/
|
|
|
|
|
2023-08-29 11:02:34 +02:00
|
|
|
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) {
|
2017-01-05 22:30:02 +01:00
|
|
|
voidpf buf;
|
2014-02-10 02:10:30 +01:00
|
|
|
ulg bsize = (ulg)items*size;
|
|
|
|
|
2017-01-05 22:30:02 +01:00
|
|
|
(void)opaque;
|
|
|
|
|
2014-02-10 02:10:30 +01: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) {
|
|
|
|
buf = farmalloc(bsize);
|
|
|
|
if (*(ush*)&buf != 0) return buf;
|
|
|
|
} else {
|
|
|
|
buf = farmalloc(bsize + 16L);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-29 11:02:34 +02:00
|
|
|
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {
|
2014-02-10 02:10:30 +01:00
|
|
|
int n;
|
2017-01-05 22:30:02 +01:00
|
|
|
|
|
|
|
(void)opaque;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (*(ush*)&ptr != 0) { /* object < 64K */
|
|
|
|
farfree(ptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Find the original pointer */
|
|
|
|
for (n = 0; n < next_ptr; n++) {
|
|
|
|
if (ptr != table[n].new_ptr) continue;
|
|
|
|
|
|
|
|
farfree(table[n].org_ptr);
|
|
|
|
while (++n < next_ptr) {
|
|
|
|
table[n-1] = table[n];
|
|
|
|
}
|
|
|
|
next_ptr--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Assert(0, "zcfree: ptr not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __TURBOC__ */
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef M_I86
|
|
|
|
/* Microsoft C in 16-bit mode */
|
|
|
|
|
|
|
|
# define MY_ZCALLOC
|
|
|
|
|
|
|
|
#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
|
|
|
|
# define _halloc halloc
|
|
|
|
# define _hfree hfree
|
|
|
|
#endif
|
|
|
|
|
2023-08-29 11:02:34 +02:00
|
|
|
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size) {
|
2017-01-05 22:30:02 +01:00
|
|
|
(void)opaque;
|
2014-02-10 02:10:30 +01:00
|
|
|
return _halloc((long)items, size);
|
|
|
|
}
|
|
|
|
|
2023-08-29 11:02:34 +02:00
|
|
|
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {
|
2017-01-05 22:30:02 +01:00
|
|
|
(void)opaque;
|
2014-02-10 02:10:30 +01:00
|
|
|
_hfree(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* M_I86 */
|
|
|
|
|
|
|
|
#endif /* SYS16BIT */
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef MY_ZCALLOC /* Any system without a special alloc function */
|
|
|
|
|
|
|
|
#ifndef STDC
|
2023-08-29 11:02:34 +02:00
|
|
|
extern voidp malloc(uInt size);
|
|
|
|
extern voidp calloc(uInt items, uInt size);
|
|
|
|
extern void free(voidpf ptr);
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
|
2023-08-29 11:02:34 +02:00
|
|
|
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) {
|
2017-01-05 22:30:02 +01:00
|
|
|
(void)opaque;
|
2014-02-10 02:10:30 +01:00
|
|
|
return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
|
|
|
|
(voidpf)calloc(items, size);
|
|
|
|
}
|
|
|
|
|
2023-08-29 11:02:34 +02:00
|
|
|
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {
|
2017-01-05 22:30:02 +01:00
|
|
|
(void)opaque;
|
2014-02-10 02:10:30 +01:00
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MY_ZCALLOC */
|
|
|
|
|
|
|
|
#endif /* !Z_SOLO */
|