annotate lib/zlib/zutil.c @ 483:98f0d3c19de2

Merge
author Nomad
date Tue, 26 Feb 2013 20:30:26 +0200
parents 9c0607679772
children
rev   line source
0
Ritor1
parents:
diff changeset
1 /* zutil.c -- target dependent utility functions for the compression library
Ritor1
parents:
diff changeset
2 * Copyright (C) 1995-1998 Jean-loup Gailly.
Ritor1
parents:
diff changeset
3 * For conditions of distribution and use, see copyright notice in zlib.h
Ritor1
parents:
diff changeset
4 */
Ritor1
parents:
diff changeset
5
Ritor1
parents:
diff changeset
6 /* @(#) $Id$ */
Ritor1
parents:
diff changeset
7
Ritor1
parents:
diff changeset
8 #include "zutil.h"
Ritor1
parents:
diff changeset
9
Ritor1
parents:
diff changeset
10 struct internal_state {int dummy;}; /* for buggy compilers */
Ritor1
parents:
diff changeset
11
Ritor1
parents:
diff changeset
12 #ifndef STDC
Ritor1
parents:
diff changeset
13 extern void exit OF((int));
Ritor1
parents:
diff changeset
14 #endif
Ritor1
parents:
diff changeset
15
Ritor1
parents:
diff changeset
16 const char *z_errmsg[10] = {
Ritor1
parents:
diff changeset
17 "need dictionary", /* Z_NEED_DICT 2 */
Ritor1
parents:
diff changeset
18 "stream end", /* Z_STREAM_END 1 */
Ritor1
parents:
diff changeset
19 "", /* Z_OK 0 */
Ritor1
parents:
diff changeset
20 "file error", /* Z_ERRNO (-1) */
Ritor1
parents:
diff changeset
21 "stream error", /* Z_STREAM_ERROR (-2) */
Ritor1
parents:
diff changeset
22 "data error", /* Z_DATA_ERROR (-3) */
Ritor1
parents:
diff changeset
23 "insufficient memory", /* Z_MEM_ERROR (-4) */
Ritor1
parents:
diff changeset
24 "buffer error", /* Z_BUF_ERROR (-5) */
Ritor1
parents:
diff changeset
25 "incompatible version",/* Z_VERSION_ERROR (-6) */
Ritor1
parents:
diff changeset
26 ""};
Ritor1
parents:
diff changeset
27
Ritor1
parents:
diff changeset
28
Ritor1
parents:
diff changeset
29 const char * ZEXPORT zlibVersion()
Ritor1
parents:
diff changeset
30 {
Ritor1
parents:
diff changeset
31 return ZLIB_VERSION;
Ritor1
parents:
diff changeset
32 }
Ritor1
parents:
diff changeset
33
Ritor1
parents:
diff changeset
34 #ifdef DEBUG
Ritor1
parents:
diff changeset
35
Ritor1
parents:
diff changeset
36 # ifndef verbose
Ritor1
parents:
diff changeset
37 # define verbose 0
Ritor1
parents:
diff changeset
38 # endif
Ritor1
parents:
diff changeset
39 int z_verbose = verbose;
Ritor1
parents:
diff changeset
40
Ritor1
parents:
diff changeset
41 void z_error (m)
Ritor1
parents:
diff changeset
42 char *m;
Ritor1
parents:
diff changeset
43 {
Ritor1
parents:
diff changeset
44 fprintf(stderr, "%s\n", m);
Ritor1
parents:
diff changeset
45 exit(1);
Ritor1
parents:
diff changeset
46 }
Ritor1
parents:
diff changeset
47 #endif
Ritor1
parents:
diff changeset
48
Ritor1
parents:
diff changeset
49 /* exported to allow conversion of error code to string for compress() and
Ritor1
parents:
diff changeset
50 * uncompress()
Ritor1
parents:
diff changeset
51 */
Ritor1
parents:
diff changeset
52 const char * ZEXPORT zError(err)
Ritor1
parents:
diff changeset
53 int err;
Ritor1
parents:
diff changeset
54 {
Ritor1
parents:
diff changeset
55 return ERR_MSG(err);
Ritor1
parents:
diff changeset
56 }
Ritor1
parents:
diff changeset
57
Ritor1
parents:
diff changeset
58
Ritor1
parents:
diff changeset
59 #ifndef HAVE_MEMCPY
Ritor1
parents:
diff changeset
60
Ritor1
parents:
diff changeset
61 void zmemcpy(dest, source, len)
Ritor1
parents:
diff changeset
62 Bytef* dest;
Ritor1
parents:
diff changeset
63 const Bytef* source;
Ritor1
parents:
diff changeset
64 uInt len;
Ritor1
parents:
diff changeset
65 {
Ritor1
parents:
diff changeset
66 if (len == 0) return;
Ritor1
parents:
diff changeset
67 do {
Ritor1
parents:
diff changeset
68 *dest++ = *source++; /* ??? to be unrolled */
Ritor1
parents:
diff changeset
69 } while (--len != 0);
Ritor1
parents:
diff changeset
70 }
Ritor1
parents:
diff changeset
71
Ritor1
parents:
diff changeset
72 int zmemcmp(s1, s2, len)
Ritor1
parents:
diff changeset
73 const Bytef* s1;
Ritor1
parents:
diff changeset
74 const Bytef* s2;
Ritor1
parents:
diff changeset
75 uInt len;
Ritor1
parents:
diff changeset
76 {
Ritor1
parents:
diff changeset
77 uInt j;
Ritor1
parents:
diff changeset
78
Ritor1
parents:
diff changeset
79 for (j = 0; j < len; j++) {
Ritor1
parents:
diff changeset
80 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
Ritor1
parents:
diff changeset
81 }
Ritor1
parents:
diff changeset
82 return 0;
Ritor1
parents:
diff changeset
83 }
Ritor1
parents:
diff changeset
84
Ritor1
parents:
diff changeset
85 void zmemzero(dest, len)
Ritor1
parents:
diff changeset
86 Bytef* dest;
Ritor1
parents:
diff changeset
87 uInt len;
Ritor1
parents:
diff changeset
88 {
Ritor1
parents:
diff changeset
89 if (len == 0) return;
Ritor1
parents:
diff changeset
90 do {
Ritor1
parents:
diff changeset
91 *dest++ = 0; /* ??? to be unrolled */
Ritor1
parents:
diff changeset
92 } while (--len != 0);
Ritor1
parents:
diff changeset
93 }
Ritor1
parents:
diff changeset
94 #endif
Ritor1
parents:
diff changeset
95
Ritor1
parents:
diff changeset
96 #ifdef __TURBOC__
Ritor1
parents:
diff changeset
97 #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
Ritor1
parents:
diff changeset
98 /* Small and medium model in Turbo C are for now limited to near allocation
Ritor1
parents:
diff changeset
99 * with reduced MAX_WBITS and MAX_MEM_LEVEL
Ritor1
parents:
diff changeset
100 */
Ritor1
parents:
diff changeset
101 # define MY_ZCALLOC
Ritor1
parents:
diff changeset
102
Ritor1
parents:
diff changeset
103 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
Ritor1
parents:
diff changeset
104 * and farmalloc(64K) returns a pointer with an offset of 8, so we
Ritor1
parents:
diff changeset
105 * must fix the pointer. Warning: the pointer must be put back to its
Ritor1
parents:
diff changeset
106 * original form in order to free it, use zcfree().
Ritor1
parents:
diff changeset
107 */
Ritor1
parents:
diff changeset
108
Ritor1
parents:
diff changeset
109 #define MAX_PTR 10
Ritor1
parents:
diff changeset
110 /* 10*64K = 640K */
Ritor1
parents:
diff changeset
111
Ritor1
parents:
diff changeset
112 local int next_ptr = 0;
Ritor1
parents:
diff changeset
113
Ritor1
parents:
diff changeset
114 typedef struct ptr_table_s {
Ritor1
parents:
diff changeset
115 voidpf org_ptr;
Ritor1
parents:
diff changeset
116 voidpf new_ptr;
Ritor1
parents:
diff changeset
117 } ptr_table;
Ritor1
parents:
diff changeset
118
Ritor1
parents:
diff changeset
119 local ptr_table table[MAX_PTR];
Ritor1
parents:
diff changeset
120 /* This table is used to remember the original form of pointers
Ritor1
parents:
diff changeset
121 * to large buffers (64K). Such pointers are normalized with a zero offset.
Ritor1
parents:
diff changeset
122 * Since MSDOS is not a preemptive multitasking OS, this table is not
Ritor1
parents:
diff changeset
123 * protected from concurrent access. This hack doesn't work anyway on
Ritor1
parents:
diff changeset
124 * a protected system like OS/2. Use Microsoft C instead.
Ritor1
parents:
diff changeset
125 */
Ritor1
parents:
diff changeset
126
Ritor1
parents:
diff changeset
127 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
Ritor1
parents:
diff changeset
128 {
Ritor1
parents:
diff changeset
129 voidpf buf = opaque; /* just to make some compilers happy */
Ritor1
parents:
diff changeset
130 ulg bsize = (ulg)items*size;
Ritor1
parents:
diff changeset
131
Ritor1
parents:
diff changeset
132 /* If we allocate less than 65520 bytes, we assume that farmalloc
Ritor1
parents:
diff changeset
133 * will return a usable pointer which doesn't have to be normalized.
Ritor1
parents:
diff changeset
134 */
Ritor1
parents:
diff changeset
135 if (bsize < 65520L) {
Ritor1
parents:
diff changeset
136 buf = farmalloc(bsize);
Ritor1
parents:
diff changeset
137 if (*(ush*)&buf != 0) return buf;
Ritor1
parents:
diff changeset
138 } else {
Ritor1
parents:
diff changeset
139 buf = farmalloc(bsize + 16L);
Ritor1
parents:
diff changeset
140 }
Ritor1
parents:
diff changeset
141 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
Ritor1
parents:
diff changeset
142 table[next_ptr].org_ptr = buf;
Ritor1
parents:
diff changeset
143
Ritor1
parents:
diff changeset
144 /* Normalize the pointer to seg:0 */
Ritor1
parents:
diff changeset
145 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
Ritor1
parents:
diff changeset
146 *(ush*)&buf = 0;
Ritor1
parents:
diff changeset
147 table[next_ptr++].new_ptr = buf;
Ritor1
parents:
diff changeset
148 return buf;
Ritor1
parents:
diff changeset
149 }
Ritor1
parents:
diff changeset
150
Ritor1
parents:
diff changeset
151 void zcfree (voidpf opaque, voidpf ptr)
Ritor1
parents:
diff changeset
152 {
Ritor1
parents:
diff changeset
153 int n;
Ritor1
parents:
diff changeset
154 if (*(ush*)&ptr != 0) { /* object < 64K */
Ritor1
parents:
diff changeset
155 farfree(ptr);
Ritor1
parents:
diff changeset
156 return;
Ritor1
parents:
diff changeset
157 }
Ritor1
parents:
diff changeset
158 /* Find the original pointer */
Ritor1
parents:
diff changeset
159 for (n = 0; n < next_ptr; n++) {
Ritor1
parents:
diff changeset
160 if (ptr != table[n].new_ptr) continue;
Ritor1
parents:
diff changeset
161
Ritor1
parents:
diff changeset
162 farfree(table[n].org_ptr);
Ritor1
parents:
diff changeset
163 while (++n < next_ptr) {
Ritor1
parents:
diff changeset
164 table[n-1] = table[n];
Ritor1
parents:
diff changeset
165 }
Ritor1
parents:
diff changeset
166 next_ptr--;
Ritor1
parents:
diff changeset
167 return;
Ritor1
parents:
diff changeset
168 }
Ritor1
parents:
diff changeset
169 ptr = opaque; /* just to make some compilers happy */
Ritor1
parents:
diff changeset
170 Assert(0, "zcfree: ptr not found");
Ritor1
parents:
diff changeset
171 }
Ritor1
parents:
diff changeset
172 #endif
Ritor1
parents:
diff changeset
173 #endif /* __TURBOC__ */
Ritor1
parents:
diff changeset
174
Ritor1
parents:
diff changeset
175
Ritor1
parents:
diff changeset
176 #if defined(M_I86) && !defined(__32BIT__)
Ritor1
parents:
diff changeset
177 /* Microsoft C in 16-bit mode */
Ritor1
parents:
diff changeset
178
Ritor1
parents:
diff changeset
179 # define MY_ZCALLOC
Ritor1
parents:
diff changeset
180
Ritor1
parents:
diff changeset
181 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
Ritor1
parents:
diff changeset
182 # define _halloc halloc
Ritor1
parents:
diff changeset
183 # define _hfree hfree
Ritor1
parents:
diff changeset
184 #endif
Ritor1
parents:
diff changeset
185
Ritor1
parents:
diff changeset
186 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
Ritor1
parents:
diff changeset
187 {
Ritor1
parents:
diff changeset
188 if (opaque) opaque = 0; /* to make compiler happy */
Ritor1
parents:
diff changeset
189 return _halloc((long)items, size);
Ritor1
parents:
diff changeset
190 }
Ritor1
parents:
diff changeset
191
Ritor1
parents:
diff changeset
192 void zcfree (voidpf opaque, voidpf ptr)
Ritor1
parents:
diff changeset
193 {
Ritor1
parents:
diff changeset
194 if (opaque) opaque = 0; /* to make compiler happy */
Ritor1
parents:
diff changeset
195 _hfree(ptr);
Ritor1
parents:
diff changeset
196 }
Ritor1
parents:
diff changeset
197
Ritor1
parents:
diff changeset
198 #endif /* MSC */
Ritor1
parents:
diff changeset
199
Ritor1
parents:
diff changeset
200
Ritor1
parents:
diff changeset
201 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
Ritor1
parents:
diff changeset
202
Ritor1
parents:
diff changeset
203 #ifndef STDC
Ritor1
parents:
diff changeset
204 extern voidp calloc OF((uInt items, uInt size));
Ritor1
parents:
diff changeset
205 extern void free OF((voidpf ptr));
Ritor1
parents:
diff changeset
206 #endif
Ritor1
parents:
diff changeset
207
Ritor1
parents:
diff changeset
208 voidpf zcalloc (opaque, items, size)
Ritor1
parents:
diff changeset
209 voidpf opaque;
Ritor1
parents:
diff changeset
210 unsigned items;
Ritor1
parents:
diff changeset
211 unsigned size;
Ritor1
parents:
diff changeset
212 {
Ritor1
parents:
diff changeset
213 if (opaque) items += size - size; /* make compiler happy */
Ritor1
parents:
diff changeset
214 return (voidpf)calloc(items, size);
Ritor1
parents:
diff changeset
215 }
Ritor1
parents:
diff changeset
216
Ritor1
parents:
diff changeset
217 void zcfree (opaque, ptr)
Ritor1
parents:
diff changeset
218 voidpf opaque;
Ritor1
parents:
diff changeset
219 voidpf ptr;
Ritor1
parents:
diff changeset
220 {
Ritor1
parents:
diff changeset
221 free(ptr);
Ritor1
parents:
diff changeset
222 if (opaque) return; /* make compiler happy */
Ritor1
parents:
diff changeset
223 }
Ritor1
parents:
diff changeset
224
Ritor1
parents:
diff changeset
225 #endif /* MY_ZCALLOC */