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