zutil.c 7.1 KB
Newer Older
M
Mark Adler 已提交
1
/* zutil.c -- target dependent utility functions for the compression library
M
Mark Adler 已提交
2
 * Copyright (C) 1995-2005, 2010 Jean-loup Gailly.
M
Mark Adler 已提交
3
 * For conditions of distribution and use, see copyright notice in zlib.h
M
Mark Adler 已提交
4 5
 */

M
Mark Adler 已提交
6
/* @(#) $Id$ */
M
Mark Adler 已提交
7 8 9

#include "zutil.h"

M
Mark Adler 已提交
10
#ifndef NO_DUMMY_DECL
M
Mark Adler 已提交
11
struct internal_state      {int dummy;}; /* for buggy compilers */
M
Mark Adler 已提交
12
#endif
M
Mark Adler 已提交
13

M
Mark Adler 已提交
14
const char * const z_errmsg[10] = {
M
Mark Adler 已提交
15 16 17 18 19 20 21 22 23
"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) */
M
Mark Adler 已提交
24 25 26
""};


M
Mark Adler 已提交
27
const char * ZEXPORT zlibVersion()
M
Mark Adler 已提交
28 29 30 31
{
    return ZLIB_VERSION;
}

M
Mark Adler 已提交
32 33 34 35 36
uLong ZEXPORT zlibCompileFlags()
{
    uLong flags;

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

M
Mark Adler 已提交
115
#ifdef DEBUG
M
Mark Adler 已提交
116 117 118 119

#  ifndef verbose
#    define verbose 0
#  endif
M
Mark Adler 已提交
120
int ZLIB_INTERNAL z_verbose = verbose;
M
Mark Adler 已提交
121

M
Mark Adler 已提交
122
void ZLIB_INTERNAL z_error (m)
M
Mark Adler 已提交
123 124 125 126 127
    char *m;
{
    fprintf(stderr, "%s\n", m);
    exit(1);
}
M
Mark Adler 已提交
128
#endif
M
Mark Adler 已提交
129

M
Mark Adler 已提交
130 131 132
/* exported to allow conversion of error code to string for compress() and
 * uncompress()
 */
M
Mark Adler 已提交
133
const char * ZEXPORT zError(err)
M
Mark Adler 已提交
134 135 136 137 138
    int err;
{
    return ERR_MSG(err);
}

M
Mark Adler 已提交
139
#if defined(_WIN32_WCE)
M
Mark Adler 已提交
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.
     */
M
Mark Adler 已提交
144 145
    int errno = 0;
#endif
M
Mark Adler 已提交
146

M
Mark Adler 已提交
147 148
#ifndef HAVE_MEMCPY

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

M
Mark Adler 已提交
160
int ZLIB_INTERNAL zmemcmp(s1, s2, len)
M
Mark Adler 已提交
161 162
    const Bytef* s1;
    const Bytef* s2;
M
Mark Adler 已提交
163 164 165 166 167 168 169 170 171 172
    uInt  len;
{
    uInt j;

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

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

M
Mark Adler 已提交
184 185 186

#ifdef SYS16BIT

M
Mark Adler 已提交
187
#ifdef __TURBOC__
M
Mark Adler 已提交
188 189
/* Turbo C in 16-bit mode */

M
Mark Adler 已提交
190
#  define MY_ZCALLOC
M
Mark Adler 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203

/* 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 {
M
Mark Adler 已提交
204 205
    voidpf org_ptr;
    voidpf new_ptr;
M
Mark Adler 已提交
206 207 208 209 210 211 212 213 214 215
} 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.
 */

M
Mark Adler 已提交
216
voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
M
Mark Adler 已提交
217
{
M
Mark Adler 已提交
218
    voidpf buf = opaque; /* just to make some compilers happy */
M
Mark Adler 已提交
219 220
    ulg bsize = (ulg)items*size;

M
Mark Adler 已提交
221 222 223 224
    /* 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) {
M
Mark Adler 已提交
225 226
        buf = farmalloc(bsize);
        if (*(ush*)&buf != 0) return buf;
M
Mark Adler 已提交
227
    } else {
M
Mark Adler 已提交
228
        buf = farmalloc(bsize + 16L);
M
Mark Adler 已提交
229 230 231 232 233 234 235 236 237 238 239
    }
    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;
}

M
Mark Adler 已提交
240
void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
M
Mark Adler 已提交
241 242 243
{
    int n;
    if (*(ush*)&ptr != 0) { /* object < 64K */
M
Mark Adler 已提交
244 245
        farfree(ptr);
        return;
M
Mark Adler 已提交
246 247 248
    }
    /* Find the original pointer */
    for (n = 0; n < next_ptr; n++) {
M
Mark Adler 已提交
249 250 251 252 253 254 255 256
        if (ptr != table[n].new_ptr) continue;

        farfree(table[n].org_ptr);
        while (++n < next_ptr) {
            table[n-1] = table[n];
        }
        next_ptr--;
        return;
M
Mark Adler 已提交
257
    }
M
Mark Adler 已提交
258
    ptr = opaque; /* just to make some compilers happy */
M
Mark Adler 已提交
259
    Assert(0, "zcfree: ptr not found");
M
Mark Adler 已提交
260
}
M
Mark Adler 已提交
261

M
Mark Adler 已提交
262 263
#endif /* __TURBOC__ */

M
Mark Adler 已提交
264

M
Mark Adler 已提交
265
#ifdef M_I86
M
Mark Adler 已提交
266
/* Microsoft C in 16-bit mode */
M
Mark Adler 已提交
267

M
Mark Adler 已提交
268
#  define MY_ZCALLOC
M
Mark Adler 已提交
269

M
Mark Adler 已提交
270
#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
M
Mark Adler 已提交
271 272 273 274
#  define _halloc  halloc
#  define _hfree   hfree
#endif

M
Mark Adler 已提交
275
voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
M
Mark Adler 已提交
276
{
M
Mark Adler 已提交
277
    if (opaque) opaque = 0; /* to make compiler happy */
M
Mark Adler 已提交
278 279 280
    return _halloc((long)items, size);
}

M
Mark Adler 已提交
281
void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
M
Mark Adler 已提交
282
{
M
Mark Adler 已提交
283
    if (opaque) opaque = 0; /* to make compiler happy */
M
Mark Adler 已提交
284
    _hfree(ptr);
M
Mark Adler 已提交
285 286
}

M
Mark Adler 已提交
287 288 289
#endif /* M_I86 */

#endif /* SYS16BIT */
M
Mark Adler 已提交
290 291


M
Mark Adler 已提交
292 293
#ifndef MY_ZCALLOC /* Any system without a special alloc function */

M
Mark Adler 已提交
294
#ifndef STDC
M
Mark Adler 已提交
295
extern voidp  malloc OF((uInt size));
M
Mark Adler 已提交
296 297
extern voidp  calloc OF((uInt items, uInt size));
extern void   free   OF((voidpf ptr));
M
Mark Adler 已提交
298
#endif
M
Mark Adler 已提交
299

M
Mark Adler 已提交
300
voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
M
Mark Adler 已提交
301
    voidpf opaque;
M
Mark Adler 已提交
302 303 304
    unsigned items;
    unsigned size;
{
M
Mark Adler 已提交
305
    if (opaque) items += size - size; /* make compiler happy */
M
Mark Adler 已提交
306 307
    return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
                              (voidpf)calloc(items, size);
M
Mark Adler 已提交
308 309
}

M
Mark Adler 已提交
310
void ZLIB_INTERNAL zcfree (opaque, ptr)
M
Mark Adler 已提交
311 312
    voidpf opaque;
    voidpf ptr;
M
Mark Adler 已提交
313 314
{
    free(ptr);
M
Mark Adler 已提交
315
    if (opaque) return; /* make compiler happy */
M
Mark Adler 已提交
316 317
}

M
Mark Adler 已提交
318
#endif /* MY_ZCALLOC */