gzguts.h 6.7 KB
Newer Older
M
Mark Adler 已提交
1
/* gzguts.h -- zlib internal header definitions for gz* operations
S
shuaiyutao 已提交
2
 * Copyright (C) 2004-2019 Mark Adler
M
Mark Adler 已提交
3 4 5
 * For conditions of distribution and use, see copyright notice in zlib.h
 */

M
Mark Adler 已提交
6
#ifdef _LARGEFILE64_SOURCE
M
Mark Adler 已提交
7
#  ifndef _LARGEFILE_SOURCE
M
Mark Adler 已提交
8
#    define _LARGEFILE_SOURCE 1
M
Mark Adler 已提交
9 10 11 12 13 14
#  endif
#  ifdef _FILE_OFFSET_BITS
#    undef _FILE_OFFSET_BITS
#  endif
#endif

15
#ifdef HAVE_HIDDEN
M
Mark Adler 已提交
16 17 18 19
#  define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
#else
#  define ZLIB_INTERNAL
#endif
M
Mark Adler 已提交
20 21 22

#include <stdio.h>
#include "zlib.h"
M
Mark Adler 已提交
23 24 25
#ifdef STDC
#  include <string.h>
#  include <stdlib.h>
M
Mark Adler 已提交
26
#  include <limits.h>
M
Mark Adler 已提交
27
#endif
28 29 30 31

#ifndef _POSIX_SOURCE
#  define _POSIX_SOURCE
#endif
M
Mark Adler 已提交
32
#include <fcntl.h>
K
k00229000 已提交
33
#include <unistd.h>
M
Mark Adler 已提交
34

35 36 37 38
#ifdef _WIN32
#  include <stddef.h>
#endif

39
#if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32)
40 41 42
#  include <io.h>
#endif

S
shuaiyutao 已提交
43
#if defined(_WIN32)
44 45 46
#  define WIDECHAR
#endif

47 48 49 50 51 52 53
#ifdef WINAPI_FAMILY
#  define open _open
#  define read _read
#  define write _write
#  define close _close
#endif

M
Mark Adler 已提交
54 55 56 57
#ifdef NO_DEFLATE       /* for compatibility with old definition */
#  define NO_GZCOMPRESS
#endif

58 59 60 61 62 63 64 65 66 67 68 69
#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
#  ifndef HAVE_VSNPRINTF
#    define HAVE_VSNPRINTF
#  endif
#endif

#if defined(__CYGWIN__)
#  ifndef HAVE_VSNPRINTF
#    define HAVE_VSNPRINTF
#  endif
#endif

70 71 72 73 74 75
#if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410)
#  ifndef HAVE_VSNPRINTF
#    define HAVE_VSNPRINTF
#  endif
#endif

76 77 78
#ifndef HAVE_VSNPRINTF
#  ifdef MSDOS
/* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
79
   but for now we just assume it doesn't. */
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#    define NO_vsnprintf
#  endif
#  ifdef __TURBOC__
#    define NO_vsnprintf
#  endif
#  ifdef WIN32
/* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
#    if !defined(vsnprintf) && !defined(NO_vsnprintf)
#      if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 )
#         define vsnprintf _vsnprintf
#      endif
#    endif
#  endif
#  ifdef __SASC
#    define NO_vsnprintf
#  endif
#  ifdef VMS
#    define NO_vsnprintf
#  endif
#  ifdef __OS400__
#    define NO_vsnprintf
#  endif
#  ifdef __MVS__
#    define NO_vsnprintf
#  endif
M
Mark Adler 已提交
105 106
#endif

107 108
/* unlike snprintf (which is required in C99), _snprintf does not guarantee
   null termination of the result -- however this is only used in gzlib.c where
109
   the result is assured to fit in the space provided */
110
#if defined(_MSC_VER) && _MSC_VER < 1900
111 112 113
#  define snprintf _snprintf
#endif

M
Mark Adler 已提交
114 115 116
#ifndef local
#  define local static
#endif
117 118 119
/* since "static" is used to mean two completely different things in C, we
   define "local" for the non-static meaning of "static", for readability
   (compile with -Dlocal if your debugger can't find static symbols) */
M
Mark Adler 已提交
120 121 122 123 124 125 126 127

/* gz* functions always use library allocation functions */
#ifndef STDC
  extern voidp  malloc OF((uInt size));
  extern void   free   OF((voidpf ptr));
#endif

/* get errno and strerror definition */
M
Mark Adler 已提交
128
#if defined UNDER_CE
M
Mark Adler 已提交
129 130
#  include <windows.h>
#  define zstrerror() gz_strwinerror((DWORD)GetLastError())
M
Mark Adler 已提交
131
#else
M
Mark Adler 已提交
132
#  ifndef NO_STRERROR
M
Mark Adler 已提交
133 134 135 136 137 138 139
#    include <errno.h>
#    define zstrerror() strerror(errno)
#  else
#    define zstrerror() "stdio error (consult errno)"
#  endif
#endif

M
Mark Adler 已提交
140
/* provide prototypes for these when building zlib without LFS */
M
Mark Adler 已提交
141
#if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0
M
Mark Adler 已提交
142
    ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
M
Mark Adler 已提交
143 144 145
    ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
    ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
    ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
M
Mark Adler 已提交
146 147
#endif

148 149 150 151 152 153 154
/* default memLevel */
#if MAX_MEM_LEVEL >= 8
#  define DEF_MEM_LEVEL 8
#else
#  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
#endif

155 156
/* default i/o buffer size -- double this for output when reading (this and
   twice this must be able to fit in an unsigned type) */
M
Mark Adler 已提交
157 158 159 160 161 162 163 164
#define GZBUFSIZE 8192

/* gzip modes, also provide a little integrity check on the passed structure */
#define GZ_NONE 0
#define GZ_READ 7247
#define GZ_WRITE 31153
#define GZ_APPEND 1     /* mode set to GZ_WRITE after the file is opened */

M
Mark Adler 已提交
165 166 167 168 169
/* values for gz_state how */
#define LOOK 0      /* look for a gzip header */
#define COPY 1      /* copy input directly */
#define GZIP 2      /* decompress a gzip stream */

M
Mark Adler 已提交
170 171
/* internal gzip file state data structure */
typedef struct {
172 173 174 175 176
        /* exposed contents for gzgetc() macro */
    struct gzFile_s x;      /* "x" for exposed */
                            /* x.have: number of bytes available at x.next */
                            /* x.next: next output data to deliver or write */
                            /* x.pos: current position in uncompressed data */
M
Mark Adler 已提交
177 178 179 180 181 182
        /* used for both reading and writing */
    int mode;               /* see gzip modes above */
    int fd;                 /* file descriptor */
    char *path;             /* path or fd for error messages */
    unsigned size;          /* buffer size, zero if not allocated yet */
    unsigned want;          /* requested buffer size, default is GZBUFSIZE */
183
    unsigned char *in;      /* input buffer (double-sized when writing) */
M
Mark Adler 已提交
184
    unsigned char *out;     /* output buffer (double-sized when reading) */
185
    int direct;             /* 0 if processing gzip, 1 if transparent */
M
Mark Adler 已提交
186
        /* just for reading */
M
Mark Adler 已提交
187
    int how;                /* 0: get header, 1: copy, 2: decompress */
188 189 190
    z_off64_t start;        /* where the gzip data started, for rewinding */
    int eof;                /* true if end of input file reached */
    int past;               /* true if read requested past end */
M
Mark Adler 已提交
191 192 193
        /* just for writing */
    int level;              /* compression level */
    int strategy;           /* compression strategy */
S
shuaiyutao 已提交
194
    int reset;              /* true if a reset is pending after a Z_FINISH */
M
Mark Adler 已提交
195 196
        /* seek request */
    z_off64_t skip;         /* amount to skip (already rewound if backwards) */
M
Mark Adler 已提交
197
    int seek;               /* true if seek request pending */
M
Mark Adler 已提交
198 199 200 201 202 203 204 205 206
        /* error information */
    int err;                /* error code */
    char *msg;              /* error message */
        /* zlib inflate or deflate stream */
    z_stream strm;          /* stream structure in-place (not a pointer) */
} gz_state;
typedef gz_state FAR *gz_statep;

/* shared functions */
M
Mark Adler 已提交
207
void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *));
M
Mark Adler 已提交
208
#if defined UNDER_CE
M
Mark Adler 已提交
209
char ZLIB_INTERNAL *gz_strwinerror OF((DWORD error));
M
Mark Adler 已提交
210
#endif
M
Mark Adler 已提交
211 212 213 214 215 216 217

/* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t
   value -- needed when comparing unsigned to z_off64_t, which is signed
   (possible z_off64_t types off_t, off64_t, and long are all signed) */
#ifdef INT_MAX
#  define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX)
#else
M
Mark Adler 已提交
218
unsigned ZLIB_INTERNAL gz_intmax OF((void));
M
Mark Adler 已提交
219 220
#  define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax())
#endif