Nektar++
Functions
Nektar::LibUtilities::CompressData Namespace Reference

Functions

std::string GetCompressString (void)
 
std::string GetBitSizeStr (void)
 
void BinaryStrToBase64Str (std::string &compressedDataString, std::string &base64string)
 
void Base64StrToBinaryStr (std::string &base64string, std::string &compressedDataString)
 
template<class T >
int ZlibEncode (std::vector< T > &in, std::string &out)
 
template<class T >
int ZlibEncodeToBase64Str (std::vector< T > &in, std::string &out64)
 
template<class T >
int ZlibDecode (std::string &in, std::vector< T > &out)
 
template<class T >
int ZlibDecodeFromBase64Str (std::string &in64, std::vector< T > &out)
 

Function Documentation

◆ Base64StrToBinaryStr()

void Nektar::LibUtilities::CompressData::Base64StrToBinaryStr ( std::string &  base64string,
std::string &  compressedDataString 
)

Convert a Base 64 string into a binary string

Convert a string containing base 64 (i.e. from xml file) into a binary string

Definition at line 141 of file CompressData.cpp.

143{
144 // Convert from base64 to binary.
145 typedef boost::archive::iterators::transform_width<
146 boost::archive::iterators::binary_from_base64<
147 std::string::const_iterator>,
148 8, 6>
149 binary_t;
150 compressedDataString = std::string(binary_t(base64string.begin()),
151 binary_t(base64string.end()));
152}

Referenced by ZlibDecodeFromBase64Str().

◆ BinaryStrToBase64Str()

void Nektar::LibUtilities::CompressData::BinaryStrToBase64Str ( std::string &  compressedDataString,
std::string &  base64string 
)

Convert a binary string to Base 64 string

Convert a string containing compressed binary (i.e. from deflate) into a base 64 string

Definition at line 112 of file CompressData.cpp.

114{
115 // If the string length is not divisible by 3,
116 // pad it. There is a bug in transform_width
117 // that will make it reference past the end
118 // and crash.
119 switch (compressedDataString.length() % 3)
120 {
121 case 1:
122 compressedDataString += '\0';
123 /* Falls through. */
124 case 2:
125 compressedDataString += '\0';
126 break;
127 }
128
129 // Convert from binary to base64.
130 typedef boost::archive::iterators::base64_from_binary<
131 boost::archive::iterators::transform_width<std::string::const_iterator,
132 6, 8>>
133 base64_t;
134 base64string = std::string(base64_t(compressedDataString.begin()),
135 base64_t(compressedDataString.end()));
136}

Referenced by ZlibEncodeToBase64Str().

◆ GetBitSizeStr()

std::string Nektar::LibUtilities::CompressData::GetBitSizeStr ( void  )

◆ GetCompressString()

std::string Nektar::LibUtilities::CompressData::GetCompressString ( void  )

Return a string describing this compression and endianness

Definition at line 99 of file CompressData.cpp.

100{
101 return "B64Z-" + EndianTypeMap[Endianness()];
102}
const std::string EndianTypeMap[]
Definition: CompressData.h:57
EndianType Endianness(void)

References Nektar::LibUtilities::Endianness(), and Nektar::LibUtilities::EndianTypeMap.

Referenced by Nektar::LibUtilities::FieldIOXml::ImportFieldData(), Nektar::LibUtilities::FieldIOXml::ImportFieldDefs(), Nektar::SpatialDomains::MeshGraphIOXmlCompressed::v_ReadCurves(), Nektar::SpatialDomains::MeshGraphIOXmlCompressed::v_ReadEdges(), Nektar::SpatialDomains::MeshGraphIOXmlCompressed::v_ReadElements1D(), Nektar::SpatialDomains::MeshGraphIOXmlCompressed::v_ReadElements2D(), Nektar::SpatialDomains::MeshGraphIOXmlCompressed::v_ReadElements3D(), Nektar::SpatialDomains::MeshGraphIOXmlCompressed::v_ReadFaces(), Nektar::SpatialDomains::MeshGraphIOXmlCompressed::v_ReadVertices(), Nektar::LibUtilities::FieldIOXml::v_Write(), Nektar::SpatialDomains::MeshGraphIOXmlCompressed::v_WriteCurves(), Nektar::SpatialDomains::MeshGraphIOXmlCompressed::v_WriteEdges(), Nektar::SpatialDomains::MeshGraphIOXmlCompressed::v_WriteHexs(), Nektar::SpatialDomains::MeshGraphIOXmlCompressed::v_WritePrisms(), Nektar::SpatialDomains::MeshGraphIOXmlCompressed::v_WritePyrs(), Nektar::SpatialDomains::MeshGraphIOXmlCompressed::v_WriteQuads(), Nektar::SpatialDomains::MeshGraphIOXmlCompressed::v_WriteTets(), Nektar::SpatialDomains::MeshGraphIOXmlCompressed::v_WriteTris(), and Nektar::SpatialDomains::MeshGraphIOXmlCompressed::v_WriteVertices().

◆ ZlibDecode()

template<class T >
int Nektar::LibUtilities::CompressData::ZlibDecode ( std::string &  in,
std::vector< T > &  out 
)

Decompress a zlib-compressed string into a vector of NekDouble values.

Definition at line 144 of file CompressData.h.

145{
146 int ret;
147 unsigned have;
148 z_stream strm;
149 std::string buffer;
150 buffer.resize(CHUNK);
151 std::string output;
152
153 strm.zalloc = Z_NULL;
154 strm.zfree = Z_NULL;
155 strm.opaque = Z_NULL;
156 strm.avail_in = 0;
157 strm.next_in = Z_NULL;
158 ret = inflateInit(&strm);
159 ASSERTL0(ret == Z_OK, "Error initializing zlib decompression.");
160
161 strm.avail_in = (unsigned int)in.size();
162 strm.next_in = (unsigned char *)(&in[0]);
163
164 do
165 {
166 strm.avail_out = CHUNK;
167 strm.next_out = (unsigned char *)(&buffer[0]);
168
169 ret = inflate(&strm, Z_NO_FLUSH);
170
171 ASSERTL0(ret != Z_STREAM_ERROR, "Stream error occured.");
172
173 switch (ret)
174 {
175 case Z_NEED_DICT:
176 ret = Z_DATA_ERROR;
177 /* Falls through. */
178 case Z_DATA_ERROR:
179 case Z_MEM_ERROR:
180 (void)inflateEnd(&strm);
181 return ret;
182 }
183
184 have = CHUNK - strm.avail_out;
185 output += buffer.substr(0, have);
186 } while (strm.avail_out == 0);
187
188 (void)inflateEnd(&strm);
189
190 if (ret == Z_STREAM_END)
191 {
192 T *readFieldData = (T *)output.c_str();
193 unsigned int len =
194 (unsigned int)output.size() * sizeof(*output.c_str()) / sizeof(T);
195 out.assign(readFieldData, readFieldData + len);
196 return Z_OK;
197 }
198 else
199 {
200 return Z_DATA_ERROR;
201 }
202}
#define CHUNK
Definition: CompressData.h:43
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
array buffer
Definition: GsLib.hpp:81

References ASSERTL0, CHUNK, and Nektar::void.

Referenced by ZlibDecodeFromBase64Str().

◆ ZlibDecodeFromBase64Str()

template<class T >
int Nektar::LibUtilities::CompressData::ZlibDecodeFromBase64Str ( std::string &  in64,
std::vector< T > &  out 
)

◆ ZlibEncode()

template<class T >
int Nektar::LibUtilities::CompressData::ZlibEncode ( std::vector< T > &  in,
std::string &  out 
)

Compress a vector of NekDouble values into a string using zlib.

Definition at line 71 of file CompressData.h.

72{
73 int ret;
74 unsigned have;
75 std::string buffer;
76 buffer.resize(CHUNK);
77 z_stream strm;
78 unsigned char *input = (unsigned char *)(&in[0]);
79
80 /* allocate deflate state */
81 strm.zalloc = Z_NULL;
82 strm.zfree = Z_NULL;
83 strm.opaque = Z_NULL;
84 ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
85
86 ASSERTL0(ret == Z_OK, "Error initializing Zlib.");
87
88 strm.avail_in = (unsigned int)in.size() * sizeof(T) / sizeof(char);
89 strm.next_in = input;
90
91 // Deflate input until output buffer is no longer full.
92 do
93 {
94 strm.avail_out = CHUNK;
95 strm.next_out = (unsigned char *)(&buffer[0]);
96
97 ret = deflate(&strm, Z_FINISH);
98
99 // Deflate can return Z_OK, Z_STREAM_ERROR, Z_BUF_ERROR or
100 // Z_STREAM_END. All, except Z_STREAM_ERROR are ok.
101 ASSERTL0(ret != Z_STREAM_ERROR, "Zlib stream error");
102
103 have = CHUNK - strm.avail_out;
104 out += buffer.substr(0, have);
105 } while (strm.avail_out == 0);
106
107 // Check all input was processed.
108 ASSERTL0(strm.avail_in == 0, "Not all input was used.");
109
110 // Check stream is complete.
111 ASSERTL0(ret == Z_STREAM_END, "Stream not finished");
112
113 // Clean-up and return
114 (void)deflateEnd(&strm);
115 return Z_OK;
116}

References ASSERTL0, CHUNK, and Nektar::void.

Referenced by ZlibEncodeToBase64Str().

◆ ZlibEncodeToBase64Str()

template<class T >
int Nektar::LibUtilities::CompressData::ZlibEncodeToBase64Str ( std::vector< T > &  in,
std::string &  out64 
)