annotate zlib/zutil.c @ 3:5a977ccbc7a9 default tip

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