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