]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - FatPkg/EnhancedFatDxe/UnicodeCollation.c
FatPkg: Clean up source files
[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 This program and the accompanying materials are licensed and made available\r
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
13\r
14**/\r
15\r
16#include "Fat.h"\r
17\r
18EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollationInterface = NULL;\r
19\r
20/**\r
21 Worker function to initialize Unicode Collation support.\r
22\r
23 It tries to locate Unicode Collation (2) protocol and matches it with current\r
24 platform language code.\r
25\r
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
30\r
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
33\r
34**/\r
35EFI_STATUS\r
36InitializeUnicodeCollationSupportWorker (\r
37 IN EFI_HANDLE AgentHandle,\r
38 IN EFI_GUID *ProtocolGuid,\r
39 IN CONST CHAR16 *VariableName,\r
40 IN CONST CHAR8 *DefaultLanguage\r
41 )\r
42{\r
43 EFI_STATUS ReturnStatus;\r
44 EFI_STATUS Status;\r
45 UINTN NumHandles;\r
46 UINTN Index;\r
47 EFI_HANDLE *Handles;\r
48 EFI_UNICODE_COLLATION_PROTOCOL *Uci;\r
49 BOOLEAN Iso639Language;\r
50 CHAR8 *Language;\r
51 CHAR8 *BestLanguage;\r
52\r
53 Status = gBS->LocateHandleBuffer (\r
54 ByProtocol,\r
55 ProtocolGuid,\r
56 NULL,\r
57 &NumHandles,\r
58 &Handles\r
59 );\r
60 if (EFI_ERROR (Status)) {\r
61 return Status;\r
62 }\r
63\r
64 Iso639Language = (BOOLEAN) (ProtocolGuid == &gEfiUnicodeCollationProtocolGuid);\r
65 GetEfiGlobalVariable2 (VariableName, (VOID**) &Language, NULL);\r
66\r
67 ReturnStatus = EFI_UNSUPPORTED;\r
68 for (Index = 0; Index < NumHandles; Index++) {\r
69 //\r
70 // Open Unicode Collation Protocol\r
71 //\r
72 Status = gBS->OpenProtocol (\r
73 Handles[Index],\r
74 ProtocolGuid,\r
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
84 //\r
85 // Find the best matching matching language from the supported languages\r
86 // of Unicode Collation (2) protocol.\r
87 //\r
88 BestLanguage = GetBestLanguage (\r
89 Uci->SupportedLanguages,\r
90 Iso639Language,\r
91 (Language == NULL) ? "" : Language,\r
92 DefaultLanguage,\r
93 NULL\r
94 );\r
95 if (BestLanguage != NULL) {\r
96 FreePool (BestLanguage);\r
97 mUnicodeCollationInterface = Uci;\r
98 ReturnStatus = EFI_SUCCESS;\r
99 break;\r
100 }\r
101 }\r
102\r
103 if (Language != NULL) {\r
104 FreePool (Language);\r
105 }\r
106\r
107 FreePool (Handles);\r
108\r
109 return ReturnStatus;\r
110}\r
111\r
112/**\r
113 Initialize Unicode Collation support.\r
114\r
115 It tries to locate Unicode Collation 2 protocol and matches it with current\r
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
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
134\r
135 //\r
136 // First try to use RFC 4646 Unicode Collation 2 Protocol.\r
137 //\r
138 Status = InitializeUnicodeCollationSupportWorker (\r
139 AgentHandle,\r
140 &gEfiUnicodeCollation2ProtocolGuid,\r
141 L"PlatformLang",\r
142 (CONST CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang)\r
143 );\r
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
148 if (EFI_ERROR (Status)) {\r
149 Status = InitializeUnicodeCollationSupportWorker (\r
150 AgentHandle,\r
151 &gEfiUnicodeCollationProtocolGuid,\r
152 L"Lang",\r
153 (CONST CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultLang)\r
154 );\r
155 }\r
156\r
157 return Status;\r
158}\r
159\r
160\r
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
192 @param String The string which will be upper-cased.\r
193\r
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
211 @param String The string which will be lower-cased.\r
212\r
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
246 ASSERT (((UINTN) String & 0x01) == 0);\r
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