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