modules/zlib/src/zutil.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/modules/zlib/src/zutil.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,324 @@
     1.4 +/* zutil.c -- target dependent utility functions for the compression library
     1.5 + * Copyright (C) 1995-2005, 2010, 2011, 2012 Jean-loup Gailly.
     1.6 + * For conditions of distribution and use, see copyright notice in zlib.h
     1.7 + */
     1.8 +
     1.9 +/* @(#) $Id$ */
    1.10 +
    1.11 +#include "zutil.h"
    1.12 +#ifndef Z_SOLO
    1.13 +#  include "gzguts.h"
    1.14 +#endif
    1.15 +
    1.16 +#ifndef NO_DUMMY_DECL
    1.17 +struct internal_state      {int dummy;}; /* for buggy compilers */
    1.18 +#endif
    1.19 +
    1.20 +z_const char * const z_errmsg[10] = {
    1.21 +"need dictionary",     /* Z_NEED_DICT       2  */
    1.22 +"stream end",          /* Z_STREAM_END      1  */
    1.23 +"",                    /* Z_OK              0  */
    1.24 +"file error",          /* Z_ERRNO         (-1) */
    1.25 +"stream error",        /* Z_STREAM_ERROR  (-2) */
    1.26 +"data error",          /* Z_DATA_ERROR    (-3) */
    1.27 +"insufficient memory", /* Z_MEM_ERROR     (-4) */
    1.28 +"buffer error",        /* Z_BUF_ERROR     (-5) */
    1.29 +"incompatible version",/* Z_VERSION_ERROR (-6) */
    1.30 +""};
    1.31 +
    1.32 +
    1.33 +const char * ZEXPORT zlibVersion()
    1.34 +{
    1.35 +    return ZLIB_VERSION;
    1.36 +}
    1.37 +
    1.38 +uLong ZEXPORT zlibCompileFlags()
    1.39 +{
    1.40 +    uLong flags;
    1.41 +
    1.42 +    flags = 0;
    1.43 +    switch ((int)(sizeof(uInt))) {
    1.44 +    case 2:     break;
    1.45 +    case 4:     flags += 1;     break;
    1.46 +    case 8:     flags += 2;     break;
    1.47 +    default:    flags += 3;
    1.48 +    }
    1.49 +    switch ((int)(sizeof(uLong))) {
    1.50 +    case 2:     break;
    1.51 +    case 4:     flags += 1 << 2;        break;
    1.52 +    case 8:     flags += 2 << 2;        break;
    1.53 +    default:    flags += 3 << 2;
    1.54 +    }
    1.55 +    switch ((int)(sizeof(voidpf))) {
    1.56 +    case 2:     break;
    1.57 +    case 4:     flags += 1 << 4;        break;
    1.58 +    case 8:     flags += 2 << 4;        break;
    1.59 +    default:    flags += 3 << 4;
    1.60 +    }
    1.61 +    switch ((int)(sizeof(z_off_t))) {
    1.62 +    case 2:     break;
    1.63 +    case 4:     flags += 1 << 6;        break;
    1.64 +    case 8:     flags += 2 << 6;        break;
    1.65 +    default:    flags += 3 << 6;
    1.66 +    }
    1.67 +#ifdef DEBUG
    1.68 +    flags += 1 << 8;
    1.69 +#endif
    1.70 +#if defined(ASMV) || defined(ASMINF)
    1.71 +    flags += 1 << 9;
    1.72 +#endif
    1.73 +#ifdef ZLIB_WINAPI
    1.74 +    flags += 1 << 10;
    1.75 +#endif
    1.76 +#ifdef BUILDFIXED
    1.77 +    flags += 1 << 12;
    1.78 +#endif
    1.79 +#ifdef DYNAMIC_CRC_TABLE
    1.80 +    flags += 1 << 13;
    1.81 +#endif
    1.82 +#ifdef NO_GZCOMPRESS
    1.83 +    flags += 1L << 16;
    1.84 +#endif
    1.85 +#ifdef NO_GZIP
    1.86 +    flags += 1L << 17;
    1.87 +#endif
    1.88 +#ifdef PKZIP_BUG_WORKAROUND
    1.89 +    flags += 1L << 20;
    1.90 +#endif
    1.91 +#ifdef FASTEST
    1.92 +    flags += 1L << 21;
    1.93 +#endif
    1.94 +#if defined(STDC) || defined(Z_HAVE_STDARG_H)
    1.95 +#  ifdef NO_vsnprintf
    1.96 +    flags += 1L << 25;
    1.97 +#    ifdef HAS_vsprintf_void
    1.98 +    flags += 1L << 26;
    1.99 +#    endif
   1.100 +#  else
   1.101 +#    ifdef HAS_vsnprintf_void
   1.102 +    flags += 1L << 26;
   1.103 +#    endif
   1.104 +#  endif
   1.105 +#else
   1.106 +    flags += 1L << 24;
   1.107 +#  ifdef NO_snprintf
   1.108 +    flags += 1L << 25;
   1.109 +#    ifdef HAS_sprintf_void
   1.110 +    flags += 1L << 26;
   1.111 +#    endif
   1.112 +#  else
   1.113 +#    ifdef HAS_snprintf_void
   1.114 +    flags += 1L << 26;
   1.115 +#    endif
   1.116 +#  endif
   1.117 +#endif
   1.118 +    return flags;
   1.119 +}
   1.120 +
   1.121 +#ifdef DEBUG
   1.122 +
   1.123 +#  ifndef verbose
   1.124 +#    define verbose 0
   1.125 +#  endif
   1.126 +int ZLIB_INTERNAL z_verbose = verbose;
   1.127 +
   1.128 +void ZLIB_INTERNAL z_error (m)
   1.129 +    char *m;
   1.130 +{
   1.131 +    fprintf(stderr, "%s\n", m);
   1.132 +    exit(1);
   1.133 +}
   1.134 +#endif
   1.135 +
   1.136 +/* exported to allow conversion of error code to string for compress() and
   1.137 + * uncompress()
   1.138 + */
   1.139 +const char * ZEXPORT zError(err)
   1.140 +    int err;
   1.141 +{
   1.142 +    return ERR_MSG(err);
   1.143 +}
   1.144 +
   1.145 +#if defined(_WIN32_WCE)
   1.146 +    /* The Microsoft C Run-Time Library for Windows CE doesn't have
   1.147 +     * errno.  We define it as a global variable to simplify porting.
   1.148 +     * Its value is always 0 and should not be used.
   1.149 +     */
   1.150 +    int errno = 0;
   1.151 +#endif
   1.152 +
   1.153 +#ifndef HAVE_MEMCPY
   1.154 +
   1.155 +void ZLIB_INTERNAL zmemcpy(dest, source, len)
   1.156 +    Bytef* dest;
   1.157 +    const Bytef* source;
   1.158 +    uInt  len;
   1.159 +{
   1.160 +    if (len == 0) return;
   1.161 +    do {
   1.162 +        *dest++ = *source++; /* ??? to be unrolled */
   1.163 +    } while (--len != 0);
   1.164 +}
   1.165 +
   1.166 +int ZLIB_INTERNAL zmemcmp(s1, s2, len)
   1.167 +    const Bytef* s1;
   1.168 +    const Bytef* s2;
   1.169 +    uInt  len;
   1.170 +{
   1.171 +    uInt j;
   1.172 +
   1.173 +    for (j = 0; j < len; j++) {
   1.174 +        if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
   1.175 +    }
   1.176 +    return 0;
   1.177 +}
   1.178 +
   1.179 +void ZLIB_INTERNAL zmemzero(dest, len)
   1.180 +    Bytef* dest;
   1.181 +    uInt  len;
   1.182 +{
   1.183 +    if (len == 0) return;
   1.184 +    do {
   1.185 +        *dest++ = 0;  /* ??? to be unrolled */
   1.186 +    } while (--len != 0);
   1.187 +}
   1.188 +#endif
   1.189 +
   1.190 +#ifndef Z_SOLO
   1.191 +
   1.192 +#ifdef SYS16BIT
   1.193 +
   1.194 +#ifdef __TURBOC__
   1.195 +/* Turbo C in 16-bit mode */
   1.196 +
   1.197 +#  define MY_ZCALLOC
   1.198 +
   1.199 +/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
   1.200 + * and farmalloc(64K) returns a pointer with an offset of 8, so we
   1.201 + * must fix the pointer. Warning: the pointer must be put back to its
   1.202 + * original form in order to free it, use zcfree().
   1.203 + */
   1.204 +
   1.205 +#define MAX_PTR 10
   1.206 +/* 10*64K = 640K */
   1.207 +
   1.208 +local int next_ptr = 0;
   1.209 +
   1.210 +typedef struct ptr_table_s {
   1.211 +    voidpf org_ptr;
   1.212 +    voidpf new_ptr;
   1.213 +} ptr_table;
   1.214 +
   1.215 +local ptr_table table[MAX_PTR];
   1.216 +/* This table is used to remember the original form of pointers
   1.217 + * to large buffers (64K). Such pointers are normalized with a zero offset.
   1.218 + * Since MSDOS is not a preemptive multitasking OS, this table is not
   1.219 + * protected from concurrent access. This hack doesn't work anyway on
   1.220 + * a protected system like OS/2. Use Microsoft C instead.
   1.221 + */
   1.222 +
   1.223 +voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
   1.224 +{
   1.225 +    voidpf buf = opaque; /* just to make some compilers happy */
   1.226 +    ulg bsize = (ulg)items*size;
   1.227 +
   1.228 +    /* If we allocate less than 65520 bytes, we assume that farmalloc
   1.229 +     * will return a usable pointer which doesn't have to be normalized.
   1.230 +     */
   1.231 +    if (bsize < 65520L) {
   1.232 +        buf = farmalloc(bsize);
   1.233 +        if (*(ush*)&buf != 0) return buf;
   1.234 +    } else {
   1.235 +        buf = farmalloc(bsize + 16L);
   1.236 +    }
   1.237 +    if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
   1.238 +    table[next_ptr].org_ptr = buf;
   1.239 +
   1.240 +    /* Normalize the pointer to seg:0 */
   1.241 +    *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
   1.242 +    *(ush*)&buf = 0;
   1.243 +    table[next_ptr++].new_ptr = buf;
   1.244 +    return buf;
   1.245 +}
   1.246 +
   1.247 +void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
   1.248 +{
   1.249 +    int n;
   1.250 +    if (*(ush*)&ptr != 0) { /* object < 64K */
   1.251 +        farfree(ptr);
   1.252 +        return;
   1.253 +    }
   1.254 +    /* Find the original pointer */
   1.255 +    for (n = 0; n < next_ptr; n++) {
   1.256 +        if (ptr != table[n].new_ptr) continue;
   1.257 +
   1.258 +        farfree(table[n].org_ptr);
   1.259 +        while (++n < next_ptr) {
   1.260 +            table[n-1] = table[n];
   1.261 +        }
   1.262 +        next_ptr--;
   1.263 +        return;
   1.264 +    }
   1.265 +    ptr = opaque; /* just to make some compilers happy */
   1.266 +    Assert(0, "zcfree: ptr not found");
   1.267 +}
   1.268 +
   1.269 +#endif /* __TURBOC__ */
   1.270 +
   1.271 +
   1.272 +#ifdef M_I86
   1.273 +/* Microsoft C in 16-bit mode */
   1.274 +
   1.275 +#  define MY_ZCALLOC
   1.276 +
   1.277 +#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
   1.278 +#  define _halloc  halloc
   1.279 +#  define _hfree   hfree
   1.280 +#endif
   1.281 +
   1.282 +voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
   1.283 +{
   1.284 +    if (opaque) opaque = 0; /* to make compiler happy */
   1.285 +    return _halloc((long)items, size);
   1.286 +}
   1.287 +
   1.288 +void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
   1.289 +{
   1.290 +    if (opaque) opaque = 0; /* to make compiler happy */
   1.291 +    _hfree(ptr);
   1.292 +}
   1.293 +
   1.294 +#endif /* M_I86 */
   1.295 +
   1.296 +#endif /* SYS16BIT */
   1.297 +
   1.298 +
   1.299 +#ifndef MY_ZCALLOC /* Any system without a special alloc function */
   1.300 +
   1.301 +#ifndef STDC
   1.302 +extern voidp  malloc OF((uInt size));
   1.303 +extern voidp  calloc OF((uInt items, uInt size));
   1.304 +extern void   free   OF((voidpf ptr));
   1.305 +#endif
   1.306 +
   1.307 +voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
   1.308 +    voidpf opaque;
   1.309 +    unsigned items;
   1.310 +    unsigned size;
   1.311 +{
   1.312 +    if (opaque) items += size - size; /* make compiler happy */
   1.313 +    return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
   1.314 +                              (voidpf)calloc(items, size);
   1.315 +}
   1.316 +
   1.317 +void ZLIB_INTERNAL zcfree (opaque, ptr)
   1.318 +    voidpf opaque;
   1.319 +    voidpf ptr;
   1.320 +{
   1.321 +    free(ptr);
   1.322 +    if (opaque) return; /* make compiler happy */
   1.323 +}
   1.324 +
   1.325 +#endif /* MY_ZCALLOC */
   1.326 +
   1.327 +#endif /* !Z_SOLO */

mercurial