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