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 /* gzguts.h -- zlib internal header definitions for gz* operations
  26  * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler
  27  * For conditions of distribution and use, see copyright notice in zlib.h
  28  */
  29 
  30 #ifdef _LARGEFILE64_SOURCE
  31 #  ifndef _LARGEFILE_SOURCE
  32 #    define _LARGEFILE_SOURCE 1
  33 #  endif
  34 #  ifdef _FILE_OFFSET_BITS
  35 #    undef _FILE_OFFSET_BITS
  36 #  endif
  37 #endif
  38 
  39 #ifdef HAVE_HIDDEN
  40 #  define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
  41 #else
  42 #  define ZLIB_INTERNAL
  43 #endif
  44 
  45 #include <stdio.h>
  46 #include "zlib.h"
  47 #ifdef STDC
  48 #  include <string.h>
  49 #  include <stdlib.h>
  50 #  include <limits.h>
  51 #endif
  52 #include <fcntl.h>
  53 
  54 #ifdef _WIN32
  55 #  include <stddef.h>
  56 #endif
  57 
  58 #if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32)
  59 #  include <io.h>
  60 #endif
  61 
  62 #ifdef WINAPI_FAMILY
  63 #  define open _open
  64 #  define read _read
  65 #  define write _write
  66 #  define close _close
  67 #endif
  68 
  69 #ifdef NO_DEFLATE       /* for compatibility with old definition */
  70 #  define NO_GZCOMPRESS
  71 #endif
  72 
  73 #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
  74 #  ifndef HAVE_VSNPRINTF
  75 #    define HAVE_VSNPRINTF
  76 #  endif
  77 #endif
  78 
  79 #if defined(__CYGWIN__)
  80 #  ifndef HAVE_VSNPRINTF
  81 #    define HAVE_VSNPRINTF
  82 #  endif
  83 #endif
  84 
  85 #if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410)
  86 #  ifndef HAVE_VSNPRINTF
  87 #    define HAVE_VSNPRINTF
  88 #  endif
  89 #endif
  90 
  91 #ifndef HAVE_VSNPRINTF
  92 #  ifdef MSDOS
  93 /* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
  94    but for now we just assume it doesn't. */
  95 #    define NO_vsnprintf
  96 #  endif
  97 #  ifdef __TURBOC__
  98 #    define NO_vsnprintf
  99 #  endif
 100 #  ifdef WIN32
 101 /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
 102 #    if !defined(vsnprintf) && !defined(NO_vsnprintf)
 103 #      if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 )
 104 #         define vsnprintf _vsnprintf
 105 #      endif
 106 #    endif
 107 #  endif
 108 #  ifdef __SASC
 109 #    define NO_vsnprintf
 110 #  endif
 111 #  ifdef VMS
 112 #    define NO_vsnprintf
 113 #  endif
 114 #  ifdef __OS400__
 115 #    define NO_vsnprintf
 116 #  endif
 117 #  ifdef __MVS__
 118 #    define NO_vsnprintf
 119 #  endif
 120 #endif
 121 
 122 /* unlike snprintf (which is required in C99, yet still not supported by
 123    Microsoft more than a decade later!), _snprintf does not guarantee null
 124    termination of the result -- however this is only used in gzlib.c where
 125    the result is assured to fit in the space provided */
 126 #ifdef _MSC_VER
 127 #  define snprintf _snprintf
 128 #endif
 129 
 130 #ifndef local
 131 #  define local static
 132 #endif
 133 /* compile with -Dlocal if your debugger can't find static symbols */
 134 
 135 /* gz* functions always use library allocation functions */
 136 #ifndef STDC
 137   extern voidp  malloc OF((uInt size));
 138   extern void   free   OF((voidpf ptr));
 139 #endif
 140 
 141 /* get errno and strerror definition */
 142 #if defined UNDER_CE
 143 #  include <windows.h>
 144 #  define zstrerror() gz_strwinerror((DWORD)GetLastError())
 145 #else
 146 #  ifndef NO_STRERROR
 147 #    include <errno.h>
 148 #    define zstrerror() strerror(errno)
 149 #  else
 150 #    define zstrerror() "stdio error (consult errno)"
 151 #  endif
 152 #endif
 153 
 154 /* provide prototypes for these when building zlib without LFS */
 155 #if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0
 156     ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
 157     ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
 158     ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
 159     ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
 160 #endif
 161 
 162 /* default memLevel */
 163 #if MAX_MEM_LEVEL >= 8
 164 #  define DEF_MEM_LEVEL 8
 165 #else
 166 #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
 167 #endif
 168 
 169 /* default i/o buffer size -- double this for output when reading (this and
 170    twice this must be able to fit in an unsigned type) */
 171 #define GZBUFSIZE 8192
 172 
 173 /* gzip modes, also provide a little integrity check on the passed structure */
 174 #define GZ_NONE 0
 175 #define GZ_READ 7247
 176 #define GZ_WRITE 31153
 177 #define GZ_APPEND 1     /* mode set to GZ_WRITE after the file is opened */
 178 
 179 /* values for gz_state how */
 180 #define LOOK 0      /* look for a gzip header */
 181 #define COPY 1      /* copy input directly */
 182 #define GZIP 2      /* decompress a gzip stream */
 183 
 184 /* internal gzip file state data structure */
 185 typedef struct {
 186         /* exposed contents for gzgetc() macro */
 187     struct gzFile_s x;      /* "x" for exposed */
 188                             /* x.have: number of bytes available at x.next */
 189                             /* x.next: next output data to deliver or write */
 190                             /* x.pos: current position in uncompressed data */
 191         /* used for both reading and writing */
 192     int mode;               /* see gzip modes above */
 193     int fd;                 /* file descriptor */
 194     char *path;             /* path or fd for error messages */
 195     unsigned size;          /* buffer size, zero if not allocated yet */
 196     unsigned want;          /* requested buffer size, default is GZBUFSIZE */
 197     unsigned char *in;      /* input buffer */
 198     unsigned char *out;     /* output buffer (double-sized when reading) */
 199     int direct;             /* 0 if processing gzip, 1 if transparent */
 200         /* just for reading */
 201     int how;                /* 0: get header, 1: copy, 2: decompress */
 202     z_off64_t start;        /* where the gzip data started, for rewinding */
 203     int eof;                /* true if end of input file reached */
 204     int past;               /* true if read requested past end */
 205         /* just for writing */
 206     int level;              /* compression level */
 207     int strategy;           /* compression strategy */
 208         /* seek request */
 209     z_off64_t skip;         /* amount to skip (already rewound if backwards) */
 210     int seek;               /* true if seek request pending */
 211         /* error information */
 212     int err;                /* error code */
 213     char *msg;              /* error message */
 214         /* zlib inflate or deflate stream */
 215     z_stream strm;          /* stream structure in-place (not a pointer) */
 216 } gz_state;
 217 typedef gz_state FAR *gz_statep;
 218 
 219 /* shared functions */
 220 void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *));
 221 #if defined UNDER_CE
 222 char ZLIB_INTERNAL *gz_strwinerror OF((DWORD error));
 223 #endif
 224 
 225 /* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t
 226    value -- needed when comparing unsigned to z_off64_t, which is signed
 227    (possible z_off64_t types off_t, off64_t, and long are all signed) */
 228 #ifdef INT_MAX
 229 #  define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX)
 230 #else
 231 unsigned ZLIB_INTERNAL gz_intmax OF((void));
 232 #  define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax())
 233 #endif