zutil.c 8.2 KB
Newer Older
D
duke 已提交
1 2 3 4 5
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
6
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
7
 * particular file as subject to the "Classpath" exception as provided
8
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
9 10 11 12 13 14 15 16 17 18 19
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
20 21 22
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
23 24
 */

25
/* zutil.c -- target dependent utility functions for the compression library
C
coffeys 已提交
26
 * Copyright (C) 1995-2017 Jean-loup Gailly
D
duke 已提交
27 28 29
 * For conditions of distribution and use, see copyright notice in zlib.h
 */

30 31
/* @(#) $Id$ */

D
duke 已提交
32
#include "zutil.h"
C
coffeys 已提交
33

34 35 36
#ifndef Z_SOLO
#  include "gzguts.h"
#endif
D
duke 已提交
37

38
z_const char * const z_errmsg[10] = {
C
coffeys 已提交
39 40 41 42 43 44 45 46 47 48 49
    (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 *)""
};
D
duke 已提交
50 51 52 53 54 55 56


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

57 58 59 60 61
uLong ZEXPORT zlibCompileFlags()
{
    uLong flags;

    flags = 0;
62
    switch ((int)(sizeof(uInt))) {
63 64 65 66 67
    case 2:     break;
    case 4:     flags += 1;     break;
    case 8:     flags += 2;     break;
    default:    flags += 3;
    }
68
    switch ((int)(sizeof(uLong))) {
69 70 71 72 73
    case 2:     break;
    case 4:     flags += 1 << 2;        break;
    case 8:     flags += 2 << 2;        break;
    default:    flags += 3 << 2;
    }
74
    switch ((int)(sizeof(voidpf))) {
75 76 77 78 79
    case 2:     break;
    case 4:     flags += 1 << 4;        break;
    case 8:     flags += 2 << 4;        break;
    default:    flags += 3 << 4;
    }
80
    switch ((int)(sizeof(z_off_t))) {
81 82 83 84 85
    case 2:     break;
    case 4:     flags += 1 << 6;        break;
    case 8:     flags += 2 << 6;        break;
    default:    flags += 3 << 6;
    }
C
coffeys 已提交
86
#ifdef ZLIB_DEBUG
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
    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
113
#if defined(STDC) || defined(Z_HAVE_STDARG_H)
114
#  ifdef NO_vsnprintf
115
    flags += 1L << 25;
116
#    ifdef HAS_vsprintf_void
117
    flags += 1L << 26;
118 119 120
#    endif
#  else
#    ifdef HAS_vsnprintf_void
121
    flags += 1L << 26;
122 123 124
#    endif
#  endif
#else
125
    flags += 1L << 24;
126
#  ifdef NO_snprintf
127
    flags += 1L << 25;
128
#    ifdef HAS_sprintf_void
129
    flags += 1L << 26;
130 131 132
#    endif
#  else
#    ifdef HAS_snprintf_void
133
    flags += 1L << 26;
134 135 136 137 138 139
#    endif
#  endif
#endif
    return flags;
}

C
coffeys 已提交
140 141
#ifdef ZLIB_DEBUG
#include <stdlib.h>
D
duke 已提交
142 143 144
#  ifndef verbose
#    define verbose 0
#  endif
145
int ZLIB_INTERNAL z_verbose = verbose;
D
duke 已提交
146

147
void ZLIB_INTERNAL z_error (m)
D
duke 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    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);
}

164 165 166 167 168 169 170
#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
D
duke 已提交
171 172 173

#ifndef HAVE_MEMCPY

174
void ZLIB_INTERNAL zmemcpy(dest, source, len)
D
duke 已提交
175 176 177 178 179 180 181 182 183 184
    Bytef* dest;
    const Bytef* source;
    uInt  len;
{
    if (len == 0) return;
    do {
        *dest++ = *source++; /* ??? to be unrolled */
    } while (--len != 0);
}

185
int ZLIB_INTERNAL zmemcmp(s1, s2, len)
D
duke 已提交
186 187 188 189 190 191 192 193 194 195 196 197
    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;
}

198
void ZLIB_INTERNAL zmemzero(dest, len)
D
duke 已提交
199 200 201 202 203 204 205 206 207 208
    Bytef* dest;
    uInt  len;
{
    if (len == 0) return;
    do {
        *dest++ = 0;  /* ??? to be unrolled */
    } while (--len != 0);
}
#endif

209
#ifndef Z_SOLO
210 211 212

#ifdef SYS16BIT

D
duke 已提交
213
#ifdef __TURBOC__
214 215
/* Turbo C in 16-bit mode */

D
duke 已提交
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
#  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.
 */

242
voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
D
duke 已提交
243
{
C
coffeys 已提交
244
    voidpf buf;
D
duke 已提交
245 246
    ulg bsize = (ulg)items*size;

C
coffeys 已提交
247 248
    (void)opaque;

D
duke 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    /* 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;
}

268
void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
D
duke 已提交
269 270
{
    int n;
C
coffeys 已提交
271 272 273

    (void)opaque;

D
duke 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    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");
}
291

D
duke 已提交
292 293 294
#endif /* __TURBOC__ */


295
#ifdef M_I86
D
duke 已提交
296 297 298 299 300 301 302 303 304
/* Microsoft C in 16-bit mode */

#  define MY_ZCALLOC

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

305
voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
D
duke 已提交
306
{
C
coffeys 已提交
307
    (void)opaque;
D
duke 已提交
308 309 310
    return _halloc((long)items, size);
}

311
void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
D
duke 已提交
312
{
C
coffeys 已提交
313
    (void)opaque;
D
duke 已提交
314 315 316
    _hfree(ptr);
}

317 318 319
#endif /* M_I86 */

#endif /* SYS16BIT */
D
duke 已提交
320 321 322 323 324


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

#ifndef STDC
325
extern voidp  malloc OF((uInt size));
D
duke 已提交
326 327 328 329
extern voidp  calloc OF((uInt items, uInt size));
extern void   free   OF((voidpf ptr));
#endif

330
voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
D
duke 已提交
331 332 333 334
    voidpf opaque;
    unsigned items;
    unsigned size;
{
C
coffeys 已提交
335
    (void)opaque;
336 337
    return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
                              (voidpf)calloc(items, size);
D
duke 已提交
338 339
}

340
void ZLIB_INTERNAL zcfree (opaque, ptr)
D
duke 已提交
341 342 343
    voidpf opaque;
    voidpf ptr;
{
C
coffeys 已提交
344
    (void)opaque;
D
duke 已提交
345 346 347 348
    free(ptr);
}

#endif /* MY_ZCALLOC */
349 350

#endif /* !Z_SOLO */