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 /* inftrees.h -- header to use inftrees.c
jonathonfletcher 0:54f5be781526 2 * Copyright (C) 1995-2005, 2010 Mark Adler
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 /* Structure for decoding tables. Each entry provides either the
jonathonfletcher 0:54f5be781526 12 information needed to do the operation requested by the code that
jonathonfletcher 0:54f5be781526 13 indexed that table entry, or it provides a pointer to another
jonathonfletcher 0:54f5be781526 14 table that indexes more bits of the code. op indicates whether
jonathonfletcher 0:54f5be781526 15 the entry is a pointer to another table, a literal, a length or
jonathonfletcher 0:54f5be781526 16 distance, an end-of-block, or an invalid code. For a table
jonathonfletcher 0:54f5be781526 17 pointer, the low four bits of op is the number of index bits of
jonathonfletcher 0:54f5be781526 18 that table. For a length or distance, the low four bits of op
jonathonfletcher 0:54f5be781526 19 is the number of extra bits to get after the code. bits is
jonathonfletcher 0:54f5be781526 20 the number of bits in this code or part of the code to drop off
jonathonfletcher 0:54f5be781526 21 of the bit buffer. val is the actual byte to output in the case
jonathonfletcher 0:54f5be781526 22 of a literal, the base length or distance, or the offset from
jonathonfletcher 0:54f5be781526 23 the current table to the next table. Each entry is four bytes. */
jonathonfletcher 0:54f5be781526 24 typedef struct {
jonathonfletcher 0:54f5be781526 25 unsigned char op; /* operation, extra bits, table bits */
jonathonfletcher 0:54f5be781526 26 unsigned char bits; /* bits in this part of the code */
jonathonfletcher 0:54f5be781526 27 unsigned short val; /* offset in table or code value */
jonathonfletcher 0:54f5be781526 28 } code;
jonathonfletcher 0:54f5be781526 29
jonathonfletcher 0:54f5be781526 30 /* op values as set by inflate_table():
jonathonfletcher 0:54f5be781526 31 00000000 - literal
jonathonfletcher 0:54f5be781526 32 0000tttt - table link, tttt != 0 is the number of table index bits
jonathonfletcher 0:54f5be781526 33 0001eeee - length or distance, eeee is the number of extra bits
jonathonfletcher 0:54f5be781526 34 01100000 - end of block
jonathonfletcher 0:54f5be781526 35 01000000 - invalid code
jonathonfletcher 0:54f5be781526 36 */
jonathonfletcher 0:54f5be781526 37
jonathonfletcher 0:54f5be781526 38 /* Maximum size of the dynamic table. The maximum number of code structures is
jonathonfletcher 0:54f5be781526 39 1444, which is the sum of 852 for literal/length codes and 592 for distance
jonathonfletcher 0:54f5be781526 40 codes. These values were found by exhaustive searches using the program
jonathonfletcher 0:54f5be781526 41 examples/enough.c found in the zlib distribtution. The arguments to that
jonathonfletcher 0:54f5be781526 42 program are the number of symbols, the initial root table size, and the
jonathonfletcher 0:54f5be781526 43 maximum bit length of a code. "enough 286 9 15" for literal/length codes
jonathonfletcher 0:54f5be781526 44 returns returns 852, and "enough 30 6 15" for distance codes returns 592.
jonathonfletcher 0:54f5be781526 45 The initial root table size (9 or 6) is found in the fifth argument of the
jonathonfletcher 0:54f5be781526 46 inflate_table() calls in inflate.c and infback.c. If the root table size is
jonathonfletcher 0:54f5be781526 47 changed, then these maximum sizes would be need to be recalculated and
jonathonfletcher 0:54f5be781526 48 updated. */
jonathonfletcher 0:54f5be781526 49 #define ENOUGH_LENS 852
jonathonfletcher 0:54f5be781526 50 #define ENOUGH_DISTS 592
jonathonfletcher 0:54f5be781526 51 #define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS)
jonathonfletcher 0:54f5be781526 52
jonathonfletcher 0:54f5be781526 53 /* Type of code to build for inflate_table() */
jonathonfletcher 0:54f5be781526 54 typedef enum {
jonathonfletcher 0:54f5be781526 55 CODES,
jonathonfletcher 0:54f5be781526 56 LENS,
jonathonfletcher 0:54f5be781526 57 DISTS
jonathonfletcher 0:54f5be781526 58 } codetype;
jonathonfletcher 0:54f5be781526 59
jonathonfletcher 0:54f5be781526 60 int ZLIB_INTERNAL inflate_table OF((codetype type, unsigned short FAR *lens,
jonathonfletcher 0:54f5be781526 61 unsigned codes, code FAR * FAR *table,
jonathonfletcher 0:54f5be781526 62 unsigned FAR *bits, unsigned short FAR *work));