AuroraRuntime/Source/Parse/AuBase64.cpp

84 lines
2.4 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuBase64.cpp
Date: 2021-6-12
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuBase64.hpp"
#include <tomcrypt.h>
namespace Aurora::Parse
{
AUKN_SYM bool Base64Decode(const AuString &in, AuByteBuffer &decoded, bool bUrl)
{
int iRet;
unsigned long uLength = (unsigned long)in.size();
auto writeView = decoded.GetOrAllocateLinearWriteable(uLength);
if (!writeView)
{
SysPushErrorMem();
return {};
}
if (bUrl)
{
iRet = ::base64url_decode(AuReinterpretCast<const char *>(decoded.writePtr),
(unsigned long)uLength,
AuReinterpretCast<unsigned char *>(&decoded[0]),
&uLength);
}
else
{
iRet = ::base64_decode(AuReinterpretCast<const char *>(decoded.writePtr),
(unsigned long)uLength,
AuReinterpretCast<unsigned char *>(&decoded[0]),
&uLength);
}
if (iRet != CRYPT_OK)
{
return false;
}
decoded.writePtr += uLength;
return true;
}
AUKN_SYM bool Base64Encode(const Memory::MemoryViewRead &input, AuString &encoded, bool bUrl)
{
int iRet;
unsigned long uOutLength = input.length + (input.length / 3.0) + 16;
if (!AuTryResize(encoded, uOutLength))
{
SysPushErrorMem();
return false;
}
if (bUrl)
{
iRet = ::base64url_encode(AuReinterpretCast<const unsigned char*>(input.ptr),
(unsigned long)input.length,
&encoded[0],
&uOutLength);
}
else
{
iRet = ::base64_encode(AuReinterpretCast<const unsigned char*>(input.ptr),
(unsigned long)input.length,
&encoded[0],
&uOutLength);
}
if (!AuTryResize(encoded, uOutLength))
{
SysPushErrorMem();
return false;
}
return iRet == CRYPT_OK;
}
}