]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Disk/UnicodeCollation/EnglishDxe/UnicodeCollationEng.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Universal / Disk / UnicodeCollation / EnglishDxe / UnicodeCollationEng.c
CommitLineData
f42be642 1/** @file\r
61e33430 2 Driver to implement English version of Unicode Collation Protocol.\r
adbcbf8f 3\r
d1102dba 4Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
9d510e61 5SPDX-License-Identifier: BSD-2-Clause-Patent\r
adbcbf8f 6\r
f42be642 7**/\r
adbcbf8f 8\r
9\r
10#include "UnicodeCollationEng.h"\r
11\r
48557c65 12CHAR8 mEngUpperMap[MAP_TABLE_SIZE];\r
13CHAR8 mEngLowerMap[MAP_TABLE_SIZE];\r
14CHAR8 mEngInfoMap[MAP_TABLE_SIZE];\r
adbcbf8f 15\r
16CHAR8 mOtherChars[] = {\r
17 '0',\r
18 '1',\r
19 '2',\r
20 '3',\r
21 '4',\r
22 '5',\r
23 '6',\r
24 '7',\r
25 '8',\r
26 '9',\r
27 '\\',\r
28 '.',\r
29 '_',\r
30 '^',\r
31 '$',\r
32 '~',\r
33 '!',\r
34 '#',\r
35 '%',\r
36 '&',\r
37 '-',\r
38 '{',\r
39 '}',\r
40 '(',\r
41 ')',\r
42 '@',\r
43 '`',\r
44 '\'',\r
45 '\0'\r
46};\r
47\r
ff61847d 48EFI_HANDLE mHandle = NULL;\r
adbcbf8f 49\r
2d9a7522 50//\r
51// EFI Unicode Collation Protocol supporting ISO 639-2 language code\r
52//\r
53GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_COLLATION_PROTOCOL UnicodeEng = {\r
adbcbf8f 54 EngStriColl,\r
55 EngMetaiMatch,\r
56 EngStrLwr,\r
57 EngStrUpr,\r
58 EngFatToStr,\r
59 EngStrToFat,\r
60 "eng"\r
61};\r
62\r
63//\r
0254efc0 64// EFI Unicode Collation2 Protocol supporting RFC 4646 language code\r
adbcbf8f 65//\r
2d9a7522 66GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_COLLATION_PROTOCOL Unicode2Eng = {\r
67 EngStriColl,\r
68 EngMetaiMatch,\r
69 EngStrLwr,\r
70 EngStrUpr,\r
71 EngFatToStr,\r
72 EngStrToFat,\r
73 "en"\r
74};\r
75\r
61e33430 76/**\r
77 The user Entry Point for English module.\r
d1102dba 78\r
61e33430 79 This function initializes unicode character mapping and then installs Unicode\r
d1102dba 80 Collation & Unicode Collation 2 Protocols based on the feature flags.\r
61e33430 81\r
d1102dba 82 @param ImageHandle The firmware allocated handle for the EFI image.\r
61e33430 83 @param SystemTable A pointer to the EFI System Table.\r
d1102dba 84\r
61e33430 85 @retval EFI_SUCCESS The entry point is executed successfully.\r
86 @retval other Some error occurs when executing this entry point.\r
87\r
88**/\r
adbcbf8f 89EFI_STATUS\r
2d9a7522 90EFIAPI\r
adbcbf8f 91InitializeUnicodeCollationEng (\r
92 IN EFI_HANDLE ImageHandle,\r
93 IN EFI_SYSTEM_TABLE *SystemTable\r
94 )\r
adbcbf8f 95{\r
2d9a7522 96 EFI_STATUS Status;\r
adbcbf8f 97 UINTN Index;\r
98 UINTN Index2;\r
99\r
100 //\r
101 // Initialize mapping tables for the supported languages\r
102 //\r
48557c65 103 for (Index = 0; Index < MAP_TABLE_SIZE; Index++) {\r
adbcbf8f 104 mEngUpperMap[Index] = (CHAR8) Index;\r
105 mEngLowerMap[Index] = (CHAR8) Index;\r
106 mEngInfoMap[Index] = 0;\r
107\r
108 if ((Index >= 'a' && Index <= 'z') || (Index >= 0xe0 && Index <= 0xf6) || (Index >= 0xf8 && Index <= 0xfe)) {\r
109\r
110 Index2 = Index - 0x20;\r
111 mEngUpperMap[Index] = (CHAR8) Index2;\r
112 mEngLowerMap[Index2] = (CHAR8) Index;\r
113\r
114 mEngInfoMap[Index] |= CHAR_FAT_VALID;\r
115 mEngInfoMap[Index2] |= CHAR_FAT_VALID;\r
116 }\r
117 }\r
118\r
ea7cb08c 119 for (Index = 0; mOtherChars[Index] != 0; Index++) {\r
adbcbf8f 120 Index2 = mOtherChars[Index];\r
121 mEngInfoMap[Index2] |= CHAR_FAT_VALID;\r
122 }\r
2d9a7522 123\r
124 if (FeaturePcdGet (PcdUnicodeCollation2Support)) {\r
125 if (FeaturePcdGet (PcdUnicodeCollationSupport)) {\r
126 Status = gBS->InstallMultipleProtocolInterfaces (\r
127 &mHandle,\r
128 &gEfiUnicodeCollationProtocolGuid,\r
129 &UnicodeEng,\r
130 &gEfiUnicodeCollation2ProtocolGuid,\r
131 &Unicode2Eng,\r
132 NULL\r
133 );\r
134 ASSERT_EFI_ERROR (Status);\r
135 } else {\r
136 Status = gBS->InstallMultipleProtocolInterfaces (\r
137 &mHandle,\r
138 &gEfiUnicodeCollation2ProtocolGuid,\r
139 &Unicode2Eng,\r
140 NULL\r
141 );\r
142 ASSERT_EFI_ERROR (Status);\r
143 }\r
144 } else {\r
145 if (FeaturePcdGet (PcdUnicodeCollationSupport)) {\r
146 Status = gBS->InstallMultipleProtocolInterfaces (\r
147 &mHandle,\r
148 &gEfiUnicodeCollationProtocolGuid,\r
149 &UnicodeEng,\r
150 NULL\r
151 );\r
152 ASSERT_EFI_ERROR (Status);\r
153 } else {\r
154 //\r
155 // This module must support to produce at least one of Unicode Collation Protocol\r
156 // and Unicode Collation 2 Protocol.\r
157 //\r
158 ASSERT (FALSE);\r
159 Status = EFI_UNSUPPORTED;\r
160 }\r
161 }\r
162\r
163 return Status;\r
adbcbf8f 164}\r
165\r
61e33430 166\r
167/**\r
4f077902 168 Performs a case-insensitive comparison of two Null-terminated strings.\r
61e33430 169\r
170 @param This Protocol instance pointer.\r
4f077902
SZ
171 @param Str1 A pointer to a Null-terminated string.\r
172 @param Str2 A pointer to a Null-terminated string.\r
61e33430 173\r
174 @retval 0 Str1 is equivalent to Str2\r
175 @retval > 0 Str1 is lexically greater than Str2\r
176 @retval < 0 Str1 is lexically less than Str2\r
177\r
178**/\r
adbcbf8f 179INTN\r
180EFIAPI\r
181EngStriColl (\r
182 IN EFI_UNICODE_COLLATION_PROTOCOL *This,\r
61e33430 183 IN CHAR16 *Str1,\r
184 IN CHAR16 *Str2\r
adbcbf8f 185 )\r
adbcbf8f 186{\r
ea7cb08c 187 while (*Str1 != 0) {\r
188 if (TO_UPPER (*Str1) != TO_UPPER (*Str2)) {\r
adbcbf8f 189 break;\r
190 }\r
191\r
61e33430 192 Str1 += 1;\r
193 Str2 += 1;\r
adbcbf8f 194 }\r
195\r
ea7cb08c 196 return TO_UPPER (*Str1) - TO_UPPER (*Str2);\r
adbcbf8f 197}\r
198\r
61e33430 199\r
200/**\r
d1102dba 201 Converts all the characters in a Null-terminated string to\r
4f077902 202 lower case characters.\r
61e33430 203\r
204 @param This Protocol instance pointer.\r
4f077902 205 @param Str A pointer to a Null-terminated string.\r
61e33430 206\r
207**/\r
adbcbf8f 208VOID\r
209EFIAPI\r
210EngStrLwr (\r
211 IN EFI_UNICODE_COLLATION_PROTOCOL *This,\r
212 IN OUT CHAR16 *Str\r
213 )\r
adbcbf8f 214{\r
ea7cb08c 215 while (*Str != 0) {\r
216 *Str = TO_LOWER (*Str);\r
adbcbf8f 217 Str += 1;\r
218 }\r
219}\r
220\r
61e33430 221\r
222/**\r
4f077902
SZ
223 Converts all the characters in a Null-terminated string to upper\r
224 case characters.\r
61e33430 225\r
226 @param This Protocol instance pointer.\r
4f077902 227 @param Str A pointer to a Null-terminated string.\r
61e33430 228\r
229**/\r
adbcbf8f 230VOID\r
231EFIAPI\r
232EngStrUpr (\r
233 IN EFI_UNICODE_COLLATION_PROTOCOL *This,\r
234 IN OUT CHAR16 *Str\r
235 )\r
adbcbf8f 236{\r
ea7cb08c 237 while (*Str != 0) {\r
238 *Str = TO_UPPER (*Str);\r
adbcbf8f 239 Str += 1;\r
240 }\r
241}\r
242\r
61e33430 243/**\r
4f077902
SZ
244 Performs a case-insensitive comparison of a Null-terminated\r
245 pattern string and a Null-terminated string.\r
61e33430 246\r
247 @param This Protocol instance pointer.\r
4f077902
SZ
248 @param String A pointer to a Null-terminated string.\r
249 @param Pattern A pointer to a Null-terminated pattern string.\r
61e33430 250\r
251 @retval TRUE Pattern was found in String.\r
252 @retval FALSE Pattern was not found in String.\r
253\r
254**/\r
adbcbf8f 255BOOLEAN\r
256EFIAPI\r
257EngMetaiMatch (\r
258 IN EFI_UNICODE_COLLATION_PROTOCOL *This,\r
259 IN CHAR16 *String,\r
260 IN CHAR16 *Pattern\r
261 )\r
adbcbf8f 262{\r
263 CHAR16 CharC;\r
264 CHAR16 CharP;\r
265 CHAR16 Index3;\r
266\r
267 for (;;) {\r
268 CharP = *Pattern;\r
269 Pattern += 1;\r
270\r
271 switch (CharP) {\r
272 case 0:\r
273 //\r
274 // End of pattern. If end of string, TRUE match\r
275 //\r
ea7cb08c 276 if (*String != 0) {\r
adbcbf8f 277 return FALSE;\r
278 } else {\r
279 return TRUE;\r
280 }\r
281\r
282 case '*':\r
283 //\r
284 // Match zero or more chars\r
285 //\r
ea7cb08c 286 while (*String != 0) {\r
adbcbf8f 287 if (EngMetaiMatch (This, String, Pattern)) {\r
288 return TRUE;\r
289 }\r
290\r
291 String += 1;\r
292 }\r
293\r
294 return EngMetaiMatch (This, String, Pattern);\r
295\r
296 case '?':\r
297 //\r
298 // Match any one char\r
299 //\r
ea7cb08c 300 if (*String == 0) {\r
adbcbf8f 301 return FALSE;\r
302 }\r
303\r
304 String += 1;\r
305 break;\r
306\r
307 case '[':\r
308 //\r
309 // Match char set\r
310 //\r
311 CharC = *String;\r
ea7cb08c 312 if (CharC == 0) {\r
adbcbf8f 313 //\r
314 // syntax problem\r
315 //\r
316 return FALSE;\r
317 }\r
318\r
319 Index3 = 0;\r
320 CharP = *Pattern++;\r
ea7cb08c 321 while (CharP != 0) {\r
adbcbf8f 322 if (CharP == ']') {\r
323 return FALSE;\r
324 }\r
325\r
326 if (CharP == '-') {\r
327 //\r
328 // if range of chars, get high range\r
329 //\r
330 CharP = *Pattern;\r
331 if (CharP == 0 || CharP == ']') {\r
332 //\r
333 // syntax problem\r
334 //\r
335 return FALSE;\r
336 }\r
337\r
ea7cb08c 338 if (TO_UPPER (CharC) >= TO_UPPER (Index3) && TO_UPPER (CharC) <= TO_UPPER (CharP)) {\r
adbcbf8f 339 //\r
340 // if in range, it's a match\r
341 //\r
342 break;\r
343 }\r
344 }\r
345\r
346 Index3 = CharP;\r
ea7cb08c 347 if (TO_UPPER (CharC) == TO_UPPER (CharP)) {\r
adbcbf8f 348 //\r
349 // if char matches\r
350 //\r
351 break;\r
352 }\r
353\r
354 CharP = *Pattern++;\r
355 }\r
356 //\r
357 // skip to end of match char set\r
358 //\r
ea7cb08c 359 while ((CharP != 0) && (CharP != ']')) {\r
adbcbf8f 360 CharP = *Pattern;\r
361 Pattern += 1;\r
362 }\r
363\r
364 String += 1;\r
365 break;\r
366\r
367 default:\r
368 CharC = *String;\r
ea7cb08c 369 if (TO_UPPER (CharC) != TO_UPPER (CharP)) {\r
adbcbf8f 370 return FALSE;\r
371 }\r
372\r
373 String += 1;\r
374 break;\r
375 }\r
376 }\r
377}\r
378\r
61e33430 379\r
380/**\r
4f077902 381 Converts an 8.3 FAT file name in an OEM character set to a Null-terminated string.\r
61e33430 382\r
383 @param This Protocol instance pointer.\r
384 @param FatSize The size of the string Fat in bytes.\r
385 @param Fat A pointer to a Null-terminated string that contains an 8.3 file\r
4f077902
SZ
386 name using an 8-bit OEM character set.\r
387 @param String A pointer to a Null-terminated string. The string must\r
388 be preallocated to hold FatSize characters.\r
61e33430 389\r
390**/\r
adbcbf8f 391VOID\r
392EFIAPI\r
393EngFatToStr (\r
394 IN EFI_UNICODE_COLLATION_PROTOCOL *This,\r
395 IN UINTN FatSize,\r
396 IN CHAR8 *Fat,\r
397 OUT CHAR16 *String\r
398 )\r
adbcbf8f 399{\r
400 //\r
401 // No DBCS issues, just expand and add null terminate to end of string\r
402 //\r
ea7cb08c 403 while ((*Fat != 0) && (FatSize != 0)) {\r
adbcbf8f 404 *String = *Fat;\r
405 String += 1;\r
406 Fat += 1;\r
407 FatSize -= 1;\r
408 }\r
409\r
410 *String = 0;\r
411}\r
412\r
61e33430 413\r
414/**\r
d1102dba
LG
415 Converts a Null-terminated string to legal characters in a FAT\r
416 filename using an OEM character set.\r
61e33430 417\r
418 @param This Protocol instance pointer.\r
4f077902
SZ
419 @param String A pointer to a Null-terminated string. The string must\r
420 be preallocated to hold FatSize characters.\r
61e33430 421 @param FatSize The size of the string Fat in bytes.\r
422 @param Fat A pointer to a Null-terminated string that contains an 8.3 file\r
423 name using an OEM character set.\r
424\r
425 @retval TRUE Fat is a Long File Name\r
426 @retval FALSE Fat is an 8.3 file name\r
427\r
428**/\r
adbcbf8f 429BOOLEAN\r
430EFIAPI\r
431EngStrToFat (\r
432 IN EFI_UNICODE_COLLATION_PROTOCOL *This,\r
433 IN CHAR16 *String,\r
434 IN UINTN FatSize,\r
435 OUT CHAR8 *Fat\r
436 )\r
adbcbf8f 437{\r
438 BOOLEAN SpecialCharExist;\r
439\r
440 SpecialCharExist = FALSE;\r
ea7cb08c 441 while ((*String != 0) && (FatSize != 0)) {\r
adbcbf8f 442 //\r
443 // Skip '.' or ' ' when making a fat name\r
444 //\r
445 if (*String != '.' && *String != ' ') {\r
446 //\r
447 // If this is a valid fat char, move it.\r
48557c65 448 // Otherwise, move a '_' and flag the fact that the name needs a long file name.\r
adbcbf8f 449 //\r
48557c65 450 if (*String < MAP_TABLE_SIZE && ((mEngInfoMap[*String] & CHAR_FAT_VALID) != 0)) {\r
adbcbf8f 451 *Fat = mEngUpperMap[*String];\r
452 } else {\r
453 *Fat = '_';\r
454 SpecialCharExist = TRUE;\r
455 }\r
456\r
457 Fat += 1;\r
458 FatSize -= 1;\r
459 }\r
460\r
461 String += 1;\r
462 }\r
463 //\r
464 // Do not terminate that fat string\r
465 //\r
466 return SpecialCharExist;\r
467}\r