AuroraRuntime/Source/Parse/AuBase64.cpp
Jamie Reece Wilson 83f34b0c47 [*] I was right. String views are [mostly] pointless (*)
03:28:55:638  17>2 of 53388 functions (<0.1%) were compiled, the rest were copied from previous compilation.
03:28:55:638  17>  0 functions were new in current compilation
03:28:55:638  17>  65 functions had inline decision re-evaluated but remain unchanged
03:28:56:749  17>Finished generating code

the header of const AuString & is the same as std::string_view therefore nothing changes. in fact, we still need to alloc strings a bunch of times for a zero terminated string. worse, <c++20 always allocs each time we want to access a hashmap with o(1) lookup, making small hashmaps kinda pointless when we always have to alloc+copy (thx std)

perhaps this will help some language binders
2024-04-19 05:58:08 +01:00

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 AuROString &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 *>(in.data()),
(unsigned long)uLength,
AuReinterpretCast<unsigned char *>(decoded.writePtr),
&uLength);
}
else
{
iRet = ::base64_decode(AuReinterpretCast<const char *>(in.data()),
(unsigned long)uLength,
AuReinterpretCast<unsigned char *>(decoded.writePtr),
&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;
}
}