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
1 change: 1 addition & 0 deletions 3rdparty/terminalwidget/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 2 additions & 57 deletions 3rdparty/terminalwidget/lib/encodes/detectcode.cpp
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -12,6 +12,7 @@
#include <QTextCodec>
#include <exception>

#include "utf8multibytecount.h"
#include <stdio.h>

#define DISABLE_TEXTCODEC
Expand Down Expand Up @@ -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区域异常处理

Expand Down
61 changes: 61 additions & 0 deletions 3rdparty/terminalwidget/lib/encodes/utf8multibytecount.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 12 additions & 0 deletions 3rdparty/terminalwidget/lib/encodes/utf8multibytecount.h
Original file line number Diff line number Diff line change
@@ -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 <cstddef>

int utf8MultiByteCount(char *buf, size_t size);

#endif // UTF8_MULTIBYTE_COUNT_H
12 changes: 10 additions & 2 deletions src/common/settingio.cpp
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
140 changes: 140 additions & 0 deletions tests/src/3rdparty/terminalwidget/lib/ut_utf8multibytecount_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>

#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<char>(0xC0)};
EXPECT_EQ(utf8MultiByteCount(buf1, 1), 2);

char buf2[] = {static_cast<char>(0xC2)};
EXPECT_EQ(utf8MultiByteCount(buf2, 1), 2);

char buf3[] = {static_cast<char>(0xDF)};
EXPECT_EQ(utf8MultiByteCount(buf3, 1), 2);
}

/*******************************************************************************
1. @函数: utf8MultiByteCountThreeByteLeadTest
2. @说明: 测试 UTF-8 三字节首字节 (0xE0-0xEF)
*******************************************************************************/
TEST(Utf8MultiByteCountTest, ThreeByteLead)
{
char buf1[] = {static_cast<char>(0xE0)};
EXPECT_EQ(utf8MultiByteCount(buf1, 1), 3);

char buf2[] = {static_cast<char>(0xE4)};
EXPECT_EQ(utf8MultiByteCount(buf2, 1), 3);

char buf3[] = {static_cast<char>(0xEF)};
EXPECT_EQ(utf8MultiByteCount(buf3, 1), 3);
}

/*******************************************************************************
1. @函数: utf8MultiByteCountFourByteLeadTest
2. @说明: 测试 UTF-8 四字节首字节 (0xF0-0xF7)
*******************************************************************************/
TEST(Utf8MultiByteCountTest, FourByteLead)
{
char buf1[] = {static_cast<char>(0xF0)};
EXPECT_EQ(utf8MultiByteCount(buf1, 1), 4);

char buf2[] = {static_cast<char>(0xF4)};
EXPECT_EQ(utf8MultiByteCount(buf2, 1), 4);

char buf3[] = {static_cast<char>(0xF7)};
EXPECT_EQ(utf8MultiByteCount(buf3, 1), 4);
}

/*******************************************************************************
1. @函数: utf8MultiByteCountContinuationByteTest
2. @说明: 测试 UTF-8 续字节 (0x80-0xBF)
*******************************************************************************/
TEST(Utf8MultiByteCountTest, ContinuationByte)
{
char buf1[] = {static_cast<char>(0x80)};
EXPECT_EQ(utf8MultiByteCount(buf1, 1), 1);

char buf2[] = {static_cast<char>(0xBF)};
EXPECT_EQ(utf8MultiByteCount(buf2, 1), 1);

// Multiple continuation bytes: count increments until FourBytes
char buf3[] = {static_cast<char>(0x80), static_cast<char>(0x80)};
EXPECT_EQ(utf8MultiByteCount(buf3, 2), 2);

char buf4[] = {static_cast<char>(0x80), static_cast<char>(0x80), static_cast<char>(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<char>(0xFF)};
EXPECT_EQ(utf8MultiByteCount(bufFF, 1), 1);

// 0xFE: 7 leading 1s -> default case, returns 1
char bufFE[] = {static_cast<char>(0xFE)};
EXPECT_EQ(utf8MultiByteCount(bufFE, 1), 1);

// 0xFD: 6 leading 1s -> default case, returns 1
char bufFD[] = {static_cast<char>(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<char>(0xE0), static_cast<char>(0x80), static_cast<char>(0x80)};
EXPECT_EQ(utf8MultiByteCount(buf3, 3), 3);

// 4-byte sequence
char buf4[] = {static_cast<char>(0xF0), static_cast<char>(0x80), static_cast<char>(0x80), static_cast<char>(0x80)};
EXPECT_EQ(utf8MultiByteCount(buf4, 4), 4);

// 2-byte sequence
char buf2[] = {static_cast<char>(0xC2), static_cast<char>(0x80)};
EXPECT_EQ(utf8MultiByteCount(buf2, 2), 2);
}

/*******************************************************************************
1. @函数: utf8MultiByteCountZeroSizeTest
2. @说明: 测试 size=0 的边界情况
*******************************************************************************/
TEST(Utf8MultiByteCountTest, ZeroSize)
{
char buf[] = {0x41};
EXPECT_EQ(utf8MultiByteCount(buf, 0), 0);
}
Loading
Loading