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__) && !defined(__MEDIUM__)
63 /* Small and medium model are for now limited to near allocation with
64 * reduced MAX_WBITS and MAX_MEM_LEVEL
68 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
69 * and farmalloc(64K) returns a pointer with an offset of 8, so we
70 * must fix the pointer. Warning: the pointer must be put back to its
71 * original form in order to free it, use zcfree().
77 local int next_ptr = 0;
79 typedef struct ptr_table_s {
84 local ptr_table table[MAX_PTR];
85 /* This table is used to remember the original form of pointers
86 * to large buffers (64K). Such pointers are normalized with a zero offset.
87 * Since MSDOS is not a preemptive multitasking OS, this table is not
88 * protected from concurrent access. This hack doesn't work anyway on
89 * a protected system like OS/2. Use Microsoft C instead.
92 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
94 voidpf buf = opaque; /* just to make some compilers happy */
95 ulg bsize = (ulg)items*size;
98 buf = farmalloc(bsize);
99 if (*(ush*)&buf != 0) return buf;
101 buf = farmalloc(bsize + 16L);
103 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
104 table[next_ptr].org_ptr = buf;
106 /* Normalize the pointer to seg:0 */
107 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
109 table[next_ptr++].new_ptr = buf;
113 void zcfree (voidpf opaque, voidpf ptr)
116 if (*(ush*)&ptr != 0) { /* object < 64K */
120 /* Find the original pointer */
121 for (n = 0; n < next_ptr; n++) {
122 if (ptr != table[n].new_ptr) continue;
124 farfree(table[n].org_ptr);
125 while (++n < next_ptr) {
126 table[n-1] = table[n];
131 ptr = opaque; /* just to make some compilers happy */
132 z_error("zcfree: ptr not found");
134 #endif /* __TURBOC__ */
136 #if defined(M_I86SM)||defined(M_I86MM)||defined(M_I86CM)||defined(M_I86LM)
141 #if (!defined(_MSC_VER) || (_MSC_VER < 600))
142 # define _halloc halloc
143 # define _hfree hfree
146 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
148 if (opaque) opaque = 0; /* to make compiler happy */
149 return _halloc((long)items, size);
152 void zcfree (voidpf opaque, voidpf ptr)
154 if (opaque) opaque = 0; /* to make compiler happy */
161 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
164 extern voidp calloc OF((uInt items, uInt size));
165 extern void free OF((voidpf ptr));
168 voidpf zcalloc (opaque, items, size)
173 return (voidpf)calloc(items, size);
176 void zcfree (opaque, ptr)
183 #endif /* MY_ZCALLOC */