1275793eaSopenharmony_ci/* gzguts.h -- zlib internal header definitions for gz* operations 2275793eaSopenharmony_ci * Copyright (C) 2004-2024 Mark Adler 3275793eaSopenharmony_ci * For conditions of distribution and use, see copyright notice in zlib.h 4275793eaSopenharmony_ci */ 5275793eaSopenharmony_ci 6275793eaSopenharmony_ci#ifdef _LARGEFILE64_SOURCE 7275793eaSopenharmony_ci# ifndef _LARGEFILE_SOURCE 8275793eaSopenharmony_ci# define _LARGEFILE_SOURCE 1 9275793eaSopenharmony_ci# endif 10275793eaSopenharmony_ci# undef _FILE_OFFSET_BITS 11275793eaSopenharmony_ci# undef _TIME_BITS 12275793eaSopenharmony_ci#endif 13275793eaSopenharmony_ci 14275793eaSopenharmony_ci#ifdef HAVE_HIDDEN 15275793eaSopenharmony_ci# define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) 16275793eaSopenharmony_ci#else 17275793eaSopenharmony_ci# define ZLIB_INTERNAL 18275793eaSopenharmony_ci#endif 19275793eaSopenharmony_ci 20275793eaSopenharmony_ci#include <stdio.h> 21275793eaSopenharmony_ci#include "zlib.h" 22275793eaSopenharmony_ci#ifdef STDC 23275793eaSopenharmony_ci# include <string.h> 24275793eaSopenharmony_ci# include <stdlib.h> 25275793eaSopenharmony_ci# include <limits.h> 26275793eaSopenharmony_ci#endif 27275793eaSopenharmony_ci 28275793eaSopenharmony_ci#ifndef _POSIX_SOURCE 29275793eaSopenharmony_ci# define _POSIX_SOURCE 30275793eaSopenharmony_ci#endif 31275793eaSopenharmony_ci#include <fcntl.h> 32275793eaSopenharmony_ci 33275793eaSopenharmony_ci#ifdef _WIN32 34275793eaSopenharmony_ci# include <stddef.h> 35275793eaSopenharmony_ci#endif 36275793eaSopenharmony_ci 37275793eaSopenharmony_ci#if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32) 38275793eaSopenharmony_ci# include <io.h> 39275793eaSopenharmony_ci#endif 40275793eaSopenharmony_ci 41275793eaSopenharmony_ci#if defined(_WIN32) 42275793eaSopenharmony_ci# define WIDECHAR 43275793eaSopenharmony_ci#endif 44275793eaSopenharmony_ci 45275793eaSopenharmony_ci#ifdef WINAPI_FAMILY 46275793eaSopenharmony_ci# define open _open 47275793eaSopenharmony_ci# define read _read 48275793eaSopenharmony_ci# define write _write 49275793eaSopenharmony_ci# define close _close 50275793eaSopenharmony_ci#endif 51275793eaSopenharmony_ci 52275793eaSopenharmony_ci#ifdef NO_DEFLATE /* for compatibility with old definition */ 53275793eaSopenharmony_ci# define NO_GZCOMPRESS 54275793eaSopenharmony_ci#endif 55275793eaSopenharmony_ci 56275793eaSopenharmony_ci#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550) 57275793eaSopenharmony_ci# ifndef HAVE_VSNPRINTF 58275793eaSopenharmony_ci# define HAVE_VSNPRINTF 59275793eaSopenharmony_ci# endif 60275793eaSopenharmony_ci#endif 61275793eaSopenharmony_ci 62275793eaSopenharmony_ci#if defined(__CYGWIN__) 63275793eaSopenharmony_ci# ifndef HAVE_VSNPRINTF 64275793eaSopenharmony_ci# define HAVE_VSNPRINTF 65275793eaSopenharmony_ci# endif 66275793eaSopenharmony_ci#endif 67275793eaSopenharmony_ci 68275793eaSopenharmony_ci#if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410) 69275793eaSopenharmony_ci# ifndef HAVE_VSNPRINTF 70275793eaSopenharmony_ci# define HAVE_VSNPRINTF 71275793eaSopenharmony_ci# endif 72275793eaSopenharmony_ci#endif 73275793eaSopenharmony_ci 74275793eaSopenharmony_ci#ifndef HAVE_VSNPRINTF 75275793eaSopenharmony_ci# ifdef MSDOS 76275793eaSopenharmony_ci/* vsnprintf may exist on some MS-DOS compilers (DJGPP?), 77275793eaSopenharmony_ci but for now we just assume it doesn't. */ 78275793eaSopenharmony_ci# define NO_vsnprintf 79275793eaSopenharmony_ci# endif 80275793eaSopenharmony_ci# ifdef __TURBOC__ 81275793eaSopenharmony_ci# define NO_vsnprintf 82275793eaSopenharmony_ci# endif 83275793eaSopenharmony_ci# ifdef WIN32 84275793eaSopenharmony_ci/* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */ 85275793eaSopenharmony_ci# if !defined(vsnprintf) && !defined(NO_vsnprintf) 86275793eaSopenharmony_ci# if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 ) 87275793eaSopenharmony_ci# define vsnprintf _vsnprintf 88275793eaSopenharmony_ci# endif 89275793eaSopenharmony_ci# endif 90275793eaSopenharmony_ci# endif 91275793eaSopenharmony_ci# ifdef __SASC 92275793eaSopenharmony_ci# define NO_vsnprintf 93275793eaSopenharmony_ci# endif 94275793eaSopenharmony_ci# ifdef VMS 95275793eaSopenharmony_ci# define NO_vsnprintf 96275793eaSopenharmony_ci# endif 97275793eaSopenharmony_ci# ifdef __OS400__ 98275793eaSopenharmony_ci# define NO_vsnprintf 99275793eaSopenharmony_ci# endif 100275793eaSopenharmony_ci# ifdef __MVS__ 101275793eaSopenharmony_ci# define NO_vsnprintf 102275793eaSopenharmony_ci# endif 103275793eaSopenharmony_ci#endif 104275793eaSopenharmony_ci 105275793eaSopenharmony_ci/* unlike snprintf (which is required in C99), _snprintf does not guarantee 106275793eaSopenharmony_ci null termination of the result -- however this is only used in gzlib.c where 107275793eaSopenharmony_ci the result is assured to fit in the space provided */ 108275793eaSopenharmony_ci#if defined(_MSC_VER) && _MSC_VER < 1900 109275793eaSopenharmony_ci# define snprintf _snprintf 110275793eaSopenharmony_ci#endif 111275793eaSopenharmony_ci 112275793eaSopenharmony_ci#ifndef local 113275793eaSopenharmony_ci# define local static 114275793eaSopenharmony_ci#endif 115275793eaSopenharmony_ci/* since "static" is used to mean two completely different things in C, we 116275793eaSopenharmony_ci define "local" for the non-static meaning of "static", for readability 117275793eaSopenharmony_ci (compile with -Dlocal if your debugger can't find static symbols) */ 118275793eaSopenharmony_ci 119275793eaSopenharmony_ci/* gz* functions always use library allocation functions */ 120275793eaSopenharmony_ci#ifndef STDC 121275793eaSopenharmony_ci extern voidp malloc(uInt size); 122275793eaSopenharmony_ci extern void free(voidpf ptr); 123275793eaSopenharmony_ci#endif 124275793eaSopenharmony_ci 125275793eaSopenharmony_ci/* get errno and strerror definition */ 126275793eaSopenharmony_ci#if defined UNDER_CE 127275793eaSopenharmony_ci# include <windows.h> 128275793eaSopenharmony_ci# define zstrerror() gz_strwinerror((DWORD)GetLastError()) 129275793eaSopenharmony_ci#else 130275793eaSopenharmony_ci# ifndef NO_STRERROR 131275793eaSopenharmony_ci# include <errno.h> 132275793eaSopenharmony_ci# define zstrerror() strerror(errno) 133275793eaSopenharmony_ci# else 134275793eaSopenharmony_ci# define zstrerror() "stdio error (consult errno)" 135275793eaSopenharmony_ci# endif 136275793eaSopenharmony_ci#endif 137275793eaSopenharmony_ci 138275793eaSopenharmony_ci/* provide prototypes for these when building zlib without LFS */ 139275793eaSopenharmony_ci#if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0 140275793eaSopenharmony_ci ZEXTERN gzFile ZEXPORT gzopen64(const char *, const char *); 141275793eaSopenharmony_ci ZEXTERN z_off64_t ZEXPORT gzseek64(gzFile, z_off64_t, int); 142275793eaSopenharmony_ci ZEXTERN z_off64_t ZEXPORT gztell64(gzFile); 143275793eaSopenharmony_ci ZEXTERN z_off64_t ZEXPORT gzoffset64(gzFile); 144275793eaSopenharmony_ci#endif 145275793eaSopenharmony_ci 146275793eaSopenharmony_ci/* default memLevel */ 147275793eaSopenharmony_ci#if MAX_MEM_LEVEL >= 8 148275793eaSopenharmony_ci# define DEF_MEM_LEVEL 8 149275793eaSopenharmony_ci#else 150275793eaSopenharmony_ci# define DEF_MEM_LEVEL MAX_MEM_LEVEL 151275793eaSopenharmony_ci#endif 152275793eaSopenharmony_ci 153275793eaSopenharmony_ci/* default i/o buffer size -- double this for output when reading (this and 154275793eaSopenharmony_ci twice this must be able to fit in an unsigned type) */ 155275793eaSopenharmony_ci#define GZBUFSIZE 8192 156275793eaSopenharmony_ci 157275793eaSopenharmony_ci/* gzip modes, also provide a little integrity check on the passed structure */ 158275793eaSopenharmony_ci#define GZ_NONE 0 159275793eaSopenharmony_ci#define GZ_READ 7247 160275793eaSopenharmony_ci#define GZ_WRITE 31153 161275793eaSopenharmony_ci#define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */ 162275793eaSopenharmony_ci 163275793eaSopenharmony_ci/* values for gz_state how */ 164275793eaSopenharmony_ci#define LOOK 0 /* look for a gzip header */ 165275793eaSopenharmony_ci#define COPY 1 /* copy input directly */ 166275793eaSopenharmony_ci#define GZIP 2 /* decompress a gzip stream */ 167275793eaSopenharmony_ci 168275793eaSopenharmony_ci/* internal gzip file state data structure */ 169275793eaSopenharmony_citypedef struct { 170275793eaSopenharmony_ci /* exposed contents for gzgetc() macro */ 171275793eaSopenharmony_ci struct gzFile_s x; /* "x" for exposed */ 172275793eaSopenharmony_ci /* x.have: number of bytes available at x.next */ 173275793eaSopenharmony_ci /* x.next: next output data to deliver or write */ 174275793eaSopenharmony_ci /* x.pos: current position in uncompressed data */ 175275793eaSopenharmony_ci /* used for both reading and writing */ 176275793eaSopenharmony_ci int mode; /* see gzip modes above */ 177275793eaSopenharmony_ci int fd; /* file descriptor */ 178275793eaSopenharmony_ci char *path; /* path or fd for error messages */ 179275793eaSopenharmony_ci unsigned size; /* buffer size, zero if not allocated yet */ 180275793eaSopenharmony_ci unsigned want; /* requested buffer size, default is GZBUFSIZE */ 181275793eaSopenharmony_ci unsigned char *in; /* input buffer (double-sized when writing) */ 182275793eaSopenharmony_ci unsigned char *out; /* output buffer (double-sized when reading) */ 183275793eaSopenharmony_ci int direct; /* 0 if processing gzip, 1 if transparent */ 184275793eaSopenharmony_ci /* just for reading */ 185275793eaSopenharmony_ci int how; /* 0: get header, 1: copy, 2: decompress */ 186275793eaSopenharmony_ci z_off64_t start; /* where the gzip data started, for rewinding */ 187275793eaSopenharmony_ci int eof; /* true if end of input file reached */ 188275793eaSopenharmony_ci int past; /* true if read requested past end */ 189275793eaSopenharmony_ci /* just for writing */ 190275793eaSopenharmony_ci int level; /* compression level */ 191275793eaSopenharmony_ci int strategy; /* compression strategy */ 192275793eaSopenharmony_ci int reset; /* true if a reset is pending after a Z_FINISH */ 193275793eaSopenharmony_ci /* seek request */ 194275793eaSopenharmony_ci z_off64_t skip; /* amount to skip (already rewound if backwards) */ 195275793eaSopenharmony_ci int seek; /* true if seek request pending */ 196275793eaSopenharmony_ci /* error information */ 197275793eaSopenharmony_ci int err; /* error code */ 198275793eaSopenharmony_ci char *msg; /* error message */ 199275793eaSopenharmony_ci /* zlib inflate or deflate stream */ 200275793eaSopenharmony_ci z_stream strm; /* stream structure in-place (not a pointer) */ 201275793eaSopenharmony_ci} gz_state; 202275793eaSopenharmony_citypedef gz_state FAR *gz_statep; 203275793eaSopenharmony_ci 204275793eaSopenharmony_ci/* shared functions */ 205275793eaSopenharmony_civoid ZLIB_INTERNAL gz_error(gz_statep, int, const char *); 206275793eaSopenharmony_ci#if defined UNDER_CE 207275793eaSopenharmony_cichar ZLIB_INTERNAL *gz_strwinerror(DWORD error); 208275793eaSopenharmony_ci#endif 209275793eaSopenharmony_ci 210275793eaSopenharmony_ci/* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t 211275793eaSopenharmony_ci value -- needed when comparing unsigned to z_off64_t, which is signed 212275793eaSopenharmony_ci (possible z_off64_t types off_t, off64_t, and long are all signed) */ 213275793eaSopenharmony_ciunsigned ZLIB_INTERNAL gz_intmax(void); 214275793eaSopenharmony_ci#define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax()) 215