2021-09-06 10:58:08 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: EncoderNSL.cpp
|
|
|
|
Date: 2021-8-19
|
|
|
|
Author: Reece
|
|
|
|
***/
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2021-09-06 10:58:08 +00:00
|
|
|
#include "Encoding.hpp"
|
|
|
|
#include "EncoderNSL.hpp"
|
|
|
|
|
|
|
|
namespace Aurora::Locale::Encoding
|
|
|
|
{
|
|
|
|
static AuStreamReadWrittenPair_t Win32ConvertCpAToCPB(AuUInt32 cpA, AuUInt32 cpB, const void *in, AuUInt32 inLength, void *cpBlob, AuUInt32 cpLen)
|
|
|
|
{
|
|
|
|
#if defined(AU_HAS_MSFT_NATIONALLANGSUPPORT)
|
|
|
|
if (!in)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
// Get the UTF-16 character count of the cpA string in/inLength
|
|
|
|
auto chars = MultiByteToWideChar(cpA, 0, (LPCCH)in, inLength, NULL, 0);
|
|
|
|
|
|
|
|
if (!chars)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a temp utf-16/widechar string buffer
|
|
|
|
auto ret = _new wchar_t[chars];
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the cpA buffer to UTF-16
|
|
|
|
MultiByteToWideChar(cpA, 0, (LPCCH)in, inLength, ret, chars);
|
|
|
|
|
|
|
|
// convert the shortened string with invalid surrogates back into a cpA length
|
|
|
|
AuUInt32 cpLength;
|
|
|
|
if (!cpBlob)
|
|
|
|
{
|
2021-09-06 13:57:33 +00:00
|
|
|
cpLength = WideCharToMultiByte(cpB, 0, ret, chars, NULL, NULL, NULL, NULL);
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-06 13:57:33 +00:00
|
|
|
cpLength = WideCharToMultiByte(cpB, 0, ret, chars, (LPSTR)cpBlob, cpLen, NULL, NULL);
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete[] ret;
|
2021-09-06 13:57:33 +00:00
|
|
|
return {inLength, cpLength};
|
2021-09-06 10:58:08 +00:00
|
|
|
#else
|
|
|
|
return {};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
AuStreamReadWrittenPair_t Win32ConvertFromCPToUTF8(AuUInt32 cp, const void *in, AuUInt length, void *utf8, AuUInt32 utf8Len)
|
|
|
|
{
|
2021-10-02 16:07:33 +00:00
|
|
|
#if !defined(AU_HAS_MSFT_NATIONALLANGSUPPORT)
|
|
|
|
return {};
|
|
|
|
#else
|
2021-09-06 10:58:08 +00:00
|
|
|
return Win32ConvertCpAToCPB(cp, CP_UTF8, in, length, utf8, utf8Len);
|
2021-10-02 16:07:33 +00:00
|
|
|
#endif
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuStreamReadWrittenPair_t Win32ConvertFromUTF8ToCp(AuUInt32 cp, const void *utf8, AuUInt utf8Length, void *cpBlob, AuUInt32 cpLen)
|
|
|
|
{
|
2021-10-02 16:07:33 +00:00
|
|
|
#if !defined(AU_HAS_MSFT_NATIONALLANGSUPPORT)
|
|
|
|
return {};
|
|
|
|
#else
|
2021-09-06 10:58:08 +00:00
|
|
|
return Win32ConvertCpAToCPB(CP_UTF8, cp, utf8, utf8Length, cpBlob, cpLen);
|
2021-10-02 16:07:33 +00:00
|
|
|
#endif
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuStreamReadWrittenPair_t Win32ConvertFromUTF16ToUTF8(const void *in, AuUInt32 inLength, void *utf8, AuUInt32 utf8Len)
|
|
|
|
{
|
|
|
|
#if defined(AU_HAS_MSFT_NATIONALLANGSUPPORT)
|
|
|
|
if (!in)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pWideChars = (const wchar_t *)in;
|
2021-09-06 19:04:01 +00:00
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
AuUInt32 cpLength;
|
|
|
|
if (!utf8)
|
|
|
|
{
|
2021-09-06 19:04:01 +00:00
|
|
|
auto cpLength = WideCharToMultiByte(CP_UTF8, 0, (const wchar_t *)in, inLength / sizeof(wchar_t), NULL, NULL, NULL, NULL);
|
|
|
|
return {inLength, cpLength};
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-06 19:04:01 +00:00
|
|
|
cpLength = WideCharToMultiByte(CP_UTF8, 0, (const wchar_t *)in, inLength / sizeof(wchar_t), (LPSTR)utf8, utf8Len, NULL, NULL);
|
|
|
|
return {inLength, cpLength};
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
return {};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
AuStreamReadWrittenPair_t Win32ConvertFromUTF8ToUTF16(const void *in, AuUInt32 inLength, void *utf16, AuUInt32 utf16Len)
|
|
|
|
{
|
|
|
|
#if defined(AU_HAS_MSFT_NATIONALLANGSUPPORT)
|
|
|
|
if (!in)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the UTF-16 character count of the cpA string in/inLength
|
|
|
|
auto chars = MultiByteToWideChar(CP_UTF8, 0, (LPCCH)in, inLength, NULL, 0);
|
|
|
|
|
|
|
|
if (!chars)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chars > utf16Len)
|
|
|
|
{
|
|
|
|
if (!utf16)
|
|
|
|
{
|
|
|
|
return {0, utf16Len};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the CP_UTF8 buffer to UTF-16
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, (LPCCH)in, inLength, (LPWSTR)utf16, chars);
|
|
|
|
|
|
|
|
// convert the shortened string with invalid surrogates back into a CP_UTF8 length
|
|
|
|
AuUInt32 read = inLength;
|
|
|
|
#if 0
|
|
|
|
if (slowPath)
|
|
|
|
#endif
|
|
|
|
{
|
2021-09-06 13:03:45 +00:00
|
|
|
read = WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)utf16, chars, NULL, 0, NULL, NULL);
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 19:04:01 +00:00
|
|
|
return {read, chars * sizeof(wchar_t)};
|
2021-09-06 10:58:08 +00:00
|
|
|
#else
|
|
|
|
return {};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
AuStreamReadWrittenPair_t Win32CPToUTF8(ECodePage page, const void *in, AuUInt length, void *utf8, AuUInt32 utf8Max)
|
|
|
|
{
|
|
|
|
AuStreamReadWrittenPair_t ret {};
|
|
|
|
|
2021-10-02 16:07:33 +00:00
|
|
|
#if defined(AU_HAS_MSFT_NATIONALLANGSUPPORT)
|
2021-09-06 10:58:08 +00:00
|
|
|
switch (page)
|
|
|
|
{
|
|
|
|
default:
|
[*/+/-] MEGA COMMIT. ~2 weeks compressed.
The intention is to quickly improve and add util apis, enhance functionality given current demands, go back to the build pipeline, finish that, publish runtime tests, and then use what we have to go back to to linux support with a more stable api.
[+] AuMakeSharedArray
[+] Technet ArgvQuote
[+] Grug subsystem (UNIX signal thread async safe ipc + telemetry flusher + log flusher.)
[+] auEndianness -> Endian swap utils
[+] AuGet<N>(...)
[*] AUE_DEFINE conversion for
ECompresionType, EAnsiColor, EHashType, EStreamError, EHexDump
[+] ConsoleMessage ByteBuffer serialization
[+] CmdLine subsystem for parsing command line arguments and simple switch/flag checks
[*] Split logger from console subsystem
[+] StartupParameters -> A part of a clean up effort under Process
[*] Refactor SysErrors header + get caller hack
[+] Atomic APIs
[+] popcnt
[+] Ring Buffer sink
[+] Added more standard errors
Catch,
Submission,
LockError,
NoAccess,
ResourceMissing,
ResourceLocked,
MalformedData,
InSandboxContext,
ParseError
[+] Added ErrorCategorySet, ErrorCategoryClear, GetStackTrace
[+] IExitSubscriber, ETriggerLevel
[*] Write bias the high performance RWLockImpl read-lock operation operation
[+] ExitHandlerAdd/ExitHandlerRemove (exit subsystem)
[*] Updated API style
Digests
[+] CpuId::CpuBitCount
[+] GetUserProgramsFolder
[+] GetPackagePath
[*] Split IStreamReader with an inl file
[*] BlobWriter/BlobReader/BlobArbitraryReader can now take shared pointers to bytebuffers. default constructor allocates a new scalable bytebuffer
[+] ICharacterProvider
[+] ICharacterProviderEx
[+] IBufferedCharacterConsumer
[+] ProviderFromSharedString
[+] ProviderFromString
[+] BufferConsumerFromProvider
[*] Parse Subsystem uses character io bufferer
[*] Rewritten NT's high perf semaphore to use userland SRW/ConVars [like mutex, based on generic semaphore]
[+] ByteBuffer::ResetReadPointer
[*] Bug fix bytebuffer base not reset on free and some scaling issues
[+] ProcessMap -> Added kSectionNameStack, kSectionNameFile, kSectionNameHeap for Section
[*] ProcessMap -> Refactor Segment to Section. I was stupid for keeping a type conflict hack API facing
[+] Added 64 *byte* fast RNG seeds
[+] File Advisorys/File Lock Awareness
[+] Added extended IAuroraThread from OS identifier caches for debug purposes
[*] Tweaked how memory is reported on Windows. Better consistency of what values mean across functions.
[*] Broke AuroraUtils/Typedefs out into a separate library
[*] Update build script
[+] Put some more effort into adding detail to the readme before rewriting it, plus, added some media
[*] Improved public API documentation
[*] Bug fix `SetConsoleCtrlHandler`
[+] Locale TimeDateToFileNameISO8601
[+] Console config stdOutShortTime
[*] Begin using internal UTF8/16 decoders when platform support isnt available (instead of stl)
[*] Bug fixes in decoders
[*] Major bug fix, AuMax
[+] RateLimiter
[+] Binary file sink
[+] Log directory sink
[*] Data header usability (more operators)
[+] AuRemoveRange
[+] AuRemove
[+] AuTryRemove
[+] AuTryRemoveRange
[+] auCastUtils
[+] Finish NewLSWin32Source
[+] AuTryFindByTupleN, AuTryRemoveByTupleN
[+] Separated AuRead/Write types, now in auTypeUtils
[+] Added GetPosition/SetPosition to FileWriter
[*] Fix stupid AuMin in place of AuMax in SpawnThread.Unix.Cpp
[*] Refactored Arbitrary readers to SeekingReaders (as in, they could be atomic and/or parallelized, and accept an arbitrary position as a work parameter -> not Seekable, as in, you can simply set the position)
[*] Hack back in the sched deinit
[+] File AIO loop source interop
[+] Begin to prototype a LoopQueue object I had in mind for NT, untested btw
[+] Stub code for networking
[+] Compression BaseStream/IngestableStreamBase
[*] Major: read/write locks now support write-entrant read routines.
[*] Compression subsystem now uses the MemoryView concept
[*] Rewrite the base stream compressions, made them less broken
[*] Update hashing api
[*] WriterTryGoForward and ReaderTryGoForward now revert to the previous relative index instead of panicing
[+] Added new AuByteBuffer apis
Trim, Pad, WriteFrom, WriteString, [TODO: ReadString]
[+] Added ByteBufferPushReadState
[+] Added ByteBufferPushWriteState
[*] Move from USC-16 to full UTF-16. Win32 can handle full UTF-16.
[*] ELogLevel is now an Aurora enum
[+] Raised arbitrary limit in header to 255, the max filter buffer
[+] Explicit GZip support
[+] Explicit Zip support
[+] Added [some] compressors
et al
2022-02-17 00:11:40 +00:00
|
|
|
case ECodePage::eEnumInvalid:
|
2021-09-06 10:58:08 +00:00
|
|
|
return {};
|
|
|
|
case ECodePage::e18030:
|
|
|
|
ret = Win32ConvertFromUTF8ToCp(CP_CHINESE, in, length, utf8, utf8Max);
|
|
|
|
break;
|
|
|
|
case ECodePage::eSysUnk:
|
|
|
|
ret = Win32ConvertFromCPToUTF8(CP_ACP, in, length, utf8, utf8Max);
|
|
|
|
break;
|
|
|
|
case ECodePage::eLatin1:
|
|
|
|
ret = Win32ConvertFromCPToUTF8(CP_LATIN_1, in, length, utf8, utf8Max);
|
|
|
|
break;
|
|
|
|
case ECodePage::eUTF7:
|
|
|
|
ret = Win32ConvertFromCPToUTF8(CP_UTF7, in, length, utf8, utf8Max);
|
|
|
|
break;
|
|
|
|
case ECodePage::e2312:
|
|
|
|
ret = Win32ConvertFromCPToUTF8(CP_2312_LIMITED_GBK, in, length, utf8, utf8Max);
|
|
|
|
break;
|
|
|
|
case ECodePage::eGBK:
|
|
|
|
ret = Win32ConvertFromCPToUTF8(CP_2312_LIMITED_GBK, in, length, utf8, utf8Max);
|
|
|
|
break;
|
|
|
|
case ECodePage::eSJIS:
|
|
|
|
ret = Win32ConvertFromCPToUTF8(CP_SHIFTJIS, in, length, utf8, utf8Max);
|
|
|
|
break;
|
|
|
|
case ECodePage::eUTF16:
|
|
|
|
ret = Win32ConvertFromUTF16ToUTF8(in, length, utf8, utf8Max);
|
|
|
|
break;
|
|
|
|
}
|
2021-10-02 16:07:33 +00:00
|
|
|
#endif
|
2021-09-06 10:58:08 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuStreamReadWrittenPair_t Win32UTF8ToCp(ECodePage page, const void *utf8, AuUInt32 utf8Length, void *cp, AuUInt32 cpLen)
|
|
|
|
{
|
|
|
|
AuStreamReadWrittenPair_t ret {};
|
2021-10-02 16:07:33 +00:00
|
|
|
|
|
|
|
#if defined(AU_HAS_MSFT_NATIONALLANGSUPPORT)
|
2021-09-06 10:58:08 +00:00
|
|
|
switch (page)
|
|
|
|
{
|
|
|
|
default:
|
[*/+/-] MEGA COMMIT. ~2 weeks compressed.
The intention is to quickly improve and add util apis, enhance functionality given current demands, go back to the build pipeline, finish that, publish runtime tests, and then use what we have to go back to to linux support with a more stable api.
[+] AuMakeSharedArray
[+] Technet ArgvQuote
[+] Grug subsystem (UNIX signal thread async safe ipc + telemetry flusher + log flusher.)
[+] auEndianness -> Endian swap utils
[+] AuGet<N>(...)
[*] AUE_DEFINE conversion for
ECompresionType, EAnsiColor, EHashType, EStreamError, EHexDump
[+] ConsoleMessage ByteBuffer serialization
[+] CmdLine subsystem for parsing command line arguments and simple switch/flag checks
[*] Split logger from console subsystem
[+] StartupParameters -> A part of a clean up effort under Process
[*] Refactor SysErrors header + get caller hack
[+] Atomic APIs
[+] popcnt
[+] Ring Buffer sink
[+] Added more standard errors
Catch,
Submission,
LockError,
NoAccess,
ResourceMissing,
ResourceLocked,
MalformedData,
InSandboxContext,
ParseError
[+] Added ErrorCategorySet, ErrorCategoryClear, GetStackTrace
[+] IExitSubscriber, ETriggerLevel
[*] Write bias the high performance RWLockImpl read-lock operation operation
[+] ExitHandlerAdd/ExitHandlerRemove (exit subsystem)
[*] Updated API style
Digests
[+] CpuId::CpuBitCount
[+] GetUserProgramsFolder
[+] GetPackagePath
[*] Split IStreamReader with an inl file
[*] BlobWriter/BlobReader/BlobArbitraryReader can now take shared pointers to bytebuffers. default constructor allocates a new scalable bytebuffer
[+] ICharacterProvider
[+] ICharacterProviderEx
[+] IBufferedCharacterConsumer
[+] ProviderFromSharedString
[+] ProviderFromString
[+] BufferConsumerFromProvider
[*] Parse Subsystem uses character io bufferer
[*] Rewritten NT's high perf semaphore to use userland SRW/ConVars [like mutex, based on generic semaphore]
[+] ByteBuffer::ResetReadPointer
[*] Bug fix bytebuffer base not reset on free and some scaling issues
[+] ProcessMap -> Added kSectionNameStack, kSectionNameFile, kSectionNameHeap for Section
[*] ProcessMap -> Refactor Segment to Section. I was stupid for keeping a type conflict hack API facing
[+] Added 64 *byte* fast RNG seeds
[+] File Advisorys/File Lock Awareness
[+] Added extended IAuroraThread from OS identifier caches for debug purposes
[*] Tweaked how memory is reported on Windows. Better consistency of what values mean across functions.
[*] Broke AuroraUtils/Typedefs out into a separate library
[*] Update build script
[+] Put some more effort into adding detail to the readme before rewriting it, plus, added some media
[*] Improved public API documentation
[*] Bug fix `SetConsoleCtrlHandler`
[+] Locale TimeDateToFileNameISO8601
[+] Console config stdOutShortTime
[*] Begin using internal UTF8/16 decoders when platform support isnt available (instead of stl)
[*] Bug fixes in decoders
[*] Major bug fix, AuMax
[+] RateLimiter
[+] Binary file sink
[+] Log directory sink
[*] Data header usability (more operators)
[+] AuRemoveRange
[+] AuRemove
[+] AuTryRemove
[+] AuTryRemoveRange
[+] auCastUtils
[+] Finish NewLSWin32Source
[+] AuTryFindByTupleN, AuTryRemoveByTupleN
[+] Separated AuRead/Write types, now in auTypeUtils
[+] Added GetPosition/SetPosition to FileWriter
[*] Fix stupid AuMin in place of AuMax in SpawnThread.Unix.Cpp
[*] Refactored Arbitrary readers to SeekingReaders (as in, they could be atomic and/or parallelized, and accept an arbitrary position as a work parameter -> not Seekable, as in, you can simply set the position)
[*] Hack back in the sched deinit
[+] File AIO loop source interop
[+] Begin to prototype a LoopQueue object I had in mind for NT, untested btw
[+] Stub code for networking
[+] Compression BaseStream/IngestableStreamBase
[*] Major: read/write locks now support write-entrant read routines.
[*] Compression subsystem now uses the MemoryView concept
[*] Rewrite the base stream compressions, made them less broken
[*] Update hashing api
[*] WriterTryGoForward and ReaderTryGoForward now revert to the previous relative index instead of panicing
[+] Added new AuByteBuffer apis
Trim, Pad, WriteFrom, WriteString, [TODO: ReadString]
[+] Added ByteBufferPushReadState
[+] Added ByteBufferPushWriteState
[*] Move from USC-16 to full UTF-16. Win32 can handle full UTF-16.
[*] ELogLevel is now an Aurora enum
[+] Raised arbitrary limit in header to 255, the max filter buffer
[+] Explicit GZip support
[+] Explicit Zip support
[+] Added [some] compressors
et al
2022-02-17 00:11:40 +00:00
|
|
|
case ECodePage::eEnumInvalid:
|
2021-09-06 10:58:08 +00:00
|
|
|
return {};
|
|
|
|
case ECodePage::eSysUnk:
|
|
|
|
ret = Win32ConvertFromUTF8ToCp(CP_ACP, utf8, utf8Length, cp, cpLen);
|
|
|
|
break;
|
|
|
|
case ECodePage::eUTF7:
|
|
|
|
ret = Win32ConvertFromUTF8ToCp(CP_UTF7, utf8, utf8Length, cp, cpLen);
|
|
|
|
break;
|
|
|
|
case ECodePage::eLatin1:
|
|
|
|
ret = Win32ConvertFromUTF8ToCp(CP_LATIN_1, utf8, utf8Length, cp, cpLen);
|
|
|
|
break;
|
|
|
|
case ECodePage::e18030:
|
|
|
|
ret = Win32ConvertFromUTF8ToCp(CP_CHINESE, utf8, utf8Length, cp, cpLen);
|
|
|
|
break;
|
|
|
|
case ECodePage::eSJIS:
|
|
|
|
ret = Win32ConvertFromUTF8ToCp(CP_SHIFTJIS, utf8, utf8Length, cp, cpLen);
|
|
|
|
break;
|
|
|
|
case ECodePage::e2312:
|
|
|
|
case ECodePage::eGBK:
|
|
|
|
ret = Win32ConvertFromUTF8ToCp(CP_2312_LIMITED_GBK, utf8, utf8Length, cp, cpLen);
|
|
|
|
break;
|
|
|
|
case ECodePage::eUTF16:
|
|
|
|
ret = Win32ConvertFromUTF8ToUTF16(utf8, utf8Length, cp, cpLen);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-10-02 16:07:33 +00:00
|
|
|
#endif
|
2021-09-06 10:58:08 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2021-10-02 16:07:33 +00:00
|
|
|
}
|