From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0b-002e3701.pphosted.com (mx0b-002e3701.pphosted.com [148.163.143.35]) by mx.groups.io with SMTP id smtpd.web11.7585.1607568540657420902 for ; Wed, 09 Dec 2020 18:49:00 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@hpe.com header.s=pps0720 header.b=D2Y+d+Y+; spf=pass (domain: hpe.com, ip: 148.163.143.35, mailfrom: prvs=061307e4aa=nickle.wang@hpe.com) Received: from pps.filterd (m0134424.ppops.net [127.0.0.1]) by mx0b-002e3701.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 0BA2cqbq011913; Thu, 10 Dec 2020 02:48:56 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hpe.com; h=from : to : cc : subject : date : message-id : references : in-reply-to : content-type : content-transfer-encoding : mime-version; s=pps0720; bh=bD4JCPxjcu1x8iTnmIrLFi+Pu4LrHVyqHT9FXGQYl2A=; b=D2Y+d+Y+vIC+Q9Ep2f6A9QKwe3M4yOvfRyIuzYc7NOh3PhoNdJlY4f2aXv+BY1wuxmDy 2iUDTEBsLSrUbf/MXn3ASLbr4U9Ts10xqiNWPSTOyoqrZWpIEA/8AU/4pt6FFcX0ALgo v1yiAo8GU+W+99HYBm3OaMYn7x9NRuIIC8WbavVPaNUHfLhYwpzoh0ZSGF7inxAADYjw ACIu/rk1TtrUEzD67E2hQVf3NqSfit+CwvaG9kFYPD0fbFzZ6n65+UQF5tRjiZ3f7WoZ 8gmc/vG/QOGiA6F8PF/ofQ7o9O9rb9W4E+5LmoIBuyc8KEk2gKBr0CFbBnkQlG53dhsr SQ== Received: from g9t5009.houston.hpe.com (g9t5009.houston.hpe.com [15.241.48.73]) by mx0b-002e3701.pphosted.com with ESMTP id 35a7190ys2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 10 Dec 2020 02:48:55 +0000 Received: from G2W6311.americas.hpqcorp.net (g2w6311.austin.hp.com [16.197.64.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by g9t5009.houston.hpe.com (Postfix) with ESMTPS id E710058; Thu, 10 Dec 2020 02:48:54 +0000 (UTC) Received: from G4W9329.americas.hpqcorp.net (16.208.32.99) by G2W6311.americas.hpqcorp.net (16.197.64.53) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 10 Dec 2020 02:48:54 +0000 Received: from G4W10204.americas.hpqcorp.net (2002:10cf:5210::10cf:5210) by G4W9329.americas.hpqcorp.net (2002:10d0:2063::10d0:2063) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 10 Dec 2020 02:48:54 +0000 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (15.241.52.12) by G4W10204.americas.hpqcorp.net (16.207.82.16) with Microsoft SMTP Server (TLS) id 15.0.1497.2 via Frontend Transport; Thu, 10 Dec 2020 02:48:54 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PBR01qJfscj3vF31TD/cUmdoE5muzFkJ3/ZtonGo9kE9xARO883N3Znfl5hEm2Wh6kpR2I7FBXwkAU2x8FM+7wg2DgJGjH1Yzlrj21iqHqBqNcD1Rhr9+sAhSIVNHLYlL/7qnGwybxkD2fgY1dcYP+mSUoRSSASDzVQVQT1Ty9N1dtRjHIa/Jo3GE1D4UV3Le5ViXeTZAm+f5z2NkJKA8hBoQWhME3S/97boE/GSS6bnC3tXFsMJn0Q2k/++RP2pYwkmKYLs8rKaTdiVUSFM0djjLa3G3Dhps4/2WQP6/l5+btgCajiXwM7rEOvwsETHXvK2uaQfLTZ6bZ2TNd/sWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=bD4JCPxjcu1x8iTnmIrLFi+Pu4LrHVyqHT9FXGQYl2A=; b=ACrAehVtXPLPM+LzgcD5Owy+6r/SFMB/LD9vxOA5KuLubcJBjRppc9k7YbQr1IoGh77TLVESmpJ1y1UtVDKry6oS0G12v2/JEEZ6oxWzaGr9kFVoUJk4qbYxE936VPcwWviokUq9/viH4y/eGD+WIMVxJ/0HEgMBJd7TdvjlrfGrt6AO6zhCTYOoYev7kTvv5epshPWaMZNKXQtWPN8pRMdqILdQzMrg8Qo75nzBjdEp1bn/P6X9Tle6PJcO6pX8wQvM8ZqxtKfE0mNTOJltHtqd24n9wehjN7lUvTnePhh6HDGmtjgG0a2nFJzEEIe1fre15hWCE2I+TY3YoRXVUw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=hpe.com; dmarc=pass action=none header.from=hpe.com; dkim=pass header.d=hpe.com; arc=none Received: from DF4PR8401MB0812.NAMPRD84.PROD.OUTLOOK.COM (2a01:111:e400:760d::7) by DF4PR8401MB1195.NAMPRD84.PROD.OUTLOOK.COM (2a01:111:e400:760e::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3632.21; Thu, 10 Dec 2020 02:48:50 +0000 Received: from DF4PR8401MB0812.NAMPRD84.PROD.OUTLOOK.COM ([fe80::e475:6a2b:ed1c:a060]) by DF4PR8401MB0812.NAMPRD84.PROD.OUTLOOK.COM ([fe80::e475:6a2b:ed1c:a060%11]) with mapi id 15.20.3654.014; Thu, 10 Dec 2020 02:48:50 +0000 From: "Nickle Wang" To: "Chang, Abner (HPS SW/FW Technologist)" , "devel@edk2.groups.io" CC: Liming Gao , Leif Lindholm , "O'Hanley, Peter (EXL)" Subject: Re: [PATCH v5 1/6] RedfishPkg/Ucs2Utf8lib: UCS2 to UFT8 manipulation library Thread-Topic: [PATCH v5 1/6] RedfishPkg/Ucs2Utf8lib: UCS2 to UFT8 manipulation library Thread-Index: AQHWzQ3pcMFKUum4KkSWDflOlPZ/2qnvo9Zg Date: Thu, 10 Dec 2020 02:48:50 +0000 Message-ID: References: <20201208021048.9104-1-abner.chang@hpe.com> <20201208021048.9104-2-abner.chang@hpe.com> In-Reply-To: <20201208021048.9104-2-abner.chang@hpe.com> Accept-Language: zh-TW, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: hpe.com; dkim=none (message not signed) header.d=none;hpe.com; dmarc=none action=none header.from=hpe.com; x-originating-ip: [2001:b011:5c04:5ee2:852e:ddde:5093:2eac] x-ms-publictraffictype: Email x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: befeb4ca-2413-4f6f-2d9d-08d89cb6200d x-ms-traffictypediagnostic: DF4PR8401MB1195: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:529; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: /Gd8ivjAy0NK1o3Isnk0+2T4ibdX/oy4x55MRWnnU34+1ppzggjb6eycG6Zm03Gf7ZDEKIOYRxnV6FlTMG0lPy/3ze4xlQSIKxnfzCXBe/UtaYyIoWZO3wTzWeIwg2+s1nXU1JINorVYQoQDfyKm0Y0cfHTeGs6XLG8WAKOodkx3kzJ8cpdc5iTDcSE7ghGIsUOArlRrkiRieRffBZ8xZY47/XH4vVjMZtNdEzM4xY+L6Gl5b2ZDD1+cdA+M/bCg7JJtxusuqqAENFBwuQxjfexpknX3m2YDhgR+iWeQQ47InRctMsVbP/JRNOiImXUK x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DF4PR8401MB0812.NAMPRD84.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(376002)(366004)(136003)(346002)(2906002)(86362001)(7696005)(186003)(4326008)(6506007)(53546011)(508600001)(55016002)(110136005)(8936002)(30864003)(8676002)(83380400001)(9686003)(33656002)(54906003)(66946007)(71200400001)(66446008)(66476007)(76116006)(66556008)(52536014)(64756008)(5660300002)(19627235002);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata: =?us-ascii?Q?5uDWhOGqm7hNyA5Uv8MMHQZufVECJnK26A+fXFSP6lfWqsbztUg2BcaFdgIq?= =?us-ascii?Q?Tp/Y/hHsoedGlanQvgYB/HwzsUjjEjZ9Pe54lyIdjqdyL5YdZA7VI4BhRYHN?= =?us-ascii?Q?5tQMaOvXYrOTil+VAhAbxnaLS2UuM+COFyfl2EezIJAePBEltbLifH+tqHB9?= =?us-ascii?Q?vjePQk08O9TmEMq3FDOl/5c43oO0Yh3TqzFj6XOdF4OsQTPyYG2XF3vNE+RD?= =?us-ascii?Q?+F3Y6I+JbQZOD/OnfP81jdPnsR8AljK3pdeNNgUNyWC+jO40iL130jfYsSYz?= =?us-ascii?Q?3jdYR0hcVs+z/vQyduFfNIzL7uvD4P/K8QW5vjvTjB0FDQbbj1VRA/3Vz4qK?= =?us-ascii?Q?ERPlzD/3WgRETilWh//PxrRfZv+Vim76nAvwH7OPszMasZfpNzv/Fgw/zibO?= =?us-ascii?Q?Ji0HBI7zyt/q3VVv8zwVPYeLYeS/EFFavbACpJtlfDgU8MbrdTWzwxpfwlWx?= =?us-ascii?Q?ddkWkNWs2OicHk+GlQXYQW+/R0BCer18Nt5OMtrSAGKNol1rhxxVKUsdroHG?= =?us-ascii?Q?AkHtYP4NX8YsbKxBqwsX8gONiu1akw+3Ta1DW7z/JJ4HVy1Iie8s5wq9VSQ7?= =?us-ascii?Q?2epdV+yJQea71g9gt/G1Hskir3NE++AERhL39o6P1kF4r4inL1weYvJ0pmJ5?= =?us-ascii?Q?R+LJ7zBRhtFi4hG5LZPCPENxSzBzI6uGyXeuNB8DIvBSM+SE7TekYezJAwXH?= =?us-ascii?Q?o9Ck0vi/nDTf6eI6ek7iYXl+E1XsKM9dnFhJ9n6/tPiJnU02R+Vy3Ri/z3JN?= =?us-ascii?Q?eTKIrH7cZEiMkfk+yF7vnINsa2VOsQ7TNccG+OsgbGVM5xjlSUdqHfccIShM?= =?us-ascii?Q?Q9C5V64TXaD99bXbCTTm4R819eLY0g28XHh6dgaeiyY43PId5XbQUna0c0Mi?= =?us-ascii?Q?AZP0AqP/T3H1phpmU7aVtjaxDi0SGYkqnxOSYg35QZjfs9Qm0Ly0C8tm2WBZ?= =?us-ascii?Q?unw8j2b4eDuOEJQXDYjSFhXu9dsXES5b684Tm6RaQz+TXzSQ9HOQDevcrJnc?= =?us-ascii?Q?g4YxcJeSoBN9g7pgIocWQdmCuM6G9xeNzDLuUG50wB70wtQ=3D?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: DF4PR8401MB0812.NAMPRD84.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-Network-Message-Id: befeb4ca-2413-4f6f-2d9d-08d89cb6200d X-MS-Exchange-CrossTenant-originalarrivaltime: 10 Dec 2020 02:48:50.7461 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 105b2061-b669-4b31-92ac-24d304d195dc X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: LNXDtEv7SBRQD5B+lH9TgGWcLhQeJ70ngfxOyDxek9PPLsAmQb70y0mKEmtg4b3wdBGdL92yThp/t4GR61OuCA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DF4PR8401MB1195 X-OriginatorOrg: hpe.com X-HPE-SCL: -1 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.343,18.0.737 definitions=2020-12-09_19:2020-12-09,2020-12-09 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 suspectscore=0 adultscore=0 priorityscore=1501 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 lowpriorityscore=0 phishscore=0 impostorscore=0 clxscore=1011 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2012100018 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Nickle Wang > -----Original Message----- > From: Chang, Abner (HPS SW/FW Technologist) > Sent: Tuesday, December 8, 2020 10:11 AM > To: devel@edk2.groups.io > Cc: Liming Gao ; Leif Lindholm > ; Wang, Nickle (HPS SW) ; > O'Hanley, Peter (EXL) > Subject: [PATCH v5 1/6] RedfishPkg/Ucs2Utf8lib: UCS2 to UFT8 manipulation > library >=20 > 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. >=20 > Signed-off-by: Abner Chang >=20 > Cc: Liming Gao > Cc: Leif Lindholm > Cc: Nickle Wang > Cc: Peter O'Hanley > --- > RedfishPkg/Include/Library/BaseUcs2Utf8Lib.h | 69 +++ > .../Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.c | 417 ++++++++++++++++++ > .../BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.inf | 31 ++ > RedfishPkg/RedfishLibs.dsc.inc | 1 + > RedfishPkg/RedfishPkg.dec | 4 + > RedfishPkg/RedfishPkg.dsc | 1 + > 6 files changed, 523 insertions(+) > create mode 100644 RedfishPkg/Include/Library/BaseUcs2Utf8Lib.h > create mode 100644 RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.c > create mode 100644 > RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.inf >=20 > diff --git a/RedfishPkg/Include/Library/BaseUcs2Utf8Lib.h > b/RedfishPkg/Include/Library/BaseUcs2Utf8Lib.h > new file mode 100644 > index 0000000000..51ae924929 > --- /dev/null > +++ b/RedfishPkg/Include/Library/BaseUcs2Utf8Lib.h > @@ -0,0 +1,69 @@ > +/** @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_ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +/// > +/// 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 stri= ng. > + > + @retval EFI_INVALID_PARAMETER One or more parameters are invali= d. > + @retval EFI_OUT_OF_RESOURCES System runs out of resources. > + @retval EFI_SUCCESS The UTF8 encoded string has been > converted. > + > +**/ > +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 stri= ng. > + > + @retval EFI_INVALID_PARAMETER The UTF8 encoded string is not va= lid > to > + convert to UCS2 string. > + One or more parameters are invali= d. > + @retval EFI_OUT_OF_RESOURCES System runs out of resources. > + @retval EFI_SUCCESS The UCS2 string has been converte= d. > + > +**/ > +EFI_STATUS > +UTF8StrToUCS2 ( > + IN CHAR8 *Utf8Str, > + OUT CHAR16 **Ucs2StrAddr > + ); > + > +#endif > diff --git a/RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.c > b/RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.c > new file mode 100644 > index 0000000000..6cff109a5c > --- /dev/null > +++ b/RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.c > @@ -0,0 +1,417 @@ > +/** @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 > + > +/** > + Since each UCS2 character can be represented by 1-3 UTF8 encoded > characters, > + this function is used to retrieve the UTF8 encoding size for a UCS2 > character. > + > + @param[in] Utf8Buffer The buffer for UTF8 encoded data. > + > + @retval Return the size of UTF8 encoding string or 0 if it is not= for > + 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, th= is > 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 buffe= r. > + > + @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 > hexadecimal > + digits string, and can't be conve= rted to a UCS2 > + character. > + @retval EFI_SUCCESS The UCS2 character has been retri= eved. > + > +**/ > +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 chara= cter. > + > +**/ > +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 va= lid > 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 stri= ng. > + > + @retval EFI_INVALID_PARAMETER One or more parameters are invali= d. > + @retval EFI_OUT_OF_RESOURCES System runs out of resources. > + @retval EFI_SUCCESS The UTF8 encoded string has been > converted. > + > +**/ > +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 stri= ng. > + > + @retval EFI_INVALID_PARAMETER The UTF8 encoded string is not va= lid > to > + convert to UCS2 string. > + One or more parameters are invali= d. > + @retval EFI_OUT_OF_RESOURCES System runs out of resources. > + @retval EFI_SUCCESS The UCS2 string has been converte= d. > + > +**/ > +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, Ucs2StrTem= p + > 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 < Utf8= BufferSize) > { > + > + FreePool (Ucs2StrTemp); > + return EFI_INVALID_PARAMETER; > + } > + > + Status =3D UTF8ToUCS2Char (Utf8Str + Utf8StrIndex, Ucs2StrTemp + > Ucs2StrIndex); > + 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; > +} > + > diff --git a/RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.inf > b/RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.inf > new file mode 100644 > index 0000000000..ccd02ee320 > --- /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-CDE1A33B525= 6 > + MODULE_TYPE =3D BASE > + VERSION_STRING =3D 1.0 > + LIBRARY_CLASS =3D Ucs2Utf8Lib > + > +# > +# VALID_ARCHITECTURES =3D IA32 X64 ARM AARCH64 RISCV64 > +# > + > +[Sources.common] > + BaseUcs2Utf8Lib.c > + > +[Packages] > + MdePkg/MdePkg.dec > + MdeModulePkg/MdeModulePkg.dec > + RedfishPkg/RedfishPkg.dec > + > + > diff --git a/RedfishPkg/RedfishLibs.dsc.inc b/RedfishPkg/RedfishLibs.dsc.= inc > index e780b5c270..d4c08e18ac 100644 > --- a/RedfishPkg/RedfishLibs.dsc.inc > +++ b/RedfishPkg/RedfishLibs.dsc.inc > @@ -11,5 +11,6 @@ > # > ## > !if $(REDFISH_ENABLE) =3D=3D TRUE > + Ucs2Utf8Lib|RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.inf > !endif >=20 > diff --git a/RedfishPkg/RedfishPkg.dec b/RedfishPkg/RedfishPkg.dec > index b38e9b4789..aecc1f4a3a 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. >=20 > RedfishPlatformHostInterfaceLib|Include/Library/RedfishHostInterfaceLib.h > + ## @libraryclass This library provides UCS2 to UTF8 manipulation > + # functions. > + # > + Ucs2Utf8Lib|Include/Library/BaseUcs2Utf8Lib.h >=20 > [Protocols] > ## Include/Protocol/RedfishDiscover.h > diff --git a/RedfishPkg/RedfishPkg.dsc b/RedfishPkg/RedfishPkg.dsc > index 94e7127bc6..d198a94b73 100644 > --- a/RedfishPkg/RedfishPkg.dsc > +++ b/RedfishPkg/RedfishPkg.dsc > @@ -43,5 +43,6 @@ >=20 > [Components] >=20 > RedfishPkg/Library/PlatformHostInterfaceLibNull/PlatformHostInterfaceLibN > ull.inf > + RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.inf >=20 > !include RedfishPkg/Redfish.dsc.inc > -- > 2.17.1