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