annotate src/zlib-1.2.7/zutil.c @ 83:ae30d91d2ffe

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