2 Unicode Collation Library that hides the trival difference of Unicode Collation
3 and Unicode collation 2 Protocol.
5 Copyright (c) 2007, Intel Corporation<BR>
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Guid/GlobalVariable.h>
19 #include <Protocol/UnicodeCollation.h>
21 #include <Library/BaseLib.h>
22 #include <Library/BaseMemoryLib.h>
23 #include <Library/DebugLib.h>
24 #include <Library/PcdLib.h>
25 #include <Library/MemoryAllocationLib.h>
26 #include <Library/UefiBootServicesTableLib.h>
27 #include <Library/UefiRuntimeServicesTableLib.h>
29 STATIC EFI_UNICODE_COLLATION_PROTOCOL
*mUnicodeCollationInterface
= NULL
;
33 (* SEARCH_LANG_CODE
) (
34 IN CONST CHAR8
*Languages
,
35 IN CONST CHAR8
*MatchLangCode
38 struct _UNICODE_INTERFACE
{
40 CHAR8
*DefaultLangCode
;
41 SEARCH_LANG_CODE SearchLangCode
;
42 EFI_GUID
*UnicodeProtocolGuid
;
45 typedef struct _UNICODE_INTERFACE UNICODE_INTERFACE
;
49 SearchIso639LangCode (
50 IN CONST CHAR8
*Languages
,
51 IN CONST CHAR8
*MatchLangCode
54 CONST CHAR8
*LangCode
;
56 for (LangCode
= Languages
; *LangCode
!= '\0'; LangCode
+= 3) {
57 if (CompareMem (LangCode
, MatchLangCode
, 3) == 0) {
67 SearchRfc3066LangCode (
68 IN CONST CHAR8
*Languages
,
69 IN CONST CHAR8
*MatchLangCode
75 SubStr
= AsciiStrStr (Languages
, MatchLangCode
);
80 if (SubStr
!= Languages
&& *(SubStr
- 1) != ';') {
84 Terminal
= *(SubStr
+ AsciiStrLen (MatchLangCode
));
85 if (Terminal
!= '\0' && Terminal
!= ';') {
92 GLOBAL_REMOVE_IF_UNREFERENCED UNICODE_INTERFACE mIso639Lang
= {
94 (CHAR8
*) PcdGetPtr (PcdUefiVariableDefaultLang
),
96 &gEfiUnicodeCollationProtocolGuid
,
99 GLOBAL_REMOVE_IF_UNREFERENCED UNICODE_INTERFACE mRfc3066Lang
= {
101 (CHAR8
*) PcdGetPtr (PcdUefiVariableDefaultPlatformLang
),
102 SearchRfc3066LangCode
,
103 &gEfiUnicodeCollation2ProtocolGuid
,
108 InitializeUnicodeCollationSupportWithConfig (
109 IN EFI_HANDLE AgentHandle
,
110 IN UNICODE_INTERFACE
*UnicodeInterface
120 EFI_UNICODE_COLLATION_PROTOCOL
*Uci
;
123 BufferSize
= sizeof (Buffer
);
124 Status
= gRT
->GetVariable (
125 UnicodeInterface
->VariableName
,
126 &gEfiGlobalVariableGuid
,
131 if (EFI_ERROR (Status
)) {
132 LangCode
= UnicodeInterface
->DefaultLangCode
;
135 Status
= gBS
->LocateHandleBuffer (
137 UnicodeInterface
->UnicodeProtocolGuid
,
142 if (EFI_ERROR (Status
)) {
146 for (Index
= 0; Index
< NoHandles
; Index
++) {
148 // Open Unicode Collation Protocol
150 Status
= gBS
->OpenProtocol (
152 UnicodeInterface
->UnicodeProtocolGuid
,
156 EFI_OPEN_PROTOCOL_GET_PROTOCOL
158 if (EFI_ERROR (Status
)) {
162 if (UnicodeInterface
->SearchLangCode (Uci
->SupportedLanguages
, LangCode
)) {
163 mUnicodeCollationInterface
= Uci
;
170 return (mUnicodeCollationInterface
!= NULL
)? EFI_SUCCESS
: EFI_NOT_FOUND
;
174 Initialize Unicode Collation support.
176 @param AgentHandle The handle used to open Unicode Collation (2) protocol.
178 @retval EFI_SUCCESS The Unicode Collation (2) protocol has been successfully located.
179 @retval Others The Unicode Collation (2) protocol has not been located.
183 InitializeUnicodeCollationSupport (
184 IN EFI_HANDLE AgentHandle
190 Status
= EFI_UNSUPPORTED
;
191 if (FeaturePcdGet (PcdUnicodeCollation2Support
)) {
192 Status
= InitializeUnicodeCollationSupportWithConfig (AgentHandle
, &mRfc3066Lang
);
195 if (FeaturePcdGet (PcdUnicodeCollationSupport
) && EFI_ERROR (Status
)) {
196 Status
= InitializeUnicodeCollationSupportWithConfig (AgentHandle
, &mIso639Lang
);
203 Performs a case-insensitive comparison of two Null-terminated Unicode strings.
205 @param S1 A pointer to a Null-terminated Unicode string.
206 @param S2 A pointer to a Null-terminated Unicode string.
208 @retval 0 S1 is equivalent to S2.
209 @retval >0 S1 is lexically greater than S2.
210 @retval <0 S1 is lexically less than S2.
218 ASSERT (StrSize (S1
) != 0);
219 ASSERT (StrSize (S2
) != 0);
220 ASSERT (mUnicodeCollationInterface
!= NULL
);
222 return mUnicodeCollationInterface
->StriColl (
223 mUnicodeCollationInterface
,
233 @param Str The string which will be upper-cased.
240 IN OUT CHAR16
*String
243 ASSERT (StrSize (String
) != 0);
244 ASSERT (mUnicodeCollationInterface
!= NULL
);
246 mUnicodeCollationInterface
->StrUpr (mUnicodeCollationInterface
, String
);
253 @param Str The string which will be lower-cased.
260 IN OUT CHAR16
*String
263 ASSERT (StrSize (String
) != 0);
264 ASSERT (mUnicodeCollationInterface
!= NULL
);
266 mUnicodeCollationInterface
->StrLwr (mUnicodeCollationInterface
, String
);
271 Convert FAT string to unicode string.
273 @param FatSize The size of FAT string.
274 @param Fat The FAT string.
275 @param String The unicode string.
287 ASSERT (Fat
!= NULL
);
288 ASSERT (String
!= NULL
);
289 ASSERT (((UINTN
) String
& 0x01) != 0);
290 ASSERT (mUnicodeCollationInterface
!= NULL
);
292 mUnicodeCollationInterface
->FatToStr (mUnicodeCollationInterface
, FatSize
, Fat
, String
);
297 Convert unicode string to Fat string.
299 @param String The unicode string.
300 @param FatSize The size of the FAT string.
301 @param Fat The FAT string.
303 @retval TRUE Convert successfully.
304 @retval FALSE Convert error.
314 ASSERT (Fat
!= NULL
);
315 ASSERT (StrSize (String
) != 0);
316 ASSERT (mUnicodeCollationInterface
!= NULL
);
318 return mUnicodeCollationInterface
->StrToFat (
319 mUnicodeCollationInterface
,