1205194Sdelphij/* gzguts.h -- zlib internal header definitions for gz* operations 2254069Sdelphij * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler 3205194Sdelphij * For conditions of distribution and use, see copyright notice in zlib.h 4205194Sdelphij */ 5205194Sdelphij 6206708Sdelphij#ifdef _LARGEFILE64_SOURCE 7205194Sdelphij# ifndef _LARGEFILE_SOURCE 8206002Sdelphij# define _LARGEFILE_SOURCE 1 9205194Sdelphij# endif 10205194Sdelphij# ifdef _FILE_OFFSET_BITS 11205194Sdelphij# undef _FILE_OFFSET_BITS 12205194Sdelphij# endif 13205194Sdelphij#endif 14205194Sdelphij 15237691Sdelphij#ifdef HAVE_HIDDEN 16206924Sdelphij# define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) 17206924Sdelphij#else 18206924Sdelphij# define ZLIB_INTERNAL 19206924Sdelphij#endif 20205194Sdelphij 21205194Sdelphij#include <stdio.h> 22205194Sdelphij#include "zlib.h" 23205194Sdelphij#ifdef STDC 24205194Sdelphij# include <string.h> 25205194Sdelphij# include <stdlib.h> 26205194Sdelphij# include <limits.h> 27205194Sdelphij#endif 28205194Sdelphij#include <fcntl.h> 29205194Sdelphij 30237691Sdelphij#ifdef _WIN32 31237691Sdelphij# include <stddef.h> 32237691Sdelphij#endif 33237691Sdelphij 34237691Sdelphij#if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32) 35237691Sdelphij# include <io.h> 36237691Sdelphij#endif 37237691Sdelphij 38254069Sdelphij#ifdef WINAPI_FAMILY 39254069Sdelphij# define open _open 40254069Sdelphij# define read _read 41254069Sdelphij# define write _write 42254069Sdelphij# define close _close 43254069Sdelphij#endif 44254069Sdelphij 45205194Sdelphij#ifdef NO_DEFLATE /* for compatibility with old definition */ 46205194Sdelphij# define NO_GZCOMPRESS 47205194Sdelphij#endif 48205194Sdelphij 49237691Sdelphij#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550) 50237691Sdelphij# ifndef HAVE_VSNPRINTF 51237691Sdelphij# define HAVE_VSNPRINTF 52237691Sdelphij# endif 53205194Sdelphij#endif 54205194Sdelphij 55237691Sdelphij#if defined(__CYGWIN__) 56237691Sdelphij# ifndef HAVE_VSNPRINTF 57237691Sdelphij# define HAVE_VSNPRINTF 58237691Sdelphij# endif 59237691Sdelphij#endif 60237691Sdelphij 61237691Sdelphij#if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410) 62237691Sdelphij# ifndef HAVE_VSNPRINTF 63237691Sdelphij# define HAVE_VSNPRINTF 64237691Sdelphij# endif 65237691Sdelphij#endif 66237691Sdelphij 67237691Sdelphij#ifndef HAVE_VSNPRINTF 68237691Sdelphij# ifdef MSDOS 69237691Sdelphij/* vsnprintf may exist on some MS-DOS compilers (DJGPP?), 70254069Sdelphij but for now we just assume it doesn't. */ 71237691Sdelphij# define NO_vsnprintf 72237691Sdelphij# endif 73237691Sdelphij# ifdef __TURBOC__ 74237691Sdelphij# define NO_vsnprintf 75237691Sdelphij# endif 76237691Sdelphij# ifdef WIN32 77237691Sdelphij/* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */ 78237691Sdelphij# if !defined(vsnprintf) && !defined(NO_vsnprintf) 79237691Sdelphij# if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 ) 80237691Sdelphij# define vsnprintf _vsnprintf 81237691Sdelphij# endif 82237691Sdelphij# endif 83237691Sdelphij# endif 84237691Sdelphij# ifdef __SASC 85237691Sdelphij# define NO_vsnprintf 86237691Sdelphij# endif 87237691Sdelphij# ifdef VMS 88237691Sdelphij# define NO_vsnprintf 89237691Sdelphij# endif 90237691Sdelphij# ifdef __OS400__ 91237691Sdelphij# define NO_vsnprintf 92237691Sdelphij# endif 93237691Sdelphij# ifdef __MVS__ 94237691Sdelphij# define NO_vsnprintf 95237691Sdelphij# endif 96237691Sdelphij#endif 97237691Sdelphij 98254069Sdelphij/* unlike snprintf (which is required in C99, yet still not supported by 99254069Sdelphij Microsoft more than a decade later!), _snprintf does not guarantee null 100254069Sdelphij termination of the result -- however this is only used in gzlib.c where 101254069Sdelphij the result is assured to fit in the space provided */ 102254069Sdelphij#ifdef _MSC_VER 103254069Sdelphij# define snprintf _snprintf 104254069Sdelphij#endif 105254069Sdelphij 106205194Sdelphij#ifndef local 107205194Sdelphij# define local static 108205194Sdelphij#endif 109205194Sdelphij/* compile with -Dlocal if your debugger can't find static symbols */ 110205194Sdelphij 111205194Sdelphij/* gz* functions always use library allocation functions */ 112205194Sdelphij#ifndef STDC 113205194Sdelphij extern voidp malloc OF((uInt size)); 114205194Sdelphij extern void free OF((voidpf ptr)); 115205194Sdelphij#endif 116205194Sdelphij 117205194Sdelphij/* get errno and strerror definition */ 118206002Sdelphij#if defined UNDER_CE 119205194Sdelphij# include <windows.h> 120205194Sdelphij# define zstrerror() gz_strwinerror((DWORD)GetLastError()) 121205194Sdelphij#else 122237691Sdelphij# ifndef NO_STRERROR 123205194Sdelphij# include <errno.h> 124205194Sdelphij# define zstrerror() strerror(errno) 125205194Sdelphij# else 126205194Sdelphij# define zstrerror() "stdio error (consult errno)" 127205194Sdelphij# endif 128205194Sdelphij#endif 129205194Sdelphij 130206708Sdelphij/* provide prototypes for these when building zlib without LFS */ 131206708Sdelphij#if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0 132206708Sdelphij ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); 133206708Sdelphij ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int)); 134206708Sdelphij ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile)); 135206708Sdelphij ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile)); 136205194Sdelphij#endif 137205194Sdelphij 138237691Sdelphij/* default memLevel */ 139237691Sdelphij#if MAX_MEM_LEVEL >= 8 140237691Sdelphij# define DEF_MEM_LEVEL 8 141237691Sdelphij#else 142237691Sdelphij# define DEF_MEM_LEVEL MAX_MEM_LEVEL 143237691Sdelphij#endif 144237691Sdelphij 145254069Sdelphij/* default i/o buffer size -- double this for output when reading (this and 146254069Sdelphij twice this must be able to fit in an unsigned type) */ 147205194Sdelphij#define GZBUFSIZE 8192 148205194Sdelphij 149205194Sdelphij/* gzip modes, also provide a little integrity check on the passed structure */ 150205194Sdelphij#define GZ_NONE 0 151205194Sdelphij#define GZ_READ 7247 152205194Sdelphij#define GZ_WRITE 31153 153205194Sdelphij#define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */ 154205194Sdelphij 155205194Sdelphij/* values for gz_state how */ 156205194Sdelphij#define LOOK 0 /* look for a gzip header */ 157205194Sdelphij#define COPY 1 /* copy input directly */ 158205194Sdelphij#define GZIP 2 /* decompress a gzip stream */ 159205194Sdelphij 160205194Sdelphij/* internal gzip file state data structure */ 161205194Sdelphijtypedef struct { 162237691Sdelphij /* exposed contents for gzgetc() macro */ 163237691Sdelphij struct gzFile_s x; /* "x" for exposed */ 164237691Sdelphij /* x.have: number of bytes available at x.next */ 165237691Sdelphij /* x.next: next output data to deliver or write */ 166237691Sdelphij /* x.pos: current position in uncompressed data */ 167205194Sdelphij /* used for both reading and writing */ 168205194Sdelphij int mode; /* see gzip modes above */ 169205194Sdelphij int fd; /* file descriptor */ 170205194Sdelphij char *path; /* path or fd for error messages */ 171205194Sdelphij unsigned size; /* buffer size, zero if not allocated yet */ 172205194Sdelphij unsigned want; /* requested buffer size, default is GZBUFSIZE */ 173205194Sdelphij unsigned char *in; /* input buffer */ 174205194Sdelphij unsigned char *out; /* output buffer (double-sized when reading) */ 175237691Sdelphij int direct; /* 0 if processing gzip, 1 if transparent */ 176205194Sdelphij /* just for reading */ 177237691Sdelphij int how; /* 0: get header, 1: copy, 2: decompress */ 178237691Sdelphij z_off64_t start; /* where the gzip data started, for rewinding */ 179205194Sdelphij int eof; /* true if end of input file reached */ 180237691Sdelphij int past; /* true if read requested past end */ 181205194Sdelphij /* just for writing */ 182205194Sdelphij int level; /* compression level */ 183205194Sdelphij int strategy; /* compression strategy */ 184205194Sdelphij /* seek request */ 185205194Sdelphij z_off64_t skip; /* amount to skip (already rewound if backwards) */ 186205194Sdelphij int seek; /* true if seek request pending */ 187205194Sdelphij /* error information */ 188205194Sdelphij int err; /* error code */ 189205194Sdelphij char *msg; /* error message */ 190205194Sdelphij /* zlib inflate or deflate stream */ 191205194Sdelphij z_stream strm; /* stream structure in-place (not a pointer) */ 192205194Sdelphij} gz_state; 193205194Sdelphijtypedef gz_state FAR *gz_statep; 194205194Sdelphij 195205194Sdelphij/* shared functions */ 196206924Sdelphijvoid ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *)); 197206002Sdelphij#if defined UNDER_CE 198206924Sdelphijchar ZLIB_INTERNAL *gz_strwinerror OF((DWORD error)); 199205194Sdelphij#endif 200205194Sdelphij 201205194Sdelphij/* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t 202205194Sdelphij value -- needed when comparing unsigned to z_off64_t, which is signed 203205194Sdelphij (possible z_off64_t types off_t, off64_t, and long are all signed) */ 204205194Sdelphij#ifdef INT_MAX 205205194Sdelphij# define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX) 206205194Sdelphij#else 207206924Sdelphijunsigned ZLIB_INTERNAL gz_intmax OF((void)); 208205194Sdelphij# define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax()) 209205194Sdelphij#endif 210