jdk/src/java.base/share/native/libzip/zlib-1.2.8/zutil.c
changeset 44203 d2d435372329
parent 44080 56713dcfe871
parent 43915 4a79ad46e578
child 44205 0c46195767fb
child 46327 91576389a517
equal deleted inserted replaced
44080:56713dcfe871 44203:d2d435372329
     1 /*
       
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     3  *
       
     4  * This code is free software; you can redistribute it and/or modify it
       
     5  * under the terms of the GNU General Public License version 2 only, as
       
     6  * published by the Free Software Foundation.  Oracle designates this
       
     7  * particular file as subject to the "Classpath" exception as provided
       
     8  * by Oracle in the LICENSE file that accompanied this code.
       
     9  *
       
    10  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    13  * version 2 for more details (a copy is included in the LICENSE file that
       
    14  * accompanied this code).
       
    15  *
       
    16  * You should have received a copy of the GNU General Public License version
       
    17  * 2 along with this work; if not, write to the Free Software Foundation,
       
    18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    19  *
       
    20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    21  * or visit www.oracle.com if you need additional information or have any
       
    22  * questions.
       
    23  */
       
    24 
       
    25 /* zutil.c -- target dependent utility functions for the compression library
       
    26  * Copyright (C) 1995-2005, 2010, 2011, 2012 Jean-loup Gailly.
       
    27  * For conditions of distribution and use, see copyright notice in zlib.h
       
    28  */
       
    29 
       
    30 /* @(#) $Id$ */
       
    31 
       
    32 #include "zutil.h"
       
    33 #ifndef Z_SOLO
       
    34 #  include "gzguts.h"
       
    35 #endif
       
    36 
       
    37 #ifndef NO_DUMMY_DECL
       
    38 struct internal_state      {int dummy;}; /* for buggy compilers */
       
    39 #endif
       
    40 
       
    41 z_const char * const z_errmsg[10] = {
       
    42 "need dictionary",     /* Z_NEED_DICT       2  */
       
    43 "stream end",          /* Z_STREAM_END      1  */
       
    44 "",                    /* Z_OK              0  */
       
    45 "file error",          /* Z_ERRNO         (-1) */
       
    46 "stream error",        /* Z_STREAM_ERROR  (-2) */
       
    47 "data error",          /* Z_DATA_ERROR    (-3) */
       
    48 "insufficient memory", /* Z_MEM_ERROR     (-4) */
       
    49 "buffer error",        /* Z_BUF_ERROR     (-5) */
       
    50 "incompatible version",/* Z_VERSION_ERROR (-6) */
       
    51 ""};
       
    52 
       
    53 
       
    54 const char * ZEXPORT zlibVersion()
       
    55 {
       
    56     return ZLIB_VERSION;
       
    57 }
       
    58 
       
    59 uLong ZEXPORT zlibCompileFlags()
       
    60 {
       
    61     uLong flags;
       
    62 
       
    63     flags = 0;
       
    64     switch ((int)(sizeof(uInt))) {
       
    65     case 2:     break;
       
    66     case 4:     flags += 1;     break;
       
    67     case 8:     flags += 2;     break;
       
    68     default:    flags += 3;
       
    69     }
       
    70     switch ((int)(sizeof(uLong))) {
       
    71     case 2:     break;
       
    72     case 4:     flags += 1 << 2;        break;
       
    73     case 8:     flags += 2 << 2;        break;
       
    74     default:    flags += 3 << 2;
       
    75     }
       
    76     switch ((int)(sizeof(voidpf))) {
       
    77     case 2:     break;
       
    78     case 4:     flags += 1 << 4;        break;
       
    79     case 8:     flags += 2 << 4;        break;
       
    80     default:    flags += 3 << 4;
       
    81     }
       
    82     switch ((int)(sizeof(z_off_t))) {
       
    83     case 2:     break;
       
    84     case 4:     flags += 1 << 6;        break;
       
    85     case 8:     flags += 2 << 6;        break;
       
    86     default:    flags += 3 << 6;
       
    87     }
       
    88 #ifdef DEBUG
       
    89     flags += 1 << 8;
       
    90 #endif
       
    91 #if defined(ASMV) || defined(ASMINF)
       
    92     flags += 1 << 9;
       
    93 #endif
       
    94 #ifdef ZLIB_WINAPI
       
    95     flags += 1 << 10;
       
    96 #endif
       
    97 #ifdef BUILDFIXED
       
    98     flags += 1 << 12;
       
    99 #endif
       
   100 #ifdef DYNAMIC_CRC_TABLE
       
   101     flags += 1 << 13;
       
   102 #endif
       
   103 #ifdef NO_GZCOMPRESS
       
   104     flags += 1L << 16;
       
   105 #endif
       
   106 #ifdef NO_GZIP
       
   107     flags += 1L << 17;
       
   108 #endif
       
   109 #ifdef PKZIP_BUG_WORKAROUND
       
   110     flags += 1L << 20;
       
   111 #endif
       
   112 #ifdef FASTEST
       
   113     flags += 1L << 21;
       
   114 #endif
       
   115 #if defined(STDC) || defined(Z_HAVE_STDARG_H)
       
   116 #  ifdef NO_vsnprintf
       
   117     flags += 1L << 25;
       
   118 #    ifdef HAS_vsprintf_void
       
   119     flags += 1L << 26;
       
   120 #    endif
       
   121 #  else
       
   122 #    ifdef HAS_vsnprintf_void
       
   123     flags += 1L << 26;
       
   124 #    endif
       
   125 #  endif
       
   126 #else
       
   127     flags += 1L << 24;
       
   128 #  ifdef NO_snprintf
       
   129     flags += 1L << 25;
       
   130 #    ifdef HAS_sprintf_void
       
   131     flags += 1L << 26;
       
   132 #    endif
       
   133 #  else
       
   134 #    ifdef HAS_snprintf_void
       
   135     flags += 1L << 26;
       
   136 #    endif
       
   137 #  endif
       
   138 #endif
       
   139     return flags;
       
   140 }
       
   141 
       
   142 #ifdef DEBUG
       
   143 
       
   144 #  ifndef verbose
       
   145 #    define verbose 0
       
   146 #  endif
       
   147 int ZLIB_INTERNAL z_verbose = verbose;
       
   148 
       
   149 void ZLIB_INTERNAL z_error (m)
       
   150     char *m;
       
   151 {
       
   152     fprintf(stderr, "%s\n", m);
       
   153     exit(1);
       
   154 }
       
   155 #endif
       
   156 
       
   157 /* exported to allow conversion of error code to string for compress() and
       
   158  * uncompress()
       
   159  */
       
   160 const char * ZEXPORT zError(err)
       
   161     int err;
       
   162 {
       
   163     return ERR_MSG(err);
       
   164 }
       
   165 
       
   166 #if defined(_WIN32_WCE)
       
   167     /* The Microsoft C Run-Time Library for Windows CE doesn't have
       
   168      * errno.  We define it as a global variable to simplify porting.
       
   169      * Its value is always 0 and should not be used.
       
   170      */
       
   171     int errno = 0;
       
   172 #endif
       
   173 
       
   174 #ifndef HAVE_MEMCPY
       
   175 
       
   176 void ZLIB_INTERNAL zmemcpy(dest, source, len)
       
   177     Bytef* dest;
       
   178     const Bytef* source;
       
   179     uInt  len;
       
   180 {
       
   181     if (len == 0) return;
       
   182     do {
       
   183         *dest++ = *source++; /* ??? to be unrolled */
       
   184     } while (--len != 0);
       
   185 }
       
   186 
       
   187 int ZLIB_INTERNAL zmemcmp(s1, s2, len)
       
   188     const Bytef* s1;
       
   189     const Bytef* s2;
       
   190     uInt  len;
       
   191 {
       
   192     uInt j;
       
   193 
       
   194     for (j = 0; j < len; j++) {
       
   195         if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
       
   196     }
       
   197     return 0;
       
   198 }
       
   199 
       
   200 void ZLIB_INTERNAL zmemzero(dest, len)
       
   201     Bytef* dest;
       
   202     uInt  len;
       
   203 {
       
   204     if (len == 0) return;
       
   205     do {
       
   206         *dest++ = 0;  /* ??? to be unrolled */
       
   207     } while (--len != 0);
       
   208 }
       
   209 #endif
       
   210 
       
   211 #ifndef Z_SOLO
       
   212 
       
   213 #ifdef SYS16BIT
       
   214 
       
   215 #ifdef __TURBOC__
       
   216 /* Turbo C in 16-bit mode */
       
   217 
       
   218 #  define MY_ZCALLOC
       
   219 
       
   220 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
       
   221  * and farmalloc(64K) returns a pointer with an offset of 8, so we
       
   222  * must fix the pointer. Warning: the pointer must be put back to its
       
   223  * original form in order to free it, use zcfree().
       
   224  */
       
   225 
       
   226 #define MAX_PTR 10
       
   227 /* 10*64K = 640K */
       
   228 
       
   229 local int next_ptr = 0;
       
   230 
       
   231 typedef struct ptr_table_s {
       
   232     voidpf org_ptr;
       
   233     voidpf new_ptr;
       
   234 } ptr_table;
       
   235 
       
   236 local ptr_table table[MAX_PTR];
       
   237 /* This table is used to remember the original form of pointers
       
   238  * to large buffers (64K). Such pointers are normalized with a zero offset.
       
   239  * Since MSDOS is not a preemptive multitasking OS, this table is not
       
   240  * protected from concurrent access. This hack doesn't work anyway on
       
   241  * a protected system like OS/2. Use Microsoft C instead.
       
   242  */
       
   243 
       
   244 voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
       
   245 {
       
   246     voidpf buf = opaque; /* just to make some compilers happy */
       
   247     ulg bsize = (ulg)items*size;
       
   248 
       
   249     /* If we allocate less than 65520 bytes, we assume that farmalloc
       
   250      * will return a usable pointer which doesn't have to be normalized.
       
   251      */
       
   252     if (bsize < 65520L) {
       
   253         buf = farmalloc(bsize);
       
   254         if (*(ush*)&buf != 0) return buf;
       
   255     } else {
       
   256         buf = farmalloc(bsize + 16L);
       
   257     }
       
   258     if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
       
   259     table[next_ptr].org_ptr = buf;
       
   260 
       
   261     /* Normalize the pointer to seg:0 */
       
   262     *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
       
   263     *(ush*)&buf = 0;
       
   264     table[next_ptr++].new_ptr = buf;
       
   265     return buf;
       
   266 }
       
   267 
       
   268 void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
       
   269 {
       
   270     int n;
       
   271     if (*(ush*)&ptr != 0) { /* object < 64K */
       
   272         farfree(ptr);
       
   273         return;
       
   274     }
       
   275     /* Find the original pointer */
       
   276     for (n = 0; n < next_ptr; n++) {
       
   277         if (ptr != table[n].new_ptr) continue;
       
   278 
       
   279         farfree(table[n].org_ptr);
       
   280         while (++n < next_ptr) {
       
   281             table[n-1] = table[n];
       
   282         }
       
   283         next_ptr--;
       
   284         return;
       
   285     }
       
   286     ptr = opaque; /* just to make some compilers happy */
       
   287     Assert(0, "zcfree: ptr not found");
       
   288 }
       
   289 
       
   290 #endif /* __TURBOC__ */
       
   291 
       
   292 
       
   293 #ifdef M_I86
       
   294 /* Microsoft C in 16-bit mode */
       
   295 
       
   296 #  define MY_ZCALLOC
       
   297 
       
   298 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
       
   299 #  define _halloc  halloc
       
   300 #  define _hfree   hfree
       
   301 #endif
       
   302 
       
   303 voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
       
   304 {
       
   305     if (opaque) opaque = 0; /* to make compiler happy */
       
   306     return _halloc((long)items, size);
       
   307 }
       
   308 
       
   309 void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
       
   310 {
       
   311     if (opaque) opaque = 0; /* to make compiler happy */
       
   312     _hfree(ptr);
       
   313 }
       
   314 
       
   315 #endif /* M_I86 */
       
   316 
       
   317 #endif /* SYS16BIT */
       
   318 
       
   319 
       
   320 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
       
   321 
       
   322 #ifndef STDC
       
   323 extern voidp  malloc OF((uInt size));
       
   324 extern voidp  calloc OF((uInt items, uInt size));
       
   325 extern void   free   OF((voidpf ptr));
       
   326 #endif
       
   327 
       
   328 voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
       
   329     voidpf opaque;
       
   330     unsigned items;
       
   331     unsigned size;
       
   332 {
       
   333     if (opaque) items += size - size; /* make compiler happy */
       
   334     return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
       
   335                               (voidpf)calloc(items, size);
       
   336 }
       
   337 
       
   338 void ZLIB_INTERNAL zcfree (opaque, ptr)
       
   339     voidpf opaque;
       
   340     voidpf ptr;
       
   341 {
       
   342     free(ptr);
       
   343     if (opaque) return; /* make compiler happy */
       
   344 }
       
   345 
       
   346 #endif /* MY_ZCALLOC */
       
   347 
       
   348 #endif /* !Z_SOLO */