2021-06-27 21:25:29 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: Base32.cpp
|
|
|
|
Date: 2021-6-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2021-06-27 21:25:29 +00:00
|
|
|
#include "Base32.hpp"
|
|
|
|
#include <tomcrypt.h>
|
|
|
|
|
|
|
|
namespace Aurora::Parse
|
|
|
|
{
|
2022-01-20 16:37:22 +00:00
|
|
|
AUKN_SYM bool Base32Decode(const AuString &in, AuByteBuffer &decoded)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
unsigned long length = in.size();
|
2022-09-19 06:08:32 +00:00
|
|
|
|
|
|
|
auto writeView = decoded.GetOrAllocateLinearWriteable(length);
|
|
|
|
if (!writeView)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-03-30 14:18:56 +00:00
|
|
|
SysPushErrorMem();
|
2022-09-19 06:08:32 +00:00
|
|
|
return {};
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2022-03-30 14:18:56 +00:00
|
|
|
|
2022-09-19 06:08:32 +00:00
|
|
|
auto status = ::base32_decode(AuReinterpretCast<const char *>(decoded.writePtr),
|
|
|
|
(unsigned long)length,
|
|
|
|
AuReinterpretCast<unsigned char *>(&decoded[0]),
|
|
|
|
&length, BASE32_RFC4648);
|
|
|
|
decoded.writePtr += length;
|
2022-03-30 14:18:56 +00:00
|
|
|
return status == CRYPT_OK;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM bool Base32Encode(const void *buffer, AuMach length, AuString &encoded)
|
|
|
|
{
|
2022-06-03 19:28:08 +00:00
|
|
|
unsigned long outLength = ((length * 8 + 4) / 5) + 5;
|
2022-03-30 14:18:56 +00:00
|
|
|
|
2022-06-03 19:28:08 +00:00
|
|
|
if (!AuTryResize(encoded, outLength))
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-03-30 14:18:56 +00:00
|
|
|
SysPushErrorMem();
|
2022-04-10 15:40:49 +00:00
|
|
|
return false;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2022-03-30 14:18:56 +00:00
|
|
|
|
2022-09-19 06:08:32 +00:00
|
|
|
auto status = ::base32_encode(AuReinterpretCast<const unsigned char *>(buffer),
|
|
|
|
(unsigned long)length,
|
|
|
|
&encoded[0],
|
|
|
|
&outLength,
|
|
|
|
BASE32_RFC4648);
|
2022-03-30 14:18:56 +00:00
|
|
|
if (!AuTryResize(encoded, length))
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-03-30 14:18:56 +00:00
|
|
|
SysPushErrorMem();
|
2022-04-10 15:40:49 +00:00
|
|
|
return false;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2022-03-30 14:18:56 +00:00
|
|
|
|
|
|
|
return status == CRYPT_OK;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|