35 #ifndef OPENMS_FORMAT_BASE64_H 36 #define OPENMS_FORMAT_BASE64_H 38 #ifndef OPENMS_IS_BIG_ENDIAN 39 #if defined OPENMS_BIG_ENDIAN 40 #define OPENMS_IS_BIG_ENDIAN true 42 #define OPENMS_IS_BIG_ENDIAN false 79 BYTEORDER_LITTLEENDIAN
89 template <
typename FromType>
90 void encode(std::vector<FromType> & in,
ByteOrder to_byte_order,
String & out,
bool zlib_compression =
false);
97 template <
typename ToType>
98 void decode(
const String & in,
ByteOrder from_byte_order, std::vector<ToType> & out,
bool zlib_compression =
false);
107 template <
typename FromType>
108 void encodeIntegers(std::vector<FromType> & in,
ByteOrder to_byte_order,
String & out,
bool zlib_compression =
false);
115 template <
typename ToType>
116 void decodeIntegers(
const String & in,
ByteOrder from_byte_order, std::vector<ToType> & out,
bool zlib_compression =
false);
130 void encodeStrings(
const std::vector<String> & in,
String & out,
bool zlib_compression =
false,
bool append_null_byte =
true);
141 void decodeStrings(
const String & in, std::vector<String> & out,
bool zlib_compression =
false);
150 void decodeSingleString(
const String & in, QByteArray & base64_uncompressed,
bool zlib_compression);
168 static const char encoder_[];
169 static const char decoder_[];
171 template <
typename ToType>
172 void decodeUncompressed_(
const String & in,
ByteOrder from_byte_order, std::vector<ToType> & out);
175 template <
typename ToType>
176 void decodeCompressed_(
const String & in,
ByteOrder from_byte_order, std::vector<ToType> & out);
179 template <
typename ToType>
180 void decodeIntegersUncompressed_(
const String & in,
ByteOrder from_byte_order, std::vector<ToType> & out);
183 template <
typename ToType>
184 void decodeIntegersCompressed_(
const String & in,
ByteOrder from_byte_order, std::vector<ToType> & out);
190 return ((n & 0xff) << 24) | ((n & 0xff00) << 8) | ((n & 0xff0000) >> 8) | ((n & 0xff000000) >> 24);
196 return ((n & 0x00000000000000ffll) << 56) |
197 ((n & 0x000000000000ff00ll) << 40) |
198 ((n & 0x0000000000ff0000ll) << 24) |
199 ((n & 0x00000000ff000000ll) << 8) |
200 ((n & 0x000000ff00000000ll) >> 8) |
201 ((n & 0x0000ff0000000000ll) >> 24) |
202 ((n & 0x00ff000000000000ll) >> 40) |
203 ((n & 0xff00000000000000ll) >> 56);
206 template <
typename FromType>
214 const Size element_size =
sizeof(FromType);
215 const Size input_bytes = element_size * in.size();
222 if (element_size == 4)
224 for (
Size i = 0; i < in.size(); ++i)
234 for (
Size i = 0; i < in.size(); ++i)
245 if (zlib_compression)
247 unsigned long sourceLen = (
unsigned long)in.size();
248 unsigned long compressed_length =
249 sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
257 compressed.resize(compressed_length);
258 zlib_error = compress(reinterpret_cast<Bytef *>(&compressed[0]), &compressed_length, reinterpret_cast<Bytef *>(&in[0]), (
unsigned long)input_bytes);
267 compressed_length *= 2;
270 while (zlib_error == Z_BUF_ERROR);
272 if (zlib_error != Z_OK)
277 String(compressed).swap(compressed);
278 it =
reinterpret_cast<Byte *
>(&compressed[0]);
279 end = it + compressed_length;
280 out.resize((
Size)ceil(compressed_length / 3.) * 4);
285 out.resize((
Size)ceil(input_bytes / 3.) * 4);
286 it =
reinterpret_cast<Byte *
>(&in[0]);
287 end = it + input_bytes;
290 Byte * to =
reinterpret_cast<Byte *
>(&out[0]);
298 Int padding_count = 0;
301 for (
Size i = 0; i < 3; i++)
305 int_24bit |= *it++ << ((2 - i) * 8);
314 for (
Int i = 3; i >= 0; i--)
316 to[i] = encoder_[int_24bit & 0x3F];
321 if (padding_count > 0)
323 if (padding_count > 1)
333 template <
typename ToType>
336 if (zlib_compression)
338 decodeCompressed_(in, from_byte_order, out);
342 decodeUncompressed_(in, from_byte_order, out);
346 template <
typename ToType>
355 std::vector<unsigned char> binary;
356 const Size element_size =
sizeof(ToType);
360 QByteArray qt_byte_array = QByteArray::fromRawData(in.c_str(), (int) in.size());
361 QByteArray bazip = QByteArray::fromBase64(qt_byte_array);
364 czip[0] = (bazip.size() & 0xff000000) >> 24;
365 czip[1] = (bazip.size() & 0x00ff0000) >> 16;
366 czip[2] = (bazip.size() & 0x0000ff00) >> 8;
367 czip[3] = (bazip.size() & 0x000000ff);
369 QByteArray base64_uncompressed = qUncompress(czip);
371 if (base64_uncompressed.isEmpty())
375 decompressed.resize(base64_uncompressed.size());
377 std::copy(base64_uncompressed.begin(), base64_uncompressed.end(), decompressed.begin());
379 byte_buffer =
reinterpret_cast<void *
>(&decompressed[0]);
380 buffer_size = decompressed.size();
385 if (element_size == 4)
387 const float * float_buffer =
reinterpret_cast<const float *
>(byte_buffer);
388 if (buffer_size % element_size != 0)
390 Size float_count = buffer_size / element_size;
391 Int32 * p =
reinterpret_cast<Int32 *
>(byte_buffer);
392 std::transform(p, p + float_count, p,
endianize32);
393 out.assign(float_buffer, float_buffer + float_count);
397 const double * float_buffer =
reinterpret_cast<const double *
>(byte_buffer);
399 if (buffer_size % element_size != 0)
402 Size float_count = buffer_size / element_size;
404 Int64 * p =
reinterpret_cast<Int64 *
>(byte_buffer);
405 std::transform(p, p + float_count, p,
endianize64);
407 out.resize(float_count);
409 for (
Size i = 0; i < float_count; ++i)
411 out[i] = (ToType) * float_buffer;
418 if (element_size == 4)
420 const float * float_buffer =
reinterpret_cast<const float *
>(byte_buffer);
421 if (buffer_size % element_size != 0)
424 Size float_count = buffer_size / element_size;
425 out.assign(float_buffer, float_buffer + float_count);
429 const double * float_buffer =
reinterpret_cast<const double *
>(byte_buffer);
431 if (buffer_size % element_size != 0)
434 Size float_count = buffer_size / element_size;
435 out.resize(float_count);
437 for (
Size i = 0; i < float_count; ++i)
439 out[i] = (ToType) * float_buffer;
447 template <
typename ToType>
459 Size src_size = in.size();
462 if (in[src_size - 1] ==
'=') padding++;
463 if (in[src_size - 2] ==
'=') padding++;
474 const Size element_size =
sizeof(ToType);
477 char element[8] =
"\x00\x00\x00\x00\x00\x00\x00";
481 offset = (element_size - 1);
491 out.reserve((
UInt)(std::ceil((4.0 * src_size) / 3.0) + 6.0));
495 for (
Size i = 0; i < src_size; i += 4)
501 a = decoder_[(int)in[i] - 43] - 62;
502 b = decoder_[(int)in[i + 1] - 43] - 62;
503 if (i + 1 >= src_size)
508 element[offset] = (
unsigned char) ((a << 2) | (b >> 4));
510 offset = (offset + inc) % element_size;
512 if (written % element_size == 0)
514 ToType * to_type =
reinterpret_cast<ToType *
>(&element[0]);
515 out.push_back((*to_type));
520 a = decoder_[(int)in[i + 2] - 43] - 62;
521 if (i + 2 >= src_size)
526 element[offset] = (
unsigned char) (((b & 15) << 4) | (a >> 2));
528 offset = (offset + inc) % element_size;
530 if (written % element_size == 0)
532 ToType * to_type =
reinterpret_cast<ToType *
>(&element[0]);
533 out.push_back((*to_type));
538 b = decoder_[(int)in[i + 3] - 43] - 62;
539 if (i + 3 >= src_size)
544 element[offset] = (
unsigned char) (((a & 3) << 6) | b);
546 offset = (offset + inc) % element_size;
548 if (written % element_size == 0)
550 ToType * to_type =
reinterpret_cast<ToType *
>(&element[0]);
551 out.push_back((*to_type));
557 template <
typename FromType>
565 const Size element_size =
sizeof(FromType);
566 const Size input_bytes = element_size * in.size();
573 if (element_size == 4)
575 for (
Size i = 0; i < in.size(); ++i)
584 for (
Size i = 0; i < in.size(); ++i)
594 if (zlib_compression)
596 unsigned long sourceLen = (
unsigned long)input_bytes;
597 unsigned long compressed_length =
598 sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
600 compressed.resize(compressed_length);
601 while (compress(reinterpret_cast<Bytef *>(&compressed[0]), &compressed_length, reinterpret_cast<Bytef *>(&in[0]), (
unsigned long)input_bytes) != Z_OK)
603 compressed_length *= 2;
604 compressed.reserve(compressed_length);
608 String(compressed).swap(compressed);
609 it =
reinterpret_cast<Byte *
>(&compressed[0]);
610 end = it + compressed_length;
611 out.resize((
Size)ceil(compressed_length / 3.) * 4);
616 out.resize((
Size)ceil(input_bytes / 3.) * 4);
617 it =
reinterpret_cast<Byte *
>(&in[0]);
618 end = it + input_bytes;
621 Byte * to =
reinterpret_cast<Byte *
>(&out[0]);
629 Int padding_count = 0;
632 for (
Size i = 0; i < 3; i++)
636 int_24bit |= *it++ << ((2 - i) * 8);
645 for (
Int i = 3; i >= 0; i--)
647 to[i] = encoder_[int_24bit & 0x3F];
652 if (padding_count > 0)
654 if (padding_count > 1)
664 template <
typename ToType>
667 if (zlib_compression)
669 decodeIntegersCompressed_(in, from_byte_order, out);
673 decodeIntegersUncompressed_(in, from_byte_order, out);
677 template <
typename ToType>
686 std::vector<unsigned char> binary;
687 const Size element_size =
sizeof(ToType);
691 QByteArray qt_byte_array = QByteArray::fromRawData(in.c_str(), (int) in.size());
692 QByteArray bazip = QByteArray::fromBase64(qt_byte_array);
695 czip[0] = (bazip.size() & 0xff000000) >> 24;
696 czip[1] = (bazip.size() & 0x00ff0000) >> 16;
697 czip[2] = (bazip.size() & 0x0000ff00) >> 8;
698 czip[3] = (bazip.size() & 0x000000ff);
700 QByteArray base64_uncompressed = qUncompress(czip);
701 if (base64_uncompressed.isEmpty())
705 decompressed.resize(base64_uncompressed.size());
707 std::copy(base64_uncompressed.begin(), base64_uncompressed.end(), decompressed.begin());
709 byte_buffer =
reinterpret_cast<void *
>(&decompressed[0]);
710 buffer_size = decompressed.size();
715 if (element_size == 4)
717 const Int32 * float_buffer =
reinterpret_cast<const Int32 *
>(byte_buffer);
718 if (buffer_size % element_size != 0)
720 Size float_count = buffer_size / element_size;
721 Int32 * p =
reinterpret_cast<Int32 *
>(byte_buffer);
722 std::transform(p, p + float_count, p,
endianize32);
724 out.resize(float_count);
726 for (
Size i = 0; i < float_count; ++i)
728 out[i] = (ToType) * float_buffer;
734 const Int64 * float_buffer =
reinterpret_cast<const Int64 *
>(byte_buffer);
736 if (buffer_size % element_size != 0)
739 Size float_count = buffer_size / element_size;
741 Int64 * p =
reinterpret_cast<Int64 *
>(byte_buffer);
742 std::transform(p, p + float_count, p,
endianize64);
744 out.resize(float_count);
746 for (
Size i = 0; i < float_count; ++i)
748 out[i] = (ToType) * float_buffer;
755 if (element_size == 4)
757 const Int * float_buffer =
reinterpret_cast<const Int *
>(byte_buffer);
758 if (buffer_size % element_size != 0)
761 Size float_count = buffer_size / element_size;
762 out.resize(float_count);
764 for (
Size i = 0; i < float_count; ++i)
766 out[i] = (ToType) * float_buffer;
772 const Int64 * float_buffer =
reinterpret_cast<const Int64 *
>(byte_buffer);
774 if (buffer_size % element_size != 0)
777 Size float_count = buffer_size / element_size;
778 out.resize(float_count);
780 for (
Size i = 0; i < float_count; ++i)
782 out[i] = (ToType) * float_buffer;
790 template <
typename ToType>
802 Size src_size = in.size();
805 if (in[src_size - 1] ==
'=') padding++;
806 if (in[src_size - 2] ==
'=') padding++;
817 const Size element_size =
sizeof(ToType);
820 char element[8] =
"\x00\x00\x00\x00\x00\x00\x00";
824 offset = (element_size - 1);
834 out.reserve((
UInt)(std::ceil((4.0 * src_size) / 3.0) + 6.0));
838 for (
Size i = 0; i < src_size; i += 4)
845 a = decoder_[(int)in[i] - 43] - 62;
846 b = decoder_[(int)in[i + 1] - 43] - 62;
847 if (i + 1 >= src_size)
852 element[offset] = (
unsigned char) ((a << 2) | (b >> 4));
854 offset = (offset + inc) % element_size;
856 if (written % element_size == 0)
859 if (element_size == 4)
861 Int32 * value =
reinterpret_cast<Int32 *
>(&element[0]);
862 float_value = (ToType) * value;
866 Int64 * value =
reinterpret_cast<Int64 *
>(&element[0]);
867 float_value = (ToType) * value;
869 out.push_back(float_value);
874 a = decoder_[(int)in[i + 2] - 43] - 62;
875 if (i + 2 >= src_size)
880 element[offset] = (
unsigned char) (((b & 15) << 4) | (a >> 2));
882 offset = (offset + inc) % element_size;
884 if (written % element_size == 0)
887 if (element_size == 4)
889 Int32 * value =
reinterpret_cast<Int32 *
>(&element[0]);
890 float_value = (ToType) * value;
894 Int64 * value =
reinterpret_cast<Int64 *
>(&element[0]);
895 float_value = (ToType) * value;
897 out.push_back(float_value);
902 b = decoder_[(int)in[i + 3] - 43] - 62;
903 if (i + 3 >= src_size)
908 element[offset] = (
unsigned char) (((a & 3) << 6) | b);
910 offset = (offset + inc) % element_size;
912 if (written % element_size == 0)
915 if (element_size == 4)
917 Int32 * value =
reinterpret_cast<Int32 *
>(&element[0]);
918 float_value = (ToType) * value;
922 Int64 * value =
reinterpret_cast<Int64 *
>(&element[0]);
923 float_value = (ToType) * value;
925 out.push_back(float_value);
Big endian type.
Definition: Base64.h:78
A more convenient string class.
Definition: String.h:57
Class to encode and decode Base64.
Definition: Base64.h:64
Little endian type.
Definition: Base64.h:79
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
float f
Definition: Base64.h:164
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:791
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:558
ByteOrder
Byte order type.
Definition: Base64.h:76
Internal class needed for type-punning.
Definition: Base64.h:162
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:665
OPENMS_INT32_TYPE Int32
Signed integer type (32bit)
Definition: Types.h:57
double f
Definition: Base64.h:157
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:678
Int64 i
Definition: Base64.h:158
Invalid conversion exception.
Definition: Exception.h:363
Int32 i
Definition: Base64.h:165
Out of memory exception.
Definition: Exception.h:472
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:448
#define OPENMS_IS_BIG_ENDIAN
Definition: Base64.h:42
OPENMS_INT64_TYPE Int64
Signed integer type (64bit)
Definition: Types.h:64
OPENMS_BYTE_TYPE Byte
Byte type.
Definition: Types.h:105
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:207
Int64 endianize64(Int64 &n)
Endianizes a 64 bit type from big endian to little endian and vice versa.
Definition: Base64.h:194
Internal class needed for type-punning.
Definition: Base64.h:155
Int32 endianize32(Int32 &n)
Endianizes a 32 bit type from big endian to little endian and vice versa.
Definition: Base64.h:188
int Int
Signed integer type.
Definition: Types.h:96
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:334
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:347