From nobody Mon Nov 25 15:33:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+69498+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+69498+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=hpe.com ARC-Seal: i=1; a=rsa-sha256; t=1609224221; cv=none; d=zohomail.com; s=zohoarc; b=C9FCXAwyj/JcavuKVE61CEACotVSN4pad73JMXpOQiuliL8mt6Fb3eAcLW6wJArOy3D05C35AoQToTYM2WrmXxT8k97CBf8DdWCtqMmZe7f2Obq0BNPvcTRJgsC65sn0DAJi3HfX1qqj2b03X0YKPZ6Ldx3AFDUfKFCKvHONpXo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1609224221; h=Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:Message-ID:Reply-To:References:Sender:Subject:To; bh=6EM4iPiGO29/b4FUJpVIt1QBR1lsQZ5LKR/Db2MUVIc=; b=RRR3frHMugdsktI1vGi+L0Y59Mr8xfetJngMsfHujuhdbFMgijvQrg4puMfBHqgE8LolW+jk44mOaGT2EQkjP8Dvd8C4fyAX4b3sfCuhxGckwokzssTiSlM6M2rk/CBbhRPYPy+GT8ZeaLY1yllz3KsORJeKzqBJJYb5cefkfgY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+69498+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1609224221026903.7056659923529; Mon, 28 Dec 2020 22:43:41 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id H7W9YY1788612xZWKXQEtj5w; Mon, 28 Dec 2020 22:43:40 -0800 X-Received: from mx0a-002e3701.pphosted.com (mx0a-002e3701.pphosted.com [148.163.147.86]) by mx.groups.io with SMTP id smtpd.web09.9068.1609224220241293385 for ; Mon, 28 Dec 2020 22:43:40 -0800 X-Received: from pps.filterd (m0134420.ppops.net [127.0.0.1]) by mx0b-002e3701.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 0BT6TmYq023419; Tue, 29 Dec 2020 06:43:31 GMT X-Received: from g2t2353.austin.hpe.com (g2t2353.austin.hpe.com [15.233.44.26]) by mx0b-002e3701.pphosted.com with ESMTP id 35ntj5nu0k-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 29 Dec 2020 06:43:31 +0000 X-Received: from g2t2360.austin.hpecorp.net (g2t2360.austin.hpecorp.net [16.196.225.135]) by g2t2353.austin.hpe.com (Postfix) with ESMTP id C220B6D; Tue, 29 Dec 2020 06:43:30 +0000 (UTC) X-Received: from abner-virtual-machine.asiapacific.hpqcorp.net (abner-virtual-machine.asiapacific.hpqcorp.net [15.119.210.153]) by g2t2360.austin.hpecorp.net (Postfix) with ESMTP id 901A937; Tue, 29 Dec 2020 06:43:29 +0000 (UTC) From: "Abner Chang" To: devel@edk2.groups.io Cc: Liming Gao , Leif Lindholm , Nickle Wang , Peter O'Hanley Subject: [edk2-devel] [PATCH v12 1/6] RedfishPkg/Ucs2Utf8lib: UCS2 to UFT8 manipulation library Date: Tue, 29 Dec 2020 13:55:52 +0800 Message-Id: <20201229055557.4679-2-abner.chang@hpe.com> In-Reply-To: <20201229055557.4679-1-abner.chang@hpe.com> References: <20201229055557.4679-1-abner.chang@hpe.com> X-HPE-SCL: -1 Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,abner.chang@hpe.com X-Gm-Message-State: 8pa0yW3TedrTlipcOzRvJ0wex1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1609224220; bh=GriQiStD6jwAXfetczqUvh/QkIgPfkx7u/+ij7Sjb6c=; h=Cc:Date:From:Reply-To:Subject:To; b=Q5ZsmK5AOE5rIg2sOpG5JJVPpSmbcDFvOZ/+NbQ4cbIHfVOI+RRYAEQqXJluHoT5dt8 aSPdp11CI/G3LH0kRSvaqEEh2T/DEMhOB/eQhIx/8k7y1QjAyVPinUjTw0rzL3aWFCJ1s js2YQUk78Rsjbr/EvK0+LoDNebRGNpKYO98= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This library provides UCS2 to UFT8 or vise versa functions to manipulate UCS2/UTF8 strings. This library is currently used by edk2 port of open source jansson library. Signed-off-by: Abner Chang Cc: Liming Gao Cc: Leif Lindholm Cc: Nickle Wang Cc: Peter O'Hanley Reviewed-by: Nickle Wang Acked-by: Leif Lindholm --- RedfishPkg/RedfishPkg.dec | 4 + RedfishPkg/RedfishLibs.dsc.inc | 1 + RedfishPkg/RedfishPkg.dsc | 1 + .../BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.inf | 31 ++ RedfishPkg/Include/Library/BaseUcs2Utf8Lib.h | 61 +++ .../Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.c | 421 ++++++++++++++++++ 6 files changed, 519 insertions(+) create mode 100644 RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.inf create mode 100644 RedfishPkg/Include/Library/BaseUcs2Utf8Lib.h create mode 100644 RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.c diff --git a/RedfishPkg/RedfishPkg.dec b/RedfishPkg/RedfishPkg.dec index 89a2a6de1e..4cae8c3a4a 100644 --- a/RedfishPkg/RedfishPkg.dec +++ b/RedfishPkg/RedfishPkg.dec @@ -20,6 +20,10 @@ ## @libraryclass Platform Redfish Host Interface Library # Platform implementation-specific Redfish Host Interface. RedfishPlatformHostInterfaceLib|Include/Library/RedfishHostInterfaceLib.h + ## @libraryclass This library provides UCS2 to UTF8 manipulation + # functions. + # + Ucs2Utf8Lib|Include/Library/BaseUcs2Utf8Lib.h =20 ## @libraryclass Platform Redfish Credential Library # Platform implementation-specific Redfish Credential Interface. diff --git a/RedfishPkg/RedfishLibs.dsc.inc b/RedfishPkg/RedfishLibs.dsc.inc index df21664f4e..271d838db6 100644 --- a/RedfishPkg/RedfishLibs.dsc.inc +++ b/RedfishPkg/RedfishLibs.dsc.inc @@ -12,5 +12,6 @@ ## !if $(REDFISH_ENABLE) =3D=3D TRUE RestExLib|RedfishPkg/Library/DxeRestExLib/DxeRestExLib.inf + Ucs2Utf8Lib|RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.inf !endif =20 diff --git a/RedfishPkg/RedfishPkg.dsc b/RedfishPkg/RedfishPkg.dsc index 5d9476bc79..15355493e2 100644 --- a/RedfishPkg/RedfishPkg.dsc +++ b/RedfishPkg/RedfishPkg.dsc @@ -50,5 +50,6 @@ RedfishPkg/Library/PlatformHostInterfaceLibNull/PlatformHostInterfaceLib= Null.inf RedfishPkg/Library/PlatformCredentialLibNull/PlatformCredentialLibNull.i= nf RedfishPkg/Library/DxeRestExLib/DxeRestExLib.inf + RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.inf =20 !include RedfishPkg/Redfish.dsc.inc diff --git a/RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.inf b/Redfi= shPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.inf new file mode 100644 index 0000000000..beb7e77892 --- /dev/null +++ b/RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.inf @@ -0,0 +1,31 @@ +## @file +# UCS2 to UTF8 manipulation library. +# +# Copyright (c) 2019, Intel Corporation. All rights reserved.
+# (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x0001001b + BASE_NAME =3D BaseUcs2Utf8Lib + FILE_GUID =3D 536646C3-46D0-4B12-ABC4-CDE1A33B5256 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D Ucs2Utf8Lib + +# +# VALID_ARCHITECTURES =3D IA32 X64 ARM AARCH64 RISCV64 +# + +[Sources] + BaseUcs2Utf8Lib.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + RedfishPkg/RedfishPkg.dec + + diff --git a/RedfishPkg/Include/Library/BaseUcs2Utf8Lib.h b/RedfishPkg/Incl= ude/Library/BaseUcs2Utf8Lib.h new file mode 100644 index 0000000000..c6989617df --- /dev/null +++ b/RedfishPkg/Include/Library/BaseUcs2Utf8Lib.h @@ -0,0 +1,61 @@ +/** @file + UCS2 to UTF8 manipulation library header file. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef BASE_UCS2UTF8_LIB_H_ +#define BASE_UCS2UTF8_LIB_H_ + +/// +/// L"\u0000" +/// +#define UNICODE_FORMAT_LEN 6 +#define UNICODE_FORMAT_CHAR_LEN 2 +#define UNICODE_FORMAT_CHAR_SIZE 3 + +#define UTF8_BUFFER_FOR_UCS2_MAX_SIZE 3 + +/** + Convert a UCS2 string to a UTF8 encoded string. + + @param[in] Ucs2Str The provided UCS2 string. + @param[out] Utf8StrAddr The converted UTF8 string address. = Caller + is responsible for Free this string. + + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES System runs out of resources. + @retval EFI_SUCCESS The UTF8 encoded string has been co= nverted. + +**/ +EFI_STATUS +UCS2StrToUTF8 ( + IN CHAR16 *Ucs2Str, + OUT CHAR8 **Utf8StrAddr + ); + +/** + Convert a UTF8 encoded string to a UCS2 string. + + @param[in] Utf8Str The provided UTF8 encoded string. + @param[out] Ucs2StrAddr The converted UCS2 string address. = Caller + is responsible for Free this string. + + @retval EFI_INVALID_PARAMETER The UTF8 encoded string is not vali= d to + convert to UCS2 string. + One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES System runs out of resources. + @retval EFI_SUCCESS The UCS2 string has been converted. + +**/ +EFI_STATUS +UTF8StrToUCS2 ( + IN CHAR8 *Utf8Str, + OUT CHAR16 **Ucs2StrAddr + ); + +#endif diff --git a/RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.c b/Redfish= Pkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.c new file mode 100644 index 0000000000..891423734b --- /dev/null +++ b/RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.c @@ -0,0 +1,421 @@ +/** @file + UCS2 to UTF8 manipulation library. + + Copyright (c) 2018 - 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#include +#include +#include +#include +#include +#include + +/** + Since each UCS2 character can be represented by 1-3 UTF8 encoded charact= ers, + this function is used to retrieve the UTF8 encoding size for a UCS2 char= acter. + + @param[in] Utf8Buffer The buffer for UTF8 encoded data. + + @retval Return the size of UTF8 encoding string or 0 if it is not f= or + UCS2 format. + +**/ +UINT8 +GetUTF8SizeForUCS2 ( + IN CHAR8 *Utf8Buffer + ) +{ + CHAR8 TempChar; + UINT8 Utf8Size; + + ASSERT (Utf8Buffer !=3D NULL); + + TempChar =3D *Utf8Buffer; + if ((TempChar & 0xF0) =3D=3D 0xF0) { + + // + // This format is not for UCS2. + // + return 0; + } + + Utf8Size =3D 1; + if ((TempChar & 0x80) =3D=3D 0x80) { + if ((TempChar & 0xC0) =3D=3D 0xC0) { + + Utf8Size ++; + if ((TempChar & 0xE0) =3D=3D 0xE0) { + + Utf8Size ++; + } + } + } + + return Utf8Size; +} + +/** + Since each UCS2 character can be represented by the format: \uXXXX, this= function + is used to retrieve the UCS2 character from a Unicode format. + Call MUST make sure there are at least 6 Bytes in the input UTF8 buffer. + + @param[in] Utf8Buffer The buffer for UTF8 encoded data. + @param[out] Ucs2Char The converted UCS2 character. + + @retval EFI_INVALID_PARAMETER Non-Ascii characters found in the h= exadecimal + digits string, and can't be convert= ed to a UCS2 + character. + @retval EFI_SUCCESS The UCS2 character has been retriev= ed. + +**/ +EFI_STATUS +GetUCS2CharByFormat ( + IN CHAR8 *Utf8Buffer, + OUT CHAR16 *Ucs2Char + ) +{ + UINT8 Num1; + UINT8 Num2; + UINT8 Index; + CHAR8 Ucs2CharFormat[UNICODE_FORMAT_CHAR_SIZE]; /// two Hexadecimal= digits Ascii string, like "3F" + + for (Index =3D 0; Index < 4; Index ++) { + if ((*(Utf8Buffer + 2 + Index) & 0x80) !=3D 0x00) { + return EFI_INVALID_PARAMETER; + } + } + + ZeroMem (Ucs2CharFormat, UNICODE_FORMAT_CHAR_SIZE); + + // + // Get the First Number, Offset is 2 + // + CopyMem (Ucs2CharFormat, Utf8Buffer + 2, UNICODE_FORMAT_CHAR_LEN); + Num1 =3D (UINT8) AsciiStrHexToUintn (Ucs2CharFormat); + + // + // Get the Second Number, Offset is 4 + // + CopyMem (Ucs2CharFormat, Utf8Buffer + 4, UNICODE_FORMAT_CHAR_LEN); + Num2 =3D (UINT8) AsciiStrHexToUintn (Ucs2CharFormat); + + // + // Ucs2Char is Little-Endian + // + *((CHAR8 *) Ucs2Char) =3D Num2; + *(((CHAR8 *) Ucs2Char) + 1) =3D Num1; + + return EFI_SUCCESS; +} + +/** + Convert a UCS2 character to UTF8 encoding string. + + @param[in] Ucs2Char The provided UCS2 character. + @param[out] Utf8Buffer The converted UTF8 encoded data. + + @retval Return the size of UTF8 encoding data for this UCS2 charact= er. + +**/ +UINT8 +UCS2CharToUTF8 ( + IN CHAR16 Ucs2Char, + OUT CHAR8 *Utf8Buffer + ) +{ + UINT16 Ucs2Number; + + ASSERT (Utf8Buffer !=3D NULL); + + Ucs2Number =3D (UINT16) Ucs2Char; + if (Ucs2Number <=3D 0x007F) { + + // + // UTF8 format: 0xxxxxxx + // + *Utf8Buffer =3D Ucs2Char & 0x7F; + return 1; + + } else if (Ucs2Number >=3D 0x0080 && Ucs2Number <=3D 0x07FF) { + + // + // UTF8 format: 110xxxxx 10xxxxxx + // + *(Utf8Buffer + 1) =3D (Ucs2Char & 0x3F) | 0x80; + *Utf8Buffer =3D ((Ucs2Char >> 6) & 0x1F) | 0xC0; + return 2; + + } else { /// Ucs2Number >=3D 0x0800 && Ucs2Number <=3D 0xFFFF + + // + // UTF8 format: 1110xxxx 10xxxxxx 10xxxxxx + // + *(Utf8Buffer + 2) =3D (Ucs2Char & 0x3F) | 0x80; + *(Utf8Buffer + 1) =3D ((Ucs2Char >> 6) & 0x3F) | 0x80; + *Utf8Buffer =3D ((Ucs2Char >> 12) & 0x0F) | 0xE0; + return 3; + } +} + +/** + Convert a UTF8 encoded data to a UCS2 character. + + @param[in] Utf8Buffer The provided UTF8 encoded data. + @param[out] Ucs2Char The converted UCS2 character. + + @retval EFI_INVALID_PARAMETER The UTF8 encoded string is not vali= d or + not for UCS2 character. + @retval EFI_SUCCESS The converted UCS2 character. + +**/ +EFI_STATUS +UTF8ToUCS2Char ( + IN CHAR8 *Utf8Buffer, + OUT CHAR16 *Ucs2Char + ) +{ + UINT8 Utf8Size; + CHAR8 *Ucs2Buffer; + CHAR8 TempChar1; + CHAR8 TempChar2; + CHAR8 TempChar3; + + ASSERT (Utf8Buffer !=3D NULL && Ucs2Char !=3D NULL); + ZeroMem (Ucs2Char, sizeof (CHAR16)); + Ucs2Buffer =3D (CHAR8 *) Ucs2Char; + + Utf8Size =3D GetUTF8SizeForUCS2 (Utf8Buffer); + switch (Utf8Size) { + + case 1: + + // + // UTF8 format: 0xxxxxxx + // + TempChar1 =3D *Utf8Buffer; + if ((TempChar1 & 0x80) !=3D 0x00) { + return EFI_INVALID_PARAMETER; + } + + *Ucs2Buffer =3D TempChar1; + *(Ucs2Buffer + 1) =3D 0; + break; + + case 2: + + // + // UTF8 format: 110xxxxx 10xxxxxx + // + TempChar1 =3D *Utf8Buffer; + if ((TempChar1 & 0xE0) !=3D 0xC0) { + return EFI_INVALID_PARAMETER; + } + + TempChar2 =3D *(Utf8Buffer + 1); + if ((TempChar2 & 0xC0) !=3D 0x80) { + return EFI_INVALID_PARAMETER; + } + + *Ucs2Buffer =3D (TempChar1 << 6) + (TempChar2 & 0x3F); + *(Ucs2Buffer + 1) =3D (TempChar1 >> 2) & 0x07; + break; + + case 3: + + // + // UTF8 format: 1110xxxx 10xxxxxx 10xxxxxx + // + TempChar1 =3D *Utf8Buffer; + if ((TempChar1 & 0xF0) !=3D 0xE0) { + return EFI_INVALID_PARAMETER; + } + + TempChar2 =3D *(Utf8Buffer + 1); + if ((TempChar2 & 0xC0) !=3D 0x80) { + return EFI_INVALID_PARAMETER; + } + + TempChar3 =3D *(Utf8Buffer + 2); + if ((TempChar3 & 0xC0) !=3D 0x80) { + return EFI_INVALID_PARAMETER; + } + + *Ucs2Buffer =3D (TempChar2 << 6) + (TempChar3 & 0x3F); + *(Ucs2Buffer + 1) =3D (TempChar1 << 4) + ((TempChar2 >> 2) & 0x0F); + + break; + + default: + + return EFI_INVALID_PARAMETER; + } + + return EFI_SUCCESS; +} + +/** + Convert a UCS2 string to a UTF8 encoded string. + + @param[in] Ucs2Str The provided UCS2 string. + @param[out] Utf8StrAddr The converted UTF8 string address. = Caller + is responsible for Free this string. + + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES System runs out of resources. + @retval EFI_SUCCESS The UTF8 encoded string has been co= nverted. + +**/ +EFI_STATUS +UCS2StrToUTF8 ( + IN CHAR16 *Ucs2Str, + OUT CHAR8 **Utf8StrAddr + ) +{ + UINTN Ucs2StrIndex; + UINTN Ucs2StrLength; + CHAR8 *Utf8Str; + UINTN Utf8StrLength; + UINTN Utf8StrIndex; + CHAR8 Utf8Buffer[UTF8_BUFFER_FOR_UCS2_MAX_SIZE]; + UINT8 Utf8BufferSize; + + if (Ucs2Str =3D=3D NULL || Utf8StrAddr =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + Ucs2StrLength =3D StrLen (Ucs2Str); + Utf8StrLength =3D 0; + + for (Ucs2StrIndex =3D 0; Ucs2StrIndex < Ucs2StrLength; Ucs2StrIndex ++) { + + ZeroMem (Utf8Buffer, sizeof (Utf8Buffer)); + Utf8BufferSize =3D UCS2CharToUTF8 (Ucs2Str[Ucs2StrIndex], Utf8Buffer); + Utf8StrLength +=3D Utf8BufferSize; + } + + Utf8Str =3D AllocateZeroPool (Utf8StrLength + 1); + if (Utf8Str =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Utf8StrIndex =3D 0; + for (Ucs2StrIndex =3D 0; Ucs2StrIndex < Ucs2StrLength; Ucs2StrIndex ++) { + + ZeroMem (Utf8Buffer, sizeof (Utf8Buffer)); + Utf8BufferSize =3D UCS2CharToUTF8 (Ucs2Str[Ucs2StrIndex], Utf8Buffer); + + CopyMem (Utf8Str + Utf8StrIndex, Utf8Buffer, Utf8BufferSize); + Utf8StrIndex +=3D Utf8BufferSize; + } + + Utf8Str[Utf8StrIndex] =3D '\0'; + *Utf8StrAddr =3D Utf8Str; + + return EFI_SUCCESS; +} + +/** + Convert a UTF8 encoded string to a UCS2 string. + + @param[in] Utf8Str The provided UTF8 encoded string. + @param[out] Ucs2StrAddr The converted UCS2 string address. = Caller + is responsible for Free this string. + + @retval EFI_INVALID_PARAMETER The UTF8 encoded string is not vali= d to + convert to UCS2 string. + One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES System runs out of resources. + @retval EFI_SUCCESS The UCS2 string has been converted. + +**/ +EFI_STATUS +UTF8StrToUCS2 ( + IN CHAR8 *Utf8Str, + OUT CHAR16 **Ucs2StrAddr + ) +{ + EFI_STATUS Status; + UINTN Utf8StrIndex; + UINTN Utf8StrLength; + UINTN Ucs2StrIndex; + UINT8 Utf8BufferSize; + CHAR16 *Ucs2StrTemp; + + if (Utf8Str =3D=3D NULL || Ucs2StrAddr =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // It is not an Ascii string, calculate string length. + // + Utf8StrLength =3D 0; + while (*(Utf8Str + Utf8StrLength) !=3D '\0') { + Utf8StrLength ++; + } + + // + // UCS2 string shall not be longer than the UTF8 string. + // + Ucs2StrTemp =3D AllocateZeroPool ((Utf8StrLength + 1) * sizeof (CHAR16)); + if (Ucs2StrTemp =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Utf8StrIndex =3D 0; + Ucs2StrIndex =3D 0; + while (Utf8Str[Utf8StrIndex] !=3D '\0') { + + if (CompareMem (Utf8Str + Utf8StrIndex, "\\u", 2) =3D=3D 0 && + Utf8StrLength - Utf8StrIndex >=3D UNICODE_FORMAT_LEN) { + + Status =3D GetUCS2CharByFormat (Utf8Str + Utf8StrIndex, Ucs2StrTemp = + Ucs2StrIndex); + if (!EFI_ERROR (Status)) { + + Utf8StrIndex +=3D UNICODE_FORMAT_LEN; + Ucs2StrIndex ++; + } else { + + StrCpyS (Ucs2StrTemp + Ucs2StrIndex, 3, L"\\u"); + + Ucs2StrIndex +=3D 2; + Utf8StrIndex +=3D 2; + } + } else { + + Utf8BufferSize =3D GetUTF8SizeForUCS2 (Utf8Str + Utf8StrIndex); + if (Utf8BufferSize =3D=3D 0 || Utf8StrLength - Utf8StrIndex < Utf8Bu= fferSize) { + + FreePool (Ucs2StrTemp); + return EFI_INVALID_PARAMETER; + } + + Status =3D UTF8ToUCS2Char (Utf8Str + Utf8StrIndex, Ucs2StrTemp + Ucs= 2StrIndex); + if (EFI_ERROR (Status)) { + + FreePool (Ucs2StrTemp); + return EFI_INVALID_PARAMETER; + } + + Ucs2StrIndex ++; + Utf8StrIndex +=3D Utf8BufferSize; + } + } + + *Ucs2StrAddr =3D AllocateZeroPool ((Ucs2StrIndex + 1) * sizeof (CHAR16)); + if (*Ucs2StrAddr =3D=3D NULL) { + + FreePool (Ucs2StrTemp); + return EFI_OUT_OF_RESOURCES; + } + + StrCpyS (*Ucs2StrAddr, Ucs2StrIndex + 1, Ucs2StrTemp); + *(*Ucs2StrAddr + Ucs2StrIndex) =3D L'\0'; + FreePool (Ucs2StrTemp); + + return EFI_SUCCESS; +} + --=20 2.17.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#69498): https://edk2.groups.io/g/devel/message/69498 Mute This Topic: https://groups.io/mt/79286357/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-