2 Unicode and ASCII string primatives.
4 Copyright (c) 2006 - 2009, Intel Corporation<BR>
5 All rights reserved. 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
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.
15 #include "BaseLibInternals.h"
17 #define QUOTIENT_MAX_UINTN_DIVIDED_BY_10 ((UINTN) -1 / 10)
18 #define REMAINDER_MAX_UINTN_DIVIDED_BY_10 ((UINTN) -1 % 10)
20 #define QUOTIENT_MAX_UINTN_DIVIDED_BY_16 ((UINTN) -1 / 16)
21 #define REMAINDER_MAX_UINTN_DIVIDED_BY_16 ((UINTN) -1 % 16)
23 #define QUOTIENT_MAX_UINT64_DIVIDED_BY_10 ((UINT64) -1 / 10)
24 #define REMAINDER_MAX_UINT64_DIVIDED_BY_10 ((UINT64) -1 % 10)
26 #define QUOTIENT_MAX_UINT64_DIVIDED_BY_16 ((UINT64) -1 / 16)
27 #define REMAINDER_MAX_UINT64_DIVIDED_BY_16 ((UINT64) -1 % 16)
30 Copies one Null-terminated Unicode string to another Null-terminated Unicode
31 string and returns the new Unicode string.
33 This function copies the contents of the Unicode string Source to the Unicode
34 string Destination, and returns Destination. If Source and Destination
35 overlap, then the results are undefined.
37 If Destination is NULL, then ASSERT().
38 If Destination is not aligned on a 16-bit boundary, then ASSERT().
39 If Source is NULL, then ASSERT().
40 If Source is not aligned on a 16-bit boundary, then ASSERT().
41 If Source and Destination overlap, then ASSERT().
42 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
43 PcdMaximumUnicodeStringLength Unicode characters, not including the
44 Null-terminator, then ASSERT().
46 @param Destination Pointer to a Null-terminated Unicode string.
47 @param Source Pointer to a Null-terminated Unicode string.
55 OUT CHAR16
*Destination
,
56 IN CONST CHAR16
*Source
62 // Destination cannot be NULL
64 ASSERT (Destination
!= NULL
);
65 ASSERT (((UINTN
) Destination
& BIT0
) == 0);
68 // Destination and source cannot overlap
70 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
71 ASSERT ((UINTN
)(Source
- Destination
) > StrLen (Source
));
73 ReturnValue
= Destination
;
74 while (*Source
!= 0) {
75 *(Destination
++) = *(Source
++);
82 Copies up to a specified length from one Null-terminated Unicode string to
83 another Null-terminated Unicode string and returns the new Unicode string.
85 This function copies the contents of the Unicode string Source to the Unicode
86 string Destination, and returns Destination. At most, Length Unicode
87 characters are copied from Source to Destination. If Length is 0, then
88 Destination is returned unmodified. If Length is greater that the number of
89 Unicode characters in Source, then Destination is padded with Null Unicode
90 characters. If Source and Destination overlap, then the results are
93 If Length > 0 and Destination is NULL, then ASSERT().
94 If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().
95 If Length > 0 and Source is NULL, then ASSERT().
96 If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().
97 If Source and Destination overlap, then ASSERT().
98 If PcdMaximumUnicodeStringLength is not zero, and Length is greater than
99 PcdMaximumUnicodeStringLength, then ASSERT().
100 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
101 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
104 @param Destination Pointer to a Null-terminated Unicode string.
105 @param Source Pointer to a Null-terminated Unicode string.
106 @param Length Maximum number of Unicode characters to copy.
114 OUT CHAR16
*Destination
,
115 IN CONST CHAR16
*Source
,
126 // Destination cannot be NULL if Length is not zero
128 ASSERT (Destination
!= NULL
);
129 ASSERT (((UINTN
) Destination
& BIT0
) == 0);
132 // Destination and source cannot overlap
134 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
135 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
137 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
138 ASSERT (Length
<= PcdGet32 (PcdMaximumUnicodeStringLength
));
141 ReturnValue
= Destination
;
143 while ((*Source
!= L
'\0') && (Length
> 0)) {
144 *(Destination
++) = *(Source
++);
148 ZeroMem (Destination
, Length
* sizeof (*Destination
));
153 Returns the length of a Null-terminated Unicode string.
155 This function returns the number of Unicode characters in the Null-terminated
156 Unicode string specified by String.
158 If String is NULL, then ASSERT().
159 If String is not aligned on a 16-bit boundary, then ASSERT().
160 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
161 PcdMaximumUnicodeStringLength Unicode characters, not including the
162 Null-terminator, then ASSERT().
164 @param String Pointer to a Null-terminated Unicode string.
166 @return The length of String.
172 IN CONST CHAR16
*String
177 ASSERT (String
!= NULL
);
178 ASSERT (((UINTN
) String
& BIT0
) == 0);
180 for (Length
= 0; *String
!= L
'\0'; String
++, Length
++) {
182 // If PcdMaximumUnicodeStringLength is not zero,
183 // length should not more than PcdMaximumUnicodeStringLength
185 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
186 ASSERT (Length
< PcdGet32 (PcdMaximumUnicodeStringLength
));
193 Returns the size of a Null-terminated Unicode string in bytes, including the
196 This function returns the size, in bytes, of the Null-terminated Unicode string
199 If String is NULL, then ASSERT().
200 If String is not aligned on a 16-bit boundary, then ASSERT().
201 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
202 PcdMaximumUnicodeStringLength Unicode characters, not including the
203 Null-terminator, then ASSERT().
205 @param String Pointer to a Null-terminated Unicode string.
207 @return The size of String.
213 IN CONST CHAR16
*String
216 return (StrLen (String
) + 1) * sizeof (*String
);
220 Compares two Null-terminated Unicode strings, and returns the difference
221 between the first mismatched Unicode characters.
223 This function compares the Null-terminated Unicode string FirstString to the
224 Null-terminated Unicode string SecondString. If FirstString is identical to
225 SecondString, then 0 is returned. Otherwise, the value returned is the first
226 mismatched Unicode character in SecondString subtracted from the first
227 mismatched Unicode character in FirstString.
229 If FirstString is NULL, then ASSERT().
230 If FirstString is not aligned on a 16-bit boundary, then ASSERT().
231 If SecondString is NULL, then ASSERT().
232 If SecondString is not aligned on a 16-bit boundary, then ASSERT().
233 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more
234 than PcdMaximumUnicodeStringLength Unicode characters, not including the
235 Null-terminator, then ASSERT().
236 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more
237 than PcdMaximumUnicodeStringLength Unicode characters, not including the
238 Null-terminator, then ASSERT().
240 @param FirstString Pointer to a Null-terminated Unicode string.
241 @param SecondString Pointer to a Null-terminated Unicode string.
243 @retval 0 FirstString is identical to SecondString.
244 @return others FirstString is not identical to SecondString.
250 IN CONST CHAR16
*FirstString
,
251 IN CONST CHAR16
*SecondString
255 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength
257 ASSERT (StrSize (FirstString
) != 0);
258 ASSERT (StrSize (SecondString
) != 0);
260 while ((*FirstString
!= L
'\0') && (*FirstString
== *SecondString
)) {
264 return *FirstString
- *SecondString
;
268 Compares up to a specified length the contents of two Null-terminated Unicode strings,
269 and returns the difference between the first mismatched Unicode characters.
271 This function compares the Null-terminated Unicode string FirstString to the
272 Null-terminated Unicode string SecondString. At most, Length Unicode
273 characters will be compared. If Length is 0, then 0 is returned. If
274 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
275 value returned is the first mismatched Unicode character in SecondString
276 subtracted from the first mismatched Unicode character in FirstString.
278 If Length > 0 and FirstString is NULL, then ASSERT().
279 If Length > 0 and FirstString is not aligned on a 16-bit boundary, then ASSERT().
280 If Length > 0 and SecondString is NULL, then ASSERT().
281 If Length > 0 and SecondString is not aligned on a 16-bit boundary, then ASSERT().
282 If PcdMaximumUnicodeStringLength is not zero, and Length is greater than
283 PcdMaximumUnicodeStringLength, then ASSERT().
284 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more than
285 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
287 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more than
288 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
291 @param FirstString Pointer to a Null-terminated Unicode string.
292 @param SecondString Pointer to a Null-terminated Unicode string.
293 @param Length Maximum number of Unicode characters to compare.
295 @retval 0 FirstString is identical to SecondString.
296 @return others FirstString is not identical to SecondString.
302 IN CONST CHAR16
*FirstString
,
303 IN CONST CHAR16
*SecondString
,
312 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
313 // Length tests are performed inside StrLen().
315 ASSERT (StrSize (FirstString
) != 0);
316 ASSERT (StrSize (SecondString
) != 0);
318 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
319 ASSERT (Length
<= PcdGet32 (PcdMaximumUnicodeStringLength
));
322 while ((*FirstString
!= L
'\0') &&
323 (*FirstString
== *SecondString
) &&
330 return *FirstString
- *SecondString
;
334 Concatenates one Null-terminated Unicode string to another Null-terminated
335 Unicode string, and returns the concatenated Unicode string.
337 This function concatenates two Null-terminated Unicode strings. The contents
338 of Null-terminated Unicode string Source are concatenated to the end of
339 Null-terminated Unicode string Destination. The Null-terminated concatenated
340 Unicode String is returned. If Source and Destination overlap, then the
341 results are undefined.
343 If Destination is NULL, then ASSERT().
344 If Destination is not aligned on a 16-bit boundary, then ASSERT().
345 If Source is NULL, then ASSERT().
346 If Source is not aligned on a 16-bit boundary, then ASSERT().
347 If Source and Destination overlap, then ASSERT().
348 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
349 than PcdMaximumUnicodeStringLength Unicode characters, not including the
350 Null-terminator, then ASSERT().
351 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
352 PcdMaximumUnicodeStringLength Unicode characters, not including the
353 Null-terminator, then ASSERT().
354 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
355 and Source results in a Unicode string with more than
356 PcdMaximumUnicodeStringLength Unicode characters, not including the
357 Null-terminator, then ASSERT().
359 @param Destination Pointer to a Null-terminated Unicode string.
360 @param Source Pointer to a Null-terminated Unicode string.
368 IN OUT CHAR16
*Destination
,
369 IN CONST CHAR16
*Source
372 StrCpy (Destination
+ StrLen (Destination
), Source
);
375 // Size of the resulting string should never be zero.
376 // PcdMaximumUnicodeStringLength is tested inside StrLen().
378 ASSERT (StrSize (Destination
) != 0);
383 Concatenates up to a specified length one Null-terminated Unicode to the end
384 of another Null-terminated Unicode string, and returns the concatenated
387 This function concatenates two Null-terminated Unicode strings. The contents
388 of Null-terminated Unicode string Source are concatenated to the end of
389 Null-terminated Unicode string Destination, and Destination is returned. At
390 most, Length Unicode characters are concatenated from Source to the end of
391 Destination, and Destination is always Null-terminated. If Length is 0, then
392 Destination is returned unmodified. If Source and Destination overlap, then
393 the results are undefined.
395 If Destination is NULL, then ASSERT().
396 If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().
397 If Length > 0 and Source is NULL, then ASSERT().
398 If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().
399 If Source and Destination overlap, then ASSERT().
400 If PcdMaximumUnicodeStringLength is not zero, and Length is greater than
401 PcdMaximumUnicodeStringLength, then ASSERT().
402 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
403 than PcdMaximumUnicodeStringLength Unicode characters, not including the
404 Null-terminator, then ASSERT().
405 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
406 PcdMaximumUnicodeStringLength Unicode characters, not including the
407 Null-terminator, then ASSERT().
408 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
409 and Source results in a Unicode string with more than PcdMaximumUnicodeStringLength
410 Unicode characters, not including the Null-terminator, then ASSERT().
412 @param Destination Pointer to a Null-terminated Unicode string.
413 @param Source Pointer to a Null-terminated Unicode string.
414 @param Length Maximum number of Unicode characters to concatenate from
423 IN OUT CHAR16
*Destination
,
424 IN CONST CHAR16
*Source
,
428 StrnCpy (Destination
+ StrLen (Destination
), Source
, Length
);
431 // Size of the resulting string should never be zero.
432 // PcdMaximumUnicodeStringLength is tested inside StrLen().
434 ASSERT (StrSize (Destination
) != 0);
439 Returns the first occurrence of a Null-terminated Unicode sub-string
440 in a Null-terminated Unicode string.
442 This function scans the contents of the Null-terminated Unicode string
443 specified by String and returns the first occurrence of SearchString.
444 If SearchString is not found in String, then NULL is returned. If
445 the length of SearchString is zero, then String is
448 If String is NULL, then ASSERT().
449 If String is not aligned on a 16-bit boundary, then ASSERT().
450 If SearchString is NULL, then ASSERT().
451 If SearchString is not aligned on a 16-bit boundary, then ASSERT().
453 If PcdMaximumUnicodeStringLength is not zero, and SearchString
454 or String contains more than PcdMaximumUnicodeStringLength Unicode
455 characters, not including the Null-terminator, then ASSERT().
457 @param String Pointer to a Null-terminated Unicode string.
458 @param SearchString Pointer to a Null-terminated Unicode string to search for.
460 @retval NULL If the SearchString does not appear in String.
461 @return others If there is a match.
467 IN CONST CHAR16
*String
,
468 IN CONST CHAR16
*SearchString
471 CONST CHAR16
*FirstMatch
;
472 CONST CHAR16
*SearchStringTmp
;
475 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
476 // Length tests are performed inside StrLen().
478 ASSERT (StrSize (String
) != 0);
479 ASSERT (StrSize (SearchString
) != 0);
481 if (*SearchString
== L
'\0') {
482 return (CHAR16
*) String
;
485 while (*String
!= L
'\0') {
486 SearchStringTmp
= SearchString
;
489 while ((*String
== *SearchStringTmp
)
490 && (*String
!= L
'\0')) {
495 if (*SearchStringTmp
== L
'\0') {
496 return (CHAR16
*) FirstMatch
;
499 if (*String
== L
'\0') {
503 String
= FirstMatch
+ 1;
510 Check if a Unicode character is a decimal character.
512 This internal function checks if a Unicode character is a
513 decimal character. The valid decimal character is from
516 @param Char The character to check against.
518 @retval TRUE If the Char is a decmial character.
519 @retval FALSE If the Char is not a decmial character.
524 InternalIsDecimalDigitCharacter (
528 return (BOOLEAN
) (Char
>= L
'0' && Char
<= L
'9');
532 Convert a Unicode character to upper case only if
533 it maps to a valid small-case ASCII character.
535 This internal function only deal with Unicode character
536 which maps to a valid small-case ASCII character, i.e.
537 L'a' to L'z'. For other Unicode character, the input character
538 is returned directly.
540 @param Char The character to convert.
542 @retval LowerCharacter If the Char is with range L'a' to L'z'.
543 @retval Unchanged Otherwise.
548 InternalCharToUpper (
552 if (Char
>= L
'a' && Char
<= L
'z') {
553 return (CHAR16
) (Char
- (L
'a' - L
'A'));
560 Convert a Unicode character to numerical value.
562 This internal function only deal with Unicode character
563 which maps to a valid hexadecimal ASII character, i.e.
564 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
565 Unicode character, the value returned does not make sense.
567 @param Char The character to convert.
569 @return The numerical value converted.
574 InternalHexCharToUintn (
578 if (InternalIsDecimalDigitCharacter (Char
)) {
582 return (UINTN
) (10 + InternalCharToUpper (Char
) - L
'A');
586 Check if a Unicode character is a hexadecimal character.
588 This internal function checks if a Unicode character is a
589 decimal character. The valid hexadecimal character is
590 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
593 @param Char The character to check against.
595 @retval TRUE If the Char is a hexadecmial character.
596 @retval FALSE If the Char is not a hexadecmial character.
601 InternalIsHexaDecimalDigitCharacter (
606 return (BOOLEAN
) (InternalIsDecimalDigitCharacter (Char
) ||
607 (Char
>= L
'A' && Char
<= L
'F') ||
608 (Char
>= L
'a' && Char
<= L
'f'));
612 Convert a Null-terminated Unicode decimal string to a value of
615 This function returns a value of type UINTN by interpreting the contents
616 of the Unicode string specified by String as a decimal number. The format
617 of the input Unicode string String is:
619 [spaces] [decimal digits].
621 The valid decimal digit character is in the range [0-9]. The
622 function will ignore the pad space, which includes spaces or
623 tab characters, before [decimal digits]. The running zero in the
624 beginning of [decimal digits] will be ignored. Then, the function
625 stops at the first character that is a not a valid decimal character
626 or a Null-terminator, whichever one comes first.
628 If String is NULL, then ASSERT().
629 If String is not aligned in a 16-bit boundary, then ASSERT().
630 If String has only pad spaces, then 0 is returned.
631 If String has no pad spaces or valid decimal digits,
633 If the number represented by String overflows according
634 to the range defined by UINTN, then ASSERT().
636 If PcdMaximumUnicodeStringLength is not zero, and String contains
637 more than PcdMaximumUnicodeStringLength Unicode characters, not including
638 the Null-terminator, then ASSERT().
640 @param String Pointer to a Null-terminated Unicode string.
642 @retval Value translated from String.
648 IN CONST CHAR16
*String
654 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
655 // Length tests are performed inside StrLen().
657 ASSERT (StrSize (String
) != 0);
660 // Ignore the pad spaces (space or tab)
662 while ((*String
== L
' ') || (*String
== L
'\t')) {
667 // Ignore leading Zeros after the spaces
669 while (*String
== L
'0') {
675 while (InternalIsDecimalDigitCharacter (*String
)) {
677 // If the number represented by String overflows according
678 // to the range defined by UINTN, then ASSERT().
680 ASSERT ((Result
< QUOTIENT_MAX_UINTN_DIVIDED_BY_10
) ||
681 ((Result
== QUOTIENT_MAX_UINTN_DIVIDED_BY_10
) &&
682 (*String
- L
'0') <= REMAINDER_MAX_UINTN_DIVIDED_BY_10
)
685 Result
= Result
* 10 + (*String
- L
'0');
694 Convert a Null-terminated Unicode decimal string to a value of
697 This function returns a value of type UINT64 by interpreting the contents
698 of the Unicode string specified by String as a decimal number. The format
699 of the input Unicode string String is:
701 [spaces] [decimal digits].
703 The valid decimal digit character is in the range [0-9]. The
704 function will ignore the pad space, which includes spaces or
705 tab characters, before [decimal digits]. The running zero in the
706 beginning of [decimal digits] will be ignored. Then, the function
707 stops at the first character that is a not a valid decimal character
708 or a Null-terminator, whichever one comes first.
710 If String is NULL, then ASSERT().
711 If String is not aligned in a 16-bit boundary, then ASSERT().
712 If String has only pad spaces, then 0 is returned.
713 If String has no pad spaces or valid decimal digits,
715 If the number represented by String overflows according
716 to the range defined by UINT64, then ASSERT().
718 If PcdMaximumUnicodeStringLength is not zero, and String contains
719 more than PcdMaximumUnicodeStringLength Unicode characters, not including
720 the Null-terminator, then ASSERT().
722 @param String Pointer to a Null-terminated Unicode string.
724 @retval Value translated from String.
730 IN CONST CHAR16
*String
736 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
737 // Length tests are performed inside StrLen().
739 ASSERT (StrSize (String
) != 0);
742 // Ignore the pad spaces (space or tab)
744 while ((*String
== L
' ') || (*String
== L
'\t')) {
749 // Ignore leading Zeros after the spaces
751 while (*String
== L
'0') {
757 while (InternalIsDecimalDigitCharacter (*String
)) {
759 // If the number represented by String overflows according
760 // to the range defined by UINTN, then ASSERT().
762 ASSERT ((Result
< QUOTIENT_MAX_UINT64_DIVIDED_BY_10
) ||
763 ((Result
== QUOTIENT_MAX_UINT64_DIVIDED_BY_10
) &&
764 (*String
- L
'0') <= REMAINDER_MAX_UINT64_DIVIDED_BY_10
)
767 Result
= MultU64x32 (Result
, 10) + (*String
- L
'0');
775 Convert a Null-terminated Unicode hexadecimal string to a value of type UINTN.
777 This function returns a value of type UINTN by interpreting the contents
778 of the Unicode string specified by String as a hexadecimal number.
779 The format of the input Unicode string String is:
781 [spaces][zeros][x][hexadecimal digits].
783 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
784 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
785 If "x" appears in the input string, it must be prefixed with at least one 0.
786 The function will ignore the pad space, which includes spaces or tab characters,
787 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
788 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
789 first valid hexadecimal digit. Then, the function stops at the first character that is
790 a not a valid hexadecimal character or NULL, whichever one comes first.
792 If String is NULL, then ASSERT().
793 If String is not aligned in a 16-bit boundary, then ASSERT().
794 If String has only pad spaces, then zero is returned.
795 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
796 then zero is returned.
797 If the number represented by String overflows according to the range defined by
798 UINTN, then ASSERT().
800 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
801 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
804 @param String Pointer to a Null-terminated Unicode string.
806 @retval Value translated from String.
812 IN CONST CHAR16
*String
818 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
819 // Length tests are performed inside StrLen().
821 ASSERT (StrSize (String
) != 0);
824 // Ignore the pad spaces (space or tab)
826 while ((*String
== L
' ') || (*String
== L
'\t')) {
831 // Ignore leading Zeros after the spaces
833 while (*String
== L
'0') {
837 if (InternalCharToUpper (*String
) == L
'X') {
838 if (*(String
- 1) != L
'0') {
849 while (InternalIsHexaDecimalDigitCharacter (*String
)) {
851 // If the Hex Number represented by String overflows according
852 // to the range defined by UINTN, then ASSERT().
854 ASSERT ((Result
< QUOTIENT_MAX_UINTN_DIVIDED_BY_16
) ||
855 ((Result
== QUOTIENT_MAX_UINTN_DIVIDED_BY_16
) &&
856 (InternalHexCharToUintn (*String
) <= REMAINDER_MAX_UINTN_DIVIDED_BY_16
))
859 Result
= (Result
<< 4) + InternalHexCharToUintn (*String
);
868 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
870 This function returns a value of type UINT64 by interpreting the contents
871 of the Unicode string specified by String as a hexadecimal number.
872 The format of the input Unicode string String is
874 [spaces][zeros][x][hexadecimal digits].
876 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
877 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
878 If "x" appears in the input string, it must be prefixed with at least one 0.
879 The function will ignore the pad space, which includes spaces or tab characters,
880 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
881 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
882 first valid hexadecimal digit. Then, the function stops at the first character that is
883 a not a valid hexadecimal character or NULL, whichever one comes first.
885 If String is NULL, then ASSERT().
886 If String is not aligned in a 16-bit boundary, then ASSERT().
887 If String has only pad spaces, then zero is returned.
888 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
889 then zero is returned.
890 If the number represented by String overflows according to the range defined by
891 UINT64, then ASSERT().
893 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
894 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
897 @param String Pointer to a Null-terminated Unicode string.
899 @retval Value translated from String.
905 IN CONST CHAR16
*String
911 // ASSERT String is less long than PcdMaximumUnicodeStringLength.
912 // Length tests are performed inside StrLen().
914 ASSERT (StrSize (String
) != 0);
917 // Ignore the pad spaces (space or tab)
919 while ((*String
== L
' ') || (*String
== L
'\t')) {
924 // Ignore leading Zeros after the spaces
926 while (*String
== L
'0') {
930 if (InternalCharToUpper (*String
) == L
'X') {
931 ASSERT (*(String
- 1) == L
'0');
932 if (*(String
- 1) != L
'0') {
943 while (InternalIsHexaDecimalDigitCharacter (*String
)) {
945 // If the Hex Number represented by String overflows according
946 // to the range defined by UINTN, then ASSERT().
948 ASSERT ((Result
< QUOTIENT_MAX_UINT64_DIVIDED_BY_16
)||
949 ((Result
== QUOTIENT_MAX_UINT64_DIVIDED_BY_16
) &&
950 (InternalHexCharToUintn (*String
) <= REMAINDER_MAX_UINT64_DIVIDED_BY_16
))
953 Result
= LShiftU64 (Result
, 4);
954 Result
= Result
+ InternalHexCharToUintn (*String
);
962 Check if a ASCII character is a decimal character.
964 This internal function checks if a Unicode character is a
965 decimal character. The valid decimal character is from
968 @param Char The character to check against.
970 @retval TRUE If the Char is a decmial character.
971 @retval FALSE If the Char is not a decmial character.
976 InternalAsciiIsDecimalDigitCharacter (
980 return (BOOLEAN
) (Char
>= '0' && Char
<= '9');
984 Check if a ASCII character is a hexadecimal character.
986 This internal function checks if a ASCII character is a
987 decimal character. The valid hexadecimal character is
988 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
991 @param Char The character to check against.
993 @retval TRUE If the Char is a hexadecmial character.
994 @retval FALSE If the Char is not a hexadecmial character.
999 InternalAsciiIsHexaDecimalDigitCharacter (
1004 return (BOOLEAN
) (InternalAsciiIsDecimalDigitCharacter (Char
) ||
1005 (Char
>= 'A' && Char
<= 'F') ||
1006 (Char
>= 'a' && Char
<= 'f'));
1010 Convert a Null-terminated Unicode string to a Null-terminated
1011 ASCII string and returns the ASCII string.
1013 This function converts the content of the Unicode string Source
1014 to the ASCII string Destination by copying the lower 8 bits of
1015 each Unicode character. It returns Destination.
1017 If any Unicode characters in Source contain non-zero value in
1018 the upper 8 bits, then ASSERT().
1020 If Destination is NULL, then ASSERT().
1021 If Source is NULL, then ASSERT().
1022 If Source is not aligned on a 16-bit boundary, then ASSERT().
1023 If Source and Destination overlap, then ASSERT().
1025 If PcdMaximumUnicodeStringLength is not zero, and Source contains
1026 more than PcdMaximumUnicodeStringLength Unicode characters, not including
1027 the Null-terminator, then ASSERT().
1029 If PcdMaximumAsciiStringLength is not zero, and Source contains more
1030 than PcdMaximumAsciiStringLength Unicode characters, not including the
1031 Null-terminator, then ASSERT().
1033 @param Source Pointer to a Null-terminated Unicode string.
1034 @param Destination Pointer to a Null-terminated ASCII string.
1036 @return Destination.
1041 UnicodeStrToAsciiStr (
1042 IN CONST CHAR16
*Source
,
1043 OUT CHAR8
*Destination
1048 ASSERT (Destination
!= NULL
);
1051 // ASSERT if Source is long than PcdMaximumUnicodeStringLength.
1052 // Length tests are performed inside StrLen().
1054 ASSERT (StrSize (Source
) != 0);
1057 // Source and Destination should not overlap
1059 ASSERT ((UINTN
) ((CHAR16
*) Destination
- Source
) > StrLen (Source
));
1060 ASSERT ((UINTN
) ((CHAR8
*) Source
- Destination
) > StrLen (Source
));
1063 ReturnValue
= Destination
;
1064 while (*Source
!= '\0') {
1066 // If any Unicode characters in Source contain
1067 // non-zero value in the upper 8 bits, then ASSERT().
1069 ASSERT (*Source
< 0x100);
1070 *(Destination
++) = (CHAR8
) *(Source
++);
1073 *Destination
= '\0';
1076 // ASSERT Original Destination is less long than PcdMaximumAsciiStringLength.
1077 // Length tests are performed inside AsciiStrLen().
1079 ASSERT (AsciiStrSize (ReturnValue
) != 0);
1086 Copies one Null-terminated ASCII string to another Null-terminated ASCII
1087 string and returns the new ASCII string.
1089 This function copies the contents of the ASCII string Source to the ASCII
1090 string Destination, and returns Destination. If Source and Destination
1091 overlap, then the results are undefined.
1093 If Destination is NULL, then ASSERT().
1094 If Source is NULL, then ASSERT().
1095 If Source and Destination overlap, then ASSERT().
1096 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1097 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1100 @param Destination Pointer to a Null-terminated ASCII string.
1101 @param Source Pointer to a Null-terminated ASCII string.
1109 OUT CHAR8
*Destination
,
1110 IN CONST CHAR8
*Source
1116 // Destination cannot be NULL
1118 ASSERT (Destination
!= NULL
);
1121 // Destination and source cannot overlap
1123 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
1124 ASSERT ((UINTN
)(Source
- Destination
) > AsciiStrLen (Source
));
1126 ReturnValue
= Destination
;
1127 while (*Source
!= 0) {
1128 *(Destination
++) = *(Source
++);
1135 Copies up to a specified length one Null-terminated ASCII string to another
1136 Null-terminated ASCII string and returns the new ASCII string.
1138 This function copies the contents of the ASCII string Source to the ASCII
1139 string Destination, and returns Destination. At most, Length ASCII characters
1140 are copied from Source to Destination. If Length is 0, then Destination is
1141 returned unmodified. If Length is greater that the number of ASCII characters
1142 in Source, then Destination is padded with Null ASCII characters. If Source
1143 and Destination overlap, then the results are undefined.
1145 If Destination is NULL, then ASSERT().
1146 If Source is NULL, then ASSERT().
1147 If Source and Destination overlap, then ASSERT().
1148 If PcdMaximumAsciiStringLength is not zero, and Length is greater than
1149 PcdMaximumAsciiStringLength, then ASSERT().
1150 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1151 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1154 @param Destination Pointer to a Null-terminated ASCII string.
1155 @param Source Pointer to a Null-terminated ASCII string.
1156 @param Length Maximum number of ASCII characters to copy.
1164 OUT CHAR8
*Destination
,
1165 IN CONST CHAR8
*Source
,
1176 // Destination cannot be NULL
1178 ASSERT (Destination
!= NULL
);
1181 // Destination and source cannot overlap
1183 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
1184 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
1186 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1187 ASSERT (Length
<= PcdGet32 (PcdMaximumAsciiStringLength
));
1190 ReturnValue
= Destination
;
1192 while (*Source
!= 0 && Length
> 0) {
1193 *(Destination
++) = *(Source
++);
1197 ZeroMem (Destination
, Length
* sizeof (*Destination
));
1202 Returns the length of a Null-terminated ASCII string.
1204 This function returns the number of ASCII characters in the Null-terminated
1205 ASCII string specified by String.
1207 If Length > 0 and Destination is NULL, then ASSERT().
1208 If Length > 0 and Source is NULL, then ASSERT().
1209 If PcdMaximumAsciiStringLength is not zero and String contains more than
1210 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1213 @param String Pointer to a Null-terminated ASCII string.
1215 @return The length of String.
1221 IN CONST CHAR8
*String
1226 ASSERT (String
!= NULL
);
1228 for (Length
= 0; *String
!= '\0'; String
++, Length
++) {
1230 // If PcdMaximumUnicodeStringLength is not zero,
1231 // length should not more than PcdMaximumUnicodeStringLength
1233 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1234 ASSERT (Length
< PcdGet32 (PcdMaximumAsciiStringLength
));
1241 Returns the size of a Null-terminated ASCII string in bytes, including the
1244 This function returns the size, in bytes, of the Null-terminated ASCII string
1245 specified by String.
1247 If String is NULL, then ASSERT().
1248 If PcdMaximumAsciiStringLength is not zero and String contains more than
1249 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1252 @param String Pointer to a Null-terminated ASCII string.
1254 @return The size of String.
1260 IN CONST CHAR8
*String
1263 return (AsciiStrLen (String
) + 1) * sizeof (*String
);
1267 Compares two Null-terminated ASCII strings, and returns the difference
1268 between the first mismatched ASCII characters.
1270 This function compares the Null-terminated ASCII string FirstString to the
1271 Null-terminated ASCII string SecondString. If FirstString is identical to
1272 SecondString, then 0 is returned. Otherwise, the value returned is the first
1273 mismatched ASCII character in SecondString subtracted from the first
1274 mismatched ASCII character in FirstString.
1276 If FirstString is NULL, then ASSERT().
1277 If SecondString is NULL, then ASSERT().
1278 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1279 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1281 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
1282 than PcdMaximumAsciiStringLength ASCII characters, not including the
1283 Null-terminator, then ASSERT().
1285 @param FirstString Pointer to a Null-terminated ASCII string.
1286 @param SecondString Pointer to a Null-terminated ASCII string.
1288 @retval ==0 FirstString is identical to SecondString.
1289 @retval !=0 FirstString is not identical to SecondString.
1295 IN CONST CHAR8
*FirstString
,
1296 IN CONST CHAR8
*SecondString
1300 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1302 ASSERT (AsciiStrSize (FirstString
));
1303 ASSERT (AsciiStrSize (SecondString
));
1305 while ((*FirstString
!= '\0') && (*FirstString
== *SecondString
)) {
1310 return *FirstString
- *SecondString
;
1314 Converts a lowercase Ascii character to upper one.
1316 If Chr is lowercase Ascii character, then converts it to upper one.
1318 If Value >= 0xA0, then ASSERT().
1319 If (Value & 0x0F) >= 0x0A, then ASSERT().
1321 @param Chr one Ascii character
1323 @return The uppercase value of Ascii character
1332 return (UINT8
) ((Chr
>= 'a' && Chr
<= 'z') ? Chr
- ('a' - 'A') : Chr
);
1336 Convert a ASCII character to numerical value.
1338 This internal function only deal with Unicode character
1339 which maps to a valid hexadecimal ASII character, i.e.
1340 '0' to '9', 'a' to 'f' or 'A' to 'F'. For other
1341 ASCII character, the value returned does not make sense.
1343 @param Char The character to convert.
1345 @return The numerical value converted.
1350 InternalAsciiHexCharToUintn (
1354 if (InternalIsDecimalDigitCharacter (Char
)) {
1358 return (UINTN
) (10 + AsciiToUpper (Char
) - 'A');
1363 Performs a case insensitive comparison of two Null-terminated ASCII strings,
1364 and returns the difference between the first mismatched ASCII characters.
1366 This function performs a case insensitive comparison of the Null-terminated
1367 ASCII string FirstString to the Null-terminated ASCII string SecondString. If
1368 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
1369 value returned is the first mismatched lower case ASCII character in
1370 SecondString subtracted from the first mismatched lower case ASCII character
1373 If FirstString is NULL, then ASSERT().
1374 If SecondString is NULL, then ASSERT().
1375 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1376 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1378 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
1379 than PcdMaximumAsciiStringLength ASCII characters, not including the
1380 Null-terminator, then ASSERT().
1382 @param FirstString Pointer to a Null-terminated ASCII string.
1383 @param SecondString Pointer to a Null-terminated ASCII string.
1385 @retval ==0 FirstString is identical to SecondString using case insensitive
1387 @retval !=0 FirstString is not identical to SecondString using case
1388 insensitive comparisons.
1394 IN CONST CHAR8
*FirstString
,
1395 IN CONST CHAR8
*SecondString
1398 CHAR8 UpperFirstString
;
1399 CHAR8 UpperSecondString
;
1402 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1404 ASSERT (AsciiStrSize (FirstString
));
1405 ASSERT (AsciiStrSize (SecondString
));
1407 UpperFirstString
= AsciiToUpper (*FirstString
);
1408 UpperSecondString
= AsciiToUpper (*SecondString
);
1409 while ((*FirstString
!= '\0') && (UpperFirstString
== UpperSecondString
)) {
1412 UpperFirstString
= AsciiToUpper (*FirstString
);
1413 UpperSecondString
= AsciiToUpper (*SecondString
);
1416 return UpperFirstString
- UpperSecondString
;
1420 Compares two Null-terminated ASCII strings with maximum lengths, and returns
1421 the difference between the first mismatched ASCII characters.
1423 This function compares the Null-terminated ASCII string FirstString to the
1424 Null-terminated ASCII string SecondString. At most, Length ASCII characters
1425 will be compared. If Length is 0, then 0 is returned. If FirstString is
1426 identical to SecondString, then 0 is returned. Otherwise, the value returned
1427 is the first mismatched ASCII character in SecondString subtracted from the
1428 first mismatched ASCII character in FirstString.
1430 If Length > 0 and FirstString is NULL, then ASSERT().
1431 If Length > 0 and SecondString is NULL, then ASSERT().
1432 If PcdMaximumAsciiStringLength is not zero, and Length is greater than
1433 PcdMaximumAsciiStringLength, then ASSERT().
1434 If PcdMaximumAsciiStringLength is not zero, and FirstString contains more than
1435 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1437 If PcdMaximumAsciiStringLength is not zero, and SecondString contains more than
1438 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1441 @param FirstString Pointer to a Null-terminated ASCII string.
1442 @param SecondString Pointer to a Null-terminated ASCII string.
1443 @param Length Maximum number of ASCII characters for compare.
1445 @retval ==0 FirstString is identical to SecondString.
1446 @retval !=0 FirstString is not identical to SecondString.
1452 IN CONST CHAR8
*FirstString
,
1453 IN CONST CHAR8
*SecondString
,
1462 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1464 ASSERT (AsciiStrSize (FirstString
));
1465 ASSERT (AsciiStrSize (SecondString
));
1467 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1468 ASSERT (Length
<= PcdGet32 (PcdMaximumAsciiStringLength
));
1471 while ((*FirstString
!= '\0') &&
1472 (*FirstString
== *SecondString
) &&
1478 return *FirstString
- *SecondString
;
1482 Concatenates one Null-terminated ASCII string to another Null-terminated
1483 ASCII string, and returns the concatenated ASCII string.
1485 This function concatenates two Null-terminated ASCII strings. The contents of
1486 Null-terminated ASCII string Source are concatenated to the end of Null-
1487 terminated ASCII string Destination. The Null-terminated concatenated ASCII
1490 If Destination is NULL, then ASSERT().
1491 If Source is NULL, then ASSERT().
1492 If PcdMaximumAsciiStringLength is not zero and Destination contains more than
1493 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1495 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1496 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1498 If PcdMaximumAsciiStringLength is not zero and concatenating Destination and
1499 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1500 ASCII characters, then ASSERT().
1502 @param Destination Pointer to a Null-terminated ASCII string.
1503 @param Source Pointer to a Null-terminated ASCII string.
1511 IN OUT CHAR8
*Destination
,
1512 IN CONST CHAR8
*Source
1515 AsciiStrCpy (Destination
+ AsciiStrLen (Destination
), Source
);
1518 // Size of the resulting string should never be zero.
1519 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1521 ASSERT (AsciiStrSize (Destination
) != 0);
1526 Concatenates up to a specified length one Null-terminated ASCII string to
1527 the end of another Null-terminated ASCII string, and returns the
1528 concatenated ASCII string.
1530 This function concatenates two Null-terminated ASCII strings. The contents
1531 of Null-terminated ASCII string Source are concatenated to the end of Null-
1532 terminated ASCII string Destination, and Destination is returned. At most,
1533 Length ASCII characters are concatenated from Source to the end of
1534 Destination, and Destination is always Null-terminated. If Length is 0, then
1535 Destination is returned unmodified. If Source and Destination overlap, then
1536 the results are undefined.
1538 If Length > 0 and Destination is NULL, then ASSERT().
1539 If Length > 0 and Source is NULL, then ASSERT().
1540 If Source and Destination overlap, then ASSERT().
1541 If PcdMaximumAsciiStringLength is not zero, and Length is greater than
1542 PcdMaximumAsciiStringLength, then ASSERT().
1543 If PcdMaximumAsciiStringLength is not zero, and Destination contains more than
1544 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1546 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1547 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1549 If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and
1550 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1551 ASCII characters, not including the Null-terminator, then ASSERT().
1553 @param Destination Pointer to a Null-terminated ASCII string.
1554 @param Source Pointer to a Null-terminated ASCII string.
1555 @param Length Maximum number of ASCII characters to concatenate from
1564 IN OUT CHAR8
*Destination
,
1565 IN CONST CHAR8
*Source
,
1569 AsciiStrnCpy (Destination
+ AsciiStrLen (Destination
), Source
, Length
);
1572 // Size of the resulting string should never be zero.
1573 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1575 ASSERT (AsciiStrSize (Destination
) != 0);
1580 Returns the first occurrence of a Null-terminated ASCII sub-string
1581 in a Null-terminated ASCII string.
1583 This function scans the contents of the ASCII string specified by String
1584 and returns the first occurrence of SearchString. If SearchString is not
1585 found in String, then NULL is returned. If the length of SearchString is zero,
1586 then String is returned.
1588 If String is NULL, then ASSERT().
1589 If SearchString is NULL, then ASSERT().
1591 If PcdMaximumAsciiStringLength is not zero, and SearchString or
1592 String contains more than PcdMaximumAsciiStringLength Unicode characters
1593 not including the Null-terminator, then ASSERT().
1595 @param String Pointer to a Null-terminated ASCII string.
1596 @param SearchString Pointer to a Null-terminated ASCII string to search for.
1598 @retval NULL If the SearchString does not appear in String.
1599 @retval others If there is a match return the first occurrence of SearchingString.
1600 If the length of SearchString is zero,return String.
1606 IN CONST CHAR8
*String
,
1607 IN CONST CHAR8
*SearchString
1610 CONST CHAR8
*FirstMatch
;
1611 CONST CHAR8
*SearchStringTmp
;
1614 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1616 ASSERT (AsciiStrSize (String
) != 0);
1617 ASSERT (AsciiStrSize (SearchString
) != 0);
1619 if (*SearchString
== '\0') {
1620 return (CHAR8
*) String
;
1623 while (*String
!= '\0') {
1624 SearchStringTmp
= SearchString
;
1625 FirstMatch
= String
;
1627 while ((*String
== *SearchStringTmp
)
1628 && (*String
!= '\0')) {
1633 if (*SearchStringTmp
== '\0') {
1634 return (CHAR8
*) FirstMatch
;
1637 if (*String
== '\0') {
1641 String
= FirstMatch
+ 1;
1648 Convert a Null-terminated ASCII decimal string to a value of type
1651 This function returns a value of type UINTN by interpreting the contents
1652 of the ASCII string String as a decimal number. The format of the input
1653 ASCII string String is:
1655 [spaces] [decimal digits].
1657 The valid decimal digit character is in the range [0-9]. The function will
1658 ignore the pad space, which includes spaces or tab characters, before the digits.
1659 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1660 function stops at the first character that is a not a valid decimal character or
1661 Null-terminator, whichever on comes first.
1663 If String has only pad spaces, then 0 is returned.
1664 If String has no pad spaces or valid decimal digits, then 0 is returned.
1665 If the number represented by String overflows according to the range defined by
1666 UINTN, then ASSERT().
1667 If String is NULL, then ASSERT().
1668 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1669 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1672 @param String Pointer to a Null-terminated ASCII string.
1674 @retval Value translated from String.
1679 AsciiStrDecimalToUintn (
1680 IN CONST CHAR8
*String
1686 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1688 ASSERT (AsciiStrSize (String
) != 0);
1691 // Ignore the pad spaces (space or tab)
1693 while ((*String
== ' ') || (*String
== '\t' )) {
1698 // Ignore leading Zeros after the spaces
1700 while (*String
== '0') {
1706 while (InternalAsciiIsDecimalDigitCharacter (*String
)) {
1708 // If the number represented by String overflows according
1709 // to the range defined by UINTN, then ASSERT().
1711 ASSERT ((Result
< QUOTIENT_MAX_UINTN_DIVIDED_BY_10
) ||
1712 ((Result
== QUOTIENT_MAX_UINTN_DIVIDED_BY_10
) &&
1713 (*String
- '0') <= REMAINDER_MAX_UINTN_DIVIDED_BY_10
)
1716 Result
= Result
* 10 + (*String
- '0');
1725 Convert a Null-terminated ASCII decimal string to a value of type
1728 This function returns a value of type UINT64 by interpreting the contents
1729 of the ASCII string String as a decimal number. The format of the input
1730 ASCII string String is:
1732 [spaces] [decimal digits].
1734 The valid decimal digit character is in the range [0-9]. The function will
1735 ignore the pad space, which includes spaces or tab characters, before the digits.
1736 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1737 function stops at the first character that is a not a valid decimal character or
1738 Null-terminator, whichever on comes first.
1740 If String has only pad spaces, then 0 is returned.
1741 If String has no pad spaces or valid decimal digits, then 0 is returned.
1742 If the number represented by String overflows according to the range defined by
1743 UINT64, then ASSERT().
1744 If String is NULL, then ASSERT().
1745 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1746 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1749 @param String Pointer to a Null-terminated ASCII string.
1751 @retval Value translated from String.
1756 AsciiStrDecimalToUint64 (
1757 IN CONST CHAR8
*String
1763 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1765 ASSERT (AsciiStrSize (String
) != 0);
1768 // Ignore the pad spaces (space or tab)
1770 while ((*String
== ' ') || (*String
== '\t' )) {
1775 // Ignore leading Zeros after the spaces
1777 while (*String
== '0') {
1783 while (InternalAsciiIsDecimalDigitCharacter (*String
)) {
1785 // If the number represented by String overflows according
1786 // to the range defined by UINTN, then ASSERT().
1788 ASSERT ((Result
< QUOTIENT_MAX_UINT64_DIVIDED_BY_10
) ||
1789 ((Result
== QUOTIENT_MAX_UINT64_DIVIDED_BY_10
) &&
1790 (*String
- '0') <= REMAINDER_MAX_UINT64_DIVIDED_BY_10
)
1793 Result
= MultU64x32 (Result
, 10) + (*String
- '0');
1801 Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.
1803 This function returns a value of type UINTN by interpreting the contents of
1804 the ASCII string String as a hexadecimal number. The format of the input ASCII
1807 [spaces][zeros][x][hexadecimal digits].
1809 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1810 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1811 appears in the input string, it must be prefixed with at least one 0. The function
1812 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1813 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1814 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1815 digit. Then, the function stops at the first character that is a not a valid
1816 hexadecimal character or Null-terminator, whichever on comes first.
1818 If String has only pad spaces, then 0 is returned.
1819 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1822 If the number represented by String overflows according to the range defined by UINTN,
1824 If String is NULL, then ASSERT().
1825 If PcdMaximumAsciiStringLength is not zero,
1826 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1827 the Null-terminator, then ASSERT().
1829 @param String Pointer to a Null-terminated ASCII string.
1831 @retval Value translated from String.
1836 AsciiStrHexToUintn (
1837 IN CONST CHAR8
*String
1843 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1845 ASSERT (AsciiStrSize (String
) != 0);
1848 // Ignore the pad spaces (space or tab)
1850 while ((*String
== ' ') || (*String
== '\t' )) {
1855 // Ignore leading Zeros after the spaces
1857 while (*String
== '0') {
1861 if (AsciiToUpper (*String
) == 'X') {
1862 ASSERT (*(String
- 1) == '0');
1863 if (*(String
- 1) != '0') {
1874 while (InternalAsciiIsHexaDecimalDigitCharacter (*String
)) {
1876 // If the Hex Number represented by String overflows according
1877 // to the range defined by UINTN, then ASSERT().
1879 ASSERT ((Result
< QUOTIENT_MAX_UINTN_DIVIDED_BY_16
) ||
1880 ((Result
== QUOTIENT_MAX_UINTN_DIVIDED_BY_16
) &&
1881 (InternalAsciiHexCharToUintn (*String
) <= REMAINDER_MAX_UINTN_DIVIDED_BY_16
))
1884 Result
= (Result
<< 4) + InternalAsciiHexCharToUintn (*String
);
1893 Convert a Null-terminated ASCII hexadecimal string to a value of type UINT64.
1895 This function returns a value of type UINT64 by interpreting the contents of
1896 the ASCII string String as a hexadecimal number. The format of the input ASCII
1899 [spaces][zeros][x][hexadecimal digits].
1901 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1902 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1903 appears in the input string, it must be prefixed with at least one 0. The function
1904 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1905 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1906 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1907 digit. Then, the function stops at the first character that is a not a valid
1908 hexadecimal character or Null-terminator, whichever on comes first.
1910 If String has only pad spaces, then 0 is returned.
1911 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1914 If the number represented by String overflows according to the range defined by UINT64,
1916 If String is NULL, then ASSERT().
1917 If PcdMaximumAsciiStringLength is not zero,
1918 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1919 the Null-terminator, then ASSERT().
1921 @param String Pointer to a Null-terminated ASCII string.
1923 @retval Value translated from String.
1928 AsciiStrHexToUint64 (
1929 IN CONST CHAR8
*String
1935 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1937 ASSERT (AsciiStrSize (String
) != 0);
1940 // Ignore the pad spaces (space or tab) and leading Zeros
1943 // Ignore the pad spaces (space or tab)
1945 while ((*String
== ' ') || (*String
== '\t' )) {
1950 // Ignore leading Zeros after the spaces
1952 while (*String
== '0') {
1956 if (AsciiToUpper (*String
) == 'X') {
1957 ASSERT (*(String
- 1) == '0');
1958 if (*(String
- 1) != '0') {
1969 while (InternalAsciiIsHexaDecimalDigitCharacter (*String
)) {
1971 // If the Hex Number represented by String overflows according
1972 // to the range defined by UINTN, then ASSERT().
1974 ASSERT ((Result
< QUOTIENT_MAX_UINT64_DIVIDED_BY_16
) ||
1975 ((Result
== QUOTIENT_MAX_UINT64_DIVIDED_BY_16
) &&
1976 (InternalAsciiHexCharToUintn (*String
) <= REMAINDER_MAX_UINT64_DIVIDED_BY_16
))
1979 Result
= LShiftU64 (Result
, 4);
1980 Result
= Result
+ InternalAsciiHexCharToUintn (*String
);
1989 Convert one Null-terminated ASCII string to a Null-terminated
1990 Unicode string and returns the Unicode string.
1992 This function converts the contents of the ASCII string Source to the Unicode
1993 string Destination, and returns Destination. The function terminates the
1994 Unicode string Destination by appending a Null-terminator character at the end.
1995 The caller is responsible to make sure Destination points to a buffer with size
1996 equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.
1998 If Destination is NULL, then ASSERT().
1999 If Destination is not aligned on a 16-bit boundary, then ASSERT().
2000 If Source is NULL, then ASSERT().
2001 If Source and Destination overlap, then ASSERT().
2002 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
2003 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
2005 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
2006 PcdMaximumUnicodeStringLength ASCII characters not including the
2007 Null-terminator, then ASSERT().
2009 @param Source Pointer to a Null-terminated ASCII string.
2010 @param Destination Pointer to a Null-terminated Unicode string.
2012 @return Destination.
2017 AsciiStrToUnicodeStr (
2018 IN CONST CHAR8
*Source
,
2019 OUT CHAR16
*Destination
2022 CHAR16
*ReturnValue
;
2024 ASSERT (Destination
!= NULL
);
2027 // ASSERT Source is less long than PcdMaximumAsciiStringLength
2029 ASSERT (AsciiStrSize (Source
) != 0);
2032 // Source and Destination should not overlap
2034 ASSERT ((UINTN
) ((CHAR8
*) Destination
- Source
) > AsciiStrLen (Source
));
2035 ASSERT ((UINTN
) (Source
- (CHAR8
*) Destination
) > (AsciiStrLen (Source
) * sizeof (CHAR16
)));
2038 ReturnValue
= Destination
;
2039 while (*Source
!= '\0') {
2040 *(Destination
++) = (CHAR16
) *(Source
++);
2043 // End the Destination with a NULL.
2045 *Destination
= '\0';
2048 // ASSERT Original Destination is less long than PcdMaximumUnicodeStringLength
2050 ASSERT (StrSize (ReturnValue
) != 0);
2056 Converts an 8-bit value to an 8-bit BCD value.
2058 Converts the 8-bit value specified by Value to BCD. The BCD value is
2061 If Value >= 100, then ASSERT().
2063 @param Value The 8-bit value to convert to BCD. Range 0..99.
2065 @return The BCD value.
2074 ASSERT (Value
< 100);
2075 return (UINT8
) (((Value
/ 10) << 4) | (Value
% 10));
2079 Converts an 8-bit BCD value to an 8-bit value.
2081 Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit
2084 If Value >= 0xA0, then ASSERT().
2085 If (Value & 0x0F) >= 0x0A, then ASSERT().
2087 @param Value The 8-bit BCD value to convert to an 8-bit value.
2089 @return The 8-bit value is returned.
2098 ASSERT (Value
< 0xa0);
2099 ASSERT ((Value
& 0xf) < 0xa);
2100 return (UINT8
) ((Value
>> 4) * 10 + (Value
& 0xf));