Jonathon Fletcher / Mbed 2 deprecated MBEDZlib

Dependencies:   mbed-rtos mbed

Revision:
0:54f5be781526
diff -r 000000000000 -r 54f5be781526 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Oct 21 07:46:41 2012 +0000
@@ -0,0 +1,167 @@
+#include "mbed.h"
+#include "rtos.h"
+
+#include "zlib.h"
+
+
+static const char *newline = "\r\n";
+
+
+BusOut leds(LED1, LED2, LED3, LED4);
+LocalFileSystem fs("local");
+
+
+
+/** GUNZIP a from src buffer to dst buffer
+ @param dst : destination buffer
+ @param dst_length : pointer to destination buffer length
+ @param src : source buffer
+ @param src_length : source buffer length
+ @return Z_OK on success, zlib error (<0) on failure
+ */
+int gunzip(unsigned char *dst, unsigned long *dst_length, unsigned char *src, unsigned long src_length)
+{
+    z_stream stream;
+    memset(&stream, 0, sizeof(stream));
+
+    stream.next_in = src;
+    stream.avail_in = src_length;
+
+    stream.next_out = dst;
+    stream.avail_out = *dst_length;
+
+    int rv = inflateInit2(&stream, 15 + 16);
+    if (Z_OK == rv) {
+        rv = inflate(&stream, Z_NO_FLUSH);
+        if (Z_STREAM_END == rv) {
+            inflateEnd(&stream);
+            rv = Z_OK;
+        }
+    }
+
+    if (Z_OK == rv) {
+        *dst_length = stream.total_out;
+    } else {
+        *dst_length = 0;
+    }
+
+    return rv;
+}
+
+/** GUNZIP a from src buffer to dst buffer
+ @param dst : destination buffer
+ @param dst_length : pointer to destination buffer length
+ @param src : source buffer
+ @param src_length : source buffer length
+ @return Z_OK on success, zlib error (<0) on failure
+ */
+int gzip(unsigned char *dst, unsigned long *dst_length, unsigned char *src, unsigned long src_length)
+{
+    z_stream        stream;
+    memset(&stream, 0, sizeof(stream));
+
+    stream.next_in = src;
+    stream.avail_in = src_length;
+
+    stream.next_out = Z_NULL;
+    stream.avail_out = 0;
+
+    /* add 16 to MAX_WBITS to specify gzip format - it gets taken off again in defaultInit2 */
+    int rv = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 16 + MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
+    if (Z_OK == rv) {
+        unsigned long dst_bound = deflateBound(&stream, stream.avail_in) + 12; /* 12 bytes for the gzip header */
+        if (dst_bound > *dst_length) {
+            rv = Z_MEM_ERROR;
+        } else {
+            stream.next_out   = dst;
+            stream.avail_out = dst_bound;
+        }
+    }
+
+    if (Z_OK == rv) {
+        gz_header        header;
+        memset(&header, 0, sizeof(header));
+        rv = deflateSetHeader(&stream, &header);
+    }
+
+    if (Z_OK == rv) {
+        rv = deflate(&stream, Z_FINISH);
+        if (Z_STREAM_END == rv) {
+            rv = deflateEnd(&stream);
+        }
+    }
+
+    if (Z_OK == rv) {
+        *dst_length = stream.total_out;
+    } else {
+        *dst_length = 0;
+    }
+
+    return rv;
+}
+
+
+int main(int argc, char**argv)
+{
+
+    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);
+    }
+
+    while (true) {
+        leds = (leds ^ 1);
+        Thread::wait(1000);
+    }
+}