2021-09-06 10:58:08 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: EncoderAdapter.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 "../Locale.hpp"
|
|
|
|
#include "Encoding.hpp"
|
|
|
|
#include "EncoderIConv.hpp"
|
|
|
|
#include "EncoderNSL.hpp"
|
|
|
|
#include "ConvertInternal.hpp"
|
|
|
|
#include "EncoderAdapter.hpp"
|
|
|
|
|
|
|
|
namespace Aurora::Locale::Encoding
|
|
|
|
{
|
|
|
|
EncoderAdapter::EncoderAdapter()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
EncoderAdapter::~EncoderAdapter()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void EncoderAdapter::Init(ECodePage page, bool decode)
|
|
|
|
{
|
|
|
|
this->page = page;
|
|
|
|
this->decode = decode;
|
|
|
|
}
|
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
bool EncoderAdapter::TestPage(ECodePage ref)
|
|
|
|
{
|
|
|
|
return (((page == ECodePage::eSysUnk) &&
|
|
|
|
(GetInternalCodePage() == ref)) ||
|
|
|
|
(page == ref));
|
|
|
|
}
|
|
|
|
|
|
|
|
AuStreamReadWrittenPair_t EncoderAdapter::CPToUTF8(const void *in, AuUInt32 length, void *utf8, AuUInt32 utf8Max)
|
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
AuStreamReadWrittenPair_t ret {};
|
|
|
|
|
2021-09-06 19:04:01 +00:00
|
|
|
if (!length)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2021-11-05 17:34:23 +00:00
|
|
|
|
|
|
|
// decode using internal and/or optimized apis first
|
|
|
|
if (TestPage(ECodePage::eUTF8))
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
length = CountUTF8Length({in, length}, true);
|
|
|
|
|
2022-01-19 17:08:13 +00:00
|
|
|
auto readable = AuMin(length, utf8Max);
|
2021-09-06 13:03:45 +00:00
|
|
|
if (utf8 && in)
|
|
|
|
{
|
2022-01-19 17:08:13 +00:00
|
|
|
AuMemcpy(utf8, in, readable);
|
2021-09-06 13:03:45 +00:00
|
|
|
}
|
2021-11-05 17:34:23 +00:00
|
|
|
|
|
|
|
return {readable, readable};
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
// never remove me. the stl and windows can't bet trusted to fail on conversion failure
|
|
|
|
if (TestPage(ECodePage::eGBK))
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
length = GBK::CountGbk(in, length, true);
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
2021-11-05 17:34:23 +00:00
|
|
|
else if (TestPage(ECodePage::eSJIS))
|
2021-09-06 19:04:01 +00:00
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
length = SJIS::CountSJIS(in, length, true);
|
2021-09-06 19:04:01 +00:00
|
|
|
}
|
2021-11-05 17:34:23 +00:00
|
|
|
else if (TestPage(ECodePage::eUTF16) || TestPage(ECodePage::eUTF16BE))
|
2021-09-06 19:04:01 +00:00
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
length = UTF16::Count16(in, length, true);
|
2021-09-06 19:04:01 +00:00
|
|
|
}
|
2021-11-05 17:34:23 +00:00
|
|
|
else if ((page == ECodePage::eUTF32) || (page == ECodePage::eUTF32BE))
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
length &= ~3;
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = Win32CPToUTF8(page, in, length, utf8, utf8Max);
|
|
|
|
if (!ret.first)
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
// TODO: iconv support here
|
2021-09-06 10:58:08 +00:00
|
|
|
ret = STLCPToUTF8(page, in, length, utf8, utf8Max);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuStreamReadWrittenPair_t EncoderAdapter::UTF8ToCp(const void *utf8, AuUInt32 utf8Length, void *cp, AuUInt32 cpLen)
|
|
|
|
{
|
|
|
|
AuStreamReadWrittenPair_t ret {};
|
|
|
|
|
2021-09-06 19:04:01 +00:00
|
|
|
if (!utf8)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!utf8Length)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
if (TestPage(ECodePage::eUTF8))
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2022-01-19 17:08:13 +00:00
|
|
|
auto readable = AuMin(utf8Length, cpLen);
|
2021-09-06 13:03:45 +00:00
|
|
|
if (utf8 && cp)
|
|
|
|
{
|
2022-01-19 17:08:13 +00:00
|
|
|
AuMemcpy(cp, utf8, readable);
|
2021-09-06 13:03:45 +00:00
|
|
|
}
|
|
|
|
return {utf8Length, utf8 ? cpLen : utf8Length};
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = Win32UTF8ToCp(page, utf8, utf8Length, cp, cpLen);
|
|
|
|
if (!ret.first)
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
// TODO: iconv support here
|
2021-09-06 10:58:08 +00:00
|
|
|
ret = STLUTF8ToCp(page, utf8, utf8Length, cp, cpLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|