]> git.proxmox.com Git - mirror_edk2.git/blame - FatPkg/EnhancedFatDxe/UnicodeCollation.c
Add EDK II Prime FatPkg New Feature: Support both Unicode Collation and Unicode Colla...
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / UnicodeCollation.c
CommitLineData
b9ec9330
QH
1/** @file\r
2 Unicode Collation Library that hides the trival difference of Unicode Collation\r
3 and Unicode collation 2 Protocol.\r
4\r
5 Copyright (c) 2007, Intel Corporation<BR>\r
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
16#include <Uefi.h>\r
17\r
18#include <Guid/GlobalVariable.h>\r
19#include <Protocol/UnicodeCollation.h>\r
20\r
21#include <Library/BaseLib.h>\r
22#include <Library/BaseMemoryLib.h>\r
23#include <Library/DebugLib.h>\r
24#include <Library/PcdLib.h>\r
25#include <Library/MemoryAllocationLib.h>\r
26#include <Library/UefiBootServicesTableLib.h>\r
27#include <Library/UefiRuntimeServicesTableLib.h>\r
28\r
29STATIC EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollationInterface = NULL;\r
30\r
31typedef\r
32BOOLEAN\r
33(* SEARCH_LANG_CODE) (\r
34 IN CONST CHAR8 *Languages,\r
35 IN CONST CHAR8 *MatchLangCode\r
36 );\r
37\r
38struct _UNICODE_INTERFACE {\r
39 CHAR16 *VariableName;\r
40 CHAR8 *DefaultLangCode;\r
41 SEARCH_LANG_CODE SearchLangCode;\r
42 EFI_GUID *UnicodeProtocolGuid;\r
43};\r
44\r
45typedef struct _UNICODE_INTERFACE UNICODE_INTERFACE;\r
46\r
47STATIC\r
48BOOLEAN\r
49SearchIso639LangCode (\r
50 IN CONST CHAR8 *Languages,\r
51 IN CONST CHAR8 *MatchLangCode\r
52 )\r
53{\r
54 CONST CHAR8 *LangCode;\r
55\r
56 for (LangCode = Languages; *LangCode != '\0'; LangCode += 3) {\r
57 if (CompareMem (LangCode, MatchLangCode, 3) == 0) {\r
58 return TRUE;\r
59 }\r
60 }\r
61\r
62 return FALSE;\r
63}\r
64\r
65STATIC\r
66BOOLEAN\r
67SearchRfc3066LangCode (\r
68 IN CONST CHAR8 *Languages,\r
69 IN CONST CHAR8 *MatchLangCode\r
70 )\r
71{\r
72 CHAR8 *SubStr;\r
73 CHAR8 Terminal;\r
74\r
75 SubStr = AsciiStrStr (Languages, MatchLangCode);\r
76 if (SubStr == NULL) {\r
77 return FALSE;\r
78 }\r
79\r
80 if (SubStr != Languages && *(SubStr - 1) != ';') {\r
81 return FALSE;\r
82 }\r
83\r
84 Terminal = *(SubStr + AsciiStrLen (MatchLangCode));\r
85 if (Terminal != '\0' && Terminal != ';') {\r
86 return FALSE;\r
87 }\r
88\r
89 return TRUE;\r
90}\r
91\r
92GLOBAL_REMOVE_IF_UNREFERENCED UNICODE_INTERFACE mIso639Lang = {\r
93 L"Lang",\r
94 (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultLang),\r
95 SearchIso639LangCode,\r
96 &gEfiUnicodeCollationProtocolGuid,\r
97};\r
98\r
99GLOBAL_REMOVE_IF_UNREFERENCED UNICODE_INTERFACE mRfc3066Lang = {\r
100 L"PlatformLang",\r
101 (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang),\r
102 SearchRfc3066LangCode,\r
103 &gEfiUnicodeCollation2ProtocolGuid,\r
104};\r
105\r
106STATIC\r
107EFI_STATUS\r
108InitializeUnicodeCollationSupportWithConfig (\r
109 IN EFI_HANDLE AgentHandle,\r
110 IN UNICODE_INTERFACE *UnicodeInterface\r
111 )\r
112{\r
113 EFI_STATUS Status;\r
114 CHAR8 Buffer[100];\r
115 UINTN BufferSize;\r
116 UINTN Index;\r
117 CHAR8 *LangCode;\r
118 UINTN NoHandles;\r
119 EFI_HANDLE *Handles;\r
120 EFI_UNICODE_COLLATION_PROTOCOL *Uci;\r
121\r
122 LangCode = Buffer;\r
123 BufferSize = sizeof (Buffer);\r
124 Status = gRT->GetVariable (\r
125 UnicodeInterface->VariableName,\r
126 &gEfiGlobalVariableGuid,\r
127 NULL,\r
128 &BufferSize,\r
129 Buffer\r
130 );\r
131 if (EFI_ERROR (Status)) {\r
132 LangCode = UnicodeInterface->DefaultLangCode;\r
133 }\r
134\r
135 Status = gBS->LocateHandleBuffer (\r
136 ByProtocol,\r
137 UnicodeInterface->UnicodeProtocolGuid,\r
138 NULL,\r
139 &NoHandles,\r
140 &Handles\r
141 );\r
142 if (EFI_ERROR (Status)) {\r
143 return Status;\r
144 }\r
145\r
146 for (Index = 0; Index < NoHandles; Index++) {\r
147 //\r
148 // Open Unicode Collation Protocol\r
149 //\r
150 Status = gBS->OpenProtocol (\r
151 Handles[Index],\r
152 UnicodeInterface->UnicodeProtocolGuid,\r
153 (VOID **) &Uci,\r
154 AgentHandle,\r
155 NULL,\r
156 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
157 );\r
158 if (EFI_ERROR (Status)) {\r
159 continue;\r
160 }\r
161\r
162 if (UnicodeInterface->SearchLangCode (Uci->SupportedLanguages, LangCode)) {\r
163 mUnicodeCollationInterface = Uci;\r
164 break;\r
165 }\r
166 }\r
167\r
168 FreePool (Handles);\r
169\r
170 return (mUnicodeCollationInterface != NULL)? EFI_SUCCESS : EFI_NOT_FOUND;\r
171}\r
172\r
173/**\r
174 Initialize Unicode Collation support.\r
175\r
176 @param AgentHandle The handle used to open Unicode Collation (2) protocol.\r
177\r
178 @retval EFI_SUCCESS The Unicode Collation (2) protocol has been successfully located.\r
179 @retval Others The Unicode Collation (2) protocol has not been located.\r
180\r
181**/\r
182EFI_STATUS\r
183InitializeUnicodeCollationSupport (\r
184 IN EFI_HANDLE AgentHandle\r
185 )\r
186{\r
187\r
188 EFI_STATUS Status;\r
189\r
190 Status = EFI_UNSUPPORTED;\r
191 if (FeaturePcdGet (PcdUnicodeCollation2Support)) {\r
192 Status = InitializeUnicodeCollationSupportWithConfig (AgentHandle, &mRfc3066Lang);\r
193 }\r
194\r
195 if (FeaturePcdGet (PcdUnicodeCollationSupport) && EFI_ERROR (Status)) {\r
196 Status = InitializeUnicodeCollationSupportWithConfig (AgentHandle, &mIso639Lang);\r
197 }\r
198\r
199 return Status;\r
200}\r
201\r
202/**\r
203 Performs a case-insensitive comparison of two Null-terminated Unicode strings.\r
204\r
205 @param S1 A pointer to a Null-terminated Unicode string.\r
206 @param S2 A pointer to a Null-terminated Unicode string.\r
207\r
208 @retval 0 S1 is equivalent to S2.\r
209 @retval >0 S1 is lexically greater than S2.\r
210 @retval <0 S1 is lexically less than S2.\r
211**/\r
212INTN\r
213FatStriCmp (\r
214 IN CHAR16 *S1,\r
215 IN CHAR16 *S2\r
216 )\r
217{\r
218 ASSERT (StrSize (S1) != 0);\r
219 ASSERT (StrSize (S2) != 0);\r
220 ASSERT (mUnicodeCollationInterface != NULL);\r
221\r
222 return mUnicodeCollationInterface->StriColl (\r
223 mUnicodeCollationInterface,\r
224 S1,\r
225 S2\r
226 );\r
227}\r
228\r
229\r
230/**\r
231 Uppercase a string.\r
232\r
233 @param Str The string which will be upper-cased.\r
234\r
235 @return None.\r
236\r
237**/\r
238VOID\r
239FatStrUpr (\r
240 IN OUT CHAR16 *String\r
241 )\r
242{\r
243 ASSERT (StrSize (String) != 0);\r
244 ASSERT (mUnicodeCollationInterface != NULL);\r
245\r
246 mUnicodeCollationInterface->StrUpr (mUnicodeCollationInterface, String);\r
247}\r
248\r
249\r
250/**\r
251 Lowercase a string\r
252\r
253 @param Str The string which will be lower-cased.\r
254\r
255 @return None\r
256\r
257**/\r
258VOID\r
259FatStrLwr (\r
260 IN OUT CHAR16 *String\r
261 )\r
262{\r
263 ASSERT (StrSize (String) != 0);\r
264 ASSERT (mUnicodeCollationInterface != NULL);\r
265\r
266 mUnicodeCollationInterface->StrLwr (mUnicodeCollationInterface, String);\r
267}\r
268\r
269\r
270/**\r
271 Convert FAT string to unicode string.\r
272\r
273 @param FatSize The size of FAT string.\r
274 @param Fat The FAT string.\r
275 @param String The unicode string.\r
276\r
277 @return None.\r
278\r
279**/\r
280VOID\r
281FatFatToStr (\r
282 IN UINTN FatSize,\r
283 IN CHAR8 *Fat,\r
284 OUT CHAR16 *String\r
285 )\r
286{\r
287 ASSERT (Fat != NULL);\r
288 ASSERT (String != NULL);\r
289 ASSERT (((UINTN) String & 0x01) != 0);\r
290 ASSERT (mUnicodeCollationInterface != NULL);\r
291\r
292 mUnicodeCollationInterface->FatToStr (mUnicodeCollationInterface, FatSize, Fat, String);\r
293}\r
294\r
295\r
296/**\r
297 Convert unicode string to Fat string.\r
298\r
299 @param String The unicode string.\r
300 @param FatSize The size of the FAT string.\r
301 @param Fat The FAT string.\r
302\r
303 @retval TRUE Convert successfully.\r
304 @retval FALSE Convert error.\r
305\r
306**/\r
307BOOLEAN\r
308FatStrToFat (\r
309 IN CHAR16 *String,\r
310 IN UINTN FatSize,\r
311 OUT CHAR8 *Fat\r
312 )\r
313{\r
314 ASSERT (Fat != NULL);\r
315 ASSERT (StrSize (String) != 0);\r
316 ASSERT (mUnicodeCollationInterface != NULL);\r
317\r
318 return mUnicodeCollationInterface->StrToFat (\r
319 mUnicodeCollationInterface,\r
320 String,\r
321 FatSize,\r
322 Fat\r
323 );\r
324}\r