diff --git a/3rdparty/terminalwidget/CMakeLists.txt b/3rdparty/terminalwidget/CMakeLists.txt index c178ae3bc..7d5dfb852 100644 --- a/3rdparty/terminalwidget/CMakeLists.txt +++ b/3rdparty/terminalwidget/CMakeLists.txt @@ -107,6 +107,7 @@ set(SRCS lib/Vt102Emulation.cpp lib/EscapeSequenceUrlExtractor.cpp lib/encodes/detectcode.cpp + lib/encodes/utf8multibytecount.cpp ) # Only the Headers that need to be moc'd go here diff --git a/3rdparty/terminalwidget/lib/encodes/detectcode.cpp b/3rdparty/terminalwidget/lib/encodes/detectcode.cpp index e5f5a35e8..b0cf04006 100644 --- a/3rdparty/terminalwidget/lib/encodes/detectcode.cpp +++ b/3rdparty/terminalwidget/lib/encodes/detectcode.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2019 - 2023 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2019 - 2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later @@ -12,6 +12,7 @@ #include #include +#include "utf8multibytecount.h" #include #define DISABLE_TEXTCODEC @@ -484,62 +485,6 @@ int DetectCode::ChartDet_DetectingTextCoding(const char *str, QString &encoding, return CHARDET_SUCCESS; } -/** - * @return 根据 UTF-8 字符编码,返回传入的字符串 \a buf 头部字符可能占用的字节数量 - * @note 不同编码UTF-8字节划分示例,除首个字节为0外 - * 如果一个字节的第一位是0,则这个字节单独就是一个字符;如果第一位是1,则连续有多少个1,就表示当前字符占用多少个字节。 - * 0000 0000-0000 007F | 0xxxxxxx - * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx - * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx - * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx - * - * @link https://zh.wikipedia.org/wiki/UTF-8 - */ -int utf8MultiByteCount(char *buf, size_t size) -{ - // UTF 字符类型,单字符,中间字符,双字节,三字节,四字节 - enum UtfCharType { - Single, - Mid, - DoubleBytes, - ThreeBytes, - FourBytes, - }; - - // 取得 UTF-8 字节序数值 - auto LeftBitFunc = [](char data) -> int { - // 返回前导1的个数 - int res = 0; - while (data & 0x80) { - res++; - data <<= 1; - } - return res; - }; - - int count = 0; - while (size > 0 && count < FourBytes) { - int leftBits = LeftBitFunc(*buf); - switch (leftBits) { - case Mid: - count++; - break; - case DoubleBytes: - case ThreeBytes: - case FourBytes: - return leftBits; - default: - // 超过4字节或单字节,均返回1 - return 1; - } - - buf++; - size--; - } - - return count; -} - /** @brief 检测 GB18030 转换 UTF-8 时PUA区域异常处理 diff --git a/3rdparty/terminalwidget/lib/encodes/utf8multibytecount.cpp b/3rdparty/terminalwidget/lib/encodes/utf8multibytecount.cpp new file mode 100644 index 000000000..55f686436 --- /dev/null +++ b/3rdparty/terminalwidget/lib/encodes/utf8multibytecount.cpp @@ -0,0 +1,61 @@ +// SPDX-FileCopyrightText: 2019 - 2026 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "utf8multibytecount.h" + +/*! + * @brief 计算 UTF-8 字符的字节数 + * + * 如果一个字节的第一位是0,则这个字节单独就是一个字符;如果第一位是1,则连续有多少个1,就表示当前字符占用多少个字节。 + * 0000 0000-0000 007F | 0xxxxxxx + * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx + * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx + * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx + * + * @link https://zh.wikipedia.org/wiki/UTF-8 + */ +int utf8MultiByteCount(char *buf, size_t size) +{ + // UTF 字符类型,单字符,中间字符,双字节,三字节,四字节 + enum UtfCharType { + Single, + Mid, + DoubleBytes, + ThreeBytes, + FourBytes, + }; + + // 取得 UTF-8 字节序数值 + auto LeftBitFunc = [](unsigned char data) -> int { + // 返回前导1的个数 + int res = 0; + while (data & 0x80) { + res++; + data <<= 1; + } + return res; + }; + + int count = 0; + while (size > 0 && count < FourBytes) { + int leftBits = LeftBitFunc(*buf); + switch (leftBits) { + case Mid: + count++; + break; + case DoubleBytes: + case ThreeBytes: + case FourBytes: + return leftBits; + default: + // 超过4字节或单字节,均返回1 + return 1; + } + + buf++; + size--; + } + + return count; +} diff --git a/3rdparty/terminalwidget/lib/encodes/utf8multibytecount.h b/3rdparty/terminalwidget/lib/encodes/utf8multibytecount.h new file mode 100644 index 000000000..330055d0e --- /dev/null +++ b/3rdparty/terminalwidget/lib/encodes/utf8multibytecount.h @@ -0,0 +1,12 @@ +// SPDX-FileCopyrightText: 2019 - 2026 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef UTF8_MULTIBYTE_COUNT_H +#define UTF8_MULTIBYTE_COUNT_H + +#include + +int utf8MultiByteCount(char *buf, size_t size); + +#endif // UTF8_MULTIBYTE_COUNT_H diff --git a/src/common/settingio.cpp b/src/common/settingio.cpp index 72926cb08..93ac10f4d 100644 --- a/src/common/settingio.cpp +++ b/src/common/settingio.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd. -// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2022-2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later @@ -315,7 +315,7 @@ QString SettingIO::unescapedString(const QString &src) static const int numEscapeCodes = sizeof(escapeCodes) / sizeof(escapeCodes[0]); QString stringResult; - int escapeVal = 0; + unsigned int escapeVal = 0; QChar ch; int i = 0; normal: @@ -363,6 +363,10 @@ QString SettingIO::unescapedString(const QString &src) if (ch >= 'a') ch = ch.unicode() - ('a' - 'A'); if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F')) { + if (escapeVal > 0x0FFFFFFF) { // 0x0FFFFFFF << 4 = 0xFFFFFFF0, max before 32-bit overflow + stringResult += QChar(escapeVal); + goto normal; + } escapeVal <<= 4; escapeVal += strchr(hexDigits, ch.toLatin1()) - hexDigits; ++i; @@ -380,6 +384,10 @@ QString SettingIO::unescapedString(const QString &src) ch = src.at(i); if (ch >= '0' && ch <= '7') { + if (escapeVal > 0x1FFFFFFF) { // 0x1FFFFFFF << 3 = 0xFFFFFFF8, max before 32-bit overflow + stringResult += QChar(escapeVal); + goto normal; + } escapeVal <<= 3; escapeVal += ch.toLatin1() - '0'; ++i; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a9180f4d7..5dc1e603c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. +# SPDX-FileCopyrightText: 2022-2026 UnionTech Software Technology Co., Ltd. # # SPDX-License-Identifier: GPL-3.0-or-later @@ -63,6 +63,7 @@ FILE(GLOB allThirdPartySource ../3rdparty/terminalwidget/lib/*.cpp ../3rdparty/terminalwidget/lib/history/*.cpp ../3rdparty/terminalwidget/lib/history/compact/*.cpp + ../3rdparty/terminalwidget/lib/encodes/utf8multibytecount.cpp ) FILE(GLOB qrcFiles @@ -88,6 +89,7 @@ FILE(GLOB allTestSource src/remotemanage/*.cpp src/settings/*.cpp src/views/*.cpp + src/3rdparty/terminalwidget/lib/*.cpp ) set(UI ../3rdparty/terminalwidget/lib/SearchBar.ui) diff --git a/tests/src/3rdparty/terminalwidget/lib/ut_utf8multibytecount_test.cpp b/tests/src/3rdparty/terminalwidget/lib/ut_utf8multibytecount_test.cpp new file mode 100644 index 000000000..98af9cc74 --- /dev/null +++ b/tests/src/3rdparty/terminalwidget/lib/ut_utf8multibytecount_test.cpp @@ -0,0 +1,140 @@ +// Copyright (C) 2026 Uniontech Software Technology Co.,Ltd. +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include + +#include "encodes/utf8multibytecount.h" + +/******************************************************************************* + 1. @函数: utf8MultiByteCountAsciiTest + 2. @说明: 测试 ASCII 单字节 (0x00-0x7F) +*******************************************************************************/ +TEST(Utf8MultiByteCountTest, AsciiSingleByte) +{ + char buf[] = {0x41}; // 'A' + EXPECT_EQ(utf8MultiByteCount(buf, 1), 1); + + char buf2[] = {0x00}; + EXPECT_EQ(utf8MultiByteCount(buf2, 1), 1); + + char buf3[] = {0x7F}; + EXPECT_EQ(utf8MultiByteCount(buf3, 1), 1); +} + +/******************************************************************************* + 1. @函数: utf8MultiByteCountTwoByteLeadTest + 2. @说明: 测试 UTF-8 双字节首字节 (0xC0-0xDF) +*******************************************************************************/ +TEST(Utf8MultiByteCountTest, TwoByteLead) +{ + char buf1[] = {static_cast(0xC0)}; + EXPECT_EQ(utf8MultiByteCount(buf1, 1), 2); + + char buf2[] = {static_cast(0xC2)}; + EXPECT_EQ(utf8MultiByteCount(buf2, 1), 2); + + char buf3[] = {static_cast(0xDF)}; + EXPECT_EQ(utf8MultiByteCount(buf3, 1), 2); +} + +/******************************************************************************* + 1. @函数: utf8MultiByteCountThreeByteLeadTest + 2. @说明: 测试 UTF-8 三字节首字节 (0xE0-0xEF) +*******************************************************************************/ +TEST(Utf8MultiByteCountTest, ThreeByteLead) +{ + char buf1[] = {static_cast(0xE0)}; + EXPECT_EQ(utf8MultiByteCount(buf1, 1), 3); + + char buf2[] = {static_cast(0xE4)}; + EXPECT_EQ(utf8MultiByteCount(buf2, 1), 3); + + char buf3[] = {static_cast(0xEF)}; + EXPECT_EQ(utf8MultiByteCount(buf3, 1), 3); +} + +/******************************************************************************* + 1. @函数: utf8MultiByteCountFourByteLeadTest + 2. @说明: 测试 UTF-8 四字节首字节 (0xF0-0xF7) +*******************************************************************************/ +TEST(Utf8MultiByteCountTest, FourByteLead) +{ + char buf1[] = {static_cast(0xF0)}; + EXPECT_EQ(utf8MultiByteCount(buf1, 1), 4); + + char buf2[] = {static_cast(0xF4)}; + EXPECT_EQ(utf8MultiByteCount(buf2, 1), 4); + + char buf3[] = {static_cast(0xF7)}; + EXPECT_EQ(utf8MultiByteCount(buf3, 1), 4); +} + +/******************************************************************************* + 1. @函数: utf8MultiByteCountContinuationByteTest + 2. @说明: 测试 UTF-8 续字节 (0x80-0xBF) +*******************************************************************************/ +TEST(Utf8MultiByteCountTest, ContinuationByte) +{ + char buf1[] = {static_cast(0x80)}; + EXPECT_EQ(utf8MultiByteCount(buf1, 1), 1); + + char buf2[] = {static_cast(0xBF)}; + EXPECT_EQ(utf8MultiByteCount(buf2, 1), 1); + + // Multiple continuation bytes: count increments until FourBytes + char buf3[] = {static_cast(0x80), static_cast(0x80)}; + EXPECT_EQ(utf8MultiByteCount(buf3, 2), 2); + + char buf4[] = {static_cast(0x80), static_cast(0x80), static_cast(0x80)}; + EXPECT_EQ(utf8MultiByteCount(buf4, 3), 3); +} + +/******************************************************************************* + 1. @函数: utf8MultiByteCountBoundaryTest + 2. @说明: 测试边界值 0xFF 和 0xFE +*******************************************************************************/ +TEST(Utf8MultiByteCountTest, BoundaryValues) +{ + // 0xFF: 8 leading 1s -> default case, returns 1 + char bufFF[] = {static_cast(0xFF)}; + EXPECT_EQ(utf8MultiByteCount(bufFF, 1), 1); + + // 0xFE: 7 leading 1s -> default case, returns 1 + char bufFE[] = {static_cast(0xFE)}; + EXPECT_EQ(utf8MultiByteCount(bufFE, 1), 1); + + // 0xFD: 6 leading 1s -> default case, returns 1 + char bufFD[] = {static_cast(0xFD)}; + EXPECT_EQ(utf8MultiByteCount(bufFD, 1), 1); +} + +/******************************************************************************* + 1. @函数: utf8MultiByteCountMultiByteSequenceTest + 2. @说明: 测试多字节序列(首字节决定返回值) +*******************************************************************************/ +TEST(Utf8MultiByteCountTest, MultiByteSequence) +{ + // 3-byte sequence: lead byte determines return value + char buf3[] = {static_cast(0xE0), static_cast(0x80), static_cast(0x80)}; + EXPECT_EQ(utf8MultiByteCount(buf3, 3), 3); + + // 4-byte sequence + char buf4[] = {static_cast(0xF0), static_cast(0x80), static_cast(0x80), static_cast(0x80)}; + EXPECT_EQ(utf8MultiByteCount(buf4, 4), 4); + + // 2-byte sequence + char buf2[] = {static_cast(0xC2), static_cast(0x80)}; + EXPECT_EQ(utf8MultiByteCount(buf2, 2), 2); +} + +/******************************************************************************* + 1. @函数: utf8MultiByteCountZeroSizeTest + 2. @说明: 测试 size=0 的边界情况 +*******************************************************************************/ +TEST(Utf8MultiByteCountTest, ZeroSize) +{ + char buf[] = {0x41}; + EXPECT_EQ(utf8MultiByteCount(buf, 0), 0); +} diff --git a/tests/src/common/ut_settingio_test.cpp b/tests/src/common/ut_settingio_test.cpp index a0abc3c4b..925564a27 100644 --- a/tests/src/common/ut_settingio_test.cpp +++ b/tests/src/common/ut_settingio_test.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd. -// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2022-2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later @@ -94,4 +94,121 @@ TEST_F(UT_SettingIO_Test, unescapedStringTest) qDebug() << strResut; } + +/******************************************************************************* + 1. @函数: unescapedStringCommonEscapesTest + 2. @说明: 测试常见转义序列的解码 +*******************************************************************************/ +TEST_F(UT_SettingIO_Test, unescapedStringCommonEscapesTest) +{ + EXPECT_EQ(SettingIO::unescapedString(QString("\\n")), QString("\n")); + EXPECT_EQ(SettingIO::unescapedString(QString("\\t")), QString("\t")); + EXPECT_EQ(SettingIO::unescapedString(QString("\\\\")), QString("\\")); + EXPECT_EQ(SettingIO::unescapedString(QString("\\r")), QString("\r")); + EXPECT_EQ(SettingIO::unescapedString(QString("\\a")), QString("\a")); + EXPECT_EQ(SettingIO::unescapedString(QString("\\b")), QString("\b")); + EXPECT_EQ(SettingIO::unescapedString(QString("\\f")), QString("\f")); + EXPECT_EQ(SettingIO::unescapedString(QString("\\v")), QString("\v")); + EXPECT_EQ(SettingIO::unescapedString(QString("\\\"")), QString("\"")); + EXPECT_EQ(SettingIO::unescapedString(QString("\\'")), QString("'")); + EXPECT_EQ(SettingIO::unescapedString(QString("\\s")), QString(" ")); + EXPECT_EQ(SettingIO::unescapedString(QString("\\?")), QString("?")); +} + +/******************************************************************************* + 1. @函数: unescapedStringHexEscapeTest + 2. @说明: 测试十六进制转义序列的解码 +*******************************************************************************/ +TEST_F(UT_SettingIO_Test, unescapedStringHexEscapeTest) +{ + EXPECT_EQ(SettingIO::unescapedString(QString("\\x41")), QString("A")); + EXPECT_EQ(SettingIO::unescapedString(QString("\\x4A")), QString("J")); + EXPECT_EQ(SettingIO::unescapedString(QString("\\x61")), QString("a")); + EXPECT_EQ(SettingIO::unescapedString(QString("\\xFF")), QString(QChar(0xFF))); + EXPECT_EQ(SettingIO::unescapedString(QString("\\x00")), QString(QChar(0))); +} + +/******************************************************************************* + 1. @函数: unescapedStringHexOverflowTest + 2. @说明: 测试超长十六进制转义序列触发溢出保护 +*******************************************************************************/ +TEST_F(UT_SettingIO_Test, unescapedStringHexOverflowTest) +{ + // \x100000000: 9 hex digits, overflow guard triggers after 8 digits + // escapeVal = 0x10000000 > 0x0FFFFFFF, outputs QChar(0) then '0' as normal text + QString result = SettingIO::unescapedString(QString("\\x100000000")); + EXPECT_EQ(result.size(), 2); + EXPECT_EQ(result.at(0), QChar(0)); + EXPECT_EQ(result.at(1), QChar('0')); +} + +/******************************************************************************* + 1. @函数: unescapedStringOctalEscapeTest + 2. @说明: 测试八进制转义序列的解码 +*******************************************************************************/ +TEST_F(UT_SettingIO_Test, unescapedStringOctalEscapeTest) +{ + EXPECT_EQ(SettingIO::unescapedString(QString("\\101")), QString("A")); + EXPECT_EQ(SettingIO::unescapedString(QString("\\123")), QString("S")); + EXPECT_EQ(SettingIO::unescapedString(QString("\\0")), QString(QChar(0))); +} + +/******************************************************************************* + 1. @函数: unescapedStringOctalOverflowTest + 2. @说明: 测试超长八进制转义序列触发溢出保护 +*******************************************************************************/ +TEST_F(UT_SettingIO_Test, unescapedStringOctalOverflowTest) +{ + // \77777777777: 11 octal digits, overflow guard triggers after 10 digits + // escapeVal = 0x3FFFFFFF > 0x1FFFFFFF, outputs QChar(0xFFFF) then '7' as normal text + QString result = SettingIO::unescapedString(QString("\\77777777777")); + EXPECT_EQ(result.size(), 2); + EXPECT_EQ(result.at(0), QChar(0xFFFF)); + EXPECT_EQ(result.at(1), QChar('7')); +} + +/******************************************************************************* + 1. @函数: unescapedStringEmptyTest + 2. @说明: 测试空字符串 +*******************************************************************************/ +TEST_F(UT_SettingIO_Test, unescapedStringEmptyTest) +{ + EXPECT_EQ(SettingIO::unescapedString(QString("")), QString("")); +} + +/******************************************************************************* + 1. @函数: unescapedStringInvalidEscapeTest + 2. @说明: 测试非法转义序列(字符被跳过) +*******************************************************************************/ +TEST_F(UT_SettingIO_Test, unescapedStringInvalidEscapeTest) +{ + // \z: 'z' is not a valid escape, it is skipped + EXPECT_EQ(SettingIO::unescapedString(QString("\\z")), QString("")); + // \q followed by normal text + EXPECT_EQ(SettingIO::unescapedString(QString("\\qhello")), QString("ello")); +} + +/******************************************************************************* + 1. @函数: unescapedStringMixedContentTest + 2. @说明: 测试混合内容的转义解码 +*******************************************************************************/ +TEST_F(UT_SettingIO_Test, unescapedStringMixedContentTest) +{ + EXPECT_EQ(SettingIO::unescapedString(QString("hello\\nworld\\x41")), + QString("hello\nworldA")); + EXPECT_EQ(SettingIO::unescapedString(QString("a\\tb\\rc\\\\d")), + QString("a\tb\rc\\d")); +} + +/******************************************************************************* + 1. @函数: unescapedStringTrailingBackslashTest + 2. @说明: 测试末尾反斜杠 +*******************************************************************************/ +TEST_F(UT_SettingIO_Test, unescapedStringTrailingBackslashTest) +{ + EXPECT_EQ(SettingIO::unescapedString(QString("hello\\")), QString("hello")); + EXPECT_EQ(SettingIO::unescapedString(QString("\\")), QString("")); +} + + #endif