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
32 #ifdef OPENMS_COMPILER_MSVC
33 #pragma comment(linker, "/export:compress")
55 BYTEORDER_LITTLEENDIAN
65 template <
typename FromType>
66 static void encode(std::vector<FromType> & in, ByteOrder to_byte_order,
String & out,
bool zlib_compression =
false);
73 template <
typename ToType>
74 static void decode(
const String & in, ByteOrder from_byte_order, std::vector<ToType> & out,
bool zlib_compression =
false);
83 template <
typename FromType>
84 static void encodeIntegers(std::vector<FromType> & in, ByteOrder to_byte_order,
String & out,
bool zlib_compression =
false);
91 template <
typename ToType>
92 static void decodeIntegers(
const String & in, ByteOrder from_byte_order, std::vector<ToType> & out,
bool zlib_compression =
false);
106 static void encodeStrings(
const std::vector<String> & in,
String & out,
bool zlib_compression =
false,
bool append_null_byte =
true);
144 static const char encoder_[];
145 static const char decoder_[];
147 template <
typename ToType>
148 static void decodeUncompressed_(
const String & in,
ByteOrder from_byte_order, std::vector<ToType> & out);
151 template <
typename ToType>
152 static void decodeCompressed_(
const String & in,
ByteOrder from_byte_order, std::vector<ToType> & out);
155 template <
typename ToType>
156 static void decodeIntegersUncompressed_(
const String & in,
ByteOrder from_byte_order, std::vector<ToType> & out);
159 template <
typename ToType>
160 static void decodeIntegersCompressed_(
const String & in,
ByteOrder from_byte_order, std::vector<ToType> & out);
174 return ((n & 0x000000ff) << 24) |
175 ((n & 0x0000ff00) << 8) |
176 ((n & 0x00ff0000) >> 8) |
177 ((n & 0xff000000) >> 24);
183 return ((n >> 56) & 0x00000000000000FF) |
184 ((n >> 40) & 0x000000000000FF00) |
185 ((n >> 24) & 0x0000000000FF0000) |
186 ((n >> 8) & 0x00000000FF000000) |
187 ((n << 8) & 0x000000FF00000000) |
188 ((n << 24) & 0x0000FF0000000000) |
189 ((n << 40) & 0x00FF000000000000) |
190 ((n << 56) & 0xFF00000000000000);
193 template <
typename FromType>
203 const Size element_size =
sizeof(FromType);
204 const Size input_bytes = element_size * in.size();
208 if (element_size == 4)
210 for (
Size i = 0; i < in.size(); ++i)
220 for (
Size i = 0; i < in.size(); ++i)
223 tmp.
f =
static_cast<double>(in[i]);
232 if (zlib_compression)
240 String str((
char*)in.data(), input_bytes);
246 template <
typename ToType>
249 if (zlib_compression)
259 template <
int type_size>
265 std::transform(p, p + element_count, p,
endianize32);
271 std::transform(p, p + element_count, p,
endianize64);
275 template <
typename ToType>
287 void* byte_buffer =
reinterpret_cast<void*
>(&decompressed[0]);
288 Size buffer_size = decompressed.size();
290 const ToType * float_buffer =
reinterpret_cast<const ToType *
>(byte_buffer);
291 constexpr
Size element_size =
sizeof(ToType);
292 if (buffer_size % element_size != 0)
297 Size float_count = buffer_size / element_size;
302 invertEndianess<element_size>(byte_buffer, float_count);
306 out.assign(float_buffer, float_buffer + float_count);
309 template <
typename ToType>
320 if (in.size() % 4 != 0)
322 throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
"Malformed base64 input, length is not a multiple of 4.");
325 Size src_size = in.size();
328 if (in[src_size - 1] ==
'=') padding++;
329 if (in[src_size - 2] ==
'=') padding++;
333 constexpr
Size element_size =
sizeof(ToType);
340 invertEndianess<element_size>((
void*)s.data(), s.size() / element_size);
343 const char* cptr = s.data();
344 const ToType * fptr =
reinterpret_cast<const ToType*
>(cptr);
345 out.assign(fptr,fptr + s.size()/element_size);
348 template <
typename FromType>
356 const Size element_size =
sizeof(FromType);
357 const Size input_bytes = element_size * in.size();
362 if (element_size == 4)
364 for (
Size i = 0; i < in.size(); ++i)
373 for (
Size i = 0; i < in.size(); ++i)
383 if (zlib_compression)
391 String str((
char*)in.data(), input_bytes);
396 template <
typename ToType>
399 if (zlib_compression)
409 template <
typename ToType>
416 constexpr
Size element_size =
sizeof(ToType);
420 if (decompressed.empty())
425 void* byte_buffer =
reinterpret_cast<void*
>(&decompressed[0]);
426 Size buffer_size = decompressed.size();
431 if constexpr(element_size == 4)
433 const Int32 * float_buffer =
reinterpret_cast<const Int32 *
>(byte_buffer);
434 if (buffer_size % element_size != 0)
436 Size float_count = buffer_size / element_size;
438 std::transform(p, p + float_count, p,
endianize32);
440 out.resize(float_count);
442 for (
Size i = 0; i < float_count; ++i)
444 out[i] = (ToType) * float_buffer;
450 const Int64 * float_buffer =
reinterpret_cast<const Int64 *
>(byte_buffer);
452 if (buffer_size % element_size != 0)
455 Size float_count = buffer_size / element_size;
458 std::transform(p, p + float_count, p,
endianize64);
460 out.resize(float_count);
462 for (
Size i = 0; i < float_count; ++i)
464 out[i] = (ToType) * float_buffer;
471 if constexpr(element_size == 4)
473 const Int * float_buffer =
reinterpret_cast<const Int *
>(byte_buffer);
474 if (buffer_size % element_size != 0)
477 Size float_count = buffer_size / element_size;
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;
494 out.resize(float_count);
496 for (
Size i = 0; i < float_count; ++i)
498 out[i] = (ToType) * float_buffer;
506 template <
typename ToType>
518 Size src_size = in.size();
521 if (in[src_size - 1] ==
'=') padding++;
522 if (in[src_size - 2] ==
'=') padding++;
533 const Size element_size =
sizeof(ToType);
536 char element[8] =
"\x00\x00\x00\x00\x00\x00\x00";
540 offset = (element_size - 1);
550 out.reserve((
UInt)(std::ceil((4.0 * src_size) / 3.0) + 6.0));
554 for (
Size i = 0; i < src_size; i += 4)
562 b =
decoder_[(int)in[i + 1] - 43] - 62;
563 if (i + 1 >= src_size)
568 element[offset] = (
unsigned char) ((a << 2) | (b >> 4));
570 offset = (offset + inc) % element_size;
572 if (written % element_size == 0)
575 if (element_size == 4)
577 Int32 * value =
reinterpret_cast<Int32 *
>(&element[0]);
578 float_value = (ToType) * value;
582 Int64 * value =
reinterpret_cast<Int64 *
>(&element[0]);
583 float_value = (ToType) * value;
585 out.push_back(float_value);
590 a =
decoder_[(int)in[i + 2] - 43] - 62;
591 if (i + 2 >= src_size)
596 element[offset] = (
unsigned char) (((b & 15) << 4) | (a >> 2));
598 offset = (offset + inc) % element_size;
600 if (written % element_size == 0)
603 if (element_size == 4)
605 Int32 * value =
reinterpret_cast<Int32 *
>(&element[0]);
606 float_value = (ToType) * value;
610 Int64 * value =
reinterpret_cast<Int64 *
>(&element[0]);
611 float_value = (ToType) * value;
613 out.push_back(float_value);
618 b =
decoder_[(int)in[i + 3] - 43] - 62;
619 if (i + 3 >= src_size)
624 element[offset] = (
unsigned char) (((a & 3) << 6) | b);
626 offset = (offset + inc) % element_size;
628 if (written % element_size == 0)
631 if (element_size == 4)
633 Int32 * value =
reinterpret_cast<Int32 *
>(&element[0]);
634 float_value = (ToType) * value;
638 Int64 * value =
reinterpret_cast<Int64 *
>(&element[0]);
639 float_value = (ToType) * value;
641 out.push_back(float_value);
#define OPENMS_IS_BIG_ENDIAN
Definition: Base64.h:15
Class to encode and decode Base64.
Definition: Base64.h:44
static void stringSimdDecoder_(const std::string &in, std::string &out)
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:247
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:410
double f
Definition: Base64.h:133
static const char decoder_[]
Definition: Base64.h:145
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:276
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:507
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:310
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:134
ByteOrder
Byte order type.
Definition: Base64.h:53
@ BYTEORDER_BIGENDIAN
Big endian type.
Definition: Base64.h:54
@ BYTEORDER_LITTLEENDIAN
Little endian type.
Definition: Base64.h:55
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:194
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:397
UInt32 i
Definition: Base64.h:141
static void decodeSingleString(const String &in, String &out, bool zlib_compression)
Decodes a Base64 string.
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:349
float f
Definition: Base64.h:140
Internal class needed for type-punning.
Definition: Base64.h:139
Internal class needed for type-punning.
Definition: Base64.h:132
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:172
void invertEndianess< 4 >(void *byte_buffer, const size_t element_count)
Definition: Base64.h:262
void invertEndianess(void *byte_buffer, const size_t element_count)
void invertEndianess< 8 >(void *byte_buffer, const size_t element_count)
Definition: Base64.h:268
UInt64 endianize64(const UInt64 &n)
Endianizes a 64 bit type from big endian to little endian and vice versa.
Definition: Base64.h:181