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