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 & 0x000000ff) << 24) | 
   191            ((n & 0x0000ff00) <<  8) |
   192            ((n & 0x00ff0000) >>  8) |
   193            ((n & 0xff000000) >> 24);
   199     return ((n >> 56) & 0x00000000000000FF) |
   200            ((n >> 40) & 0x000000000000FF00) | 
   201            ((n >> 24) & 0x0000000000FF0000) | 
   202            ((n >>  8) & 0x00000000FF000000) |
   203            ((n <<  8) & 0x000000FF00000000) |
   204            ((n << 24) & 0x0000FF0000000000) |
   205            ((n << 40) & 0x00FF000000000000) | 
   206            ((n << 56) & 0xFF00000000000000);
   209   template <
typename FromType>
   217     const Size element_size = 
sizeof(FromType);
   218     const Size input_bytes = element_size * in.size();
   225       if (element_size == 4)
   227         for (
Size i = 0; i < in.size(); ++i)
   237         for (
Size i = 0; i < in.size(); ++i)
   240           tmp.
f = 
static_cast<double>(in[i]);
   248     if (zlib_compression)
   250       unsigned long sourceLen =   (
unsigned long)in.size();
   251       unsigned long compressed_length =       
   252                                         sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11; 
   260         compressed.resize(compressed_length);
   261         zlib_error = compress(reinterpret_cast<Bytef *>(&compressed[0]), &compressed_length, reinterpret_cast<Bytef *>(&in[0]), (
unsigned long)input_bytes);
   270           compressed_length *= 2;
   273       while (zlib_error == Z_BUF_ERROR);
   275       if (zlib_error != Z_OK)
   280       String(compressed).swap(compressed);
   281       it = 
reinterpret_cast<Byte *
>(&compressed[0]);
   282       end = it + compressed_length;
   283       out.resize((
Size)ceil(compressed_length / 3.) * 4);     
   288       out.resize((
Size)ceil(input_bytes / 3.) * 4);     
   289       it = 
reinterpret_cast<Byte *
>(&in[0]);
   290       end = it + input_bytes;
   293     Byte * to = 
reinterpret_cast<Byte *
>(&out[0]);
   301       Int padding_count = 0;
   304       for (
Size i = 0; i < 3; i++)
   308           int_24bit |= *it++ << ((2 - i) * 8);
   317       for (
Int i = 3; i >= 0; i--)
   319         to[i] = encoder_[int_24bit & 0x3F];
   324       if (padding_count > 0)
   326       if (padding_count > 1)
   336   template <
typename ToType>
   339     if (zlib_compression)
   341       decodeCompressed_(in, from_byte_order, out);
   345       decodeUncompressed_(in, from_byte_order, out);
   349   template <
typename ToType>
   353     if (in == 
"") 
return;
   355     const Size element_size = 
sizeof(ToType);
   359     QByteArray qt_byte_array = QByteArray::fromRawData(in.c_str(), (int) in.size());
   360     QByteArray bazip = QByteArray::fromBase64(qt_byte_array);
   363     czip[0] = (bazip.size() & 0xff000000) >> 24;
   364     czip[1] = (bazip.size() & 0x00ff0000) >> 16;
   365     czip[2] = (bazip.size() & 0x0000ff00) >> 8;
   366     czip[3] = (bazip.size() & 0x000000ff);
   368     QByteArray base64_uncompressed = qUncompress(czip);
   370     if (base64_uncompressed.isEmpty())
   374     decompressed.resize(base64_uncompressed.size());
   376     std::copy(base64_uncompressed.begin(), base64_uncompressed.end(), decompressed.begin());
   378     void* byte_buffer = 
reinterpret_cast<void *
>(&decompressed[0]);
   379     Size buffer_size = decompressed.size();
   381     const ToType * float_buffer = 
reinterpret_cast<const ToType *
>(byte_buffer);
   382     if (buffer_size % element_size != 0)
   387     Size float_count = buffer_size / element_size;
   392       if (element_size == 4) 
   395         std::transform(p, p + float_count, p, 
endianize32);
   400         std::transform(p, p + float_count, p, 
endianize64);
   405     out.assign(float_buffer, float_buffer + float_count);
   408   template <
typename ToType>
   419     if (in.size() % 4 != 0)
   421       throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, 
"Malformed base64 input, length is not a multiple of 4.");
   424     Size src_size = in.size();
   427     if (in[src_size - 1] == 
'=') padding++;
   428     if (in[src_size - 2] == 
'=') padding++;
   439     const Size element_size = 
sizeof(ToType);
   442     char element[8] = 
"\x00\x00\x00\x00\x00\x00\x00";
   448       offset = (element_size - 1);              
   458     out.reserve((
UInt)(std::ceil((4.0 * src_size) / 3.0) + 6.0));
   462     for (
Size i = 0; i < src_size; i += 4)
   468       a = decoder_[(int)in[i] - 43] - 62;
   469       b = decoder_[(int)in[i + 1] - 43] - 62;
   470       if (i + 1 >= src_size)
   475       element[offset] = (
unsigned char) ((a << 2) | (b >> 4));
   477       offset = (offset + inc) % element_size;
   479       if (written % element_size == 0)
   481         ToType * to_type = 
reinterpret_cast<ToType *
>(&element[0]);
   482         out.push_back((*to_type));
   487       a = decoder_[(int)in[i + 2] - 43] - 62;
   488       if (i + 2 >= src_size)
   493       element[offset] = (
unsigned char) (((b & 15) << 4) | (a >> 2));
   495       offset = (offset + inc) % element_size;
   497       if (written % element_size == 0)
   499         ToType * to_type = 
reinterpret_cast<ToType *
>(&element[0]);
   500         out.push_back((*to_type));
   505       b = decoder_[(int)in[i + 3] - 43] - 62;
   506       if (i + 3 >= src_size)
   511       element[offset] = (
unsigned char) (((a & 3) << 6) | b);
   513       offset = (offset + inc) % element_size;
   515       if (written % element_size == 0)
   517         ToType * to_type = 
reinterpret_cast<ToType *
>(&element[0]);
   518         out.push_back((*to_type));
   524   template <
typename FromType>
   532     const Size element_size = 
sizeof(FromType);
   533     const Size input_bytes = element_size * in.size();
   540       if (element_size == 4)
   542         for (
Size i = 0; i < in.size(); ++i)
   551         for (
Size i = 0; i < in.size(); ++i)
   561     if (zlib_compression)
   563       unsigned long sourceLen =   (
unsigned long)input_bytes;
   564       unsigned long compressed_length =       
   565                                         sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11; 
   567       compressed.resize(compressed_length);
   568       while (compress(reinterpret_cast<Bytef *>(&compressed[0]), &compressed_length, reinterpret_cast<Bytef *>(&in[0]), (
unsigned long)input_bytes) != Z_OK)
   570         compressed_length *= 2;
   571         compressed.reserve(compressed_length);
   575       String(compressed).swap(compressed);
   576       it = 
reinterpret_cast<Byte *
>(&compressed[0]);
   577       end = it + compressed_length;
   578       out.resize((
Size)ceil(compressed_length / 3.) * 4);     
   583       out.resize((
Size)ceil(input_bytes / 3.) * 4);     
   584       it = 
reinterpret_cast<Byte *
>(&in[0]);
   585       end = it + input_bytes;
   588     Byte * to = 
reinterpret_cast<Byte *
>(&out[0]);
   596       Int padding_count = 0;
   599       for (
Size i = 0; i < 3; i++)
   603           int_24bit |= *it++ << ((2 - i) * 8);
   612       for (
Int i = 3; i >= 0; i--)
   614         to[i] = encoder_[int_24bit & 0x3F];
   619       if (padding_count > 0)
   621       if (padding_count > 1)
   631   template <
typename ToType>
   634     if (zlib_compression)
   636       decodeIntegersCompressed_(in, from_byte_order, out);
   640       decodeIntegersUncompressed_(in, from_byte_order, out);
   644   template <
typename ToType>
   653     const Size element_size = 
sizeof(ToType);
   657     QByteArray qt_byte_array = QByteArray::fromRawData(in.c_str(), (int) in.size());
   658     QByteArray bazip = QByteArray::fromBase64(qt_byte_array);
   661     czip[0] = (bazip.size() & 0xff000000) >> 24;
   662     czip[1] = (bazip.size() & 0x00ff0000) >> 16;
   663     czip[2] = (bazip.size() & 0x0000ff00) >> 8;
   664     czip[3] = (bazip.size() & 0x000000ff);
   666     QByteArray base64_uncompressed = qUncompress(czip);
   667     if (base64_uncompressed.isEmpty())
   671     decompressed.resize(base64_uncompressed.size());
   673     std::copy(base64_uncompressed.begin(), base64_uncompressed.end(), decompressed.begin());
   675     byte_buffer = 
reinterpret_cast<void *
>(&decompressed[0]);
   676     buffer_size = decompressed.size();
   681       if (element_size == 4)
   683         const Int32 * float_buffer = 
reinterpret_cast<const Int32 *
>(byte_buffer);
   684         if (buffer_size % element_size != 0)
   686         Size float_count = buffer_size / element_size;
   688         std::transform(p, p + float_count, p, 
endianize32);
   690         out.resize(float_count);
   692         for (
Size i = 0; i < float_count; ++i)
   694           out[i] = (ToType) * float_buffer;
   700         const Int64 * float_buffer = 
reinterpret_cast<const Int64 *
>(byte_buffer);
   702         if (buffer_size % element_size != 0)
   705         Size float_count = buffer_size / element_size;
   708         std::transform(p, p + float_count, p, 
endianize64);
   710         out.resize(float_count);
   712         for (
Size i = 0; i < float_count; ++i)
   714           out[i] = (ToType) * float_buffer;
   721       if (element_size == 4)
   723         const Int * float_buffer = 
reinterpret_cast<const Int *
>(byte_buffer);
   724         if (buffer_size % element_size != 0)
   727         Size float_count = buffer_size / element_size;
   728         out.resize(float_count);
   730         for (
Size i = 0; i < float_count; ++i)
   732           out[i] = (ToType) * float_buffer;
   738         const Int64 * float_buffer = 
reinterpret_cast<const Int64 *
>(byte_buffer);
   740         if (buffer_size % element_size != 0)
   743         Size float_count = buffer_size / element_size;
   744         out.resize(float_count);
   746         for (
Size i = 0; i < float_count; ++i)
   748           out[i] = (ToType) * float_buffer;
   756   template <
typename ToType>
   768     Size src_size = in.size();
   771     if (in[src_size - 1] == 
'=') padding++;
   772     if (in[src_size - 2] == 
'=') padding++;
   783     const Size element_size = 
sizeof(ToType);
   786     char element[8] = 
"\x00\x00\x00\x00\x00\x00\x00";
   790       offset = (element_size - 1);              
   800     out.reserve((
UInt)(std::ceil((4.0 * src_size) / 3.0) + 6.0));
   804     for (
Size i = 0; i < src_size; i += 4)
   811       a = decoder_[(int)in[i] - 43] - 62;
   812       b = decoder_[(int)in[i + 1] - 43] - 62;
   813       if (i + 1 >= src_size)
   818       element[offset] = (
unsigned char) ((a << 2) | (b >> 4));
   820       offset = (offset + inc) % element_size;
   822       if (written % element_size == 0)
   825         if (element_size == 4)
   827           Int32 * value = 
reinterpret_cast<Int32 *
>(&element[0]);
   828           float_value = (ToType) * value;
   832           Int64 * value = 
reinterpret_cast<Int64 *
>(&element[0]);
   833           float_value = (ToType) * value;
   835         out.push_back(float_value);
   840       a = decoder_[(int)in[i + 2] - 43] - 62;
   841       if (i + 2 >= src_size)
   846       element[offset] = (
unsigned char) (((b & 15) << 4) | (a >> 2));
   848       offset = (offset + inc) % element_size;
   850       if (written % element_size == 0)
   853         if (element_size == 4)
   855           Int32 * value = 
reinterpret_cast<Int32 *
>(&element[0]);
   856           float_value = (ToType) * value;
   860           Int64 * value = 
reinterpret_cast<Int64 *
>(&element[0]);
   861           float_value = (ToType) * value;
   863         out.push_back(float_value);
   868       b = decoder_[(int)in[i + 3] - 43] - 62;
   869       if (i + 3 >= src_size)
   874       element[offset] = (
unsigned char) (((a & 3) << 6) | b);
   876       offset = (offset + inc) % element_size;
   878       if (written % element_size == 0)
   881         if (element_size == 4)
   883           Int32 * value = 
reinterpret_cast<Int32 *
>(&element[0]);
   884           float_value = (ToType) * value;
   888           Int64 * value = 
reinterpret_cast<Int64 *
>(&element[0]);
   889           float_value = (ToType) * value;
   891         out.push_back(float_value);
 Big endian type. 
Definition: Base64.h:78
 
UInt64 endianize64(const UInt64 &n)
Endianizes a 64 bit type from big endian to little endian and vice versa. 
Definition: Base64.h:197
 
A more convenient string class. 
Definition: String.h:57
 
Class to encode and decode Base64. 
Definition: Base64.h:64
 
unsigned int UInt
Unsigned integer type. 
Definition: Types.h:95
 
Little endian type. 
Definition: Base64.h:79
 
Main OpenMS namespace. 
Definition: FeatureDeconvolution.h:47
 
UInt32 endianize32(const UInt32 &n)
Endianizes a 32 bit type from big endian to little endian and vice versa. 
Definition: Base64.h:188
 
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:757
 
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:525
 
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:632
 
UInt32 i
Definition: Base64.h:165
 
OPENMS_INT32_TYPE Int32
Signed integer type (32bit) 
Definition: Types.h:57
 
OPENMS_UINT32_TYPE UInt32
Unsigned integer type (32bit) 
Definition: Types.h:64
 
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:645
 
OPENMS_UINT64_TYPE UInt64
Unsigned integer type (64bit) 
Definition: Types.h:78
 
Invalid conversion exception. 
Definition: Exception.h:363
 
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:409
 
#define OPENMS_IS_BIG_ENDIAN
Definition: Base64.h:42
 
size_t Size
Size type e.g. used as variable which can hold result of size() 
Definition: Types.h:128
 
OPENMS_INT64_TYPE Int64
Signed integer type (64bit) 
Definition: Types.h:71
 
OPENMS_BYTE_TYPE Byte
Byte type. 
Definition: Types.h:112
 
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:210
 
Internal class needed for type-punning. 
Definition: Base64.h:155
 
int Int
Signed integer type. 
Definition: Types.h:103
 
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:337
 
UInt64 i
Definition: Base64.h:158
 
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:350