istereo

annotate libs/zlib/zutil.c @ 43:73813c1176de

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