/usr/include/OpenMS/FORMAT/Base64.h is in libopenms-dev 1.11.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 | // --------------------------------------------------------------------------
// OpenMS -- Open-Source Mass Spectrometry
// --------------------------------------------------------------------------
// Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
// ETH Zurich, and Freie Universitaet Berlin 2002-2013.
//
// This software is released under a three-clause BSD license:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of any author or any participating institution
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
// For a full list of authors, refer to the file AUTHORS.
// --------------------------------------------------------------------------
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
// INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// --------------------------------------------------------------------------
// $Maintainer: Andreas Bertsch $
// $Authors: Marc Sturm $
// --------------------------------------------------------------------------
#ifndef OPENMS_FORMAT_BASE64_H
#define OPENMS_FORMAT_BASE64_H
#ifndef OPENMS_IS_BIG_ENDIAN
#if defined OPENMS_BIG_ENDIAN
#define OPENMS_IS_BIG_ENDIAN true
#else
#define OPENMS_IS_BIG_ENDIAN false
#endif
#endif
#include <OpenMS/CONCEPT/Types.h>
#include <OpenMS/CONCEPT/Exception.h>
#include <OpenMS/DATASTRUCTURES/String.h>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <vector>
#include <QByteArray>
#include <zlib.h>
namespace OpenMS
{
/**
@brief Class to encode and decode Base64
Base64 supports two precisions: 32 bit (float) and 64 bit (double).
*/
class OPENMS_DLLAPI Base64
{
public:
/// default constructor
Base64();
/// Destructor
virtual ~Base64();
/// Byte order type
enum ByteOrder
{
BYTEORDER_BIGENDIAN, ///< Big endian type
BYTEORDER_LITTLEENDIAN ///< Little endian type
};
/**
@brief Encodes a vector of floating point numbers to a Base64 string
You can specify the byte order of the output and if it is zlib-compressed.
@note @p in will be empty after this method
*/
template <typename FromType>
void encode(std::vector<FromType> & in, ByteOrder to_byte_order, String & out, bool zlib_compression = false);
/**
@brief Decodes a Base64 string to a vector of floating point numbers
You have to specify the byte order of the input and if it is zlib-compressed.
@note @p in will be empty after this method
*/
template <typename ToType>
void decode(const String & in, ByteOrder from_byte_order, std::vector<ToType> & out, bool zlib_compression = false);
/**
@brief Encodes a vector of integer point numbers to a Base64 string
You can specify the byte order of the output and if it is zlib-compressed.
@note @p in will be empty after this method
*/
template <typename FromType>
void encodeIntegers(std::vector<FromType> & in, ByteOrder to_byte_order, String & out, bool zlib_compression = false);
/**
@brief Decodes a Base64 string to a vector of integer numbers
You have to specify the byte order of the input and if it is zlib-compressed.
@note @p in will be empty after this method
*/
template <typename ToType>
void decodeIntegers(const String & in, ByteOrder from_byte_order, std::vector<ToType> & out, bool zlib_compression = false);
/**
@brief Encodes a vector of strings to a Base64 string
You can specify zlib-compression.
@note @p in will be empty after this method
*/
void encodeStrings(std::vector<String> & in, String & out, bool zlib_compression = false);
/**
@brief Decodes a Base64 string to a vector of (null-terminated) strings
You have to specify whether the Base64 string is zlib-compressed.
@note @p in will be empty after this method
*/
void decodeStrings(const String & in, std::vector<String> & out, bool zlib_compression = false);
private:
///Internal class needed for type-punning
union Reinterpreter64_
{
DoubleReal f;
Int64 i;
};
///Internal class needed for type-punning
union Reinterpreter32_
{
Real f;
Int32 i;
};
static const char encoder_[];
static const char decoder_[];
/// Decodes a Base64 string to a vector of floating point numbers
template <typename ToType>
void decodeUncompressed_(const String & in, ByteOrder from_byte_order, std::vector<ToType> & out);
///Decodes a compressed Base64 string to a vector of floating point numbers
template <typename ToType>
void decodeCompressed_(const String & in, ByteOrder from_byte_order, std::vector<ToType> & out);
/// Decodes a Base64 string to a vector of integer numbers
template <typename ToType>
void decodeIntegersUncompressed_(const String & in, ByteOrder from_byte_order, std::vector<ToType> & out);
///Decodes a compressed Base64 string to a vector of integer numbers
template <typename ToType>
void decodeIntegersCompressed_(const String & in, ByteOrder from_byte_order, std::vector<ToType> & out);
};
///Endianizes a 32 bit type from big endian to little endian and vice versa
inline Int32 endianize32(Int32 & n)
{
return ((n & 0xff) << 24) | ((n & 0xff00) << 8) | ((n & 0xff0000) >> 8) | ((n & 0xff000000) >> 24);
}
///Endianizes a 64 bit type from big endian to little endian and vice versa
inline Int64 endianize64(Int64 & n)
{
return ((n & 0x00000000000000ffll) << 56) |
((n & 0x000000000000ff00ll) << 40) |
((n & 0x0000000000ff0000ll) << 24) |
((n & 0x00000000ff000000ll) << 8) |
((n & 0x000000ff00000000ll) >> 8) |
((n & 0x0000ff0000000000ll) >> 24) |
((n & 0x00ff000000000000ll) >> 40) |
((n & 0xff00000000000000ll) >> 56);
}
template <typename FromType>
void Base64::encode(std::vector<FromType> & in, ByteOrder to_byte_order, String & out, bool zlib_compression)
{
out.clear();
if (in.empty())
return;
//initialize
const Size element_size = sizeof(FromType);
const Size input_bytes = element_size * in.size();
String compressed;
Byte * it;
Byte * end;
//Change endianness if necessary
if ((OPENMS_IS_BIG_ENDIAN && to_byte_order == Base64::BYTEORDER_LITTLEENDIAN) || (!OPENMS_IS_BIG_ENDIAN && to_byte_order == Base64::BYTEORDER_BIGENDIAN))
{
if (element_size == 4)
{
for (Size i = 0; i < in.size(); ++i)
{
Reinterpreter32_ tmp;
tmp.f = in[i];
tmp.i = endianize32(tmp.i);
in[i] = tmp.f;
}
}
else
{
for (Size i = 0; i < in.size(); ++i)
{
Reinterpreter64_ tmp;
tmp.f = in[i];
tmp.i = endianize64(tmp.i);
in[i] = tmp.f;
}
}
}
//encode with compression
if (zlib_compression)
{
unsigned long sourceLen = (unsigned long)in.size();
unsigned long compressed_length = //compressBound((unsigned long)in.size());
sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11; // taken from zlib's compress.c, as we cannot use compressBound*
//
// (*) compressBound is not defined in the QtCore lib, which forces the linker under windows to link in our zlib.
// This leads to multiply defined symbols as compress() is then defined twice.
int zlib_error;
do
{
compressed.resize(compressed_length);
zlib_error = compress(reinterpret_cast<Bytef *>(&compressed[0]), &compressed_length, reinterpret_cast<Bytef *>(&in[0]), (unsigned long)input_bytes);
switch (zlib_error)
{
case Z_MEM_ERROR:
throw Exception::OutOfMemory(__FILE__, __LINE__, __PRETTY_FUNCTION__, compressed_length);
break;
case Z_BUF_ERROR:
compressed_length *= 2;
}
}
while (zlib_error == Z_BUF_ERROR);
if (zlib_error != Z_OK)
{
throw Exception::ConversionError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Compression error?");
}
String(compressed).swap(compressed);
it = reinterpret_cast<Byte *>(&compressed[0]);
end = it + compressed_length;
out.resize((Size)ceil(compressed_length / 3.) * 4); //resize output array in order to have enough space for all characters
}
//encode without compression
else
{
out.resize((Size)ceil(input_bytes / 3.) * 4); //resize output array in order to have enough space for all characters
it = reinterpret_cast<Byte *>(&in[0]);
end = it + input_bytes;
}
Byte * to = reinterpret_cast<Byte *>(&out[0]);
Size written = 0;
while (it != end)
{
Int int_24bit = 0;
Int padding_count = 0;
// construct 24-bit integer from 3 bytes
for (Size i = 0; i < 3; i++)
{
if (it != end)
{
int_24bit |= *it++ << ((2 - i) * 8);
}
else
{
padding_count++;
}
}
// write out 4 characters
for (Int i = 3; i >= 0; i--)
{
to[i] = encoder_[int_24bit & 0x3F];
int_24bit >>= 6;
}
// fixup for padding
if (padding_count > 0)
to[3] = '=';
if (padding_count > 1)
to[2] = '=';
to += 4;
written += 4;
}
out.resize(written); //no more space is needed
}
template <typename ToType>
void Base64::decode(const String & in, ByteOrder from_byte_order, std::vector<ToType> & out, bool zlib_compression)
{
if (zlib_compression)
{
decodeCompressed_(in, from_byte_order, out);
}
else
{
decodeUncompressed_(in, from_byte_order, out);
}
}
template <typename ToType>
void Base64::decodeCompressed_(const String & in, ByteOrder from_byte_order, std::vector<ToType> & out)
{
out.clear();
if (in == "")
return;
void * byte_buffer;
Size buffer_size;
std::vector<unsigned char> binary;
const Size element_size = sizeof(ToType);
String decompressed;
QByteArray qt_byte_array = QByteArray::fromRawData(in.c_str(), (int) in.size());
QByteArray bazip = QByteArray::fromBase64(qt_byte_array);
QByteArray czip;
czip.resize(4);
czip[0] = (bazip.size() & 0xff000000) >> 24;
czip[1] = (bazip.size() & 0x00ff0000) >> 16;
czip[2] = (bazip.size() & 0x0000ff00) >> 8;
czip[3] = (bazip.size() & 0x000000ff);
czip += bazip;
QByteArray base64_uncompressed = qUncompress(czip);
if (base64_uncompressed.isEmpty())
{
throw Exception::ConversionError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Decompression error?");
}
decompressed.resize(base64_uncompressed.size());
std::copy(base64_uncompressed.begin(), base64_uncompressed.end(), decompressed.begin());
byte_buffer = reinterpret_cast<void *>(&decompressed[0]);
buffer_size = decompressed.size();
//change endianness if necessary
if ((OPENMS_IS_BIG_ENDIAN && from_byte_order == Base64::BYTEORDER_LITTLEENDIAN) || (!OPENMS_IS_BIG_ENDIAN && from_byte_order == Base64::BYTEORDER_BIGENDIAN))
{
if (element_size == 4)
{
const Real * float_buffer = reinterpret_cast<const Real *>(byte_buffer);
if (buffer_size % element_size != 0)
throw Exception::ConversionError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Bad BufferCount?");
Size float_count = buffer_size / element_size;
Int32 * p = reinterpret_cast<Int32 *>(byte_buffer);
std::transform(p, p + float_count, p, endianize32);
out.assign(float_buffer, float_buffer + float_count);
}
else
{
const DoubleReal * float_buffer = reinterpret_cast<const DoubleReal *>(byte_buffer);
if (buffer_size % element_size != 0)
throw Exception::ConversionError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Bad BufferCount?");
Size float_count = buffer_size / element_size;
Int64 * p = reinterpret_cast<Int64 *>(byte_buffer);
std::transform(p, p + float_count, p, endianize64);
out.resize(float_count);
// do NOT use assign here, as it will give a lot of type conversion warnings on VS compiler
for (Size i = 0; i < float_count; ++i)
{
out[i] = (ToType) * float_buffer;
++float_buffer;
}
}
}
else
{
if (element_size == 4)
{
const Real * float_buffer = reinterpret_cast<const Real *>(byte_buffer);
if (buffer_size % element_size != 0)
throw Exception::ConversionError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Bad BufferCount while decoding?");
Size float_count = buffer_size / element_size;
out.assign(float_buffer, float_buffer + float_count);
}
else
{
const DoubleReal * float_buffer = reinterpret_cast<const DoubleReal *>(byte_buffer);
if (buffer_size % element_size != 0)
throw Exception::ConversionError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Bad BufferCount while decoding?");
Size float_count = buffer_size / element_size;
out.resize(float_count);
// do NOT use assign here, as it will give a lot of type conversion warnings on VS compiler
for (Size i = 0; i < float_count; ++i)
{
out[i] = (ToType) * float_buffer;
++float_buffer;
}
}
}
}
template <typename ToType>
void Base64::decodeUncompressed_(const String & in, ByteOrder from_byte_order, std::vector<ToType> & out)
{
out.clear();
if (in == "")
return;
Size src_size = in.size();
// last one or two '=' are skipped if contained
int padding = 0;
if (in[src_size - 1] == '=')
padding++;
if (in[src_size - 2] == '=')
padding++;
src_size -= padding;
register UInt a;
register UInt b;
UInt offset = 0;
int inc = 1;
UInt written = 0;
const Size element_size = sizeof(ToType);
// enough for either float or double
char element[8] = "\x00\x00\x00\x00\x00\x00\x00";
if ((OPENMS_IS_BIG_ENDIAN && from_byte_order == Base64::BYTEORDER_LITTLEENDIAN) || (!OPENMS_IS_BIG_ENDIAN && from_byte_order == Base64::BYTEORDER_BIGENDIAN))
{
offset = (element_size - 1); // other endian
inc = -1;
}
else
{
offset = 0;
inc = 1;
}
//reserve enough space in the output vector
out.reserve((UInt)(std::ceil((4.0 * src_size) / 3.0) + 6.0));
// sort all read bytes correctly into a char[4] (double) or
// char[8] (Real) and push_back when necessary.
for (Size i = 0; i < src_size; i += 4)
{
// decode 4 Base64-Chars to 3 Byte
a = decoder_[(int)in[i] - 43] - 62;
b = decoder_[(int)in[i + 1] - 43] - 62;
if (i + 1 >= src_size)
b = 0;
element[offset] = (unsigned char) ((a << 2) | (b >> 4));
written++;
offset = (offset + inc) % element_size;
if (written % element_size == 0)
{
ToType * to_type = reinterpret_cast<ToType *>(&element[0]);
out.push_back((*to_type));
strcpy(element, "");
}
a = decoder_[(int)in[i + 2] - 43] - 62;
if (i + 2 >= src_size)
a = 0;
element[offset] = (unsigned char) (((b & 15) << 4) | (a >> 2));
written++;
offset = (offset + inc) % element_size;
if (written % element_size == 0)
{
ToType * to_type = reinterpret_cast<ToType *>(&element[0]);
out.push_back((*to_type));
strcpy(element, "");
}
b = decoder_[(int)in[i + 3] - 43] - 62;
if (i + 3 >= src_size)
b = 0;
element[offset] = (unsigned char) (((a & 3) << 6) | b);
written++;
offset = (offset + inc) % element_size;
if (written % element_size == 0)
{
ToType * to_type = reinterpret_cast<ToType *>(&element[0]);
out.push_back((*to_type));
strcpy(element, "");
}
}
}
template <typename FromType>
void Base64::encodeIntegers(std::vector<FromType> & in, ByteOrder to_byte_order, String & out, bool zlib_compression)
{
out.clear();
if (in.empty())
return;
//initialize
const Size element_size = sizeof(FromType);
const Size input_bytes = element_size * in.size();
String compressed;
Byte * it;
Byte * end;
//Change endianness if necessary
if ((OPENMS_IS_BIG_ENDIAN && to_byte_order == Base64::BYTEORDER_LITTLEENDIAN) || (!OPENMS_IS_BIG_ENDIAN && to_byte_order == Base64::BYTEORDER_BIGENDIAN))
{
if (element_size == 4)
{
for (Size i = 0; i < in.size(); ++i)
{
Int32 tmp = in[i];
tmp = endianize32(tmp);
in[i] = tmp;
}
}
else
{
for (Size i = 0; i < in.size(); ++i)
{
Int64 tmp = in[i];
tmp = endianize64(tmp);
in[i] = tmp;
}
}
}
//encode with compression (use Qt because of zlib support)
if (zlib_compression)
{
unsigned long sourceLen = (unsigned long)input_bytes;
unsigned long compressed_length = //compressBound((unsigned long)in.size());
sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11; // taken from zlib's compress.c, as we cannot use compressBound*
compressed.resize(compressed_length);
while (compress(reinterpret_cast<Bytef *>(&compressed[0]), &compressed_length, reinterpret_cast<Bytef *>(&in[0]), (unsigned long)input_bytes) != Z_OK)
{
compressed_length *= 2;
compressed.reserve(compressed_length);
}
String(compressed).swap(compressed);
it = reinterpret_cast<Byte *>(&compressed[0]);
end = it + compressed_length;
out.resize((Size)ceil(compressed_length / 3.) * 4); //resize output array in order to have enough space for all characters
}
//encode without compression
else
{
out.resize((Size)ceil(input_bytes / 3.) * 4); //resize output array in order to have enough space for all characters
it = reinterpret_cast<Byte *>(&in[0]);
end = it + input_bytes;
}
Byte * to = reinterpret_cast<Byte *>(&out[0]);
Size written = 0;
while (it != end)
{
Int int_24bit = 0;
Int padding_count = 0;
// construct 24-bit integer from 3 bytes
for (Size i = 0; i < 3; i++)
{
if (it != end)
{
int_24bit |= *it++ << ((2 - i) * 8);
}
else
{
padding_count++;
}
}
// write out 4 characters
for (Int i = 3; i >= 0; i--)
{
to[i] = encoder_[int_24bit & 0x3F];
int_24bit >>= 6;
}
// fixup for padding
if (padding_count > 0)
to[3] = '=';
if (padding_count > 1)
to[2] = '=';
to += 4;
written += 4;
}
out.resize(written); //no more space is needed
}
template <typename ToType>
void Base64::decodeIntegers(const String & in, ByteOrder from_byte_order, std::vector<ToType> & out, bool zlib_compression)
{
if (zlib_compression)
{
decodeIntegersCompressed_(in, from_byte_order, out);
}
else
{
decodeIntegersUncompressed_(in, from_byte_order, out);
}
}
template <typename ToType>
void Base64::decodeIntegersCompressed_(const String & in, ByteOrder from_byte_order, std::vector<ToType> & out)
{
out.clear();
if (in == "")
return;
void * byte_buffer;
Size buffer_size;
std::vector<unsigned char> binary;
const Size element_size = sizeof(ToType);
String decompressed;
QByteArray qt_byte_array = QByteArray::fromRawData(in.c_str(), (int) in.size());
QByteArray bazip = QByteArray::fromBase64(qt_byte_array);
QByteArray czip;
czip.resize(4);
czip[0] = (bazip.size() & 0xff000000) >> 24;
czip[1] = (bazip.size() & 0x00ff0000) >> 16;
czip[2] = (bazip.size() | 0x00000800) >> 8;
czip[3] = (bazip.size() & 0x000000ff);
czip += bazip;
QByteArray base64_uncompressed = qUncompress(czip);
if (base64_uncompressed.isEmpty())
{
throw Exception::ConversionError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Decompression error?");
}
decompressed.resize(base64_uncompressed.size());
std::copy(base64_uncompressed.begin(), base64_uncompressed.end(), decompressed.begin());
byte_buffer = reinterpret_cast<void *>(&decompressed[0]);
buffer_size = decompressed.size();
//change endianness if necessary
if ((OPENMS_IS_BIG_ENDIAN && from_byte_order == Base64::BYTEORDER_LITTLEENDIAN) || (!OPENMS_IS_BIG_ENDIAN && from_byte_order == Base64::BYTEORDER_BIGENDIAN))
{
if (element_size == 4)
{
const Int32 * float_buffer = reinterpret_cast<const Int32 *>(byte_buffer);
if (buffer_size % element_size != 0)
throw Exception::ConversionError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Bad BufferCount?");
Size float_count = buffer_size / element_size;
Int32 * p = reinterpret_cast<Int32 *>(byte_buffer);
std::transform(p, p + float_count, p, endianize32);
out.resize(float_count);
// do NOT use assign here, as it will give a lot of type conversion warnings on VS compiler
for (Size i = 0; i < float_count; ++i)
{
out[i] = (ToType) * float_buffer;
++float_buffer;
}
}
else
{
const Int64 * float_buffer = reinterpret_cast<const Int64 *>(byte_buffer);
if (buffer_size % element_size != 0)
throw Exception::ConversionError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Bad BufferCount?");
Size float_count = buffer_size / element_size;
Int64 * p = reinterpret_cast<Int64 *>(byte_buffer);
std::transform(p, p + float_count, p, endianize64);
out.resize(float_count);
// do NOT use assign here, as it will give a lot of type conversion warnings on VS compiler
for (Size i = 0; i < float_count; ++i)
{
out[i] = (ToType) * float_buffer;
++float_buffer;
}
}
}
else
{
if (element_size == 4)
{
const Int * float_buffer = reinterpret_cast<const Int *>(byte_buffer);
if (buffer_size % element_size != 0)
throw Exception::ConversionError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Bad BufferCount while decoding?");
Size float_count = buffer_size / element_size;
out.resize(float_count);
// do NOT use assign here, as it will give a lot of type conversion warnings on VS compiler
for (Size i = 0; i < float_count; ++i)
{
out[i] = (ToType) * float_buffer;
++float_buffer;
}
}
else
{
const Int64 * float_buffer = reinterpret_cast<const Int64 *>(byte_buffer);
if (buffer_size % element_size != 0)
throw Exception::ConversionError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Bad BufferCount while decoding?");
Size float_count = buffer_size / element_size;
out.resize(float_count);
// do NOT use assign here, as it will give a lot of type conversion warnings on VS compiler
for (Size i = 0; i < float_count; ++i)
{
out[i] = (ToType) * float_buffer;
++float_buffer;
}
}
}
}
template <typename ToType>
void Base64::decodeIntegersUncompressed_(const String & in, ByteOrder from_byte_order, std::vector<ToType> & out)
{
out.clear();
if (in == "")
return;
Size src_size = in.size();
// last one or two '=' are skipped if contained
int padding = 0;
if (in[src_size - 1] == '=')
padding++;
if (in[src_size - 2] == '=')
padding++;
src_size -= padding;
register UInt a;
register UInt b;
UInt offset = 0;
int inc = 1;
UInt written = 0;
const Size element_size = sizeof(ToType);
// enough for either float or double
char element[8] = "\x00\x00\x00\x00\x00\x00\x00";
if ((OPENMS_IS_BIG_ENDIAN && from_byte_order == Base64::BYTEORDER_LITTLEENDIAN) || (!OPENMS_IS_BIG_ENDIAN && from_byte_order == Base64::BYTEORDER_BIGENDIAN))
{
offset = (element_size - 1); // other endian
inc = -1;
}
else
{
offset = 0;
inc = 1;
}
//reserve enough space in the output vector
out.reserve((UInt)(std::ceil((4.0 * src_size) / 3.0) + 6.0));
// sort all read bytes correctly into a char[4] (double) or
// char[8] (Real) and push_back when necessary.
for (Size i = 0; i < src_size; i += 4)
{
//printf ("start: i=%d, offset %d\n", i, offset);
// decode 4 Base64-Chars to 3 Byte
a = decoder_[(int)in[i] - 43] - 62;
b = decoder_[(int)in[i + 1] - 43] - 62;
if (i + 1 >= src_size)
b = 0;
element[offset] = (unsigned char) ((a << 2) | (b >> 4));
written++;
//printf ("1: i=%d, offset %d, wrote %d\n", i, offset, element[offset]);
offset = (offset + inc) % element_size;
if (written % element_size == 0)
{
ToType float_value;
if (element_size == 4)
{
Int32 * value = reinterpret_cast<Int32 *>(&element[0]);
float_value = (ToType) * value;
}
else
{
Int64 * value = reinterpret_cast<Int64 *>(&element[0]);
float_value = (ToType) * value;
}
out.push_back(float_value);
strcpy(element, "");
}
a = decoder_[(int)in[i + 2] - 43] - 62;
if (i + 2 >= src_size)
a = 0;
element[offset] = (unsigned char) (((b & 15) << 4) | (a >> 2));
written++;
offset = (offset + inc) % element_size;
if (written % element_size == 0)
{
ToType float_value;
if (element_size == 4)
{
Int32 * value = reinterpret_cast<Int32 *>(&element[0]);
float_value = (ToType) * value;
}
else
{
Int64 * value = reinterpret_cast<Int64 *>(&element[0]);
float_value = (ToType) * value;
}
out.push_back(float_value);
strcpy(element, "");
}
b = decoder_[(int)in[i + 3] - 43] - 62;
if (i + 3 >= src_size)
b = 0;
element[offset] = (unsigned char) (((a & 3) << 6) | b);
written++;
offset = (offset + inc) % element_size;
if (written % element_size == 0)
{
ToType float_value;
if (element_size == 4)
{
Int32 * value = reinterpret_cast<Int32 *>(&element[0]);
float_value = (ToType) * value;
}
else
{
Int64 * value = reinterpret_cast<Int64 *>(&element[0]);
float_value = (ToType) * value;
}
out.push_back(float_value);
strcpy(element, "");
}
}
}
} //namespace OpenMS
#endif /* OPENMS_FORMAT_BASE64_H */
|