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