os/ossrv/compressionlibs/ziplib/test/oldezlib/Zlib/zutil.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* zutil.c -- target dependent utility functions for the compression library
sl@0
     2
 * Copyright (C) 1995-1998 Jean-loup Gailly.
sl@0
     3
 * For conditions of distribution and use, see copyright notice in zlib.h 
sl@0
     4
 */
sl@0
     5
sl@0
     6
/* @(#) $Id$ */
sl@0
     7
sl@0
     8
#include "zutil.h"
sl@0
     9
sl@0
    10
struct internal_state      {int dummy;}; /* for buggy compilers */
sl@0
    11
sl@0
    12
#ifndef STDC
sl@0
    13
extern void exit OF((int));
sl@0
    14
#endif
sl@0
    15
sl@0
    16
const char *z_errmsg[10] = {
sl@0
    17
"need dictionary",     /* Z_NEED_DICT       2  */
sl@0
    18
"stream end",          /* Z_STREAM_END      1  */
sl@0
    19
"",                    /* Z_OK              0  */
sl@0
    20
"file error",          /* Z_ERRNO         (-1) */
sl@0
    21
"stream error",        /* Z_STREAM_ERROR  (-2) */
sl@0
    22
"data error",          /* Z_DATA_ERROR    (-3) */
sl@0
    23
"insufficient memory", /* Z_MEM_ERROR     (-4) */
sl@0
    24
"buffer error",        /* Z_BUF_ERROR     (-5) */
sl@0
    25
"incompatible version",/* Z_VERSION_ERROR (-6) */
sl@0
    26
""};
sl@0
    27
sl@0
    28
sl@0
    29
const char * ZEXPORT zlibVersion()
sl@0
    30
{
sl@0
    31
    return ZLIB_VERSION;
sl@0
    32
}
sl@0
    33
sl@0
    34
#ifdef DEBUG
sl@0
    35
sl@0
    36
#  ifndef verbose
sl@0
    37
#    define verbose 0
sl@0
    38
#  endif
sl@0
    39
int z_verbose = verbose;
sl@0
    40
sl@0
    41
void z_error (char *m)
sl@0
    42
{
sl@0
    43
    fprintf(stderr, "%s\n", m);
sl@0
    44
    exit(1);
sl@0
    45
}
sl@0
    46
#endif
sl@0
    47
sl@0
    48
/* exported to allow conversion of error code to string for compress() and
sl@0
    49
 * uncompress()
sl@0
    50
 */
sl@0
    51
const char * ZEXPORT zError(int err)
sl@0
    52
{
sl@0
    53
    return ERR_MSG(err);
sl@0
    54
}
sl@0
    55
sl@0
    56
sl@0
    57
#ifndef HAVE_MEMCPY
sl@0
    58
sl@0
    59
void zmemcpy(Bytef* dest, const Bytef* source, uInt len)
sl@0
    60
{
sl@0
    61
    if (len == 0) return;
sl@0
    62
    do {
sl@0
    63
        *dest++ = *source++; /* ??? to be unrolled */
sl@0
    64
    } while (--len != 0);
sl@0
    65
}
sl@0
    66
sl@0
    67
int zmemcmp(const Bytef* s1, const Bytef* s2, uInt len)
sl@0
    68
{
sl@0
    69
    uInt j;
sl@0
    70
sl@0
    71
    for (j = 0; j < len; j++) {
sl@0
    72
        if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
sl@0
    73
    }
sl@0
    74
    return 0;
sl@0
    75
}
sl@0
    76
sl@0
    77
void zmemzero(Bytef* dest, uInt len)
sl@0
    78
{
sl@0
    79
    if (len == 0) return;
sl@0
    80
    do {
sl@0
    81
        *dest++ = 0;  /* ??? to be unrolled */
sl@0
    82
    } while (--len != 0);
sl@0
    83
}
sl@0
    84
#endif
sl@0
    85
sl@0
    86
#ifdef __TURBOC__
sl@0
    87
#if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
sl@0
    88
/* Small and medium model in Turbo C are for now limited to near allocation
sl@0
    89
 * with reduced MAX_WBITS and MAX_MEM_LEVEL
sl@0
    90
 */
sl@0
    91
#  define MY_ZCALLOC
sl@0
    92
sl@0
    93
/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
sl@0
    94
 * and farmalloc(64K) returns a pointer with an offset of 8, so we
sl@0
    95
 * must fix the pointer. Warning: the pointer must be put back to its
sl@0
    96
 * original form in order to free it, use zcfree().
sl@0
    97
 */
sl@0
    98
sl@0
    99
#define MAX_PTR 10
sl@0
   100
/* 10*64K = 640K */
sl@0
   101
sl@0
   102
local int next_ptr = 0;
sl@0
   103
sl@0
   104
typedef struct ptr_table_s {
sl@0
   105
    voidpf org_ptr;
sl@0
   106
    voidpf new_ptr;
sl@0
   107
} ptr_table;
sl@0
   108
sl@0
   109
local ptr_table table[MAX_PTR];
sl@0
   110
/* This table is used to remember the original form of pointers
sl@0
   111
 * to large buffers (64K). Such pointers are normalized with a zero offset.
sl@0
   112
 * Since MSDOS is not a preemptive multitasking OS, this table is not
sl@0
   113
 * protected from concurrent access. This hack doesn't work anyway on
sl@0
   114
 * a protected system like OS/2. Use Microsoft C instead.
sl@0
   115
 */
sl@0
   116
sl@0
   117
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
sl@0
   118
{
sl@0
   119
    voidpf buf = opaque; /* just to make some compilers happy */
sl@0
   120
    ulg bsize = (ulg)items*size;
sl@0
   121
sl@0
   122
    /* If we allocate less than 65520 bytes, we assume that farmalloc
sl@0
   123
     * will return a usable pointer which doesn't have to be normalized.
sl@0
   124
     */
sl@0
   125
    if (bsize < 65520L) {
sl@0
   126
        buf = farmalloc(bsize);
sl@0
   127
        if (*(ush*)&buf != 0) return buf;
sl@0
   128
    } else {
sl@0
   129
        buf = farmalloc(bsize + 16L);
sl@0
   130
    }
sl@0
   131
    if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
sl@0
   132
    table[next_ptr].org_ptr = buf;
sl@0
   133
sl@0
   134
    /* Normalize the pointer to seg:0 */
sl@0
   135
    *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
sl@0
   136
    *(ush*)&buf = 0;
sl@0
   137
    table[next_ptr++].new_ptr = buf;
sl@0
   138
    return buf;
sl@0
   139
}
sl@0
   140
sl@0
   141
void  zcfree (voidpf opaque, voidpf ptr)
sl@0
   142
{
sl@0
   143
    int n;
sl@0
   144
    if (*(ush*)&ptr != 0) { /* object < 64K */
sl@0
   145
        farfree(ptr);
sl@0
   146
        return;
sl@0
   147
    }
sl@0
   148
    /* Find the original pointer */
sl@0
   149
    for (n = 0; n < next_ptr; n++) {
sl@0
   150
        if (ptr != table[n].new_ptr) continue;
sl@0
   151
sl@0
   152
        farfree(table[n].org_ptr);
sl@0
   153
        while (++n < next_ptr) {
sl@0
   154
            table[n-1] = table[n];
sl@0
   155
        }
sl@0
   156
        next_ptr--;
sl@0
   157
        return;
sl@0
   158
    }
sl@0
   159
    ptr = opaque; /* just to make some compilers happy */
sl@0
   160
    Assert(0, "zcfree: ptr not found");
sl@0
   161
}
sl@0
   162
#endif
sl@0
   163
#endif /* __TURBOC__ */
sl@0
   164
sl@0
   165
sl@0
   166
#if defined(M_I86) && !defined(__32BIT__)
sl@0
   167
/* Microsoft C in 16-bit mode */
sl@0
   168
sl@0
   169
#  define MY_ZCALLOC
sl@0
   170
sl@0
   171
#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
sl@0
   172
#  define _halloc  halloc
sl@0
   173
#  define _hfree   hfree
sl@0
   174
#endif
sl@0
   175
sl@0
   176
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
sl@0
   177
{
sl@0
   178
    if (opaque) opaque = 0; /* to make compiler happy */
sl@0
   179
    return _halloc((long)items, size);
sl@0
   180
}
sl@0
   181
sl@0
   182
void  zcfree (voidpf opaque, voidpf ptr)
sl@0
   183
{
sl@0
   184
    if (opaque) opaque = 0; /* to make compiler happy */
sl@0
   185
    _hfree(ptr);
sl@0
   186
}
sl@0
   187
sl@0
   188
#endif /* MSC */
sl@0
   189
sl@0
   190
sl@0
   191
#ifndef MY_ZCALLOC /* Any system without a special alloc function */
sl@0
   192
sl@0
   193
#ifndef STDC
sl@0
   194
extern voidp  calloc OF((uInt items, uInt size));
sl@0
   195
extern void   free   OF((voidpf ptr));
sl@0
   196
#endif
sl@0
   197
sl@0
   198
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
sl@0
   199
{
sl@0
   200
    if (opaque) items += size - size; /* make compiler happy */
sl@0
   201
    return (voidpf)calloc(items, size);
sl@0
   202
}
sl@0
   203
sl@0
   204
void  zcfree (voidpf opaque,voidpf ptr)
sl@0
   205
{
sl@0
   206
    free(ptr);
sl@0
   207
    if (opaque) return; /* make compiler happy */
sl@0
   208
}
sl@0
   209
sl@0
   210
#endif /* MY_ZCALLOC */