Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 58 additions & 21 deletions src/encodes/detectcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,38 @@ static QHash<QByteArray, QByteArray> gs_ReplaceFromUtf8ToGB18030_2020Error{
{QByteArray::fromHex("9635B630"), "\u241FE"},
};

// GB18030-2022 PUA → 2005 Unicode 映射表
// iconv 在 2022 标准下将 FE51 等转换为 PUA 码位(U+E816 等),无字体覆盖导致乱码
// 重映射为 2005 标准 Unicode 码位(有字体覆盖)
static QHash<QByteArray, QByteArray> gs_PUA2022ToUnicode2005{
{QByteArray::fromHex("EEA096"), QByteArray::fromHex("F0A08287")},
{QByteArray::fromHex("EEA097"), QByteArray::fromHex("F0A08289")},
{QByteArray::fromHex("EEA098"), QByteArray::fromHex("F0A0838C")},
{QByteArray::fromHex("EEA0B1"), QByteArray::fromHex("F0A19797")},
{QByteArray::fromHex("EEA0BB"), QByteArray::fromHex("F0A2A68F")},
{QByteArray::fromHex("EEA195"), QByteArray::fromHex("F0A487BE")},
// 竖排形式:四字节 8431xxxx 在 2022 下映射 PUA,等价双字节 A6D9 系的正式码位
{QByteArray::fromHex("EE9E8D"), QByteArray::fromHex("EFB890")}, // U+E78D -> U+FE10
{QByteArray::fromHex("EE9E8F"), QByteArray::fromHex("EFB891")}, // U+E78F -> U+FE11
{QByteArray::fromHex("EE9E8E"), QByteArray::fromHex("EFB892")}, // U+E78E -> U+FE12
{QByteArray::fromHex("EE9E90"), QByteArray::fromHex("EFB893")}, // U+E790 -> U+FE13
{QByteArray::fromHex("EE9E91"), QByteArray::fromHex("EFB894")}, // U+E791 -> U+FE14
{QByteArray::fromHex("EE9E92"), QByteArray::fromHex("EFB895")}, // U+E792 -> U+FE15
{QByteArray::fromHex("EE9E93"), QByteArray::fromHex("EFB896")}, // U+E793 -> U+FE16
{QByteArray::fromHex("EE9E94"), QByteArray::fromHex("EFB897")}, // U+E794 -> U+FE17
{QByteArray::fromHex("EE9E95"), QByteArray::fromHex("EFB898")}, // U+E795 -> U+FE18
{QByteArray::fromHex("EE9E96"), QByteArray::fromHex("EFB899")}, // U+E796 -> U+FE19
// CJK 扩展 B:四字节 8235xxxx 在 2022 下映射 PUA,等价正式码位 U+9FB4 系
{QByteArray::fromHex("EEA09E"), QByteArray::fromHex("E9BEB4")}, // U+E81E -> U+9FB4
{QByteArray::fromHex("EEA0A6"), QByteArray::fromHex("E9BEB5")}, // U+E826 -> U+9FB5
{QByteArray::fromHex("EEA0AB"), QByteArray::fromHex("E9BEB6")}, // U+E82B -> U+9FB6
{QByteArray::fromHex("EEA0AC"), QByteArray::fromHex("E9BEB7")}, // U+E82C -> U+9FB7
{QByteArray::fromHex("EEA0B2"), QByteArray::fromHex("E9BEB8")}, // U+E832 -> U+9FB8
{QByteArray::fromHex("EEA183"), QByteArray::fromHex("E9BEB9")}, // U+E843 -> U+9FB9
{QByteArray::fromHex("EEA194"), QByteArray::fromHex("E9BEBA")}, // U+E854 -> U+9FBA
{QByteArray::fromHex("EEA1A4"), QByteArray::fromHex("E9BEBB")}, // U+E864 -> U+9FBB
};

// 见QTextCodec::mibEnum()
enum MibEncoding {
UnknownMib = 0,
Expand Down Expand Up @@ -811,29 +843,24 @@ bool DetectCode::ChangeFileEncodingFormat(QByteArray &inputStr,
fromMib = static_cast<MibEncoding>(fromCodec->mibEnum());
qCDebug(appLog) << "fromMib:" << fromMib;
}
// 不使用上层修改的Iconv处理时,不检测转换的编码格式,跳过GB18030转换特殊处理
MibEncoding toMib = UnknownMib;

const bool enablePatchedIconv = Config::instance()->enablePatchedIconv();
if (enablePatchedIconv) {
qCDebug(appLog) << "Patched Iconv enabled";
QTextCodec *toCodec = QTextCodec::codecForName(toCode.toUtf8());
if (toCodec) {
toMib = static_cast<MibEncoding>(toCodec->mibEnum());
qCDebug(appLog) << "toMib:" << toMib;
}
QTextCodec *toCodec = QTextCodec::codecForName(toCode.toUtf8());
if (toCodec) {
toMib = static_cast<MibEncoding>(toCodec->mibEnum());
qCDebug(appLog) << "toMib:" << toMib;
}

// 替换需手动修改 UTF-8 和 GB180303 编码转换的部分
if (GB18030 == fromMib && UTF_8 == toMib) {
qCDebug(appLog) << "Converting from GB18030 to UTF-8, applying 2005 error replacements";
for (auto itr = gs_ReplaceFromGB18030_2005Error.begin(); itr != gs_ReplaceFromGB18030_2005Error.end(); ++itr) {
inputStr.replace(itr.key(), itr.value());
}
} else if (UTF_8 == fromMib && GB18030 == toMib) {
qCDebug(appLog) << "Converting from UTF-8 to GB18030, applying 2020 error replacements";
for (auto itr = gs_ReplaceToGB18030_2020Error.begin(); itr != gs_ReplaceToGB18030_2020Error.end(); ++itr) {
inputStr.replace(itr.key(), itr.value());
}
// 替换需手动修改 UTF-8 和 GB18030 编码转换的部分
if (GB18030 == fromMib && UTF_8 == toMib) {
qCDebug(appLog) << "Converting from GB18030 to UTF-8, applying 2005 error replacements";
for (auto itr = gs_ReplaceFromGB18030_2005Error.begin(); itr != gs_ReplaceFromGB18030_2005Error.end(); ++itr) {
inputStr.replace(itr.key(), itr.value());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Neighboring characters are corrupted

When a valid two-byte GB18030 character ending in 0xFE is followed by ASCII Q (0x51), inputStr.replace() matches FE51 across the character boundary and inserts the sentinel; the error handler then emits incorrect text for the neighboring characters.

Apply replacements only to complete GB18030 character sequences, without matching across character boundaries.

Prompt for AI agents
In `src/encodes/detectcode.cpp` at line 858:

**Neighboring characters are corrupted**

When a valid two-byte GB18030 character ending in `0xFE` is followed by ASCII `Q` (`0x51`), `inputStr.replace()` matches `FE51` across the character boundary and inserts the sentinel; the error handler then emits incorrect text for the neighboring characters.

Apply replacements only to complete GB18030 character sequences, without matching across character boundaries.

}
} else if (UTF_8 == fromMib && GB18030 == toMib) {
qCDebug(appLog) << "Converting from UTF-8 to GB18030, applying 2020 error replacements";
for (auto itr = gs_ReplaceToGB18030_2020Error.begin(); itr != gs_ReplaceToGB18030_2020Error.end(); ++itr) {
inputStr.replace(itr.key(), itr.value());
}
Comment on lines +861 to 864

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · GB18030 saves corrupt Unicode

When standard iconv is used to save UTF-8 text containing one of the six mapped Unicode characters as GB18030, ChangeFileEncodingFormat replaces characters such as U+20087 with the FFFF11 sentinel, but checkUTF8ToGB18030Error does not recognize that sentinel; its EILSEQ path emits ? while consuming the sentinel bytes, so saving these characters as GB18030 corrupts them.

Handle the sentinel explicitly in the EILSEQ path, or avoid substituting it on iconv paths that cannot decode it.

Prompt for AI agents
In `src/encodes/detectcode.cpp` at lines 861-864:

**GB18030 saves corrupt Unicode**

When standard iconv is used to save UTF-8 text containing one of the six mapped Unicode characters as GB18030, `ChangeFileEncodingFormat` replaces characters such as U+20087 with the `FFFF11` sentinel, but `checkUTF8ToGB18030Error` does not recognize that sentinel; its EILSEQ path emits `?` while consuming the sentinel bytes, so saving these characters as GB18030 corrupts them.

Handle the sentinel explicitly in the EILSEQ path, or avoid substituting it on iconv paths that cannot decode it.

}

Expand Down Expand Up @@ -944,7 +971,17 @@ bool DetectCode::ChangeFileEncodingFormat(QByteArray &inputStr,

// 计算 iconv() 实际转换的字节数
size_t realConvertSize = maxBufferSize - outbytesleft;
outStr += QByteArray(bufferHeader, static_cast<int>(realConvertSize));
QByteArray convertedData(bufferHeader, static_cast<int>(realConvertSize));

// GB18030-2022 标准下 iconv 将 FE51 等转为 PUA 码位(U+E816 等),
// 无字体覆盖导致乱码,重映射为 2005 标准 Unicode 码位
if (GB18030 == fromMib && UTF_8 == toMib) {
for (auto itr = gs_PUA2022ToUnicode2005.begin(); itr != gs_PUA2022ToUnicode2005.end(); ++itr) {
convertedData.replace(itr.key(), itr.value());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Valid private-use text changes

When GB18030 decoding produces a mapped PUA scalar from intentional private-use text or a valid legacy mapping, convertedData.replace() changes every matching PUA scalar, including ones that should be preserved; readFile returns different text and saving can persist the corruption.

Apply the remapping only to the GB18030 byte sequences whose interpretation changed, rather than to every matching decoded PUA scalar.

Prompt for AI agents
In `src/encodes/detectcode.cpp` at line 980:

**Valid private-use text changes**

When GB18030 decoding produces a mapped PUA scalar from intentional private-use text or a valid legacy mapping, `convertedData.replace()` changes every matching PUA scalar, including ones that should be preserved; `readFile` returns different text and saving can persist the corruption.

Apply the remapping only to the GB18030 byte sequences whose interpretation changed, rather than to every matching decoded PUA scalar.

}
}

outStr += convertedData;
qCDebug(appLog) << "Converted data size:" << realConvertSize;

delete[] bufferHeader;
Expand Down
37 changes: 32 additions & 5 deletions tests/common2/test_detectcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,24 +808,25 @@ TEST_F(DetectCodeTest, ChangeEncoding_InvalidGb18030Byte_ReplacedAndContinues)
TEST_F(DetectCodeTest, ChangeEncoding_Gb18030Pua_MapsToUtf8Pua)
{
// Arrange: E8 GB18030 PUA 0x37903582(小端字节 82 35 90 37)
// GB18030-2022 下 iconv 输出 PUA \uE81E,post-iconv 重映射为等价正式码位 \u9FB4
QByteArray in = QByteArray::fromHex("82359037"), out;
// Act
const bool ok = DetectCode::ChangeFileEncodingFormat(in, out, QStringLiteral("GB18030"), QStringLiteral("UTF-8"));
// Assert: \uE81E 的 UTF-8 编码(本机 iconv 实测映射,见文件头注释)
// Assert: \u9FB4 的 UTF-8 编码 E9 BE B4
EXPECT_TRUE(ok);
EXPECT_EQ(out, QByteArray::fromHex("EEA09E"));
EXPECT_EQ(out, QByteArray::fromHex("E9BEB4"));
}

TEST_F(DetectCodeTest, ChangeEncoding_PatchedIconv_Fe51ReplacedVia2005Map)
{
// Arrange: E9 enablePatchedIconv=true → FE51 预替换为 FFFFFF01 → EILSEQ → \uE816
// Arrange: FE51 预替换为 FFFFFF01 → EILSEQ → \uE816 → post-iconv PUA 重映射 → \u20087
installConfigStubs(true, true, QByteArrayLiteral("UTF-8"));
QByteArray in = QByteArray::fromHex("FE51"), out;
// Act
const bool ok = DetectCode::ChangeFileEncodingFormat(in, out, QStringLiteral("GB18030"), QStringLiteral("UTF-8"));
// Assert: \uE816 UTF-8 = EE A0 96
// Assert: \u20087 UTF-8 = F0 A0 82 87(post-iconv PUA 重映射后)
EXPECT_TRUE(ok);
EXPECT_EQ(out, QByteArray::fromHex("EEA096"));
EXPECT_EQ(out, QByteArray::fromHex("F0A08287"));
}

TEST_F(DetectCodeTest, ChangeEncoding_PatchedIconv_U20087MapsToGbPuaBytes)
Expand All @@ -840,6 +841,32 @@ TEST_F(DetectCodeTest, ChangeEncoding_PatchedIconv_U20087MapsToGbPuaBytes)
EXPECT_EQ(out, QByteArray::fromHex("95329031"));
}

TEST_F(DetectCodeTest, ChangeEncoding_StandardIconv_FourByteE78DRemappedToFE10)
{
// Arrange: GB18030-2022 下 glibc iconv 将四字节 8431 8236 转为 PUA \uE78D,
// post-iconv 重映射 → \uFE10(与双字节 A6D9 的解码结果一致)
installConfigStubs(false, false, QByteArrayLiteral("UTF-8"));
QByteArray in = QByteArray::fromHex("84318236"), out;
// Act
const bool ok = DetectCode::ChangeFileEncodingFormat(in, out, QStringLiteral("GB18030"), QStringLiteral("UTF-8"));
// Assert: \uFE10 UTF-8 = EF B8 90
EXPECT_TRUE(ok);
EXPECT_EQ(out, QByteArray::fromHex("EFB890"));
}

TEST_F(DetectCodeTest, ChangeEncoding_StandardIconv_FourByteE81ERemappedTo9FB4)
{
// Arrange: GB18030-2022 下 glibc iconv 将四字节 8235 9037 转为 PUA \uE81E,
// post-iconv 重映射 → \u9FB4(glibc charmap 注释的等价字符)
installConfigStubs(false, false, QByteArrayLiteral("UTF-8"));
QByteArray in = QByteArray::fromHex("82359037"), out;
// Act
const bool ok = DetectCode::ChangeFileEncodingFormat(in, out, QStringLiteral("GB18030"), QStringLiteral("UTF-8"));
// Assert: \u9FB4 UTF-8 = E9 BE B4
EXPECT_TRUE(ok);
EXPECT_EQ(out, QByteArray::fromHex("E9BEB4"));
}

TEST_F(DetectCodeTest, ChangeEncoding_InvalidFromCode_FallsBackAndFails)
{
// Arrange: E7 iconv_open 失败 → QTextCodec 回退 → 源编码不存在 → false
Expand Down
Loading