zlib/zutil.c

230 lines
5.3 KiB
C
Raw Normal View History

2011-09-10 05:36:31 +00:00
/* zutil.c -- target dependent utility functions for the compression library
2011-09-10 06:21:47 +00:00
* Copyright (C) 1995-2003 Jean-loup Gailly.
2011-09-10 05:36:31 +00:00
* For conditions of distribution and use, see copyright notice in zlib.h
*/
2011-09-10 06:17:33 +00:00
/* @(#) $Id$ */
2011-09-10 05:36:31 +00:00
#include "zutil.h"
2011-09-10 06:09:18 +00:00
struct internal_state {int dummy;}; /* for buggy compilers */
2011-09-10 06:11:37 +00:00
#ifndef STDC
2011-09-10 06:09:18 +00:00
extern void exit OF((int));
2011-09-10 06:07:35 +00:00
#endif
2011-09-10 05:52:17 +00:00
2011-09-10 06:21:47 +00:00
const char * const z_errmsg[10] = {
2011-09-10 06:14:39 +00:00
"need dictionary", /* Z_NEED_DICT 2 */
"stream end", /* Z_STREAM_END 1 */
"", /* Z_OK 0 */
"file error", /* Z_ERRNO (-1) */
"stream error", /* Z_STREAM_ERROR (-2) */
"data error", /* Z_DATA_ERROR (-3) */
"insufficient memory", /* Z_MEM_ERROR (-4) */
"buffer error", /* Z_BUF_ERROR (-5) */
"incompatible version",/* Z_VERSION_ERROR (-6) */
2011-09-10 05:36:31 +00:00
""};
2011-09-10 06:19:21 +00:00
const char * ZEXPORT zlibVersion()
2011-09-10 06:15:17 +00:00
{
return ZLIB_VERSION;
}
2011-09-10 06:17:02 +00:00
#ifdef DEBUG
2011-09-10 06:17:33 +00:00
# ifndef verbose
# define verbose 0
# endif
int z_verbose = verbose;
2011-09-10 05:36:31 +00:00
void z_error (m)
char *m;
{
fprintf(stderr, "%s\n", m);
exit(1);
}
2011-09-10 06:17:02 +00:00
#endif
2011-09-10 05:36:31 +00:00
2011-09-10 06:17:33 +00:00
/* exported to allow conversion of error code to string for compress() and
* uncompress()
*/
2011-09-10 06:19:21 +00:00
const char * ZEXPORT zError(err)
2011-09-10 06:17:33 +00:00
int err;
{
return ERR_MSG(err);
}
2011-09-10 05:36:31 +00:00
#ifndef HAVE_MEMCPY
void zmemcpy(dest, source, len)
2011-09-10 06:10:21 +00:00
Bytef* dest;
2011-09-10 06:20:29 +00:00
const Bytef* source;
2011-09-10 05:36:31 +00:00
uInt len;
{
if (len == 0) return;
do {
2011-09-10 06:08:07 +00:00
*dest++ = *source++; /* ??? to be unrolled */
2011-09-10 05:36:31 +00:00
} while (--len != 0);
}
2011-09-10 06:17:02 +00:00
int zmemcmp(s1, s2, len)
2011-09-10 06:20:29 +00:00
const Bytef* s1;
const Bytef* s2;
2011-09-10 06:17:02 +00: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 05:36:31 +00:00
void zmemzero(dest, len)
2011-09-10 06:10:21 +00:00
Bytef* dest;
2011-09-10 05:36:31 +00:00
uInt len;
{
if (len == 0) return;
do {
2011-09-10 06:08:07 +00:00
*dest++ = 0; /* ??? to be unrolled */
2011-09-10 05:36:31 +00:00
} while (--len != 0);
}
#endif
2011-09-10 06:11:37 +00:00
#ifdef __TURBOC__
2011-09-10 06:14:39 +00:00
#if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
2011-09-10 06:21:47 +00:00
#if !defined(__linux)
2011-09-10 06:14:39 +00:00
/* Small and medium model in Turbo C are for now limited to near allocation
* with reduced MAX_WBITS and MAX_MEM_LEVEL
2011-09-10 06:10:21 +00:00
*/
2011-09-10 06:07:35 +00:00
# define MY_ZCALLOC
2011-09-10 05:36:31 +00: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 06:10:21 +00:00
voidpf org_ptr;
voidpf new_ptr;
2011-09-10 05:36:31 +00: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 06:10:21 +00:00
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
2011-09-10 05:36:31 +00:00
{
2011-09-10 06:10:21 +00:00
voidpf buf = opaque; /* just to make some compilers happy */
2011-09-10 05:36:31 +00:00
ulg bsize = (ulg)items*size;
2011-09-10 06:11:37 +00: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 06:08:07 +00:00
buf = farmalloc(bsize);
if (*(ush*)&buf != 0) return buf;
2011-09-10 05:36:31 +00:00
} else {
2011-09-10 06:08:07 +00:00
buf = farmalloc(bsize + 16L);
2011-09-10 05:36:31 +00: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 06:10:21 +00:00
void zcfree (voidpf opaque, voidpf ptr)
2011-09-10 05:36:31 +00:00
{
int n;
if (*(ush*)&ptr != 0) { /* object < 64K */
2011-09-10 06:08:07 +00:00
farfree(ptr);
return;
2011-09-10 05:36:31 +00:00
}
/* Find the original pointer */
for (n = 0; n < next_ptr; n++) {
2011-09-10 06:08:07 +00: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 05:36:31 +00:00
}
2011-09-10 06:03:14 +00:00
ptr = opaque; /* just to make some compilers happy */
2011-09-10 06:17:02 +00:00
Assert(0, "zcfree: ptr not found");
2011-09-10 05:36:31 +00:00
}
2011-09-10 06:11:37 +00:00
#endif
2011-09-10 06:21:47 +00:00
#endif
2011-09-10 06:07:35 +00:00
#endif /* __TURBOC__ */
2011-09-10 06:11:37 +00:00
2011-09-10 06:17:02 +00:00
#if defined(M_I86) && !defined(__32BIT__)
/* Microsoft C in 16-bit mode */
2011-09-10 05:36:31 +00:00
2011-09-10 06:07:35 +00:00
# define MY_ZCALLOC
2011-09-10 05:36:31 +00:00
2011-09-10 06:20:29 +00:00
#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
2011-09-10 05:36:31 +00:00
# define _halloc halloc
# define _hfree hfree
#endif
2011-09-10 06:10:21 +00:00
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
2011-09-10 05:36:31 +00:00
{
2011-09-10 06:03:14 +00:00
if (opaque) opaque = 0; /* to make compiler happy */
2011-09-10 05:36:31 +00:00
return _halloc((long)items, size);
}
2011-09-10 06:10:21 +00:00
void zcfree (voidpf opaque, voidpf ptr)
2011-09-10 05:36:31 +00:00
{
2011-09-10 06:03:14 +00:00
if (opaque) opaque = 0; /* to make compiler happy */
2011-09-10 05:52:17 +00:00
_hfree(ptr);
2011-09-10 05:36:31 +00:00
}
2011-09-10 06:09:18 +00:00
#endif /* MSC */
2011-09-10 05:36:31 +00:00
2011-09-10 06:07:35 +00:00
#ifndef MY_ZCALLOC /* Any system without a special alloc function */
2011-09-10 06:11:37 +00:00
#ifndef STDC
2011-09-10 06:21:47 +00:00
extern voidp malloc OF((uInt size));
2011-09-10 06:10:21 +00:00
extern voidp calloc OF((uInt items, uInt size));
extern void free OF((voidpf ptr));
2011-09-10 06:07:35 +00:00
#endif
2011-09-10 05:36:31 +00:00
2011-09-10 06:10:21 +00:00
voidpf zcalloc (opaque, items, size)
voidpf opaque;
2011-09-10 05:36:31 +00:00
unsigned items;
unsigned size;
{
2011-09-10 06:14:39 +00:00
if (opaque) items += size - size; /* make compiler happy */
2011-09-10 06:21:47 +00:00
return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
(voidpf)calloc(items, size);
2011-09-10 05:36:31 +00:00
}
void zcfree (opaque, ptr)
2011-09-10 06:10:21 +00:00
voidpf opaque;
voidpf ptr;
2011-09-10 05:36:31 +00:00
{
free(ptr);
2011-09-10 06:14:39 +00:00
if (opaque) return; /* make compiler happy */
2011-09-10 05:36:31 +00:00
}
2011-09-10 06:07:35 +00:00
#endif /* MY_ZCALLOC */