1275793eaSopenharmony_ci/* zutil.c -- target dependent utility functions for the compression library 2275793eaSopenharmony_ci * Copyright (C) 1995-2017 Jean-loup Gailly 3275793eaSopenharmony_ci * For conditions of distribution and use, see copyright notice in zlib.h 4275793eaSopenharmony_ci */ 5275793eaSopenharmony_ci 6275793eaSopenharmony_ci/* @(#) $Id$ */ 7275793eaSopenharmony_ci 8275793eaSopenharmony_ci#include "zutil.h" 9275793eaSopenharmony_ci#ifndef Z_SOLO 10275793eaSopenharmony_ci# include "gzguts.h" 11275793eaSopenharmony_ci#endif 12275793eaSopenharmony_ci 13275793eaSopenharmony_ciz_const char * const z_errmsg[10] = { 14275793eaSopenharmony_ci (z_const char *)"need dictionary", /* Z_NEED_DICT 2 */ 15275793eaSopenharmony_ci (z_const char *)"stream end", /* Z_STREAM_END 1 */ 16275793eaSopenharmony_ci (z_const char *)"", /* Z_OK 0 */ 17275793eaSopenharmony_ci (z_const char *)"file error", /* Z_ERRNO (-1) */ 18275793eaSopenharmony_ci (z_const char *)"stream error", /* Z_STREAM_ERROR (-2) */ 19275793eaSopenharmony_ci (z_const char *)"data error", /* Z_DATA_ERROR (-3) */ 20275793eaSopenharmony_ci (z_const char *)"insufficient memory", /* Z_MEM_ERROR (-4) */ 21275793eaSopenharmony_ci (z_const char *)"buffer error", /* Z_BUF_ERROR (-5) */ 22275793eaSopenharmony_ci (z_const char *)"incompatible version",/* Z_VERSION_ERROR (-6) */ 23275793eaSopenharmony_ci (z_const char *)"" 24275793eaSopenharmony_ci}; 25275793eaSopenharmony_ci 26275793eaSopenharmony_ci 27275793eaSopenharmony_ciconst char * ZEXPORT zlibVersion(void) 28275793eaSopenharmony_ci{ 29275793eaSopenharmony_ci return ZLIB_VERSION; 30275793eaSopenharmony_ci} 31275793eaSopenharmony_ci 32275793eaSopenharmony_ciuLong ZEXPORT zlibCompileFlags(void) 33275793eaSopenharmony_ci{ 34275793eaSopenharmony_ci uLong flags; 35275793eaSopenharmony_ci 36275793eaSopenharmony_ci flags = 0; 37275793eaSopenharmony_ci switch ((int)(sizeof(uInt))) { 38275793eaSopenharmony_ci case 2: break; 39275793eaSopenharmony_ci case 4: flags += 1; break; 40275793eaSopenharmony_ci case 8: flags += 2; break; 41275793eaSopenharmony_ci default: flags += 3; 42275793eaSopenharmony_ci } 43275793eaSopenharmony_ci switch ((int)(sizeof(uLong))) { 44275793eaSopenharmony_ci case 2: break; 45275793eaSopenharmony_ci case 4: flags += 1 << 2; break; 46275793eaSopenharmony_ci case 8: flags += 2 << 2; break; 47275793eaSopenharmony_ci default: flags += 3 << 2; 48275793eaSopenharmony_ci } 49275793eaSopenharmony_ci switch ((int)(sizeof(voidpf))) { 50275793eaSopenharmony_ci case 2: break; 51275793eaSopenharmony_ci case 4: flags += 1 << 4; break; 52275793eaSopenharmony_ci case 8: flags += 2 << 4; break; 53275793eaSopenharmony_ci default: flags += 3 << 4; 54275793eaSopenharmony_ci } 55275793eaSopenharmony_ci switch ((int)(sizeof(z_off_t))) { 56275793eaSopenharmony_ci case 2: break; 57275793eaSopenharmony_ci case 4: flags += 1 << 6; break; 58275793eaSopenharmony_ci case 8: flags += 2 << 6; break; 59275793eaSopenharmony_ci default: flags += 3 << 6; 60275793eaSopenharmony_ci } 61275793eaSopenharmony_ci#ifdef ZLIB_DEBUG 62275793eaSopenharmony_ci flags += 1 << 8; 63275793eaSopenharmony_ci#endif 64275793eaSopenharmony_ci /* 65275793eaSopenharmony_ci#if defined(ASMV) || defined(ASMINF) 66275793eaSopenharmony_ci flags += 1 << 9; 67275793eaSopenharmony_ci#endif 68275793eaSopenharmony_ci */ 69275793eaSopenharmony_ci#ifdef ZLIB_WINAPI 70275793eaSopenharmony_ci flags += 1 << 10; 71275793eaSopenharmony_ci#endif 72275793eaSopenharmony_ci#ifdef BUILDFIXED 73275793eaSopenharmony_ci flags += 1 << 12; 74275793eaSopenharmony_ci#endif 75275793eaSopenharmony_ci#ifdef DYNAMIC_CRC_TABLE 76275793eaSopenharmony_ci flags += 1 << 13; 77275793eaSopenharmony_ci#endif 78275793eaSopenharmony_ci#ifdef NO_GZCOMPRESS 79275793eaSopenharmony_ci flags += 1L << 16; 80275793eaSopenharmony_ci#endif 81275793eaSopenharmony_ci#ifdef NO_GZIP 82275793eaSopenharmony_ci flags += 1L << 17; 83275793eaSopenharmony_ci#endif 84275793eaSopenharmony_ci#ifdef PKZIP_BUG_WORKAROUND 85275793eaSopenharmony_ci flags += 1L << 20; 86275793eaSopenharmony_ci#endif 87275793eaSopenharmony_ci#ifdef FASTEST 88275793eaSopenharmony_ci flags += 1L << 21; 89275793eaSopenharmony_ci#endif 90275793eaSopenharmony_ci#if defined(STDC) || defined(Z_HAVE_STDARG_H) 91275793eaSopenharmony_ci# ifdef NO_vsnprintf 92275793eaSopenharmony_ci flags += 1L << 25; 93275793eaSopenharmony_ci# ifdef HAS_vsprintf_void 94275793eaSopenharmony_ci flags += 1L << 26; 95275793eaSopenharmony_ci# endif 96275793eaSopenharmony_ci# else 97275793eaSopenharmony_ci# ifdef HAS_vsnprintf_void 98275793eaSopenharmony_ci flags += 1L << 26; 99275793eaSopenharmony_ci# endif 100275793eaSopenharmony_ci# endif 101275793eaSopenharmony_ci#else 102275793eaSopenharmony_ci flags += 1L << 24; 103275793eaSopenharmony_ci# ifdef NO_snprintf 104275793eaSopenharmony_ci flags += 1L << 25; 105275793eaSopenharmony_ci# ifdef HAS_sprintf_void 106275793eaSopenharmony_ci flags += 1L << 26; 107275793eaSopenharmony_ci# endif 108275793eaSopenharmony_ci# else 109275793eaSopenharmony_ci# ifdef HAS_snprintf_void 110275793eaSopenharmony_ci flags += 1L << 26; 111275793eaSopenharmony_ci# endif 112275793eaSopenharmony_ci# endif 113275793eaSopenharmony_ci#endif 114275793eaSopenharmony_ci return flags; 115275793eaSopenharmony_ci} 116275793eaSopenharmony_ci 117275793eaSopenharmony_ci#ifdef ZLIB_DEBUG 118275793eaSopenharmony_ci#include <stdlib.h> 119275793eaSopenharmony_ci# ifndef verbose 120275793eaSopenharmony_ci# define verbose 0 121275793eaSopenharmony_ci# endif 122275793eaSopenharmony_ciint ZLIB_INTERNAL z_verbose = verbose; 123275793eaSopenharmony_ci 124275793eaSopenharmony_civoid ZLIB_INTERNAL z_error(char *m) 125275793eaSopenharmony_ci{ 126275793eaSopenharmony_ci fprintf(stderr, "%s\n", m); 127275793eaSopenharmony_ci exit(1); 128275793eaSopenharmony_ci} 129275793eaSopenharmony_ci#endif 130275793eaSopenharmony_ci 131275793eaSopenharmony_ci/* exported to allow conversion of error code to string for compress() and 132275793eaSopenharmony_ci * uncompress() 133275793eaSopenharmony_ci */ 134275793eaSopenharmony_ciconst char * ZEXPORT zError(int err) 135275793eaSopenharmony_ci{ 136275793eaSopenharmony_ci return ERR_MSG(err); 137275793eaSopenharmony_ci} 138275793eaSopenharmony_ci 139275793eaSopenharmony_ci#if defined(_WIN32_WCE) && _WIN32_WCE < 0x800 140275793eaSopenharmony_ci /* The older Microsoft C Run-Time Library for Windows CE doesn't have 141275793eaSopenharmony_ci * errno. We define it as a global variable to simplify porting. 142275793eaSopenharmony_ci * Its value is always 0 and should not be used. 143275793eaSopenharmony_ci */ 144275793eaSopenharmony_ci int errno = 0; 145275793eaSopenharmony_ci#endif 146275793eaSopenharmony_ci 147275793eaSopenharmony_ci#ifndef HAVE_MEMCPY 148275793eaSopenharmony_ci 149275793eaSopenharmony_civoid ZLIB_INTERNAL zmemcpy(Bytef* dest, const Bytef* source, uInt len) 150275793eaSopenharmony_ci{ 151275793eaSopenharmony_ci if (len == 0) return; 152275793eaSopenharmony_ci do { 153275793eaSopenharmony_ci *dest++ = *source++; /* ??? to be unrolled */ 154275793eaSopenharmony_ci } while (--len != 0); 155275793eaSopenharmony_ci} 156275793eaSopenharmony_ci 157275793eaSopenharmony_ciint ZLIB_INTERNAL zmemcmp(const Bytef* s1, const Bytef* s2, uInt len) 158275793eaSopenharmony_ci{ 159275793eaSopenharmony_ci uInt j; 160275793eaSopenharmony_ci 161275793eaSopenharmony_ci for (j = 0; j < len; j++) { 162275793eaSopenharmony_ci if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; 163275793eaSopenharmony_ci } 164275793eaSopenharmony_ci return 0; 165275793eaSopenharmony_ci} 166275793eaSopenharmony_ci 167275793eaSopenharmony_civoid ZLIB_INTERNAL zmemzero(Bytef* dest, uInt len) 168275793eaSopenharmony_ci{ 169275793eaSopenharmony_ci if (len == 0) return; 170275793eaSopenharmony_ci do { 171275793eaSopenharmony_ci *dest++ = 0; /* ??? to be unrolled */ 172275793eaSopenharmony_ci } while (--len != 0); 173275793eaSopenharmony_ci} 174275793eaSopenharmony_ci#endif 175275793eaSopenharmony_ci 176275793eaSopenharmony_ci#ifndef Z_SOLO 177275793eaSopenharmony_ci 178275793eaSopenharmony_ci#ifdef SYS16BIT 179275793eaSopenharmony_ci 180275793eaSopenharmony_ci#ifdef __TURBOC__ 181275793eaSopenharmony_ci/* Turbo C in 16-bit mode */ 182275793eaSopenharmony_ci 183275793eaSopenharmony_ci# define MY_ZCALLOC 184275793eaSopenharmony_ci 185275793eaSopenharmony_ci/* Turbo C malloc() does not allow dynamic allocation of 64K bytes 186275793eaSopenharmony_ci * and farmalloc(64K) returns a pointer with an offset of 8, so we 187275793eaSopenharmony_ci * must fix the pointer. Warning: the pointer must be put back to its 188275793eaSopenharmony_ci * original form in order to free it, use zcfree(). 189275793eaSopenharmony_ci */ 190275793eaSopenharmony_ci 191275793eaSopenharmony_ci#define MAX_PTR 10 192275793eaSopenharmony_ci/* 10*64K = 640K */ 193275793eaSopenharmony_ci 194275793eaSopenharmony_cilocal int next_ptr = 0; 195275793eaSopenharmony_ci 196275793eaSopenharmony_citypedef struct ptr_table_s { 197275793eaSopenharmony_ci voidpf org_ptr; 198275793eaSopenharmony_ci voidpf new_ptr; 199275793eaSopenharmony_ci} ptr_table; 200275793eaSopenharmony_ci 201275793eaSopenharmony_cilocal ptr_table table[MAX_PTR]; 202275793eaSopenharmony_ci/* This table is used to remember the original form of pointers 203275793eaSopenharmony_ci * to large buffers (64K). Such pointers are normalized with a zero offset. 204275793eaSopenharmony_ci * Since MSDOS is not a preemptive multitasking OS, this table is not 205275793eaSopenharmony_ci * protected from concurrent access. This hack doesn't work anyway on 206275793eaSopenharmony_ci * a protected system like OS/2. Use Microsoft C instead. 207275793eaSopenharmony_ci */ 208275793eaSopenharmony_ci 209275793eaSopenharmony_civoidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) 210275793eaSopenharmony_ci{ 211275793eaSopenharmony_ci voidpf buf; 212275793eaSopenharmony_ci ulg bsize = (ulg)items*size; 213275793eaSopenharmony_ci 214275793eaSopenharmony_ci (void)opaque; 215275793eaSopenharmony_ci 216275793eaSopenharmony_ci /* If we allocate less than 65520 bytes, we assume that farmalloc 217275793eaSopenharmony_ci * will return a usable pointer which doesn't have to be normalized. 218275793eaSopenharmony_ci */ 219275793eaSopenharmony_ci if (bsize < 65520L) { 220275793eaSopenharmony_ci buf = farmalloc(bsize); 221275793eaSopenharmony_ci if (*(ush*)&buf != 0) return buf; 222275793eaSopenharmony_ci } else { 223275793eaSopenharmony_ci buf = farmalloc(bsize + 16L); 224275793eaSopenharmony_ci } 225275793eaSopenharmony_ci if (buf == NULL || next_ptr >= MAX_PTR) return NULL; 226275793eaSopenharmony_ci table[next_ptr].org_ptr = buf; 227275793eaSopenharmony_ci 228275793eaSopenharmony_ci /* Normalize the pointer to seg:0 */ 229275793eaSopenharmony_ci *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; 230275793eaSopenharmony_ci *(ush*)&buf = 0; 231275793eaSopenharmony_ci table[next_ptr++].new_ptr = buf; 232275793eaSopenharmony_ci return buf; 233275793eaSopenharmony_ci} 234275793eaSopenharmony_ci 235275793eaSopenharmony_civoid ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) 236275793eaSopenharmony_ci{ 237275793eaSopenharmony_ci int n; 238275793eaSopenharmony_ci 239275793eaSopenharmony_ci (void)opaque; 240275793eaSopenharmony_ci 241275793eaSopenharmony_ci if (*(ush*)&ptr != 0) { /* object < 64K */ 242275793eaSopenharmony_ci farfree(ptr); 243275793eaSopenharmony_ci return; 244275793eaSopenharmony_ci } 245275793eaSopenharmony_ci /* Find the original pointer */ 246275793eaSopenharmony_ci for (n = 0; n < next_ptr; n++) { 247275793eaSopenharmony_ci if (ptr != table[n].new_ptr) continue; 248275793eaSopenharmony_ci 249275793eaSopenharmony_ci farfree(table[n].org_ptr); 250275793eaSopenharmony_ci while (++n < next_ptr) { 251275793eaSopenharmony_ci table[n-1] = table[n]; 252275793eaSopenharmony_ci } 253275793eaSopenharmony_ci next_ptr--; 254275793eaSopenharmony_ci return; 255275793eaSopenharmony_ci } 256275793eaSopenharmony_ci Assert(0, "zcfree: ptr not found"); 257275793eaSopenharmony_ci} 258275793eaSopenharmony_ci 259275793eaSopenharmony_ci#endif /* __TURBOC__ */ 260275793eaSopenharmony_ci 261275793eaSopenharmony_ci 262275793eaSopenharmony_ci#ifdef M_I86 263275793eaSopenharmony_ci/* Microsoft C in 16-bit mode */ 264275793eaSopenharmony_ci 265275793eaSopenharmony_ci# define MY_ZCALLOC 266275793eaSopenharmony_ci 267275793eaSopenharmony_ci#if (!defined(_MSC_VER) || (_MSC_VER <= 600)) 268275793eaSopenharmony_ci# define _halloc halloc 269275793eaSopenharmony_ci# define _hfree hfree 270275793eaSopenharmony_ci#endif 271275793eaSopenharmony_ci 272275793eaSopenharmony_civoidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size) 273275793eaSopenharmony_ci{ 274275793eaSopenharmony_ci (void)opaque; 275275793eaSopenharmony_ci return _halloc((long)items, size); 276275793eaSopenharmony_ci} 277275793eaSopenharmony_ci 278275793eaSopenharmony_civoid ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) 279275793eaSopenharmony_ci{ 280275793eaSopenharmony_ci (void)opaque; 281275793eaSopenharmony_ci _hfree(ptr); 282275793eaSopenharmony_ci} 283275793eaSopenharmony_ci 284275793eaSopenharmony_ci#endif /* M_I86 */ 285275793eaSopenharmony_ci 286275793eaSopenharmony_ci#endif /* SYS16BIT */ 287275793eaSopenharmony_ci 288275793eaSopenharmony_ci 289275793eaSopenharmony_ci#ifndef MY_ZCALLOC /* Any system without a special alloc function */ 290275793eaSopenharmony_ci 291275793eaSopenharmony_ci#ifndef STDC 292275793eaSopenharmony_ciextern voidp malloc(uInt size); 293275793eaSopenharmony_ciextern voidp calloc(uInt items, uInt size); 294275793eaSopenharmony_ciextern void free(voidpf ptr); 295275793eaSopenharmony_ci#endif 296275793eaSopenharmony_ci 297275793eaSopenharmony_civoidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) 298275793eaSopenharmony_ci{ 299275793eaSopenharmony_ci (void)opaque; 300275793eaSopenharmony_ci return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : 301275793eaSopenharmony_ci (voidpf)calloc(items, size); 302275793eaSopenharmony_ci} 303275793eaSopenharmony_ci 304275793eaSopenharmony_civoid ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) 305275793eaSopenharmony_ci{ 306275793eaSopenharmony_ci (void)opaque; 307275793eaSopenharmony_ci free(ptr); 308275793eaSopenharmony_ci} 309275793eaSopenharmony_ci 310275793eaSopenharmony_ci#endif /* MY_ZCALLOC */ 311275793eaSopenharmony_ci 312275793eaSopenharmony_ci#endif /* !Z_SOLO */ 313