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.6 1995/04/29 14:54:02 jloup Exp $ */
12 extern void exit __P((int));
14 char *zlib_version = ZLIB_VERSION;
17 "stream end", /* Z_STREAM_END 1 */
19 "file error", /* Z_ERRNO (-1) */
20 "stream error", /* Z_STREAM_ERROR (-2) */
21 "data error", /* Z_DATA_ERROR (-3) */
22 "insufficient memory", /* Z_MEM_ERROR (-4) */
23 "buffer error", /* Z_BUF_ERROR (-5) */
30 fprintf(stderr, "%s\n", m);
36 void zmemcpy(dest, source, len)
43 *dest++ = *source++; /* ??? to be unrolled */
47 void zmemzero(dest, len)
53 *dest++ = 0; /* ??? to be unrolled */
58 #if defined(MSDOS) && !defined(__SMALL__) && !defined(M_I86SM)
61 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
62 * and farmalloc(64K) returns a pointer with an offset of 8, so we
63 * must fix the pointer. Warning: the pointer must be put back to its
64 * original form in order to free it, use zcfree().
70 local int next_ptr = 0;
72 typedef struct ptr_table_s {
77 local ptr_table table[MAX_PTR];
78 /* This table is used to remember the original form of pointers
79 * to large buffers (64K). Such pointers are normalized with a zero offset.
80 * Since MSDOS is not a preemptive multitasking OS, this table is not
81 * protected from concurrent access. This hack doesn't work anyway on
82 * a protected system like OS/2. Use Microsoft C instead.
85 voidp zcalloc (voidp opaque, unsigned items, unsigned size)
87 voidp buf = opaque; /* just to make some compilers happy */
88 ulg bsize = (ulg)items*size;
91 buf = farmalloc(bsize);
92 if (*(ush*)&buf != 0) return buf;
94 buf = farmalloc(bsize + 16L);
96 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
97 table[next_ptr].org_ptr = buf;
99 /* Normalize the pointer to seg:0 */
100 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
102 table[next_ptr++].new_ptr = buf;
106 void zcfree (voidp opaque, voidp ptr)
109 if (*(ush*)&ptr != 0) { /* object < 64K */
113 /* Find the original pointer */
114 for (n = 0; n < next_ptr; n++) {
115 if (ptr != table[n].new_ptr) continue;
117 farfree(table[n].org_ptr);
118 while (++n < next_ptr) {
119 table[n-1] = table[n];
124 ptr = opaque; /* just to make some compilers happy */
125 z_error("zcfree: ptr not found");
130 #if (!defined(_MSC_VER) || (_MSC_VER < 600))
131 # define _halloc halloc
132 # define _hfree hfree
135 voidp zcalloc (voidp opaque, unsigned items, unsigned size)
137 if (opaque) opaque = 0; /* to make compiler happy */
138 return _halloc((long)items, size);
141 void zcfree (voidp opaque, voidp ptr)
143 if (opaque) opaque = 0; /* to make compiler happy */
147 # endif /* __TURBOC__ ? */
151 extern voidp calloc __P((uInt items, uInt size));
152 extern void free __P((voidp ptr));
154 voidp zcalloc (opaque, items, size)
159 return calloc(items, size);
162 void zcfree (opaque, ptr)