Code cleanup and some new unittests for edgecases.
parent
46e571e497
commit
14186bcd6b
File diff suppressed because it is too large
Load Diff
|
@ -46,7 +46,7 @@ namespace Assimp {
|
||||||
|
|
||||||
namespace Base64 {
|
namespace Base64 {
|
||||||
|
|
||||||
static const uint8_t tableDecodeBase64[128] = {
|
static constexpr 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, 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,
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63,
|
||||||
|
@ -57,7 +57,7 @@ static const uint8_t tableDecodeBase64[128] = {
|
||||||
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0
|
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0
|
||||||
};
|
};
|
||||||
|
|
||||||
static const char *tableEncodeBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
static constexpr char *tableEncodeBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
||||||
|
|
||||||
static inline char EncodeChar(uint8_t b) {
|
static inline char EncodeChar(uint8_t b) {
|
||||||
return tableEncodeBase64[size_t(b)];
|
return tableEncodeBase64[size_t(b)];
|
||||||
|
@ -71,6 +71,11 @@ inline uint8_t DecodeChar(char c) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Encode(const uint8_t *in, size_t inLength, std::string &out) {
|
void Encode(const uint8_t *in, size_t inLength, std::string &out) {
|
||||||
|
if (in == nullptr || inLength==0) {
|
||||||
|
out.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
size_t outLength = ((inLength + 2) / 3) * 4;
|
size_t outLength = ((inLength + 2) / 3) * 4;
|
||||||
|
|
||||||
size_t j = out.size();
|
size_t j = out.size();
|
||||||
|
@ -115,8 +120,14 @@ std::string Encode(const std::vector<uint8_t> &in) {
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Decode(const char *in, size_t inLength, uint8_t *&out) {
|
size_t Decode(const char *in, size_t inLength, uint8_t *&out) {
|
||||||
|
if (in == nullptr) {
|
||||||
|
out = nullptr;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (inLength % 4 != 0) {
|
if (inLength % 4 != 0) {
|
||||||
throw DeadlyImportError("Invalid base64 encoded data: \"", std::string(in, std::min(size_t(32), inLength)), "\", length:", inLength);
|
throw DeadlyImportError("Invalid base64 encoded data: \"", std::string(in, std::min(size_t(32), inLength)),
|
||||||
|
"\", length:", inLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inLength < 4) {
|
if (inLength < 4) {
|
||||||
|
|
|
@ -50,16 +50,38 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
namespace Assimp {
|
namespace Assimp {
|
||||||
namespace Base64 {
|
namespace Base64 {
|
||||||
|
|
||||||
/// @brief Will encode the given
|
/// @brief Will encode the given character buffer from UTF64 to ASCII
|
||||||
/// @param in
|
/// @param in The UTF-64 buffer.
|
||||||
/// @param inLength
|
/// @param inLength The size of the buffer
|
||||||
/// @param out
|
/// @param out The encoded ASCII string.
|
||||||
void Encode(const uint8_t *in, size_t inLength, std::string &out);
|
void Encode(const uint8_t *in, size_t inLength, std::string &out);
|
||||||
|
|
||||||
|
/// @brief Will encode the given character buffer from UTF64 to ASCII.
|
||||||
|
/// @param in A vector, which contains the buffer for encoding.
|
||||||
|
/// @param out The encoded ASCII string.
|
||||||
void Encode(const std::vector<uint8_t>& in, std::string &out);
|
void Encode(const std::vector<uint8_t>& in, std::string &out);
|
||||||
|
|
||||||
|
/// @brief Will encode the given character buffer from UTF64 to ASCII.
|
||||||
|
/// @param in A vector, which contains the buffer for encoding.
|
||||||
|
/// @return The encoded ASCII string.
|
||||||
std::string Encode(const std::vector<uint8_t>& in);
|
std::string Encode(const std::vector<uint8_t>& in);
|
||||||
|
|
||||||
|
/// @brief Will decode the given character buffer from ASCII to UTF64.
|
||||||
|
/// @param in The ASCII buffer to decode.
|
||||||
|
/// @param inLength The size of the buffer.
|
||||||
|
/// @param out The decoded buffer.
|
||||||
|
/// @return The new buffer size.
|
||||||
size_t Decode(const char *in, size_t inLength, uint8_t *&out);
|
size_t Decode(const char *in, size_t inLength, uint8_t *&out);
|
||||||
|
|
||||||
|
/// @brief Will decode the given character buffer from ASCII to UTF64.
|
||||||
|
/// @param in The ASCII buffer to decode as a std::string.
|
||||||
|
/// @param out The decoded buffer.
|
||||||
|
/// @return The new buffer size.
|
||||||
size_t Decode(const std::string& in, std::vector<uint8_t>& out);
|
size_t Decode(const std::string& in, std::vector<uint8_t>& out);
|
||||||
|
|
||||||
|
/// @brief Will decode the given character buffer from ASCII to UTF64.
|
||||||
|
/// @param in The ASCII string.
|
||||||
|
/// @return The decoded buffer in a vector.
|
||||||
std::vector<uint8_t> Decode(const std::string& in);
|
std::vector<uint8_t> Decode(const std::string& in);
|
||||||
|
|
||||||
} // namespace Base64
|
} // namespace Base64
|
||||||
|
|
|
@ -47,26 +47,35 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace Assimp;
|
using namespace Assimp;
|
||||||
|
|
||||||
class Base64Test : public ::testing::Test {
|
class Base64Test : public ::testing::Test {};
|
||||||
public:
|
|
||||||
virtual void SetUp() {
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void TearDown() {
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
static const std::vector<uint8_t> assimpStringBinary = { 97, 115, 115, 105, 109, 112 };
|
static const std::vector<uint8_t> assimpStringBinary = { 97, 115, 115, 105, 109, 112 };
|
||||||
static const std::string assimpStringEncoded = "YXNzaW1w";
|
static const std::string assimpStringEncoded = "YXNzaW1w";
|
||||||
|
|
||||||
TEST_F( Base64Test, encodeTest ) {
|
TEST_F( Base64Test, encodeTest) {
|
||||||
EXPECT_EQ( "", Base64::Encode (std::vector<uint8_t>{}) );
|
EXPECT_EQ( "", Base64::Encode(std::vector<uint8_t>{}) );
|
||||||
EXPECT_EQ( "Vg==", Base64::Encode (std::vector<uint8_t>{ 86 }) );
|
EXPECT_EQ( "Vg==", Base64::Encode(std::vector<uint8_t>{ 86 }) );
|
||||||
EXPECT_EQ( assimpStringEncoded, Base64::Encode (assimpStringBinary) );
|
EXPECT_EQ( assimpStringEncoded, Base64::Encode(assimpStringBinary) );
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F( Base64Test, decodeTest ) {
|
TEST_F( Base64Test, encodeTestWithNullptr ) {
|
||||||
EXPECT_EQ( std::vector<uint8_t> {}, Base64::Decode ("") );
|
std::string out;
|
||||||
EXPECT_EQ( std::vector<uint8_t> { 86 }, Base64::Decode ("Vg==") );
|
Base64::Encode(nullptr, 100u, out);
|
||||||
EXPECT_EQ( assimpStringBinary, Base64::Decode (assimpStringEncoded) );
|
EXPECT_TRUE(out.empty());
|
||||||
|
|
||||||
|
Base64::Encode(&assimpStringBinary[0], 0u, out);
|
||||||
|
EXPECT_TRUE(out.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F( Base64Test, decodeTest) {
|
||||||
|
EXPECT_EQ( std::vector<uint8_t> {}, Base64::Decode("") );
|
||||||
|
EXPECT_EQ( std::vector<uint8_t> { 86 }, Base64::Decode("Vg==") );
|
||||||
|
EXPECT_EQ( assimpStringBinary, Base64::Decode(assimpStringEncoded) );
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(Base64Test, decodeTestWithNullptr) {
|
||||||
|
uint8_t *out = nullptr;
|
||||||
|
size_t size = Base64::Decode(nullptr, 100u, out);
|
||||||
|
EXPECT_EQ(nullptr, out);
|
||||||
|
EXPECT_EQ(0u, size);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue