36 #ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_COMPRESSDATA_H
37 #define NEKTAR_LIB_UTILITIES_BASIC_UTILS_COMPRESSDATA_H
41 #include <boost/archive/iterators/base64_from_binary.hpp>
42 #include <boost/archive/iterators/binary_from_base64.hpp>
43 #include <boost/archive/iterators/transform_width.hpp>
44 #include <boost/iostreams/copy.hpp>
45 #include <boost/iostreams/filter/zlib.hpp>
46 #include <boost/iostreams/filtering_stream.hpp>
47 #include <boost/assign/list_of.hpp>
57 namespace LibUtilities
80 namespace CompressData
91 template<
class T>
int ZlibEncode(std::vector<T>& in, std::string& out)
98 unsigned char* input = (
unsigned char*)(&in[0]);
101 strm.zalloc = Z_NULL;
103 strm.opaque = Z_NULL;
104 ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
106 ASSERTL0(ret == Z_OK,
"Error initializing Zlib.");
108 strm.avail_in = in.size() *
sizeof(T) /
sizeof(
char);
109 strm.next_in = input;
113 strm.avail_out =
CHUNK;
114 strm.next_out = (
unsigned char*)(&buffer[0]);
116 ret = deflate(&strm, Z_FINISH);
120 ASSERTL0(ret != Z_STREAM_ERROR,
"Zlib stream error");
122 have =
CHUNK - strm.avail_out;
123 out += buffer.substr(0, have);
125 }
while (strm.avail_out == 0);
128 ASSERTL0(strm.avail_in == 0,
"Not all input was used.");
131 ASSERTL0(ret == Z_STREAM_END,
"Stream not finished");
134 (
void)deflateEnd(&strm);
144 std::string &compressedDataString,
145 std::string &base64string);
174 buffer.resize(
CHUNK);
177 strm.zalloc = Z_NULL;
179 strm.opaque = Z_NULL;
181 strm.next_in = Z_NULL;
182 ret = inflateInit(&strm);
183 ASSERTL0(ret == Z_OK,
"Error initializing zlib decompression.");
185 strm.avail_in = in.size();
186 strm.next_in = (
unsigned char*)(&in[0]);
189 strm.avail_out =
CHUNK;
190 strm.next_out = (
unsigned char*)(&buffer[0]);
192 ret = inflate(&strm, Z_NO_FLUSH);
194 ASSERTL0(ret != Z_STREAM_ERROR,
"Stream error occured.");
201 (
void)inflateEnd(&strm);
205 have =
CHUNK - strm.avail_out;
206 output += buffer.substr(0, have);
208 }
while (strm.avail_out == 0);
210 (
void)inflateEnd(&strm);
212 if (ret == Z_STREAM_END)
214 T* readFieldData = (T*) output.c_str();
215 unsigned int len = output.size() *
sizeof(*output.c_str())
217 out.assign( readFieldData, readFieldData + len);
233 std::string &base64string,
234 std::string &compressedDataString);
#define ASSERTL0(condition, msg)
void BinaryStrToBase64Str(std::string &compressedDataString, std::string &base64string)
int ZlibDecode(std::string &in, std::vector< T > &out)
std::string GetCompressString(void)
int ZlibEncode(std::vector< T > &in, std::string &out)
#define LIB_UTILITIES_EXPORT
EndianType Endianness(void)
int ZlibEncodeToBase64Str(std::vector< T > &in, std::string &out64)
std::string GetBitSizeStr(void)
const std::string EndianTypeMap[]
int ZlibDecodeFromBase64Str(std::string &in64, std::vector< T > &out)
void Base64StrToBinaryStr(std::string &base64string, std::string &compressedDataString)