1 /* zutil.c -- target dependent utility functions for the compression library
2 * Copyright (C) 1995 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
6 /* $Id: zutil.c,v 1.8 1995/05/03 17:27:12 jloup Exp $ */
12 struct internal_state {int dummy;}; /* for buggy compilers */
15 extern void exit OF((int));
18 char *zlib_version = ZLIB_VERSION;
21 "stream end", /* Z_STREAM_END 1 */
23 "file error", /* Z_ERRNO (-1) */
24 "stream error", /* Z_STREAM_ERROR (-2) */
25 "data error", /* Z_DATA_ERROR (-3) */
26 "insufficient memory", /* Z_MEM_ERROR (-4) */
27 "buffer error", /* Z_BUF_ERROR (-5) */
34 fprintf(stderr, "%s\n", m);
40 void zmemcpy(dest, source, len)
47 *dest++ = *source++; /* ??? to be unrolled */
51 void zmemzero(dest, len)
57 *dest++ = 0; /* ??? to be unrolled */
62 #if defined(__TURBOC__) && !defined(__SMALL__)
66 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
67 * and farmalloc(64K) returns a pointer with an offset of 8, so we
68 * must fix the pointer. Warning: the pointer must be put back to its
69 * original form in order to free it, use zcfree().
75 local int next_ptr = 0;
77 typedef struct ptr_table_s {
82 local ptr_table table[MAX_PTR];
83 /* This table is used to remember the original form of pointers
84 * to large buffers (64K). Such pointers are normalized with a zero offset.
85 * Since MSDOS is not a preemptive multitasking OS, this table is not
86 * protected from concurrent access. This hack doesn't work anyway on
87 * a protected system like OS/2. Use Microsoft C instead.
90 voidp zcalloc (voidp opaque, unsigned items, unsigned size)
92 voidp buf = opaque; /* just to make some compilers happy */
93 ulg bsize = (ulg)items*size;
96 buf = farmalloc(bsize);
97 if (*(ush*)&buf != 0) return buf;
99 buf = farmalloc(bsize + 16L);
101 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
102 table[next_ptr].org_ptr = buf;
104 /* Normalize the pointer to seg:0 */
105 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
107 table[next_ptr++].new_ptr = buf;
111 void zcfree (voidp opaque, voidp ptr)
114 if (*(ush*)&ptr != 0) { /* object < 64K */
118 /* Find the original pointer */
119 for (n = 0; n < next_ptr; n++) {
120 if (ptr != table[n].new_ptr) continue;
122 farfree(table[n].org_ptr);
123 while (++n < next_ptr) {
124 table[n-1] = table[n];
129 ptr = opaque; /* just to make some compilers happy */
130 z_error("zcfree: ptr not found");
132 #endif /* __TURBOC__ */
134 #if defined(MSDOS) && !defined(__TURBOC__) /* MSC */
138 #if (!defined(_MSC_VER) || (_MSC_VER < 600))
139 # define _halloc halloc
140 # define _hfree hfree
143 voidp zcalloc (voidp opaque, unsigned items, unsigned size)
145 if (opaque) opaque = 0; /* to make compiler happy */
146 return _halloc((long)items, size);
149 void zcfree (voidp opaque, voidp ptr)
151 if (opaque) opaque = 0; /* to make compiler happy */
158 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
161 extern voidp calloc OF((uInt items, uInt size));
162 extern void free OF((voidp ptr));
165 voidp zcalloc (opaque, items, size)
170 return calloc(items, size);
173 void zcfree (opaque, ptr)
180 #endif /* MY_ZCALLOC */