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