]> git.lizzy.rs Git - zlib.git/blob - zutil.c
9fd4ecc658f5801fb882348c94694a2ce3b298b3
[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.5 1995/04/14 21:30:23 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(USE_CALLOC)
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;
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     z_error("zcfree: ptr not found");
125 }
126
127 #  else /* MSC */
128
129 #if (!defined(_MSC_VER) || (_MSC_VER < 600))
130 #  define _halloc  halloc
131 #  define _hfree   hfree
132 #endif
133
134 voidp zcalloc (voidp opaque, unsigned items, unsigned size)
135 {
136     return _halloc((long)items, size);
137 }
138
139 void  zcfree (voidp opaque, voidp ptr)
140 {
141     _hfree(ptr);
142 }
143
144 #  endif /* __TURBOC__ ? */
145
146 #else /* !MSDOS */
147
148 extern voidp calloc __P((uInt items, uInt size));
149 extern void  free   __P((voidp ptr));
150
151 voidp zcalloc (opaque, items, size)
152     voidp opaque;
153     unsigned items;
154     unsigned size;
155 {
156     return calloc(items, size);
157 }
158
159 void  zcfree (opaque, ptr)
160     voidp opaque;
161     voidp ptr;
162 {
163     free(ptr);
164 }
165
166 #endif /* MSDOS */