1 /* zutil.c -- target dependent utility functions for the compression library
2 * Copyright (C) 1995-1996 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
6 /* $Id: zutil.c,v 1.12 1996/01/30 21:59:29 me Exp $ */
12 struct internal_state {int dummy;}; /* for buggy compilers */
15 extern void exit OF((int));
18 const char *zlib_version = ZLIB_VERSION;
20 const char *z_errmsg[10] = {
21 "need dictionary", /* Z_NEED_DICT 2 */
22 "stream end", /* Z_STREAM_END 1 */
24 "file error", /* Z_ERRNO (-1) */
25 "stream error", /* Z_STREAM_ERROR (-2) */
26 "data error", /* Z_DATA_ERROR (-3) */
27 "insufficient memory", /* Z_MEM_ERROR (-4) */
28 "buffer error", /* Z_BUF_ERROR (-5) */
29 "incompatible version",/* Z_VERSION_ERROR (-6) */
36 fprintf(stderr, "%s\n", m);
42 void zmemcpy(dest, source, len)
49 *dest++ = *source++; /* ??? to be unrolled */
53 void zmemzero(dest, len)
59 *dest++ = 0; /* ??? to be unrolled */
65 #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
66 /* Small and medium model in Turbo C are for now limited to near allocation
67 * with reduced MAX_WBITS and MAX_MEM_LEVEL
71 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
72 * and farmalloc(64K) returns a pointer with an offset of 8, so we
73 * must fix the pointer. Warning: the pointer must be put back to its
74 * original form in order to free it, use zcfree().
80 local int next_ptr = 0;
82 typedef struct ptr_table_s {
87 local ptr_table table[MAX_PTR];
88 /* This table is used to remember the original form of pointers
89 * to large buffers (64K). Such pointers are normalized with a zero offset.
90 * Since MSDOS is not a preemptive multitasking OS, this table is not
91 * protected from concurrent access. This hack doesn't work anyway on
92 * a protected system like OS/2. Use Microsoft C instead.
95 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
97 voidpf buf = opaque; /* just to make some compilers happy */
98 ulg bsize = (ulg)items*size;
100 /* If we allocate less than 65520 bytes, we assume that farmalloc
101 * will return a usable pointer which doesn't have to be normalized.
103 if (bsize < 65520L) {
104 buf = farmalloc(bsize);
105 if (*(ush*)&buf != 0) return buf;
107 buf = farmalloc(bsize + 16L);
109 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
110 table[next_ptr].org_ptr = buf;
112 /* Normalize the pointer to seg:0 */
113 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
115 table[next_ptr++].new_ptr = buf;
119 void zcfree (voidpf opaque, voidpf ptr)
122 if (*(ush*)&ptr != 0) { /* object < 64K */
126 /* Find the original pointer */
127 for (n = 0; n < next_ptr; n++) {
128 if (ptr != table[n].new_ptr) continue;
130 farfree(table[n].org_ptr);
131 while (++n < next_ptr) {
132 table[n-1] = table[n];
137 ptr = opaque; /* just to make some compilers happy */
138 z_error("zcfree: ptr not found");
141 #endif /* __TURBOC__ */
144 #if defined(M_I86) && !(defined(__WATCOMC__) && defined(__386__))
149 #if (!defined(_MSC_VER) || (_MSC_VER < 600))
150 # define _halloc halloc
151 # define _hfree hfree
154 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
156 if (opaque) opaque = 0; /* to make compiler happy */
157 return _halloc((long)items, size);
160 void zcfree (voidpf opaque, voidpf ptr)
162 if (opaque) opaque = 0; /* to make compiler happy */
169 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
172 extern voidp calloc OF((uInt items, uInt size));
173 extern void free OF((voidpf ptr));
176 voidpf zcalloc (opaque, items, size)
181 if (opaque) items += size - size; /* make compiler happy */
182 return (voidpf)calloc(items, size);
185 void zcfree (opaque, ptr)
190 if (opaque) return; /* make compiler happy */
193 #endif /* MY_ZCALLOC */