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