1 /* zutil.c -- target dependent utility functions for the compression library
2 * Copyright (C) 1995-2002 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6 /* @(#) $Id$ */
7
8 #include "zutil.h"
9
10 #ifndef STDC
11 extern void exit OF((int));
12 #endif
13
14
15 #ifndef HAVE_MEMCPY
16
zmemcpy(dest,source,len)17 void zmemcpy(dest, source, len)
18 Bytef* dest;
19 const Bytef* source;
20 uInt len;
21 {
22 if (len == 0) return;
23 do {
24 *dest++ = *source++; /* ??? to be unrolled */
25 } while (--len != 0);
26 }
27
zmemcmp(s1,s2,len)28 int zmemcmp(s1, s2, len)
29 const Bytef* s1;
30 const Bytef* s2;
31 uInt len;
32 {
33 uInt j;
34
35 for (j = 0; j < len; j++) {
36 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
37 }
38 return 0;
39 }
40
zmemzero(dest,len)41 void zmemzero(dest, len)
42 Bytef* dest;
43 uInt len;
44 {
45 if (len == 0) return;
46 do {
47 *dest++ = 0; /* ??? to be unrolled */
48 } while (--len != 0);
49 }
50 #endif
51
52 #if defined( MSDOS ) && defined( __TURBOC__ ) && !defined( MY_ZCALLOC )
53 #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
54 /* Small and medium model in Turbo C are for now limited to near allocation
55 * with reduced MAX_WBITS and MAX_MEM_LEVEL
56 */
57 # define MY_ZCALLOC
58
59 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
60 * and farmalloc(64K) returns a pointer with an offset of 8, so we
61 * must fix the pointer. Warning: the pointer must be put back to its
62 * original form in order to free it, use zcfree().
63 */
64
65 #define MAX_PTR 10
66 /* 10*64K = 640K */
67
68 local int next_ptr = 0;
69
70 typedef struct ptr_table_s {
71 voidpf org_ptr;
72 voidpf new_ptr;
73 } ptr_table;
74
75 local ptr_table table[MAX_PTR];
76 /* This table is used to remember the original form of pointers
77 * to large buffers (64K). Such pointers are normalized with a zero offset.
78 * Since MSDOS is not a preemptive multitasking OS, this table is not
79 * protected from concurrent access. This hack doesn't work anyway on
80 * a protected system like OS/2. Use Microsoft C instead.
81 */
82
zcalloc(voidpf opaque,unsigned items,unsigned size)83 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
84 {
85 voidpf buf = opaque; /* just to make some compilers happy */
86 ulg bsize = (ulg)items*size;
87
88 /* If we allocate less than 65520 bytes, we assume that farmalloc
89 * will return a usable pointer which doesn't have to be normalized.
90 */
91 if (bsize < 65520L) {
92 buf = farmalloc(bsize);
93 if (*(ush*)&buf != 0) return buf;
94 } else {
95 buf = farmalloc(bsize + 16L);
96 }
97 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
98 table[next_ptr].org_ptr = buf;
99
100 /* Normalize the pointer to seg:0 */
101 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
102 *(ush*)&buf = 0;
103 table[next_ptr++].new_ptr = buf;
104 return buf;
105 }
106
zcfree(voidpf opaque,voidpf ptr)107 void zcfree (voidpf opaque, voidpf ptr)
108 {
109 int n;
110 if (*(ush*)&ptr != 0) { /* object < 64K */
111 farfree(ptr);
112 return;
113 }
114 /* Find the original pointer */
115 for (n = 0; n < next_ptr; n++) {
116 if (ptr != table[n].new_ptr) continue;
117
118 farfree(table[n].org_ptr);
119 while (++n < next_ptr) {
120 table[n-1] = table[n];
121 }
122 next_ptr--;
123 return;
124 }
125 ptr = opaque; /* just to make some compilers happy */
126 Assert(0, "zcfree: ptr not found");
127 }
128 #endif
129 #endif /* MSDOS && __TURBOC__ */
130
131
132 #if defined(M_I86) && !defined(__32BIT__) && !defined( MY_ZCALLOC )
133 /* Microsoft C in 16-bit mode */
134
135 # define MY_ZCALLOC
136
137 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
138 # define _halloc halloc
139 # define _hfree hfree
140 #endif
141
zcalloc(voidpf opaque,unsigned items,unsigned size)142 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
143 {
144 if (opaque) opaque = 0; /* to make compiler happy */
145 return _halloc((long)items, size);
146 }
147
zcfree(voidpf opaque,voidpf ptr)148 void zcfree (voidpf opaque, voidpf ptr)
149 {
150 if (opaque) opaque = 0; /* to make compiler happy */
151 _hfree(ptr);
152 }
153
154 #endif /* MSC */
155
156
157 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
158
159 #ifndef STDC
160 extern voidp ft_scalloc OF((uInt items, uInt size));
161 extern void ft_sfree OF((voidpf ptr));
162 #endif
163
zcalloc(opaque,items,size)164 voidpf zcalloc (opaque, items, size)
165 voidpf opaque;
166 unsigned items;
167 unsigned size;
168 {
169 if (opaque) items += size - size; /* make compiler happy */
170 return (voidpf)ft_scalloc(items, size);
171 }
172
zcfree(opaque,ptr)173 void zcfree (opaque, ptr)
174 voidpf opaque;
175 voidpf ptr;
176 {
177 ft_sfree(ptr);
178 if (opaque) return; /* make compiler happy */
179 }
180
181 #endif /* MY_ZCALLOC */
182