vrshoot
diff libs/zlib/zutil.c @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/zlib/zutil.c Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,318 @@ 1.4 +/* zutil.c -- target dependent utility functions for the compression library 1.5 + * Copyright (C) 1995-2005 Jean-loup Gailly. 1.6 + * For conditions of distribution and use, see copyright notice in zlib.h 1.7 + */ 1.8 + 1.9 +/* @(#) $Id$ */ 1.10 + 1.11 +#include "zutil.h" 1.12 + 1.13 +#ifndef NO_DUMMY_DECL 1.14 +struct internal_state {int dummy;}; /* for buggy compilers */ 1.15 +#endif 1.16 + 1.17 +const char * const z_errmsg[10] = { 1.18 +"need dictionary", /* Z_NEED_DICT 2 */ 1.19 +"stream end", /* Z_STREAM_END 1 */ 1.20 +"", /* Z_OK 0 */ 1.21 +"file error", /* Z_ERRNO (-1) */ 1.22 +"stream error", /* Z_STREAM_ERROR (-2) */ 1.23 +"data error", /* Z_DATA_ERROR (-3) */ 1.24 +"insufficient memory", /* Z_MEM_ERROR (-4) */ 1.25 +"buffer error", /* Z_BUF_ERROR (-5) */ 1.26 +"incompatible version",/* Z_VERSION_ERROR (-6) */ 1.27 +""}; 1.28 + 1.29 + 1.30 +const char * ZEXPORT zlibVersion() 1.31 +{ 1.32 + return ZLIB_VERSION; 1.33 +} 1.34 + 1.35 +uLong ZEXPORT zlibCompileFlags() 1.36 +{ 1.37 + uLong flags; 1.38 + 1.39 + flags = 0; 1.40 + switch (sizeof(uInt)) { 1.41 + case 2: break; 1.42 + case 4: flags += 1; break; 1.43 + case 8: flags += 2; break; 1.44 + default: flags += 3; 1.45 + } 1.46 + switch (sizeof(uLong)) { 1.47 + case 2: break; 1.48 + case 4: flags += 1 << 2; break; 1.49 + case 8: flags += 2 << 2; break; 1.50 + default: flags += 3 << 2; 1.51 + } 1.52 + switch (sizeof(voidpf)) { 1.53 + case 2: break; 1.54 + case 4: flags += 1 << 4; break; 1.55 + case 8: flags += 2 << 4; break; 1.56 + default: flags += 3 << 4; 1.57 + } 1.58 + switch (sizeof(z_off_t)) { 1.59 + case 2: break; 1.60 + case 4: flags += 1 << 6; break; 1.61 + case 8: flags += 2 << 6; break; 1.62 + default: flags += 3 << 6; 1.63 + } 1.64 +#ifdef DEBUG 1.65 + flags += 1 << 8; 1.66 +#endif 1.67 +#if defined(ASMV) || defined(ASMINF) 1.68 + flags += 1 << 9; 1.69 +#endif 1.70 +#ifdef ZLIB_WINAPI 1.71 + flags += 1 << 10; 1.72 +#endif 1.73 +#ifdef BUILDFIXED 1.74 + flags += 1 << 12; 1.75 +#endif 1.76 +#ifdef DYNAMIC_CRC_TABLE 1.77 + flags += 1 << 13; 1.78 +#endif 1.79 +#ifdef NO_GZCOMPRESS 1.80 + flags += 1L << 16; 1.81 +#endif 1.82 +#ifdef NO_GZIP 1.83 + flags += 1L << 17; 1.84 +#endif 1.85 +#ifdef PKZIP_BUG_WORKAROUND 1.86 + flags += 1L << 20; 1.87 +#endif 1.88 +#ifdef FASTEST 1.89 + flags += 1L << 21; 1.90 +#endif 1.91 +#ifdef STDC 1.92 +# ifdef NO_vsnprintf 1.93 + flags += 1L << 25; 1.94 +# ifdef HAS_vsprintf_void 1.95 + flags += 1L << 26; 1.96 +# endif 1.97 +# else 1.98 +# ifdef HAS_vsnprintf_void 1.99 + flags += 1L << 26; 1.100 +# endif 1.101 +# endif 1.102 +#else 1.103 + flags += 1L << 24; 1.104 +# ifdef NO_snprintf 1.105 + flags += 1L << 25; 1.106 +# ifdef HAS_sprintf_void 1.107 + flags += 1L << 26; 1.108 +# endif 1.109 +# else 1.110 +# ifdef HAS_snprintf_void 1.111 + flags += 1L << 26; 1.112 +# endif 1.113 +# endif 1.114 +#endif 1.115 + return flags; 1.116 +} 1.117 + 1.118 +#ifdef DEBUG 1.119 + 1.120 +# ifndef verbose 1.121 +# define verbose 0 1.122 +# endif 1.123 +int z_verbose = verbose; 1.124 + 1.125 +void z_error (m) 1.126 + char *m; 1.127 +{ 1.128 + fprintf(stderr, "%s\n", m); 1.129 + exit(1); 1.130 +} 1.131 +#endif 1.132 + 1.133 +/* exported to allow conversion of error code to string for compress() and 1.134 + * uncompress() 1.135 + */ 1.136 +const char * ZEXPORT zError(err) 1.137 + int err; 1.138 +{ 1.139 + return ERR_MSG(err); 1.140 +} 1.141 + 1.142 +#if defined(_WIN32_WCE) 1.143 + /* The Microsoft C Run-Time Library for Windows CE doesn't have 1.144 + * errno. We define it as a global variable to simplify porting. 1.145 + * Its value is always 0 and should not be used. 1.146 + */ 1.147 + int errno = 0; 1.148 +#endif 1.149 + 1.150 +#ifndef HAVE_MEMCPY 1.151 + 1.152 +void zmemcpy(dest, source, len) 1.153 + Bytef* dest; 1.154 + const Bytef* source; 1.155 + uInt len; 1.156 +{ 1.157 + if (len == 0) return; 1.158 + do { 1.159 + *dest++ = *source++; /* ??? to be unrolled */ 1.160 + } while (--len != 0); 1.161 +} 1.162 + 1.163 +int zmemcmp(s1, s2, len) 1.164 + const Bytef* s1; 1.165 + const Bytef* s2; 1.166 + uInt len; 1.167 +{ 1.168 + uInt j; 1.169 + 1.170 + for (j = 0; j < len; j++) { 1.171 + if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; 1.172 + } 1.173 + return 0; 1.174 +} 1.175 + 1.176 +void zmemzero(dest, len) 1.177 + Bytef* dest; 1.178 + uInt len; 1.179 +{ 1.180 + if (len == 0) return; 1.181 + do { 1.182 + *dest++ = 0; /* ??? to be unrolled */ 1.183 + } while (--len != 0); 1.184 +} 1.185 +#endif 1.186 + 1.187 + 1.188 +#ifdef SYS16BIT 1.189 + 1.190 +#ifdef __TURBOC__ 1.191 +/* Turbo C in 16-bit mode */ 1.192 + 1.193 +# define MY_ZCALLOC 1.194 + 1.195 +/* Turbo C malloc() does not allow dynamic allocation of 64K bytes 1.196 + * and farmalloc(64K) returns a pointer with an offset of 8, so we 1.197 + * must fix the pointer. Warning: the pointer must be put back to its 1.198 + * original form in order to free it, use zcfree(). 1.199 + */ 1.200 + 1.201 +#define MAX_PTR 10 1.202 +/* 10*64K = 640K */ 1.203 + 1.204 +local int next_ptr = 0; 1.205 + 1.206 +typedef struct ptr_table_s { 1.207 + voidpf org_ptr; 1.208 + voidpf new_ptr; 1.209 +} ptr_table; 1.210 + 1.211 +local ptr_table table[MAX_PTR]; 1.212 +/* This table is used to remember the original form of pointers 1.213 + * to large buffers (64K). Such pointers are normalized with a zero offset. 1.214 + * Since MSDOS is not a preemptive multitasking OS, this table is not 1.215 + * protected from concurrent access. This hack doesn't work anyway on 1.216 + * a protected system like OS/2. Use Microsoft C instead. 1.217 + */ 1.218 + 1.219 +voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) 1.220 +{ 1.221 + voidpf buf = opaque; /* just to make some compilers happy */ 1.222 + ulg bsize = (ulg)items*size; 1.223 + 1.224 + /* If we allocate less than 65520 bytes, we assume that farmalloc 1.225 + * will return a usable pointer which doesn't have to be normalized. 1.226 + */ 1.227 + if (bsize < 65520L) { 1.228 + buf = farmalloc(bsize); 1.229 + if (*(ush*)&buf != 0) return buf; 1.230 + } else { 1.231 + buf = farmalloc(bsize + 16L); 1.232 + } 1.233 + if (buf == NULL || next_ptr >= MAX_PTR) return NULL; 1.234 + table[next_ptr].org_ptr = buf; 1.235 + 1.236 + /* Normalize the pointer to seg:0 */ 1.237 + *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; 1.238 + *(ush*)&buf = 0; 1.239 + table[next_ptr++].new_ptr = buf; 1.240 + return buf; 1.241 +} 1.242 + 1.243 +void zcfree (voidpf opaque, voidpf ptr) 1.244 +{ 1.245 + int n; 1.246 + if (*(ush*)&ptr != 0) { /* object < 64K */ 1.247 + farfree(ptr); 1.248 + return; 1.249 + } 1.250 + /* Find the original pointer */ 1.251 + for (n = 0; n < next_ptr; n++) { 1.252 + if (ptr != table[n].new_ptr) continue; 1.253 + 1.254 + farfree(table[n].org_ptr); 1.255 + while (++n < next_ptr) { 1.256 + table[n-1] = table[n]; 1.257 + } 1.258 + next_ptr--; 1.259 + return; 1.260 + } 1.261 + ptr = opaque; /* just to make some compilers happy */ 1.262 + Assert(0, "zcfree: ptr not found"); 1.263 +} 1.264 + 1.265 +#endif /* __TURBOC__ */ 1.266 + 1.267 + 1.268 +#ifdef M_I86 1.269 +/* Microsoft C in 16-bit mode */ 1.270 + 1.271 +# define MY_ZCALLOC 1.272 + 1.273 +#if (!defined(_MSC_VER) || (_MSC_VER <= 600)) 1.274 +# define _halloc halloc 1.275 +# define _hfree hfree 1.276 +#endif 1.277 + 1.278 +voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) 1.279 +{ 1.280 + if (opaque) opaque = 0; /* to make compiler happy */ 1.281 + return _halloc((long)items, size); 1.282 +} 1.283 + 1.284 +void zcfree (voidpf opaque, voidpf ptr) 1.285 +{ 1.286 + if (opaque) opaque = 0; /* to make compiler happy */ 1.287 + _hfree(ptr); 1.288 +} 1.289 + 1.290 +#endif /* M_I86 */ 1.291 + 1.292 +#endif /* SYS16BIT */ 1.293 + 1.294 + 1.295 +#ifndef MY_ZCALLOC /* Any system without a special alloc function */ 1.296 + 1.297 +#ifndef STDC 1.298 +extern voidp malloc OF((uInt size)); 1.299 +extern voidp calloc OF((uInt items, uInt size)); 1.300 +extern void free OF((voidpf ptr)); 1.301 +#endif 1.302 + 1.303 +voidpf zcalloc (opaque, items, size) 1.304 + voidpf opaque; 1.305 + unsigned items; 1.306 + unsigned size; 1.307 +{ 1.308 + if (opaque) items += size - size; /* make compiler happy */ 1.309 + return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : 1.310 + (voidpf)calloc(items, size); 1.311 +} 1.312 + 1.313 +void zcfree (opaque, ptr) 1.314 + voidpf opaque; 1.315 + voidpf ptr; 1.316 +{ 1.317 + free(ptr); 1.318 + if (opaque) return; /* make compiler happy */ 1.319 +} 1.320 + 1.321 +#endif /* MY_ZCALLOC */