< prev index next >

src/java.base/share/native/libzip/zlib/zutil.c

Print this page

 31 
 32 #include "zutil.h"
 33 #ifndef Z_SOLO
 34 #  include "gzguts.h"
 35 #endif
 36 
 37 z_const char * const z_errmsg[10] = {
 38     (z_const char *)"need dictionary",     /* Z_NEED_DICT       2  */
 39     (z_const char *)"stream end",          /* Z_STREAM_END      1  */
 40     (z_const char *)"",                    /* Z_OK              0  */
 41     (z_const char *)"file error",          /* Z_ERRNO         (-1) */
 42     (z_const char *)"stream error",        /* Z_STREAM_ERROR  (-2) */
 43     (z_const char *)"data error",          /* Z_DATA_ERROR    (-3) */
 44     (z_const char *)"insufficient memory", /* Z_MEM_ERROR     (-4) */
 45     (z_const char *)"buffer error",        /* Z_BUF_ERROR     (-5) */
 46     (z_const char *)"incompatible version",/* Z_VERSION_ERROR (-6) */
 47     (z_const char *)""
 48 };
 49 
 50 
 51 const char * ZEXPORT zlibVersion()
 52 {
 53     return ZLIB_VERSION;
 54 }
 55 
 56 uLong ZEXPORT zlibCompileFlags()
 57 {
 58     uLong flags;
 59 
 60     flags = 0;
 61     switch ((int)(sizeof(uInt))) {
 62     case 2:     break;
 63     case 4:     flags += 1;     break;
 64     case 8:     flags += 2;     break;
 65     default:    flags += 3;
 66     }
 67     switch ((int)(sizeof(uLong))) {
 68     case 2:     break;
 69     case 4:     flags += 1 << 2;        break;
 70     case 8:     flags += 2 << 2;        break;
 71     default:    flags += 3 << 2;
 72     }
 73     switch ((int)(sizeof(voidpf))) {
 74     case 2:     break;
 75     case 4:     flags += 1 << 4;        break;
 76     case 8:     flags += 2 << 4;        break;
 77     default:    flags += 3 << 4;

128     flags += 1L << 25;
129 #    ifdef HAS_sprintf_void
130     flags += 1L << 26;
131 #    endif
132 #  else
133 #    ifdef HAS_snprintf_void
134     flags += 1L << 26;
135 #    endif
136 #  endif
137 #endif
138     return flags;
139 }
140 
141 #ifdef ZLIB_DEBUG
142 #include <stdlib.h>
143 #  ifndef verbose
144 #    define verbose 0
145 #  endif
146 int ZLIB_INTERNAL z_verbose = verbose;
147 
148 void ZLIB_INTERNAL z_error(m)
149     char *m;
150 {
151     fprintf(stderr, "%s\n", m);
152     exit(1);
153 }
154 #endif
155 
156 /* exported to allow conversion of error code to string for compress() and
157  * uncompress()
158  */
159 const char * ZEXPORT zError(err)
160     int err;
161 {
162     return ERR_MSG(err);
163 }
164 
165 #if defined(_WIN32_WCE) && _WIN32_WCE < 0x800
166     /* The older Microsoft C Run-Time Library for Windows CE doesn't have
167      * errno.  We define it as a global variable to simplify porting.
168      * Its value is always 0 and should not be used.
169      */
170     int errno = 0;
171 #endif
172 
173 #ifndef HAVE_MEMCPY
174 
175 void ZLIB_INTERNAL zmemcpy(dest, source, len)
176     Bytef* dest;
177     const Bytef* source;
178     uInt  len;
179 {
180     if (len == 0) return;
181     do {
182         *dest++ = *source++; /* ??? to be unrolled */
183     } while (--len != 0);
184 }
185 
186 int ZLIB_INTERNAL zmemcmp(s1, s2, len)
187     const Bytef* s1;
188     const Bytef* s2;
189     uInt  len;
190 {
191     uInt j;
192 
193     for (j = 0; j < len; j++) {
194         if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
195     }
196     return 0;
197 }
198 
199 void ZLIB_INTERNAL zmemzero(dest, len)
200     Bytef* dest;
201     uInt  len;
202 {
203     if (len == 0) return;
204     do {
205         *dest++ = 0;  /* ??? to be unrolled */
206     } while (--len != 0);
207 }
208 #endif
209 
210 #ifndef Z_SOLO
211 
212 #ifdef SYS16BIT
213 
214 #ifdef __TURBOC__
215 /* Turbo C in 16-bit mode */
216 
217 #  define MY_ZCALLOC
218 
219 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
220  * and farmalloc(64K) returns a pointer with an offset of 8, so we
221  * must fix the pointer. Warning: the pointer must be put back to its
222  * original form in order to free it, use zcfree().
223  */
224 
225 #define MAX_PTR 10
226 /* 10*64K = 640K */
227 
228 local int next_ptr = 0;
229 
230 typedef struct ptr_table_s {
231     voidpf org_ptr;
232     voidpf new_ptr;
233 } ptr_table;
234 
235 local ptr_table table[MAX_PTR];
236 /* This table is used to remember the original form of pointers
237  * to large buffers (64K). Such pointers are normalized with a zero offset.
238  * Since MSDOS is not a preemptive multitasking OS, this table is not
239  * protected from concurrent access. This hack doesn't work anyway on
240  * a protected system like OS/2. Use Microsoft C instead.
241  */
242 
243 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size)
244 {
245     voidpf buf;
246     ulg bsize = (ulg)items*size;
247 
248     (void)opaque;
249 
250     /* If we allocate less than 65520 bytes, we assume that farmalloc
251      * will return a usable pointer which doesn't have to be normalized.
252      */
253     if (bsize < 65520L) {
254         buf = farmalloc(bsize);
255         if (*(ush*)&buf != 0) return buf;
256     } else {
257         buf = farmalloc(bsize + 16L);
258     }
259     if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
260     table[next_ptr].org_ptr = buf;
261 
262     /* Normalize the pointer to seg:0 */
263     *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
264     *(ush*)&buf = 0;
265     table[next_ptr++].new_ptr = buf;
266     return buf;
267 }
268 
269 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
270 {
271     int n;
272 
273     (void)opaque;
274 
275     if (*(ush*)&ptr != 0) { /* object < 64K */
276         farfree(ptr);
277         return;
278     }
279     /* Find the original pointer */
280     for (n = 0; n < next_ptr; n++) {
281         if (ptr != table[n].new_ptr) continue;
282 
283         farfree(table[n].org_ptr);
284         while (++n < next_ptr) {
285             table[n-1] = table[n];
286         }
287         next_ptr--;
288         return;
289     }
290     Assert(0, "zcfree: ptr not found");
291 }
292 
293 #endif /* __TURBOC__ */
294 
295 
296 #ifdef M_I86
297 /* Microsoft C in 16-bit mode */
298 
299 #  define MY_ZCALLOC
300 
301 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
302 #  define _halloc  halloc
303 #  define _hfree   hfree
304 #endif
305 
306 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size)
307 {
308     (void)opaque;
309     return _halloc((long)items, size);
310 }
311 
312 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
313 {
314     (void)opaque;
315     _hfree(ptr);
316 }
317 
318 #endif /* M_I86 */
319 
320 #endif /* SYS16BIT */
321 
322 
323 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
324 
325 #ifndef STDC
326 extern voidp  malloc OF((uInt size));
327 extern voidp  calloc OF((uInt items, uInt size));
328 extern void   free   OF((voidpf ptr));
329 #endif
330 
331 voidpf ZLIB_INTERNAL zcalloc(opaque, items, size)
332     voidpf opaque;
333     unsigned items;
334     unsigned size;
335 {
336     (void)opaque;
337     return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
338                               (voidpf)calloc(items, size);
339 }
340 
341 void ZLIB_INTERNAL zcfree(opaque, ptr)
342     voidpf opaque;
343     voidpf ptr;
344 {
345     (void)opaque;
346     free(ptr);
347 }
348 
349 #endif /* MY_ZCALLOC */
350 
351 #endif /* !Z_SOLO */

 31 
 32 #include "zutil.h"
 33 #ifndef Z_SOLO
 34 #  include "gzguts.h"
 35 #endif
 36 
 37 z_const char * const z_errmsg[10] = {
 38     (z_const char *)"need dictionary",     /* Z_NEED_DICT       2  */
 39     (z_const char *)"stream end",          /* Z_STREAM_END      1  */
 40     (z_const char *)"",                    /* Z_OK              0  */
 41     (z_const char *)"file error",          /* Z_ERRNO         (-1) */
 42     (z_const char *)"stream error",        /* Z_STREAM_ERROR  (-2) */
 43     (z_const char *)"data error",          /* Z_DATA_ERROR    (-3) */
 44     (z_const char *)"insufficient memory", /* Z_MEM_ERROR     (-4) */
 45     (z_const char *)"buffer error",        /* Z_BUF_ERROR     (-5) */
 46     (z_const char *)"incompatible version",/* Z_VERSION_ERROR (-6) */
 47     (z_const char *)""
 48 };
 49 
 50 
 51 const char * ZEXPORT zlibVersion(void) {

 52     return ZLIB_VERSION;
 53 }
 54 
 55 uLong ZEXPORT zlibCompileFlags(void) {

 56     uLong flags;
 57 
 58     flags = 0;
 59     switch ((int)(sizeof(uInt))) {
 60     case 2:     break;
 61     case 4:     flags += 1;     break;
 62     case 8:     flags += 2;     break;
 63     default:    flags += 3;
 64     }
 65     switch ((int)(sizeof(uLong))) {
 66     case 2:     break;
 67     case 4:     flags += 1 << 2;        break;
 68     case 8:     flags += 2 << 2;        break;
 69     default:    flags += 3 << 2;
 70     }
 71     switch ((int)(sizeof(voidpf))) {
 72     case 2:     break;
 73     case 4:     flags += 1 << 4;        break;
 74     case 8:     flags += 2 << 4;        break;
 75     default:    flags += 3 << 4;

126     flags += 1L << 25;
127 #    ifdef HAS_sprintf_void
128     flags += 1L << 26;
129 #    endif
130 #  else
131 #    ifdef HAS_snprintf_void
132     flags += 1L << 26;
133 #    endif
134 #  endif
135 #endif
136     return flags;
137 }
138 
139 #ifdef ZLIB_DEBUG
140 #include <stdlib.h>
141 #  ifndef verbose
142 #    define verbose 0
143 #  endif
144 int ZLIB_INTERNAL z_verbose = verbose;
145 
146 void ZLIB_INTERNAL z_error(char *m) {


147     fprintf(stderr, "%s\n", m);
148     exit(1);
149 }
150 #endif
151 
152 /* exported to allow conversion of error code to string for compress() and
153  * uncompress()
154  */
155 const char * ZEXPORT zError(int err) {


156     return ERR_MSG(err);
157 }
158 
159 #if defined(_WIN32_WCE) && _WIN32_WCE < 0x800
160     /* The older Microsoft C Run-Time Library for Windows CE doesn't have
161      * errno.  We define it as a global variable to simplify porting.
162      * Its value is always 0 and should not be used.
163      */
164     int errno = 0;
165 #endif
166 
167 #ifndef HAVE_MEMCPY
168 
169 void ZLIB_INTERNAL zmemcpy(Bytef* dest, const Bytef* source, uInt len) {




170     if (len == 0) return;
171     do {
172         *dest++ = *source++; /* ??? to be unrolled */
173     } while (--len != 0);
174 }
175 
176 int ZLIB_INTERNAL zmemcmp(const Bytef* s1, const Bytef* s2, uInt len) {




177     uInt j;
178 
179     for (j = 0; j < len; j++) {
180         if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
181     }
182     return 0;
183 }
184 
185 void ZLIB_INTERNAL zmemzero(Bytef* dest, uInt len) {



186     if (len == 0) return;
187     do {
188         *dest++ = 0;  /* ??? to be unrolled */
189     } while (--len != 0);
190 }
191 #endif
192 
193 #ifndef Z_SOLO
194 
195 #ifdef SYS16BIT
196 
197 #ifdef __TURBOC__
198 /* Turbo C in 16-bit mode */
199 
200 #  define MY_ZCALLOC
201 
202 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
203  * and farmalloc(64K) returns a pointer with an offset of 8, so we
204  * must fix the pointer. Warning: the pointer must be put back to its
205  * original form in order to free it, use zcfree().
206  */
207 
208 #define MAX_PTR 10
209 /* 10*64K = 640K */
210 
211 local int next_ptr = 0;
212 
213 typedef struct ptr_table_s {
214     voidpf org_ptr;
215     voidpf new_ptr;
216 } ptr_table;
217 
218 local ptr_table table[MAX_PTR];
219 /* This table is used to remember the original form of pointers
220  * to large buffers (64K). Such pointers are normalized with a zero offset.
221  * Since MSDOS is not a preemptive multitasking OS, this table is not
222  * protected from concurrent access. This hack doesn't work anyway on
223  * a protected system like OS/2. Use Microsoft C instead.
224  */
225 
226 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) {

227     voidpf buf;
228     ulg bsize = (ulg)items*size;
229 
230     (void)opaque;
231 
232     /* If we allocate less than 65520 bytes, we assume that farmalloc
233      * will return a usable pointer which doesn't have to be normalized.
234      */
235     if (bsize < 65520L) {
236         buf = farmalloc(bsize);
237         if (*(ush*)&buf != 0) return buf;
238     } else {
239         buf = farmalloc(bsize + 16L);
240     }
241     if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
242     table[next_ptr].org_ptr = buf;
243 
244     /* Normalize the pointer to seg:0 */
245     *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
246     *(ush*)&buf = 0;
247     table[next_ptr++].new_ptr = buf;
248     return buf;
249 }
250 
251 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {

252     int n;
253 
254     (void)opaque;
255 
256     if (*(ush*)&ptr != 0) { /* object < 64K */
257         farfree(ptr);
258         return;
259     }
260     /* Find the original pointer */
261     for (n = 0; n < next_ptr; n++) {
262         if (ptr != table[n].new_ptr) continue;
263 
264         farfree(table[n].org_ptr);
265         while (++n < next_ptr) {
266             table[n-1] = table[n];
267         }
268         next_ptr--;
269         return;
270     }
271     Assert(0, "zcfree: ptr not found");
272 }
273 
274 #endif /* __TURBOC__ */
275 
276 
277 #ifdef M_I86
278 /* Microsoft C in 16-bit mode */
279 
280 #  define MY_ZCALLOC
281 
282 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
283 #  define _halloc  halloc
284 #  define _hfree   hfree
285 #endif
286 
287 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size) {

288     (void)opaque;
289     return _halloc((long)items, size);
290 }
291 
292 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {

293     (void)opaque;
294     _hfree(ptr);
295 }
296 
297 #endif /* M_I86 */
298 
299 #endif /* SYS16BIT */
300 
301 
302 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
303 
304 #ifndef STDC
305 extern voidp malloc(uInt size);
306 extern voidp calloc(uInt items, uInt size);
307 extern void free(voidpf ptr);
308 #endif
309 
310 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) {




311     (void)opaque;
312     return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
313                               (voidpf)calloc(items, size);
314 }
315 
316 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {



317     (void)opaque;
318     free(ptr);
319 }
320 
321 #endif /* MY_ZCALLOC */
322 
323 #endif /* !Z_SOLO */
< prev index next >