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