From bbe12240caec793e165a1fc8d37372904269c97f Mon Sep 17 00:00:00 2001 From: kovacsv Date: Sun, 2 Jan 2022 19:47:43 +0100 Subject: [PATCH] Move Base64 encode/decode functionality to the common folder. --- code/AssetLib/glTF/glTFAsset.inl | 5 +- code/AssetLib/glTF/glTFAssetWriter.inl | 4 +- code/AssetLib/glTF/glTFCommon.cpp | 78 ---------- code/AssetLib/glTF/glTFCommon.h | 31 ---- code/AssetLib/glTF2/glTF2Asset.inl | 5 +- code/AssetLib/glTF2/glTF2AssetWriter.inl | 3 +- code/CMakeLists.txt | 2 + code/Common/Base64.cpp | 181 +++++++++++++++++++++++ include/assimp/Base64.hpp | 66 +++++++++ test/CMakeLists.txt | 2 + test/unit/Common/utBase64.cpp | 72 +++++++++ 11 files changed, 334 insertions(+), 115 deletions(-) create mode 100644 code/Common/Base64.cpp create mode 100644 include/assimp/Base64.hpp create mode 100644 test/unit/Common/utBase64.cpp diff --git a/code/AssetLib/glTF/glTFAsset.inl b/code/AssetLib/glTF/glTFAsset.inl index 10be7c78e..352bbe7e8 100644 --- a/code/AssetLib/glTF/glTFAsset.inl +++ b/code/AssetLib/glTF/glTFAsset.inl @@ -45,6 +45,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // Header files, Assimp #include +#include #ifdef ASSIMP_IMPORTER_GLTF_USE_OPEN3DGC // Header files, Open3DGC. @@ -194,7 +195,7 @@ inline void Buffer::Read(Value &obj, Asset &r) { if (ParseDataURI(uri, it->GetStringLength(), dataURI)) { if (dataURI.base64) { uint8_t *data = 0; - this->byteLength = Util::DecodeBase64(dataURI.data, dataURI.dataLength, data); + this->byteLength = Base64::Decode(dataURI.data, dataURI.dataLength, data); this->mData.reset(data, std::default_delete()); if (statedLength > 0 && this->byteLength != statedLength) { @@ -513,7 +514,7 @@ inline void Image::Read(Value &obj, Asset &r) { mimeType = dataURI.mediaType; if (dataURI.base64) { uint8_t *ptr = nullptr; - mDataLength = glTFCommon::Util::DecodeBase64(dataURI.data, dataURI.dataLength, ptr); + mDataLength = Base64::Decode(dataURI.data, dataURI.dataLength, ptr); mData.reset(ptr); } } else { diff --git a/code/AssetLib/glTF/glTFAssetWriter.inl b/code/AssetLib/glTF/glTFAssetWriter.inl index a8efdd35a..5e1e2a8fe 100644 --- a/code/AssetLib/glTF/glTFAssetWriter.inl +++ b/code/AssetLib/glTF/glTFAssetWriter.inl @@ -39,6 +39,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ---------------------------------------------------------------------- */ +#include + #include #include #include @@ -237,7 +239,7 @@ namespace glTF { else if (img.HasData()) { uri = "data:" + (img.mimeType.empty() ? "application/octet-stream" : img.mimeType); uri += ";base64,"; - glTFCommon::Util::EncodeBase64(img.GetData(), img.GetDataLength(), uri); + Base64::Encode(img.GetData(), img.GetDataLength(), uri); } else { uri = img.uri; diff --git a/code/AssetLib/glTF/glTFCommon.cpp b/code/AssetLib/glTF/glTFCommon.cpp index 6c63c01d9..620471297 100644 --- a/code/AssetLib/glTF/glTFCommon.cpp +++ b/code/AssetLib/glTF/glTFCommon.cpp @@ -48,84 +48,6 @@ using namespace glTFCommon::Util; namespace Util { -size_t DecodeBase64(const char *in, size_t inLength, uint8_t *&out) { - if (inLength % 4 != 0) { - throw DeadlyImportError("Invalid base64 encoded data: \"", std::string(in, std::min(size_t(32), inLength)), "\", length:", inLength); - } - - if (inLength < 4) { - out = nullptr; - return 0; - } - - int nEquals = int(in[inLength - 1] == '=') + - int(in[inLength - 2] == '='); - - size_t outLength = (inLength * 3) / 4 - nEquals; - out = new uint8_t[outLength]; - memset(out, 0, outLength); - - size_t i, j = 0; - - for (i = 0; i + 4 < inLength; i += 4) { - uint8_t b0 = DecodeCharBase64(in[i]); - uint8_t b1 = DecodeCharBase64(in[i + 1]); - uint8_t b2 = DecodeCharBase64(in[i + 2]); - uint8_t b3 = DecodeCharBase64(in[i + 3]); - - out[j++] = (uint8_t)((b0 << 2) | (b1 >> 4)); - out[j++] = (uint8_t)((b1 << 4) | (b2 >> 2)); - out[j++] = (uint8_t)((b2 << 6) | b3); - } - - { - uint8_t b0 = DecodeCharBase64(in[i]); - uint8_t b1 = DecodeCharBase64(in[i + 1]); - uint8_t b2 = DecodeCharBase64(in[i + 2]); - uint8_t b3 = DecodeCharBase64(in[i + 3]); - - out[j++] = (uint8_t)((b0 << 2) | (b1 >> 4)); - if (b2 < 64) out[j++] = (uint8_t)((b1 << 4) | (b2 >> 2)); - if (b3 < 64) out[j++] = (uint8_t)((b2 << 6) | b3); - } - - return outLength; -} - -void EncodeBase64(const uint8_t *in, size_t inLength, std::string &out) { - size_t outLength = ((inLength + 2) / 3) * 4; - - size_t j = out.size(); - out.resize(j + outLength); - - for (size_t i = 0; i < inLength; i += 3) { - uint8_t b = (in[i] & 0xFC) >> 2; - out[j++] = EncodeCharBase64(b); - - b = (in[i] & 0x03) << 4; - if (i + 1 < inLength) { - b |= (in[i + 1] & 0xF0) >> 4; - out[j++] = EncodeCharBase64(b); - - b = (in[i + 1] & 0x0F) << 2; - if (i + 2 < inLength) { - b |= (in[i + 2] & 0xC0) >> 6; - out[j++] = EncodeCharBase64(b); - - b = in[i + 2] & 0x3F; - out[j++] = EncodeCharBase64(b); - } else { - out[j++] = EncodeCharBase64(b); - out[j++] = '='; - } - } else { - out[j++] = EncodeCharBase64(b); - out[j++] = '='; - out[j++] = '='; - } - } -} - bool ParseDataURI(const char *const_uri, size_t uriLen, DataURI &out) { if (nullptr == const_uri) { return false; diff --git a/code/AssetLib/glTF/glTFCommon.h b/code/AssetLib/glTF/glTFCommon.h index 14b82aa29..6762ca6a1 100644 --- a/code/AssetLib/glTF/glTFCommon.h +++ b/code/AssetLib/glTF/glTFCommon.h @@ -232,37 +232,6 @@ struct DataURI { //! Check if a uri is a data URI bool ParseDataURI(const char *const_uri, size_t uriLen, DataURI &out); -template -struct DATA { - static const uint8_t tableDecodeBase64[128]; -}; - -template -const uint8_t DATA::tableDecodeBase64[128] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 64, 0, 0, - 0, 0, 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, 0, 0, 0, 0, 0, - 0, 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, 0, 0, 0, 0, 0 -}; - -inline char EncodeCharBase64(uint8_t b) { - return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="[size_t(b)]; -} - -inline uint8_t DecodeCharBase64(char c) { - if (c & 0x80) { - throw DeadlyImportError("Invalid base64 char value: ", size_t(c)); - } - return DATA::tableDecodeBase64[size_t(c & 0x7F)]; // TODO faster with lookup table or ifs? -} - -size_t DecodeBase64(const char *in, size_t inLength, uint8_t *&out); - -void EncodeBase64(const uint8_t *in, size_t inLength, std::string &out); } // namespace Util #define CHECK_EXT(EXT) \ diff --git a/code/AssetLib/glTF2/glTF2Asset.inl b/code/AssetLib/glTF2/glTF2Asset.inl index f35a47efa..f6c3aa87a 100644 --- a/code/AssetLib/glTF2/glTF2Asset.inl +++ b/code/AssetLib/glTF2/glTF2Asset.inl @@ -44,6 +44,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include // clang-format off #ifdef ASSIMP_ENABLE_DRACO @@ -563,7 +564,7 @@ inline void Buffer::Read(Value &obj, Asset &r) { if (ParseDataURI(uri, it->GetStringLength(), dataURI)) { if (dataURI.base64) { uint8_t *data = nullptr; - this->byteLength = glTFCommon::Util::DecodeBase64(dataURI.data, dataURI.dataLength, data); + this->byteLength = Base64::Decode(dataURI.data, dataURI.dataLength, data); this->mData.reset(data, std::default_delete()); if (statedLength > 0 && this->byteLength != statedLength) { @@ -1062,7 +1063,7 @@ inline void Image::Read(Value &obj, Asset &r) { mimeType = dataURI.mediaType; if (dataURI.base64) { uint8_t *ptr = nullptr; - mDataLength = glTFCommon::Util::DecodeBase64(dataURI.data, dataURI.dataLength, ptr); + mDataLength = Base64::Decode(dataURI.data, dataURI.dataLength, ptr); mData.reset(ptr); } } else { diff --git a/code/AssetLib/glTF2/glTF2AssetWriter.inl b/code/AssetLib/glTF2/glTF2AssetWriter.inl index b91553fbe..8a2b73d03 100644 --- a/code/AssetLib/glTF2/glTF2AssetWriter.inl +++ b/code/AssetLib/glTF2/glTF2AssetWriter.inl @@ -39,6 +39,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ---------------------------------------------------------------------- */ +#include #include #include #include @@ -260,7 +261,7 @@ namespace glTF2 { if (img.HasData()) { uri = "data:" + (img.mimeType.empty() ? "application/octet-stream" : img.mimeType); uri += ";base64,"; - glTFCommon::Util::EncodeBase64(img.GetData(), img.GetDataLength(), uri); + Base64::Encode(img.GetData(), img.GetDataLength(), uri); } else { uri = img.uri; diff --git a/code/CMakeLists.txt b/code/CMakeLists.txt index 1af9407d2..0c462f4ed 100644 --- a/code/CMakeLists.txt +++ b/code/CMakeLists.txt @@ -142,6 +142,7 @@ SET( PUBLIC_HEADERS ${HEADER_PATH}/MathFunctions.h ${HEADER_PATH}/Exceptional.h ${HEADER_PATH}/ByteSwapper.h + ${HEADER_PATH}/Base64.hpp ) SET( Core_SRCS @@ -201,6 +202,7 @@ SET( Common_SRCS Common/material.cpp Common/AssertHandler.cpp Common/Exceptional.cpp + Common/Base64.cpp ) SOURCE_GROUP(Common FILES ${Common_SRCS}) diff --git a/code/Common/Base64.cpp b/code/Common/Base64.cpp new file mode 100644 index 000000000..2916cae7f --- /dev/null +++ b/code/Common/Base64.cpp @@ -0,0 +1,181 @@ +/* +--------------------------------------------------------------------------- +Open Asset Import Library (assimp) +--------------------------------------------------------------------------- + +Copyright (c) 2006-2021, assimp team + +All rights reserved. + +Redistribution and use of this software in source and binary forms, +with or without modification, are permitted provided that the following +conditions are met: + +* 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 the assimp team, nor the names of its + contributors may be used to endorse or promote products + derived from this software without specific prior + written permission of the assimp team. + +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 THE COPYRIGHT +OWNER OR CONTRIBUTORS 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. +--------------------------------------------------------------------------- +*/ + +#include +#include + +namespace Assimp { + +namespace Base64 { + +static const uint8_t tableDecodeBase64[128] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 64, 0, 0, + 0, 0, 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, 0, 0, 0, 0, 0, + 0, 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, 0, 0, 0, 0, 0 +}; + +static const char* tableEncodeBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + +static inline char EncodeChar(uint8_t b) { + return tableEncodeBase64[size_t(b)]; +} + +inline uint8_t DecodeChar(char c) { + if (c & 0x80) { + throw DeadlyImportError("Invalid base64 char value: ", size_t(c)); + } + return tableDecodeBase64[size_t(c & 0x7F)]; // TODO faster with lookup table or ifs? +} + +void Encode(const uint8_t *in, size_t inLength, std::string &out) { + size_t outLength = ((inLength + 2) / 3) * 4; + + size_t j = out.size(); + out.resize(j + outLength); + + for (size_t i = 0; i < inLength; i += 3) { + uint8_t b = (in[i] & 0xFC) >> 2; + out[j++] = EncodeChar(b); + + b = (in[i] & 0x03) << 4; + if (i + 1 < inLength) { + b |= (in[i + 1] & 0xF0) >> 4; + out[j++] = EncodeChar(b); + + b = (in[i + 1] & 0x0F) << 2; + if (i + 2 < inLength) { + b |= (in[i + 2] & 0xC0) >> 6; + out[j++] = EncodeChar(b); + + b = in[i + 2] & 0x3F; + out[j++] = EncodeChar(b); + } else { + out[j++] = EncodeChar(b); + out[j++] = '='; + } + } else { + out[j++] = EncodeChar(b); + out[j++] = '='; + out[j++] = '='; + } + } +} + +void Encode(const std::vector& in, std::string &out) { + Encode (in.data (), in.size (), out); +} + +std::string Encode (const std::vector& in) { + std::string encoded; + Encode (in, encoded); + return encoded; +} + + +size_t Decode(const char *in, size_t inLength, uint8_t *&out) { + if (inLength % 4 != 0) { + throw DeadlyImportError("Invalid base64 encoded data: \"", std::string(in, std::min(size_t(32), inLength)), "\", length:", inLength); + } + + if (inLength < 4) { + out = nullptr; + return 0; + } + + int nEquals = int(in[inLength - 1] == '=') + + int(in[inLength - 2] == '='); + + size_t outLength = (inLength * 3) / 4 - nEquals; + out = new uint8_t[outLength]; + memset(out, 0, outLength); + + size_t i, j = 0; + + for (i = 0; i + 4 < inLength; i += 4) { + uint8_t b0 = DecodeChar(in[i]); + uint8_t b1 = DecodeChar(in[i + 1]); + uint8_t b2 = DecodeChar(in[i + 2]); + uint8_t b3 = DecodeChar(in[i + 3]); + + out[j++] = (uint8_t)((b0 << 2) | (b1 >> 4)); + out[j++] = (uint8_t)((b1 << 4) | (b2 >> 2)); + out[j++] = (uint8_t)((b2 << 6) | b3); + } + + { + uint8_t b0 = DecodeChar(in[i]); + uint8_t b1 = DecodeChar(in[i + 1]); + uint8_t b2 = DecodeChar(in[i + 2]); + uint8_t b3 = DecodeChar(in[i + 3]); + + out[j++] = (uint8_t)((b0 << 2) | (b1 >> 4)); + if (b2 < 64) out[j++] = (uint8_t)((b1 << 4) | (b2 >> 2)); + if (b3 < 64) out[j++] = (uint8_t)((b2 << 6) | b3); + } + + return outLength; +} + +size_t Decode(const std::string& in, std::vector& out) { + uint8_t* outPtr = nullptr; + size_t decodedSize = Decode (in.data (), in.size (), outPtr); + if (outPtr == nullptr) { + return 0; + } + out.assign (outPtr, outPtr + decodedSize); + delete[] outPtr; + return decodedSize; +} + +std::vector Decode (const std::string& in) { + std::vector result; + Decode (in, result); + return result; +} + +} + +} diff --git a/include/assimp/Base64.hpp b/include/assimp/Base64.hpp new file mode 100644 index 000000000..9db71c314 --- /dev/null +++ b/include/assimp/Base64.hpp @@ -0,0 +1,66 @@ +/* +--------------------------------------------------------------------------- +Open Asset Import Library (assimp) +--------------------------------------------------------------------------- + +Copyright (c) 2006-2021, assimp team + +All rights reserved. + +Redistribution and use of this software in source and binary forms, +with or without modification, are permitted provided that the following +conditions are met: + +* 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 the assimp team, nor the names of its + contributors may be used to endorse or promote products + derived from this software without specific prior + written permission of the assimp team. + +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 THE COPYRIGHT +OWNER OR CONTRIBUTORS 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. +--------------------------------------------------------------------------- +*/ + +#pragma once +#ifndef AI_BASE64_HPP_INC +#define AI_BASE64_HPP_INC + +#include +#include +#include + +namespace Assimp { + +namespace Base64 { + +void Encode(const uint8_t *in, size_t inLength, std::string &out); +void Encode(const std::vector& in, std::string &out); +std::string Encode(const std::vector& in); + +size_t Decode(const char *in, size_t inLength, uint8_t *&out); +size_t Decode(const std::string& in, std::vector& out); +std::vector Decode(const std::string& in); + +} + +} + +#endif // AI_BASE64_HPP_INC diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5e573ed7c..fde051275 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -97,6 +97,7 @@ SET( COMMON unit/Common/utSpatialSort.cpp unit/Common/utAssertHandler.cpp unit/Common/utXmlParser.cpp + unit/Common/utBase64.cpp ) SET( IMPORTERS @@ -208,6 +209,7 @@ add_executable( unit unit/CCompilerTest.c unit/Main.cpp ../code/Common/Version.cpp + ../code/Common/Base64.cpp ${COMMON} ${IMPORTERS} ${MATERIAL} diff --git a/test/unit/Common/utBase64.cpp b/test/unit/Common/utBase64.cpp new file mode 100644 index 000000000..6ac960364 --- /dev/null +++ b/test/unit/Common/utBase64.cpp @@ -0,0 +1,72 @@ +/* +--------------------------------------------------------------------------- +Open Asset Import Library (assimp) +--------------------------------------------------------------------------- + +Copyright (c) 2006-2021, assimp team + +All rights reserved. + +Redistribution and use of this software in source and binary forms, +with or without modification, are permitted provided that the following +conditions are met: + +* 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 the assimp team, nor the names of its +contributors may be used to endorse or promote products +derived from this software without specific prior +written permission of the assimp team. + +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 THE COPYRIGHT +OWNER OR CONTRIBUTORS 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. +--------------------------------------------------------------------------- +*/ + +#include "UnitTestPCH.h" +#include "TestIOSystem.h" + +#include + +using namespace std; +using namespace Assimp; + +class Base64Test : public ::testing::Test { +public: + virtual void SetUp() { + } + + virtual void TearDown() { + } +}; + +static const std::vector assimpStringBinary = { 97, 115, 115, 105, 109, 112 }; +static const std::string assimpStringEncoded = "YXNzaW1w"; + +TEST_F( Base64Test, encodeTest ) { + EXPECT_EQ( "", Base64::Encode (std::vector{}) ); + EXPECT_EQ( "Vg==", Base64::Encode (std::vector{ 86 }) ); + EXPECT_EQ( assimpStringEncoded, Base64::Encode (assimpStringBinary) ); +} + +TEST_F( Base64Test, decodeTest ) { + EXPECT_EQ( std::vector {}, Base64::Decode ("") ); + EXPECT_EQ( std::vector { 86 }, Base64::Decode ("Vg==") ); + EXPECT_EQ( assimpStringBinary, Base64::Decode (assimpStringEncoded) ); +}