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