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
24 #include <QtCore/QByteArray>
34 #ifdef OPENMS_COMPILER_MSVC
35 #pragma comment(linker, "/export:compress")
57 BYTEORDER_LITTLEENDIAN
67 template <
typename FromType>
68 static void encode(std::vector<FromType> & in, ByteOrder to_byte_order,
String & out,
bool zlib_compression =
false);
75 template <
typename ToType>
76 static void decode(
const String & in, ByteOrder from_byte_order, std::vector<ToType> & out,
bool zlib_compression =
false);
85 template <
typename FromType>
86 static void encodeIntegers(std::vector<FromType> & in, ByteOrder to_byte_order,
String & out,
bool zlib_compression =
false);
93 template <
typename ToType>
94 static void decodeIntegers(
const String & in, ByteOrder from_byte_order, std::vector<ToType> & out,
bool zlib_compression =
false);
108 static void encodeStrings(
const std::vector<String> & in,
String & out,
bool zlib_compression =
false,
bool append_null_byte =
true);
146 static const char encoder_[];
147 static const char decoder_[];
149 template <
typename ToType>
150 static void decodeUncompressed_(
const String & in,
ByteOrder from_byte_order, std::vector<ToType> & out);
153 template <
typename ToType>
154 static void decodeCompressed_(
const String & in,
ByteOrder from_byte_order, std::vector<ToType> & out);
157 template <
typename ToType>
158 static void decodeIntegersUncompressed_(
const String & in,
ByteOrder from_byte_order, std::vector<ToType> & out);
161 template <
typename ToType>
162 static void decodeIntegersCompressed_(
const String & in,
ByteOrder from_byte_order, std::vector<ToType> & out);
176 return ((n & 0x000000ff) << 24) |
177 ((n & 0x0000ff00) << 8) |
178 ((n & 0x00ff0000) >> 8) |
179 ((n & 0xff000000) >> 24);
185 return ((n >> 56) & 0x00000000000000FF) |
186 ((n >> 40) & 0x000000000000FF00) |
187 ((n >> 24) & 0x0000000000FF0000) |
188 ((n >> 8) & 0x00000000FF000000) |
189 ((n << 8) & 0x000000FF00000000) |
190 ((n << 24) & 0x0000FF0000000000) |
191 ((n << 40) & 0x00FF000000000000) |
192 ((n << 56) & 0xFF00000000000000);
195 template <
typename FromType>
205 const Size element_size =
sizeof(FromType);
206 const Size input_bytes = element_size * in.size();
210 if (element_size == 4)
212 for (
Size i = 0; i < in.size(); ++i)
222 for (
Size i = 0; i < in.size(); ++i)
225 tmp.
f =
static_cast<double>(in[i]);
234 if (zlib_compression)
242 String str((
char*)in.data(), input_bytes);
248 template <
typename ToType>
251 if (zlib_compression)
261 template <
int type_size>
267 std::transform(p, p + element_count, p,
endianize32);
273 std::transform(p, p + element_count, p,
endianize64);
277 template <
typename ToType>
281 if (in.empty())
return;
283 constexpr
Size element_size =
sizeof(ToType);
289 QByteArray bazip = QByteArray::fromRawData(s.c_str(), (
int) s.size());
296 czip[0] = (bazip.size() & 0xff000000) >> 24;
297 czip[1] = (bazip.size() & 0x00ff0000) >> 16;
298 czip[2] = (bazip.size() & 0x0000ff00) >> 8;
299 czip[3] = (bazip.size() & 0x000000ff);
301 QByteArray base64_uncompressed = qUncompress(czip);
303 if (base64_uncompressed.isEmpty())
307 decompressed.resize(base64_uncompressed.size());
309 std::copy(base64_uncompressed.begin(), base64_uncompressed.end(), decompressed.begin());
311 void* byte_buffer =
reinterpret_cast<void *
>(&decompressed[0]);
312 Size buffer_size = decompressed.size();
314 const ToType * float_buffer =
reinterpret_cast<const ToType *
>(byte_buffer);
315 if (buffer_size % element_size != 0)
320 Size float_count = buffer_size / element_size;
325 invertEndianess<element_size>(byte_buffer, float_count);
329 out.assign(float_buffer, float_buffer + float_count);
332 template <
typename ToType>
343 if (in.size() % 4 != 0)
345 throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
"Malformed base64 input, length is not a multiple of 4.");
348 Size src_size = in.size();
351 if (in[src_size - 1] ==
'=') padding++;
352 if (in[src_size - 2] ==
'=') padding++;
356 constexpr
Size element_size =
sizeof(ToType);
363 invertEndianess<element_size>((
void*)s.data(), s.size() / element_size);
366 const char* cptr = s.data();
367 const ToType * fptr =
reinterpret_cast<const ToType*
>(cptr);
368 out.assign(fptr,fptr + s.size()/element_size);
371 template <
typename FromType>
379 const Size element_size =
sizeof(FromType);
380 const Size input_bytes = element_size * in.size();
385 if (element_size == 4)
387 for (
Size i = 0; i < in.size(); ++i)
396 for (
Size i = 0; i < in.size(); ++i)
406 if (zlib_compression)
414 String str((
char*)in.data(), input_bytes);
419 template <
typename ToType>
422 if (zlib_compression)
432 template <
typename ToType>
441 constexpr
Size element_size =
sizeof(ToType);
445 QByteArray qt_byte_array = QByteArray::fromRawData(in.c_str(), (
int) in.size());
446 QByteArray bazip = QByteArray::fromBase64(qt_byte_array);
449 czip[0] = (bazip.size() & 0xff000000) >> 24;
450 czip[1] = (bazip.size() & 0x00ff0000) >> 16;
451 czip[2] = (bazip.size() & 0x0000ff00) >> 8;
452 czip[3] = (bazip.size() & 0x000000ff);
454 QByteArray base64_uncompressed = qUncompress(czip);
455 if (base64_uncompressed.isEmpty())
459 decompressed.resize(base64_uncompressed.size());
461 std::copy(base64_uncompressed.begin(), base64_uncompressed.end(), decompressed.begin());
463 byte_buffer =
reinterpret_cast<void *
>(&decompressed[0]);
464 buffer_size = decompressed.size();
469 if constexpr(element_size == 4)
471 const Int32 * float_buffer =
reinterpret_cast<const Int32 *
>(byte_buffer);
472 if (buffer_size % element_size != 0)
474 Size float_count = buffer_size / element_size;
476 std::transform(p, p + float_count, p,
endianize32);
478 out.resize(float_count);
480 for (
Size i = 0; i < float_count; ++i)
482 out[i] = (ToType) * float_buffer;
488 const Int64 * float_buffer =
reinterpret_cast<const Int64 *
>(byte_buffer);
490 if (buffer_size % element_size != 0)
493 Size float_count = buffer_size / element_size;
496 std::transform(p, p + float_count, p,
endianize64);
498 out.resize(float_count);
500 for (
Size i = 0; i < float_count; ++i)
502 out[i] = (ToType) * float_buffer;
509 if constexpr(element_size == 4)
511 const Int * float_buffer =
reinterpret_cast<const Int *
>(byte_buffer);
512 if (buffer_size % element_size != 0)
515 Size float_count = buffer_size / element_size;
516 out.resize(float_count);
518 for (
Size i = 0; i < float_count; ++i)
520 out[i] = (ToType) * float_buffer;
526 const Int64 * float_buffer =
reinterpret_cast<const Int64 *
>(byte_buffer);
528 if (buffer_size % element_size != 0)
531 Size float_count = buffer_size / element_size;
532 out.resize(float_count);
534 for (
Size i = 0; i < float_count; ++i)
536 out[i] = (ToType) * float_buffer;
544 template <
typename ToType>
556 Size src_size = in.size();
559 if (in[src_size - 1] ==
'=') padding++;
560 if (in[src_size - 2] ==
'=') padding++;
571 const Size element_size =
sizeof(ToType);
574 char element[8] =
"\x00\x00\x00\x00\x00\x00\x00";
578 offset = (element_size - 1);
588 out.reserve((
UInt)(std::ceil((4.0 * src_size) / 3.0) + 6.0));
592 for (
Size i = 0; i < src_size; i += 4)
600 b =
decoder_[(int)in[i + 1] - 43] - 62;
601 if (i + 1 >= src_size)
606 element[offset] = (
unsigned char) ((a << 2) | (b >> 4));
608 offset = (offset + inc) % element_size;
610 if (written % element_size == 0)
613 if (element_size == 4)
615 Int32 * value =
reinterpret_cast<Int32 *
>(&element[0]);
616 float_value = (ToType) * value;
620 Int64 * value =
reinterpret_cast<Int64 *
>(&element[0]);
621 float_value = (ToType) * value;
623 out.push_back(float_value);
628 a =
decoder_[(int)in[i + 2] - 43] - 62;
629 if (i + 2 >= src_size)
634 element[offset] = (
unsigned char) (((b & 15) << 4) | (a >> 2));
636 offset = (offset + inc) % element_size;
638 if (written % element_size == 0)
641 if (element_size == 4)
643 Int32 * value =
reinterpret_cast<Int32 *
>(&element[0]);
644 float_value = (ToType) * value;
648 Int64 * value =
reinterpret_cast<Int64 *
>(&element[0]);
649 float_value = (ToType) * value;
651 out.push_back(float_value);
656 b =
decoder_[(int)in[i + 3] - 43] - 62;
657 if (i + 3 >= src_size)
662 element[offset] = (
unsigned char) (((a & 3) << 6) | b);
664 offset = (offset + inc) % element_size;
666 if (written % element_size == 0)
669 if (element_size == 4)
671 Int32 * value =
reinterpret_cast<Int32 *
>(&element[0]);
672 float_value = (ToType) * value;
676 Int64 * value =
reinterpret_cast<Int64 *
>(&element[0]);
677 float_value = (ToType) * value;
679 out.push_back(float_value);
#define OPENMS_IS_BIG_ENDIAN
Definition: Base64.h:15
Class to encode and decode Base64.
Definition: Base64.h:46
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:249
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:433
double f
Definition: Base64.h:135
static const char decoder_[]
Definition: Base64.h:147
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:278
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:545
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:333
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:136
ByteOrder
Byte order type.
Definition: Base64.h:55
@ BYTEORDER_BIGENDIAN
Big endian type.
Definition: Base64.h:56
@ BYTEORDER_LITTLEENDIAN
Little endian type.
Definition: Base64.h:57
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:196
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:420
UInt32 i
Definition: Base64.h:143
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:372
float f
Definition: Base64.h:142
Internal class needed for type-punning.
Definition: Base64.h:141
Internal class needed for type-punning.
Definition: Base64.h:134
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.
int32_t Int32
Signed integer type (32bit)
Definition: Types.h:26
int64_t Int64
Signed integer type (64bit)
Definition: Types.h:40
int Int
Signed integer type.
Definition: Types.h:72
uint32_t UInt32
Unsigned integer type (32bit)
Definition: Types.h:33
uint64_t UInt64
Unsigned integer type (64bit)
Definition: Types.h:47
unsigned int UInt
Unsigned integer type.
Definition: Types.h:64
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:97
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
UInt32 endianize32(const UInt32 &n)
Endianizes a 32 bit type from big endian to little endian and vice versa.
Definition: Base64.h:174
void invertEndianess< 4 >(void *byte_buffer, const size_t element_count)
Definition: Base64.h:264
void invertEndianess(void *byte_buffer, const size_t element_count)
void invertEndianess< 8 >(void *byte_buffer, const size_t element_count)
Definition: Base64.h:270
UInt64 endianize64(const UInt64 &n)
Endianizes a 64 bit type from big endian to little endian and vice versa.
Definition: Base64.h:183