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