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