Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Routines for compressing and inflating data
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 #define NOMINMAX
36 
40 
41 #include <set>
42 
43 #ifdef NEKTAR_USE_MPI
44 #include <mpi.h>
45 #endif
46 
47 
48 #ifndef NEKTAR_VERSION
49 #define NEKTAR_VERSION "Unknown"
50 #endif
51 
52 namespace Nektar
53 {
54 namespace LibUtilities
55 {
56 
57  /**
58  * run time determination of endianness, returning an EndianType
59  */
61  {
62  union
63  {
64  boost::uint32_t value;
65  boost::uint8_t data[sizeof(boost::uint32_t)];
66  } number;
67 
68  number.data[0] = 0x00;
69  number.data[1] = 0x01;
70  number.data[2] = 0x02;
71  number.data[3] = 0x03;
72 
73  switch (number.value)
74  {
75  case UINT32_C(0x00010203): return eEndianBig;
76  case UINT32_C(0x03020100): return eEndianLittle;
77  case UINT32_C(0x02030001): return eEndianBigWord;
78  case UINT32_C(0x01000302): return eEndianLittleWord;
79  default: return eEndianUnknown;
80  }
81  }
82 
83  namespace CompressData
84  {
85 
86  /**
87  * Return a string describing this compression and endianness
88  */
89  std::string GetCompressString(void)
90  {
91  return "B64Z-"+ EndianTypeMap[Endianness()];
92  }
93 
94  std::string GetBitSizeStr(void)
95  {
96  return boost::lexical_cast<std::string>(sizeof(void*)*8);
97  }
98 
99  /**
100  * Convert a binary string to Base 64 string
101  */
102  void BinaryStrToBase64Str(std::string &compressedDataString,
103  std::string &base64string)
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  }
125 
126  /**
127  * Convert a Base 64 string into a binary string
128  */
129  void Base64StrToBinaryStr(std::string &base64string,
130  std::string &compressedDataString)
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  }
139  }
140 }
141 }
void BinaryStrToBase64Str(std::string &compressedDataString, std::string &base64string)
EndianType Endianness(void)
const std::string EndianTypeMap[]
Definition: CompressData.h:69
void Base64StrToBinaryStr(std::string &base64string, std::string &compressedDataString)