Basic gzip/gunzip in memory buffer examples using zlib code.
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); }
History
initial checkin
2012-10-21, by jonathonfletcher [Sun, 21 Oct 2012 07:46:41 +0000] rev 0
initial checkin