Path: blob/master/src/java.base/share/native/libzip/zlib/zutil.c
41153 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/* zutil.c -- target dependent utility functions for the compression library25* Copyright (C) 1995-2017 Jean-loup Gailly26* For conditions of distribution and use, see copyright notice in zlib.h27*/2829/* @(#) $Id$ */3031#include "zutil.h"3233#ifndef Z_SOLO34# include "gzguts.h"35#endif3637z_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};495051const char * ZEXPORT zlibVersion()52{53return ZLIB_VERSION;54}5556uLong ZEXPORT zlibCompileFlags()57{58uLong flags;5960flags = 0;61switch ((int)(sizeof(uInt))) {62case 2: break;63case 4: flags += 1; break;64case 8: flags += 2; break;65default: flags += 3;66}67switch ((int)(sizeof(uLong))) {68case 2: break;69case 4: flags += 1 << 2; break;70case 8: flags += 2 << 2; break;71default: flags += 3 << 2;72}73switch ((int)(sizeof(voidpf))) {74case 2: break;75case 4: flags += 1 << 4; break;76case 8: flags += 2 << 4; break;77default: flags += 3 << 4;78}79switch ((int)(sizeof(z_off_t))) {80case 2: break;81case 4: flags += 1 << 6; break;82case 8: flags += 2 << 6; break;83default: flags += 3 << 6;84}85#ifdef ZLIB_DEBUG86flags += 1 << 8;87#endif88#if defined(ASMV) || defined(ASMINF)89flags += 1 << 9;90#endif91#ifdef ZLIB_WINAPI92flags += 1 << 10;93#endif94#ifdef BUILDFIXED95flags += 1 << 12;96#endif97#ifdef DYNAMIC_CRC_TABLE98flags += 1 << 13;99#endif100#ifdef NO_GZCOMPRESS101flags += 1L << 16;102#endif103#ifdef NO_GZIP104flags += 1L << 17;105#endif106#ifdef PKZIP_BUG_WORKAROUND107flags += 1L << 20;108#endif109#ifdef FASTEST110flags += 1L << 21;111#endif112#if defined(STDC) || defined(Z_HAVE_STDARG_H)113# ifdef NO_vsnprintf114flags += 1L << 25;115# ifdef HAS_vsprintf_void116flags += 1L << 26;117# endif118# else119# ifdef HAS_vsnprintf_void120flags += 1L << 26;121# endif122# endif123#else124flags += 1L << 24;125# ifdef NO_snprintf126flags += 1L << 25;127# ifdef HAS_sprintf_void128flags += 1L << 26;129# endif130# else131# ifdef HAS_snprintf_void132flags += 1L << 26;133# endif134# endif135#endif136return flags;137}138139#ifdef ZLIB_DEBUG140#include <stdlib.h>141# ifndef verbose142# define verbose 0143# endif144int ZLIB_INTERNAL z_verbose = verbose;145146void ZLIB_INTERNAL z_error (m)147char *m;148{149fprintf(stderr, "%s\n", m);150exit(1);151}152#endif153154/* exported to allow conversion of error code to string for compress() and155* uncompress()156*/157const char * ZEXPORT zError(err)158int err;159{160return ERR_MSG(err);161}162163#if defined(_WIN32_WCE)164/* The Microsoft C Run-Time Library for Windows CE doesn't have165* errno. We define it as a global variable to simplify porting.166* Its value is always 0 and should not be used.167*/168int errno = 0;169#endif170171#ifndef HAVE_MEMCPY172173void ZLIB_INTERNAL zmemcpy(dest, source, len)174Bytef* dest;175const Bytef* source;176uInt len;177{178if (len == 0) return;179do {180*dest++ = *source++; /* ??? to be unrolled */181} while (--len != 0);182}183184int ZLIB_INTERNAL zmemcmp(s1, s2, len)185const Bytef* s1;186const Bytef* s2;187uInt len;188{189uInt j;190191for (j = 0; j < len; j++) {192if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;193}194return 0;195}196197void ZLIB_INTERNAL zmemzero(dest, len)198Bytef* dest;199uInt len;200{201if (len == 0) return;202do {203*dest++ = 0; /* ??? to be unrolled */204} while (--len != 0);205}206#endif207208#ifndef Z_SOLO209210#ifdef SYS16BIT211212#ifdef __TURBOC__213/* Turbo C in 16-bit mode */214215# define MY_ZCALLOC216217/* Turbo C malloc() does not allow dynamic allocation of 64K bytes218* and farmalloc(64K) returns a pointer with an offset of 8, so we219* must fix the pointer. Warning: the pointer must be put back to its220* original form in order to free it, use zcfree().221*/222223#define MAX_PTR 10224/* 10*64K = 640K */225226local int next_ptr = 0;227228typedef struct ptr_table_s {229voidpf org_ptr;230voidpf new_ptr;231} ptr_table;232233local ptr_table table[MAX_PTR];234/* This table is used to remember the original form of pointers235* to large buffers (64K). Such pointers are normalized with a zero offset.236* Since MSDOS is not a preemptive multitasking OS, this table is not237* protected from concurrent access. This hack doesn't work anyway on238* a protected system like OS/2. Use Microsoft C instead.239*/240241voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)242{243voidpf buf;244ulg bsize = (ulg)items*size;245246(void)opaque;247248/* If we allocate less than 65520 bytes, we assume that farmalloc249* will return a usable pointer which doesn't have to be normalized.250*/251if (bsize < 65520L) {252buf = farmalloc(bsize);253if (*(ush*)&buf != 0) return buf;254} else {255buf = farmalloc(bsize + 16L);256}257if (buf == NULL || next_ptr >= MAX_PTR) return NULL;258table[next_ptr].org_ptr = buf;259260/* Normalize the pointer to seg:0 */261*((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;262*(ush*)&buf = 0;263table[next_ptr++].new_ptr = buf;264return buf;265}266267void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)268{269int n;270271(void)opaque;272273if (*(ush*)&ptr != 0) { /* object < 64K */274farfree(ptr);275return;276}277/* Find the original pointer */278for (n = 0; n < next_ptr; n++) {279if (ptr != table[n].new_ptr) continue;280281farfree(table[n].org_ptr);282while (++n < next_ptr) {283table[n-1] = table[n];284}285next_ptr--;286return;287}288Assert(0, "zcfree: ptr not found");289}290291#endif /* __TURBOC__ */292293294#ifdef M_I86295/* Microsoft C in 16-bit mode */296297# define MY_ZCALLOC298299#if (!defined(_MSC_VER) || (_MSC_VER <= 600))300# define _halloc halloc301# define _hfree hfree302#endif303304voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)305{306(void)opaque;307return _halloc((long)items, size);308}309310void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)311{312(void)opaque;313_hfree(ptr);314}315316#endif /* M_I86 */317318#endif /* SYS16BIT */319320321#ifndef MY_ZCALLOC /* Any system without a special alloc function */322323#ifndef STDC324extern voidp malloc OF((uInt size));325extern voidp calloc OF((uInt items, uInt size));326extern void free OF((voidpf ptr));327#endif328329voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)330voidpf opaque;331unsigned items;332unsigned size;333{334(void)opaque;335return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :336(voidpf)calloc(items, size);337}338339void ZLIB_INTERNAL zcfree (opaque, ptr)340voidpf opaque;341voidpf ptr;342{343(void)opaque;344free(ptr);345}346347#endif /* MY_ZCALLOC */348349#endif /* !Z_SOLO */350351352