35#ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_COMPRESSDATA_H
36#define NEKTAR_LIB_UTILITIES_BASIC_UTILS_COMPRESSDATA_H
60 "LittleEndian",
"BigWordEndian",
73template <
class T>
int ZlibEncode(std::vector<T> &in, std::string &out)
80 unsigned char *input = (
unsigned char *)(&in[0]);
86 ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
88 ASSERTL0(ret == Z_OK,
"Error initializing Zlib.");
90 strm.avail_in = (
unsigned int)in.size() *
sizeof(T) /
sizeof(
char);
96 strm.avail_out =
CHUNK;
97 strm.next_out = (
unsigned char *)(&
buffer[0]);
99 ret = deflate(&strm, Z_FINISH);
103 ASSERTL0(ret != Z_STREAM_ERROR,
"Zlib stream error");
105 have =
CHUNK - strm.avail_out;
106 out +=
buffer.substr(0, have);
108 }
while (strm.avail_out == 0);
111 ASSERTL0(strm.avail_in == 0,
"Not all input was used.");
114 ASSERTL0(ret == Z_STREAM_END,
"Stream not finished");
117 (
void)deflateEnd(&strm);
126 std::string &compressedDataString, std::string &base64string);
147template <
class T>
int ZlibDecode(std::string &in, std::vector<T> &out)
156 strm.zalloc = Z_NULL;
158 strm.opaque = Z_NULL;
160 strm.next_in = Z_NULL;
161 ret = inflateInit(&strm);
162 ASSERTL0(ret == Z_OK,
"Error initializing zlib decompression.");
164 strm.avail_in = (
unsigned int)in.size();
165 strm.next_in = (
unsigned char *)(&in[0]);
169 strm.avail_out =
CHUNK;
170 strm.next_out = (
unsigned char *)(&
buffer[0]);
172 ret = inflate(&strm, Z_NO_FLUSH);
174 ASSERTL0(ret != Z_STREAM_ERROR,
"Stream error occured.");
183 (
void)inflateEnd(&strm);
187 have =
CHUNK - strm.avail_out;
188 output +=
buffer.substr(0, have);
190 }
while (strm.avail_out == 0);
192 (
void)inflateEnd(&strm);
194 if (ret == Z_STREAM_END)
196 T *readFieldData = (T *)output.c_str();
198 (
unsigned int)output.size() *
sizeof(*output.c_str()) /
sizeof(T);
199 out.assign(readFieldData, readFieldData + len);
213 std::string &base64string, std::string &compressedDataString);
#define ASSERTL0(condition, msg)
#define LIB_UTILITIES_EXPORT
void Base64StrToBinaryStr(std::string &base64string, std::string &compressedDataString)
std::string GetBitSizeStr(void)
int ZlibDecode(std::string &in, std::vector< T > &out)
int ZlibEncode(std::vector< T > &in, std::string &out)
int ZlibDecodeFromBase64Str(std::string &in64, std::vector< T > &out)
void BinaryStrToBase64Str(std::string &compressedDataString, std::string &base64string)
std::string GetCompressString(void)
int ZlibEncodeToBase64Str(std::vector< T > &in, std::string &out64)
const std::string EndianTypeMap[]
EndianType Endianness(void)
The above copyright notice and this permission notice shall be included.