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