Nektar++
CompressData.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: CompressData.cpp
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 #define NOMINMAX
35 
38 
39 #include <boost/archive/iterators/base64_from_binary.hpp>
40 #include <boost/archive/iterators/binary_from_base64.hpp>
41 #include <boost/archive/iterators/transform_width.hpp>
42 #include <boost/iostreams/copy.hpp>
43 #include <boost/iostreams/filter/zlib.hpp>
44 #include <boost/iostreams/filtering_stream.hpp>
45 #include <boost/assign/list_of.hpp>
46 #include <boost/lexical_cast.hpp>
47 
48 #include <set>
49 #include <cstdint>
50 
51 #ifdef NEKTAR_USE_MPI
52 #include <mpi.h>
53 #endif
54 
55 
56 #ifndef NEKTAR_VERSION
57 #define NEKTAR_VERSION "Unknown"
58 #endif
59 
60 namespace Nektar
61 {
62 namespace LibUtilities
63 {
64 
65  /**
66  * run time determination of endianness, returning an EndianType
67  */
69  {
70  union
71  {
72  std::uint32_t value;
73  std::uint8_t data[sizeof(std::uint32_t)];
74  } number;
75 
76  number.data[0] = 0x00;
77  number.data[1] = 0x01;
78  number.data[2] = 0x02;
79  number.data[3] = 0x03;
80 
81  switch (number.value)
82  {
83  case UINT32_C(0x00010203): return eEndianBig;
84  case UINT32_C(0x03020100): return eEndianLittle;
85  case UINT32_C(0x02030001): return eEndianBigWord;
86  case UINT32_C(0x01000302): return eEndianLittleWord;
87  default: return eEndianUnknown;
88  }
89  }
90 
91  namespace CompressData
92  {
93 
94  /**
95  * Return a string describing this compression and endianness
96  */
97  std::string GetCompressString(void)
98  {
99  return "B64Z-"+ EndianTypeMap[Endianness()];
100  }
101 
102  std::string GetBitSizeStr(void)
103  {
104  return boost::lexical_cast<std::string>(sizeof(void*)*8);
105  }
106 
107  /**
108  * Convert a binary string to Base 64 string
109  */
110  void BinaryStrToBase64Str(std::string &compressedDataString,
111  std::string &base64string)
112  {
113  // If the string length is not divisible by 3,
114  // pad it. There is a bug in transform_width
115  // that will make it reference past the end
116  // and crash.
117  switch (compressedDataString.length() % 3)
118  {
119  case 1:
120  compressedDataString += '\0';
121  /* Falls through. */
122  case 2:
123  compressedDataString += '\0';
124  break;
125  }
126 
127  // Convert from binary to base64.
128  typedef boost::archive::iterators::base64_from_binary<
129  boost::archive::iterators::transform_width<
130  std::string::const_iterator, 6, 8> > base64_t;
131  base64string = std::string(base64_t(compressedDataString.begin()),
132  base64_t(compressedDataString.end()));
133  }
134 
135  /**
136  * Convert a Base 64 string into a binary string
137  */
138  void Base64StrToBinaryStr(std::string &base64string,
139  std::string &compressedDataString)
140  {
141  // Convert from base64 to binary.
142  typedef boost::archive::iterators::transform_width<
143  boost::archive::iterators::binary_from_base64<
144  std::string::const_iterator>, 8, 6 > binary_t;
145  compressedDataString = std::string(binary_t(base64string.begin()),
146  binary_t(base64string.end()));
147  }
148  }
149 }
150 }
void BinaryStrToBase64Str(std::string &compressedDataString, std::string &base64string)
EndianType Endianness(void)
const std::string EndianTypeMap[]
Definition: CompressData.h:59
void Base64StrToBinaryStr(std::string &base64string, std::string &compressedDataString)