]> git.proxmox.com Git - mirror_edk2.git/blame - FatPkg/EnhancedFatDxe/UnicodeCollation.c
BaseTools: Library hashing fix and optimization for --hash feature
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / UnicodeCollation.c
CommitLineData
b9ec9330 1/** @file\r
b2477ca4 2 Unicode Collation Support component that hides the trivial difference of Unicode Collation\r
b9ec9330
QH
3 and Unicode collation 2 Protocol.\r
4\r
e38f26a2 5 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
eb6cb4ce 6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
b9ec9330
QH
7\r
8**/\r
9\r
657e3612 10#include "Fat.h"\r
b9ec9330 11\r
b2477ca4 12EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollationInterface = NULL;\r
b9ec9330 13\r
b2477ca4
QH
14/**\r
15 Worker function to initialize Unicode Collation support.\r
b9ec9330 16\r
5779282d
QH
17 It tries to locate Unicode Collation (2) protocol and matches it with current\r
18 platform language code.\r
b9ec9330 19\r
b2477ca4
QH
20 @param AgentHandle The handle used to open Unicode Collation (2) protocol.\r
21 @param ProtocolGuid The pointer to Unicode Collation (2) protocol GUID.\r
22 @param VariableName The name of the RFC 4646 or ISO 639-2 language variable.\r
23 @param DefaultLanguage The default language in case the RFC 4646 or ISO 639-2 language is absent.\r
b9ec9330 24\r
b2477ca4
QH
25 @retval EFI_SUCCESS The Unicode Collation (2) protocol has been successfully located.\r
26 @retval Others The Unicode Collation (2) protocol has not been located.\r
b9ec9330 27\r
b2477ca4 28**/\r
b9ec9330 29EFI_STATUS\r
b2477ca4 30InitializeUnicodeCollationSupportWorker (\r
b9ec9330 31 IN EFI_HANDLE AgentHandle,\r
b2477ca4
QH
32 IN EFI_GUID *ProtocolGuid,\r
33 IN CONST CHAR16 *VariableName,\r
34 IN CONST CHAR8 *DefaultLanguage\r
b9ec9330
QH
35 )\r
36{\r
4a30cd67 37 EFI_STATUS ReturnStatus;\r
b9ec9330 38 EFI_STATUS Status;\r
b2477ca4 39 UINTN NumHandles;\r
b9ec9330 40 UINTN Index;\r
b9ec9330
QH
41 EFI_HANDLE *Handles;\r
42 EFI_UNICODE_COLLATION_PROTOCOL *Uci;\r
b2477ca4
QH
43 BOOLEAN Iso639Language;\r
44 CHAR8 *Language;\r
45 CHAR8 *BestLanguage;\r
b9ec9330
QH
46\r
47 Status = gBS->LocateHandleBuffer (\r
48 ByProtocol,\r
b2477ca4 49 ProtocolGuid,\r
b9ec9330 50 NULL,\r
b2477ca4 51 &NumHandles,\r
b9ec9330
QH
52 &Handles\r
53 );\r
54 if (EFI_ERROR (Status)) {\r
55 return Status;\r
56 }\r
57\r
b2477ca4 58 Iso639Language = (BOOLEAN) (ProtocolGuid == &gEfiUnicodeCollationProtocolGuid);\r
01951d1b 59 GetEfiGlobalVariable2 (VariableName, (VOID**) &Language, NULL);\r
b2477ca4 60\r
4a30cd67 61 ReturnStatus = EFI_UNSUPPORTED;\r
b2477ca4 62 for (Index = 0; Index < NumHandles; Index++) {\r
b9ec9330
QH
63 //\r
64 // Open Unicode Collation Protocol\r
65 //\r
66 Status = gBS->OpenProtocol (\r
67 Handles[Index],\r
b2477ca4 68 ProtocolGuid,\r
b9ec9330
QH
69 (VOID **) &Uci,\r
70 AgentHandle,\r
71 NULL,\r
72 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
73 );\r
74 if (EFI_ERROR (Status)) {\r
75 continue;\r
76 }\r
77\r
b2477ca4
QH
78 //\r
79 // Find the best matching matching language from the supported languages\r
e38f26a2 80 // of Unicode Collation (2) protocol.\r
b2477ca4 81 //\r
2cd1716d
QH
82 BestLanguage = GetBestLanguage (\r
83 Uci->SupportedLanguages,\r
84 Iso639Language,\r
dcc3386b 85 (Language == NULL) ? "" : Language,\r
2cd1716d
QH
86 DefaultLanguage,\r
87 NULL\r
88 );\r
b2477ca4
QH
89 if (BestLanguage != NULL) {\r
90 FreePool (BestLanguage);\r
b9ec9330 91 mUnicodeCollationInterface = Uci;\r
4a30cd67 92 ReturnStatus = EFI_SUCCESS;\r
b9ec9330
QH
93 break;\r
94 }\r
95 }\r
96\r
b2477ca4
QH
97 if (Language != NULL) {\r
98 FreePool (Language);\r
99 }\r
100\r
b9ec9330
QH
101 FreePool (Handles);\r
102\r
4a30cd67 103 return ReturnStatus;\r
b9ec9330
QH
104}\r
105\r
106/**\r
107 Initialize Unicode Collation support.\r
108\r
5779282d 109 It tries to locate Unicode Collation 2 protocol and matches it with current\r
1f7de705
QH
110 platform language code. If for any reason the first attempt fails, it then tries to\r
111 use Unicode Collation Protocol.\r
112\r
b9ec9330
QH
113 @param AgentHandle The handle used to open Unicode Collation (2) protocol.\r
114\r
115 @retval EFI_SUCCESS The Unicode Collation (2) protocol has been successfully located.\r
116 @retval Others The Unicode Collation (2) protocol has not been located.\r
117\r
118**/\r
119EFI_STATUS\r
120InitializeUnicodeCollationSupport (\r
121 IN EFI_HANDLE AgentHandle\r
122 )\r
123{\r
124\r
125 EFI_STATUS Status;\r
126\r
127 Status = EFI_UNSUPPORTED;\r
1f7de705
QH
128\r
129 //\r
b2477ca4 130 // First try to use RFC 4646 Unicode Collation 2 Protocol.\r
1f7de705 131 //\r
5779282d
QH
132 Status = InitializeUnicodeCollationSupportWorker (\r
133 AgentHandle,\r
134 &gEfiUnicodeCollation2ProtocolGuid,\r
135 L"PlatformLang",\r
136 (CONST CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang)\r
137 );\r
1f7de705
QH
138 //\r
139 // If the attempt to use Unicode Collation 2 Protocol fails, then we fall back\r
140 // on the ISO 639-2 Unicode Collation Protocol.\r
141 //\r
5779282d 142 if (EFI_ERROR (Status)) {\r
b2477ca4
QH
143 Status = InitializeUnicodeCollationSupportWorker (\r
144 AgentHandle,\r
145 &gEfiUnicodeCollationProtocolGuid,\r
146 L"Lang",\r
147 (CONST CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultLang)\r
148 );\r
b9ec9330
QH
149 }\r
150\r
151 return Status;\r
152}\r
153\r
b2477ca4 154\r
b9ec9330
QH
155/**\r
156 Performs a case-insensitive comparison of two Null-terminated Unicode strings.\r
157\r
158 @param S1 A pointer to a Null-terminated Unicode string.\r
159 @param S2 A pointer to a Null-terminated Unicode string.\r
160\r
161 @retval 0 S1 is equivalent to S2.\r
162 @retval >0 S1 is lexically greater than S2.\r
163 @retval <0 S1 is lexically less than S2.\r
164**/\r
165INTN\r
166FatStriCmp (\r
167 IN CHAR16 *S1,\r
168 IN CHAR16 *S2\r
169 )\r
170{\r
171 ASSERT (StrSize (S1) != 0);\r
172 ASSERT (StrSize (S2) != 0);\r
173 ASSERT (mUnicodeCollationInterface != NULL);\r
174\r
175 return mUnicodeCollationInterface->StriColl (\r
176 mUnicodeCollationInterface,\r
177 S1,\r
178 S2\r
179 );\r
180}\r
181\r
182\r
183/**\r
184 Uppercase a string.\r
185\r
cae7420b 186 @param String The string which will be upper-cased.\r
b9ec9330 187\r
b9ec9330
QH
188\r
189**/\r
190VOID\r
191FatStrUpr (\r
192 IN OUT CHAR16 *String\r
193 )\r
194{\r
195 ASSERT (StrSize (String) != 0);\r
196 ASSERT (mUnicodeCollationInterface != NULL);\r
197\r
198 mUnicodeCollationInterface->StrUpr (mUnicodeCollationInterface, String);\r
199}\r
200\r
201\r
202/**\r
203 Lowercase a string\r
204\r
cae7420b 205 @param String The string which will be lower-cased.\r
b9ec9330 206\r
b9ec9330
QH
207\r
208**/\r
209VOID\r
210FatStrLwr (\r
211 IN OUT CHAR16 *String\r
212 )\r
213{\r
214 ASSERT (StrSize (String) != 0);\r
215 ASSERT (mUnicodeCollationInterface != NULL);\r
216\r
217 mUnicodeCollationInterface->StrLwr (mUnicodeCollationInterface, String);\r
218}\r
219\r
220\r
221/**\r
222 Convert FAT string to unicode string.\r
223\r
224 @param FatSize The size of FAT string.\r
225 @param Fat The FAT string.\r
226 @param String The unicode string.\r
227\r
228 @return None.\r
229\r
230**/\r
231VOID\r
232FatFatToStr (\r
233 IN UINTN FatSize,\r
234 IN CHAR8 *Fat,\r
235 OUT CHAR16 *String\r
236 )\r
237{\r
238 ASSERT (Fat != NULL);\r
239 ASSERT (String != NULL);\r
8288dd3e 240 ASSERT (((UINTN) String & 0x01) == 0);\r
b9ec9330
QH
241 ASSERT (mUnicodeCollationInterface != NULL);\r
242\r
243 mUnicodeCollationInterface->FatToStr (mUnicodeCollationInterface, FatSize, Fat, String);\r
244}\r
245\r
246\r
247/**\r
248 Convert unicode string to Fat string.\r
249\r
250 @param String The unicode string.\r
251 @param FatSize The size of the FAT string.\r
252 @param Fat The FAT string.\r
253\r
254 @retval TRUE Convert successfully.\r
255 @retval FALSE Convert error.\r
256\r
257**/\r
258BOOLEAN\r
259FatStrToFat (\r
260 IN CHAR16 *String,\r
261 IN UINTN FatSize,\r
262 OUT CHAR8 *Fat\r
263 )\r
264{\r
265 ASSERT (Fat != NULL);\r
266 ASSERT (StrSize (String) != 0);\r
267 ASSERT (mUnicodeCollationInterface != NULL);\r
268\r
269 return mUnicodeCollationInterface->StrToFat (\r
270 mUnicodeCollationInterface,\r
271 String,\r
272 FatSize,\r
273 Fat\r
274 );\r
275}\r