回退 'Pull Request !16226 : 修复TS AddText接口非法UTF-8转UTF-16崩溃问题'

This commit is contained in:
oh_ci 2024-10-24 01:44:10 +00:00 committed by Gitee
parent bba181f9a1
commit f84de924e1
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 30 additions and 87 deletions

View File

@ -17,7 +17,6 @@
#include "line_typeset_napi/js_line_typeset.h"
#include "napi_common.h"
#include "paragraph_napi/js_paragraph.h"
#include "utils/string_util.h"
#include "utils/text_log.h"
namespace OHOS::Rosen {
@ -169,10 +168,6 @@ napi_value JsParagraphBuilder::OnAddText(napi_env env, napi_callback_info info)
}
std::string text = "";
if (ConvertFromJsValue(env, argv[0], text)) {
if (!IsUtf8(text.c_str())) {
TEXT_LOGE("Invalid utf-8 text");
return NapiThrowError(env, TextErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
}
typographyCreate_->AppendText(Str8ToStr16(text));
}
return NapiGetUndefined(env);

View File

@ -169,7 +169,6 @@ template("graphics2d_source_set") {
"$drawing_core_src_dir/utils/region.cpp",
"$drawing_core_src_dir/utils/resource_holder.cpp",
"$drawing_core_src_dir/utils/round_rect.cpp",
"$drawing_core_src_dir/utils/string_util.cpp",
"$drawing_core_src_dir/utils/vertices.cpp",
]

View File

@ -33,7 +33,6 @@
#include "utils/log.h"
#include "utils/object_mgr.h"
#include "utils/string_util.h"
using namespace OHOS::Rosen;
@ -427,6 +426,36 @@ void OH_Drawing_TypographyHandlerPushTextStyle(OH_Drawing_TypographyCreate* hand
ConvertToOriginalText<TypographyCreate>(handler)->PushStyle(*rosenTextStyle);
}
static bool IsUtf8(const char* text)
{
int len = strlen(text);
int n;
for (int i = 0; i < len; i++) {
uint32_t c = text[i];
if (c <= 0x7F) { // 0x00 and 0x7F is the range of utf-8
n = 0;
} else if ((c & 0xE0) == 0xC0) { // 0xE0 and 0xC0 is the range of utf-8
n = 1;
} else if (c == 0xED && i < (len - 1) && (text[i + 1] & 0xA0) == 0xA0) { // 0xA0 and 0xED is the range of utf-8
return false;
} else if ((c & 0xF0) == 0xE0) { // 0xE0 and 0xF0 is the range of utf-8
n = 2; // 2 means the size of range
} else if ((c & 0xF8) == 0xF0) { // 0xF0 and 0xF8 is the range of utf-8
n = 3; // 3 means the size of range
} else {
return false;
}
for (int j = 0; j < n && i < len; j++) {
// 0x80 and 0xC0 is the range of utf-8
if ((++i == len) || ((text[i] & 0xC0) != 0x80)) {
return false;
}
}
}
return true;
}
void OH_Drawing_TypographyHandlerAddText(OH_Drawing_TypographyCreate* handler, const char* text)
{
if (!text || !handler) {

View File

@ -1,27 +0,0 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef GRAPHIC_GRAPHIC_2D_STRING_UTIL_H
#define GRAPHIC_GRAPHIC_2D_STRING_UTIL_H
namespace OHOS {
namespace Rosen {
bool IsUtf8(const char* text);
}
} // namespace OHOS
#endif // GRAPHIC_GRAPHIC_2D_STRING_UTIL_H

View File

@ -1,53 +0,0 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string>
#include "utils/string_util.h"
namespace OHOS {
namespace Rosen {
bool IsUtf8(const char* text)
{
int len = strlen(text);
int n;
int i = 0;
while (i < len) {
uint32_t c = text[i];
if (c <= 0x7F) { // 0x00 and 0x7F is the range of utf-8
n = 0;
} else if ((c & 0xE0) == 0xC0) { // 0xE0 and 0xC0 is the range of utf-8
n = 1;
} else if (c == 0xED && i < (len - 1) && (text[i + 1] & 0xA0) == 0xA0) { // 0xA0 and 0xED is the range of utf-8
return false;
} else if ((c & 0xF0) == 0xE0) { // 0xE0 and 0xF0 is the range of utf-8
n = 2; // 2 means the size of range
} else if ((c & 0xF8) == 0xF0) { // 0xF0 and 0xF8 is the range of utf-8
n = 3; // 3 means the size of range
} else {
return false;
}
for (int j = 0; j < n && i < len; j++) {
// 0x80 and 0xC0 is the range of utf-8
if ((++i == len) || ((text[i] & 0xC0) != 0x80)) {
return false;
}
}
i++; // move to the next character
}
return true;
}
} // namespace Rosen
} // namespace OHOS