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