A massively spiffy yet delicately unobtrusive compression library. (grpc依赖)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
4.7 KiB

13 years ago
/* zutil.c -- target dependent utility functions for the compression library
13 years ago
* Copyright (C) 1995-1996 Jean-loup Gailly.
13 years ago
* For conditions of distribution and use, see copyright notice in zlib.h
*/
13 years ago
/* $Id: zutil.c,v 1.15 1996/05/23 17:11:36 me Exp $ */
13 years ago
#include <stdio.h>
#include "zutil.h"
13 years ago
struct internal_state {int dummy;}; /* for buggy compilers */
13 years ago
#ifndef STDC
13 years ago
extern void exit OF((int));
13 years ago
#endif
13 years ago
13 years ago
const char *z_errmsg[10] = {
"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) */
13 years ago
""};
13 years ago
char *zlibVersion()
{
return ZLIB_VERSION;
}
13 years ago
void z_error (m)
char *m;
{
fprintf(stderr, "%s\n", m);
exit(1);
}
#ifndef HAVE_MEMCPY
void zmemcpy(dest, source, len)
13 years ago
Bytef* dest;
Bytef* source;
13 years ago
uInt len;
{
if (len == 0) return;
do {
13 years ago
*dest++ = *source++; /* ??? to be unrolled */
13 years ago
} while (--len != 0);
}
void zmemzero(dest, len)
13 years ago
Bytef* dest;
13 years ago
uInt len;
{
if (len == 0) return;
do {
13 years ago
*dest++ = 0; /* ??? to be unrolled */
13 years ago
} while (--len != 0);
}
#endif
13 years ago
#ifdef __TURBOC__
13 years ago
#if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
/* Small and medium model in Turbo C are for now limited to near allocation
* with reduced MAX_WBITS and MAX_MEM_LEVEL
13 years ago
*/
13 years ago
# define MY_ZCALLOC
13 years ago
/* 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 {
13 years ago
voidpf org_ptr;
voidpf new_ptr;
13 years ago
} 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.
*/
13 years ago
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
13 years ago
{
13 years ago
voidpf buf = opaque; /* just to make some compilers happy */
13 years ago
ulg bsize = (ulg)items*size;
13 years ago
/* 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) {
13 years ago
buf = farmalloc(bsize);
if (*(ush*)&buf != 0) return buf;
13 years ago
} else {
13 years ago
buf = farmalloc(bsize + 16L);
13 years ago
}
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;
}
13 years ago
void zcfree (voidpf opaque, voidpf ptr)
13 years ago
{
int n;
if (*(ush*)&ptr != 0) { /* object < 64K */
13 years ago
farfree(ptr);
return;
13 years ago
}
/* Find the original pointer */
for (n = 0; n < next_ptr; n++) {
13 years ago
if (ptr != table[n].new_ptr) continue;
farfree(table[n].org_ptr);
while (++n < next_ptr) {
table[n-1] = table[n];
}
next_ptr--;
return;
13 years ago
}
13 years ago
ptr = opaque; /* just to make some compilers happy */
13 years ago
z_error("zcfree: ptr not found");
}
13 years ago
#endif
13 years ago
#endif /* __TURBOC__ */
13 years ago
13 years ago
#if defined(M_I86) && !(defined(__WATCOMC__) && defined(__386__))
/* Microsoft C */
13 years ago
13 years ago
# define MY_ZCALLOC
13 years ago
#if (!defined(_MSC_VER) || (_MSC_VER < 600))
# define _halloc halloc
# define _hfree hfree
#endif
13 years ago
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
13 years ago
{
13 years ago
if (opaque) opaque = 0; /* to make compiler happy */
13 years ago
return _halloc((long)items, size);
}
13 years ago
void zcfree (voidpf opaque, voidpf ptr)
13 years ago
{
13 years ago
if (opaque) opaque = 0; /* to make compiler happy */
13 years ago
_hfree(ptr);
13 years ago
}
13 years ago
#endif /* MSC */
13 years ago
13 years ago
#ifndef MY_ZCALLOC /* Any system without a special alloc function */
13 years ago
#ifndef STDC
13 years ago
extern voidp calloc OF((uInt items, uInt size));
extern void free OF((voidpf ptr));
13 years ago
#endif
13 years ago
13 years ago
voidpf zcalloc (opaque, items, size)
voidpf opaque;
13 years ago
unsigned items;
unsigned size;
{
13 years ago
if (opaque) items += size - size; /* make compiler happy */
13 years ago
return (voidpf)calloc(items, size);
13 years ago
}
void zcfree (opaque, ptr)
13 years ago
voidpf opaque;
voidpf ptr;
13 years ago
{
free(ptr);
13 years ago
if (opaque) return; /* make compiler happy */
13 years ago
}
13 years ago
#endif /* MY_ZCALLOC */