11 #ifndef OPENMS_IS_BIG_ENDIAN
12 #if defined OPENMS_BIG_ENDIAN
13 #define OPENMS_IS_BIG_ENDIAN true
15 #define OPENMS_IS_BIG_ENDIAN false
36 #ifdef OPENMS_COMPILER_MSVC
37 #pragma comment(linker, "/export:compress")
59 BYTEORDER_LITTLEENDIAN
69 template <
typename FromType>
70 static void encode(std::vector<FromType> & in, ByteOrder to_byte_order,
String & out,
bool zlib_compression =
false);
77 template <
typename ToType>
78 static void decode(
const String & in, ByteOrder from_byte_order, std::vector<ToType> & out,
bool zlib_compression =
false);
87 template <
typename FromType>
88 static void encodeIntegers(std::vector<FromType> & in, ByteOrder to_byte_order,
String & out,
bool zlib_compression =
false);
95 template <
typename ToType>
96 static void decodeIntegers(
const String & in, ByteOrder from_byte_order, std::vector<ToType> & out,
bool zlib_compression =
false);
110 static void encodeStrings(
const std::vector<String> & in,
String & out,
bool zlib_compression =
false,
bool append_null_byte =
true);
148 static const char encoder_[];
149 static const char decoder_[];
151 template <
typename ToType>
152 static void decodeUncompressed_(
const String & in,
ByteOrder from_byte_order, std::vector<ToType> & out);
155 template <
typename ToType>
156 static void decodeCompressed_(
const String & in,
ByteOrder from_byte_order, std::vector<ToType> & out);
159 template <
typename ToType>
160 static void decodeIntegersUncompressed_(
const String & in,
ByteOrder from_byte_order, std::vector<ToType> & out);
163 template <
typename ToType>
164 static void decodeIntegersCompressed_(
const String & in,
ByteOrder from_byte_order, std::vector<ToType> & out);
178 return ((n & 0x000000ff) << 24) |
179 ((n & 0x0000ff00) << 8) |
180 ((n & 0x00ff0000) >> 8) |
181 ((n & 0xff000000) >> 24);
187 return ((n >> 56) & 0x00000000000000FF) |
188 ((n >> 40) & 0x000000000000FF00) |
189 ((n >> 24) & 0x0000000000FF0000) |
190 ((n >> 8) & 0x00000000FF000000) |
191 ((n << 8) & 0x000000FF00000000) |
192 ((n << 24) & 0x0000FF0000000000) |
193 ((n << 40) & 0x00FF000000000000) |
194 ((n << 56) & 0xFF00000000000000);
197 template <
typename FromType>
207 const Size element_size =
sizeof(FromType);
208 const Size input_bytes = element_size * in.size();
212 if (element_size == 4)
214 for (
Size i = 0; i < in.size(); ++i)
224 for (
Size i = 0; i < in.size(); ++i)
227 tmp.
f =
static_cast<double>(in[i]);
236 if (zlib_compression)
244 String str((
char*)in.data(), input_bytes);
250 template <
typename ToType>
253 if (zlib_compression)
263 template <
int type_size>
269 std::transform(p, p + element_count, p,
endianize32);
275 std::transform(p, p + element_count, p,
endianize64);
279 template <
typename ToType>
283 if (in.empty())
return;
285 constexpr
Size element_size =
sizeof(ToType);
291 QByteArray bazip = QByteArray::fromRawData(s.c_str(), (
int) s.size());
298 czip[0] = (bazip.size() & 0xff000000) >> 24;
299 czip[1] = (bazip.size() & 0x00ff0000) >> 16;
300 czip[2] = (bazip.size() & 0x0000ff00) >> 8;
301 czip[3] = (bazip.size() & 0x000000ff);
303 QByteArray base64_uncompressed = qUncompress(czip);
305 if (base64_uncompressed.isEmpty())
309 decompressed.resize(base64_uncompressed.size());
311 std::copy(base64_uncompressed.begin(), base64_uncompressed.end(), decompressed.begin());
313 void* byte_buffer =
reinterpret_cast<void *
>(&decompressed[0]);
314 Size buffer_size = decompressed.size();
316 const ToType * float_buffer =
reinterpret_cast<const ToType *
>(byte_buffer);
317 if (buffer_size % element_size != 0)
322 Size float_count = buffer_size / element_size;
327 invertEndianess<element_size>(byte_buffer, float_count);
331 out.assign(float_buffer, float_buffer + float_count);
334 template <
typename ToType>
345 if (in.size() % 4 != 0)
347 throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
"Malformed base64 input, length is not a multiple of 4.");
350 Size src_size = in.size();
353 if (in[src_size - 1] ==
'=') padding++;
354 if (in[src_size - 2] ==
'=') padding++;
358 constexpr
Size element_size =
sizeof(ToType);
365 invertEndianess<element_size>((
void*)s.data(), s.size() / element_size);
368 const char* cptr = s.data();
369 const ToType * fptr =
reinterpret_cast<const ToType*
>(cptr);
370 out.assign(fptr,fptr + s.size()/element_size);
373 template <
typename FromType>
381 const Size element_size =
sizeof(FromType);
382 const Size input_bytes = element_size * in.size();
387 if (element_size == 4)
389 for (
Size i = 0; i < in.size(); ++i)
398 for (
Size i = 0; i < in.size(); ++i)
408 if (zlib_compression)
416 String str((
char*)in.data(), input_bytes);
421 template <
typename ToType>
424 if (zlib_compression)
434 template <
typename ToType>
443 constexpr
Size element_size =
sizeof(ToType);
447 QByteArray qt_byte_array = QByteArray::fromRawData(in.c_str(), (
int) in.size());
448 QByteArray bazip = QByteArray::fromBase64(qt_byte_array);
451 czip[0] = (bazip.size() & 0xff000000) >> 24;
452 czip[1] = (bazip.size() & 0x00ff0000) >> 16;
453 czip[2] = (bazip.size() & 0x0000ff00) >> 8;
454 czip[3] = (bazip.size() & 0x000000ff);
456 QByteArray base64_uncompressed = qUncompress(czip);
457 if (base64_uncompressed.isEmpty())
461 decompressed.resize(base64_uncompressed.size());
463 std::copy(base64_uncompressed.begin(), base64_uncompressed.end(), decompressed.begin());
465 byte_buffer =
reinterpret_cast<void *
>(&decompressed[0]);
466 buffer_size = decompressed.size();
471 if constexpr(element_size == 4)
473 const Int32 * float_buffer =
reinterpret_cast<const Int32 *
>(byte_buffer);
474 if (buffer_size % element_size != 0)
476 Size float_count = buffer_size / element_size;
478 std::transform(p, p + float_count, p,
endianize32);
480 out.resize(float_count);
482 for (
Size i = 0; i < float_count; ++i)
484 out[i] = (ToType) * float_buffer;
490 const Int64 * float_buffer =
reinterpret_cast<const Int64 *
>(byte_buffer);
492 if (buffer_size % element_size != 0)
495 Size float_count = buffer_size / element_size;
498 std::transform(p, p + float_count, p,
endianize64);
500 out.resize(float_count);
502 for (
Size i = 0; i < float_count; ++i)
504 out[i] = (ToType) * float_buffer;
511 if constexpr(element_size == 4)
513 const Int * float_buffer =
reinterpret_cast<const Int *
>(byte_buffer);
514 if (buffer_size % element_size != 0)
517 Size float_count = buffer_size / element_size;
518 out.resize(float_count);
520 for (
Size i = 0; i < float_count; ++i)
522 out[i] = (ToType) * float_buffer;
528 const Int64 * float_buffer =
reinterpret_cast<const Int64 *
>(byte_buffer);
530 if (buffer_size % element_size != 0)
533 Size float_count = buffer_size / element_size;
534 out.resize(float_count);
536 for (
Size i = 0; i < float_count; ++i)
538 out[i] = (ToType) * float_buffer;
546 template <
typename ToType>
558 Size src_size = in.size();
561 if (in[src_size - 1] ==
'=') padding++;
562 if (in[src_size - 2] ==
'=') padding++;
573 const Size element_size =
sizeof(ToType);
576 char element[8] =
"\x00\x00\x00\x00\x00\x00\x00";
580 offset = (element_size - 1);
590 out.reserve((
UInt)(std::ceil((4.0 * src_size) / 3.0) + 6.0));
594 for (
Size i = 0; i < src_size; i += 4)
602 b =
decoder_[(int)in[i + 1] - 43] - 62;
603 if (i + 1 >= src_size)
608 element[offset] = (
unsigned char) ((a << 2) | (b >> 4));
610 offset = (offset + inc) % element_size;
612 if (written % element_size == 0)
615 if (element_size == 4)
617 Int32 * value =
reinterpret_cast<Int32 *
>(&element[0]);
618 float_value = (ToType) * value;
622 Int64 * value =
reinterpret_cast<Int64 *
>(&element[0]);
623 float_value = (ToType) * value;
625 out.push_back(float_value);
630 a =
decoder_[(int)in[i + 2] - 43] - 62;
631 if (i + 2 >= src_size)
636 element[offset] = (
unsigned char) (((b & 15) << 4) | (a >> 2));
638 offset = (offset + inc) % element_size;
640 if (written % element_size == 0)
643 if (element_size == 4)
645 Int32 * value =
reinterpret_cast<Int32 *
>(&element[0]);
646 float_value = (ToType) * value;
650 Int64 * value =
reinterpret_cast<Int64 *
>(&element[0]);
651 float_value = (ToType) * value;
653 out.push_back(float_value);
658 b =
decoder_[(int)in[i + 3] - 43] - 62;
659 if (i + 3 >= src_size)
664 element[offset] = (
unsigned char) (((a & 3) << 6) | b);
666 offset = (offset + inc) % element_size;
668 if (written % element_size == 0)
671 if (element_size == 4)
673 Int32 * value =
reinterpret_cast<Int32 *
>(&element[0]);
674 float_value = (ToType) * value;
678 Int64 * value =
reinterpret_cast<Int64 *
>(&element[0]);
679 float_value = (ToType) * value;
681 out.push_back(float_value);
#define OPENMS_IS_BIG_ENDIAN
Definition: Base64.h:15
Class to encode and decode Base64.
Definition: Base64.h:48
static void stringSimdDecoder_(const std::string &in, std::string &out)
static void decodeSingleString(const String &in, QByteArray &base64_uncompressed, bool zlib_compression)
Decodes a Base64 string to a QByteArray.
static void decode(const String &in, ByteOrder from_byte_order, std::vector< ToType > &out, bool zlib_compression=false)
Decodes a Base64 string to a vector of floating point numbers.
Definition: Base64.h:251
static void decodeIntegersCompressed_(const String &in, ByteOrder from_byte_order, std::vector< ToType > &out)
Decodes a compressed Base64 string to a vector of integer numbers.
Definition: Base64.h:435
double f
Definition: Base64.h:137
static const char decoder_[]
Definition: Base64.h:149
static void decodeCompressed_(const String &in, ByteOrder from_byte_order, std::vector< ToType > &out)
Decodes a compressed Base64 string to a vector of floating point numbers.
Definition: Base64.h:280
static void decodeIntegersUncompressed_(const String &in, ByteOrder from_byte_order, std::vector< ToType > &out)
Decodes a Base64 string to a vector of integer numbers.
Definition: Base64.h:547
static void decodeStrings(const String &in, std::vector< String > &out, bool zlib_compression=false)
Decodes a Base64 string to a vector of (null-terminated) strings.
static void stringSimdEncoder_(std::string &in, std::string &out)
static void decodeUncompressed_(const String &in, ByteOrder from_byte_order, std::vector< ToType > &out)
Decodes a Base64 string to a vector of floating point numbers.
Definition: Base64.h:335
Base64()=default
default constructor
static void encodeStrings(const std::vector< String > &in, String &out, bool zlib_compression=false, bool append_null_byte=true)
Encodes a vector of strings to a Base64 string.
UInt64 i
Definition: Base64.h:138
ByteOrder
Byte order type.
Definition: Base64.h:57
@ BYTEORDER_BIGENDIAN
Big endian type.
Definition: Base64.h:58
@ BYTEORDER_LITTLEENDIAN
Little endian type.
Definition: Base64.h:59
static void encode(std::vector< FromType > &in, ByteOrder to_byte_order, String &out, bool zlib_compression=false)
Encodes a vector of floating point numbers to a Base64 string.
Definition: Base64.h:198
static void decodeIntegers(const String &in, ByteOrder from_byte_order, std::vector< ToType > &out, bool zlib_compression=false)
Decodes a Base64 string to a vector of integer numbers.
Definition: Base64.h:422
UInt32 i
Definition: Base64.h:145
static void encodeIntegers(std::vector< FromType > &in, ByteOrder to_byte_order, String &out, bool zlib_compression=false)
Encodes a vector of integer point numbers to a Base64 string.
Definition: Base64.h:374
float f
Definition: Base64.h:144
Internal class needed for type-punning.
Definition: Base64.h:143
Internal class needed for type-punning.
Definition: Base64.h:136
Invalid conversion exception.
Definition: Exception.h:330
A more convenient string class.
Definition: String.h:34
static void compressData(const void *raw_data, const size_t in_length, std::string &compressed_data)
Compresses data using zlib directly.
OPENMS_UINT32_TYPE UInt32
Unsigned integer type (32bit)
Definition: Types.h:37
OPENMS_UINT64_TYPE UInt64
Unsigned integer type (64bit)
Definition: Types.h:51
int Int
Signed integer type.
Definition: Types.h:76
OPENMS_INT32_TYPE Int32
Signed integer type (32bit)
Definition: Types.h:30
OPENMS_INT64_TYPE Int64
Signed integer type (64bit)
Definition: Types.h:44
unsigned int UInt
Unsigned integer type.
Definition: Types.h:68
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:101
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22
UInt32 endianize32(const UInt32 &n)
Endianizes a 32 bit type from big endian to little endian and vice versa.
Definition: Base64.h:176
void invertEndianess< 4 >(void *byte_buffer, const size_t element_count)
Definition: Base64.h:266
void invertEndianess(void *byte_buffer, const size_t element_count)
void invertEndianess< 8 >(void *byte_buffer, const size_t element_count)
Definition: Base64.h:272
UInt64 endianize64(const UInt64 &n)
Endianizes a 64 bit type from big endian to little endian and vice versa.
Definition: Base64.h:185