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 $ */
13 extern void exit __P((int));
16 char *zlib_version = ZLIB_VERSION;
19 "stream end", /* Z_STREAM_END 1 */
21 "file error", /* Z_ERRNO (-1) */
22 "stream error", /* Z_STREAM_ERROR (-2) */
23 "data error", /* Z_DATA_ERROR (-3) */
24 "insufficient memory", /* Z_MEM_ERROR (-4) */
25 "buffer error", /* Z_BUF_ERROR (-5) */
32 fprintf(stderr, "%s\n", m);
38 void zmemcpy(dest, source, len)
45 *dest++ = *source++; /* ??? to be unrolled */
49 void zmemzero(dest, len)
55 *dest++ = 0; /* ??? to be unrolled */
60 #if defined(__TURBOC__) && !defined(__SMALL__)
64 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
65 * and farmalloc(64K) returns a pointer with an offset of 8, so we
66 * must fix the pointer. Warning: the pointer must be put back to its
67 * original form in order to free it, use zcfree().
73 local int next_ptr = 0;
75 typedef struct ptr_table_s {
80 local ptr_table table[MAX_PTR];
81 /* This table is used to remember the original form of pointers
82 * to large buffers (64K). Such pointers are normalized with a zero offset.
83 * Since MSDOS is not a preemptive multitasking OS, this table is not
84 * protected from concurrent access. This hack doesn't work anyway on
85 * a protected system like OS/2. Use Microsoft C instead.
88 voidp zcalloc (voidp opaque, unsigned items, unsigned size)
90 voidp buf = opaque; /* just to make some compilers happy */
91 ulg bsize = (ulg)items*size;
94 buf = farmalloc(bsize);
95 if (*(ush*)&buf != 0) return buf;
97 buf = farmalloc(bsize + 16L);
99 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
100 table[next_ptr].org_ptr = buf;
102 /* Normalize the pointer to seg:0 */
103 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
105 table[next_ptr++].new_ptr = buf;
109 void zcfree (voidp opaque, voidp ptr)
112 if (*(ush*)&ptr != 0) { /* object < 64K */
116 /* Find the original pointer */
117 for (n = 0; n < next_ptr; n++) {
118 if (ptr != table[n].new_ptr) continue;
120 farfree(table[n].org_ptr);
121 while (++n < next_ptr) {
122 table[n-1] = table[n];
127 ptr = opaque; /* just to make some compilers happy */
128 z_error("zcfree: ptr not found");
130 #endif /* __TURBOC__ */
132 #if defined(M_I86CM) || defined(M_I86LM) /* MSC compact or large model */
136 #if (!defined(_MSC_VER) || (_MSC_VER < 600))
137 # define _halloc halloc
138 # define _hfree hfree
141 voidp zcalloc (voidp opaque, unsigned items, unsigned size)
143 if (opaque) opaque = 0; /* to make compiler happy */
144 return _halloc((long)items, size);
147 void zcfree (voidp opaque, voidp ptr)
149 if (opaque) opaque = 0; /* to make compiler happy */
153 #endif /* defined(M_I86CM) || defined(M_I86LM) */
156 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
159 extern voidp calloc __P((uInt items, uInt size));
160 extern void free __P((voidp ptr));
163 voidp zcalloc (opaque, items, size)
168 return calloc(items, size);
171 void zcfree (opaque, ptr)
178 #endif /* MY_ZCALLOC */