- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathzutil.c
323 lines (273 loc) · 8.12 KB
/
zutil.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* zutil.c -- target dependent utility functions for the compression library
* Copyright (C) 1995-2017 Jean-loup Gailly
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* @(#) $Id$ */
#include"zutil.h"
#ifndefZ_SOLO
# include"gzguts.h"
#endif
z_constchar*constz_errmsg[10] = {
(z_constchar*)"need dictionary", /* Z_NEED_DICT 2 */
(z_constchar*)"stream end", /* Z_STREAM_END 1 */
(z_constchar*)"", /* Z_OK 0 */
(z_constchar*)"file error", /* Z_ERRNO (-1) */
(z_constchar*)"stream error", /* Z_STREAM_ERROR (-2) */
(z_constchar*)"data error", /* Z_DATA_ERROR (-3) */
(z_constchar*)"insufficient memory", /* Z_MEM_ERROR (-4) */
(z_constchar*)"buffer error", /* Z_BUF_ERROR (-5) */
(z_constchar*)"incompatible version",/* Z_VERSION_ERROR (-6) */
(z_constchar*)""
};
constchar*ZEXPORTzlibVersion(void) {
returnZLIB_VERSION;
}
uLongZEXPORTzlibCompileFlags(void) {
uLongflags;
flags=0;
switch ((int)(sizeof(uInt))) {
case2: break;
case4: flags+=1; break;
case8: flags+=2; break;
default: flags+=3;
}
switch ((int)(sizeof(uLong))) {
case2: break;
case4: flags+=1 << 2; break;
case8: flags+=2 << 2; break;
default: flags+=3 << 2;
}
switch ((int)(sizeof(voidpf))) {
case2: break;
case4: flags+=1 << 4; break;
case8: flags+=2 << 4; break;
default: flags+=3 << 4;
}
switch ((int)(sizeof(z_off_t))) {
case2: break;
case4: flags+=1 << 6; break;
case8: flags+=2 << 6; break;
default: flags+=3 << 6;
}
#ifdefZLIB_DEBUG
flags+=1 << 8;
#endif
/*
#if defined(ASMV) || defined(ASMINF)
flags += 1 << 9;
#endif
*/
#ifdefZLIB_WINAPI
flags+=1 << 10;
#endif
#ifdefBUILDFIXED
flags+=1 << 12;
#endif
#ifdefDYNAMIC_CRC_TABLE
flags+=1 << 13;
#endif
#ifdefNO_GZCOMPRESS
flags+=1L << 16;
#endif
#ifdefNO_GZIP
flags+=1L << 17;
#endif
#ifdefPKZIP_BUG_WORKAROUND
flags+=1L << 20;
#endif
#ifdefFASTEST
flags+=1L << 21;
#endif
#if defined(STDC) || defined(Z_HAVE_STDARG_H)
# ifdefNO_vsnprintf
flags+=1L << 25;
# ifdefHAS_vsprintf_void
flags+=1L << 26;
# endif
# else
# ifdefHAS_vsnprintf_void
flags+=1L << 26;
# endif
# endif
#else
flags+=1L << 24;
# ifdefNO_snprintf
flags+=1L << 25;
# ifdefHAS_sprintf_void
flags+=1L << 26;
# endif
# else
# ifdefHAS_snprintf_void
flags+=1L << 26;
# endif
# endif
#endif
returnflags;
}
#ifdefZLIB_DEBUG
#include<stdlib.h>
# ifndefverbose
# defineverbose 0
# endif
intZLIB_INTERNALz_verbose=verbose;
voidZLIB_INTERNALz_error(char*m) {
fprintf(stderr, "%s\n", m);
exit(1);
}
#endif
/* exported to allow conversion of error code to string for compress() and
* uncompress()
*/
constchar*ZEXPORTzError(interr) {
returnERR_MSG(err);
}
#if defined(_WIN32_WCE) &&_WIN32_WCE<0x800
/* The older Microsoft C Run-Time Library for Windows CE doesn't have
* errno. We define it as a global variable to simplify porting.
* Its value is always 0 and should not be used.
*/
interrno=0;
#endif
#ifndefHAVE_MEMCPY
voidZLIB_INTERNALzmemcpy(Bytef*dest, constBytef*source, uIntlen) {
if (len==0) return;
do {
*dest++=*source++; /* ??? to be unrolled */
} while (--len!=0);
}
intZLIB_INTERNALzmemcmp(constBytef*s1, constBytef*s2, uIntlen) {
uIntj;
for (j=0; j<len; j++) {
if (s1[j] !=s2[j]) return2*(s1[j] >s2[j])-1;
}
return0;
}
voidZLIB_INTERNALzmemzero(Bytef*dest, uIntlen) {
if (len==0) return;
do {
*dest++=0; /* ??? to be unrolled */
} while (--len!=0);
}
#endif
#ifndefZ_SOLO
#ifdefSYS16BIT
#ifdef__TURBOC__
/* Turbo C in 16-bit mode */
# defineMY_ZCALLOC
/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
* and farmalloc(64K) returns a pointer with an offset of 8, so we
* must fix the pointer. Warning: the pointer must be put back to its
* original form in order to free it, use zcfree().
*/
#defineMAX_PTR 10
/* 10*64K = 640K */
localintnext_ptr=0;
typedefstructptr_table_s {
voidpforg_ptr;
voidpfnew_ptr;
} ptr_table;
localptr_tabletable[MAX_PTR];
/* This table is used to remember the original form of pointers
* to large buffers (64K). Such pointers are normalized with a zero offset.
* Since MSDOS is not a preemptive multitasking OS, this table is not
* protected from concurrent access. This hack doesn't work anyway on
* a protected system like OS/2. Use Microsoft C instead.
*/
voidpfZLIB_INTERNALzcalloc(voidpfopaque, unsigneditems, unsignedsize) {
voidpfbuf;
ulgbsize= (ulg)items*size;
(void)opaque;
/* If we allocate less than 65520 bytes, we assume that farmalloc
* will return a usable pointer which doesn't have to be normalized.
*/
if (bsize<65520L) {
buf=farmalloc(bsize);
if (*(ush*)&buf!=0) returnbuf;
} else {
buf=farmalloc(bsize+16L);
}
if (buf==NULL||next_ptr >= MAX_PTR) returnNULL;
table[next_ptr].org_ptr=buf;
/* Normalize the pointer to seg:0 */
*((ush*)&buf+1) += ((ush)((uch*)buf-0) +15) >> 4;
*(ush*)&buf=0;
table[next_ptr++].new_ptr=buf;
returnbuf;
}
voidZLIB_INTERNALzcfree(voidpfopaque, voidpfptr) {
intn;
(void)opaque;
if (*(ush*)&ptr!=0) { /* object < 64K */
farfree(ptr);
return;
}
/* Find the original pointer */
for (n=0; n<next_ptr; n++) {
if (ptr!=table[n].new_ptr) continue;
farfree(table[n].org_ptr);
while (++n<next_ptr) {
table[n-1] =table[n];
}
next_ptr--;
return;
}
Assert(0, "zcfree: ptr not found");
}
#endif/* __TURBOC__ */
#ifdefM_I86
/* Microsoft C in 16-bit mode */
# defineMY_ZCALLOC
#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
# define_halloc halloc
# define_hfree hfree
#endif
voidpfZLIB_INTERNALzcalloc(voidpfopaque, uIntitems, uIntsize) {
(void)opaque;
return_halloc((long)items, size);
}
voidZLIB_INTERNALzcfree(voidpfopaque, voidpfptr) {
(void)opaque;
_hfree(ptr);
}
#endif/* M_I86 */
#endif/* SYS16BIT */
#ifndefMY_ZCALLOC/* Any system without a special alloc function */
#ifndefSTDC
externvoidpmalloc(uIntsize);
externvoidpcalloc(uIntitems, uIntsize);
externvoidfree(voidpfptr);
#endif
voidpfZLIB_INTERNALzcalloc(voidpfopaque, unsigneditems, unsignedsize) {
(void)opaque;
returnsizeof(uInt) >2 ? (voidpf)malloc(items*size) :
(voidpf)calloc(items, size);
}
voidZLIB_INTERNALzcfree(voidpfopaque, voidpfptr) {
(void)opaque;
free(ptr);
}
#endif/* MY_ZCALLOC */
#endif/* !Z_SOLO */