Basic gzip/gunzip in memory buffer examples using zlib code.

Dependencies:   mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers gzguts.h Source File

gzguts.h

00001 /* gzguts.h -- zlib internal header definitions for gz* operations
00002  * Copyright (C) 2004, 2005, 2010, 2011, 2012 Mark Adler
00003  * For conditions of distribution and use, see copyright notice in zlib.h
00004  */
00005 
00006 #ifdef _LARGEFILE64_SOURCE
00007 #  ifndef _LARGEFILE_SOURCE
00008 #    define _LARGEFILE_SOURCE 1
00009 #  endif
00010 #  ifdef _FILE_OFFSET_BITS
00011 #    undef _FILE_OFFSET_BITS
00012 #  endif
00013 #endif
00014 
00015 #ifdef HAVE_HIDDEN
00016 #  define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
00017 #else
00018 #  define ZLIB_INTERNAL
00019 #endif
00020 
00021 #include <stdio.h>
00022 #include "zlib.h"
00023 #ifdef STDC
00024 #  include <string.h>
00025 #  include <stdlib.h>
00026 #  include <limits.h>
00027 #endif
00028 //#include <fcntl.h>
00029 
00030 #ifdef _WIN32
00031 #  include <stddef.h>
00032 #endif
00033 
00034 #if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32)
00035 #  include <io.h>
00036 #endif
00037 
00038 #ifdef NO_DEFLATE       /* for compatibility with old definition */
00039 #  define NO_GZCOMPRESS
00040 #endif
00041 
00042 #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
00043 #  ifndef HAVE_VSNPRINTF
00044 #    define HAVE_VSNPRINTF
00045 #  endif
00046 #endif
00047 
00048 #if defined(__CYGWIN__)
00049 #  ifndef HAVE_VSNPRINTF
00050 #    define HAVE_VSNPRINTF
00051 #  endif
00052 #endif
00053 
00054 #if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410)
00055 #  ifndef HAVE_VSNPRINTF
00056 #    define HAVE_VSNPRINTF
00057 #  endif
00058 #endif
00059 
00060 #ifndef HAVE_VSNPRINTF
00061 #  ifdef MSDOS
00062 /* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
00063  but for now we just assume it doesn't. */
00064 #    define NO_vsnprintf
00065 #  endif
00066 #  ifdef __TURBOC__
00067 #    define NO_vsnprintf
00068 #  endif
00069 #  ifdef WIN32
00070 /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
00071 #    if !defined(vsnprintf) && !defined(NO_vsnprintf)
00072 #      if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 )
00073 #         define vsnprintf _vsnprintf
00074 #      endif
00075 #    endif
00076 #  endif
00077 #  ifdef __SASC
00078 #    define NO_vsnprintf
00079 #  endif
00080 #  ifdef VMS
00081 #    define NO_vsnprintf
00082 #  endif
00083 #  ifdef __OS400__
00084 #    define NO_vsnprintf
00085 #  endif
00086 #  ifdef __MVS__
00087 #    define NO_vsnprintf
00088 #  endif
00089 #endif
00090 
00091 #ifndef local
00092 #  define local static
00093 #endif
00094 /* compile with -Dlocal if your debugger can't find static symbols */
00095 
00096 /* gz* functions always use library allocation functions */
00097 #ifndef STDC
00098   extern voidp  malloc OF((uInt size));
00099   extern void   free   OF((voidpf ptr));
00100 #endif
00101 
00102 /* get errno and strerror definition */
00103 #if defined UNDER_CE
00104 #  include <windows.h>
00105 #  define zstrerror() gz_strwinerror((DWORD)GetLastError())
00106 #else
00107 #  ifndef NO_STRERROR
00108 #    include <errno.h>
00109 #    define zstrerror() strerror(errno)
00110 #  else
00111 #    define zstrerror() "stdio error (consult errno)"
00112 #  endif
00113 #endif
00114 
00115 /* provide prototypes for these when building zlib without LFS */
00116 #if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0
00117     ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
00118     ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
00119     ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
00120     ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
00121 #endif
00122 
00123 /* default memLevel */
00124 #if MAX_MEM_LEVEL >= 8
00125 #  define DEF_MEM_LEVEL 8
00126 #else
00127 #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
00128 #endif
00129 
00130 /* default i/o buffer size -- double this for output when reading */
00131 #define GZBUFSIZE 8192
00132 
00133 /* gzip modes, also provide a little integrity check on the passed structure */
00134 #define GZ_NONE 0
00135 #define GZ_READ 7247
00136 #define GZ_WRITE 31153
00137 #define GZ_APPEND 1     /* mode set to GZ_WRITE after the file is opened */
00138 
00139 /* values for gz_state how */
00140 #define LOOK 0      /* look for a gzip header */
00141 #define COPY 1      /* copy input directly */
00142 #define GZIP 2      /* decompress a gzip stream */
00143 
00144 /* internal gzip file state data structure */
00145 typedef struct {
00146         /* exposed contents for gzgetc() macro */
00147     struct gzFile_s x;      /* "x" for exposed */
00148                             /* x.have: number of bytes available at x.next */
00149                             /* x.next: next output data to deliver or write */
00150                             /* x.pos: current position in uncompressed data */
00151         /* used for both reading and writing */
00152     int mode;               /* see gzip modes above */
00153     int fd;                 /* file descriptor */
00154     char *path;             /* path or fd for error messages */
00155     unsigned size;          /* buffer size, zero if not allocated yet */
00156     unsigned want;          /* requested buffer size, default is GZBUFSIZE */
00157     unsigned char *in;      /* input buffer */
00158     unsigned char *out;     /* output buffer (double-sized when reading) */
00159     int direct;             /* 0 if processing gzip, 1 if transparent */
00160         /* just for reading */
00161     int how;                /* 0: get header, 1: copy, 2: decompress */
00162     z_off64_t start;        /* where the gzip data started, for rewinding */
00163     int eof;                /* true if end of input file reached */
00164     int past;               /* true if read requested past end */
00165         /* just for writing */
00166     int level;              /* compression level */
00167     int strategy;           /* compression strategy */
00168         /* seek request */
00169     z_off64_t skip;         /* amount to skip (already rewound if backwards) */
00170     int seek;               /* true if seek request pending */
00171         /* error information */
00172     int err;                /* error code */
00173     char *msg;              /* error message */
00174         /* zlib inflate or deflate stream */
00175     z_stream strm;          /* stream structure in-place (not a pointer) */
00176 } gz_state;
00177 typedef gz_state FAR *gz_statep;
00178 
00179 /* shared functions */
00180 void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *));
00181 #if defined UNDER_CE
00182 char ZLIB_INTERNAL *gz_strwinerror OF((DWORD error));
00183 #endif
00184 
00185 /* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t
00186    value -- needed when comparing unsigned to z_off64_t, which is signed
00187    (possible z_off64_t types off_t, off64_t, and long are all signed) */
00188 #ifdef INT_MAX
00189 #  define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX)
00190 #else
00191 unsigned ZLIB_INTERNAL gz_intmax OF((void));
00192 #  define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax())
00193 #endif