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