First public contribution.
1 /* zutil.c -- target dependent utility functions for the compression library
2 * Copyright (C) 1995-1998 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
10 struct internal_state {int dummy;}; /* for buggy compilers */
13 extern void exit OF((int));
16 const char *z_errmsg[10] = {
17 "need dictionary", /* Z_NEED_DICT 2 */
18 "stream end", /* Z_STREAM_END 1 */
20 "file error", /* Z_ERRNO (-1) */
21 "stream error", /* Z_STREAM_ERROR (-2) */
22 "data error", /* Z_DATA_ERROR (-3) */
23 "insufficient memory", /* Z_MEM_ERROR (-4) */
24 "buffer error", /* Z_BUF_ERROR (-5) */
25 "incompatible version",/* Z_VERSION_ERROR (-6) */
29 const char * ZEXPORT zlibVersion()
39 int z_verbose = verbose;
41 void z_error (char *m)
43 fprintf(stderr, "%s\n", m);
48 /* exported to allow conversion of error code to string for compress() and
51 const char * ZEXPORT zError(int err)
59 void zmemcpy(Bytef* dest, const Bytef* source, uInt len)
63 *dest++ = *source++; /* ??? to be unrolled */
67 int zmemcmp(const Bytef* s1, const Bytef* s2, uInt len)
71 for (j = 0; j < len; j++) {
72 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
77 void zmemzero(Bytef* dest, uInt len)
81 *dest++ = 0; /* ??? to be unrolled */
87 #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
88 /* Small and medium model in Turbo C are for now limited to near allocation
89 * with reduced MAX_WBITS and MAX_MEM_LEVEL
93 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
94 * and farmalloc(64K) returns a pointer with an offset of 8, so we
95 * must fix the pointer. Warning: the pointer must be put back to its
96 * original form in order to free it, use zcfree().
102 local int next_ptr = 0;
104 typedef struct ptr_table_s {
109 local ptr_table table[MAX_PTR];
110 /* This table is used to remember the original form of pointers
111 * to large buffers (64K). Such pointers are normalized with a zero offset.
112 * Since MSDOS is not a preemptive multitasking OS, this table is not
113 * protected from concurrent access. This hack doesn't work anyway on
114 * a protected system like OS/2. Use Microsoft C instead.
117 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
119 voidpf buf = opaque; /* just to make some compilers happy */
120 ulg bsize = (ulg)items*size;
122 /* If we allocate less than 65520 bytes, we assume that farmalloc
123 * will return a usable pointer which doesn't have to be normalized.
125 if (bsize < 65520L) {
126 buf = farmalloc(bsize);
127 if (*(ush*)&buf != 0) return buf;
129 buf = farmalloc(bsize + 16L);
131 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
132 table[next_ptr].org_ptr = buf;
134 /* Normalize the pointer to seg:0 */
135 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
137 table[next_ptr++].new_ptr = buf;
141 void zcfree (voidpf opaque, voidpf ptr)
144 if (*(ush*)&ptr != 0) { /* object < 64K */
148 /* Find the original pointer */
149 for (n = 0; n < next_ptr; n++) {
150 if (ptr != table[n].new_ptr) continue;
152 farfree(table[n].org_ptr);
153 while (++n < next_ptr) {
154 table[n-1] = table[n];
159 ptr = opaque; /* just to make some compilers happy */
160 Assert(0, "zcfree: ptr not found");
163 #endif /* __TURBOC__ */
166 #if defined(M_I86) && !defined(__32BIT__)
167 /* Microsoft C in 16-bit mode */
171 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
172 # define _halloc halloc
173 # define _hfree hfree
176 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
178 if (opaque) opaque = 0; /* to make compiler happy */
179 return _halloc((long)items, size);
182 void zcfree (voidpf opaque, voidpf ptr)
184 if (opaque) opaque = 0; /* to make compiler happy */
191 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
194 extern voidp calloc OF((uInt items, uInt size));
195 extern void free OF((voidpf ptr));
198 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
200 if (opaque) items += size - size; /* make compiler happy */
201 return (voidpf)calloc(items, size);
204 void zcfree (voidpf opaque,voidpf ptr)
207 if (opaque) return; /* make compiler happy */
210 #endif /* MY_ZCALLOC */