]> git.proxmox.com Git - mirror_edk2.git/blob - FatPkg/EnhancedFatDxe/UnicodeCollation.c
Fix a typo when checking the 16-bit alignment of Unicode String.
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / UnicodeCollation.c
1 /** @file
2 Unicode Collation Library that hides the trival difference of Unicode Collation
3 and Unicode collation 2 Protocol.
4
5 Copyright (c) 2007, 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 STATIC EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollationInterface = NULL;
19
20 typedef
21 BOOLEAN
22 (* SEARCH_LANG_CODE) (
23 IN CONST CHAR8 *Languages,
24 IN CONST CHAR8 *MatchLangCode
25 );
26
27 struct _UNICODE_INTERFACE {
28 CHAR16 *VariableName;
29 CHAR8 *DefaultLangCode;
30 SEARCH_LANG_CODE SearchLangCode;
31 EFI_GUID *UnicodeProtocolGuid;
32 };
33
34 typedef struct _UNICODE_INTERFACE UNICODE_INTERFACE;
35
36 STATIC
37 BOOLEAN
38 SearchIso639LangCode (
39 IN CONST CHAR8 *Languages,
40 IN CONST CHAR8 *MatchLangCode
41 )
42 {
43 CONST CHAR8 *LangCode;
44
45 for (LangCode = Languages; *LangCode != '\0'; LangCode += 3) {
46 if (CompareMem (LangCode, MatchLangCode, 3) == 0) {
47 return TRUE;
48 }
49 }
50
51 return FALSE;
52 }
53
54 STATIC
55 BOOLEAN
56 SearchRfc3066LangCode (
57 IN CONST CHAR8 *Languages,
58 IN CONST CHAR8 *MatchLangCode
59 )
60 {
61 CHAR8 *SubStr;
62 CHAR8 Terminal;
63
64 SubStr = AsciiStrStr (Languages, MatchLangCode);
65 if (SubStr == NULL) {
66 return FALSE;
67 }
68
69 if (SubStr != Languages && *(SubStr - 1) != ';') {
70 return FALSE;
71 }
72
73 Terminal = *(SubStr + AsciiStrLen (MatchLangCode));
74 if (Terminal != '\0' && Terminal != ';') {
75 return FALSE;
76 }
77
78 return TRUE;
79 }
80
81 GLOBAL_REMOVE_IF_UNREFERENCED UNICODE_INTERFACE mIso639Lang = {
82 L"Lang",
83 (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultLang),
84 SearchIso639LangCode,
85 &gEfiUnicodeCollationProtocolGuid,
86 };
87
88 GLOBAL_REMOVE_IF_UNREFERENCED UNICODE_INTERFACE mRfc3066Lang = {
89 L"PlatformLang",
90 (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang),
91 SearchRfc3066LangCode,
92 &gEfiUnicodeCollation2ProtocolGuid,
93 };
94
95 STATIC
96 EFI_STATUS
97 InitializeUnicodeCollationSupportWithConfig (
98 IN EFI_HANDLE AgentHandle,
99 IN UNICODE_INTERFACE *UnicodeInterface
100 )
101 {
102 EFI_STATUS Status;
103 CHAR8 Buffer[100];
104 UINTN BufferSize;
105 UINTN Index;
106 CHAR8 *LangCode;
107 UINTN NoHandles;
108 EFI_HANDLE *Handles;
109 EFI_UNICODE_COLLATION_PROTOCOL *Uci;
110
111 LangCode = Buffer;
112 BufferSize = sizeof (Buffer);
113 Status = gRT->GetVariable (
114 UnicodeInterface->VariableName,
115 &gEfiGlobalVariableGuid,
116 NULL,
117 &BufferSize,
118 Buffer
119 );
120 if (EFI_ERROR (Status)) {
121 LangCode = UnicodeInterface->DefaultLangCode;
122 }
123
124 Status = gBS->LocateHandleBuffer (
125 ByProtocol,
126 UnicodeInterface->UnicodeProtocolGuid,
127 NULL,
128 &NoHandles,
129 &Handles
130 );
131 if (EFI_ERROR (Status)) {
132 return Status;
133 }
134
135 for (Index = 0; Index < NoHandles; Index++) {
136 //
137 // Open Unicode Collation Protocol
138 //
139 Status = gBS->OpenProtocol (
140 Handles[Index],
141 UnicodeInterface->UnicodeProtocolGuid,
142 (VOID **) &Uci,
143 AgentHandle,
144 NULL,
145 EFI_OPEN_PROTOCOL_GET_PROTOCOL
146 );
147 if (EFI_ERROR (Status)) {
148 continue;
149 }
150
151 if (UnicodeInterface->SearchLangCode (Uci->SupportedLanguages, LangCode)) {
152 mUnicodeCollationInterface = Uci;
153 break;
154 }
155 }
156
157 FreePool (Handles);
158
159 return (mUnicodeCollationInterface != NULL)? EFI_SUCCESS : EFI_NOT_FOUND;
160 }
161
162 /**
163 Initialize Unicode Collation support.
164
165 @param AgentHandle The handle used to open Unicode Collation (2) protocol.
166
167 @retval EFI_SUCCESS The Unicode Collation (2) protocol has been successfully located.
168 @retval Others The Unicode Collation (2) protocol has not been located.
169
170 **/
171 EFI_STATUS
172 InitializeUnicodeCollationSupport (
173 IN EFI_HANDLE AgentHandle
174 )
175 {
176
177 EFI_STATUS Status;
178
179 Status = EFI_UNSUPPORTED;
180 if (FeaturePcdGet (PcdUnicodeCollation2Support)) {
181 Status = InitializeUnicodeCollationSupportWithConfig (AgentHandle, &mRfc3066Lang);
182 }
183
184 if (FeaturePcdGet (PcdUnicodeCollationSupport) && EFI_ERROR (Status)) {
185 Status = InitializeUnicodeCollationSupportWithConfig (AgentHandle, &mIso639Lang);
186 }
187
188 return Status;
189 }
190
191 /**
192 Performs a case-insensitive comparison of two Null-terminated Unicode strings.
193
194 @param S1 A pointer to a Null-terminated Unicode string.
195 @param S2 A pointer to a Null-terminated Unicode string.
196
197 @retval 0 S1 is equivalent to S2.
198 @retval >0 S1 is lexically greater than S2.
199 @retval <0 S1 is lexically less than S2.
200 **/
201 INTN
202 FatStriCmp (
203 IN CHAR16 *S1,
204 IN CHAR16 *S2
205 )
206 {
207 ASSERT (StrSize (S1) != 0);
208 ASSERT (StrSize (S2) != 0);
209 ASSERT (mUnicodeCollationInterface != NULL);
210
211 return mUnicodeCollationInterface->StriColl (
212 mUnicodeCollationInterface,
213 S1,
214 S2
215 );
216 }
217
218
219 /**
220 Uppercase a string.
221
222 @param Str The string which will be upper-cased.
223
224 @return None.
225
226 **/
227 VOID
228 FatStrUpr (
229 IN OUT CHAR16 *String
230 )
231 {
232 ASSERT (StrSize (String) != 0);
233 ASSERT (mUnicodeCollationInterface != NULL);
234
235 mUnicodeCollationInterface->StrUpr (mUnicodeCollationInterface, String);
236 }
237
238
239 /**
240 Lowercase a string
241
242 @param Str The string which will be lower-cased.
243
244 @return None
245
246 **/
247 VOID
248 FatStrLwr (
249 IN OUT CHAR16 *String
250 )
251 {
252 ASSERT (StrSize (String) != 0);
253 ASSERT (mUnicodeCollationInterface != NULL);
254
255 mUnicodeCollationInterface->StrLwr (mUnicodeCollationInterface, String);
256 }
257
258
259 /**
260 Convert FAT string to unicode string.
261
262 @param FatSize The size of FAT string.
263 @param Fat The FAT string.
264 @param String The unicode string.
265
266 @return None.
267
268 **/
269 VOID
270 FatFatToStr (
271 IN UINTN FatSize,
272 IN CHAR8 *Fat,
273 OUT CHAR16 *String
274 )
275 {
276 ASSERT (Fat != NULL);
277 ASSERT (String != NULL);
278 ASSERT (((UINTN) String & 0x01) == 0);
279 ASSERT (mUnicodeCollationInterface != NULL);
280
281 mUnicodeCollationInterface->FatToStr (mUnicodeCollationInterface, FatSize, Fat, String);
282 }
283
284
285 /**
286 Convert unicode string to Fat string.
287
288 @param String The unicode string.
289 @param FatSize The size of the FAT string.
290 @param Fat The FAT string.
291
292 @retval TRUE Convert successfully.
293 @retval FALSE Convert error.
294
295 **/
296 BOOLEAN
297 FatStrToFat (
298 IN CHAR16 *String,
299 IN UINTN FatSize,
300 OUT CHAR8 *Fat
301 )
302 {
303 ASSERT (Fat != NULL);
304 ASSERT (StrSize (String) != 0);
305 ASSERT (mUnicodeCollationInterface != NULL);
306
307 return mUnicodeCollationInterface->StrToFat (
308 mUnicodeCollationInterface,
309 String,
310 FatSize,
311 Fat
312 );
313 }