Nektar++
CompressData.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: CompressData.h
4 //
5 // For more information, please see: http://www.nektar.info/
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: Routines for compressing and inflating data
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
35 #ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_COMPRESSDATA_H
36 #define NEKTAR_LIB_UTILITIES_BASIC_UTILS_COMPRESSDATA_H
37 
39 
40 #include "zlib.h"
41 
42 // Buffer size for zlib compression/decompression
43 #define CHUNK 16384
44 
45 namespace Nektar
46 {
47 namespace LibUtilities
48 {
49 
51  {
55  eEndianBigWord, /* Middle-endian, Honeywell 316 style */
56  eEndianLittleWord /* Middle-endian, PDP-11 style */
57  };
58 
59  const std::string EndianTypeMap[] =
60  {
61  "UnknownEndian",
62  "BigEndian",
63  "LittleEndian",
64  "BigWordEndian",
65  "LittleWordEndian"
66  };
67 
69 
70  namespace CompressData
71  {
72  LIB_UTILITIES_EXPORT std::string GetCompressString(void);
73  LIB_UTILITIES_EXPORT std::string GetBitSizeStr(void);
74 
75  /**
76  * Compress a vector of NekDouble values into a string using zlib.
77  */
78  template<class T> int ZlibEncode(std::vector<T>& in, std::string& out)
79  {
80  int ret;
81  unsigned have;
82  std::string buffer;
83  buffer.resize(CHUNK);
84  z_stream strm;
85  unsigned char* input = (unsigned char*)(&in[0]);
86 
87  /* allocate deflate state */
88  strm.zalloc = Z_NULL;
89  strm.zfree = Z_NULL;
90  strm.opaque = Z_NULL;
91  ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
92 
93  ASSERTL0(ret == Z_OK, "Error initializing Zlib.");
94 
95  strm.avail_in = (unsigned int)in.size() * sizeof(T) / sizeof(char);
96  strm.next_in = input;
97 
98  // Deflate input until output buffer is no longer full.
99  do {
100  strm.avail_out = CHUNK;
101  strm.next_out = (unsigned char*)(&buffer[0]);
102 
103  ret = deflate(&strm, Z_FINISH);
104 
105  // Deflate can return Z_OK, Z_STREAM_ERROR, Z_BUF_ERROR or
106  // Z_STREAM_END. All, except Z_STREAM_ERROR are ok.
107  ASSERTL0(ret != Z_STREAM_ERROR, "Zlib stream error");
108 
109  have = CHUNK - strm.avail_out;
110  out += buffer.substr(0, have);
111 
112  } while (strm.avail_out == 0);
113 
114  // Check all input was processed.
115  ASSERTL0(strm.avail_in == 0, "Not all input was used.");
116 
117  // Check stream is complete.
118  ASSERTL0(ret == Z_STREAM_END, "Stream not finished");
119 
120  // Clean-up and return
121  (void)deflateEnd(&strm);
122  return Z_OK;
123  }
124 
125 
126  /**
127  * Convert a string containing compressed binary (i.e. from
128  * deflate) into a base 64 string
129  */
131  std::string &compressedDataString,
132  std::string &base64string);
133 
134  /**
135  * Compress a vector of NekDouble values into a base64 string.
136  */
137  template<class T>
138  int ZlibEncodeToBase64Str(std::vector<T>& in, std::string& out64)
139  {
140  std::string out;
141 
142  int ok = ZlibEncode(in,out);
143 
144  BinaryStrToBase64Str(out,out64);
145 
146  return ok;
147  }
148 
149 
150  /**
151  * Decompress a zlib-compressed string into a vector of NekDouble
152  * values.
153  */
154  template<class T>
155  int ZlibDecode(std::string& in, std::vector<T>& out)
156  {
157  int ret;
158  unsigned have;
159  z_stream strm;
160  std::string buffer;
161  buffer.resize(CHUNK);
162  std::string output;
163 
164  strm.zalloc = Z_NULL;
165  strm.zfree = Z_NULL;
166  strm.opaque = Z_NULL;
167  strm.avail_in = 0;
168  strm.next_in = Z_NULL;
169  ret = inflateInit(&strm);
170  ASSERTL0(ret == Z_OK, "Error initializing zlib decompression.");
171 
172  strm.avail_in = (unsigned int)in.size();
173  strm.next_in = (unsigned char*)(&in[0]);
174 
175  do {
176  strm.avail_out = CHUNK;
177  strm.next_out = (unsigned char*)(&buffer[0]);
178 
179  ret = inflate(&strm, Z_NO_FLUSH);
180 
181  ASSERTL0(ret != Z_STREAM_ERROR, "Stream error occured.");
182 
183  switch (ret) {
184  case Z_NEED_DICT:
185  ret = Z_DATA_ERROR;
186  /* Falls through. */
187  case Z_DATA_ERROR:
188  case Z_MEM_ERROR:
189  (void)inflateEnd(&strm);
190  return ret;
191  }
192 
193  have = CHUNK - strm.avail_out;
194  output += buffer.substr(0, have);
195 
196  } while (strm.avail_out == 0);
197 
198  (void)inflateEnd(&strm);
199 
200  if (ret == Z_STREAM_END)
201  {
202  T* readFieldData = (T*) output.c_str();
203  unsigned int len = (unsigned int)output.size() * sizeof(*output.c_str())
204  / sizeof(T);
205  out.assign( readFieldData, readFieldData + len);
206  return Z_OK;
207  }
208  else
209  {
210  return Z_DATA_ERROR;
211  }
212  }
213 
214 
215 
216  /**
217  * Convert a string containing base 64 (i.e. from xml file)
218  * into a binary string
219  */
221  std::string &base64string,
222  std::string &compressedDataString);
223 
224 
225 
226  /**
227  * Decompress a base 64 compressed binary string into a vector
228  * of NekDouble values.
229  */
230  template<class T>
231  int ZlibDecodeFromBase64Str(std::string& in64, std::vector<T>& out)
232  {
233  std::string in;
234  Base64StrToBinaryStr(in64,in);
235 
236  return ZlibDecode(in,out);
237  }
238 
239  }
240 }
241 }
242 #endif
#define CHUNK
Definition: CompressData.h:43
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
#define LIB_UTILITIES_EXPORT
array buffer
Definition: GsLib.hpp:61
void Base64StrToBinaryStr(std::string &base64string, std::string &compressedDataString)
int ZlibDecode(std::string &in, std::vector< T > &out)
Definition: CompressData.h:155
int ZlibEncode(std::vector< T > &in, std::string &out)
Definition: CompressData.h:78
int ZlibDecodeFromBase64Str(std::string &in64, std::vector< T > &out)
Definition: CompressData.h:231
void BinaryStrToBase64Str(std::string &compressedDataString, std::string &base64string)
int ZlibEncodeToBase64Str(std::vector< T > &in, std::string &out64)
Definition: CompressData.h:138
const std::string EndianTypeMap[]
Definition: CompressData.h:59
EndianType Endianness(void)
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1