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