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