annotate lib/zlib/zutil.c @ 0:8b8875f5b359

Initial commit
author Nomad
date Fri, 05 Oct 2012 16:07:14 +0200
parents
children
rev   line source
0
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
1 /* zutil.c -- target dependent utility functions for the compression library
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
2 * Copyright (C) 1995-1998 Jean-loup Gailly.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
3 * For conditions of distribution and use, see copyright notice in zlib.h
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
4 */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
5
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
6 /* @(#) $Id$ */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
7
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
8 #include "zutil.h"
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
9
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
10 struct internal_state {int dummy;}; /* for buggy compilers */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
11
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
12 #ifndef STDC
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
13 extern void exit OF((int));
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
14 #endif
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
15
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
16 const char *z_errmsg[10] = {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
17 "need dictionary", /* Z_NEED_DICT 2 */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
18 "stream end", /* Z_STREAM_END 1 */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
19 "", /* Z_OK 0 */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
20 "file error", /* Z_ERRNO (-1) */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
21 "stream error", /* Z_STREAM_ERROR (-2) */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
22 "data error", /* Z_DATA_ERROR (-3) */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
23 "insufficient memory", /* Z_MEM_ERROR (-4) */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
24 "buffer error", /* Z_BUF_ERROR (-5) */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
25 "incompatible version",/* Z_VERSION_ERROR (-6) */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
26 ""};
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
27
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
28
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
29 const char * ZEXPORT zlibVersion()
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
30 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
31 return ZLIB_VERSION;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
32 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
33
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
34 #ifdef DEBUG
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
35
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
36 # ifndef verbose
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
37 # define verbose 0
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
38 # endif
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
39 int z_verbose = verbose;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
40
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
41 void z_error (m)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
42 char *m;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
43 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
44 fprintf(stderr, "%s\n", m);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
45 exit(1);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
46 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
47 #endif
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
48
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
49 /* exported to allow conversion of error code to string for compress() and
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
50 * uncompress()
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
51 */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
52 const char * ZEXPORT zError(err)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
53 int err;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
54 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
55 return ERR_MSG(err);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
56 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
57
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
58
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
59 #ifndef HAVE_MEMCPY
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
60
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
61 void zmemcpy(dest, source, len)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
62 Bytef* dest;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
63 const Bytef* source;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
64 uInt len;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
65 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
66 if (len == 0) return;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
67 do {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
68 *dest++ = *source++; /* ??? to be unrolled */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
69 } while (--len != 0);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
70 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
71
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
72 int zmemcmp(s1, s2, len)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
73 const Bytef* s1;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
74 const Bytef* s2;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
75 uInt len;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
76 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
77 uInt j;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
78
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
79 for (j = 0; j < len; j++) {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
80 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
81 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
82 return 0;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
83 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
84
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
85 void zmemzero(dest, len)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
86 Bytef* dest;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
87 uInt len;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
88 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
89 if (len == 0) return;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
90 do {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
91 *dest++ = 0; /* ??? to be unrolled */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
92 } while (--len != 0);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
93 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
94 #endif
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
95
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
96 #ifdef __TURBOC__
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
97 #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
98 /* Small and medium model in Turbo C are for now limited to near allocation
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
99 * with reduced MAX_WBITS and MAX_MEM_LEVEL
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
100 */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
101 # define MY_ZCALLOC
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
102
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
103 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
104 * and farmalloc(64K) returns a pointer with an offset of 8, so we
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
105 * must fix the pointer. Warning: the pointer must be put back to its
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
106 * original form in order to free it, use zcfree().
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
107 */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
108
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
109 #define MAX_PTR 10
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
110 /* 10*64K = 640K */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
111
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
112 local int next_ptr = 0;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
113
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
114 typedef struct ptr_table_s {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
115 voidpf org_ptr;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
116 voidpf new_ptr;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
117 } ptr_table;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
118
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
119 local ptr_table table[MAX_PTR];
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
120 /* This table is used to remember the original form of pointers
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
121 * to large buffers (64K). Such pointers are normalized with a zero offset.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
122 * Since MSDOS is not a preemptive multitasking OS, this table is not
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
123 * protected from concurrent access. This hack doesn't work anyway on
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
124 * a protected system like OS/2. Use Microsoft C instead.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
125 */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
126
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
127 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
128 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
129 voidpf buf = opaque; /* just to make some compilers happy */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
130 ulg bsize = (ulg)items*size;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
131
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
132 /* If we allocate less than 65520 bytes, we assume that farmalloc
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
133 * will return a usable pointer which doesn't have to be normalized.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
134 */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
135 if (bsize < 65520L) {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
136 buf = farmalloc(bsize);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
137 if (*(ush*)&buf != 0) return buf;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
138 } else {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
139 buf = farmalloc(bsize + 16L);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
140 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
141 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
142 table[next_ptr].org_ptr = buf;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
143
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
144 /* Normalize the pointer to seg:0 */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
145 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
146 *(ush*)&buf = 0;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
147 table[next_ptr++].new_ptr = buf;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
148 return buf;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
149 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
150
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
151 void zcfree (voidpf opaque, voidpf ptr)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
152 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
153 int n;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
154 if (*(ush*)&ptr != 0) { /* object < 64K */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
155 farfree(ptr);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
156 return;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
157 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
158 /* Find the original pointer */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
159 for (n = 0; n < next_ptr; n++) {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
160 if (ptr != table[n].new_ptr) continue;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
161
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
162 farfree(table[n].org_ptr);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
163 while (++n < next_ptr) {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
164 table[n-1] = table[n];
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
165 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
166 next_ptr--;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
167 return;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
168 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
169 ptr = opaque; /* just to make some compilers happy */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
170 Assert(0, "zcfree: ptr not found");
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
171 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
172 #endif
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
173 #endif /* __TURBOC__ */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
174
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
175
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
176 #if defined(M_I86) && !defined(__32BIT__)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
177 /* Microsoft C in 16-bit mode */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
178
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
179 # define MY_ZCALLOC
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
180
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
181 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
182 # define _halloc halloc
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
183 # define _hfree hfree
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
184 #endif
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
185
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
186 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
187 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
188 if (opaque) opaque = 0; /* to make compiler happy */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
189 return _halloc((long)items, size);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
190 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
191
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
192 void zcfree (voidpf opaque, voidpf ptr)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
193 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
194 if (opaque) opaque = 0; /* to make compiler happy */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
195 _hfree(ptr);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
196 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
197
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
198 #endif /* MSC */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
199
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
200
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
201 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
202
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
203 #ifndef STDC
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
204 extern voidp calloc OF((uInt items, uInt size));
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
205 extern void free OF((voidpf ptr));
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
206 #endif
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
207
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
208 voidpf zcalloc (opaque, items, size)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
209 voidpf opaque;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
210 unsigned items;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
211 unsigned size;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
212 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
213 if (opaque) items += size - size; /* make compiler happy */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
214 return (voidpf)calloc(items, size);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
215 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
216
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
217 void zcfree (opaque, ptr)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
218 voidpf opaque;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
219 voidpf ptr;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
220 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
221 free(ptr);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
222 if (opaque) return; /* make compiler happy */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
223 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
224
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
225 #endif /* MY_ZCALLOC */