]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - FatPkg/EnhancedFatDxe/UnicodeCollation.c
Refine code to make it more safely.
[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 - 2013, 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 Str The string which will be upper-cased.\r
193\r
194 @return None.\r
195\r
196**/\r
197VOID\r
198FatStrUpr (\r
199 IN OUT CHAR16 *String\r
200 )\r
201{\r
202 ASSERT (StrSize (String) != 0);\r
203 ASSERT (mUnicodeCollationInterface != NULL);\r
204\r
205 mUnicodeCollationInterface->StrUpr (mUnicodeCollationInterface, String);\r
206}\r
207\r
208\r
209/**\r
210 Lowercase a string\r
211\r
212 @param Str The string which will be lower-cased.\r
213\r
214 @return None\r
215\r
216**/\r
217VOID\r
218FatStrLwr (\r
219 IN OUT CHAR16 *String\r
220 )\r
221{\r
222 ASSERT (StrSize (String) != 0);\r
223 ASSERT (mUnicodeCollationInterface != NULL);\r
224\r
225 mUnicodeCollationInterface->StrLwr (mUnicodeCollationInterface, String);\r
226}\r
227\r
228\r
229/**\r
230 Convert FAT string to unicode string.\r
231\r
232 @param FatSize The size of FAT string.\r
233 @param Fat The FAT string.\r
234 @param String The unicode string.\r
235\r
236 @return None.\r
237\r
238**/\r
239VOID\r
240FatFatToStr (\r
241 IN UINTN FatSize,\r
242 IN CHAR8 *Fat,\r
243 OUT CHAR16 *String\r
244 )\r
245{\r
246 ASSERT (Fat != NULL);\r
247 ASSERT (String != NULL);\r
248 ASSERT (((UINTN) String & 0x01) == 0);\r
249 ASSERT (mUnicodeCollationInterface != NULL);\r
250\r
251 mUnicodeCollationInterface->FatToStr (mUnicodeCollationInterface, FatSize, Fat, String);\r
252}\r
253\r
254\r
255/**\r
256 Convert unicode string to Fat string.\r
257\r
258 @param String The unicode string.\r
259 @param FatSize The size of the FAT string.\r
260 @param Fat The FAT string.\r
261\r
262 @retval TRUE Convert successfully.\r
263 @retval FALSE Convert error.\r
264\r
265**/\r
266BOOLEAN\r
267FatStrToFat (\r
268 IN CHAR16 *String,\r
269 IN UINTN FatSize,\r
270 OUT CHAR8 *Fat\r
271 )\r
272{\r
273 ASSERT (Fat != NULL);\r
274 ASSERT (StrSize (String) != 0);\r
275 ASSERT (mUnicodeCollationInterface != NULL);\r
276\r
277 return mUnicodeCollationInterface->StrToFat (\r
278 mUnicodeCollationInterface,\r
279 String,\r
280 FatSize,\r
281 Fat\r
282 );\r
283}\r