zutil.c 4.5 KB
Newer Older
M
Mark Adler 已提交
1
/* zutil.c -- target dependent utility functions for the compression library
M
Mark Adler 已提交
2
 * Copyright (C) 1995 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.8 1995/05/03 17:27:12 jloup 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
char *zlib_version = ZLIB_VERSION;

char *z_errmsg[] = {
"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) */
M
Mark Adler 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40
""};


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

#ifndef HAVE_MEMCPY

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

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

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

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

M
Mark Adler 已提交
98 99 100 101
    /* 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 已提交
102
        buf = farmalloc(bsize);
M
Mark Adler 已提交
103 104 105
#ifdef DEBUG
        zmemzero(buf, (uInt)bsize);
#endif
M
Mark Adler 已提交
106
        if (*(ush*)&buf != 0) return buf;
M
Mark Adler 已提交
107
    } else {
M
Mark Adler 已提交
108
        buf = farmalloc(bsize + 16L);
M
Mark Adler 已提交
109 110 111 112 113 114 115 116
    }
    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;
M
Mark Adler 已提交
117 118 119 120
#ifdef DEBUG
    zmemzero(buf, (uInt)65535);
    ((uch*)buf)[65535] = 0;
#endif
M
Mark Adler 已提交
121 122 123
    return buf;
}

M
Mark Adler 已提交
124
void  zcfree (voidpf opaque, voidpf ptr)
M
Mark Adler 已提交
125 126 127
{
    int n;
    if (*(ush*)&ptr != 0) { /* object < 64K */
M
Mark Adler 已提交
128 129
        farfree(ptr);
        return;
M
Mark Adler 已提交
130 131 132
    }
    /* Find the original pointer */
    for (n = 0; n < next_ptr; n++) {
M
Mark Adler 已提交
133 134 135 136 137 138 139 140
        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 已提交
141
    }
M
Mark Adler 已提交
142
    ptr = opaque; /* just to make some compilers happy */
M
Mark Adler 已提交
143 144
    z_error("zcfree: ptr not found");
}
M
Mark Adler 已提交
145
#endif
M
Mark Adler 已提交
146 147
#endif /* __TURBOC__ */

M
Mark Adler 已提交
148

M
Mark Adler 已提交
149
#ifdef M_I86 /* Microsoft C */
M
Mark Adler 已提交
150

M
Mark Adler 已提交
151
#  define MY_ZCALLOC
M
Mark Adler 已提交
152 153 154 155 156 157

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

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

M
Mark Adler 已提交
164
void  zcfree (voidpf opaque, voidpf ptr)
M
Mark Adler 已提交
165
{
M
Mark Adler 已提交
166
    if (opaque) opaque = 0; /* to make compiler happy */
M
Mark Adler 已提交
167
    _hfree(ptr);
M
Mark Adler 已提交
168 169
}

M
Mark Adler 已提交
170
#endif /* MSC */
M
Mark Adler 已提交
171 172


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

M
Mark Adler 已提交
175
#ifndef STDC
M
Mark Adler 已提交
176 177
extern voidp  calloc OF((uInt items, uInt size));
extern void   free   OF((voidpf ptr));
M
Mark Adler 已提交
178
#endif
M
Mark Adler 已提交
179

M
Mark Adler 已提交
180 181
voidpf zcalloc (opaque, items, size)
    voidpf opaque;
M
Mark Adler 已提交
182 183 184
    unsigned items;
    unsigned size;
{
M
Mark Adler 已提交
185
    return (voidpf)calloc(items, size);
M
Mark Adler 已提交
186 187 188
}

void  zcfree (opaque, ptr)
M
Mark Adler 已提交
189 190
    voidpf opaque;
    voidpf ptr;
M
Mark Adler 已提交
191 192 193 194
{
    free(ptr);
}

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