AuroraRuntime/Source/Parse/AuBase32.cpp

64 lines
1.8 KiB
C++

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