AuroraRuntime/Source/Parse/Base64.cpp
2021-06-27 22:25:29 +01:00

69 lines
1.9 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Base64.cpp
Date: 2021-6-12
Author: Reece
***/
#include <RuntimeInternal.hpp>
#include "Base64.hpp"
#include <tomcrypt.h>
namespace Aurora::Parse
{
AUKN_SYM bool Base64Decode(const AuString &in, AuList<AuUInt8> &decoded, bool url)
{
unsigned long length = in.size();
try
{
decoded.resize(in.size());
int status;
if (url)
{
status = base64url_decode(in.data(), in.size(), reinterpret_cast<unsigned char *>(&decoded[0]), &length);
}
else
{
status = base64_decode(in.data(), in.size(), reinterpret_cast<unsigned char *>(&decoded[0]), &length);
}
decoded.resize(length);
return status == CRYPT_OK;
}
catch (...)
{
Console::Logging::LogWarn("Decoding error: {}", in);
Debug::PrintError();
return false;
}
}
AUKN_SYM bool Base64Encode(const void *buffer, AuMach length, AuString &encoded, bool url)
{
unsigned long outLength = length + (length / 3.0) + 16;
try
{
encoded.resize(outLength);
int status;
if (url)
{
status = base64url_encode(reinterpret_cast<const unsigned char*>(buffer), length, &encoded[0], &outLength);
}
else
{
status = base64_encode(reinterpret_cast<const unsigned char*>(buffer), length, &encoded[0], &outLength);
}
encoded.resize(outLength);
return status == CRYPT_OK;
}
catch (...)
{
Console::Logging::LogWarn("Encoding error");
Debug::PrintError();
return false;
}
}
}