1#ifndef GZGUTS_H_
2#define GZGUTS_H_
3/* gzguts.h -- zlib internal header definitions for gz* operations
4 * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013, 2016 Mark Adler
5 * For conditions of distribution and use, see copyright notice in zlib.h
6 */
7
8#ifdef _LARGEFILE64_SOURCE
9# ifndef _LARGEFILE_SOURCE
10# define _LARGEFILE_SOURCE 1
11# endif
12# ifdef _FILE_OFFSET_BITS
13# undef _FILE_OFFSET_BITS
14# endif
15#endif
16
17#if defined(HAVE_VISIBILITY_INTERNAL)
18# define ZLIB_INTERNAL __attribute__((visibility ("internal")))
19#elif defined(HAVE_VISIBILITY_HIDDEN)
20# define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
21#else
22# define ZLIB_INTERNAL
23#endif
24
25#include <stdio.h>
26#include <string.h>
27#include <stdlib.h>
28#include <limits.h>
29#include <fcntl.h>
30
31#if defined(ZLIB_COMPAT)
32# include "zlib.h"
33#else
34# include "zlib-ng.h"
35#endif
36
37#ifdef WIN32
38# include <stddef.h>
39#endif
40
41#if !defined(_MSC_VER) || defined(__MINGW__)
42# include <unistd.h> /* for lseek(), read(), close(), write(), unlink() */
43#endif
44
45#if defined(_MSC_VER) || defined(WIN32)
46# include <io.h>
47#endif
48
49#if defined(_WIN32) || defined(__MINGW__)
50# define WIDECHAR
51#endif
52
53#ifdef WINAPI_FAMILY
54# define open _open
55# define read _read
56# define write _write
57# define close _close
58#endif
59
60/* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
61#if !defined(STDC99) && !defined(__CYGWIN__) && !defined(__MINGW__) && defined(WIN32)
62# if !defined(vsnprintf)
63# if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 )
64# define vsnprintf _vsnprintf
65# endif
66# endif
67#endif
68
69/* unlike snprintf (which is required in C99), _snprintf does not guarantee
70 null termination of the result -- however this is only used in gzlib.c
71 where the result is assured to fit in the space provided */
72#if defined(_MSC_VER) && _MSC_VER < 1900
73# define snprintf _snprintf
74#endif
75
76/* get errno and strerror definition */
77#ifndef NO_STRERROR
78# include <errno.h>
79# define zstrerror() strerror(errno)
80#else
81# define zstrerror() "stdio error (consult errno)"
82#endif
83
84/* provide prototypes for these when building zlib without LFS */
85#if (!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0) && defined(WITH_GZFILEOP)
86 ZEXTERN gzFile ZEXPORT PREFIX(gzopen64)(const char *, const char *);
87 ZEXTERN z_off64_t ZEXPORT PREFIX(gzseek64)(gzFile, z_off64_t, int);
88 ZEXTERN z_off64_t ZEXPORT PREFIX(gztell64)(gzFile);
89 ZEXTERN z_off64_t ZEXPORT PREFIX(gzoffset64)(gzFile);
90#endif
91
92/* default memLevel */
93#if MAX_MEM_LEVEL >= 8
94# define DEF_MEM_LEVEL 8
95#else
96# define DEF_MEM_LEVEL MAX_MEM_LEVEL
97#endif
98
99/* default i/o buffer size -- double this for output when reading (this and
100 twice this must be able to fit in an unsigned type) */
101#if defined(S390_DFLTCC_DEFLATE) || defined(S390_DFLTCC_INFLATE)
102#define GZBUFSIZE 262144 /* DFLTCC works faster with larger buffers */
103#else
104#define GZBUFSIZE 8192
105#endif
106
107/* gzip modes, also provide a little integrity check on the passed structure */
108#define GZ_NONE 0
109#define GZ_READ 7247
110#define GZ_WRITE 31153
111#define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */
112
113/* values for gz_state how */
114#define LOOK 0 /* look for a gzip header */
115#define COPY 1 /* copy input directly */
116#define GZIP 2 /* decompress a gzip stream */
117
118/* internal gzip file state data structure */
119typedef struct {
120 /* exposed contents for gzgetc() macro */
121 struct gzFile_s x; /* "x" for exposed */
122 /* x.have: number of bytes available at x.next */
123 /* x.next: next output data to deliver or write */
124 /* x.pos: current position in uncompressed data */
125 /* used for both reading and writing */
126 int mode; /* see gzip modes above */
127 int fd; /* file descriptor */
128 char *path; /* path or fd for error messages */
129 unsigned size; /* buffer size, zero if not allocated yet */
130 unsigned want; /* requested buffer size, default is GZBUFSIZE */
131 unsigned char *in; /* input buffer (double-sized when writing) */
132 unsigned char *out; /* output buffer (double-sized when reading) */
133 int direct; /* 0 if processing gzip, 1 if transparent */
134 /* just for reading */
135 int how; /* 0: get header, 1: copy, 2: decompress */
136 z_off64_t start; /* where the gzip data started, for rewinding */
137 int eof; /* true if end of input file reached */
138 int past; /* true if read requested past end */
139 /* just for writing */
140 int level; /* compression level */
141 int strategy; /* compression strategy */
142 int reset; /* true if a reset is pending after a Z_FINISH */
143 /* seek request */
144 z_off64_t skip; /* amount to skip (already rewound if backwards) */
145 int seek; /* true if seek request pending */
146 /* error information */
147 int err; /* error code */
148 char *msg; /* error message */
149 /* zlib inflate or deflate stream */
150 PREFIX3(stream) strm; /* stream structure in-place (not a pointer) */
151} gz_state;
152typedef gz_state *gz_statep;
153
154/* shared functions */
155void ZLIB_INTERNAL gz_error(gz_state *, int, const char *);
156
157/* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t
158 value -- needed when comparing unsigned to z_off64_t, which is signed
159 (possible z_off64_t types off_t, off64_t, and long are all signed) */
160#ifdef INT_MAX
161# define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX)
162#else
163unsigned ZLIB_INTERNAL gz_intmax(void);
164# define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax())
165#endif
166
167#endif /* GZGUTS_H_ */
168