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