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