X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=FatPkg%2FEnhancedFatDxe%2FUnicodeCollation.c;h=9b22e05bc320f4846fab394fbf4ad8a23bee0220;hb=a7ef158b07524f9afd0cefa3180aeac0fcb6e436;hp=d74dc1e6977d8932f2e7dc295693a504717ac1d8;hpb=b9ec93308b33bcb0bb37d6213a76c3fed0b5bc0b;p=mirror_edk2.git diff --git a/FatPkg/EnhancedFatDxe/UnicodeCollation.c b/FatPkg/EnhancedFatDxe/UnicodeCollation.c index d74dc1e697..9b22e05bc3 100644 --- a/FatPkg/EnhancedFatDxe/UnicodeCollation.c +++ b/FatPkg/EnhancedFatDxe/UnicodeCollation.c @@ -1,155 +1,71 @@ /** @file - Unicode Collation Library that hides the trival difference of Unicode Collation + Unicode Collation Support component that hides the trivial difference of Unicode Collation and Unicode collation 2 Protocol. - Copyright (c) 2007, Intel Corporation
- All rights reserved. This program and the accompanying materials - are licensed and made available under the terms and conditions of the BSD License - which accompanies this distribution. The full text of the license may be found at - http://opensource.org/licenses/bsd-license.php - - THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, - WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent **/ -#include - -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -STATIC EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollationInterface = NULL; - -typedef -BOOLEAN -(* SEARCH_LANG_CODE) ( - IN CONST CHAR8 *Languages, - IN CONST CHAR8 *MatchLangCode - ); - -struct _UNICODE_INTERFACE { - CHAR16 *VariableName; - CHAR8 *DefaultLangCode; - SEARCH_LANG_CODE SearchLangCode; - EFI_GUID *UnicodeProtocolGuid; -}; - -typedef struct _UNICODE_INTERFACE UNICODE_INTERFACE; - -STATIC -BOOLEAN -SearchIso639LangCode ( - IN CONST CHAR8 *Languages, - IN CONST CHAR8 *MatchLangCode - ) -{ - CONST CHAR8 *LangCode; - - for (LangCode = Languages; *LangCode != '\0'; LangCode += 3) { - if (CompareMem (LangCode, MatchLangCode, 3) == 0) { - return TRUE; - } - } +#include "Fat.h" - return FALSE; -} +EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollationInterface = NULL; -STATIC -BOOLEAN -SearchRfc3066LangCode ( - IN CONST CHAR8 *Languages, - IN CONST CHAR8 *MatchLangCode - ) -{ - CHAR8 *SubStr; - CHAR8 Terminal; - - SubStr = AsciiStrStr (Languages, MatchLangCode); - if (SubStr == NULL) { - return FALSE; - } +/** + Worker function to initialize Unicode Collation support. - if (SubStr != Languages && *(SubStr - 1) != ';') { - return FALSE; - } + It tries to locate Unicode Collation (2) protocol and matches it with current + platform language code. - Terminal = *(SubStr + AsciiStrLen (MatchLangCode)); - if (Terminal != '\0' && Terminal != ';') { - return FALSE; - } + @param AgentHandle The handle used to open Unicode Collation (2) protocol. + @param ProtocolGuid The pointer to Unicode Collation (2) protocol GUID. + @param VariableName The name of the RFC 4646 or ISO 639-2 language variable. + @param DefaultLanguage The default language in case the RFC 4646 or ISO 639-2 language is absent. - return TRUE; -} + @retval EFI_SUCCESS The Unicode Collation (2) protocol has been successfully located. + @retval Others The Unicode Collation (2) protocol has not been located. -GLOBAL_REMOVE_IF_UNREFERENCED UNICODE_INTERFACE mIso639Lang = { - L"Lang", - (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultLang), - SearchIso639LangCode, - &gEfiUnicodeCollationProtocolGuid, -}; - -GLOBAL_REMOVE_IF_UNREFERENCED UNICODE_INTERFACE mRfc3066Lang = { - L"PlatformLang", - (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang), - SearchRfc3066LangCode, - &gEfiUnicodeCollation2ProtocolGuid, -}; - -STATIC +**/ EFI_STATUS -InitializeUnicodeCollationSupportWithConfig ( +InitializeUnicodeCollationSupportWorker ( IN EFI_HANDLE AgentHandle, - IN UNICODE_INTERFACE *UnicodeInterface + IN EFI_GUID *ProtocolGuid, + IN CONST CHAR16 *VariableName, + IN CONST CHAR8 *DefaultLanguage ) { + EFI_STATUS ReturnStatus; EFI_STATUS Status; - CHAR8 Buffer[100]; - UINTN BufferSize; + UINTN NumHandles; UINTN Index; - CHAR8 *LangCode; - UINTN NoHandles; EFI_HANDLE *Handles; EFI_UNICODE_COLLATION_PROTOCOL *Uci; - - LangCode = Buffer; - BufferSize = sizeof (Buffer); - Status = gRT->GetVariable ( - UnicodeInterface->VariableName, - &gEfiGlobalVariableGuid, - NULL, - &BufferSize, - Buffer - ); - if (EFI_ERROR (Status)) { - LangCode = UnicodeInterface->DefaultLangCode; - } + BOOLEAN Iso639Language; + CHAR8 *Language; + CHAR8 *BestLanguage; Status = gBS->LocateHandleBuffer ( ByProtocol, - UnicodeInterface->UnicodeProtocolGuid, + ProtocolGuid, NULL, - &NoHandles, + &NumHandles, &Handles ); if (EFI_ERROR (Status)) { return Status; } - for (Index = 0; Index < NoHandles; Index++) { + Iso639Language = (BOOLEAN) (ProtocolGuid == &gEfiUnicodeCollationProtocolGuid); + GetEfiGlobalVariable2 (VariableName, (VOID**) &Language, NULL); + + ReturnStatus = EFI_UNSUPPORTED; + for (Index = 0; Index < NumHandles; Index++) { // // Open Unicode Collation Protocol // Status = gBS->OpenProtocol ( Handles[Index], - UnicodeInterface->UnicodeProtocolGuid, + ProtocolGuid, (VOID **) &Uci, AgentHandle, NULL, @@ -159,20 +75,41 @@ InitializeUnicodeCollationSupportWithConfig ( continue; } - if (UnicodeInterface->SearchLangCode (Uci->SupportedLanguages, LangCode)) { + // + // Find the best matching matching language from the supported languages + // of Unicode Collation (2) protocol. + // + BestLanguage = GetBestLanguage ( + Uci->SupportedLanguages, + Iso639Language, + (Language == NULL) ? "" : Language, + DefaultLanguage, + NULL + ); + if (BestLanguage != NULL) { + FreePool (BestLanguage); mUnicodeCollationInterface = Uci; + ReturnStatus = EFI_SUCCESS; break; } } + if (Language != NULL) { + FreePool (Language); + } + FreePool (Handles); - return (mUnicodeCollationInterface != NULL)? EFI_SUCCESS : EFI_NOT_FOUND; + return ReturnStatus; } /** Initialize Unicode Collation support. + It tries to locate Unicode Collation 2 protocol and matches it with current + platform language code. If for any reason the first attempt fails, it then tries to + use Unicode Collation Protocol. + @param AgentHandle The handle used to open Unicode Collation (2) protocol. @retval EFI_SUCCESS The Unicode Collation (2) protocol has been successfully located. @@ -188,17 +125,33 @@ InitializeUnicodeCollationSupport ( EFI_STATUS Status; Status = EFI_UNSUPPORTED; - if (FeaturePcdGet (PcdUnicodeCollation2Support)) { - Status = InitializeUnicodeCollationSupportWithConfig (AgentHandle, &mRfc3066Lang); - } - if (FeaturePcdGet (PcdUnicodeCollationSupport) && EFI_ERROR (Status)) { - Status = InitializeUnicodeCollationSupportWithConfig (AgentHandle, &mIso639Lang); + // + // First try to use RFC 4646 Unicode Collation 2 Protocol. + // + Status = InitializeUnicodeCollationSupportWorker ( + AgentHandle, + &gEfiUnicodeCollation2ProtocolGuid, + L"PlatformLang", + (CONST CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang) + ); + // + // If the attempt to use Unicode Collation 2 Protocol fails, then we fall back + // on the ISO 639-2 Unicode Collation Protocol. + // + if (EFI_ERROR (Status)) { + Status = InitializeUnicodeCollationSupportWorker ( + AgentHandle, + &gEfiUnicodeCollationProtocolGuid, + L"Lang", + (CONST CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultLang) + ); } return Status; } + /** Performs a case-insensitive comparison of two Null-terminated Unicode strings. @@ -230,9 +183,8 @@ FatStriCmp ( /** Uppercase a string. - @param Str The string which will be upper-cased. + @param String The string which will be upper-cased. - @return None. **/ VOID @@ -250,9 +202,8 @@ FatStrUpr ( /** Lowercase a string - @param Str The string which will be lower-cased. + @param String The string which will be lower-cased. - @return None **/ VOID @@ -286,7 +237,7 @@ FatFatToStr ( { ASSERT (Fat != NULL); ASSERT (String != NULL); - ASSERT (((UINTN) String & 0x01) != 0); + ASSERT (((UINTN) String & 0x01) == 0); ASSERT (mUnicodeCollationInterface != NULL); mUnicodeCollationInterface->FatToStr (mUnicodeCollationInterface, FatSize, Fat, String);