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.5 1995/04/14 21:30:23 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(USE_CALLOC)
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)
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 z_error("zcfree: ptr not found");
129 #if (!defined(_MSC_VER) || (_MSC_VER < 600))
130 # define _halloc halloc
131 # define _hfree hfree
134 voidp zcalloc (voidp opaque, unsigned items, unsigned size)
136 return _halloc((long)items, size);
139 void zcfree (voidp opaque, voidp ptr)
144 # endif /* __TURBOC__ ? */
148 extern voidp calloc __P((uInt items, uInt size));
149 extern void free __P((voidp ptr));
151 voidp zcalloc (opaque, items, size)
156 return calloc(items, size);
159 void zcfree (opaque, ptr)