[*] Added/fix UTF-16 BE count

[*] Optimize stage builds - Crypto API validation
[*] Clean up
This commit is contained in:
Reece Wilson 2022-03-04 22:28:25 +00:00
parent 3a40a94f3b
commit 53e33b6fdd
8 changed files with 105 additions and 79 deletions

View File

@ -33,11 +33,12 @@ namespace Aurora::Locale::Encoding
AUKN_SYM void SwapUTF16(const Memory::MemoryViewWrite &utf16);
// Counst the amount of codepoints in a buffer, breaking when the stream is incomplete, giving you the accurate amount of bytes or relevant codepoints in a stream view
AUKN_SYM AuUInt32 CountUTF32Length(const Memory::MemoryViewRead &utf32, bool bytes = false); // codepoint = U32 encoded; always 4 bytes per codepoint
AUKN_SYM AuUInt32 CountUTF16Length(const Memory::MemoryViewRead &utf16, bool bytes = false); // codepoint = U32 encoded; at most: 4 bytes per codepoint, usual: 2 bytes
AUKN_SYM AuUInt32 CountUTF8Length (const Memory::MemoryViewRead &utf8, bool bytes = false); // codepoint = U32 encoded; at most: 6 bytes per codepoint
AUKN_SYM AuUInt32 CountSJISLength (const Memory::MemoryViewRead &sjis, bool bytes = false); // codepoint = one character
AUKN_SYM AuUInt32 CountGBK16Length(const Memory::MemoryViewRead &gbk, bool bytes = false); // codepoint = at most; one GBK byte pair
AUKN_SYM AuUInt32 CountUTF32Length (const Memory::MemoryViewRead &utf32, bool bytes = false); // codepoint = U32 encoded; always 4 bytes per codepoint
AUKN_SYM AuUInt32 CountUTF16Length (const Memory::MemoryViewRead &utf16, bool bytes = false); // codepoint = U32 encoded; at most: 4 bytes per codepoint, usual: 2 bytes
AUKN_SYM AuUInt32 CountUTF16BELength(const Memory::MemoryViewRead &utf16, bool bytes = false); // codepoint = U32 encoded; at most: 4 bytes per codepoint, usual: 2 bytes
AUKN_SYM AuUInt32 CountUTF8Length (const Memory::MemoryViewRead &utf8, bool bytes = false); // codepoint = U32 encoded; at most: 6 bytes per codepoint
AUKN_SYM AuUInt32 CountSJISLength (const Memory::MemoryViewRead &sjis, bool bytes = false); // codepoint = one character
AUKN_SYM AuUInt32 CountGBK16Length (const Memory::MemoryViewRead &gbk, bool bytes = false); // codepoint = at most; one GBK byte pair
AUKN_SYM AuUInt32 CountEncodedStringLength(ECodePage page, const Memory::MemoryViewRead &view, bool bytes = false);

View File

@ -141,42 +141,29 @@ namespace Aurora::Crypto::AES
AUKN_SYM bool Encrypt(const AuMemoryViewRead &plainText,
const AuMemoryViewRead &inIv,
const AuMemoryViewWrite & outIv,
const AuMemoryViewWrite &outIv,
const AuMemoryViewRead &inKey,
AuByteBuffer &out,
bool auCoolCodePadding)
{
symmetric_CBC cbc;
if (!plainText.length)
#if !defined(SHIP)
if (!plainText)
{
return false;
}
if (!plainText.ptr)
{
return false;
}
if (!inIv.length)
{
return false;
}
if (!inIv.ptr)
if (!inIv)
{
return false;
}
if (!inKey.length)
{
return false;
}
if (!inKey.ptr)
if (!inKey)
{
return false;
}
#endif
if (inIv.length != 16)
{
@ -264,36 +251,23 @@ namespace Aurora::Crypto::AES
symmetric_CBC cbc;
if (!cipherText.length)
{
return false;
}
if (!cipherText.ptr)
{
return false;
}
if (!inIv.length)
{
return false;
}
if (!inIv.ptr)
#if !defined(SHIP)
if (!plainText)
{
return false;
}
if (!inKey.length)
if (!inIv)
{
return false;
}
if (!inKey.ptr)
if (!inKey)
{
return false;
}
#endif
if (inIv.length != 16)
{

View File

@ -9,6 +9,13 @@
#include "SocketStatChannel.hpp"
#if defined(AURORA_IS_POSIX_DERIVED)
#include <arpa/inet.h>
#elif defined(AURORA_IS_MODERNNT_DERIVED)
#include <WS2tcpip.h>
#endif
namespace Aurora::IO::Net
{

View File

@ -11,30 +11,37 @@
namespace Aurora::Locale::Encoding
{
#define ADD_PATTERN(str, code) {str, {ECodePage::code, AuArraySize(str) - 1}}
static const AuList<AuTuple<const char *, BOM>> gKnownBoms =
{
ADD_PATTERN("\xFF\xFE\x00\x00", eUTF32),
ADD_PATTERN("\x00\x00\xFE\xFF", eUTF32BE),
ADD_PATTERN("\x84\x31\x95\x33", e18030),
ADD_PATTERN("\xDD\x73\x66\x73", eEnumInvalid), // UTF-EBCDIC
ADD_PATTERN("\xEF\xBB\xBF", eUTF8),
ADD_PATTERN("\xF7\x64\x4C", eEnumInvalid), // UTF-1
ADD_PATTERN("\xFB\xEE\x28", eUTF7), // UTF-7
ADD_PATTERN("\x2B\x2F\x76", eEnumInvalid), // BOCU-1
ADD_PATTERN("\x0E\xFE\xFF", eEnumInvalid), // SCSU
ADD_PATTERN("\xFF\xFE", eUTF16), // UTF-16
ADD_PATTERN("\xFE\xFF", eUTF16BE) // UTF-16
};
#undef ADD_PATTERN
AUKN_SYM BOM DecodeBOM(const AuMemoryViewRead &binary)
{
#define ADD_PATTERN(str, code) {str, {ECodePage::code, AuArraySize(str) - 1}}
AuList<AuTuple<const char *, BOM>> boms =
for (const auto &[string, bom] : gKnownBoms)
{
ADD_PATTERN("\xFF\xFE\x00\x00", eUTF32),
ADD_PATTERN("\x00\x00\xFE\xFF", eUTF32BE),
ADD_PATTERN("\x84\x31\x95\x33", e18030),
ADD_PATTERN("\xDD\x73\x66\x73", eEnumInvalid), // UTF-EBCDIC
ADD_PATTERN("\xEF\xBB\xBF", eUTF8),
ADD_PATTERN("\xF7\x64\x4C", eEnumInvalid), // UTF-1
ADD_PATTERN("\xFB\xEE\x28", eUTF7), // UTF-7
ADD_PATTERN("\x2B\x2F\x76", eEnumInvalid), // BOCU-1
ADD_PATTERN("\x0E\xFE\xFF", eEnumInvalid), // SCSU
ADD_PATTERN("\xFF\xFE", eUTF16), // UTF-16
ADD_PATTERN("\xFE\xFF", eUTF16BE) // UTF-16
};
#undef ADD_PATTERN
if (binary.length < bom.length)
{
continue;
}
if (AuMemcmp(binary.ptr, string, bom.length) != 0)
{
continue;
}
for (const auto &[string, bom] : boms)
{
if (binary.length < bom.length) continue;
if (AuMemcmp(binary.ptr, string, bom.length) != 0) continue;
return bom;
}

View File

@ -32,9 +32,9 @@ namespace Aurora::Locale::Encoding
bool EncoderAdapter::TestPage(ECodePage ref)
{
return (((page == ECodePage::eSysUnk) &&
(GetInternalCodePage() == ref)) ||
(page == ref));
return ((page == ref) ||
((page == ECodePage::eSysUnk) &&
(GetInternalCodePage() == ref)));
}
AuStreamReadWrittenPair_t EncoderAdapter::CPToUTF8(const void *in, AuUInt32 length, void *utf8, AuUInt32 utf8Max)
@ -77,7 +77,7 @@ namespace Aurora::Locale::Encoding
}
else if (TestPage(ECodePage::eUTF16) || TestPage(ECodePage::eUTF16BE))
{
length = UTF16::Count16(in, length, true);
length = UTF16::Count16(in, length, true, TestPage(ECodePage::eUTF16));
}
else if ((page == ECodePage::eUTF32) || (page == ECodePage::eUTF32BE))
{

View File

@ -72,12 +72,17 @@ namespace Aurora::Locale::Encoding
{
return UTF32::Count32(utf32.ptr, utf32.length, bytes);
}
AUKN_SYM AuUInt32 CountUTF16Length(const AuMemoryViewRead &utf16, bool bytes)
{
return UTF16::Count16(utf16.ptr, utf16.length, bytes);
return UTF16::Count16(utf16.ptr, utf16.length, bytes, true);
}
AUKN_SYM AuUInt32 CountUTF16BELength(const AuMemoryViewRead &utf16, bool bytes)
{
return UTF16::Count16(utf16.ptr, utf16.length, bytes, false);
}
AUKN_SYM AuUInt32 CountUTF8Length(const AuMemoryViewRead &utf8, bool bytes)
{
auto pair = ReadUTF8IntoUTF32ByteString(utf8, {});
@ -109,6 +114,8 @@ namespace Aurora::Locale::Encoding
return CountUTF32Length(view, bytes);
case ECodePage::eUTF16:
return CountUTF16Length(view, bytes);
case ECodePage::eUTF16BE:
return CountUTF16BELength(view, bytes);
default:
return {};
}

View File

@ -34,7 +34,7 @@ namespace Aurora::Locale::Encoding::UTF16
return (i & AuUInt16(0xfffffc00)) == kLowSurrogateStart;
}
static int GetLenUC2CodePoint(const AuUInt8 *in, AuUInt32 len)
static int GetLenUC2CodePointLE(const AuUInt8 *in, AuUInt32 len)
{
// Check for at least one U16 word
if (len < 2)
@ -57,6 +57,29 @@ namespace Aurora::Locale::Encoding::UTF16
return IsLowSurrogate(AuReadU16LE(in, 2)) ? 4 : 0;
}
static int GetLenUC2CodePointBE(const AuUInt8 *in, AuUInt32 len)
{
// Check for at least one U16 word
if (len < 2)
{
return 0;
}
// Neeto, we found a codepoint in range
if (!IsHighSurrogate(AuReadU16BE(in, 0)))
{
return 2;
}
// Check we have enough stream overhead to consume 4 bytes
if (len < 4)
{
return 0;
}
return IsLowSurrogate(AuReadU16BE(in, 2)) ? 4 : 0;
}
static int GetLenUC2CodePoint(AuUInt32 &codepoint, const AuUInt8 *in, AuUInt32 len)
{
// Check for at least one U16 word
@ -212,13 +235,15 @@ namespace Aurora::Locale::Encoding::UTF16
return {pair2.first, cpOffset};
}
static int Count16(const void *base, AuUInt32 length, bool bytes = false)
static int Count16(const void *base, AuUInt32 length, bool bytes = false, bool le = true)
{
AuUInt32 i {}, cps {};
for (; i < length; )
{
auto next = GetLenUC2CodePoint(((const AuUInt8 *)base) + i, length - i);
auto next = le ?
GetLenUC2CodePointLE(((const AuUInt8 *)base) + i, length - i) :
GetLenUC2CodePointBE(((const AuUInt8 *)base) + i, length - i);
if (next == 0)
{
return bytes ? i : cps;

View File

@ -22,7 +22,7 @@ namespace Aurora::Processes
{
ProcessImpl::ProcessImpl(const StartupParmaters &params) : startup_(params)
{
startup_.args.insert(startup_.args.begin(), startup_.process);
this->startup_.args.insert(startup_.args.begin(), startup_.process);
// ehhhh https://github.com/tritao/WindowsSDK/blob/07983c7ba4f6861d15e23f195744c60c0c249ce0/SDKs/SourceDir/Windows%20Kits/10/Source/10.0.17763.0/ucrt/exec/cenvarg.cpp#L23
for (const auto &arg : this->startup_.args)
@ -78,14 +78,19 @@ namespace Aurora::Processes
bool ProcessImpl::TryKill()
{
return TermWinEnumProcesses();
return HasExited() || TermWinEnumProcesses();
}
bool ProcessImpl::HasExited()
{
DWORD a;
if (!GetExitCodeProcess(this->process_, &a)) return true;
return a != STILL_ACTIVE;
DWORD exitCode;
if (!GetExitCodeProcess(this->process_, &exitCode))
{
return true;
}
return exitCode != STILL_ACTIVE;
}
bool ProcessImpl::Terminate()