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