]>
Commit | Line | Data |
---|---|---|
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 | |
4a30cd67 | 5 | Copyright (c) 2007 - 2013, Intel Corporation. All rights reserved.<BR>\r |
6163cc98 | 6 | This program and the accompanying materials are licensed and made available\r |
c80aae77 QH |
7 | under the terms and conditions of the BSD License which accompanies this\r |
8 | 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 | |
b9ec9330 QH |
13 | \r |
14 | **/\r | |
15 | \r | |
657e3612 | 16 | #include "Fat.h"\r |
b9ec9330 | 17 | \r |
b2477ca4 | 18 | EFI_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 | 35 | EFI_STATUS\r |
b2477ca4 | 36 | InitializeUnicodeCollationSupportWorker (\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 | |
86 | // of Unicode Collation (2) protocol. \r | |
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 | |
125 | EFI_STATUS\r | |
126 | InitializeUnicodeCollationSupport (\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 | |
171 | INTN\r | |
172 | FatStriCmp (\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 | |
197 | VOID\r | |
198 | FatStrUpr (\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 | |
217 | VOID\r | |
218 | FatStrLwr (\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 | |
239 | VOID\r | |
240 | FatFatToStr (\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 | |
8288dd3e | 248 | ASSERT (((UINTN) String & 0x01) == 0);\r |
b9ec9330 QH |
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 | |
266 | BOOLEAN\r | |
267 | FatStrToFat (\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 |