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