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