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.h -- internal interface and configuration of the compression library
  26  * Copyright (C) 1995-2016 Jean-loup Gailly, Mark Adler
  27  * For conditions of distribution and use, see copyright notice in zlib.h
  28  */
  29 
  30 /* WARNING: this file should *not* be used by applications. It is
  31    part of the implementation of the compression library and is
  32    subject to change. Applications should only use zlib.h.
  33  */
  34 
  35 /* @(#) $Id$ */
  36 
  37 #ifndef ZUTIL_H
  38 #define ZUTIL_H
  39 
  40 #ifdef HAVE_HIDDEN
  41 #  define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
  42 #else
  43 #  define ZLIB_INTERNAL
  44 #endif
  45 
  46 #include "zlib.h"
  47 
  48 #if defined(STDC) && !defined(Z_SOLO)
  49 #  if !(defined(_WIN32_WCE) && defined(_MSC_VER))
  50 #    include <stddef.h>
  51 #  endif
  52 #  include <string.h>
  53 #  include <stdlib.h>
  54 #endif
  55 
  56 #ifdef Z_SOLO
  57    typedef long ptrdiff_t;  /* guess -- will be caught if guess is wrong */
  58 #endif
  59 
  60 #ifndef local
  61 #  define local static
  62 #endif
  63 /* since "static" is used to mean two completely different things in C, we
  64    define "local" for the non-static meaning of "static", for readability
  65    (compile with -Dlocal if your debugger can't find static symbols) */
  66 
  67 typedef unsigned char  uch;
  68 typedef uch FAR uchf;
  69 typedef unsigned short ush;
  70 typedef ush FAR ushf;
  71 typedef unsigned long  ulg;
  72 
  73 extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
  74 /* (size given to avoid silly warnings with Visual C++) */
  75 
  76 #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
  77 
  78 #define ERR_RETURN(strm,err) \
  79   return (strm->msg = ERR_MSG(err), (err))
  80 /* To be used only when the state is known to be valid */
  81 
  82         /* common constants */
  83 
  84 #ifndef DEF_WBITS
  85 #  define DEF_WBITS MAX_WBITS
  86 #endif
  87 /* default windowBits for decompression. MAX_WBITS is for compression only */
  88 
  89 #if MAX_MEM_LEVEL >= 8
  90 #  define DEF_MEM_LEVEL 8
  91 #else
  92 #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
  93 #endif
  94 /* default memLevel */
  95 
  96 #define STORED_BLOCK 0
  97 #define STATIC_TREES 1
  98 #define DYN_TREES    2
  99 /* The three kinds of block type */
 100 
 101 #define MIN_MATCH  3
 102 #define MAX_MATCH  258
 103 /* The minimum and maximum match lengths */
 104 
 105 #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
 106 
 107         /* target dependencies */
 108 
 109 #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
 110 #  define OS_CODE  0x00
 111 #  ifndef Z_SOLO
 112 #    if defined(__TURBOC__) || defined(__BORLANDC__)
 113 #      if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
 114          /* Allow compilation with ANSI keywords only enabled */
 115          void _Cdecl farfree( void *block );
 116          void *_Cdecl farmalloc( unsigned long nbytes );
 117 #      else
 118 #        include <alloc.h>
 119 #      endif
 120 #    else /* MSC or DJGPP */
 121 #      include <malloc.h>
 122 #    endif
 123 #  endif
 124 #endif
 125 
 126 #ifdef AMIGA
 127 #  define OS_CODE  1
 128 #endif
 129 
 130 #if defined(VAXC) || defined(VMS)
 131 #  define OS_CODE  2
 132 #  define F_OPEN(name, mode) \
 133      fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
 134 #endif
 135 
 136 #ifdef __370__
 137 #  if __TARGET_LIB__ < 0x20000000
 138 #    define OS_CODE 4
 139 #  elif __TARGET_LIB__ < 0x40000000
 140 #    define OS_CODE 11
 141 #  else
 142 #    define OS_CODE 8
 143 #  endif
 144 #endif
 145 
 146 #if defined(ATARI) || defined(atarist)
 147 #  define OS_CODE  5
 148 #endif
 149 
 150 #ifdef OS2
 151 #  define OS_CODE  6
 152 #  if defined(M_I86) && !defined(Z_SOLO)
 153 #    include <malloc.h>
 154 #  endif
 155 #endif
 156 
 157 #if defined(MACOS) || defined(TARGET_OS_MAC)
 158 #  define OS_CODE  7
 159 #  ifndef Z_SOLO
 160 #    if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
 161 #      include <unix.h> /* for fdopen */
 162 #    else
 163 #      ifndef fdopen
 164 #        define fdopen(fd,mode) NULL /* No fdopen() */
 165 #      endif
 166 #    endif
 167 #  endif
 168 #endif
 169 
 170 #ifdef __acorn
 171 #  define OS_CODE 13
 172 #endif
 173 
 174 #if defined(WIN32) && !defined(__CYGWIN__)
 175 #  define OS_CODE  10
 176 #endif
 177 
 178 #ifdef _BEOS_
 179 #  define OS_CODE  16
 180 #endif
 181 
 182 #ifdef __TOS_OS400__
 183 #  define OS_CODE 18
 184 #endif
 185 
 186 #ifdef __APPLE__
 187 #  define OS_CODE 19
 188 #endif
 189 
 190 #if defined(_BEOS_) || defined(RISCOS)
 191 #  define fdopen(fd,mode) NULL /* No fdopen() */
 192 #endif
 193 
 194 #if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX
 195 #  if defined(_WIN32_WCE)
 196 #    define fdopen(fd,mode) NULL /* No fdopen() */
 197 #    ifndef _PTRDIFF_T_DEFINED
 198        typedef int ptrdiff_t;
 199 #      define _PTRDIFF_T_DEFINED
 200 #    endif
 201 #  else
 202 #    define fdopen(fd,type)  _fdopen(fd,type)
 203 #  endif
 204 #endif
 205 
 206 #if defined(__BORLANDC__) && !defined(MSDOS)
 207   #pragma warn -8004
 208   #pragma warn -8008
 209   #pragma warn -8066
 210 #endif
 211 
 212 /* provide prototypes for these when building zlib without LFS */
 213 #if !defined(_WIN32) && \
 214     (!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0)
 215     ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
 216     ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
 217 #endif
 218 
 219         /* common defaults */
 220 
 221 #ifndef OS_CODE
 222 #  define OS_CODE  3     /* assume Unix */
 223 #endif
 224 
 225 #ifndef F_OPEN
 226 #  define F_OPEN(name, mode) fopen((name), (mode))
 227 #endif
 228 
 229          /* functions */
 230 
 231 #if defined(pyr) || defined(Z_SOLO)
 232 #  define NO_MEMCPY
 233 #endif
 234 #if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
 235  /* Use our own functions for small and medium model with MSC <= 5.0.
 236   * You may have to use the same strategy for Borland C (untested).
 237   * The __SC__ check is for Symantec.
 238   */
 239 #  define NO_MEMCPY
 240 #endif
 241 #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
 242 #  define HAVE_MEMCPY
 243 #endif
 244 #ifdef HAVE_MEMCPY
 245 #  ifdef SMALL_MEDIUM /* MSDOS small or medium model */
 246 #    define zmemcpy _fmemcpy
 247 #    define zmemcmp _fmemcmp
 248 #    define zmemzero(dest, len) _fmemset(dest, 0, len)
 249 #  else
 250 #    define zmemcpy memcpy
 251 #    define zmemcmp memcmp
 252 #    define zmemzero(dest, len) memset(dest, 0, len)
 253 #  endif
 254 #else
 255    void ZLIB_INTERNAL zmemcpy OF((Bytef* dest, const Bytef* source, uInt len));
 256    int ZLIB_INTERNAL zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len));
 257    void ZLIB_INTERNAL zmemzero OF((Bytef* dest, uInt len));
 258 #endif
 259 
 260 /* Diagnostic functions */
 261 #ifdef ZLIB_DEBUG
 262 #  include <stdio.h>
 263    extern int ZLIB_INTERNAL z_verbose;
 264    extern void ZLIB_INTERNAL z_error OF((char *m));
 265 #  define Assert(cond,msg) {if(!(cond)) z_error(msg);}
 266 #  define Trace(x) {if (z_verbose>=0) fprintf x ;}
 267 #  define Tracev(x) {if (z_verbose>0) fprintf x ;}
 268 #  define Tracevv(x) {if (z_verbose>1) fprintf x ;}
 269 #  define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
 270 #  define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
 271 #else
 272 #  define Assert(cond,msg)
 273 #  define Trace(x)
 274 #  define Tracev(x)
 275 #  define Tracevv(x)
 276 #  define Tracec(c,x)
 277 #  define Tracecv(c,x)
 278 #endif
 279 
 280 #ifndef Z_SOLO
 281    voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items,
 282                                     unsigned size));
 283    void ZLIB_INTERNAL zcfree  OF((voidpf opaque, voidpf ptr));
 284 #endif
 285 
 286 #define ZALLOC(strm, items, size) \
 287            (*((strm)->zalloc))((strm)->opaque, (items), (size))
 288 #define ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
 289 #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
 290 
 291 /* Reverse the bytes in a 32-bit value */
 292 #define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
 293                     (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
 294 
 295 #endif /* ZUTIL_H */