2021-06-27 21:25:29 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
File: AuBase32.cpp
|
2021-06-27 21:25:29 +00:00
|
|
|
Date: 2021-6-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2022-11-17 07:46:07 +00:00
|
|
|
#include "AuBase32.hpp"
|
2021-06-27 21:25:29 +00:00
|
|
|
#include <tomcrypt.h>
|
|
|
|
|
|
|
|
namespace Aurora::Parse
|
|
|
|
{
|
2024-04-19 03:33:57 +00:00
|
|
|
AUKN_SYM bool Base32Decode(const AuROString &in, AuByteBuffer &decoded)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-09-19 20:34:54 +00:00
|
|
|
int iRet;
|
2022-11-17 07:46:07 +00:00
|
|
|
unsigned long uLength = in.size();
|
2022-09-19 06:08:32 +00:00
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
auto writeView = decoded.GetOrAllocateLinearWriteable(uLength);
|
2022-09-19 06:08:32 +00:00
|
|
|
if (!writeView)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-03-30 14:18:56 +00:00
|
|
|
SysPushErrorMem();
|
2022-09-19 20:34:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-04-19 03:33:57 +00:00
|
|
|
iRet = ::base32_decode(AuReinterpretCast<const char *>(in.data()),
|
2022-11-17 07:46:07 +00:00
|
|
|
(unsigned long)uLength,
|
2024-04-19 03:33:57 +00:00
|
|
|
AuReinterpretCast<unsigned char *>(decoded.writePtr),
|
2022-11-17 07:46:07 +00:00
|
|
|
&uLength,
|
2022-10-04 12:25:56 +00:00
|
|
|
BASE32_RFC4648);
|
2022-09-19 20:34:54 +00:00
|
|
|
if (iRet != CRYPT_OK)
|
|
|
|
{
|
|
|
|
return false;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2022-03-30 14:18:56 +00:00
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
decoded.writePtr += uLength;
|
2022-09-19 20:34:54 +00:00
|
|
|
return true;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 07:58:20 +00:00
|
|
|
AUKN_SYM bool Base32Encode(const Memory::MemoryViewRead &input, AuString &encoded)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-11-17 07:46:07 +00:00
|
|
|
unsigned long uOutLength = ((input.length * 8 + 4) / 5) + 5;
|
2022-03-30 14:18:56 +00:00
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
if (!AuTryResize(encoded, uOutLength))
|
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-29 07:58:20 +00:00
|
|
|
auto iRet = ::base32_encode(AuReinterpretCast<const unsigned char *>(input.ptr),
|
|
|
|
(unsigned long)input.length,
|
2022-09-19 20:34:54 +00:00
|
|
|
&encoded[0],
|
2022-11-17 07:46:07 +00:00
|
|
|
&uOutLength,
|
2022-09-19 20:34:54 +00:00
|
|
|
BASE32_RFC4648);
|
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
if (!AuTryResize(encoded, uOutLength))
|
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 20:34:54 +00:00
|
|
|
return iRet == CRYPT_OK;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|