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