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