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