xref: /dragonfly/contrib/zlib-1.2/gzguts.h (revision e041647a)
1fe927c51SPeter Avalos /* gzguts.h -- zlib internal header definitions for gz* operations
2*e041647aSSascha Wildner  * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013, 2016 Mark Adler
3fe927c51SPeter Avalos  * For conditions of distribution and use, see copyright notice in zlib.h
4fe927c51SPeter Avalos  */
5fe927c51SPeter Avalos 
6fe927c51SPeter Avalos #ifdef _LARGEFILE64_SOURCE
7fe927c51SPeter Avalos #  ifndef _LARGEFILE_SOURCE
8fe927c51SPeter Avalos #    define _LARGEFILE_SOURCE 1
9fe927c51SPeter Avalos #  endif
10fe927c51SPeter Avalos #  ifdef _FILE_OFFSET_BITS
11fe927c51SPeter Avalos #    undef _FILE_OFFSET_BITS
12fe927c51SPeter Avalos #  endif
13fe927c51SPeter Avalos #endif
14fe927c51SPeter Avalos 
15712f98b7SJohn Marino #ifdef HAVE_HIDDEN
16fe927c51SPeter Avalos #  define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
17fe927c51SPeter Avalos #else
18fe927c51SPeter Avalos #  define ZLIB_INTERNAL
19fe927c51SPeter Avalos #endif
20fe927c51SPeter Avalos 
21fe927c51SPeter Avalos #include <stdio.h>
22fe927c51SPeter Avalos #include "zlib.h"
23fe927c51SPeter Avalos #ifdef STDC
24fe927c51SPeter Avalos #  include <string.h>
25fe927c51SPeter Avalos #  include <stdlib.h>
26fe927c51SPeter Avalos #  include <limits.h>
27fe927c51SPeter Avalos #endif
28*e041647aSSascha Wildner 
29*e041647aSSascha Wildner #ifndef _POSIX_SOURCE
30*e041647aSSascha Wildner #  define _POSIX_SOURCE
31*e041647aSSascha Wildner #endif
32fe927c51SPeter Avalos #include <fcntl.h>
33fe927c51SPeter Avalos 
34712f98b7SJohn Marino #ifdef _WIN32
35712f98b7SJohn Marino #  include <stddef.h>
36712f98b7SJohn Marino #endif
37712f98b7SJohn Marino 
38712f98b7SJohn Marino #if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32)
39712f98b7SJohn Marino #  include <io.h>
40712f98b7SJohn Marino #endif
41712f98b7SJohn Marino 
42*e041647aSSascha Wildner #if defined(_WIN32) || defined(__CYGWIN__)
43*e041647aSSascha Wildner #  define WIDECHAR
44*e041647aSSascha Wildner #endif
45*e041647aSSascha Wildner 
4653ddf67cSJohn Marino #ifdef WINAPI_FAMILY
4753ddf67cSJohn Marino #  define open _open
4853ddf67cSJohn Marino #  define read _read
4953ddf67cSJohn Marino #  define write _write
5053ddf67cSJohn Marino #  define close _close
5153ddf67cSJohn Marino #endif
5253ddf67cSJohn Marino 
53fe927c51SPeter Avalos #ifdef NO_DEFLATE       /* for compatibility with old definition */
54fe927c51SPeter Avalos #  define NO_GZCOMPRESS
55fe927c51SPeter Avalos #endif
56fe927c51SPeter Avalos 
57712f98b7SJohn Marino #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
58712f98b7SJohn Marino #  ifndef HAVE_VSNPRINTF
59712f98b7SJohn Marino #    define HAVE_VSNPRINTF
60712f98b7SJohn Marino #  endif
61712f98b7SJohn Marino #endif
62712f98b7SJohn Marino 
63712f98b7SJohn Marino #if defined(__CYGWIN__)
64712f98b7SJohn Marino #  ifndef HAVE_VSNPRINTF
65712f98b7SJohn Marino #    define HAVE_VSNPRINTF
66712f98b7SJohn Marino #  endif
67712f98b7SJohn Marino #endif
68712f98b7SJohn Marino 
69712f98b7SJohn Marino #if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410)
70712f98b7SJohn Marino #  ifndef HAVE_VSNPRINTF
71712f98b7SJohn Marino #    define HAVE_VSNPRINTF
72712f98b7SJohn Marino #  endif
73712f98b7SJohn Marino #endif
74712f98b7SJohn Marino 
75712f98b7SJohn Marino #ifndef HAVE_VSNPRINTF
76712f98b7SJohn Marino #  ifdef MSDOS
77712f98b7SJohn Marino /* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
78712f98b7SJohn Marino    but for now we just assume it doesn't. */
79712f98b7SJohn Marino #    define NO_vsnprintf
80712f98b7SJohn Marino #  endif
81712f98b7SJohn Marino #  ifdef __TURBOC__
82712f98b7SJohn Marino #    define NO_vsnprintf
83712f98b7SJohn Marino #  endif
84712f98b7SJohn Marino #  ifdef WIN32
85712f98b7SJohn Marino /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
86712f98b7SJohn Marino #    if !defined(vsnprintf) && !defined(NO_vsnprintf)
87712f98b7SJohn Marino #      if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 )
88fe927c51SPeter Avalos #         define vsnprintf _vsnprintf
89fe927c51SPeter Avalos #      endif
90712f98b7SJohn Marino #    endif
91712f98b7SJohn Marino #  endif
92712f98b7SJohn Marino #  ifdef __SASC
93712f98b7SJohn Marino #    define NO_vsnprintf
94712f98b7SJohn Marino #  endif
95712f98b7SJohn Marino #  ifdef VMS
96712f98b7SJohn Marino #    define NO_vsnprintf
97712f98b7SJohn Marino #  endif
98712f98b7SJohn Marino #  ifdef __OS400__
99712f98b7SJohn Marino #    define NO_vsnprintf
100712f98b7SJohn Marino #  endif
101712f98b7SJohn Marino #  ifdef __MVS__
102712f98b7SJohn Marino #    define NO_vsnprintf
103712f98b7SJohn Marino #  endif
104712f98b7SJohn Marino #endif
105fe927c51SPeter Avalos 
106*e041647aSSascha Wildner /* unlike snprintf (which is required in C99), _snprintf does not guarantee
107*e041647aSSascha Wildner    null termination of the result -- however this is only used in gzlib.c where
10853ddf67cSJohn Marino    the result is assured to fit in the space provided */
109*e041647aSSascha Wildner #if defined(_MSC_VER) && _MSC_VER < 1900
11053ddf67cSJohn Marino #  define snprintf _snprintf
11153ddf67cSJohn Marino #endif
11253ddf67cSJohn Marino 
113fe927c51SPeter Avalos #ifndef local
114fe927c51SPeter Avalos #  define local static
115fe927c51SPeter Avalos #endif
116*e041647aSSascha Wildner /* since "static" is used to mean two completely different things in C, we
117*e041647aSSascha Wildner    define "local" for the non-static meaning of "static", for readability
118*e041647aSSascha Wildner    (compile with -Dlocal if your debugger can't find static symbols) */
119fe927c51SPeter Avalos 
120fe927c51SPeter Avalos /* gz* functions always use library allocation functions */
121fe927c51SPeter Avalos #ifndef STDC
122fe927c51SPeter Avalos   extern voidp  malloc OF((uInt size));
123fe927c51SPeter Avalos   extern void   free   OF((voidpf ptr));
124fe927c51SPeter Avalos #endif
125fe927c51SPeter Avalos 
126fe927c51SPeter Avalos /* get errno and strerror definition */
127fe927c51SPeter Avalos #if defined UNDER_CE
128fe927c51SPeter Avalos #  include <windows.h>
129fe927c51SPeter Avalos #  define zstrerror() gz_strwinerror((DWORD)GetLastError())
130fe927c51SPeter Avalos #else
131712f98b7SJohn Marino #  ifndef NO_STRERROR
132fe927c51SPeter Avalos #    include <errno.h>
133fe927c51SPeter Avalos #    define zstrerror() strerror(errno)
134fe927c51SPeter Avalos #  else
135fe927c51SPeter Avalos #    define zstrerror() "stdio error (consult errno)"
136fe927c51SPeter Avalos #  endif
137fe927c51SPeter Avalos #endif
138fe927c51SPeter Avalos 
139fe927c51SPeter Avalos /* provide prototypes for these when building zlib without LFS */
140fe927c51SPeter Avalos #if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0
141fe927c51SPeter Avalos     ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
142fe927c51SPeter Avalos     ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
143fe927c51SPeter Avalos     ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
144fe927c51SPeter Avalos     ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
145fe927c51SPeter Avalos #endif
146fe927c51SPeter Avalos 
147712f98b7SJohn Marino /* default memLevel */
148712f98b7SJohn Marino #if MAX_MEM_LEVEL >= 8
149712f98b7SJohn Marino #  define DEF_MEM_LEVEL 8
150712f98b7SJohn Marino #else
151712f98b7SJohn Marino #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
152712f98b7SJohn Marino #endif
153712f98b7SJohn Marino 
15453ddf67cSJohn Marino /* default i/o buffer size -- double this for output when reading (this and
15553ddf67cSJohn Marino    twice this must be able to fit in an unsigned type) */
156fe927c51SPeter Avalos #define GZBUFSIZE 8192
157fe927c51SPeter Avalos 
158fe927c51SPeter Avalos /* gzip modes, also provide a little integrity check on the passed structure */
159fe927c51SPeter Avalos #define GZ_NONE 0
160fe927c51SPeter Avalos #define GZ_READ 7247
161fe927c51SPeter Avalos #define GZ_WRITE 31153
162fe927c51SPeter Avalos #define GZ_APPEND 1     /* mode set to GZ_WRITE after the file is opened */
163fe927c51SPeter Avalos 
164fe927c51SPeter Avalos /* values for gz_state how */
165fe927c51SPeter Avalos #define LOOK 0      /* look for a gzip header */
166fe927c51SPeter Avalos #define COPY 1      /* copy input directly */
167fe927c51SPeter Avalos #define GZIP 2      /* decompress a gzip stream */
168fe927c51SPeter Avalos 
169fe927c51SPeter Avalos /* internal gzip file state data structure */
170fe927c51SPeter Avalos typedef struct {
171712f98b7SJohn Marino         /* exposed contents for gzgetc() macro */
172712f98b7SJohn Marino     struct gzFile_s x;      /* "x" for exposed */
173712f98b7SJohn Marino                             /* x.have: number of bytes available at x.next */
174712f98b7SJohn Marino                             /* x.next: next output data to deliver or write */
175712f98b7SJohn Marino                             /* x.pos: current position in uncompressed data */
176fe927c51SPeter Avalos         /* used for both reading and writing */
177fe927c51SPeter Avalos     int mode;               /* see gzip modes above */
178fe927c51SPeter Avalos     int fd;                 /* file descriptor */
179fe927c51SPeter Avalos     char *path;             /* path or fd for error messages */
180fe927c51SPeter Avalos     unsigned size;          /* buffer size, zero if not allocated yet */
181fe927c51SPeter Avalos     unsigned want;          /* requested buffer size, default is GZBUFSIZE */
182*e041647aSSascha Wildner     unsigned char *in;      /* input buffer (double-sized when writing) */
183fe927c51SPeter Avalos     unsigned char *out;     /* output buffer (double-sized when reading) */
184712f98b7SJohn Marino     int direct;             /* 0 if processing gzip, 1 if transparent */
185fe927c51SPeter Avalos         /* just for reading */
186fe927c51SPeter Avalos     int how;                /* 0: get header, 1: copy, 2: decompress */
187712f98b7SJohn Marino     z_off64_t start;        /* where the gzip data started, for rewinding */
188712f98b7SJohn Marino     int eof;                /* true if end of input file reached */
189712f98b7SJohn Marino     int past;               /* true if read requested past end */
190fe927c51SPeter Avalos         /* just for writing */
191fe927c51SPeter Avalos     int level;              /* compression level */
192fe927c51SPeter Avalos     int strategy;           /* compression strategy */
193fe927c51SPeter Avalos         /* seek request */
194fe927c51SPeter Avalos     z_off64_t skip;         /* amount to skip (already rewound if backwards) */
195fe927c51SPeter Avalos     int seek;               /* true if seek request pending */
196fe927c51SPeter Avalos         /* error information */
197fe927c51SPeter Avalos     int err;                /* error code */
198fe927c51SPeter Avalos     char *msg;              /* error message */
199fe927c51SPeter Avalos         /* zlib inflate or deflate stream */
200fe927c51SPeter Avalos     z_stream strm;          /* stream structure in-place (not a pointer) */
201fe927c51SPeter Avalos } gz_state;
202fe927c51SPeter Avalos typedef gz_state FAR *gz_statep;
203fe927c51SPeter Avalos 
204fe927c51SPeter Avalos /* shared functions */
205fe927c51SPeter Avalos void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *));
206fe927c51SPeter Avalos #if defined UNDER_CE
207fe927c51SPeter Avalos char ZLIB_INTERNAL *gz_strwinerror OF((DWORD error));
208fe927c51SPeter Avalos #endif
209fe927c51SPeter Avalos 
210fe927c51SPeter Avalos /* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t
211fe927c51SPeter Avalos    value -- needed when comparing unsigned to z_off64_t, which is signed
212fe927c51SPeter Avalos    (possible z_off64_t types off_t, off64_t, and long are all signed) */
213fe927c51SPeter Avalos #ifdef INT_MAX
214fe927c51SPeter Avalos #  define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX)
215fe927c51SPeter Avalos #else
216fe927c51SPeter Avalos unsigned ZLIB_INTERNAL gz_intmax OF((void));
217fe927c51SPeter Avalos #  define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax())
218fe927c51SPeter Avalos #endif
219