1381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes/* gzguts.h -- zlib internal header definitions for gz* operations
2ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes * Copyright (C) 2004, 2005, 2010, 2011, 2012 Mark Adler
3381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes * For conditions of distribution and use, see copyright notice in zlib.h
4381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes */
5381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
6381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#ifdef _LARGEFILE64_SOURCE
7381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#  ifndef _LARGEFILE_SOURCE
8381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#    define _LARGEFILE_SOURCE 1
9381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#  endif
10381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#  ifdef _FILE_OFFSET_BITS
11381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#    undef _FILE_OFFSET_BITS
12381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#  endif
13381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#endif
14381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
1509eb358bbbb9aad3fe48dd3368c8a7a481cbda1cElliott Hughes#ifdef HAVE_HIDDEN
16381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#  define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
17381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#else
18381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#  define ZLIB_INTERNAL
19381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#endif
20381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
21381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#include <stdio.h>
22381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#include "zlib.h"
23381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#ifdef STDC
24381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#  include <string.h>
25381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#  include <stdlib.h>
26381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#  include <limits.h>
27381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#endif
28381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#include <fcntl.h>
29381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
3009eb358bbbb9aad3fe48dd3368c8a7a481cbda1cElliott Hughes#ifdef _WIN32
3109eb358bbbb9aad3fe48dd3368c8a7a481cbda1cElliott Hughes#  include <stddef.h>
3209eb358bbbb9aad3fe48dd3368c8a7a481cbda1cElliott Hughes#endif
3309eb358bbbb9aad3fe48dd3368c8a7a481cbda1cElliott Hughes
3409eb358bbbb9aad3fe48dd3368c8a7a481cbda1cElliott Hughes#if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32)
35ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  include <io.h>
36ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#endif
37ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes
38381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#ifdef NO_DEFLATE       /* for compatibility with old definition */
39381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#  define NO_GZCOMPRESS
40381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#endif
41381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
42ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
43ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  ifndef HAVE_VSNPRINTF
44ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#    define HAVE_VSNPRINTF
45ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  endif
46ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#endif
47ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes
48ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#if defined(__CYGWIN__)
49ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  ifndef HAVE_VSNPRINTF
50ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#    define HAVE_VSNPRINTF
51ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  endif
52ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#endif
53ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes
54ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410)
55ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  ifndef HAVE_VSNPRINTF
56ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#    define HAVE_VSNPRINTF
57ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  endif
58ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#endif
59ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes
60ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#ifndef HAVE_VSNPRINTF
61ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  ifdef MSDOS
62ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes/* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
63ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes but for now we just assume it doesn't. */
64ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#    define NO_vsnprintf
65ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  endif
66ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  ifdef __TURBOC__
67ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#    define NO_vsnprintf
68ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  endif
69ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  ifdef WIN32
70ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes/* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
71ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#    if !defined(vsnprintf) && !defined(NO_vsnprintf)
72ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#      if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 )
73ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#         define vsnprintf _vsnprintf
74ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#      endif
75ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#    endif
76ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  endif
77ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  ifdef __SASC
78ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#    define NO_vsnprintf
79ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  endif
80ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  ifdef VMS
81ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#    define NO_vsnprintf
82ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  endif
83ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  ifdef __OS400__
84ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#    define NO_vsnprintf
85ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  endif
86ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  ifdef __MVS__
87ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#    define NO_vsnprintf
88ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  endif
89381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#endif
90381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
91381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#ifndef local
92381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#  define local static
93381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#endif
94381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes/* compile with -Dlocal if your debugger can't find static symbols */
95381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
96381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes/* gz* functions always use library allocation functions */
97381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#ifndef STDC
98381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes  extern voidp  malloc OF((uInt size));
99381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes  extern void   free   OF((voidpf ptr));
100381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#endif
101381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
102381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes/* get errno and strerror definition */
103381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#if defined UNDER_CE
104381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#  include <windows.h>
105381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#  define zstrerror() gz_strwinerror((DWORD)GetLastError())
106381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#else
10709eb358bbbb9aad3fe48dd3368c8a7a481cbda1cElliott Hughes#  ifndef NO_STRERROR
108381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#    include <errno.h>
109381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#    define zstrerror() strerror(errno)
110381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#  else
111381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#    define zstrerror() "stdio error (consult errno)"
112381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#  endif
113381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#endif
114381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
115381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes/* provide prototypes for these when building zlib without LFS */
116381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0
117381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
118381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
119381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
120381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
121381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#endif
122381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
123ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes/* default memLevel */
124ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#if MAX_MEM_LEVEL >= 8
125ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  define DEF_MEM_LEVEL 8
126ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#else
127ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
128ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#endif
129ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes
130381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes/* default i/o buffer size -- double this for output when reading */
131381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#define GZBUFSIZE 8192
132381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
133381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes/* gzip modes, also provide a little integrity check on the passed structure */
134381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#define GZ_NONE 0
135381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#define GZ_READ 7247
136381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#define GZ_WRITE 31153
137381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#define GZ_APPEND 1     /* mode set to GZ_WRITE after the file is opened */
138381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
139381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes/* values for gz_state how */
140381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#define LOOK 0      /* look for a gzip header */
141381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#define COPY 1      /* copy input directly */
142381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#define GZIP 2      /* decompress a gzip stream */
143381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
144381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes/* internal gzip file state data structure */
145381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughestypedef struct {
146ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes        /* exposed contents for gzgetc() macro */
147ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes    struct gzFile_s x;      /* "x" for exposed */
148ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes                            /* x.have: number of bytes available at x.next */
149ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes                            /* x.next: next output data to deliver or write */
150ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes                            /* x.pos: current position in uncompressed data */
151381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        /* used for both reading and writing */
152381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    int mode;               /* see gzip modes above */
153381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    int fd;                 /* file descriptor */
154381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    char *path;             /* path or fd for error messages */
155381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    unsigned size;          /* buffer size, zero if not allocated yet */
156381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    unsigned want;          /* requested buffer size, default is GZBUFSIZE */
157381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    unsigned char *in;      /* input buffer */
158381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    unsigned char *out;     /* output buffer (double-sized when reading) */
159ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes    int direct;             /* 0 if processing gzip, 1 if transparent */
160381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        /* just for reading */
161381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    int how;                /* 0: get header, 1: copy, 2: decompress */
162ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes    z_off64_t start;        /* where the gzip data started, for rewinding */
163ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes    int eof;                /* true if end of input file reached */
164ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes    int past;               /* true if read requested past end */
165381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        /* just for writing */
166381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    int level;              /* compression level */
167381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    int strategy;           /* compression strategy */
168381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        /* seek request */
169381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    z_off64_t skip;         /* amount to skip (already rewound if backwards) */
170381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    int seek;               /* true if seek request pending */
171381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        /* error information */
172381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    int err;                /* error code */
173381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    char *msg;              /* error message */
174381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        /* zlib inflate or deflate stream */
175381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    z_stream strm;          /* stream structure in-place (not a pointer) */
176381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes} gz_state;
177381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughestypedef gz_state FAR *gz_statep;
178381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
179381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes/* shared functions */
180381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughesvoid ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *));
181381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#if defined UNDER_CE
182381716e9396b55b1adb8235b020c37344f60ab07Elliott Hugheschar ZLIB_INTERNAL *gz_strwinerror OF((DWORD error));
183381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#endif
184381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
185381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes/* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t
186381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes   value -- needed when comparing unsigned to z_off64_t, which is signed
187381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes   (possible z_off64_t types off_t, off64_t, and long are all signed) */
188381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#ifdef INT_MAX
189381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#  define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX)
190381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#else
191381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughesunsigned ZLIB_INTERNAL gz_intmax OF((void));
192381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#  define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax())
193381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#endif
194