AuroraRuntime/Source/Parse/Base64.cpp

71 lines
1.8 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 <Source/RuntimeInternal.hpp>
#include "Base64.hpp"
#include <tomcrypt.h>
namespace Aurora::Parse
{
AUKN_SYM bool Base64Decode(const AuString &in, AuByteBuffer &decoded, bool url)
{
unsigned long length = (unsigned long)in.size();
if (!AuTryResize(decoded, length))
{
SysPushErrorMem();
return false;
}
int status;
if (url)
{
status = base64url_decode(in.data(), (unsigned long)in.size(), reinterpret_cast<unsigned char *>(&decoded[0]), &length);
}
else
{
status = base64_decode(in.data(), (unsigned long)in.size(), reinterpret_cast<unsigned char *>(&decoded[0]), &length);
}
if (!AuTryResize(decoded, length))
{
SysPushErrorMem();
return false;
}
return status == CRYPT_OK;
}
AUKN_SYM bool Base64Encode(const Memory::MemoryViewRead &input, AuString &encoded, bool url)
{
unsigned long outLength = input.length + (input.length / 3.0) + 16;
if (!AuTryResize(encoded, outLength))
{
SysPushErrorMem();
return false;
}
int status;
if (url)
{
status = base64url_encode(reinterpret_cast<const unsigned char*>(input.ptr), (unsigned long)input.length, &encoded[0], &outLength);
}
else
{
status = base64_encode(reinterpret_cast<const unsigned char*>(input.ptr), (unsigned long)input.length, &encoded[0], &outLength);
}
if (!AuTryResize(encoded, outLength))
{
SysPushErrorMem();
return false;
}
return status == CRYPT_OK;
}
}