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

Dependencies:   mbed-rtos mbed

There are small changes needed to the zconf.h file in the zlib distribution (I used 1.2.7). The zlib license applies to the zlib code - I have only imported a subset of the source.

The MBED has limited memory, so we need the following (near the top of zconf.h) to restrict memory allocation sizes:

#define    MAX_MEM_LEVEL    3
#define    MAX_WBITS        10

Because MAX_MEM_LEVEL and MAX_WBITS are so much lower than the default, there is a danger that the mbed cannot gunzip data compressed by a 'normal' zlib build. My use-case is to gzip on the mbed more than gunzip on the mbed so I have not given much time to this issue.

I also included this (also near the top of zconf.h) to prefix defines with Z_

#define    Z_PREFIX

In zconf.h, in the zlib distribution, the includes for <fcntl.h> and <sys/types.h> need commenting out when using the online compiler. No need when using GCC4MBED.

I also looked at miniz. I chose zlib because I needed the gzip headers and miniz does not implement them.

The sample main.cpp reads source data, compresses it, decompresses it, and finally compares the input data with the output data to confirm they are the same.

    unsigned char input_data[2048];
    unsigned long input_data_length = 0;
    FILE *ifp = fopen("/local/src.txt", "r");
    if (ifp) {
        int br = fread(input_data, 1, sizeof(input_data), ifp);
        fclose(ifp);
        input_data_length = br;
    }
    printf("%s:%d: input_data_length:%lu%s", __FILE__, __LINE__, input_data_length, newline);
 
 
    unsigned char gzip_data[2048];
    unsigned long gzip_data_length = 0;
    if (input_data_length > 0) {
        gzip_data_length = sizeof(gzip_data);
        int rv = gzip(gzip_data, &gzip_data_length, input_data, input_data_length);
        if (Z_OK == rv) {
            FILE *ofp = fopen("/local/dst.gz", "w");
            if (ofp) {
                int bw = fwrite(gzip_data, 1, gzip_data_length, ofp);
                fclose(ofp);
            }
        } else {
            printf("%s:%d: %d%s", __FILE__, __LINE__, rv, newline);
        }
    }
    printf("%s:%d: gzip_data_length:%lu%s", __FILE__, __LINE__, gzip_data_length, newline);
 
 
    unsigned char output_data[2048];
    unsigned long output_data_length = 0;
    if (gzip_data_length > 0) {
        output_data_length = sizeof(output_data);
        int rv = gunzip(output_data, &output_data_length, gzip_data, gzip_data_length);
        if (Z_OK != rv) {
            printf("%s:%d: %d%s", __FILE__, __LINE__, rv, newline);
        }
    }
    printf("%s:%d: output_data_length:%lu%s", __FILE__, __LINE__, output_data_length, newline);
 
 
    if (input_data_length > 0 and input_data_length > 0) {
        bool input_matches_output = false;
        if (input_data_length == output_data_length) {
            input_matches_output = true;
            for ( size_t i = 0 ; input_matches_output && i < input_data_length ; i++ ) {
                if (input_data[i] != output_data[i]) {
                    input_matches_output = false;
                }
            }
        }
        printf("%s:%d: input (%lu bytes) %s output (%lu bytes)%s", __FILE__, __LINE__, input_data_length, input_matches_output?"matches":"does not match", output_data_length, newline);
    } else {
        printf("%s:%d: input and/or output length is 0%s", __FILE__, __LINE__, newline);
    }
Committer:
jonathonfletcher
Date:
Sun Oct 21 07:46:41 2012 +0000
Revision:
0:54f5be781526
initial checkin

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jonathonfletcher 0:54f5be781526 1 /* zutil.h -- internal interface and configuration of the compression library
jonathonfletcher 0:54f5be781526 2 * Copyright (C) 1995-2012 Jean-loup Gailly.
jonathonfletcher 0:54f5be781526 3 * For conditions of distribution and use, see copyright notice in zlib.h
jonathonfletcher 0:54f5be781526 4 */
jonathonfletcher 0:54f5be781526 5
jonathonfletcher 0:54f5be781526 6 /* WARNING: this file should *not* be used by applications. It is
jonathonfletcher 0:54f5be781526 7 part of the implementation of the compression library and is
jonathonfletcher 0:54f5be781526 8 subject to change. Applications should only use zlib.h.
jonathonfletcher 0:54f5be781526 9 */
jonathonfletcher 0:54f5be781526 10
jonathonfletcher 0:54f5be781526 11 /* @(#) $Id$ */
jonathonfletcher 0:54f5be781526 12
jonathonfletcher 0:54f5be781526 13 #ifndef ZUTIL_H
jonathonfletcher 0:54f5be781526 14 #define ZUTIL_H
jonathonfletcher 0:54f5be781526 15
jonathonfletcher 0:54f5be781526 16 #ifdef HAVE_HIDDEN
jonathonfletcher 0:54f5be781526 17 # define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
jonathonfletcher 0:54f5be781526 18 #else
jonathonfletcher 0:54f5be781526 19 # define ZLIB_INTERNAL
jonathonfletcher 0:54f5be781526 20 #endif
jonathonfletcher 0:54f5be781526 21
jonathonfletcher 0:54f5be781526 22 #include "zlib.h"
jonathonfletcher 0:54f5be781526 23
jonathonfletcher 0:54f5be781526 24 #if defined(STDC) && !defined(Z_SOLO)
jonathonfletcher 0:54f5be781526 25 # if !(defined(_WIN32_WCE) && defined(_MSC_VER))
jonathonfletcher 0:54f5be781526 26 # include <stddef.h>
jonathonfletcher 0:54f5be781526 27 # endif
jonathonfletcher 0:54f5be781526 28 # include <string.h>
jonathonfletcher 0:54f5be781526 29 # include <stdlib.h>
jonathonfletcher 0:54f5be781526 30 #endif
jonathonfletcher 0:54f5be781526 31
jonathonfletcher 0:54f5be781526 32 #ifdef Z_SOLO
jonathonfletcher 0:54f5be781526 33 typedef long ptrdiff_t; /* guess -- will be caught if guess is wrong */
jonathonfletcher 0:54f5be781526 34 #endif
jonathonfletcher 0:54f5be781526 35
jonathonfletcher 0:54f5be781526 36 #ifndef local
jonathonfletcher 0:54f5be781526 37 # define local static
jonathonfletcher 0:54f5be781526 38 #endif
jonathonfletcher 0:54f5be781526 39 /* compile with -Dlocal if your debugger can't find static symbols */
jonathonfletcher 0:54f5be781526 40
jonathonfletcher 0:54f5be781526 41 typedef unsigned char uch;
jonathonfletcher 0:54f5be781526 42 typedef uch FAR uchf;
jonathonfletcher 0:54f5be781526 43 typedef unsigned short ush;
jonathonfletcher 0:54f5be781526 44 typedef ush FAR ushf;
jonathonfletcher 0:54f5be781526 45 typedef unsigned long ulg;
jonathonfletcher 0:54f5be781526 46
jonathonfletcher 0:54f5be781526 47 extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
jonathonfletcher 0:54f5be781526 48 /* (size given to avoid silly warnings with Visual C++) */
jonathonfletcher 0:54f5be781526 49
jonathonfletcher 0:54f5be781526 50 #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
jonathonfletcher 0:54f5be781526 51
jonathonfletcher 0:54f5be781526 52 #define ERR_RETURN(strm,err) \
jonathonfletcher 0:54f5be781526 53 return (strm->msg = (char*)ERR_MSG(err), (err))
jonathonfletcher 0:54f5be781526 54 /* To be used only when the state is known to be valid */
jonathonfletcher 0:54f5be781526 55
jonathonfletcher 0:54f5be781526 56 /* common constants */
jonathonfletcher 0:54f5be781526 57
jonathonfletcher 0:54f5be781526 58 #ifndef DEF_WBITS
jonathonfletcher 0:54f5be781526 59 # define DEF_WBITS MAX_WBITS
jonathonfletcher 0:54f5be781526 60 #endif
jonathonfletcher 0:54f5be781526 61 /* default windowBits for decompression. MAX_WBITS is for compression only */
jonathonfletcher 0:54f5be781526 62
jonathonfletcher 0:54f5be781526 63 #if MAX_MEM_LEVEL >= 8
jonathonfletcher 0:54f5be781526 64 # define DEF_MEM_LEVEL 8
jonathonfletcher 0:54f5be781526 65 #else
jonathonfletcher 0:54f5be781526 66 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
jonathonfletcher 0:54f5be781526 67 #endif
jonathonfletcher 0:54f5be781526 68 /* default memLevel */
jonathonfletcher 0:54f5be781526 69
jonathonfletcher 0:54f5be781526 70 #define STORED_BLOCK 0
jonathonfletcher 0:54f5be781526 71 #define STATIC_TREES 1
jonathonfletcher 0:54f5be781526 72 #define DYN_TREES 2
jonathonfletcher 0:54f5be781526 73 /* The three kinds of block type */
jonathonfletcher 0:54f5be781526 74
jonathonfletcher 0:54f5be781526 75 #define MIN_MATCH 3
jonathonfletcher 0:54f5be781526 76 #define MAX_MATCH 258
jonathonfletcher 0:54f5be781526 77 /* The minimum and maximum match lengths */
jonathonfletcher 0:54f5be781526 78
jonathonfletcher 0:54f5be781526 79 #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
jonathonfletcher 0:54f5be781526 80
jonathonfletcher 0:54f5be781526 81 /* target dependencies */
jonathonfletcher 0:54f5be781526 82
jonathonfletcher 0:54f5be781526 83 #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
jonathonfletcher 0:54f5be781526 84 # define OS_CODE 0x00
jonathonfletcher 0:54f5be781526 85 # ifndef Z_SOLO
jonathonfletcher 0:54f5be781526 86 # if defined(__TURBOC__) || defined(__BORLANDC__)
jonathonfletcher 0:54f5be781526 87 # if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
jonathonfletcher 0:54f5be781526 88 /* Allow compilation with ANSI keywords only enabled */
jonathonfletcher 0:54f5be781526 89 void _Cdecl farfree( void *block );
jonathonfletcher 0:54f5be781526 90 void *_Cdecl farmalloc( unsigned long nbytes );
jonathonfletcher 0:54f5be781526 91 # else
jonathonfletcher 0:54f5be781526 92 # include <alloc.h>
jonathonfletcher 0:54f5be781526 93 # endif
jonathonfletcher 0:54f5be781526 94 # else /* MSC or DJGPP */
jonathonfletcher 0:54f5be781526 95 # include <malloc.h>
jonathonfletcher 0:54f5be781526 96 # endif
jonathonfletcher 0:54f5be781526 97 # endif
jonathonfletcher 0:54f5be781526 98 #endif
jonathonfletcher 0:54f5be781526 99
jonathonfletcher 0:54f5be781526 100 #ifdef AMIGA
jonathonfletcher 0:54f5be781526 101 # define OS_CODE 0x01
jonathonfletcher 0:54f5be781526 102 #endif
jonathonfletcher 0:54f5be781526 103
jonathonfletcher 0:54f5be781526 104 #if defined(VAXC) || defined(VMS)
jonathonfletcher 0:54f5be781526 105 # define OS_CODE 0x02
jonathonfletcher 0:54f5be781526 106 # define F_OPEN(name, mode) \
jonathonfletcher 0:54f5be781526 107 fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
jonathonfletcher 0:54f5be781526 108 #endif
jonathonfletcher 0:54f5be781526 109
jonathonfletcher 0:54f5be781526 110 #if defined(ATARI) || defined(atarist)
jonathonfletcher 0:54f5be781526 111 # define OS_CODE 0x05
jonathonfletcher 0:54f5be781526 112 #endif
jonathonfletcher 0:54f5be781526 113
jonathonfletcher 0:54f5be781526 114 #ifdef OS2
jonathonfletcher 0:54f5be781526 115 # define OS_CODE 0x06
jonathonfletcher 0:54f5be781526 116 # if defined(M_I86) && !defined(Z_SOLO)
jonathonfletcher 0:54f5be781526 117 # include <malloc.h>
jonathonfletcher 0:54f5be781526 118 # endif
jonathonfletcher 0:54f5be781526 119 #endif
jonathonfletcher 0:54f5be781526 120
jonathonfletcher 0:54f5be781526 121 #if defined(MACOS) || defined(TARGET_OS_MAC)
jonathonfletcher 0:54f5be781526 122 # define OS_CODE 0x07
jonathonfletcher 0:54f5be781526 123 # ifndef Z_SOLO
jonathonfletcher 0:54f5be781526 124 # if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
jonathonfletcher 0:54f5be781526 125 # include <unix.h> /* for fdopen */
jonathonfletcher 0:54f5be781526 126 # else
jonathonfletcher 0:54f5be781526 127 # ifndef fdopen
jonathonfletcher 0:54f5be781526 128 # define fdopen(fd,mode) NULL /* No fdopen() */
jonathonfletcher 0:54f5be781526 129 # endif
jonathonfletcher 0:54f5be781526 130 # endif
jonathonfletcher 0:54f5be781526 131 # endif
jonathonfletcher 0:54f5be781526 132 #endif
jonathonfletcher 0:54f5be781526 133
jonathonfletcher 0:54f5be781526 134 #ifdef TOPS20
jonathonfletcher 0:54f5be781526 135 # define OS_CODE 0x0a
jonathonfletcher 0:54f5be781526 136 #endif
jonathonfletcher 0:54f5be781526 137
jonathonfletcher 0:54f5be781526 138 #ifdef WIN32
jonathonfletcher 0:54f5be781526 139 # ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */
jonathonfletcher 0:54f5be781526 140 # define OS_CODE 0x0b
jonathonfletcher 0:54f5be781526 141 # endif
jonathonfletcher 0:54f5be781526 142 #endif
jonathonfletcher 0:54f5be781526 143
jonathonfletcher 0:54f5be781526 144 #ifdef __50SERIES /* Prime/PRIMOS */
jonathonfletcher 0:54f5be781526 145 # define OS_CODE 0x0f
jonathonfletcher 0:54f5be781526 146 #endif
jonathonfletcher 0:54f5be781526 147
jonathonfletcher 0:54f5be781526 148 #if defined(_BEOS_) || defined(RISCOS)
jonathonfletcher 0:54f5be781526 149 # define fdopen(fd,mode) NULL /* No fdopen() */
jonathonfletcher 0:54f5be781526 150 #endif
jonathonfletcher 0:54f5be781526 151
jonathonfletcher 0:54f5be781526 152 #if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX
jonathonfletcher 0:54f5be781526 153 # if defined(_WIN32_WCE)
jonathonfletcher 0:54f5be781526 154 # define fdopen(fd,mode) NULL /* No fdopen() */
jonathonfletcher 0:54f5be781526 155 # ifndef _PTRDIFF_T_DEFINED
jonathonfletcher 0:54f5be781526 156 typedef int ptrdiff_t;
jonathonfletcher 0:54f5be781526 157 # define _PTRDIFF_T_DEFINED
jonathonfletcher 0:54f5be781526 158 # endif
jonathonfletcher 0:54f5be781526 159 # else
jonathonfletcher 0:54f5be781526 160 # define fdopen(fd,type) _fdopen(fd,type)
jonathonfletcher 0:54f5be781526 161 # endif
jonathonfletcher 0:54f5be781526 162 #endif
jonathonfletcher 0:54f5be781526 163
jonathonfletcher 0:54f5be781526 164 #if defined(__BORLANDC__) && !defined(MSDOS)
jonathonfletcher 0:54f5be781526 165 #pragma warn -8004
jonathonfletcher 0:54f5be781526 166 #pragma warn -8008
jonathonfletcher 0:54f5be781526 167 #pragma warn -8066
jonathonfletcher 0:54f5be781526 168 #endif
jonathonfletcher 0:54f5be781526 169
jonathonfletcher 0:54f5be781526 170 /* provide prototypes for these when building zlib without LFS */
jonathonfletcher 0:54f5be781526 171 #if !defined(_WIN32) && (!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0)
jonathonfletcher 0:54f5be781526 172 ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
jonathonfletcher 0:54f5be781526 173 ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
jonathonfletcher 0:54f5be781526 174 #endif
jonathonfletcher 0:54f5be781526 175
jonathonfletcher 0:54f5be781526 176 /* common defaults */
jonathonfletcher 0:54f5be781526 177
jonathonfletcher 0:54f5be781526 178 #ifndef OS_CODE
jonathonfletcher 0:54f5be781526 179 # define OS_CODE 0x03 /* assume Unix */
jonathonfletcher 0:54f5be781526 180 #endif
jonathonfletcher 0:54f5be781526 181
jonathonfletcher 0:54f5be781526 182 #ifndef F_OPEN
jonathonfletcher 0:54f5be781526 183 # define F_OPEN(name, mode) fopen((name), (mode))
jonathonfletcher 0:54f5be781526 184 #endif
jonathonfletcher 0:54f5be781526 185
jonathonfletcher 0:54f5be781526 186 /* functions */
jonathonfletcher 0:54f5be781526 187
jonathonfletcher 0:54f5be781526 188 #if defined(pyr) || defined(Z_SOLO)
jonathonfletcher 0:54f5be781526 189 # define NO_MEMCPY
jonathonfletcher 0:54f5be781526 190 #endif
jonathonfletcher 0:54f5be781526 191 #if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
jonathonfletcher 0:54f5be781526 192 /* Use our own functions for small and medium model with MSC <= 5.0.
jonathonfletcher 0:54f5be781526 193 * You may have to use the same strategy for Borland C (untested).
jonathonfletcher 0:54f5be781526 194 * The __SC__ check is for Symantec.
jonathonfletcher 0:54f5be781526 195 */
jonathonfletcher 0:54f5be781526 196 # define NO_MEMCPY
jonathonfletcher 0:54f5be781526 197 #endif
jonathonfletcher 0:54f5be781526 198 #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
jonathonfletcher 0:54f5be781526 199 # define HAVE_MEMCPY
jonathonfletcher 0:54f5be781526 200 #endif
jonathonfletcher 0:54f5be781526 201 #ifdef HAVE_MEMCPY
jonathonfletcher 0:54f5be781526 202 # ifdef SMALL_MEDIUM /* MSDOS small or medium model */
jonathonfletcher 0:54f5be781526 203 # define zmemcpy _fmemcpy
jonathonfletcher 0:54f5be781526 204 # define zmemcmp _fmemcmp
jonathonfletcher 0:54f5be781526 205 # define zmemzero(dest, len) _fmemset(dest, 0, len)
jonathonfletcher 0:54f5be781526 206 # else
jonathonfletcher 0:54f5be781526 207 # define zmemcpy memcpy
jonathonfletcher 0:54f5be781526 208 # define zmemcmp memcmp
jonathonfletcher 0:54f5be781526 209 # define zmemzero(dest, len) memset(dest, 0, len)
jonathonfletcher 0:54f5be781526 210 # endif
jonathonfletcher 0:54f5be781526 211 #else
jonathonfletcher 0:54f5be781526 212 void ZLIB_INTERNAL zmemcpy OF((Bytef* dest, const Bytef* source, uInt len));
jonathonfletcher 0:54f5be781526 213 int ZLIB_INTERNAL zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len));
jonathonfletcher 0:54f5be781526 214 void ZLIB_INTERNAL zmemzero OF((Bytef* dest, uInt len));
jonathonfletcher 0:54f5be781526 215 #endif
jonathonfletcher 0:54f5be781526 216
jonathonfletcher 0:54f5be781526 217 /* Diagnostic functions */
jonathonfletcher 0:54f5be781526 218 #ifdef DEBUG
jonathonfletcher 0:54f5be781526 219 # include <stdio.h>
jonathonfletcher 0:54f5be781526 220 extern int ZLIB_INTERNAL z_verbose;
jonathonfletcher 0:54f5be781526 221 extern void ZLIB_INTERNAL z_error OF((char *m));
jonathonfletcher 0:54f5be781526 222 # define Assert(cond,msg) {if(!(cond)) z_error(msg);}
jonathonfletcher 0:54f5be781526 223 # define Trace(x) {if (z_verbose>=0) fprintf x ;}
jonathonfletcher 0:54f5be781526 224 # define Tracev(x) {if (z_verbose>0) fprintf x ;}
jonathonfletcher 0:54f5be781526 225 # define Tracevv(x) {if (z_verbose>1) fprintf x ;}
jonathonfletcher 0:54f5be781526 226 # define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
jonathonfletcher 0:54f5be781526 227 # define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
jonathonfletcher 0:54f5be781526 228 #else
jonathonfletcher 0:54f5be781526 229 # define Assert(cond,msg)
jonathonfletcher 0:54f5be781526 230 # define Trace(x)
jonathonfletcher 0:54f5be781526 231 # define Tracev(x)
jonathonfletcher 0:54f5be781526 232 # define Tracevv(x)
jonathonfletcher 0:54f5be781526 233 # define Tracec(c,x)
jonathonfletcher 0:54f5be781526 234 # define Tracecv(c,x)
jonathonfletcher 0:54f5be781526 235 #endif
jonathonfletcher 0:54f5be781526 236
jonathonfletcher 0:54f5be781526 237 #ifndef Z_SOLO
jonathonfletcher 0:54f5be781526 238 voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items,
jonathonfletcher 0:54f5be781526 239 unsigned size));
jonathonfletcher 0:54f5be781526 240 void ZLIB_INTERNAL zcfree OF((voidpf opaque, voidpf ptr));
jonathonfletcher 0:54f5be781526 241 #endif
jonathonfletcher 0:54f5be781526 242
jonathonfletcher 0:54f5be781526 243 #define ZALLOC(strm, items, size) \
jonathonfletcher 0:54f5be781526 244 (*((strm)->zalloc))((strm)->opaque, (items), (size))
jonathonfletcher 0:54f5be781526 245 #define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
jonathonfletcher 0:54f5be781526 246 #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
jonathonfletcher 0:54f5be781526 247
jonathonfletcher 0:54f5be781526 248 /* Reverse the bytes in a 32-bit value */
jonathonfletcher 0:54f5be781526 249 #define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
jonathonfletcher 0:54f5be781526 250 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
jonathonfletcher 0:54f5be781526 251
jonathonfletcher 0:54f5be781526 252 #endif /* ZUTIL_H */