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