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