zutil.c 7.2 KB
Newer Older
M
Mark Adler 已提交
1
/* zutil.c -- target dependent utility functions for the compression library
M
Mark Adler 已提交
2
 * Copyright (C) 1995-2017 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

#include "zutil.h"
9 10 11
#ifndef Z_SOLO
#  include "gzguts.h"
#endif
M
Mark Adler 已提交
12

13
z_const char * const z_errmsg[10] = {
M
Mark Adler 已提交
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 *)""
};
M
Mark Adler 已提交
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
    case 2:     break;
    case 4:     flags += 1 << 6;        break;
    case 8:     flags += 2 << 6;        break;
    default:    flags += 3 << 6;
    }
61
#ifdef ZLIB_DEBUG
M
Mark Adler 已提交
62 63
    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
#endif
88 89 90 91 92 93 94 95 96 97 98
#if defined(STDC) || defined(Z_HAVE_STDARG_H)
#  ifdef NO_vsnprintf
    flags += 1L << 25;
#    ifdef HAS_vsprintf_void
    flags += 1L << 26;
#    endif
#  else
#    ifdef HAS_vsnprintf_void
    flags += 1L << 26;
#    endif
#  endif
99
#else
100 101 102 103 104 105 106 107 108 109 110
    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
111
#endif
112
    return flags;
M
Mark Adler 已提交
113 114
}

115
#ifdef ZLIB_DEBUG
116
#include <stdlib.h>
M
Mark Adler 已提交
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);
}

S
shuaiyutao 已提交
139 140
#if defined(_WIN32_WCE) && _WIN32_WCE < 0x800
    /* The older Microsoft C Run-Time Library for Windows CE doesn't have
M
Mark Adler 已提交
141 142 143
     * 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

184
#ifndef Z_SOLO
M
Mark Adler 已提交
185 186 187

#ifdef SYS16BIT

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

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

/* 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 已提交
205 206
    voidpf org_ptr;
    voidpf new_ptr;
M
Mark Adler 已提交
207 208 209 210 211 212 213 214 215 216
} 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 已提交
217
voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
M
Mark Adler 已提交
218
{
219
    voidpf buf;
M
Mark Adler 已提交
220 221
    ulg bsize = (ulg)items*size;

222 223
    (void)opaque;

M
Mark Adler 已提交
224 225 226 227
    /* 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 已提交
228 229
        buf = farmalloc(bsize);
        if (*(ush*)&buf != 0) return buf;
M
Mark Adler 已提交
230
    } else {
M
Mark Adler 已提交
231
        buf = farmalloc(bsize + 16L);
M
Mark Adler 已提交
232 233 234 235 236 237 238 239 240 241 242
    }
    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 已提交
243
void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
M
Mark Adler 已提交
244 245
{
    int n;
246 247 248

    (void)opaque;

M
Mark Adler 已提交
249
    if (*(ush*)&ptr != 0) { /* object < 64K */
M
Mark Adler 已提交
250 251
        farfree(ptr);
        return;
M
Mark Adler 已提交
252 253 254
    }
    /* Find the original pointer */
    for (n = 0; n < next_ptr; n++) {
M
Mark Adler 已提交
255 256 257 258 259 260 261 262
        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 已提交
263
    }
M
Mark Adler 已提交
264
    Assert(0, "zcfree: ptr not found");
M
Mark Adler 已提交
265
}
M
Mark Adler 已提交
266

M
Mark Adler 已提交
267 268
#endif /* __TURBOC__ */

M
Mark Adler 已提交
269

M
Mark Adler 已提交
270
#ifdef M_I86
M
Mark Adler 已提交
271
/* Microsoft C in 16-bit mode */
M
Mark Adler 已提交
272

M
Mark Adler 已提交
273
#  define MY_ZCALLOC
M
Mark Adler 已提交
274

M
Mark Adler 已提交
275
#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
M
Mark Adler 已提交
276 277 278 279
#  define _halloc  halloc
#  define _hfree   hfree
#endif

M
Mark Adler 已提交
280
voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
M
Mark Adler 已提交
281
{
282
    (void)opaque;
M
Mark Adler 已提交
283 284 285
    return _halloc((long)items, size);
}

M
Mark Adler 已提交
286
void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
M
Mark Adler 已提交
287
{
288
    (void)opaque;
M
Mark Adler 已提交
289
    _hfree(ptr);
M
Mark Adler 已提交
290 291
}

M
Mark Adler 已提交
292 293 294
#endif /* M_I86 */

#endif /* SYS16BIT */
M
Mark Adler 已提交
295 296


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

M
Mark Adler 已提交
299
#ifndef STDC
M
Mark Adler 已提交
300
extern voidp  malloc OF((uInt size));
M
Mark Adler 已提交
301 302
extern voidp  calloc OF((uInt items, uInt size));
extern void   free   OF((voidpf ptr));
M
Mark Adler 已提交
303
#endif
M
Mark Adler 已提交
304

M
Mark Adler 已提交
305
voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
M
Mark Adler 已提交
306
    voidpf opaque;
M
Mark Adler 已提交
307 308 309
    unsigned items;
    unsigned size;
{
310
    (void)opaque;
M
Mark Adler 已提交
311 312
    return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
                              (voidpf)calloc(items, size);
M
Mark Adler 已提交
313 314
}

M
Mark Adler 已提交
315
void ZLIB_INTERNAL zcfree (opaque, ptr)
M
Mark Adler 已提交
316 317
    voidpf opaque;
    voidpf ptr;
M
Mark Adler 已提交
318
{
319
    (void)opaque;
M
Mark Adler 已提交
320 321 322
    free(ptr);
}

M
Mark Adler 已提交
323
#endif /* MY_ZCALLOC */
324 325

#endif /* !Z_SOLO */