zutil.c 7.18 KB
Newer Older
Tom Tromey committed
1
/* zutil.c -- target dependent utility functions for the compression library
2
 * Copyright (C) 1995-2017 Jean-loup Gailly
3
 * For conditions of distribution and use, see copyright notice in zlib.h
Tom Tromey committed
4 5
 */

6
/* @(#) $Id: zutil.c,v 1.1.1.2 2002/03/11 21:53:27 tromey Exp $ */
Tom Tromey committed
7 8

#include "zutil.h"
9 10 11
#ifndef Z_SOLO
#  include "gzguts.h"
#endif
Tom Tromey committed
12

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


const char * ZEXPORT zlibVersion()
{
    return ZLIB_VERSION;
}

32 33 34 35 36
uLong ZEXPORT zlibCompileFlags()
{
    uLong flags;

    flags = 0;
37
    switch ((int)(sizeof(uInt))) {
38 39 40 41 42
    case 2:     break;
    case 4:     flags += 1;     break;
    case 8:     flags += 2;     break;
    default:    flags += 3;
    }
43
    switch ((int)(sizeof(uLong))) {
44 45 46 47 48
    case 2:     break;
    case 4:     flags += 1 << 2;        break;
    case 8:     flags += 2 << 2;        break;
    default:    flags += 3 << 2;
    }
49
    switch ((int)(sizeof(voidpf))) {
50 51 52 53 54
    case 2:     break;
    case 4:     flags += 1 << 4;        break;
    case 8:     flags += 2 << 4;        break;
    default:    flags += 3 << 4;
    }
55
    switch ((int)(sizeof(z_off_t))) {
56 57 58 59 60
    case 2:     break;
    case 4:     flags += 1 << 6;        break;
    case 8:     flags += 2 << 6;        break;
    default:    flags += 3 << 6;
    }
61
#ifdef ZLIB_DEBUG
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    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
77
    flags += 1L << 16;
78 79
#endif
#ifdef NO_GZIP
80
    flags += 1L << 17;
81 82
#endif
#ifdef PKZIP_BUG_WORKAROUND
83
    flags += 1L << 20;
84 85
#endif
#ifdef FASTEST
86
    flags += 1L << 21;
87
#endif
88
#if defined(STDC) || defined(Z_HAVE_STDARG_H)
89
#  ifdef NO_vsnprintf
90
    flags += 1L << 25;
91
#    ifdef HAS_vsprintf_void
92
    flags += 1L << 26;
93 94 95
#    endif
#  else
#    ifdef HAS_vsnprintf_void
96
    flags += 1L << 26;
97 98 99
#    endif
#  endif
#else
100
    flags += 1L << 24;
101
#  ifdef NO_snprintf
102
    flags += 1L << 25;
103
#    ifdef HAS_sprintf_void
104
    flags += 1L << 26;
105 106 107
#    endif
#  else
#    ifdef HAS_snprintf_void
108
    flags += 1L << 26;
109 110 111 112 113 114
#    endif
#  endif
#endif
    return flags;
}

115 116
#ifdef ZLIB_DEBUG
#include <stdlib.h>
Tom Tromey committed
117 118 119
#  ifndef verbose
#    define verbose 0
#  endif
120
int ZLIB_INTERNAL z_verbose = verbose;
Tom Tromey committed
121

122
void ZLIB_INTERNAL z_error (m)
Tom Tromey committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    char *m;
{
    fprintf(stderr, "%s\n", m);
    exit(1);
}
#endif

/* exported to allow conversion of error code to string for compress() and
 * uncompress()
 */
const char * ZEXPORT zError(err)
    int err;
{
    return ERR_MSG(err);
}

139
#if defined(_WIN32_WCE)
140 141 142 143
    /* 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.
     */
144 145
    int errno = 0;
#endif
Tom Tromey committed
146 147 148

#ifndef HAVE_MEMCPY

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

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

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

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

184
#ifndef Z_SOLO
185 186 187

#ifdef SYS16BIT

Tom Tromey committed
188
#ifdef __TURBOC__
189 190
/* Turbo C in 16-bit mode */

Tom Tromey committed
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
#  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.
 */

217
voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
Tom Tromey committed
218
{
219
    voidpf buf;
Tom Tromey committed
220 221
    ulg bsize = (ulg)items*size;

222 223
    (void)opaque;

Tom Tromey committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    /* 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;
}

243
void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
Tom Tromey committed
244 245
{
    int n;
246 247 248

    (void)opaque;

Tom Tromey committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    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;
    }
    Assert(0, "zcfree: ptr not found");
}
266

Tom Tromey committed
267 268 269
#endif /* __TURBOC__ */


270
#ifdef M_I86
Tom Tromey committed
271 272 273 274 275 276 277 278 279
/* Microsoft C in 16-bit mode */

#  define MY_ZCALLOC

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

280
voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
Tom Tromey committed
281
{
282
    (void)opaque;
Tom Tromey committed
283 284 285
    return _halloc((long)items, size);
}

286
void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
Tom Tromey committed
287
{
288
    (void)opaque;
Tom Tromey committed
289 290 291
    _hfree(ptr);
}

292 293 294
#endif /* M_I86 */

#endif /* SYS16BIT */
Tom Tromey committed
295 296 297 298 299


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

#ifndef STDC
300
extern voidp  malloc OF((uInt size));
Tom Tromey committed
301 302 303 304
extern voidp  calloc OF((uInt items, uInt size));
extern void   free   OF((voidpf ptr));
#endif

305
voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
Tom Tromey committed
306 307 308 309
    voidpf opaque;
    unsigned items;
    unsigned size;
{
310
    (void)opaque;
311 312
    return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
                              (voidpf)calloc(items, size);
Tom Tromey committed
313 314
}

315
void ZLIB_INTERNAL zcfree (opaque, ptr)
Tom Tromey committed
316 317 318
    voidpf opaque;
    voidpf ptr;
{
319
    (void)opaque;
Tom Tromey committed
320 321 322 323
    free(ptr);
}

#endif /* MY_ZCALLOC */
324 325

#endif /* !Z_SOLO */