Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/modules/Libefi/efiString.c

1/** @file
2 Unicode and ASCII string primatives.
3
4 Copyright (c) 2006 - 2010, 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#include <efi.h>
16
17/**
18 Copies one Null-terminated Unicode string to another Null-terminated Unicode
19 string and returns the new Unicode string.
20
21 This function copies the contents of the Unicode string Source to the Unicode
22 string Destination, and returns Destination. If Source and Destination
23 overlap, then the results are undefined.
24
25 If Destination is NULL, then ASSERT().
26 If Destination is not aligned on a 16-bit boundary, then ASSERT().
27 If Source is NULL, then ASSERT().
28 If Source is not aligned on a 16-bit boundary, then ASSERT().
29 If Source and Destination overlap, then ASSERT().
30 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
31 PcdMaximumUnicodeStringLength Unicode characters, not including the
32 Null-terminator, then ASSERT().
33
34 @param Destination A pointer to a Null-terminated Unicode string.
35 @param Source A pointer to a Null-terminated Unicode string.
36
37 @return Destination.
38
39**/
40CHAR16 *
41StrCpy (
42 OUT CHAR16 *Destination,
43 IN CONST CHAR16 *Source
44 )
45{
46 CHAR16 *ReturnValue;
47
48 //
49 // Destination cannot be NULL
50 //
51 ASSERT (Destination != NULL);
52 ASSERT (((UINTN) Destination & BIT0) == 0);
53
54 //
55 // Destination and source cannot overlap
56 //
57 ASSERT ((UINTN)(Destination - Source) > StrLen (Source));
58 ASSERT ((UINTN)(Source - Destination) > StrLen (Source));
59
60 ReturnValue = Destination;
61 while (*Source != 0) {
62 *(Destination++) = *(Source++);
63 }
64 *Destination = 0;
65 return ReturnValue;
66}
67
68/**
69 Copies up to a specified length from one Null-terminated Unicode string to
70 another Null-terminated Unicode string and returns the new Unicode string.
71
72 This function copies the contents of the Unicode string Source to the Unicode
73 string Destination, and returns Destination. At most, Length Unicode
74 characters are copied from Source to Destination. If Length is 0, then
75 Destination is returned unmodified. If Length is greater that the number of
76 Unicode characters in Source, then Destination is padded with Null Unicode
77 characters. If Source and Destination overlap, then the results are
78 undefined.
79
80 If Length > 0 and Destination is NULL, then ASSERT().
81 If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().
82 If Length > 0 and Source is NULL, then ASSERT().
83 If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().
84 If Source and Destination overlap, then ASSERT().
85 If PcdMaximumUnicodeStringLength is not zero, and Length is greater than
86 PcdMaximumUnicodeStringLength, then ASSERT().
87 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
88 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
89 then ASSERT().
90
91 @param Destination A pointer to a Null-terminated Unicode string.
92 @param Source A pointer to a Null-terminated Unicode string.
93 @param Length The maximum number of Unicode characters to copy.
94
95 @return Destination.
96
97**/
98CHAR16 *
99StrnCpy (
100 OUT CHAR16 *Destination,
101 IN CONST CHAR16 *Source,
102 IN UINTN Length
103 )
104{
105 CHAR16 *ReturnValue;
106
107 if (Length == 0) {
108 return Destination;
109 }
110
111 //
112 // Destination cannot be NULL if Length is not zero
113 //
114 ASSERT (Destination != NULL);
115 ASSERT (((UINTN) Destination & BIT0) == 0);
116
117 //
118 // Destination and source cannot overlap
119 //
120 ASSERT ((UINTN)(Destination - Source) > StrLen (Source));
121 ASSERT ((UINTN)(Source - Destination) >= Length);
122
123 if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {
124 ASSERT (Length <= PcdGet32 (PcdMaximumUnicodeStringLength));
125 }
126
127 ReturnValue = Destination;
128
129 while ((*Source != L'\0') && (Length > 0)) {
130 *(Destination++) = *(Source++);
131 Length--;
132 }
133
134 ZeroMem (Destination, Length * sizeof (*Destination));
135 return ReturnValue;
136}
137
138/**
139 Returns the length of a Null-terminated Unicode string.
140
141 This function returns the number of Unicode characters in the Null-terminated
142 Unicode string specified by String.
143
144 If String is NULL, then ASSERT().
145 If String is not aligned on a 16-bit boundary, then ASSERT().
146 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
147 PcdMaximumUnicodeStringLength Unicode characters, not including the
148 Null-terminator, then ASSERT().
149
150 @param String A pointer to a Null-terminated Unicode string.
151
152 @return The length of String.
153
154**/
155UINTN
156StrLen (
157 IN CONST CHAR16 *String
158 )
159{
160 UINTN Length;
161
162 ASSERT (String != NULL);
163 //ASSERT (((UINTN) String & BIT0) == 0);
164
165 for (Length = 0; *String != L'\0'; String++, Length++) {
166 //
167 // If PcdMaximumUnicodeStringLength is not zero,
168 // length should not more than PcdMaximumUnicodeStringLength
169 //
170 if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {
171 ASSERT (Length < PcdGet32 (PcdMaximumUnicodeStringLength));
172 }
173 }
174 return Length;
175}
176
177/**
178 Returns the size of a Null-terminated Unicode string in bytes, including the
179 Null terminator.
180
181 This function returns the size, in bytes, of the Null-terminated Unicode string
182 specified by String.
183
184 If String is NULL, then ASSERT().
185 If String is not aligned on a 16-bit boundary, then ASSERT().
186 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
187 PcdMaximumUnicodeStringLength Unicode characters, not including the
188 Null-terminator, then ASSERT().
189
190 @param String A pointer to a Null-terminated Unicode string.
191
192 @return The size of String.
193
194**/
195UINTN
196StrSize (
197 IN CONST CHAR16 *String
198 )
199{
200 return (StrLen (String) + 1) * sizeof (*String);
201}
202
203/**
204 Compares two Null-terminated Unicode strings, and returns the difference
205 between the first mismatched Unicode characters.
206
207 This function compares the Null-terminated Unicode string FirstString to the
208 Null-terminated Unicode string SecondString. If FirstString is identical to
209 SecondString, then 0 is returned. Otherwise, the value returned is the first
210 mismatched Unicode character in SecondString subtracted from the first
211 mismatched Unicode character in FirstString.
212
213 If FirstString is NULL, then ASSERT().
214 If FirstString is not aligned on a 16-bit boundary, then ASSERT().
215 If SecondString is NULL, then ASSERT().
216 If SecondString is not aligned on a 16-bit boundary, then ASSERT().
217 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more
218 than PcdMaximumUnicodeStringLength Unicode characters, not including the
219 Null-terminator, then ASSERT().
220 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more
221 than PcdMaximumUnicodeStringLength Unicode characters, not including the
222 Null-terminator, then ASSERT().
223
224 @param FirstString A pointer to a Null-terminated Unicode string.
225 @param SecondString A pointer to a Null-terminated Unicode string.
226
227 @retval 0 FirstString is identical to SecondString.
228 @return others FirstString is not identical to SecondString.
229
230**/
231INTN
232StrCmp (
233 IN CONST CHAR16 *FirstString,
234 IN CONST CHAR16 *SecondString
235 )
236{
237 //
238 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength
239 //
240 ASSERT (StrSize (FirstString) != 0);
241 ASSERT (StrSize (SecondString) != 0);
242
243 while ((*FirstString != L'\0') && (*FirstString == *SecondString)) {
244 FirstString++;
245 SecondString++;
246 }
247 return *FirstString - *SecondString;
248}
249
250/**
251 Compares up to a specified length the contents of two Null-terminated Unicode strings,
252 and returns the difference between the first mismatched Unicode characters.
253
254 This function compares the Null-terminated Unicode string FirstString to the
255 Null-terminated Unicode string SecondString. At most, Length Unicode
256 characters will be compared. If Length is 0, then 0 is returned. If
257 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
258 value returned is the first mismatched Unicode character in SecondString
259 subtracted from the first mismatched Unicode character in FirstString.
260
261 If Length > 0 and FirstString is NULL, then ASSERT().
262 If Length > 0 and FirstString is not aligned on a 16-bit boundary, then ASSERT().
263 If Length > 0 and SecondString is NULL, then ASSERT().
264 If Length > 0 and SecondString is not aligned on a 16-bit boundary, then ASSERT().
265 If PcdMaximumUnicodeStringLength is not zero, and Length is greater than
266 PcdMaximumUnicodeStringLength, then ASSERT().
267 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more than
268 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
269 then ASSERT().
270 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more than
271 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
272 then ASSERT().
273
274 @param FirstString A pointer to a Null-terminated Unicode string.
275 @param SecondString A pointer to a Null-terminated Unicode string.
276 @param Length The maximum number of Unicode characters to compare.
277
278 @retval 0 FirstString is identical to SecondString.
279 @return others FirstString is not identical to SecondString.
280
281**/
282INTN
283StrnCmp (
284 IN CONST CHAR16 *FirstString,
285 IN CONST CHAR16 *SecondString,
286 IN UINTN Length
287 )
288{
289 if (Length == 0) {
290 return 0;
291 }
292
293 //
294 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
295 // Length tests are performed inside StrLen().
296 //
297 ASSERT (StrSize (FirstString) != 0);
298 ASSERT (StrSize (SecondString) != 0);
299
300 if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {
301 ASSERT (Length <= PcdGet32 (PcdMaximumUnicodeStringLength));
302 }
303
304 while ((*FirstString != L'\0') &&
305 (*FirstString == *SecondString) &&
306 (Length > 1)) {
307 FirstString++;
308 SecondString++;
309 Length--;
310 }
311
312 return *FirstString - *SecondString;
313}
314
315/**
316 Concatenates one Null-terminated Unicode string to another Null-terminated
317 Unicode string, and returns the concatenated Unicode string.
318
319 This function concatenates two Null-terminated Unicode strings. The contents
320 of Null-terminated Unicode string Source are concatenated to the end of
321 Null-terminated Unicode string Destination. The Null-terminated concatenated
322 Unicode String is returned. If Source and Destination overlap, then the
323 results are undefined.
324
325 If Destination is NULL, then ASSERT().
326 If Destination is not aligned on a 16-bit boundary, then ASSERT().
327 If Source is NULL, then ASSERT().
328 If Source is not aligned on a 16-bit boundary, then ASSERT().
329 If Source and Destination overlap, then ASSERT().
330 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
331 than PcdMaximumUnicodeStringLength Unicode characters, not including the
332 Null-terminator, then ASSERT().
333 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
334 PcdMaximumUnicodeStringLength Unicode characters, not including the
335 Null-terminator, then ASSERT().
336 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
337 and Source results in a Unicode string with more than
338 PcdMaximumUnicodeStringLength Unicode characters, not including the
339 Null-terminator, then ASSERT().
340
341 @param Destination A pointer to a Null-terminated Unicode string.
342 @param Source A pointer to a Null-terminated Unicode string.
343
344 @return Destination.
345
346**/
347CHAR16 *
348StrCat (
349 IN OUT CHAR16 *Destination,
350 IN CONST CHAR16 *Source
351 )
352{
353 StrCpy (Destination + StrLen (Destination), Source);
354
355 //
356 // Size of the resulting string should never be zero.
357 // PcdMaximumUnicodeStringLength is tested inside StrLen().
358 //
359 ASSERT (StrSize (Destination) != 0);
360 return Destination;
361}
362
363/**
364 Concatenates up to a specified length one Null-terminated Unicode to the end
365 of another Null-terminated Unicode string, and returns the concatenated
366 Unicode string.
367
368 This function concatenates two Null-terminated Unicode strings. The contents
369 of Null-terminated Unicode string Source are concatenated to the end of
370 Null-terminated Unicode string Destination, and Destination is returned. At
371 most, Length Unicode characters are concatenated from Source to the end of
372 Destination, and Destination is always Null-terminated. If Length is 0, then
373 Destination is returned unmodified. If Source and Destination overlap, then
374 the results are undefined.
375
376 If Destination is NULL, then ASSERT().
377 If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().
378 If Length > 0 and Source is NULL, then ASSERT().
379 If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().
380 If Source and Destination overlap, then ASSERT().
381 If PcdMaximumUnicodeStringLength is not zero, and Length is greater than
382 PcdMaximumUnicodeStringLength, then ASSERT().
383 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
384 than PcdMaximumUnicodeStringLength Unicode characters, not including the
385 Null-terminator, then ASSERT().
386 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
387 PcdMaximumUnicodeStringLength Unicode characters, not including the
388 Null-terminator, then ASSERT().
389 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
390 and Source results in a Unicode string with more than PcdMaximumUnicodeStringLength
391 Unicode characters, not including the Null-terminator, then ASSERT().
392
393 @param Destination A pointer to a Null-terminated Unicode string.
394 @param Source A pointer to a Null-terminated Unicode string.
395 @param Length The maximum number of Unicode characters to concatenate from
396 Source.
397
398 @return Destination.
399
400**/
401CHAR16 *
402StrnCat (
403 IN OUT CHAR16 *Destination,
404 IN CONST CHAR16 *Source,
405 IN UINTN Length
406 )
407{
408 UINTN DestinationLen;
409
410 DestinationLen = StrLen (Destination);
411 StrnCpy (Destination + DestinationLen, Source, Length);
412 Destination[DestinationLen + Length] = L'\0';
413
414 //
415 // Size of the resulting string should never be zero.
416 // PcdMaximumUnicodeStringLength is tested inside StrLen().
417 //
418 ASSERT (StrSize (Destination) != 0);
419 return Destination;
420}
421
422/**
423 Returns the first occurrence of a Null-terminated Unicode sub-string
424 in a Null-terminated Unicode string.
425
426 This function scans the contents of the Null-terminated Unicode string
427 specified by String and returns the first occurrence of SearchString.
428 If SearchString is not found in String, then NULL is returned. If
429 the length of SearchString is zero, then String is
430 returned.
431
432 If String is NULL, then ASSERT().
433 If String is not aligned on a 16-bit boundary, then ASSERT().
434 If SearchString is NULL, then ASSERT().
435 If SearchString is not aligned on a 16-bit boundary, then ASSERT().
436
437 If PcdMaximumUnicodeStringLength is not zero, and SearchString
438 or String contains more than PcdMaximumUnicodeStringLength Unicode
439 characters, not including the Null-terminator, then ASSERT().
440
441 @param String A pointer to a Null-terminated Unicode string.
442 @param SearchString A pointer to a Null-terminated Unicode string to search for.
443
444 @retval NULL If the SearchString does not appear in String.
445 @return others If there is a match.
446
447**/
448CHAR16 *
449StrStr (
450 IN CONST CHAR16 *String,
451 IN CONST CHAR16 *SearchString
452 )
453{
454 CONST CHAR16 *FirstMatch;
455 CONST CHAR16 *SearchStringTmp;
456
457 //
458 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
459 // Length tests are performed inside StrLen().
460 //
461 ASSERT (StrSize (String) != 0);
462 ASSERT (StrSize (SearchString) != 0);
463
464 if (*SearchString == L'\0') {
465 return (CHAR16 *) String;
466 }
467
468 while (*String != L'\0') {
469 SearchStringTmp = SearchString;
470 FirstMatch = String;
471
472 while ((*String == *SearchStringTmp)
473 && (*String != L'\0')) {
474 String++;
475 SearchStringTmp++;
476 }
477
478 if (*SearchStringTmp == L'\0') {
479 return (CHAR16 *) FirstMatch;
480 }
481
482 if (*String == L'\0') {
483 return NULL;
484 }
485
486 String = FirstMatch + 1;
487 }
488
489 return NULL;
490}
491
492/**
493 Check if a Unicode character is a decimal character.
494
495 This internal function checks if a Unicode character is a
496 decimal character. The valid decimal character is from
497 L'0' to L'9'.
498
499 @param Char The character to check against.
500
501 @retval TRUE If the Char is a decmial character.
502 @retval FALSE If the Char is not a decmial character.
503
504**/
505STATIC
506BOOLEAN
507InternalIsDecimalDigitCharacter (
508 IN CHAR16 Char
509 )
510{
511 return (BOOLEAN) (Char >= L'0' && Char <= L'9');
512}
513
514/**
515 Convert a Unicode character to upper case only if
516 it maps to a valid small-case ASCII character.
517
518 This internal function only deal with Unicode character
519 which maps to a valid small-case ASCII character, i.e.
520 L'a' to L'z'. For other Unicode character, the input character
521 is returned directly.
522
523 @param Char The character to convert.
524
525 @retval LowerCharacter If the Char is with range L'a' to L'z'.
526 @retval Unchanged Otherwise.
527
528**/
529STATIC
530CHAR16
531InternalCharToUpper (
532 IN CHAR16 Char
533 )
534{
535 if (Char >= L'a' && Char <= L'z') {
536 return (CHAR16) (Char - (L'a' - L'A'));
537 }
538
539 return Char;
540}
541
542/**
543 Convert a Unicode character to numerical value.
544
545 This internal function only deal with Unicode character
546 which maps to a valid hexadecimal ASII character, i.e.
547 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
548 Unicode character, the value returned does not make sense.
549
550 @param Char The character to convert.
551
552 @return The numerical value converted.
553
554**/
555STATIC
556UINTN
557InternalHexCharToUintn (
558 IN CHAR16 Char
559 )
560{
561 if (InternalIsDecimalDigitCharacter (Char)) {
562 return Char - L'0';
563 }
564
565 return (UINTN) (10 + InternalCharToUpper (Char) - L'A');
566}
567
568/**
569 Check if a Unicode character is a hexadecimal character.
570
571 This internal function checks if a Unicode character is a
572 decimal character. The valid hexadecimal character is
573 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
574
575
576 @param Char The character to check against.
577
578 @retval TRUE If the Char is a hexadecmial character.
579 @retval FALSE If the Char is not a hexadecmial character.
580
581**/
582STATIC
583BOOLEAN
584InternalIsHexaDecimalDigitCharacter (
585 IN CHAR16 Char
586 )
587{
588
589 return (BOOLEAN) (InternalIsDecimalDigitCharacter (Char) ||
590 (Char >= L'A' && Char <= L'F') ||
591 (Char >= L'a' && Char <= L'f'));
592}
593
594/**
595 Convert a Null-terminated Unicode decimal string to a value of
596 type UINTN.
597
598 This function returns a value of type UINTN by interpreting the contents
599 of the Unicode string specified by String as a decimal number. The format
600 of the input Unicode string String is:
601
602 [spaces] [decimal digits].
603
604 The valid decimal digit character is in the range [0-9]. The
605 function will ignore the pad space, which includes spaces or
606 tab characters, before [decimal digits]. The running zero in the
607 beginning of [decimal digits] will be ignored. Then, the function
608 stops at the first character that is a not a valid decimal character
609 or a Null-terminator, whichever one comes first.
610
611 If String is NULL, then ASSERT().
612 If String is not aligned in a 16-bit boundary, then ASSERT().
613 If String has only pad spaces, then 0 is returned.
614 If String has no pad spaces or valid decimal digits,
615 then 0 is returned.
616 If the number represented by String overflows according
617 to the range defined by UINTN, then ASSERT().
618
619 If PcdMaximumUnicodeStringLength is not zero, and String contains
620 more than PcdMaximumUnicodeStringLength Unicode characters, not including
621 the Null-terminator, then ASSERT().
622
623 @param String A pointer to a Null-terminated Unicode string.
624
625 @retval Value translated from String.
626
627**/
628UINTN
629StrDecimalToUintn (
630 IN CONST CHAR16 *String
631 )
632{
633 UINTN Result;
634
635 //
636 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
637 // Length tests are performed inside StrLen().
638 //
639 ASSERT (StrSize (String) != 0);
640
641 //
642 // Ignore the pad spaces (space or tab)
643 //
644 while ((*String == L' ') || (*String == L'\t')) {
645 String++;
646 }
647
648 //
649 // Ignore leading Zeros after the spaces
650 //
651 while (*String == L'0') {
652 String++;
653 }
654
655 Result = 0;
656
657 while (InternalIsDecimalDigitCharacter (*String)) {
658 //
659 // If the number represented by String overflows according
660 // to the range defined by UINTN, then ASSERT().
661 //
662 ASSERT (Result <= ((((UINTN) ~0) - (*String - L'0')) / 10));
663
664 Result = Result * 10 + (*String - L'0');
665 String++;
666 }
667
668 return Result;
669}
670
671/**
672 Convert a Null-terminated Unicode decimal string to a value of
673 type UINT64.
674
675 This function returns a value of type UINT64 by interpreting the contents
676 of the Unicode string specified by String as a decimal number. The format
677 of the input Unicode string String is:
678
679 [spaces] [decimal digits].
680
681 The valid decimal digit character is in the range [0-9]. The
682 function will ignore the pad space, which includes spaces or
683 tab characters, before [decimal digits]. The running zero in the
684 beginning of [decimal digits] will be ignored. Then, the function
685 stops at the first character that is a not a valid decimal character
686 or a Null-terminator, whichever one comes first.
687
688 If String is NULL, then ASSERT().
689 If String is not aligned in a 16-bit boundary, then ASSERT().
690 If String has only pad spaces, then 0 is returned.
691 If String has no pad spaces or valid decimal digits,
692 then 0 is returned.
693 If the number represented by String overflows according
694 to the range defined by UINT64, then ASSERT().
695
696 If PcdMaximumUnicodeStringLength is not zero, and String contains
697 more than PcdMaximumUnicodeStringLength Unicode characters, not including
698 the Null-terminator, then ASSERT().
699
700 @param String A pointer to a Null-terminated Unicode string.
701
702 @retval Value translated from String.
703
704**/
705UINT64
706StrDecimalToUint64 (
707 IN CONST CHAR16 *String
708 )
709{
710 UINT64 Result;
711
712 //
713 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
714 // Length tests are performed inside StrLen().
715 //
716 ASSERT (StrSize (String) != 0);
717
718 //
719 // Ignore the pad spaces (space or tab)
720 //
721 while ((*String == L' ') || (*String == L'\t')) {
722 String++;
723 }
724
725 //
726 // Ignore leading Zeros after the spaces
727 //
728 while (*String == L'0') {
729 String++;
730 }
731
732 Result = 0;
733
734 while (InternalIsDecimalDigitCharacter (*String)) {
735 //
736 // If the number represented by String overflows according
737 // to the range defined by UINTN, then ASSERT().
738 //
739 ASSERT (Result <= DivU64x32 (((UINT64) ~0) - (*String - L'0') , 10, NULL));
740
741 Result = MultU64x32 (Result, 10) + (*String - L'0');
742 String++;
743 }
744
745 return Result;
746}
747
748/**
749 Convert a Null-terminated Unicode hexadecimal string to a value of type UINTN.
750
751 This function returns a value of type UINTN by interpreting the contents
752 of the Unicode string specified by String as a hexadecimal number.
753 The format of the input Unicode string String is:
754
755 [spaces][zeros][x][hexadecimal digits].
756
757 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
758 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
759 If "x" appears in the input string, it must be prefixed with at least one 0.
760 The function will ignore the pad space, which includes spaces or tab characters,
761 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
762 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
763 first valid hexadecimal digit. Then, the function stops at the first character that is
764 a not a valid hexadecimal character or NULL, whichever one comes first.
765
766 If String is NULL, then ASSERT().
767 If String is not aligned in a 16-bit boundary, then ASSERT().
768 If String has only pad spaces, then zero is returned.
769 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
770 then zero is returned.
771 If the number represented by String overflows according to the range defined by
772 UINTN, then ASSERT().
773
774 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
775 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
776 then ASSERT().
777
778 @param String A pointer to a Null-terminated Unicode string.
779
780 @retval Value translated from String.
781
782**/
783UINTN
784StrHexToUintn (
785 IN CONST CHAR16 *String
786 )
787{
788 UINTN Result;
789
790 //
791 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
792 // Length tests are performed inside StrLen().
793 //
794 ASSERT (StrSize (String) != 0);
795
796 //
797 // Ignore the pad spaces (space or tab)
798 //
799 while ((*String == L' ') || (*String == L'\t')) {
800 String++;
801 }
802
803 //
804 // Ignore leading Zeros after the spaces
805 //
806 while (*String == L'0') {
807 String++;
808 }
809
810 if (InternalCharToUpper (*String) == L'X') {
811 if (*(String - 1) != L'0') {
812 return 0;
813 }
814 //
815 // Skip the 'X'
816 //
817 String++;
818 }
819
820 Result = 0;
821
822 while (InternalIsHexaDecimalDigitCharacter (*String)) {
823 //
824 // If the Hex Number represented by String overflows according
825 // to the range defined by UINTN, then ASSERT().
826 //
827 ASSERT (Result <= ((((UINTN) ~0) - InternalHexCharToUintn (*String)) >> 4));
828
829 Result = (Result << 4) + InternalHexCharToUintn (*String);
830 String++;
831 }
832
833 return Result;
834}
835
836
837/**
838 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
839
840 This function returns a value of type UINT64 by interpreting the contents
841 of the Unicode string specified by String as a hexadecimal number.
842 The format of the input Unicode string String is
843
844 [spaces][zeros][x][hexadecimal digits].
845
846 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
847 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
848 If "x" appears in the input string, it must be prefixed with at least one 0.
849 The function will ignore the pad space, which includes spaces or tab characters,
850 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
851 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
852 first valid hexadecimal digit. Then, the function stops at the first character that is
853 a not a valid hexadecimal character or NULL, whichever one comes first.
854
855 If String is NULL, then ASSERT().
856 If String is not aligned in a 16-bit boundary, then ASSERT().
857 If String has only pad spaces, then zero is returned.
858 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
859 then zero is returned.
860 If the number represented by String overflows according to the range defined by
861 UINT64, then ASSERT().
862
863 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
864 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
865 then ASSERT().
866
867 @param String A pointer to a Null-terminated Unicode string.
868
869 @retval Value translated from String.
870
871**/
872UINT64
873StrHexToUint64 (
874 IN CONST CHAR16 *String
875 )
876{
877 UINT64 Result;
878
879 //
880 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
881 // Length tests are performed inside StrLen().
882 //
883 ASSERT (StrSize (String) != 0);
884
885 //
886 // Ignore the pad spaces (space or tab)
887 //
888 while ((*String == L' ') || (*String == L'\t')) {
889 String++;
890 }
891
892 //
893 // Ignore leading Zeros after the spaces
894 //
895 while (*String == L'0') {
896 String++;
897 }
898
899 if (InternalCharToUpper (*String) == L'X') {
900 ASSERT (*(String - 1) == L'0');
901 if (*(String - 1) != L'0') {
902 return 0;
903 }
904 //
905 // Skip the 'X'
906 //
907 String++;
908 }
909
910 Result = 0;
911
912 while (InternalIsHexaDecimalDigitCharacter (*String)) {
913 //
914 // If the Hex Number represented by String overflows according
915 // to the range defined by UINTN, then ASSERT().
916 //
917 ASSERT (Result <= RShiftU64 (((UINT64) ~0) - InternalHexCharToUintn (*String) , 4));
918
919 Result = LShiftU64 (Result, 4);
920 Result = Result + InternalHexCharToUintn (*String);
921 String++;
922 }
923
924 return Result;
925}
926
927/**
928 Check if a ASCII character is a decimal character.
929
930 This internal function checks if a Unicode character is a
931 decimal character. The valid decimal character is from
932 '0' to '9'.
933
934 @param Char The character to check against.
935
936 @retval TRUE If the Char is a decmial character.
937 @retval FALSE If the Char is not a decmial character.
938
939**/
940STATIC
941BOOLEAN
942InternalAsciiIsDecimalDigitCharacter (
943 IN CHAR8 Char
944 )
945{
946 return (BOOLEAN) (Char >= '0' && Char <= '9');
947}
948
949/**
950 Check if a ASCII character is a hexadecimal character.
951
952 This internal function checks if a ASCII character is a
953 decimal character. The valid hexadecimal character is
954 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
955
956
957 @param Char The character to check against.
958
959 @retval TRUE If the Char is a hexadecmial character.
960 @retval FALSE If the Char is not a hexadecmial character.
961
962**/
963STATIC
964BOOLEAN
965InternalAsciiIsHexaDecimalDigitCharacter (
966 IN CHAR8 Char
967 )
968{
969
970 return (BOOLEAN) (InternalAsciiIsDecimalDigitCharacter (Char) ||
971 (Char >= 'A' && Char <= 'F') ||
972 (Char >= 'a' && Char <= 'f'));
973}
974
975/**
976 Convert a Null-terminated Unicode string to a Null-terminated
977 ASCII string and returns the ASCII string.
978
979 This function converts the content of the Unicode string Source
980 to the ASCII string Destination by copying the lower 8 bits of
981 each Unicode character. It returns Destination.
982
983 If any Unicode characters in Source contain non-zero value in
984 the upper 8 bits, then ASSERT().
985
986 If Destination is NULL, then ASSERT().
987 If Source is NULL, then ASSERT().
988 If Source is not aligned on a 16-bit boundary, then ASSERT().
989 If Source and Destination overlap, then ASSERT().
990
991 If PcdMaximumUnicodeStringLength is not zero, and Source contains
992 more than PcdMaximumUnicodeStringLength Unicode characters, not including
993 the Null-terminator, then ASSERT().
994
995 If PcdMaximumAsciiStringLength is not zero, and Source contains more
996 than PcdMaximumAsciiStringLength Unicode characters, not including the
997 Null-terminator, then ASSERT().
998
999 @param Source A pointer to a Null-terminated Unicode string.
1000 @param Destination A pointer to a Null-terminated ASCII string.
1001
1002 @return Destination.
1003
1004**/
1005CHAR8 *
1006UnicodeStrToAsciiStr (
1007 IN CONST CHAR16 *Source,
1008 OUT CHAR8 *Destination
1009 )
1010{
1011 CHAR8 *ReturnValue;
1012
1013 ASSERT (Destination != NULL);
1014
1015 //
1016 // ASSERT if Source is long than PcdMaximumUnicodeStringLength.
1017 // Length tests are performed inside StrLen().
1018 //
1019 ASSERT (StrSize (Source) != 0);
1020
1021 //
1022 // Source and Destination should not overlap
1023 //
1024 ASSERT ((UINTN) ((CHAR16 *) Destination - Source) > StrLen (Source));
1025 ASSERT ((UINTN) ((CHAR8 *) Source - Destination) > StrLen (Source));
1026
1027
1028 ReturnValue = Destination;
1029 while (*Source != '\0') {
1030 //
1031 // If any Unicode characters in Source contain
1032 // non-zero value in the upper 8 bits, then ASSERT().
1033 //
1034 ASSERT (*Source < 0x100);
1035 *(Destination++) = (CHAR8) *(Source++);
1036 }
1037
1038 *Destination = '\0';
1039
1040 //
1041 // ASSERT Original Destination is less long than PcdMaximumAsciiStringLength.
1042 // Length tests are performed inside AsciiStrLen().
1043 //
1044 ASSERT (AsciiStrSize (ReturnValue) != 0);
1045
1046 return ReturnValue;
1047}
1048
1049
1050/**
1051 Copies one Null-terminated ASCII string to another Null-terminated ASCII
1052 string and returns the new ASCII string.
1053
1054 This function copies the contents of the ASCII string Source to the ASCII
1055 string Destination, and returns Destination. If Source and Destination
1056 overlap, then the results are undefined.
1057
1058 If Destination is NULL, then ASSERT().
1059 If Source is NULL, then ASSERT().
1060 If Source and Destination overlap, then ASSERT().
1061 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1062 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1063 then ASSERT().
1064
1065 @param Destination A pointer to a Null-terminated ASCII string.
1066 @param Source A pointer to a Null-terminated ASCII string.
1067
1068 @return Destination
1069
1070**/
1071CHAR8 *
1072AsciiStrCpy (
1073 OUT CHAR8 *Destination,
1074 IN CONST CHAR8 *Source
1075 )
1076{
1077 CHAR8 *ReturnValue;
1078
1079 //
1080 // Destination cannot be NULL
1081 //
1082 ASSERT (Destination != NULL);
1083
1084 //
1085 // Destination and source cannot overlap
1086 //
1087 ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));
1088 ASSERT ((UINTN)(Source - Destination) > AsciiStrLen (Source));
1089
1090 ReturnValue = Destination;
1091 while (*Source != 0) {
1092 *(Destination++) = *(Source++);
1093 }
1094 *Destination = 0;
1095 return ReturnValue;
1096}
1097
1098/**
1099 Copies up to a specified length one Null-terminated ASCII string to another
1100 Null-terminated ASCII string and returns the new ASCII string.
1101
1102 This function copies the contents of the ASCII string Source to the ASCII
1103 string Destination, and returns Destination. At most, Length ASCII characters
1104 are copied from Source to Destination. If Length is 0, then Destination is
1105 returned unmodified. If Length is greater that the number of ASCII characters
1106 in Source, then Destination is padded with Null ASCII characters. If Source
1107 and Destination overlap, then the results are undefined.
1108
1109 If Destination is NULL, then ASSERT().
1110 If Source is NULL, then ASSERT().
1111 If Source and Destination overlap, then ASSERT().
1112 If PcdMaximumAsciiStringLength is not zero, and Length is greater than
1113 PcdMaximumAsciiStringLength, then ASSERT().
1114 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1115 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1116 then ASSERT().
1117
1118 @param Destination A pointer to a Null-terminated ASCII string.
1119 @param Source A pointer to a Null-terminated ASCII string.
1120 @param Length The maximum number of ASCII characters to copy.
1121
1122 @return Destination
1123
1124**/
1125CHAR8 *
1126AsciiStrnCpy (
1127 OUT CHAR8 *Destination,
1128 IN CONST CHAR8 *Source,
1129 IN UINTN Length
1130 )
1131{
1132 CHAR8 *ReturnValue;
1133
1134 if (Length == 0) {
1135 return Destination;
1136 }
1137
1138 //
1139 // Destination cannot be NULL
1140 //
1141 ASSERT (Destination != NULL);
1142
1143 //
1144 // Destination and source cannot overlap
1145 //
1146 ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));
1147 ASSERT ((UINTN)(Source - Destination) >= Length);
1148
1149 if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {
1150 ASSERT (Length <= PcdGet32 (PcdMaximumAsciiStringLength));
1151 }
1152
1153 ReturnValue = Destination;
1154
1155 while (*Source != 0 && Length > 0) {
1156 *(Destination++) = *(Source++);
1157 Length--;
1158 }
1159
1160 ZeroMem (Destination, Length * sizeof (*Destination));
1161 return ReturnValue;
1162}
1163
1164/**
1165 Returns the length of a Null-terminated ASCII string.
1166
1167 This function returns the number of ASCII characters in the Null-terminated
1168 ASCII string specified by String.
1169
1170 If Length > 0 and Destination is NULL, then ASSERT().
1171 If Length > 0 and Source is NULL, then ASSERT().
1172 If PcdMaximumAsciiStringLength is not zero and String contains more than
1173 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1174 then ASSERT().
1175
1176 @param String A pointer to a Null-terminated ASCII string.
1177
1178 @return The length of String.
1179
1180**/
1181UINTN
1182AsciiStrLen (
1183 IN CONST CHAR8 *String
1184 )
1185{
1186 UINTN Length;
1187
1188 ASSERT (String != NULL);
1189
1190 for (Length = 0; *String != '\0'; String++, Length++) {
1191 //
1192 // If PcdMaximumUnicodeStringLength is not zero,
1193 // length should not more than PcdMaximumUnicodeStringLength
1194 //
1195 if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {
1196 ASSERT (Length < PcdGet32 (PcdMaximumAsciiStringLength));
1197 }
1198 }
1199 return Length;
1200}
1201
1202/**
1203 Returns the size of a Null-terminated ASCII string in bytes, including the
1204 Null terminator.
1205
1206 This function returns the size, in bytes, of the Null-terminated ASCII string
1207 specified by String.
1208
1209 If String is NULL, then ASSERT().
1210 If PcdMaximumAsciiStringLength is not zero and String contains more than
1211 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1212 then ASSERT().
1213
1214 @param String A pointer to a Null-terminated ASCII string.
1215
1216 @return The size of String.
1217
1218**/
1219UINTN
1220AsciiStrSize (
1221 IN CONST CHAR8 *String
1222 )
1223{
1224 return (AsciiStrLen (String) + 1) * sizeof (*String);
1225}
1226
1227/**
1228 Compares two Null-terminated ASCII strings, and returns the difference
1229 between the first mismatched ASCII characters.
1230
1231 This function compares the Null-terminated ASCII string FirstString to the
1232 Null-terminated ASCII string SecondString. If FirstString is identical to
1233 SecondString, then 0 is returned. Otherwise, the value returned is the first
1234 mismatched ASCII character in SecondString subtracted from the first
1235 mismatched ASCII character in FirstString.
1236
1237 If FirstString is NULL, then ASSERT().
1238 If SecondString is NULL, then ASSERT().
1239 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1240 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1241 then ASSERT().
1242 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
1243 than PcdMaximumAsciiStringLength ASCII characters, not including the
1244 Null-terminator, then ASSERT().
1245
1246 @param FirstString A pointer to a Null-terminated ASCII string.
1247 @param SecondString A pointer to a Null-terminated ASCII string.
1248
1249 @retval ==0 FirstString is identical to SecondString.
1250 @retval !=0 FirstString is not identical to SecondString.
1251
1252**/
1253INTN
1254AsciiStrCmp (
1255 IN CONST CHAR8 *FirstString,
1256 IN CONST CHAR8 *SecondString
1257 )
1258{
1259 //
1260 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1261 //
1262 ASSERT (AsciiStrSize (FirstString));
1263 ASSERT (AsciiStrSize (SecondString));
1264
1265 while ((*FirstString != '\0') && (*FirstString == *SecondString)) {
1266 FirstString++;
1267 SecondString++;
1268 }
1269
1270 return *FirstString - *SecondString;
1271}
1272
1273/**
1274 Converts a lowercase Ascii character to upper one.
1275
1276 If Chr is lowercase Ascii character, then converts it to upper one.
1277
1278 If Value >= 0xA0, then ASSERT().
1279 If (Value & 0x0F) >= 0x0A, then ASSERT().
1280
1281 @param Chr one Ascii character
1282
1283 @return The uppercase value of Ascii character
1284
1285**/
1286STATIC
1287CHAR8
1288InternalBaseLibAsciiToUpper (
1289 IN CHAR8 Chr
1290 )
1291{
1292 return (UINT8) ((Chr >= 'a' && Chr <= 'z') ? Chr - ('a' - 'A') : Chr);
1293}
1294
1295/**
1296 Convert a ASCII character to numerical value.
1297
1298 This internal function only deal with Unicode character
1299 which maps to a valid hexadecimal ASII character, i.e.
1300 '0' to '9', 'a' to 'f' or 'A' to 'F'. For other
1301 ASCII character, the value returned does not make sense.
1302
1303 @param Char The character to convert.
1304
1305 @return The numerical value converted.
1306
1307**/
1308STATIC
1309UINTN
1310InternalAsciiHexCharToUintn (
1311 IN CHAR8 Char
1312 )
1313{
1314 if (InternalIsDecimalDigitCharacter (Char)) {
1315 return Char - '0';
1316 }
1317
1318 return (UINTN) (10 + InternalBaseLibAsciiToUpper (Char) - 'A');
1319}
1320
1321
1322/**
1323 Performs a case insensitive comparison of two Null-terminated ASCII strings,
1324 and returns the difference between the first mismatched ASCII characters.
1325
1326 This function performs a case insensitive comparison of the Null-terminated
1327 ASCII string FirstString to the Null-terminated ASCII string SecondString. If
1328 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
1329 value returned is the first mismatched lower case ASCII character in
1330 SecondString subtracted from the first mismatched lower case ASCII character
1331 in FirstString.
1332
1333 If FirstString is NULL, then ASSERT().
1334 If SecondString is NULL, then ASSERT().
1335 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1336 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1337 then ASSERT().
1338 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
1339 than PcdMaximumAsciiStringLength ASCII characters, not including the
1340 Null-terminator, then ASSERT().
1341
1342 @param FirstString A pointer to a Null-terminated ASCII string.
1343 @param SecondString A pointer to a Null-terminated ASCII string.
1344
1345 @retval ==0 FirstString is identical to SecondString using case insensitive
1346 comparisons.
1347 @retval !=0 FirstString is not identical to SecondString using case
1348 insensitive comparisons.
1349
1350**/
1351INTN
1352AsciiStriCmp (
1353 IN CONST CHAR8 *FirstString,
1354 IN CONST CHAR8 *SecondString
1355 )
1356{
1357 CHAR8 UpperFirstString;
1358 CHAR8 UpperSecondString;
1359
1360 //
1361 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1362 //
1363 ASSERT (AsciiStrSize (FirstString));
1364 ASSERT (AsciiStrSize (SecondString));
1365
1366 UpperFirstString = InternalBaseLibAsciiToUpper (*FirstString);
1367 UpperSecondString = InternalBaseLibAsciiToUpper (*SecondString);
1368 while ((*FirstString != '\0') && (UpperFirstString == UpperSecondString)) {
1369 FirstString++;
1370 SecondString++;
1371 UpperFirstString = InternalBaseLibAsciiToUpper (*FirstString);
1372 UpperSecondString = InternalBaseLibAsciiToUpper (*SecondString);
1373 }
1374
1375 return UpperFirstString - UpperSecondString;
1376}
1377
1378/**
1379 Compares two Null-terminated ASCII strings with maximum lengths, and returns
1380 the difference between the first mismatched ASCII characters.
1381
1382 This function compares the Null-terminated ASCII string FirstString to the
1383 Null-terminated ASCII string SecondString. At most, Length ASCII characters
1384 will be compared. If Length is 0, then 0 is returned. If FirstString is
1385 identical to SecondString, then 0 is returned. Otherwise, the value returned
1386 is the first mismatched ASCII character in SecondString subtracted from the
1387 first mismatched ASCII character in FirstString.
1388
1389 If Length > 0 and FirstString is NULL, then ASSERT().
1390 If Length > 0 and SecondString is NULL, then ASSERT().
1391 If PcdMaximumAsciiStringLength is not zero, and Length is greater than
1392 PcdMaximumAsciiStringLength, then ASSERT().
1393 If PcdMaximumAsciiStringLength is not zero, and FirstString contains more than
1394 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1395 then ASSERT().
1396 If PcdMaximumAsciiStringLength is not zero, and SecondString contains more than
1397 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1398 then ASSERT().
1399
1400 @param FirstString A pointer to a Null-terminated ASCII string.
1401 @param SecondString A pointer to a Null-terminated ASCII string.
1402 @param Length The maximum number of ASCII characters for compare.
1403
1404 @retval ==0 FirstString is identical to SecondString.
1405 @retval !=0 FirstString is not identical to SecondString.
1406
1407**/
1408INTN
1409AsciiStrnCmp (
1410 IN CONST CHAR8 *FirstString,
1411 IN CONST CHAR8 *SecondString,
1412 IN UINTN Length
1413 )
1414{
1415 if (Length == 0) {
1416 return 0;
1417 }
1418
1419 //
1420 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1421 //
1422 ASSERT (AsciiStrSize (FirstString));
1423 ASSERT (AsciiStrSize (SecondString));
1424
1425 if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {
1426 ASSERT (Length <= PcdGet32 (PcdMaximumAsciiStringLength));
1427 }
1428
1429 while ((*FirstString != '\0') &&
1430 (*FirstString == *SecondString) &&
1431 (Length > 1)) {
1432 FirstString++;
1433 SecondString++;
1434 Length--;
1435 }
1436 return *FirstString - *SecondString;
1437}
1438
1439/**
1440 Concatenates one Null-terminated ASCII string to another Null-terminated
1441 ASCII string, and returns the concatenated ASCII string.
1442
1443 This function concatenates two Null-terminated ASCII strings. The contents of
1444 Null-terminated ASCII string Source are concatenated to the end of Null-
1445 terminated ASCII string Destination. The Null-terminated concatenated ASCII
1446 String is returned.
1447
1448 If Destination is NULL, then ASSERT().
1449 If Source is NULL, then ASSERT().
1450 If PcdMaximumAsciiStringLength is not zero and Destination contains more than
1451 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1452 then ASSERT().
1453 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1454 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1455 then ASSERT().
1456 If PcdMaximumAsciiStringLength is not zero and concatenating Destination and
1457 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1458 ASCII characters, then ASSERT().
1459
1460 @param Destination A pointer to a Null-terminated ASCII string.
1461 @param Source A pointer to a Null-terminated ASCII string.
1462
1463 @return Destination
1464
1465**/
1466CHAR8 *
1467AsciiStrCat (
1468 IN OUT CHAR8 *Destination,
1469 IN CONST CHAR8 *Source
1470 )
1471{
1472 AsciiStrCpy (Destination + AsciiStrLen (Destination), Source);
1473
1474 //
1475 // Size of the resulting string should never be zero.
1476 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1477 //
1478 ASSERT (AsciiStrSize (Destination) != 0);
1479 return Destination;
1480}
1481
1482/**
1483 Concatenates up to a specified length one Null-terminated ASCII string to
1484 the end of another Null-terminated ASCII string, and returns the
1485 concatenated ASCII string.
1486
1487 This function concatenates two Null-terminated ASCII strings. The contents
1488 of Null-terminated ASCII string Source are concatenated to the end of Null-
1489 terminated ASCII string Destination, and Destination is returned. At most,
1490 Length ASCII characters are concatenated from Source to the end of
1491 Destination, and Destination is always Null-terminated. If Length is 0, then
1492 Destination is returned unmodified. If Source and Destination overlap, then
1493 the results are undefined.
1494
1495 If Length > 0 and Destination is NULL, then ASSERT().
1496 If Length > 0 and Source is NULL, then ASSERT().
1497 If Source and Destination overlap, then ASSERT().
1498 If PcdMaximumAsciiStringLength is not zero, and Length is greater than
1499 PcdMaximumAsciiStringLength, then ASSERT().
1500 If PcdMaximumAsciiStringLength is not zero, and Destination contains more than
1501 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1502 then ASSERT().
1503 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1504 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1505 then ASSERT().
1506 If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and
1507 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1508 ASCII characters, not including the Null-terminator, then ASSERT().
1509
1510 @param Destination A pointer to a Null-terminated ASCII string.
1511 @param Source A pointer to a Null-terminated ASCII string.
1512 @param Length The maximum number of ASCII characters to concatenate from
1513 Source.
1514
1515 @return Destination
1516
1517**/
1518CHAR8 *
1519AsciiStrnCat (
1520 IN OUT CHAR8 *Destination,
1521 IN CONST CHAR8 *Source,
1522 IN UINTN Length
1523 )
1524{
1525 UINTN DestinationLen;
1526
1527 DestinationLen = AsciiStrLen (Destination);
1528 AsciiStrnCpy (Destination + DestinationLen, Source, Length);
1529 Destination[DestinationLen + Length] = '\0';
1530
1531 //
1532 // Size of the resulting string should never be zero.
1533 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1534 //
1535 ASSERT (AsciiStrSize (Destination) != 0);
1536 return Destination;
1537}
1538
1539/**
1540 Returns the first occurrence of a Null-terminated ASCII sub-string
1541 in a Null-terminated ASCII string.
1542
1543 This function scans the contents of the ASCII string specified by String
1544 and returns the first occurrence of SearchString. If SearchString is not
1545 found in String, then NULL is returned. If the length of SearchString is zero,
1546 then String is returned.
1547
1548 If String is NULL, then ASSERT().
1549 If SearchString is NULL, then ASSERT().
1550
1551 If PcdMaximumAsciiStringLength is not zero, and SearchString or
1552 String contains more than PcdMaximumAsciiStringLength Unicode characters
1553 not including the Null-terminator, then ASSERT().
1554
1555 @param String A pointer to a Null-terminated ASCII string.
1556 @param SearchString A pointer to a Null-terminated ASCII string to search for.
1557
1558 @retval NULL If the SearchString does not appear in String.
1559 @retval others If there is a match return the first occurrence of SearchingString.
1560 If the length of SearchString is zero,return String.
1561
1562**/
1563CHAR8 *
1564AsciiStrStr (
1565 IN CONST CHAR8 *String,
1566 IN CONST CHAR8 *SearchString
1567 )
1568{
1569 CONST CHAR8 *FirstMatch;
1570 CONST CHAR8 *SearchStringTmp;
1571
1572 //
1573 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1574 //
1575 ASSERT (AsciiStrSize (String) != 0);
1576 ASSERT (AsciiStrSize (SearchString) != 0);
1577
1578 if (*SearchString == '\0') {
1579 return (CHAR8 *) String;
1580 }
1581
1582 while (*String != '\0') {
1583 SearchStringTmp = SearchString;
1584 FirstMatch = String;
1585
1586 while ((*String == *SearchStringTmp)
1587 && (*String != '\0')) {
1588 String++;
1589 SearchStringTmp++;
1590 }
1591
1592 if (*SearchStringTmp == '\0') {
1593 return (CHAR8 *) FirstMatch;
1594 }
1595
1596 if (*String == '\0') {
1597 return NULL;
1598 }
1599
1600 String = FirstMatch + 1;
1601 }
1602
1603 return NULL;
1604}
1605
1606/**
1607 Convert a Null-terminated ASCII decimal string to a value of type
1608 UINTN.
1609
1610 This function returns a value of type UINTN by interpreting the contents
1611 of the ASCII string String as a decimal number. The format of the input
1612 ASCII string String is:
1613
1614 [spaces] [decimal digits].
1615
1616 The valid decimal digit character is in the range [0-9]. The function will
1617 ignore the pad space, which includes spaces or tab characters, before the digits.
1618 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1619 function stops at the first character that is a not a valid decimal character or
1620 Null-terminator, whichever on comes first.
1621
1622 If String has only pad spaces, then 0 is returned.
1623 If String has no pad spaces or valid decimal digits, then 0 is returned.
1624 If the number represented by String overflows according to the range defined by
1625 UINTN, then ASSERT().
1626 If String is NULL, then ASSERT().
1627 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1628 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1629 then ASSERT().
1630
1631 @param String A pointer to a Null-terminated ASCII string.
1632
1633 @retval Value translated from String.
1634
1635**/
1636UINTN
1637AsciiStrDecimalToUintn (
1638 IN CONST CHAR8 *String
1639 )
1640{
1641 UINTN Result;
1642
1643 //
1644 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1645 //
1646 ASSERT (AsciiStrSize (String) != 0);
1647
1648 //
1649 // Ignore the pad spaces (space or tab)
1650 //
1651 while ((*String == ' ') || (*String == '\t' )) {
1652 String++;
1653 }
1654
1655 //
1656 // Ignore leading Zeros after the spaces
1657 //
1658 while (*String == '0') {
1659 String++;
1660 }
1661
1662 Result = 0;
1663
1664 while (InternalAsciiIsDecimalDigitCharacter (*String)) {
1665 //
1666 // If the number represented by String overflows according
1667 // to the range defined by UINTN, then ASSERT().
1668 //
1669 ASSERT (Result <= ((((UINTN) ~0) - (*String - L'0')) / 10));
1670
1671 Result = Result * 10 + (*String - '0');
1672 String++;
1673 }
1674
1675 return Result;
1676}
1677
1678
1679/**
1680 Convert a Null-terminated ASCII decimal string to a value of type
1681 UINT64.
1682
1683 This function returns a value of type UINT64 by interpreting the contents
1684 of the ASCII string String as a decimal number. The format of the input
1685 ASCII string String is:
1686
1687 [spaces] [decimal digits].
1688
1689 The valid decimal digit character is in the range [0-9]. The function will
1690 ignore the pad space, which includes spaces or tab characters, before the digits.
1691 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1692 function stops at the first character that is a not a valid decimal character or
1693 Null-terminator, whichever on comes first.
1694
1695 If String has only pad spaces, then 0 is returned.
1696 If String has no pad spaces or valid decimal digits, then 0 is returned.
1697 If the number represented by String overflows according to the range defined by
1698 UINT64, then ASSERT().
1699 If String is NULL, then ASSERT().
1700 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1701 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1702 then ASSERT().
1703
1704 @param String A pointer to a Null-terminated ASCII string.
1705
1706 @retval Value translated from String.
1707
1708**/
1709UINT64
1710AsciiStrDecimalToUint64 (
1711 IN CONST CHAR8 *String
1712 )
1713{
1714 UINT64 Result;
1715
1716 //
1717 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1718 //
1719 ASSERT (AsciiStrSize (String) != 0);
1720
1721 //
1722 // Ignore the pad spaces (space or tab)
1723 //
1724 while ((*String == ' ') || (*String == '\t' )) {
1725 String++;
1726 }
1727
1728 //
1729 // Ignore leading Zeros after the spaces
1730 //
1731 while (*String == '0') {
1732 String++;
1733 }
1734
1735 Result = 0;
1736
1737 while (InternalAsciiIsDecimalDigitCharacter (*String)) {
1738 //
1739 // If the number represented by String overflows according
1740 // to the range defined by UINTN, then ASSERT().
1741 //
1742 ASSERT (Result <= DivU64x32 (((UINT64) ~0) - (*String - L'0') , 10, NULL));
1743
1744 Result = MultU64x32 (Result, 10) + (*String - '0');
1745 String++;
1746 }
1747
1748 return Result;
1749}
1750
1751/**
1752 Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.
1753
1754 This function returns a value of type UINTN by interpreting the contents of
1755 the ASCII string String as a hexadecimal number. The format of the input ASCII
1756 string String is:
1757
1758 [spaces][zeros][x][hexadecimal digits].
1759
1760 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1761 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1762 appears in the input string, it must be prefixed with at least one 0. The function
1763 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1764 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1765 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1766 digit. Then, the function stops at the first character that is a not a valid
1767 hexadecimal character or Null-terminator, whichever on comes first.
1768
1769 If String has only pad spaces, then 0 is returned.
1770 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1771 0 is returned.
1772
1773 If the number represented by String overflows according to the range defined by UINTN,
1774 then ASSERT().
1775 If String is NULL, then ASSERT().
1776 If PcdMaximumAsciiStringLength is not zero,
1777 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1778 the Null-terminator, then ASSERT().
1779
1780 @param String A pointer to a Null-terminated ASCII string.
1781
1782 @retval Value translated from String.
1783
1784**/
1785UINTN
1786AsciiStrHexToUintn (
1787 IN CONST CHAR8 *String
1788 )
1789{
1790 UINTN Result;
1791
1792 //
1793 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1794 //
1795 ASSERT (AsciiStrSize (String) != 0);
1796
1797 //
1798 // Ignore the pad spaces (space or tab)
1799 //
1800 while ((*String == ' ') || (*String == '\t' )) {
1801 String++;
1802 }
1803
1804 //
1805 // Ignore leading Zeros after the spaces
1806 //
1807 while (*String == '0') {
1808 String++;
1809 }
1810
1811 if (InternalBaseLibAsciiToUpper (*String) == 'X') {
1812 ASSERT (*(String - 1) == '0');
1813 if (*(String - 1) != '0') {
1814 return 0;
1815 }
1816 //
1817 // Skip the 'X'
1818 //
1819 String++;
1820 }
1821
1822 Result = 0;
1823
1824 while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {
1825 //
1826 // If the Hex Number represented by String overflows according
1827 // to the range defined by UINTN, then ASSERT().
1828 //
1829 ASSERT (Result <= ((((UINTN) ~0) - InternalHexCharToUintn (*String)) >> 4));
1830
1831 Result = (Result << 4) + InternalAsciiHexCharToUintn (*String);
1832 String++;
1833 }
1834
1835 return Result;
1836}
1837
1838/**
1839 Convert a Null-terminated ASCII hexadecimal string to a value of type UINT64.
1840
1841 This function returns a value of type UINT64 by interpreting the contents of
1842 the ASCII string String as a hexadecimal number. The format of the input ASCII
1843 string String is:
1844
1845 [spaces][zeros][x][hexadecimal digits].
1846
1847 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1848 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1849 appears in the input string, it must be prefixed with at least one 0. The function
1850 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1851 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1852 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1853 digit. Then, the function stops at the first character that is a not a valid
1854 hexadecimal character or Null-terminator, whichever on comes first.
1855
1856 If String has only pad spaces, then 0 is returned.
1857 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1858 0 is returned.
1859
1860 If the number represented by String overflows according to the range defined by UINT64,
1861 then ASSERT().
1862 If String is NULL, then ASSERT().
1863 If PcdMaximumAsciiStringLength is not zero,
1864 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1865 the Null-terminator, then ASSERT().
1866
1867 @param String A pointer to a Null-terminated ASCII string.
1868
1869 @retval Value translated from String.
1870
1871**/
1872UINT64
1873AsciiStrHexToUint64 (
1874 IN CONST CHAR8 *String
1875 )
1876{
1877 UINT64 Result;
1878
1879 //
1880 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1881 //
1882 ASSERT (AsciiStrSize (String) != 0);
1883
1884 //
1885 // Ignore the pad spaces (space or tab) and leading Zeros
1886 //
1887 //
1888 // Ignore the pad spaces (space or tab)
1889 //
1890 while ((*String == ' ') || (*String == '\t' )) {
1891 String++;
1892 }
1893
1894 //
1895 // Ignore leading Zeros after the spaces
1896 //
1897 while (*String == '0') {
1898 String++;
1899 }
1900
1901 if (InternalBaseLibAsciiToUpper (*String) == 'X') {
1902 ASSERT (*(String - 1) == '0');
1903 if (*(String - 1) != '0') {
1904 return 0;
1905 }
1906 //
1907 // Skip the 'X'
1908 //
1909 String++;
1910 }
1911
1912 Result = 0;
1913
1914 while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {
1915 //
1916 // If the Hex Number represented by String overflows according
1917 // to the range defined by UINTN, then ASSERT().
1918 //
1919 ASSERT (Result <= RShiftU64 (((UINT64) ~0) - InternalHexCharToUintn (*String) , 4));
1920
1921 Result = LShiftU64 (Result, 4);
1922 Result = Result + InternalAsciiHexCharToUintn (*String);
1923 String++;
1924 }
1925
1926 return Result;
1927}
1928
1929/**
1930 Convert one Null-terminated ASCII string to a Null-terminated
1931 Unicode string and returns the Unicode string.
1932
1933 This function converts the contents of the ASCII string Source to the Unicode
1934 string Destination, and returns Destination. The function terminates the
1935 Unicode string Destination by appending a Null-terminator character at the end.
1936 The caller is responsible to make sure Destination points to a buffer with size
1937 equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.
1938
1939 If Destination is NULL, then ASSERT().
1940 If Destination is not aligned on a 16-bit boundary, then ASSERT().
1941 If Source is NULL, then ASSERT().
1942 If Source and Destination overlap, then ASSERT().
1943 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1944 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1945 then ASSERT().
1946 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
1947 PcdMaximumUnicodeStringLength ASCII characters not including the
1948 Null-terminator, then ASSERT().
1949
1950 @param Source A pointer to a Null-terminated ASCII string.
1951 @param Destination A pointer to a Null-terminated Unicode string.
1952
1953 @return Destination.
1954
1955**/
1956CHAR16 *
1957AsciiStrToUnicodeStr (
1958 IN CONST CHAR8 *Source,
1959 OUT CHAR16 *Destination
1960 )
1961{
1962 CHAR16 *ReturnValue;
1963
1964 ASSERT (Destination != NULL);
1965
1966 //
1967 // ASSERT Source is less long than PcdMaximumAsciiStringLength
1968 //
1969 ASSERT (AsciiStrSize (Source) != 0);
1970
1971 //
1972 // Source and Destination should not overlap
1973 //
1974 ASSERT ((UINTN) ((CHAR8 *) Destination - Source) > AsciiStrLen (Source));
1975 ASSERT ((UINTN) (Source - (CHAR8 *) Destination) > (AsciiStrLen (Source) * sizeof (CHAR16)));
1976
1977
1978 ReturnValue = Destination;
1979 while (*Source != '\0') {
1980 *(Destination++) = (CHAR16) *(Source++);
1981 }
1982 //
1983 // End the Destination with a NULL.
1984 //
1985 *Destination = '\0';
1986
1987 //
1988 // ASSERT Original Destination is less long than PcdMaximumUnicodeStringLength
1989 //
1990 ASSERT (StrSize (ReturnValue) != 0);
1991
1992 return ReturnValue;
1993}
1994
1995/**
1996 Converts an 8-bit value to an 8-bit BCD value.
1997
1998 Converts the 8-bit value specified by Value to BCD. The BCD value is
1999 returned.
2000
2001 If Value >= 100, then ASSERT().
2002
2003 @param Value The 8-bit value to convert to BCD. Range 0..99.
2004
2005 @return The BCD value.
2006
2007**/
2008UINT8
2009DecimalToBcd8 (
2010 IN UINT8 Value
2011 )
2012{
2013 ASSERT (Value < 100);
2014 return (UINT8) (((Value / 10) << 4) | (Value % 10));
2015}
2016
2017/**
2018 Converts an 8-bit BCD value to an 8-bit value.
2019
2020 Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit
2021 value is returned.
2022
2023 If Value >= 0xA0, then ASSERT().
2024 If (Value & 0x0F) >= 0x0A, then ASSERT().
2025
2026 @param Value The 8-bit BCD value to convert to an 8-bit value.
2027
2028 @return The 8-bit value is returned.
2029
2030**/
2031UINT8
2032BcdToDecimal8 (
2033 IN UINT8 Value
2034 )
2035{
2036 ASSERT (Value < 0xa0);
2037 ASSERT ((Value & 0xf) < 0xa);
2038 return (UINT8) ((Value >> 4) * 10 + (Value & 0xf));
2039}
2040
2041
2042

Archive Download this file

Revision: HEAD