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

M
Mark Adler 已提交
6
/* $Id: zutil.c,v 1.12 1996/01/30 21:59:29 me Exp $ */
M
Mark Adler 已提交
7 8 9 10 11

#include <stdio.h>

#include "zutil.h"

M
Mark Adler 已提交
12 13
struct internal_state      {int dummy;}; /* for buggy compilers */

M
Mark Adler 已提交
14
#ifndef STDC
M
Mark Adler 已提交
15
extern void exit OF((int));
M
Mark Adler 已提交
16
#endif
M
Mark Adler 已提交
17

M
Mark Adler 已提交
18 19 20 21 22 23 24 25 26 27 28 29
const char *zlib_version = ZLIB_VERSION;

const char *z_errmsg[10] = {
"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 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42
""};


void z_error (m)
    char *m;
{
    fprintf(stderr, "%s\n", m);
    exit(1);
}

#ifndef HAVE_MEMCPY

void zmemcpy(dest, source, len)
M
Mark Adler 已提交
43 44
    Bytef* dest;
    Bytef* source;
M
Mark Adler 已提交
45 46 47 48
    uInt  len;
{
    if (len == 0) return;
    do {
M
Mark Adler 已提交
49
        *dest++ = *source++; /* ??? to be unrolled */
M
Mark Adler 已提交
50 51 52 53
    } while (--len != 0);
}

void zmemzero(dest, len)
M
Mark Adler 已提交
54
    Bytef* dest;
M
Mark Adler 已提交
55 56 57 58
    uInt  len;
{
    if (len == 0) return;
    do {
M
Mark Adler 已提交
59
        *dest++ = 0;  /* ??? to be unrolled */
M
Mark Adler 已提交
60 61 62 63
    } while (--len != 0);
}
#endif

M
Mark Adler 已提交
64
#ifdef __TURBOC__
M
Mark Adler 已提交
65 66 67
#if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
/* Small and medium model in Turbo C are for now limited to near allocation
 * with reduced MAX_WBITS and MAX_MEM_LEVEL
M
Mark Adler 已提交
68
 */
M
Mark Adler 已提交
69
#  define MY_ZCALLOC
M
Mark Adler 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82

/* 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 已提交
83 84
    voidpf org_ptr;
    voidpf new_ptr;
M
Mark Adler 已提交
85 86 87 88 89 90 91 92 93 94
} 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 已提交
95
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
M
Mark Adler 已提交
96
{
M
Mark Adler 已提交
97
    voidpf buf = opaque; /* just to make some compilers happy */
M
Mark Adler 已提交
98 99
    ulg bsize = (ulg)items*size;

M
Mark Adler 已提交
100 101 102 103
    /* 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 已提交
104 105
        buf = farmalloc(bsize);
        if (*(ush*)&buf != 0) return buf;
M
Mark Adler 已提交
106
    } else {
M
Mark Adler 已提交
107
        buf = farmalloc(bsize + 16L);
M
Mark Adler 已提交
108 109 110 111 112 113 114 115 116 117 118
    }
    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 已提交
119
void  zcfree (voidpf opaque, voidpf ptr)
M
Mark Adler 已提交
120 121 122
{
    int n;
    if (*(ush*)&ptr != 0) { /* object < 64K */
M
Mark Adler 已提交
123 124
        farfree(ptr);
        return;
M
Mark Adler 已提交
125 126 127
    }
    /* Find the original pointer */
    for (n = 0; n < next_ptr; n++) {
M
Mark Adler 已提交
128 129 130 131 132 133 134 135
        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 已提交
136
    }
M
Mark Adler 已提交
137
    ptr = opaque; /* just to make some compilers happy */
M
Mark Adler 已提交
138 139
    z_error("zcfree: ptr not found");
}
M
Mark Adler 已提交
140
#endif
M
Mark Adler 已提交
141 142
#endif /* __TURBOC__ */

M
Mark Adler 已提交
143

M
Mark Adler 已提交
144 145
#if defined(M_I86) && !(defined(__WATCOMC__) && defined(__386__))
/* Microsoft C */
M
Mark Adler 已提交
146

M
Mark Adler 已提交
147
#  define MY_ZCALLOC
M
Mark Adler 已提交
148 149 150 151 152 153

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

M
Mark Adler 已提交
154
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
M
Mark Adler 已提交
155
{
M
Mark Adler 已提交
156
    if (opaque) opaque = 0; /* to make compiler happy */
M
Mark Adler 已提交
157 158 159
    return _halloc((long)items, size);
}

M
Mark Adler 已提交
160
void  zcfree (voidpf opaque, voidpf ptr)
M
Mark Adler 已提交
161
{
M
Mark Adler 已提交
162
    if (opaque) opaque = 0; /* to make compiler happy */
M
Mark Adler 已提交
163
    _hfree(ptr);
M
Mark Adler 已提交
164 165
}

M
Mark Adler 已提交
166
#endif /* MSC */
M
Mark Adler 已提交
167 168


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

M
Mark Adler 已提交
171
#ifndef STDC
M
Mark Adler 已提交
172 173
extern voidp  calloc OF((uInt items, uInt size));
extern void   free   OF((voidpf ptr));
M
Mark Adler 已提交
174
#endif
M
Mark Adler 已提交
175

M
Mark Adler 已提交
176 177
voidpf zcalloc (opaque, items, size)
    voidpf opaque;
M
Mark Adler 已提交
178 179 180
    unsigned items;
    unsigned size;
{
M
Mark Adler 已提交
181
    if (opaque) items += size - size; /* make compiler happy */
M
Mark Adler 已提交
182
    return (voidpf)calloc(items, size);
M
Mark Adler 已提交
183 184 185
}

void  zcfree (opaque, ptr)
M
Mark Adler 已提交
186 187
    voidpf opaque;
    voidpf ptr;
M
Mark Adler 已提交
188 189
{
    free(ptr);
M
Mark Adler 已提交
190
    if (opaque) return; /* make compiler happy */
M
Mark Adler 已提交
191 192
}

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