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