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--)
321 if (padding_count > 0)
323 if (padding_count > 1)
333 template <
typename ToType>
336 if (zlib_compression)
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>
454 Size src_size = in.size();
457 if (in[src_size - 1] ==
'=')
459 if (in[src_size - 2] ==
'=')
471 const Size element_size =
sizeof(ToType);
474 char element[8] =
"\x00\x00\x00\x00\x00\x00\x00";
478 offset = (element_size - 1);
488 out.reserve((
UInt)(std::ceil((4.0 * src_size) / 3.0) + 6.0));
492 for (
Size i = 0; i < src_size; i += 4)
496 b =
decoder_[(int)in[i + 1] - 43] - 62;
497 if (i + 1 >= src_size)
499 element[offset] = (
unsigned char) ((a << 2) | (b >> 4));
501 offset = (offset + inc) % element_size;
503 if (written % element_size == 0)
505 ToType * to_type =
reinterpret_cast<ToType *
>(&element[0]);
506 out.push_back((*to_type));
510 a =
decoder_[(int)in[i + 2] - 43] - 62;
511 if (i + 2 >= src_size)
513 element[offset] = (
unsigned char) (((b & 15) << 4) | (a >> 2));
515 offset = (offset + inc) % element_size;
517 if (written % element_size == 0)
519 ToType * to_type =
reinterpret_cast<ToType *
>(&element[0]);
520 out.push_back((*to_type));
524 b =
decoder_[(int)in[i + 3] - 43] - 62;
525 if (i + 3 >= src_size)
527 element[offset] = (
unsigned char) (((a & 3) << 6) | b);
529 offset = (offset + inc) % element_size;
531 if (written % element_size == 0)
533 ToType * to_type =
reinterpret_cast<ToType *
>(&element[0]);
534 out.push_back((*to_type));
540 template <
typename FromType>
548 const Size element_size =
sizeof(FromType);
549 const Size input_bytes = element_size * in.size();
556 if (element_size == 4)
558 for (
Size i = 0; i < in.size(); ++i)
567 for (
Size i = 0; i < in.size(); ++i)
577 if (zlib_compression)
579 unsigned long sourceLen = (
unsigned long)input_bytes;
580 unsigned long compressed_length =
581 sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
583 compressed.resize(compressed_length);
584 while (compress(reinterpret_cast<Bytef *>(&compressed[0]), &compressed_length, reinterpret_cast<Bytef *>(&in[0]), (
unsigned long)input_bytes) != Z_OK)
586 compressed_length *= 2;
587 compressed.reserve(compressed_length);
591 String(compressed).swap(compressed);
592 it =
reinterpret_cast<Byte *
>(&compressed[0]);
593 end = it + compressed_length;
594 out.resize((
Size)ceil(compressed_length / 3.) * 4);
599 out.resize((
Size)ceil(input_bytes / 3.) * 4);
600 it =
reinterpret_cast<Byte *
>(&in[0]);
601 end = it + input_bytes;
604 Byte * to =
reinterpret_cast<Byte *
>(&out[0]);
612 Int padding_count = 0;
615 for (
Size i = 0; i < 3; i++)
619 int_24bit |= *it++ << ((2 - i) * 8);
628 for (
Int i = 3; i >= 0; i--)
635 if (padding_count > 0)
637 if (padding_count > 1)
647 template <
typename ToType>
650 if (zlib_compression)
660 template <
typename ToType>
669 std::vector<unsigned char> binary;
670 const Size element_size =
sizeof(ToType);
674 QByteArray qt_byte_array = QByteArray::fromRawData(in.c_str(), (int) in.size());
675 QByteArray bazip = QByteArray::fromBase64(qt_byte_array);
678 czip[0] = (bazip.size() & 0xff000000) >> 24;
679 czip[1] = (bazip.size() & 0x00ff0000) >> 16;
680 czip[2] = (bazip.size() & 0x0000ff00) >> 8;
681 czip[3] = (bazip.size() & 0x000000ff);
683 QByteArray base64_uncompressed = qUncompress(czip);
684 if (base64_uncompressed.isEmpty())
688 decompressed.resize(base64_uncompressed.size());
690 std::copy(base64_uncompressed.begin(), base64_uncompressed.end(), decompressed.begin());
692 byte_buffer =
reinterpret_cast<void *
>(&decompressed[0]);
693 buffer_size = decompressed.size();
698 if (element_size == 4)
700 const Int32 * float_buffer =
reinterpret_cast<const Int32 *
>(byte_buffer);
701 if (buffer_size % element_size != 0)
703 Size float_count = buffer_size / element_size;
704 Int32 * p =
reinterpret_cast<Int32 *
>(byte_buffer);
705 std::transform(p, p + float_count, p,
endianize32);
707 out.resize(float_count);
709 for (
Size i = 0; i < float_count; ++i)
711 out[i] = (ToType) * float_buffer;
717 const Int64 * float_buffer =
reinterpret_cast<const Int64 *
>(byte_buffer);
719 if (buffer_size % element_size != 0)
722 Size float_count = buffer_size / element_size;
724 Int64 * p =
reinterpret_cast<Int64 *
>(byte_buffer);
725 std::transform(p, p + float_count, p,
endianize64);
727 out.resize(float_count);
729 for (
Size i = 0; i < float_count; ++i)
731 out[i] = (ToType) * float_buffer;
738 if (element_size == 4)
740 const Int * float_buffer =
reinterpret_cast<const Int *
>(byte_buffer);
741 if (buffer_size % element_size != 0)
744 Size float_count = buffer_size / element_size;
745 out.resize(float_count);
747 for (
Size i = 0; i < float_count; ++i)
749 out[i] = (ToType) * float_buffer;
755 const Int64 * float_buffer =
reinterpret_cast<const Int64 *
>(byte_buffer);
757 if (buffer_size % element_size != 0)
760 Size float_count = buffer_size / element_size;
761 out.resize(float_count);
763 for (
Size i = 0; i < float_count; ++i)
765 out[i] = (ToType) * float_buffer;
773 template <
typename ToType>
780 Size src_size = in.size();
783 if (in[src_size - 1] ==
'=')
785 if (in[src_size - 2] ==
'=')
797 const Size element_size =
sizeof(ToType);
800 char element[8] =
"\x00\x00\x00\x00\x00\x00\x00";
804 offset = (element_size - 1);
814 out.reserve((
UInt)(std::ceil((4.0 * src_size) / 3.0) + 6.0));
818 for (
Size i = 0; i < src_size; i += 4)
824 b =
decoder_[(int)in[i + 1] - 43] - 62;
825 if (i + 1 >= src_size)
827 element[offset] = (
unsigned char) ((a << 2) | (b >> 4));
830 offset = (offset + inc) % element_size;
832 if (written % element_size == 0)
835 if (element_size == 4)
837 Int32 * value =
reinterpret_cast<Int32 *
>(&element[0]);
838 float_value = (ToType) * value;
842 Int64 * value =
reinterpret_cast<Int64 *
>(&element[0]);
843 float_value = (ToType) * value;
845 out.push_back(float_value);
849 a =
decoder_[(int)in[i + 2] - 43] - 62;
850 if (i + 2 >= src_size)
852 element[offset] = (
unsigned char) (((b & 15) << 4) | (a >> 2));
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);
873 b =
decoder_[(int)in[i + 3] - 43] - 62;
874 if (i + 3 >= src_size)
876 element[offset] = (
unsigned char) (((a & 3) << 6) | b);
878 offset = (offset + inc) % element_size;
880 if (written % element_size == 0)
883 if (element_size == 4)
885 Int32 * value =
reinterpret_cast<Int32 *
>(&element[0]);
886 float_value = (ToType) * value;
890 Int64 * value =
reinterpret_cast<Int64 *
>(&element[0]);
891 float_value = (ToType) * value;
893 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
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:774
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:541
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:648
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:661
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
static const char decoder_[]
Definition: Base64.h:169
static const char encoder_[]
Definition: Base64.h:168
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