]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - FatPkg/EnhancedFatDxe/UnicodeCollation.c
Maintainers.txt: Remove Yonghong from BaseTools Reviewer
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / UnicodeCollation.c
... / ...
CommitLineData
1/** @file\r
2 Unicode Collation Support component that hides the trivial difference of Unicode Collation\r
3 and Unicode collation 2 Protocol.\r
4\r
5 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
7\r
8**/\r
9\r
10#include "Fat.h"\r
11\r
12EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollationInterface = NULL;\r
13\r
14/**\r
15 Worker function to initialize Unicode Collation support.\r
16\r
17 It tries to locate Unicode Collation (2) protocol and matches it with current\r
18 platform language code.\r
19\r
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
24\r
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
27\r
28**/\r
29EFI_STATUS\r
30InitializeUnicodeCollationSupportWorker (\r
31 IN EFI_HANDLE AgentHandle,\r
32 IN EFI_GUID *ProtocolGuid,\r
33 IN CONST CHAR16 *VariableName,\r
34 IN CONST CHAR8 *DefaultLanguage\r
35 )\r
36{\r
37 EFI_STATUS ReturnStatus;\r
38 EFI_STATUS Status;\r
39 UINTN NumHandles;\r
40 UINTN Index;\r
41 EFI_HANDLE *Handles;\r
42 EFI_UNICODE_COLLATION_PROTOCOL *Uci;\r
43 BOOLEAN Iso639Language;\r
44 CHAR8 *Language;\r
45 CHAR8 *BestLanguage;\r
46\r
47 Status = gBS->LocateHandleBuffer (\r
48 ByProtocol,\r
49 ProtocolGuid,\r
50 NULL,\r
51 &NumHandles,\r
52 &Handles\r
53 );\r
54 if (EFI_ERROR (Status)) {\r
55 return Status;\r
56 }\r
57\r
58 Iso639Language = (BOOLEAN) (ProtocolGuid == &gEfiUnicodeCollationProtocolGuid);\r
59 GetEfiGlobalVariable2 (VariableName, (VOID**) &Language, NULL);\r
60\r
61 ReturnStatus = EFI_UNSUPPORTED;\r
62 for (Index = 0; Index < NumHandles; Index++) {\r
63 //\r
64 // Open Unicode Collation Protocol\r
65 //\r
66 Status = gBS->OpenProtocol (\r
67 Handles[Index],\r
68 ProtocolGuid,\r
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
78 //\r
79 // Find the best matching matching language from the supported languages\r
80 // of Unicode Collation (2) protocol.\r
81 //\r
82 BestLanguage = GetBestLanguage (\r
83 Uci->SupportedLanguages,\r
84 Iso639Language,\r
85 (Language == NULL) ? "" : Language,\r
86 DefaultLanguage,\r
87 NULL\r
88 );\r
89 if (BestLanguage != NULL) {\r
90 FreePool (BestLanguage);\r
91 mUnicodeCollationInterface = Uci;\r
92 ReturnStatus = EFI_SUCCESS;\r
93 break;\r
94 }\r
95 }\r
96\r
97 if (Language != NULL) {\r
98 FreePool (Language);\r
99 }\r
100\r
101 FreePool (Handles);\r
102\r
103 return ReturnStatus;\r
104}\r
105\r
106/**\r
107 Initialize Unicode Collation support.\r
108\r
109 It tries to locate Unicode Collation 2 protocol and matches it with current\r
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
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
128\r
129 //\r
130 // First try to use RFC 4646 Unicode Collation 2 Protocol.\r
131 //\r
132 Status = InitializeUnicodeCollationSupportWorker (\r
133 AgentHandle,\r
134 &gEfiUnicodeCollation2ProtocolGuid,\r
135 L"PlatformLang",\r
136 (CONST CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang)\r
137 );\r
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
142 if (EFI_ERROR (Status)) {\r
143 Status = InitializeUnicodeCollationSupportWorker (\r
144 AgentHandle,\r
145 &gEfiUnicodeCollationProtocolGuid,\r
146 L"Lang",\r
147 (CONST CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultLang)\r
148 );\r
149 }\r
150\r
151 return Status;\r
152}\r
153\r
154\r
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
186 @param String The string which will be upper-cased.\r
187\r
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
205 @param String The string which will be lower-cased.\r
206\r
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
240 ASSERT (((UINTN) String & 0x01) == 0);\r
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