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