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