1c9083b85SXin LI /* zutil.c -- target dependent utility functions for the compression library
2c9083b85SXin LI * Copyright (C) 1995-2017 Jean-loup Gailly
3c9083b85SXin LI * For conditions of distribution and use, see copyright notice in zlib.h
4c9083b85SXin LI */
5c9083b85SXin LI
6c9083b85SXin LI /* @(#) $Id$ */
7c9083b85SXin LI
8c9083b85SXin LI #include "zutil.h"
9c9083b85SXin LI #ifndef Z_SOLO
10c9083b85SXin LI # include "gzguts.h"
11c9083b85SXin LI #endif
12c9083b85SXin LI
13c9083b85SXin LI z_const char * const z_errmsg[10] = {
14c9083b85SXin LI (z_const char *)"need dictionary", /* Z_NEED_DICT 2 */
15c9083b85SXin LI (z_const char *)"stream end", /* Z_STREAM_END 1 */
16c9083b85SXin LI (z_const char *)"", /* Z_OK 0 */
17c9083b85SXin LI (z_const char *)"file error", /* Z_ERRNO (-1) */
18c9083b85SXin LI (z_const char *)"stream error", /* Z_STREAM_ERROR (-2) */
19c9083b85SXin LI (z_const char *)"data error", /* Z_DATA_ERROR (-3) */
20c9083b85SXin LI (z_const char *)"insufficient memory", /* Z_MEM_ERROR (-4) */
21c9083b85SXin LI (z_const char *)"buffer error", /* Z_BUF_ERROR (-5) */
22c9083b85SXin LI (z_const char *)"incompatible version",/* Z_VERSION_ERROR (-6) */
23c9083b85SXin LI (z_const char *)""
24c9083b85SXin LI };
25c9083b85SXin LI
26c9083b85SXin LI
zlibVersion(void)27*4717628eSXin LI const char * ZEXPORT zlibVersion(void) {
28c9083b85SXin LI return ZLIB_VERSION;
29c9083b85SXin LI }
30c9083b85SXin LI
zlibCompileFlags(void)31*4717628eSXin LI uLong ZEXPORT zlibCompileFlags(void) {
32c9083b85SXin LI uLong flags;
33c9083b85SXin LI
34c9083b85SXin LI flags = 0;
35c9083b85SXin LI switch ((int)(sizeof(uInt))) {
36c9083b85SXin LI case 2: break;
37c9083b85SXin LI case 4: flags += 1; break;
38c9083b85SXin LI case 8: flags += 2; break;
39c9083b85SXin LI default: flags += 3;
40c9083b85SXin LI }
41c9083b85SXin LI switch ((int)(sizeof(uLong))) {
42c9083b85SXin LI case 2: break;
43c9083b85SXin LI case 4: flags += 1 << 2; break;
44c9083b85SXin LI case 8: flags += 2 << 2; break;
45c9083b85SXin LI default: flags += 3 << 2;
46c9083b85SXin LI }
47c9083b85SXin LI switch ((int)(sizeof(voidpf))) {
48c9083b85SXin LI case 2: break;
49c9083b85SXin LI case 4: flags += 1 << 4; break;
50c9083b85SXin LI case 8: flags += 2 << 4; break;
51c9083b85SXin LI default: flags += 3 << 4;
52c9083b85SXin LI }
53c9083b85SXin LI switch ((int)(sizeof(z_off_t))) {
54c9083b85SXin LI case 2: break;
55c9083b85SXin LI case 4: flags += 1 << 6; break;
56c9083b85SXin LI case 8: flags += 2 << 6; break;
57c9083b85SXin LI default: flags += 3 << 6;
58c9083b85SXin LI }
59c9083b85SXin LI #ifdef ZLIB_DEBUG
60c9083b85SXin LI flags += 1 << 8;
61c9083b85SXin LI #endif
62e37bb444SXin LI /*
63c9083b85SXin LI #if defined(ASMV) || defined(ASMINF)
64c9083b85SXin LI flags += 1 << 9;
65c9083b85SXin LI #endif
66e37bb444SXin LI */
67c9083b85SXin LI #ifdef ZLIB_WINAPI
68c9083b85SXin LI flags += 1 << 10;
69c9083b85SXin LI #endif
70c9083b85SXin LI #ifdef BUILDFIXED
71c9083b85SXin LI flags += 1 << 12;
72c9083b85SXin LI #endif
73c9083b85SXin LI #ifdef DYNAMIC_CRC_TABLE
74c9083b85SXin LI flags += 1 << 13;
75c9083b85SXin LI #endif
76c9083b85SXin LI #ifdef NO_GZCOMPRESS
77c9083b85SXin LI flags += 1L << 16;
78c9083b85SXin LI #endif
79c9083b85SXin LI #ifdef NO_GZIP
80c9083b85SXin LI flags += 1L << 17;
81c9083b85SXin LI #endif
82c9083b85SXin LI #ifdef PKZIP_BUG_WORKAROUND
83c9083b85SXin LI flags += 1L << 20;
84c9083b85SXin LI #endif
85c9083b85SXin LI #ifdef FASTEST
86c9083b85SXin LI flags += 1L << 21;
87c9083b85SXin LI #endif
88c9083b85SXin LI #if defined(STDC) || defined(Z_HAVE_STDARG_H)
89c9083b85SXin LI # ifdef NO_vsnprintf
90c9083b85SXin LI flags += 1L << 25;
91c9083b85SXin LI # ifdef HAS_vsprintf_void
92c9083b85SXin LI flags += 1L << 26;
93c9083b85SXin LI # endif
94c9083b85SXin LI # else
95c9083b85SXin LI # ifdef HAS_vsnprintf_void
96c9083b85SXin LI flags += 1L << 26;
97c9083b85SXin LI # endif
98c9083b85SXin LI # endif
99c9083b85SXin LI #else
100c9083b85SXin LI flags += 1L << 24;
101c9083b85SXin LI # ifdef NO_snprintf
102c9083b85SXin LI flags += 1L << 25;
103c9083b85SXin LI # ifdef HAS_sprintf_void
104c9083b85SXin LI flags += 1L << 26;
105c9083b85SXin LI # endif
106c9083b85SXin LI # else
107c9083b85SXin LI # ifdef HAS_snprintf_void
108c9083b85SXin LI flags += 1L << 26;
109c9083b85SXin LI # endif
110c9083b85SXin LI # endif
111c9083b85SXin LI #endif
112c9083b85SXin LI return flags;
113c9083b85SXin LI }
114c9083b85SXin LI
115c9083b85SXin LI #ifdef ZLIB_DEBUG
116c9083b85SXin LI #include <stdlib.h>
117c9083b85SXin LI # ifndef verbose
118c9083b85SXin LI # define verbose 0
119c9083b85SXin LI # endif
120c9083b85SXin LI int ZLIB_INTERNAL z_verbose = verbose;
121c9083b85SXin LI
z_error(char * m)122*4717628eSXin LI void ZLIB_INTERNAL z_error(char *m) {
123c9083b85SXin LI fprintf(stderr, "%s\n", m);
124c9083b85SXin LI exit(1);
125c9083b85SXin LI }
126c9083b85SXin LI #endif
127c9083b85SXin LI
128c9083b85SXin LI /* exported to allow conversion of error code to string for compress() and
129c9083b85SXin LI * uncompress()
130c9083b85SXin LI */
zError(int err)131*4717628eSXin LI const char * ZEXPORT zError(int err) {
132c9083b85SXin LI return ERR_MSG(err);
133c9083b85SXin LI }
134c9083b85SXin LI
135cd882207SXin LI #if defined(_WIN32_WCE) && _WIN32_WCE < 0x800
136cd882207SXin LI /* The older Microsoft C Run-Time Library for Windows CE doesn't have
137c9083b85SXin LI * errno. We define it as a global variable to simplify porting.
138c9083b85SXin LI * Its value is always 0 and should not be used.
139c9083b85SXin LI */
140c9083b85SXin LI int errno = 0;
141c9083b85SXin LI #endif
142c9083b85SXin LI
143c9083b85SXin LI #ifndef HAVE_MEMCPY
144c9083b85SXin LI
zmemcpy(Bytef * dest,const Bytef * source,uInt len)145*4717628eSXin LI void ZLIB_INTERNAL zmemcpy(Bytef* dest, const Bytef* source, uInt len) {
146c9083b85SXin LI if (len == 0) return;
147c9083b85SXin LI do {
148c9083b85SXin LI *dest++ = *source++; /* ??? to be unrolled */
149c9083b85SXin LI } while (--len != 0);
150c9083b85SXin LI }
151c9083b85SXin LI
zmemcmp(const Bytef * s1,const Bytef * s2,uInt len)152*4717628eSXin LI int ZLIB_INTERNAL zmemcmp(const Bytef* s1, const Bytef* s2, uInt len) {
153c9083b85SXin LI uInt j;
154c9083b85SXin LI
155c9083b85SXin LI for (j = 0; j < len; j++) {
156c9083b85SXin LI if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
157c9083b85SXin LI }
158c9083b85SXin LI return 0;
159c9083b85SXin LI }
160c9083b85SXin LI
zmemzero(Bytef * dest,uInt len)161*4717628eSXin LI void ZLIB_INTERNAL zmemzero(Bytef* dest, uInt len) {
162c9083b85SXin LI if (len == 0) return;
163c9083b85SXin LI do {
164c9083b85SXin LI *dest++ = 0; /* ??? to be unrolled */
165c9083b85SXin LI } while (--len != 0);
166c9083b85SXin LI }
167c9083b85SXin LI #endif
168c9083b85SXin LI
169c9083b85SXin LI #ifndef Z_SOLO
170c9083b85SXin LI
171c9083b85SXin LI #ifdef SYS16BIT
172c9083b85SXin LI
173c9083b85SXin LI #ifdef __TURBOC__
174c9083b85SXin LI /* Turbo C in 16-bit mode */
175c9083b85SXin LI
176c9083b85SXin LI # define MY_ZCALLOC
177c9083b85SXin LI
178c9083b85SXin LI /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
179c9083b85SXin LI * and farmalloc(64K) returns a pointer with an offset of 8, so we
180c9083b85SXin LI * must fix the pointer. Warning: the pointer must be put back to its
181c9083b85SXin LI * original form in order to free it, use zcfree().
182c9083b85SXin LI */
183c9083b85SXin LI
184c9083b85SXin LI #define MAX_PTR 10
185c9083b85SXin LI /* 10*64K = 640K */
186c9083b85SXin LI
187c9083b85SXin LI local int next_ptr = 0;
188c9083b85SXin LI
189c9083b85SXin LI typedef struct ptr_table_s {
190c9083b85SXin LI voidpf org_ptr;
191c9083b85SXin LI voidpf new_ptr;
192c9083b85SXin LI } ptr_table;
193c9083b85SXin LI
194c9083b85SXin LI local ptr_table table[MAX_PTR];
195c9083b85SXin LI /* This table is used to remember the original form of pointers
196c9083b85SXin LI * to large buffers (64K). Such pointers are normalized with a zero offset.
197c9083b85SXin LI * Since MSDOS is not a preemptive multitasking OS, this table is not
198c9083b85SXin LI * protected from concurrent access. This hack doesn't work anyway on
199c9083b85SXin LI * a protected system like OS/2. Use Microsoft C instead.
200c9083b85SXin LI */
201c9083b85SXin LI
zcalloc(voidpf opaque,unsigned items,unsigned size)202*4717628eSXin LI voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) {
203c9083b85SXin LI voidpf buf;
204c9083b85SXin LI ulg bsize = (ulg)items*size;
205c9083b85SXin LI
206c9083b85SXin LI (void)opaque;
207c9083b85SXin LI
208c9083b85SXin LI /* If we allocate less than 65520 bytes, we assume that farmalloc
209c9083b85SXin LI * will return a usable pointer which doesn't have to be normalized.
210c9083b85SXin LI */
211c9083b85SXin LI if (bsize < 65520L) {
212c9083b85SXin LI buf = farmalloc(bsize);
213c9083b85SXin LI if (*(ush*)&buf != 0) return buf;
214c9083b85SXin LI } else {
215c9083b85SXin LI buf = farmalloc(bsize + 16L);
216c9083b85SXin LI }
217c9083b85SXin LI if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
218c9083b85SXin LI table[next_ptr].org_ptr = buf;
219c9083b85SXin LI
220c9083b85SXin LI /* Normalize the pointer to seg:0 */
221c9083b85SXin LI *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
222c9083b85SXin LI *(ush*)&buf = 0;
223c9083b85SXin LI table[next_ptr++].new_ptr = buf;
224c9083b85SXin LI return buf;
225c9083b85SXin LI }
226c9083b85SXin LI
zcfree(voidpf opaque,voidpf ptr)227*4717628eSXin LI void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {
228c9083b85SXin LI int n;
229c9083b85SXin LI
230c9083b85SXin LI (void)opaque;
231c9083b85SXin LI
232c9083b85SXin LI if (*(ush*)&ptr != 0) { /* object < 64K */
233c9083b85SXin LI farfree(ptr);
234c9083b85SXin LI return;
235c9083b85SXin LI }
236c9083b85SXin LI /* Find the original pointer */
237c9083b85SXin LI for (n = 0; n < next_ptr; n++) {
238c9083b85SXin LI if (ptr != table[n].new_ptr) continue;
239c9083b85SXin LI
240c9083b85SXin LI farfree(table[n].org_ptr);
241c9083b85SXin LI while (++n < next_ptr) {
242c9083b85SXin LI table[n-1] = table[n];
243c9083b85SXin LI }
244c9083b85SXin LI next_ptr--;
245c9083b85SXin LI return;
246c9083b85SXin LI }
247c9083b85SXin LI Assert(0, "zcfree: ptr not found");
248c9083b85SXin LI }
249c9083b85SXin LI
250c9083b85SXin LI #endif /* __TURBOC__ */
251c9083b85SXin LI
252c9083b85SXin LI
253c9083b85SXin LI #ifdef M_I86
254c9083b85SXin LI /* Microsoft C in 16-bit mode */
255c9083b85SXin LI
256c9083b85SXin LI # define MY_ZCALLOC
257c9083b85SXin LI
258c9083b85SXin LI #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
259c9083b85SXin LI # define _halloc halloc
260c9083b85SXin LI # define _hfree hfree
261c9083b85SXin LI #endif
262c9083b85SXin LI
zcalloc(voidpf opaque,uInt items,uInt size)263*4717628eSXin LI voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size) {
264c9083b85SXin LI (void)opaque;
265c9083b85SXin LI return _halloc((long)items, size);
266c9083b85SXin LI }
267c9083b85SXin LI
zcfree(voidpf opaque,voidpf ptr)268*4717628eSXin LI void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {
269c9083b85SXin LI (void)opaque;
270c9083b85SXin LI _hfree(ptr);
271c9083b85SXin LI }
272c9083b85SXin LI
273c9083b85SXin LI #endif /* M_I86 */
274c9083b85SXin LI
275c9083b85SXin LI #endif /* SYS16BIT */
276c9083b85SXin LI
277c9083b85SXin LI
278c9083b85SXin LI #ifndef MY_ZCALLOC /* Any system without a special alloc function */
279c9083b85SXin LI
280c9083b85SXin LI #ifndef STDC
281*4717628eSXin LI extern voidp malloc(uInt size);
282*4717628eSXin LI extern voidp calloc(uInt items, uInt size);
283*4717628eSXin LI extern void free(voidpf ptr);
284c9083b85SXin LI #endif
285c9083b85SXin LI
zcalloc(voidpf opaque,unsigned items,unsigned size)286*4717628eSXin LI voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) {
287c9083b85SXin LI (void)opaque;
288c9083b85SXin LI return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
289c9083b85SXin LI (voidpf)calloc(items, size);
290c9083b85SXin LI }
291c9083b85SXin LI
zcfree(voidpf opaque,voidpf ptr)292*4717628eSXin LI void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {
293c9083b85SXin LI (void)opaque;
294c9083b85SXin LI free(ptr);
295c9083b85SXin LI }
296c9083b85SXin LI
297c9083b85SXin LI #endif /* MY_ZCALLOC */
298c9083b85SXin LI
299c9083b85SXin LI #endif /* !Z_SOLO */
300