annotate src/zlib-1.2.8/zutil.c @ 43:5ea0608b923f

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