annotate src/zlib-1.2.7/zutil.c @ 167:bd3cc4d1df30

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