Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 00004 #include "zlib.h" 00005 00006 00007 static const char *newline = "\r\n"; 00008 00009 00010 BusOut leds(LED1, LED2, LED3, LED4); 00011 LocalFileSystem fs("local"); 00012 00013 00014 00015 /** GUNZIP a from src buffer to dst buffer 00016 @param dst : destination buffer 00017 @param dst_length : pointer to destination buffer length 00018 @param src : source buffer 00019 @param src_length : source buffer length 00020 @return Z_OK on success, zlib error (<0) on failure 00021 */ 00022 int gunzip(unsigned char *dst, unsigned long *dst_length, unsigned char *src, unsigned long src_length) 00023 { 00024 z_stream stream; 00025 memset(&stream, 0, sizeof(stream)); 00026 00027 stream.next_in = src; 00028 stream.avail_in = src_length; 00029 00030 stream.next_out = dst; 00031 stream.avail_out = *dst_length; 00032 00033 int rv = inflateInit2(&stream, 15 + 16); 00034 if (Z_OK == rv) { 00035 rv = inflate(&stream, Z_NO_FLUSH); 00036 if (Z_STREAM_END == rv) { 00037 inflateEnd(&stream); 00038 rv = Z_OK; 00039 } 00040 } 00041 00042 if (Z_OK == rv) { 00043 *dst_length = stream.total_out; 00044 } else { 00045 *dst_length = 0; 00046 } 00047 00048 return rv; 00049 } 00050 00051 /** GUNZIP a from src buffer to dst buffer 00052 @param dst : destination buffer 00053 @param dst_length : pointer to destination buffer length 00054 @param src : source buffer 00055 @param src_length : source buffer length 00056 @return Z_OK on success, zlib error (<0) on failure 00057 */ 00058 int gzip(unsigned char *dst, unsigned long *dst_length, unsigned char *src, unsigned long src_length) 00059 { 00060 z_stream stream; 00061 memset(&stream, 0, sizeof(stream)); 00062 00063 stream.next_in = src; 00064 stream.avail_in = src_length; 00065 00066 stream.next_out = Z_NULL; 00067 stream.avail_out = 0; 00068 00069 /* add 16 to MAX_WBITS to specify gzip format - it gets taken off again in defaultInit2 */ 00070 int rv = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 16 + MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY); 00071 if (Z_OK == rv) { 00072 unsigned long dst_bound = deflateBound(&stream, stream.avail_in) + 12; /* 12 bytes for the gzip header */ 00073 if (dst_bound > *dst_length) { 00074 rv = Z_MEM_ERROR; 00075 } else { 00076 stream.next_out = dst; 00077 stream.avail_out = dst_bound; 00078 } 00079 } 00080 00081 if (Z_OK == rv) { 00082 gz_header header; 00083 memset(&header, 0, sizeof(header)); 00084 rv = deflateSetHeader(&stream, &header); 00085 } 00086 00087 if (Z_OK == rv) { 00088 rv = deflate(&stream, Z_FINISH); 00089 if (Z_STREAM_END == rv) { 00090 rv = deflateEnd(&stream); 00091 } 00092 } 00093 00094 if (Z_OK == rv) { 00095 *dst_length = stream.total_out; 00096 } else { 00097 *dst_length = 0; 00098 } 00099 00100 return rv; 00101 } 00102 00103 00104 int main(int argc, char**argv) 00105 { 00106 00107 unsigned char input_data[2048]; 00108 unsigned long input_data_length = 0; 00109 FILE *ifp = fopen("/local/src.txt", "r"); 00110 if (ifp) { 00111 int br = fread(input_data, 1, sizeof(input_data), ifp); 00112 fclose(ifp); 00113 input_data_length = br; 00114 } 00115 printf("%s:%d: input_data_length:%lu%s", __FILE__, __LINE__, input_data_length, newline); 00116 00117 00118 unsigned char gzip_data[2048]; 00119 unsigned long gzip_data_length = 0; 00120 if (input_data_length > 0) { 00121 gzip_data_length = sizeof(gzip_data); 00122 int rv = gzip(gzip_data, &gzip_data_length, input_data, input_data_length); 00123 if (Z_OK == rv) { 00124 FILE *ofp = fopen("/local/dst.gz", "w"); 00125 if (ofp) { 00126 int bw = fwrite(gzip_data, 1, gzip_data_length, ofp); 00127 fclose(ofp); 00128 } 00129 } else { 00130 printf("%s:%d: %d%s", __FILE__, __LINE__, rv, newline); 00131 } 00132 } 00133 printf("%s:%d: gzip_data_length:%lu%s", __FILE__, __LINE__, gzip_data_length, newline); 00134 00135 00136 unsigned char output_data[2048]; 00137 unsigned long output_data_length = 0; 00138 if (gzip_data_length > 0) { 00139 output_data_length = sizeof(output_data); 00140 int rv = gunzip(output_data, &output_data_length, gzip_data, gzip_data_length); 00141 if (Z_OK != rv) { 00142 printf("%s:%d: %d%s", __FILE__, __LINE__, rv, newline); 00143 } 00144 } 00145 printf("%s:%d: output_data_length:%lu%s", __FILE__, __LINE__, output_data_length, newline); 00146 00147 00148 if (input_data_length > 0 and input_data_length > 0) { 00149 bool input_matches_output = false; 00150 if (input_data_length == output_data_length) { 00151 input_matches_output = true; 00152 for ( size_t i = 0 ; input_matches_output && i < input_data_length ; i++ ) { 00153 if (input_data[i] != output_data[i]) { 00154 input_matches_output = false; 00155 } 00156 } 00157 } 00158 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); 00159 } else { 00160 printf("%s:%d: input and/or output length is 0%s", __FILE__, __LINE__, newline); 00161 } 00162 00163 while (true) { 00164 leds = (leds ^ 1); 00165 Thread::wait(1000); 00166 } 00167 }
Generated on Wed Jul 13 2022 09:05:31 by
1.7.2