1205194Sdelphij/* gzguts.h -- zlib internal header definitions for gz* operations
2250261Sdelphij * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler
3205194Sdelphij * For conditions of distribution and use, see copyright notice in zlib.h
4205194Sdelphij */
5205194Sdelphij
6206708Sdelphij#ifdef _LARGEFILE64_SOURCE
7205194Sdelphij#  ifndef _LARGEFILE_SOURCE
8206002Sdelphij#    define _LARGEFILE_SOURCE 1
9205194Sdelphij#  endif
10205194Sdelphij#  ifdef _FILE_OFFSET_BITS
11205194Sdelphij#    undef _FILE_OFFSET_BITS
12205194Sdelphij#  endif
13205194Sdelphij#endif
14205194Sdelphij
15237410Sdelphij#ifdef HAVE_HIDDEN
16206924Sdelphij#  define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
17206924Sdelphij#else
18206924Sdelphij#  define ZLIB_INTERNAL
19206924Sdelphij#endif
20205194Sdelphij
21205194Sdelphij#include <stdio.h>
22205194Sdelphij#include "zlib.h"
23205194Sdelphij#ifdef STDC
24205194Sdelphij#  include <string.h>
25205194Sdelphij#  include <stdlib.h>
26205194Sdelphij#  include <limits.h>
27205194Sdelphij#endif
28205194Sdelphij#include <fcntl.h>
29205194Sdelphij
30237410Sdelphij#ifdef _WIN32
31237410Sdelphij#  include <stddef.h>
32237410Sdelphij#endif
33237410Sdelphij
34237410Sdelphij#if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32)
35237410Sdelphij#  include <io.h>
36237410Sdelphij#endif
37237410Sdelphij
38250261Sdelphij#ifdef WINAPI_FAMILY
39250261Sdelphij#  define open _open
40250261Sdelphij#  define read _read
41250261Sdelphij#  define write _write
42250261Sdelphij#  define close _close
43250261Sdelphij#endif
44250261Sdelphij
45205194Sdelphij#ifdef NO_DEFLATE       /* for compatibility with old definition */
46205194Sdelphij#  define NO_GZCOMPRESS
47205194Sdelphij#endif
48205194Sdelphij
49237410Sdelphij#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
50237410Sdelphij#  ifndef HAVE_VSNPRINTF
51237410Sdelphij#    define HAVE_VSNPRINTF
52237410Sdelphij#  endif
53205194Sdelphij#endif
54205194Sdelphij
55237410Sdelphij#if defined(__CYGWIN__)
56237410Sdelphij#  ifndef HAVE_VSNPRINTF
57237410Sdelphij#    define HAVE_VSNPRINTF
58237410Sdelphij#  endif
59237410Sdelphij#endif
60237410Sdelphij
61237410Sdelphij#if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410)
62237410Sdelphij#  ifndef HAVE_VSNPRINTF
63237410Sdelphij#    define HAVE_VSNPRINTF
64237410Sdelphij#  endif
65237410Sdelphij#endif
66237410Sdelphij
67237410Sdelphij#ifndef HAVE_VSNPRINTF
68237410Sdelphij#  ifdef MSDOS
69237410Sdelphij/* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
70250261Sdelphij   but for now we just assume it doesn't. */
71237410Sdelphij#    define NO_vsnprintf
72237410Sdelphij#  endif
73237410Sdelphij#  ifdef __TURBOC__
74237410Sdelphij#    define NO_vsnprintf
75237410Sdelphij#  endif
76237410Sdelphij#  ifdef WIN32
77237410Sdelphij/* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
78237410Sdelphij#    if !defined(vsnprintf) && !defined(NO_vsnprintf)
79237410Sdelphij#      if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 )
80237410Sdelphij#         define vsnprintf _vsnprintf
81237410Sdelphij#      endif
82237410Sdelphij#    endif
83237410Sdelphij#  endif
84237410Sdelphij#  ifdef __SASC
85237410Sdelphij#    define NO_vsnprintf
86237410Sdelphij#  endif
87237410Sdelphij#  ifdef VMS
88237410Sdelphij#    define NO_vsnprintf
89237410Sdelphij#  endif
90237410Sdelphij#  ifdef __OS400__
91237410Sdelphij#    define NO_vsnprintf
92237410Sdelphij#  endif
93237410Sdelphij#  ifdef __MVS__
94237410Sdelphij#    define NO_vsnprintf
95237410Sdelphij#  endif
96237410Sdelphij#endif
97237410Sdelphij
98250261Sdelphij/* unlike snprintf (which is required in C99, yet still not supported by
99250261Sdelphij   Microsoft more than a decade later!), _snprintf does not guarantee null
100250261Sdelphij   termination of the result -- however this is only used in gzlib.c where
101250261Sdelphij   the result is assured to fit in the space provided */
102250261Sdelphij#ifdef _MSC_VER
103250261Sdelphij#  define snprintf _snprintf
104250261Sdelphij#endif
105250261Sdelphij
106205194Sdelphij#ifndef local
107205194Sdelphij#  define local static
108205194Sdelphij#endif
109205194Sdelphij/* compile with -Dlocal if your debugger can't find static symbols */
110205194Sdelphij
111205194Sdelphij/* gz* functions always use library allocation functions */
112205194Sdelphij#ifndef STDC
113205194Sdelphij  extern voidp  malloc OF((uInt size));
114205194Sdelphij  extern void   free   OF((voidpf ptr));
115205194Sdelphij#endif
116205194Sdelphij
117205194Sdelphij/* get errno and strerror definition */
118206002Sdelphij#if defined UNDER_CE
119205194Sdelphij#  include <windows.h>
120205194Sdelphij#  define zstrerror() gz_strwinerror((DWORD)GetLastError())
121205194Sdelphij#else
122237410Sdelphij#  ifndef NO_STRERROR
123205194Sdelphij#    include <errno.h>
124205194Sdelphij#    define zstrerror() strerror(errno)
125205194Sdelphij#  else
126205194Sdelphij#    define zstrerror() "stdio error (consult errno)"
127205194Sdelphij#  endif
128205194Sdelphij#endif
129205194Sdelphij
130206708Sdelphij/* provide prototypes for these when building zlib without LFS */
131206708Sdelphij#if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0
132206708Sdelphij    ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
133206708Sdelphij    ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
134206708Sdelphij    ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
135206708Sdelphij    ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
136205194Sdelphij#endif
137205194Sdelphij
138237410Sdelphij/* default memLevel */
139237410Sdelphij#if MAX_MEM_LEVEL >= 8
140237410Sdelphij#  define DEF_MEM_LEVEL 8
141237410Sdelphij#else
142237410Sdelphij#  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
143237410Sdelphij#endif
144237410Sdelphij
145250261Sdelphij/* default i/o buffer size -- double this for output when reading (this and
146250261Sdelphij   twice this must be able to fit in an unsigned type) */
147205194Sdelphij#define GZBUFSIZE 8192
148205194Sdelphij
149205194Sdelphij/* gzip modes, also provide a little integrity check on the passed structure */
150205194Sdelphij#define GZ_NONE 0
151205194Sdelphij#define GZ_READ 7247
152205194Sdelphij#define GZ_WRITE 31153
153205194Sdelphij#define GZ_APPEND 1     /* mode set to GZ_WRITE after the file is opened */
154205194Sdelphij
155205194Sdelphij/* values for gz_state how */
156205194Sdelphij#define LOOK 0      /* look for a gzip header */
157205194Sdelphij#define COPY 1      /* copy input directly */
158205194Sdelphij#define GZIP 2      /* decompress a gzip stream */
159205194Sdelphij
160205194Sdelphij/* internal gzip file state data structure */
161205194Sdelphijtypedef struct {
162237410Sdelphij        /* exposed contents for gzgetc() macro */
163237410Sdelphij    struct gzFile_s x;      /* "x" for exposed */
164237410Sdelphij                            /* x.have: number of bytes available at x.next */
165237410Sdelphij                            /* x.next: next output data to deliver or write */
166237410Sdelphij                            /* x.pos: current position in uncompressed data */
167205194Sdelphij        /* used for both reading and writing */
168205194Sdelphij    int mode;               /* see gzip modes above */
169205194Sdelphij    int fd;                 /* file descriptor */
170205194Sdelphij    char *path;             /* path or fd for error messages */
171205194Sdelphij    unsigned size;          /* buffer size, zero if not allocated yet */
172205194Sdelphij    unsigned want;          /* requested buffer size, default is GZBUFSIZE */
173205194Sdelphij    unsigned char *in;      /* input buffer */
174205194Sdelphij    unsigned char *out;     /* output buffer (double-sized when reading) */
175237410Sdelphij    int direct;             /* 0 if processing gzip, 1 if transparent */
176205194Sdelphij        /* just for reading */
177237410Sdelphij    int how;                /* 0: get header, 1: copy, 2: decompress */
178237410Sdelphij    z_off64_t start;        /* where the gzip data started, for rewinding */
179205194Sdelphij    int eof;                /* true if end of input file reached */
180237410Sdelphij    int past;               /* true if read requested past end */
181205194Sdelphij        /* just for writing */
182205194Sdelphij    int level;              /* compression level */
183205194Sdelphij    int strategy;           /* compression strategy */
184205194Sdelphij        /* seek request */
185205194Sdelphij    z_off64_t skip;         /* amount to skip (already rewound if backwards) */
186205194Sdelphij    int seek;               /* true if seek request pending */
187205194Sdelphij        /* error information */
188205194Sdelphij    int err;                /* error code */
189205194Sdelphij    char *msg;              /* error message */
190205194Sdelphij        /* zlib inflate or deflate stream */
191205194Sdelphij    z_stream strm;          /* stream structure in-place (not a pointer) */
192205194Sdelphij} gz_state;
193205194Sdelphijtypedef gz_state FAR *gz_statep;
194205194Sdelphij
195205194Sdelphij/* shared functions */
196206924Sdelphijvoid ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *));
197206002Sdelphij#if defined UNDER_CE
198206924Sdelphijchar ZLIB_INTERNAL *gz_strwinerror OF((DWORD error));
199205194Sdelphij#endif
200205194Sdelphij
201205194Sdelphij/* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t
202205194Sdelphij   value -- needed when comparing unsigned to z_off64_t, which is signed
203205194Sdelphij   (possible z_off64_t types off_t, off64_t, and long are all signed) */
204205194Sdelphij#ifdef INT_MAX
205205194Sdelphij#  define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX)
206205194Sdelphij#else
207206924Sdelphijunsigned ZLIB_INTERNAL gz_intmax OF((void));
208205194Sdelphij#  define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax())
209205194Sdelphij#endif
210