modules/zlib/src/zutil.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial