Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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

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 129 of file CompressData.cpp.

Referenced by ZlibDecodeFromBase64Str().

131  {
132  // Convert from base64 to binary.
133  typedef boost::archive::iterators::transform_width<
134  boost::archive::iterators::binary_from_base64<
135  std::string::const_iterator>, 8, 6 > binary_t;
136  compressedDataString = std::string(binary_t(base64string.begin()),
137  binary_t(base64string.end()));
138  }
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 102 of file CompressData.cpp.

Referenced by ZlibEncodeToBase64Str().

104  {
105  // If the string length is not divisible by 3,
106  // pad it. There is a bug in transform_width
107  // that will make it reference past the end
108  // and crash.
109  switch (compressedDataString.length() % 3)
110  {
111  case 1:
112  compressedDataString += '\0';
113  case 2:
114  compressedDataString += '\0';
115  break;
116  }
117 
118  // Convert from binary to base64.
119  typedef boost::archive::iterators::base64_from_binary<
120  boost::archive::iterators::transform_width<
121  std::string::const_iterator, 6, 8> > base64_t;
122  base64string = std::string(base64_t(compressedDataString.begin()),
123  base64_t(compressedDataString.end()));
124  }
std::string Nektar::LibUtilities::CompressData::GetBitSizeStr ( void  )
std::string Nektar::LibUtilities::CompressData::GetCompressString ( void  )
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 168 of file CompressData.h.

References ASSERTL0, CHUNK, and Nektar::void.

Referenced by ZlibDecodeFromBase64Str().

169  {
170  int ret;
171  unsigned have;
172  z_stream strm;
173  std::string buffer;
174  buffer.resize(CHUNK);
175  std::string output;
176 
177  strm.zalloc = Z_NULL;
178  strm.zfree = Z_NULL;
179  strm.opaque = Z_NULL;
180  strm.avail_in = 0;
181  strm.next_in = Z_NULL;
182  ret = inflateInit(&strm);
183  ASSERTL0(ret == Z_OK, "Error initializing zlib decompression.");
184 
185  strm.avail_in = in.size();
186  strm.next_in = (unsigned char*)(&in[0]);
187 
188  do {
189  strm.avail_out = CHUNK;
190  strm.next_out = (unsigned char*)(&buffer[0]);
191 
192  ret = inflate(&strm, Z_NO_FLUSH);
193 
194  ASSERTL0(ret != Z_STREAM_ERROR, "Stream error occured.");
195 
196  switch (ret) {
197  case Z_NEED_DICT:
198  ret = Z_DATA_ERROR; /* and fall through */
199  case Z_DATA_ERROR:
200  case Z_MEM_ERROR:
201  (void)inflateEnd(&strm);
202  return ret;
203  }
204 
205  have = CHUNK - strm.avail_out;
206  output += buffer.substr(0, have);
207 
208  } while (strm.avail_out == 0);
209 
210  (void)inflateEnd(&strm);
211 
212  if (ret == Z_STREAM_END)
213  {
214  T* readFieldData = (T*) output.c_str();
215  unsigned int len = output.size() * sizeof(*output.c_str())
216  / sizeof(T);
217  out.assign( readFieldData, readFieldData + len);
218  return Z_OK;
219  }
220  else
221  {
222  return Z_DATA_ERROR;
223  }
224  }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:198
array buffer
Definition: GsLib.hpp:60
#define CHUNK
Definition: CompressData.h:53
template<class T >
int Nektar::LibUtilities::CompressData::ZlibDecodeFromBase64Str ( std::string &  in64,
std::vector< T > &  out 
)
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 91 of file CompressData.h.

References ASSERTL0, CHUNK, and Nektar::void.

Referenced by ZlibEncodeToBase64Str().

92  {
93  int ret;
94  unsigned have;
95  std::string buffer;
96  buffer.resize(CHUNK);
97  z_stream strm;
98  unsigned char* input = (unsigned char*)(&in[0]);
99 
100  /* allocate deflate state */
101  strm.zalloc = Z_NULL;
102  strm.zfree = Z_NULL;
103  strm.opaque = Z_NULL;
104  ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
105 
106  ASSERTL0(ret == Z_OK, "Error initializing Zlib.");
107 
108  strm.avail_in = in.size() * sizeof(T) / sizeof(char);
109  strm.next_in = input;
110 
111  // Deflate input until output buffer is no longer full.
112  do {
113  strm.avail_out = CHUNK;
114  strm.next_out = (unsigned char*)(&buffer[0]);
115 
116  ret = deflate(&strm, Z_FINISH);
117 
118  // Deflate can return Z_OK, Z_STREAM_ERROR, Z_BUF_ERROR or
119  // Z_STREAM_END. All, except Z_STREAM_ERROR are ok.
120  ASSERTL0(ret != Z_STREAM_ERROR, "Zlib stream error");
121 
122  have = CHUNK - strm.avail_out;
123  out += buffer.substr(0, have);
124 
125  } while (strm.avail_out == 0);
126 
127  // Check all input was processed.
128  ASSERTL0(strm.avail_in == 0, "Not all input was used.");
129 
130  // Check stream is complete.
131  ASSERTL0(ret == Z_STREAM_END, "Stream not finished");
132 
133  // Clean-up and return
134  (void)deflateEnd(&strm);
135  return Z_OK;
136  }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:198
array buffer
Definition: GsLib.hpp:60
#define CHUNK
Definition: CompressData.h:53
template<class T >
int Nektar::LibUtilities::CompressData::ZlibEncodeToBase64Str ( std::vector< T > &  in,
std::string &  out64 
)

Compress a vector of NekDouble values into a base64 string.

Definition at line 151 of file CompressData.h.

References BinaryStrToBase64Str(), and ZlibEncode().

Referenced by Nektar::LibUtilities::MeshPartition::OutputPartition(), Nektar::LibUtilities::FieldIOXml::v_Write(), Nektar::Utilities::OutputNekpp::WriteXmlCurves(), Nektar::Utilities::OutputNekpp::WriteXmlEdges(), Nektar::Utilities::OutputNekpp::WriteXmlElements(), Nektar::Utilities::OutputNekpp::WriteXmlFaces(), and Nektar::Utilities::OutputNekpp::WriteXmlNodes().

152  {
153  std::string out;
154 
155  int ok = ZlibEncode(in,out);
156 
157  BinaryStrToBase64Str(out,out64);
158 
159  return ok;
160  }
void BinaryStrToBase64Str(std::string &compressedDataString, std::string &base64string)
int ZlibEncode(std::vector< T > &in, std::string &out)
Definition: CompressData.h:91