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