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