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