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