2002-10-31 23:19:27 +01:00
|
|
|
/* zutil.c -- target dependent utility functions for the compression library
|
|
|
|
* Copyright (C) 1995-2002 Jean-loup Gailly.
|
|
|
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* @(#) $Id$ */
|
|
|
|
|
|
|
|
#include "zutil.h"
|
|
|
|
|
|
|
|
#ifndef STDC
|
|
|
|
extern void exit OF((int));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef HAVE_MEMCPY
|
|
|
|
|
|
|
|
void zmemcpy(dest, source, len)
|
|
|
|
Bytef* dest;
|
|
|
|
const Bytef* source;
|
|
|
|
uInt len;
|
|
|
|
{
|
|
|
|
if (len == 0) return;
|
|
|
|
do {
|
|
|
|
*dest++ = *source++; /* ??? to be unrolled */
|
|
|
|
} while (--len != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmemcmp(s1, s2, len)
|
|
|
|
const Bytef* s1;
|
|
|
|
const Bytef* s2;
|
|
|
|
uInt len;
|
|
|
|
{
|
|
|
|
uInt j;
|
|
|
|
|
|
|
|
for (j = 0; j < len; j++) {
|
|
|
|
if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmemzero(dest, len)
|
|
|
|
Bytef* dest;
|
|
|
|
uInt len;
|
|
|
|
{
|
|
|
|
if (len == 0) return;
|
|
|
|
do {
|
|
|
|
*dest++ = 0; /* ??? to be unrolled */
|
|
|
|
} while (--len != 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-07-31 17:30:11 +02:00
|
|
|
#if defined( MSDOS ) && defined( __TURBOC__ ) && !defined( MY_ZCALLOC )
|
2002-10-31 23:19:27 +01:00
|
|
|
#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
|
|
|
|
*/
|
|
|
|
# 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
|
|
|
|
{
|
|
|
|
voidpf buf = opaque; /* just to make some compilers happy */
|
|
|
|
ulg bsize = (ulg)items*size;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void zcfree (voidpf opaque, voidpf ptr)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
ptr = opaque; /* just to make some compilers happy */
|
|
|
|
Assert(0, "zcfree: ptr not found");
|
|
|
|
}
|
|
|
|
#endif
|
2009-07-31 17:30:11 +02:00
|
|
|
#endif /* MSDOS && __TURBOC__ */
|
2002-10-31 23:19:27 +01:00
|
|
|
|
|
|
|
|
2009-07-31 17:30:11 +02:00
|
|
|
#if defined(M_I86) && !defined(__32BIT__) && !defined( MY_ZCALLOC )
|
2002-10-31 23:19:27 +01:00
|
|
|
/* Microsoft C in 16-bit mode */
|
|
|
|
|
|
|
|
# define MY_ZCALLOC
|
|
|
|
|
|
|
|
#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
|
|
|
|
# define _halloc halloc
|
|
|
|
# define _hfree hfree
|
|
|
|
#endif
|
|
|
|
|
|
|
|
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
|
|
|
|
{
|
|
|
|
if (opaque) opaque = 0; /* to make compiler happy */
|
|
|
|
return _halloc((long)items, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void zcfree (voidpf opaque, voidpf ptr)
|
|
|
|
{
|
|
|
|
if (opaque) opaque = 0; /* to make compiler happy */
|
|
|
|
_hfree(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MSC */
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef MY_ZCALLOC /* Any system without a special alloc function */
|
|
|
|
|
|
|
|
#ifndef STDC
|
Further C library abstraction. Based on a patch from
msn2@bidyut.com.
* include/freetype/config/ftstdlib.h (FT_CHAR_BIT, FT_FILE,
ft_fopen, ft_fclose, ft_fseek, ft_ftell, ft_fread, ft_smalloc,
ft_scalloc, ft_srealloc, ft_sfree, ft_labs): New wrapper macros for
C library functions. Update all users accordingly (and catch some
other places where the C library function was used instead of the
wrapper functions).
* src/base/ftsystem.c: Don't include stdio.h and stdlib.h.
* src/gzip/zutil.h [MSDOS && !(__TURBOC__ || __BORLANDC__)]: Don't
include malloc.h.
2006-04-29 09:31:16 +02:00
|
|
|
extern voidp ft_scalloc OF((uInt items, uInt size));
|
|
|
|
extern void ft_sfree OF((voidpf ptr));
|
2002-10-31 23:19:27 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
voidpf zcalloc (opaque, items, size)
|
|
|
|
voidpf opaque;
|
|
|
|
unsigned items;
|
|
|
|
unsigned size;
|
|
|
|
{
|
|
|
|
if (opaque) items += size - size; /* make compiler happy */
|
Further C library abstraction. Based on a patch from
msn2@bidyut.com.
* include/freetype/config/ftstdlib.h (FT_CHAR_BIT, FT_FILE,
ft_fopen, ft_fclose, ft_fseek, ft_ftell, ft_fread, ft_smalloc,
ft_scalloc, ft_srealloc, ft_sfree, ft_labs): New wrapper macros for
C library functions. Update all users accordingly (and catch some
other places where the C library function was used instead of the
wrapper functions).
* src/base/ftsystem.c: Don't include stdio.h and stdlib.h.
* src/gzip/zutil.h [MSDOS && !(__TURBOC__ || __BORLANDC__)]: Don't
include malloc.h.
2006-04-29 09:31:16 +02:00
|
|
|
return (voidpf)ft_scalloc(items, size);
|
2002-10-31 23:19:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void zcfree (opaque, ptr)
|
|
|
|
voidpf opaque;
|
|
|
|
voidpf ptr;
|
|
|
|
{
|
Further C library abstraction. Based on a patch from
msn2@bidyut.com.
* include/freetype/config/ftstdlib.h (FT_CHAR_BIT, FT_FILE,
ft_fopen, ft_fclose, ft_fseek, ft_ftell, ft_fread, ft_smalloc,
ft_scalloc, ft_srealloc, ft_sfree, ft_labs): New wrapper macros for
C library functions. Update all users accordingly (and catch some
other places where the C library function was used instead of the
wrapper functions).
* src/base/ftsystem.c: Don't include stdio.h and stdlib.h.
* src/gzip/zutil.h [MSDOS && !(__TURBOC__ || __BORLANDC__)]: Don't
include malloc.h.
2006-04-29 09:31:16 +02:00
|
|
|
ft_sfree(ptr);
|
2002-10-31 23:19:27 +01:00
|
|
|
if (opaque) return; /* make compiler happy */
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MY_ZCALLOC */
|