zutil.c 6.97 KB
Newer Older
dimitri's avatar
dimitri committed
1
/* zutil.c -- target dependent utility functions for the compression library
dimitri's avatar
dimitri committed
2 3
 * Copyright (C) 1995-2005 Jean-loup Gailly.
 * For conditions of distribution and use, see copyright notice in zlib.h
dimitri's avatar
dimitri committed
4 5 6 7 8 9
 */

/* @(#) $Id$ */

#include "zutil.h"

dimitri's avatar
dimitri committed
10
#ifndef NO_DUMMY_DECL
dimitri's avatar
dimitri committed
11 12 13
struct internal_state      {int dummy;}; /* for buggy compilers */
#endif

dimitri's avatar
dimitri committed
14
const char * const z_errmsg[10] = {
dimitri's avatar
dimitri committed
15 16 17 18 19 20 21 22 23 24 25 26
"need dictionary",     /* Z_NEED_DICT       2  */
"stream end",          /* Z_STREAM_END      1  */
"",                    /* Z_OK              0  */
"file error",          /* Z_ERRNO         (-1) */
"stream error",        /* Z_STREAM_ERROR  (-2) */
"data error",          /* Z_DATA_ERROR    (-3) */
"insufficient memory", /* Z_MEM_ERROR     (-4) */
"buffer error",        /* Z_BUF_ERROR     (-5) */
"incompatible version",/* Z_VERSION_ERROR (-6) */
""};


dimitri's avatar
dimitri committed
27
const char * ZEXPORT zlibVersion()
dimitri's avatar
dimitri committed
28 29 30 31
{
    return ZLIB_VERSION;
}

dimitri's avatar
dimitri committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
uLong ZEXPORT zlibCompileFlags()
{
    uLong flags;

    flags = 0;
    switch (sizeof(uInt)) {
    case 2:     break;
    case 4:     flags += 1;     break;
    case 8:     flags += 2;     break;
    default:    flags += 3;
    }
    switch (sizeof(uLong)) {
    case 2:     break;
    case 4:     flags += 1 << 2;        break;
    case 8:     flags += 2 << 2;        break;
    default:    flags += 3 << 2;
    }
    switch (sizeof(voidpf)) {
    case 2:     break;
    case 4:     flags += 1 << 4;        break;
    case 8:     flags += 2 << 4;        break;
    default:    flags += 3 << 4;
    }
    switch (sizeof(z_off_t)) {
    case 2:     break;
    case 4:     flags += 1 << 6;        break;
    case 8:     flags += 2 << 6;        break;
    default:    flags += 3 << 6;
    }
#ifdef DEBUG
    flags += 1 << 8;
#endif
#if defined(ASMV) || defined(ASMINF)
    flags += 1 << 9;
#endif
#ifdef ZLIB_WINAPI
    flags += 1 << 10;
#endif
#ifdef BUILDFIXED
    flags += 1 << 12;
#endif
#ifdef DYNAMIC_CRC_TABLE
    flags += 1 << 13;
#endif
#ifdef NO_GZCOMPRESS
    flags += 1L << 16;
#endif
#ifdef NO_GZIP
    flags += 1L << 17;
#endif
#ifdef PKZIP_BUG_WORKAROUND
    flags += 1L << 20;
#endif
#ifdef FASTEST
    flags += 1L << 21;
#endif
#ifdef STDC
#  ifdef NO_vsnprintf
        flags += 1L << 25;
#    ifdef HAS_vsprintf_void
        flags += 1L << 26;
#    endif
#  else
#    ifdef HAS_vsnprintf_void
        flags += 1L << 26;
#    endif
#  endif
#else
        flags += 1L << 24;
#  ifdef NO_snprintf
        flags += 1L << 25;
#    ifdef HAS_sprintf_void
        flags += 1L << 26;
#    endif
#  else
#    ifdef HAS_snprintf_void
        flags += 1L << 26;
#    endif
#  endif
#endif
    return flags;
}

dimitri's avatar
dimitri committed
115 116 117 118 119 120 121
#ifdef DEBUG

#  ifndef verbose
#    define verbose 0
#  endif
int z_verbose = verbose;

dimitri's avatar
dimitri committed
122 123
void z_error (m)
    char *m;
dimitri's avatar
dimitri committed
124 125 126 127 128 129 130 131 132
{
    fprintf(stderr, "%s\n", m);
    exit(1);
}
#endif

/* exported to allow conversion of error code to string for compress() and
 * uncompress()
 */
dimitri's avatar
dimitri committed
133 134
const char * ZEXPORT zError(err)
    int err;
dimitri's avatar
dimitri committed
135 136 137 138
{
    return ERR_MSG(err);
}

dimitri's avatar
dimitri committed
139 140 141 142 143 144 145
#if defined(_WIN32_WCE)
    /* The Microsoft C Run-Time Library for Windows CE doesn't have
     * errno.  We define it as a global variable to simplify porting.
     * Its value is always 0 and should not be used.
     */
    int errno = 0;
#endif
dimitri's avatar
dimitri committed
146 147 148

#ifndef HAVE_MEMCPY

dimitri's avatar
dimitri committed
149 150 151 152
void zmemcpy(dest, source, len)
    Bytef* dest;
    const Bytef* source;
    uInt  len;
dimitri's avatar
dimitri committed
153 154 155 156 157 158 159
{
    if (len == 0) return;
    do {
        *dest++ = *source++; /* ??? to be unrolled */
    } while (--len != 0);
}

dimitri's avatar
dimitri committed
160 161 162 163
int zmemcmp(s1, s2, len)
    const Bytef* s1;
    const Bytef* s2;
    uInt  len;
dimitri's avatar
dimitri committed
164 165 166 167 168 169 170 171 172
{
    uInt j;

    for (j = 0; j < len; j++) {
        if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
    }
    return 0;
}

dimitri's avatar
dimitri committed
173 174 175
void zmemzero(dest, len)
    Bytef* dest;
    uInt  len;
dimitri's avatar
dimitri committed
176 177 178 179 180 181 182 183
{
    if (len == 0) return;
    do {
        *dest++ = 0;  /* ??? to be unrolled */
    } while (--len != 0);
}
#endif

dimitri's avatar
dimitri committed
184 185 186

#ifdef SYS16BIT

dimitri's avatar
dimitri committed
187
#ifdef __TURBOC__
dimitri's avatar
dimitri committed
188 189
/* Turbo C in 16-bit mode */

dimitri's avatar
dimitri committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
#  define MY_ZCALLOC

/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
 * and farmalloc(64K) returns a pointer with an offset of 8, so we
 * must fix the pointer. Warning: the pointer must be put back to its
 * original form in order to free it, use zcfree().
 */

#define MAX_PTR 10
/* 10*64K = 640K */

local int next_ptr = 0;

typedef struct ptr_table_s {
    voidpf org_ptr;
    voidpf new_ptr;
} ptr_table;

local ptr_table table[MAX_PTR];
/* This table is used to remember the original form of pointers
 * to large buffers (64K). Such pointers are normalized with a zero offset.
 * Since MSDOS is not a preemptive multitasking OS, this table is not
 * protected from concurrent access. This hack doesn't work anyway on
 * a protected system like OS/2. Use Microsoft C instead.
 */

voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
{
    voidpf buf = opaque; /* just to make some compilers happy */
    ulg bsize = (ulg)items*size;

    /* If we allocate less than 65520 bytes, we assume that farmalloc
     * will return a usable pointer which doesn't have to be normalized.
     */
    if (bsize < 65520L) {
        buf = farmalloc(bsize);
        if (*(ush*)&buf != 0) return buf;
    } else {
        buf = farmalloc(bsize + 16L);
    }
    if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
    table[next_ptr].org_ptr = buf;

    /* Normalize the pointer to seg:0 */
    *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
    *(ush*)&buf = 0;
    table[next_ptr++].new_ptr = buf;
    return buf;
}

void  zcfree (voidpf opaque, voidpf ptr)
{
    int n;
    if (*(ush*)&ptr != 0) { /* object < 64K */
        farfree(ptr);
        return;
    }
    /* Find the original pointer */
    for (n = 0; n < next_ptr; n++) {
        if (ptr != table[n].new_ptr) continue;

        farfree(table[n].org_ptr);
        while (++n < next_ptr) {
            table[n-1] = table[n];
        }
        next_ptr--;
        return;
    }
    ptr = opaque; /* just to make some compilers happy */
    Assert(0, "zcfree: ptr not found");
}
dimitri's avatar
dimitri committed
261

dimitri's avatar
dimitri committed
262 263 264
#endif /* __TURBOC__ */


dimitri's avatar
dimitri committed
265
#ifdef M_I86
dimitri's avatar
dimitri committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
/* Microsoft C in 16-bit mode */

#  define MY_ZCALLOC

#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
#  define _halloc  halloc
#  define _hfree   hfree
#endif

voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
{
    if (opaque) opaque = 0; /* to make compiler happy */
    return _halloc((long)items, size);
}

void  zcfree (voidpf opaque, voidpf ptr)
{
    if (opaque) opaque = 0; /* to make compiler happy */
    _hfree(ptr);
}

dimitri's avatar
dimitri committed
287 288 289
#endif /* M_I86 */

#endif /* SYS16BIT */
dimitri's avatar
dimitri committed
290 291 292 293 294


#ifndef MY_ZCALLOC /* Any system without a special alloc function */

#ifndef STDC
dimitri's avatar
dimitri committed
295
extern voidp  malloc OF((uInt size));
dimitri's avatar
dimitri committed
296 297 298 299
extern voidp  calloc OF((uInt items, uInt size));
extern void   free   OF((voidpf ptr));
#endif

dimitri's avatar
dimitri committed
300 301 302 303
voidpf zcalloc (opaque, items, size)
    voidpf opaque;
    unsigned items;
    unsigned size;
dimitri's avatar
dimitri committed
304 305
{
    if (opaque) items += size - size; /* make compiler happy */
dimitri's avatar
dimitri committed
306 307
    return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
                              (voidpf)calloc(items, size);
dimitri's avatar
dimitri committed
308 309
}

dimitri's avatar
dimitri committed
310 311 312
void  zcfree (opaque, ptr)
    voidpf opaque;
    voidpf ptr;
dimitri's avatar
dimitri committed
313 314 315 316 317 318
{
    free(ptr);
    if (opaque) return; /* make compiler happy */
}

#endif /* MY_ZCALLOC */