2 Unicode and ASCII string primatives.
4 Copyright (c) 2006 - 2007, 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.
17 #include "BaseLibInternals.h"
20 Copies one Null-terminated Unicode string to another Null-terminated Unicode
21 string and returns the new Unicode string.
23 This function copies the contents of the Unicode string Source to the Unicode
24 string Destination, and returns Destination. If Source and Destination
25 overlap, then the results are undefined.
27 If Destination is NULL, then ASSERT().
28 If Source is NULL, 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().
34 @param Destination Pointer to a Null-terminated Unicode string.
35 @param Source Pointer to a Null-terminated Unicode string.
43 OUT CHAR16
*Destination
,
44 IN CONST CHAR16
*Source
50 // Destination cannot be NULL
52 ASSERT (Destination
!= NULL
);
55 // Destination and source cannot overlap
57 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
58 ASSERT ((UINTN
)(Source
- Destination
) > StrLen (Source
));
60 ReturnValue
= Destination
;
62 *(Destination
++) = *(Source
++);
69 Copies one Null-terminated Unicode string with a maximum length to another
70 Null-terminated Unicode string with a maximum length and returns the new
73 This function copies the contents of the Unicode string Source to the Unicode
74 string Destination, and returns Destination. At most, Length Unicode
75 characters are copied from Source to Destination. If Length is 0, then
76 Destination is returned unmodified. If Length is greater that the number of
77 Unicode characters in Source, then Destination is padded with Null Unicode
78 characters. If Source and Destination overlap, then the results are
81 If Destination is NULL, then ASSERT().
82 If Source is NULL, then ASSERT().
83 If Source and Destination overlap, then ASSERT().
84 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
85 PcdMaximumUnicodeStringLength Unicode characters not including the
86 Null-terminator, then ASSERT().
88 @param Destination Pointer to a Null-terminated Unicode string.
89 @param Source Pointer to a Null-terminated Unicode string.
90 @param Length Maximum number of Unicode characters to copy.
98 OUT CHAR16
*Destination
,
99 IN CONST CHAR16
*Source
,
110 // Destination cannot be NULL if Length is not zero
112 ASSERT (Destination
!= NULL
);
115 // Destination and source cannot overlap
116 // Q: Does Source have to be NULL-terminated?
118 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
119 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
121 ReturnValue
= Destination
;
123 while ((*Source
!= L
'\0') && (Length
> 0)) {
124 *(Destination
++) = *(Source
++);
128 ZeroMem (Destination
, Length
* sizeof (*Destination
));
133 Returns the length of a Null-terminated Unicode string.
135 This function returns the number of Unicode characters in the Null-terminated
136 Unicode string specified by String.
138 If String is NULL, then ASSERT().
139 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
140 PcdMaximumUnicodeStringLength Unicode characters not including the
141 Null-terminator, then ASSERT().
143 @param String Pointer to a Null-terminated Unicode string.
145 @return The length of String.
151 IN CONST CHAR16
*String
156 ASSERT (String
!= NULL
);
158 for (Length
= 0; *String
!= L
'\0'; String
++, Length
++) {
160 // If PcdMaximumUnicodeStringLength is not zero,
161 // length should not more than PcdMaximumUnicodeStringLength
163 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
164 ASSERT (Length
< PcdGet32 (PcdMaximumUnicodeStringLength
));
171 Returns the size of a Null-terminated Unicode string in bytes, including the
174 This function returns the size, in bytes, of the Null-terminated Unicode
175 string specified by String.
177 If String is NULL, then ASSERT().
178 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
179 PcdMaximumUnicodeStringLength Unicode characters not including the
180 Null-terminator, then ASSERT().
182 @param String Pointer to a Null-terminated Unicode string.
184 @return The size of String.
190 IN CONST CHAR16
*String
193 return (StrLen (String
) + 1) * sizeof (*String
);
197 Compares two Null-terminated Unicode strings, and returns the difference
198 between the first mismatched Unicode characters.
200 This function compares the Null-terminated Unicode string FirstString to the
201 Null-terminated Unicode string SecondString. If FirstString is identical to
202 SecondString, then 0 is returned. Otherwise, the value returned is the first
203 mismatched Unicode character in SecondString subtracted from the first
204 mismatched Unicode character in FirstString.
206 If FirstString is NULL, then ASSERT().
207 If SecondString is NULL, then ASSERT().
208 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more
209 than PcdMaximumUnicodeStringLength Unicode characters not including the
210 Null-terminator, then ASSERT().
211 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more
212 than PcdMaximumUnicodeStringLength Unicode characters not including the
213 Null-terminator, then ASSERT().
215 @param FirstString Pointer to a Null-terminated Unicode string.
216 @param SecondString Pointer to a Null-terminated Unicode string.
218 @retval 0 FirstString is identical to SecondString.
219 @retval !=0 FirstString is not identical to SecondString.
225 IN CONST CHAR16
*FirstString
,
226 IN CONST CHAR16
*SecondString
230 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength
232 ASSERT (StrSize (FirstString
) != 0);
233 ASSERT (StrSize (SecondString
) != 0);
235 while ((*FirstString
!= L
'\0') && (*FirstString
== *SecondString
)) {
239 return *FirstString
- *SecondString
;
243 Compares two Null-terminated Unicode strings with maximum lengths, and
244 returns the difference between the first mismatched Unicode characters.
246 This function compares the Null-terminated Unicode string FirstString to the
247 Null-terminated Unicode string SecondString. At most, Length Unicode
248 characters will be compared. If Length is 0, then 0 is returned. If
249 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
250 value returned is the first mismatched Unicode character in SecondString
251 subtracted from the first mismatched Unicode character in FirstString.
253 If FirstString is NULL, then ASSERT().
254 If SecondString is NULL, then ASSERT().
255 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more
256 than PcdMaximumUnicodeStringLength Unicode characters not including the
257 Null-terminator, then ASSERT().
258 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more
259 than PcdMaximumUnicodeStringLength Unicode characters not including the
260 Null-terminator, then ASSERT().
262 @param FirstString Pointer to a Null-terminated Unicode string.
263 @param SecondString Pointer to a Null-terminated Unicode string.
264 @param Length Maximum number of Unicode characters to compare.
266 @retval 0 FirstString is identical to SecondString.
267 @retval !=0 FirstString is not identical to SecondString.
273 IN CONST CHAR16
*FirstString
,
274 IN CONST CHAR16
*SecondString
,
283 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
284 // Length tests are performed inside StrLen().
286 ASSERT (StrSize (FirstString
) != 0);
287 ASSERT (StrSize (SecondString
) != 0);
289 while ((*FirstString
!= L
'\0') &&
290 (*FirstString
== *SecondString
) &&
297 return *FirstString
- *SecondString
;
301 Concatenates one Null-terminated Unicode string to another Null-terminated
302 Unicode string, and returns the concatenated Unicode string.
304 This function concatenates two Null-terminated Unicode strings. The contents
305 of Null-terminated Unicode string Source are concatenated to the end of
306 Null-terminated Unicode string Destination. The Null-terminated concatenated
307 Unicode String is returned. If Source and Destination overlap, then the
308 results are undefined.
310 If Destination is NULL, then ASSERT().
311 If Source is NULL, then ASSERT().
312 If Source and Destination overlap, then ASSERT().
313 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
314 than PcdMaximumUnicodeStringLength Unicode characters not including the
315 Null-terminator, then ASSERT().
316 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
317 PcdMaximumUnicodeStringLength Unicode characters not including the
318 Null-terminator, then ASSERT().
319 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
320 and Source results in a Unicode string with more than
321 PcdMaximumUnicodeStringLength Unicode characters not including the
322 Null-terminator, then ASSERT().
324 @param Destination Pointer to a Null-terminated Unicode string.
325 @param Source Pointer to a Null-terminated Unicode string.
333 IN OUT CHAR16
*Destination
,
334 IN CONST CHAR16
*Source
337 StrCpy (Destination
+ StrLen (Destination
), Source
);
340 // Size of the resulting string should never be zero.
341 // PcdMaximumUnicodeStringLength is tested inside StrLen().
343 ASSERT (StrSize (Destination
) != 0);
348 Concatenates one Null-terminated Unicode string with a maximum length to the
349 end of another Null-terminated Unicode string, and returns the concatenated
352 This function concatenates two Null-terminated Unicode strings. The contents
353 of Null-terminated Unicode string Source are concatenated to the end of
354 Null-terminated Unicode string Destination, and Destination is returned. At
355 most, Length Unicode characters are concatenated from Source to the end of
356 Destination, and Destination is always Null-terminated. If Length is 0, then
357 Destination is returned unmodified. If Source and Destination overlap, then
358 the results are undefined.
360 If Destination is NULL, then ASSERT().
361 If Source is NULL, then ASSERT().
362 If Source and Destination overlap, then ASSERT().
363 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
364 than PcdMaximumUnicodeStringLength Unicode characters not including the
365 Null-terminator, then ASSERT().
366 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
367 PcdMaximumUnicodeStringLength Unicode characters not including the
368 Null-terminator, then ASSERT().
369 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
370 and Source results in a Unicode string with more than
371 PcdMaximumUnicodeStringLength Unicode characters not including the
372 Null-terminator, then ASSERT().
374 @param Destination Pointer to a Null-terminated Unicode string.
375 @param Source Pointer to a Null-terminated Unicode string.
376 @param Length Maximum number of Unicode characters to concatenate from
385 IN OUT CHAR16
*Destination
,
386 IN CONST CHAR16
*Source
,
390 StrnCpy (Destination
+ StrLen (Destination
), Source
, Length
);
393 // Size of the resulting string should never be zero.
394 // PcdMaximumUnicodeStringLength is tested inside StrLen().
396 ASSERT (StrSize (Destination
) != 0);
401 Returns the first occurance of a Null-terminated Unicode sub-string
402 in a Null-terminated Unicode string.
404 This function scans the contents of the Null-terminated Unicode string
405 specified by String and returns the first occurrence of SearchString.
406 If SearchString is not found in String, then NULL is returned. If
407 the length of SearchString is zero, then String is
410 If String is NULL, then ASSERT().
411 If String is not aligned on a 16-bit boundary, then ASSERT().
412 If SearchString is NULL, then ASSERT().
413 If SearchString is not aligned on a 16-bit boundary, then ASSERT().
415 If PcdMaximumUnicodeStringLength is not zero, and SearchString
416 or String contains more than PcdMaximumUnicodeStringLength Unicode
417 characters not including the Null-terminator, then ASSERT().
419 @param String Pointer to a Null-terminated Unicode string.
420 @param SearchString Pointer to a Null-terminated Unicode string to search for.
422 @retval NULL If the SearchString does not appear in String.
423 @retval !NULL If there is a match.
429 IN CONST CHAR16
*String
,
430 IN CONST CHAR16
*SearchString
433 CONST CHAR16
*FirstMatch
;
434 CONST CHAR16
*SearchStringTmp
;
436 ASSERT (String
!= NULL
);
437 ASSERT (((UINTN
) String
& 0x01) == 0);
438 ASSERT (SearchString
!= NULL
);
439 ASSERT (((UINTN
) SearchString
& 0x01) == 0);
442 // If PcdMaximumUnicodeStringLength is not zero,
443 // length of String should not more than PcdMaximumUnicodeStringLength
445 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
446 ASSERT (StrLen (String
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
450 // If PcdMaximumUnicodeStringLength is not zero,
451 // length of SearchString should not more than PcdMaximumUnicodeStringLength
453 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
454 ASSERT (StrLen (SearchString
) < PcdGet32 (PcdMaximumAsciiStringLength
));
457 while (*String
!= '\0') {
458 SearchStringTmp
= SearchString
;
461 while ((*String
== *SearchStringTmp
)
462 && (*SearchStringTmp
!= '\0')
463 && (*String
!= '\0')) {
468 if (*SearchStringTmp
== '\0') {
469 return (CHAR16
*) FirstMatch
;
472 if (SearchStringTmp
== SearchString
) {
474 // If no character from SearchString match,
475 // move the pointer to the String under search
486 Check if a Unicode character is a decimal character.
488 This internal function checks if a Unicode character is a
489 decimal character. The valid decimal character is from
493 @param Char The character to check against.
495 @retval TRUE If the Char is a decmial character.
496 @retval FALSE Otherwise.
501 InternalIsDecimalDigitCharacter (
505 return (BOOLEAN
) (Char
>= L
'0' && Char
<= L
'9');
509 Convert a Unicode character to upper case only if
510 it maps to a valid small-case ASCII character.
512 This internal function only deal with Unicode character
513 which maps to a valid small-case ASII character, i.e.
514 L'a' to L'z'. For other Unicode character, the input character
515 is returned directly.
518 @param Char The character to convert.
520 @retval LowerCharacter If the Char is with range L'a' to L'z'.
521 @retval Unchanged Otherwise.
526 InternalCharToUpper (
530 if (Char
>= L
'a' && Char
<= L
'z') {
531 return Char
- (L
'a' - L
'A');
538 Convert a Unicode character to numerical value.
540 This internal function only deal with Unicode character
541 which maps to a valid hexadecimal ASII character, i.e.
542 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
543 Unicode character, the value returned does not make sense.
545 @param Char The character to convert.
547 @retval UINTN The numerical value converted.
552 InternalHexCharToUintn (
556 if (InternalIsDecimalDigitCharacter (Char
)) {
560 return (UINTN
) (10 + InternalCharToUpper (Char
) - L
'A');
564 Check if a Unicode character is a hexadecimal character.
566 This internal function checks if a Unicode character is a
567 decimal character. The valid hexadecimal character is
568 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
571 @param Char The character to check against.
573 @retval TRUE If the Char is a hexadecmial character.
574 @retval FALSE Otherwise.
579 InternalIsHexaDecimalDigitCharacter (
584 return (BOOLEAN
) (InternalIsDecimalDigitCharacter (Char
) ||
585 (Char
>= L
'A' && Char
<= L
'F') ||
586 (Char
>= L
'a' && Char
<= L
'f'));
590 Convert a Null-terminated Unicode decimal string to a value of
593 This function returns a value of type UINTN by interpreting the contents
594 of the Unicode string specified by String as a decimal number. The format
595 of the input Unicode string String is:
597 [spaces] [decimal digits].
599 The valid decimal digit character is in the range [0-9]. The
600 function will ignore the pad space, which includes spaces or
601 tab characters, before [decimal digits]. The running zero in the
602 beginning of [decimal digits] will be ignored. Then, the function
603 stops at the first character that is a not a valid decimal character
604 or a Null-terminator, whichever one comes first.
606 If String is NULL, then ASSERT().
607 If String is not aligned in a 16-bit boundary, then ASSERT().
608 If String has only pad spaces, then 0 is returned.
609 If String has no pad spaces or valid decimal digits,
611 If the number represented by String overflows according
612 to the range defined by UINTN, then ASSERT().
614 If PcdMaximumUnicodeStringLength is not zero, and String contains
615 more than PcdMaximumUnicodeStringLength Unicode characters not including
616 the Null-terminator, then ASSERT().
618 @param String Pointer to a Null-terminated Unicode string.
626 IN CONST CHAR16
*String
631 ASSERT (String
!= NULL
);
632 ASSERT (((UINTN
) String
& 0x01) == 0);
633 ASSERT (StrLen (String
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
636 // Ignore the pad spaces (space or tab)
638 while ((*String
== L
' ') || (*String
== L
'\t')) {
643 // Ignore leading Zeros after the spaces
645 while (*String
== L
'0') {
651 while (InternalIsDecimalDigitCharacter (*String
)) {
653 // If the number represented by String overflows according
654 // to the range defined by UINTN, then ASSERT().
656 ASSERT ((Result
< QUIENT_MAX_UINTN_DIVIDED_BY_10
) ||
657 ((Result
== QUIENT_MAX_UINTN_DIVIDED_BY_10
) &&
658 (*String
- L
'0') <= REMINDER_MAX_UINTN_DIVIDED_BY_10
)
661 Result
= Result
* 10 + (*String
- L
'0');
670 Convert a Null-terminated Unicode decimal string to a value of
673 This function returns a value of type UINT64 by interpreting the contents
674 of the Unicode string specified by String as a decimal number. The format
675 of the input Unicode string String is:
677 [spaces] [decimal digits].
679 The valid decimal digit character is in the range [0-9]. The
680 function will ignore the pad space, which includes spaces or
681 tab characters, before [decimal digits]. The running zero in the
682 beginning of [decimal digits] will be ignored. Then, the function
683 stops at the first character that is a not a valid decimal character
684 or a Null-terminator, whichever one comes first.
686 If String is NULL, then ASSERT().
687 If String is not aligned in a 16-bit boundary, then ASSERT().
688 If String has only pad spaces, then 0 is returned.
689 If String has no pad spaces or valid decimal digits,
691 If the number represented by String overflows according
692 to the range defined by UINT64, then ASSERT().
694 If PcdMaximumUnicodeStringLength is not zero, and String contains
695 more than PcdMaximumUnicodeStringLength Unicode characters not including
696 the Null-terminator, then ASSERT().
698 @param String Pointer to a Null-terminated Unicode string.
706 IN CONST CHAR16
*String
711 ASSERT (String
!= NULL
);
712 ASSERT (((UINTN
) String
& 0x01) == 0);
713 ASSERT (StrLen (String
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
716 // Ignore the pad spaces (space or tab)
718 while ((*String
== L
' ') || (*String
== L
'\t')) {
723 // Ignore leading Zeros after the spaces
725 while (*String
== L
'0') {
731 while (InternalIsDecimalDigitCharacter (*String
)) {
733 // If the number represented by String overflows according
734 // to the range defined by UINTN, then ASSERT().
736 ASSERT ((Result
< QUIENT_MAX_UINT64_DIVIDED_BY_10
) ||
737 ((Result
== QUIENT_MAX_UINT64_DIVIDED_BY_10
) &&
738 (*String
- L
'0') <= REMINDER_MAX_UINT64_DIVIDED_BY_10
)
741 Result
= MultU64x32 (Result
, 10) + (*String
- L
'0');
749 Convert a Null-terminated Unicode hexadecimal string to a value of type UINTN.
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:
755 [spaces][zeros][x][hexadecimal digits].
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.
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().
774 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
775 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator,
778 @param String Pointer to a Null-terminated Unicode string.
786 IN CONST CHAR16
*String
791 ASSERT (String
!= NULL
);
792 ASSERT (StrLen (String
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
795 // Ignore the pad spaces (space or tab)
797 while ((*String
== L
' ') || (*String
== L
'\t')) {
802 // Ignore leading Zeros after the spaces
804 while (*String
== L
'0') {
808 if (InternalCharToUpper (*String
) == L
'X') {
809 ASSERT (*(String
- 1) == L
'0');
810 if (*(String
- 1) != L
'0') {
821 while (InternalIsHexaDecimalDigitCharacter (*String
)) {
823 // If the Hex Number represented by String overflows according
824 // to the range defined by UINTN, then ASSERT().
826 ASSERT ((Result
< QUIENT_MAX_UINTN_DIVIDED_BY_16
) ||
827 ((Result
== QUIENT_MAX_UINTN_DIVIDED_BY_16
) &&
828 (InternalHexCharToUintn (*String
) <= REMINDER_MAX_UINTN_DIVIDED_BY_16
))
831 Result
= (Result
<< 4) + InternalHexCharToUintn (*String
);
840 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
842 This function returns a value of type UINT64 by interpreting the contents
843 of the Unicode string specified by String as a hexadecimal number.
844 The format of the input Unicode string String is
846 [spaces][zeros][x][hexadecimal digits].
848 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
849 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
850 If "x" appears in the input string, it must be prefixed with at least one 0.
851 The function will ignore the pad space, which includes spaces or tab characters,
852 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
853 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
854 first valid hexadecimal digit. Then, the function stops at the first character that is
855 a not a valid hexadecimal character or NULL, whichever one comes first.
857 If String is NULL, then ASSERT().
858 If String is not aligned in a 16-bit boundary, then ASSERT().
859 If String has only pad spaces, then zero is returned.
860 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
861 then zero is returned.
862 If the number represented by String overflows according to the range defined by
863 UINT64, then ASSERT().
865 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
866 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator,
869 @param String Pointer to a Null-terminated Unicode string.
877 IN CONST CHAR16
*String
882 ASSERT (String
!= NULL
);
883 ASSERT (StrLen (String
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
886 // Ignore the pad spaces (space or tab)
888 while ((*String
== L
' ') || (*String
== L
'\t')) {
893 // Ignore leading Zeros after the spaces
895 while (*String
== L
'0') {
899 if (InternalCharToUpper (*String
) == L
'X') {
900 ASSERT (*(String
- 1) == L
'0');
901 if (*(String
- 1) != L
'0') {
912 while (InternalIsHexaDecimalDigitCharacter (*String
)) {
914 // If the Hex Number represented by String overflows according
915 // to the range defined by UINTN, then ASSERT().
917 ASSERT ((Result
< QUIENT_MAX_UINT64_DIVIDED_BY_16
)||
918 ((Result
== QUIENT_MAX_UINT64_DIVIDED_BY_16
) &&
919 (InternalHexCharToUintn (*String
) <= REMINDER_MAX_UINT64_DIVIDED_BY_16
))
922 Result
= LShiftU64 (Result
, 4) + InternalHexCharToUintn (*String
);
930 Check if a ASCII character is a decimal character.
932 This internal function checks if a Unicode character is a
933 decimal character. The valid decimal character is from
936 @param Char The character to check against.
938 @retval TRUE If the Char is a decmial character.
939 @retval FALSE Otherwise.
944 InternalAsciiIsDecimalDigitCharacter (
948 return (BOOLEAN
) (Char
>= '0' && Char
<= '9');
952 Check if a ASCII character is a hexadecimal character.
954 This internal function checks if a ASCII character is a
955 decimal character. The valid hexadecimal character is
956 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
959 @param Char The character to check against.
961 @retval TRUE If the Char is a hexadecmial character.
962 @retval FALSE Otherwise.
967 InternalAsciiIsHexaDecimalDigitCharacter (
972 return (BOOLEAN
) (InternalAsciiIsDecimalDigitCharacter (Char
) ||
973 (Char
>= 'A' && Char
<= 'F') ||
974 (Char
>= 'a' && Char
<= 'f'));
978 Convert a Null-terminated Unicode string to a Null-terminated
979 ASCII string and returns the ASCII string.
981 This function converts the content of the Unicode string Source
982 to the ASCII string Destination by copying the lower 8 bits of
983 each Unicode character. It returns Destination. The function terminates
984 the ASCII string Destination by appending a Null-terminator character
985 at the end. The caller is responsible to make sure Destination points
986 to a buffer with size equal or greater than (StrLen (Source) + 1) in bytes.
988 If Destination is NULL, then ASSERT().
989 If Source is NULL, then ASSERT().
990 If Source is not aligned on a 16-bit boundary, then ASSERT().
991 If Source and Destination overlap, then ASSERT().
993 If any Unicode characters in Source contain non-zero value in
994 the upper 8 bits, then ASSERT().
996 If PcdMaximumUnicodeStringLength is not zero, and Source contains
997 more than PcdMaximumUnicodeStringLength Unicode characters not including
998 the Null-terminator, then ASSERT().
1000 If PcdMaximumAsciiStringLength is not zero, and Source contains more
1001 than PcdMaximumAsciiStringLength Unicode characters not including the
1002 Null-terminator, then ASSERT().
1004 @param Source Pointer to a Null-terminated Unicode string.
1005 @param Destination Pointer to a Null-terminated ASCII string.
1012 UnicodeStrToAsciiStr (
1013 IN CONST CHAR16
*Source
,
1014 OUT CHAR8
*Destination
1017 ASSERT (Destination
!= NULL
);
1018 ASSERT (Source
!= NULL
);
1021 // Source and Destination should not overlap
1023 ASSERT ((UINTN
) ((CHAR16
*) Destination
- Source
) > StrLen (Source
));
1024 ASSERT ((UINTN
) ((CHAR8
*) Source
- Destination
) > StrLen (Source
));
1027 // If PcdMaximumUnicodeStringLength is not zero,
1028 // length of Source should not more than PcdMaximumUnicodeStringLength
1030 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
1031 ASSERT (StrLen (Source
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
1034 while (*Source
!= '\0') {
1036 // If any Unicode characters in Source contain
1037 // non-zero value in the upper 8 bits, then ASSERT().
1039 ASSERT (*Source
< 0x100);
1040 *(Destination
++) = (CHAR8
) *(Source
++);
1043 *Destination
= '\0';
1050 Copies one Null-terminated ASCII string to another Null-terminated ASCII
1051 string and returns the new ASCII string.
1053 This function copies the contents of the ASCII string Source to the ASCII
1054 string Destination, and returns Destination. If Source and Destination
1055 overlap, then the results are undefined.
1057 If Destination is NULL, then ASSERT().
1058 If Source is NULL, then ASSERT().
1059 If Source and Destination overlap, then ASSERT().
1060 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1061 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1064 @param Destination Pointer to a Null-terminated ASCII string.
1065 @param Source Pointer to a Null-terminated ASCII string.
1073 OUT CHAR8
*Destination
,
1074 IN CONST CHAR8
*Source
1080 // Destination cannot be NULL
1082 ASSERT (Destination
!= NULL
);
1085 // Destination and source cannot overlap
1087 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
1088 ASSERT ((UINTN
)(Source
- Destination
) > AsciiStrLen (Source
));
1090 ReturnValue
= Destination
;
1092 *(Destination
++) = *(Source
++);
1099 Copies one Null-terminated ASCII string with a maximum length to another
1100 Null-terminated ASCII string with a maximum length and returns the new ASCII
1103 This function copies the contents of the ASCII string Source to the ASCII
1104 string Destination, and returns Destination. At most, Length ASCII characters
1105 are copied from Source to Destination. If Length is 0, then Destination is
1106 returned unmodified. If Length is greater that the number of ASCII characters
1107 in Source, then Destination is padded with Null ASCII characters. If Source
1108 and Destination overlap, then the results are undefined.
1110 If Destination is NULL, then ASSERT().
1111 If Source is NULL, then ASSERT().
1112 If Source and Destination overlap, then ASSERT().
1113 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1114 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1117 @param Destination Pointer to a Null-terminated ASCII string.
1118 @param Source Pointer to a Null-terminated ASCII string.
1119 @param Length Maximum number of ASCII characters to copy.
1127 OUT CHAR8
*Destination
,
1128 IN CONST CHAR8
*Source
,
1139 // Destination cannot be NULL
1141 ASSERT (Destination
!= NULL
);
1144 // Destination and source cannot overlap
1146 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
1147 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
1149 ReturnValue
= Destination
;
1151 while (*Source
&& Length
> 0) {
1152 *(Destination
++) = *(Source
++);
1156 ZeroMem (Destination
, Length
* sizeof (*Destination
));
1161 Returns the length of a Null-terminated ASCII string.
1163 This function returns the number of ASCII characters in the Null-terminated
1164 ASCII string specified by String.
1166 If String is NULL, then ASSERT().
1167 If PcdMaximumAsciiStringLength is not zero and String contains more than
1168 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1171 @param String Pointer to a Null-terminated ASCII string.
1173 @return The length of String.
1179 IN CONST CHAR8
*String
1184 ASSERT (String
!= NULL
);
1186 for (Length
= 0; *String
!= '\0'; String
++, Length
++) {
1188 // If PcdMaximumUnicodeStringLength is not zero,
1189 // length should not more than PcdMaximumUnicodeStringLength
1191 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1192 ASSERT (Length
< PcdGet32 (PcdMaximumAsciiStringLength
));
1199 Returns the size of a Null-terminated ASCII string in bytes, including the
1202 This function returns the size, in bytes, of the Null-terminated ASCII string
1203 specified by String.
1205 If String is NULL, then ASSERT().
1206 If PcdMaximumAsciiStringLength is not zero and String contains more than
1207 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1210 @param String Pointer to a Null-terminated ASCII string.
1212 @return The size of String.
1218 IN CONST CHAR8
*String
1221 return (AsciiStrLen (String
) + 1) * sizeof (*String
);
1225 Compares two Null-terminated ASCII strings, and returns the difference
1226 between the first mismatched ASCII characters.
1228 This function compares the Null-terminated ASCII string FirstString to the
1229 Null-terminated ASCII string SecondString. If FirstString is identical to
1230 SecondString, then 0 is returned. Otherwise, the value returned is the first
1231 mismatched ASCII character in SecondString subtracted from the first
1232 mismatched ASCII character in FirstString.
1234 If FirstString is NULL, then ASSERT().
1235 If SecondString is NULL, then ASSERT().
1236 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1237 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1239 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
1240 than PcdMaximumAsciiStringLength ASCII characters not including the
1241 Null-terminator, then ASSERT().
1243 @param FirstString Pointer to a Null-terminated ASCII string.
1244 @param SecondString Pointer to a Null-terminated ASCII string.
1246 @retval 0 FirstString is identical to SecondString.
1247 @retval !=0 FirstString is not identical to SecondString.
1253 IN CONST CHAR8
*FirstString
,
1254 IN CONST CHAR8
*SecondString
1258 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1260 ASSERT (AsciiStrSize (FirstString
));
1261 ASSERT (AsciiStrSize (SecondString
));
1263 while ((*FirstString
!= '\0') && (*FirstString
== *SecondString
)) {
1268 return *FirstString
- *SecondString
;
1272 Converts a lowercase Ascii character to upper one
1274 If Chr is lowercase Ascii character, then converts it to upper one.
1276 If Value >= 0xA0, then ASSERT().
1277 If (Value & 0x0F) >= 0x0A, then ASSERT().
1279 @param chr one Ascii character
1281 @return The uppercase value of Ascii character
1290 return (UINT8
) ((Chr
>= 'a' && Chr
<= 'z') ? Chr
- ('a' - 'A') : Chr
);
1294 Convert a ASCII character to numerical value.
1296 This internal function only deal with Unicode character
1297 which maps to a valid hexadecimal ASII character, i.e.
1298 '0' to '9', 'a' to 'f' or 'A' to 'F'. For other
1299 ASCII character, the value returned does not make sense.
1301 @param Char The character to convert.
1303 @retval UINTN The numerical value converted.
1308 InternalAsciiHexCharToUintn (
1312 if (InternalIsDecimalDigitCharacter (Char
)) {
1316 return (UINTN
) (10 + AsciiToUpper (Char
) - 'A');
1321 Performs a case insensitive comparison of two Null-terminated ASCII strings,
1322 and returns the difference between the first mismatched ASCII characters.
1324 This function performs a case insensitive comparison of the Null-terminated
1325 ASCII string FirstString to the Null-terminated ASCII string SecondString. If
1326 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
1327 value returned is the first mismatched lower case ASCII character in
1328 SecondString subtracted from the first mismatched lower case ASCII character
1331 If FirstString is NULL, then ASSERT().
1332 If SecondString is NULL, then ASSERT().
1333 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1334 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1336 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
1337 than PcdMaximumAsciiStringLength ASCII characters not including the
1338 Null-terminator, then ASSERT().
1340 @param FirstString Pointer to a Null-terminated ASCII string.
1341 @param SecondString Pointer to a Null-terminated ASCII string.
1343 @retval 0 FirstString is identical to SecondString using case insensitive
1345 @retval !=0 FirstString is not identical to SecondString using case
1346 insensitive comparisons.
1352 IN CONST CHAR8
*FirstString
,
1353 IN CONST CHAR8
*SecondString
1356 CHAR8 UpperFirstString
;
1357 CHAR8 UpperSecondString
;
1360 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1362 ASSERT (AsciiStrSize (FirstString
));
1363 ASSERT (AsciiStrSize (SecondString
));
1365 UpperFirstString
= AsciiToUpper (*FirstString
);
1366 UpperSecondString
= AsciiToUpper (*SecondString
);
1367 while ((*FirstString
!= '\0') && (UpperFirstString
== UpperSecondString
)) {
1370 UpperFirstString
= AsciiToUpper (*FirstString
);
1371 UpperSecondString
= AsciiToUpper (*SecondString
);
1374 return UpperFirstString
- UpperSecondString
;
1378 Compares two Null-terminated ASCII strings with maximum lengths, and returns
1379 the difference between the first mismatched ASCII characters.
1381 This function compares the Null-terminated ASCII string FirstString to the
1382 Null-terminated ASCII string SecondString. At most, Length ASCII characters
1383 will be compared. If Length is 0, then 0 is returned. If FirstString is
1384 identical to SecondString, then 0 is returned. Otherwise, the value returned
1385 is the first mismatched ASCII character in SecondString subtracted from the
1386 first mismatched ASCII character in FirstString.
1388 If FirstString is NULL, then ASSERT().
1389 If SecondString is NULL, then ASSERT().
1390 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1391 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1393 If PcdMaximumAsciiStringLength is not zero and SecondString contains more than
1394 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1397 @param FirstString Pointer to a Null-terminated ASCII string.
1398 @param SecondString Pointer to a Null-terminated ASCII string.
1400 @retval 0 FirstString is identical to SecondString.
1401 @retval !=0 FirstString is not identical to SecondString.
1407 IN CONST CHAR8
*FirstString
,
1408 IN CONST CHAR8
*SecondString
,
1417 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1419 ASSERT (AsciiStrSize (FirstString
));
1420 ASSERT (AsciiStrSize (SecondString
));
1422 while ((*FirstString
!= '\0') &&
1423 (*FirstString
== *SecondString
) &&
1429 return *FirstString
- *SecondString
;
1433 Concatenates one Null-terminated ASCII string to another Null-terminated
1434 ASCII string, and returns the concatenated ASCII string.
1436 This function concatenates two Null-terminated ASCII strings. The contents of
1437 Null-terminated ASCII string Source are concatenated to the end of Null-
1438 terminated ASCII string Destination. The Null-terminated concatenated ASCII
1441 If Destination is NULL, then ASSERT().
1442 If Source is NULL, then ASSERT().
1443 If PcdMaximumAsciiStringLength is not zero and Destination contains more than
1444 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1446 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1447 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1449 If PcdMaximumAsciiStringLength is not zero and concatenating Destination and
1450 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1451 ASCII characters, then ASSERT().
1453 @param Destination Pointer to a Null-terminated ASCII string.
1454 @param Source Pointer to a Null-terminated ASCII string.
1462 IN OUT CHAR8
*Destination
,
1463 IN CONST CHAR8
*Source
1466 AsciiStrCpy (Destination
+ AsciiStrLen (Destination
), Source
);
1469 // Size of the resulting string should never be zero.
1470 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1472 ASSERT (AsciiStrSize (Destination
) != 0);
1477 Concatenates one Null-terminated ASCII string with a maximum length to the
1478 end of another Null-terminated ASCII string, and returns the concatenated
1481 This function concatenates two Null-terminated ASCII strings. The contents
1482 of Null-terminated ASCII string Source are concatenated to the end of Null-
1483 terminated ASCII string Destination, and Destination is returned. At most,
1484 Length ASCII characters are concatenated from Source to the end of
1485 Destination, and Destination is always Null-terminated. If Length is 0, then
1486 Destination is returned unmodified. If Source and Destination overlap, then
1487 the results are undefined.
1489 If Destination is NULL, then ASSERT().
1490 If Source is NULL, then ASSERT().
1491 If Source and Destination overlap, 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 not including the Null-terminator, then ASSERT().
1502 @param Destination Pointer to a Null-terminated ASCII string.
1503 @param Source Pointer to a Null-terminated ASCII string.
1504 @param Length Maximum number of ASCII characters to concatenate from
1513 IN OUT CHAR8
*Destination
,
1514 IN CONST CHAR8
*Source
,
1518 AsciiStrnCpy (Destination
+ AsciiStrLen (Destination
), Source
, Length
);
1521 // Size of the resulting string should never be zero.
1522 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1524 ASSERT (AsciiStrSize (Destination
) != 0);
1529 Returns the first occurance of a Null-terminated ASCII sub-string
1530 in a Null-terminated ASCII string.
1532 This function scans the contents of the ASCII string specified by String
1533 and returns the first occurrence of SearchString. If SearchString is not
1534 found in String, then NULL is returned. If the length of SearchString is zero,
1535 then String is returned.
1537 If String is NULL, then ASSERT().
1538 If SearchString is NULL, then ASSERT().
1540 If PcdMaximumAsciiStringLength is not zero, and SearchString or
1541 String contains more than PcdMaximumAsciiStringLength Unicode characters
1542 not including the Null-terminator, then ASSERT().
1544 @param String Pointer to a Null-terminated ASCII string.
1545 @param SearchString Pointer to a Null-terminated ASCII string to search for.
1547 @retval NULL If the SearchString does not appear in String.
1548 @retval !NULL If there is a match.
1554 IN CONST CHAR8
*String
,
1555 IN CONST CHAR8
*SearchString
1558 CONST CHAR8
*FirstMatch
;
1559 CONST CHAR8
*SearchStringTmp
;
1561 ASSERT (String
!= NULL
);
1562 ASSERT (SearchString
!= NULL
);
1565 // If PcdMaximumUnicodeStringLength is not zero,
1566 // length of String should not more than PcdMaximumUnicodeStringLength
1568 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1569 ASSERT (AsciiStrLen (String
) < PcdGet32 (PcdMaximumAsciiStringLength
));
1573 // If PcdMaximumUnicodeStringLength is not zero,
1574 // length of SearchString should not more than PcdMaximumUnicodeStringLength
1576 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1577 ASSERT (AsciiStrLen (SearchString
) < PcdGet32 (PcdMaximumAsciiStringLength
));
1580 while (*String
!= '\0') {
1581 SearchStringTmp
= SearchString
;
1582 FirstMatch
= String
;
1584 while ((*String
== *SearchStringTmp
)
1585 && (*SearchStringTmp
!= '\0')
1586 && (*String
!= '\0')) {
1591 if (*SearchStringTmp
== '\0') {
1592 return (CHAR8
*) FirstMatch
;
1595 if (SearchStringTmp
== SearchString
) {
1597 // If no character from SearchString match,
1598 // move the pointer to the String under search
1599 // by one character.
1610 Convert a Null-terminated ASCII decimal string to a value of type
1613 This function returns a value of type UINTN by interpreting the contents
1614 of the ASCII string String as a decimal number. The format of the input
1615 ASCII string String is:
1617 [spaces] [decimal digits].
1619 The valid decimal digit character is in the range [0-9]. The function will
1620 ignore the pad space, which includes spaces or tab characters, before the digits.
1621 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1622 function stops at the first character that is a not a valid decimal character or
1623 Null-terminator, whichever on comes first.
1625 If String has only pad spaces, then 0 is returned.
1626 If String has no pad spaces or valid decimal digits, then 0 is returned.
1627 If the number represented by String overflows according to the range defined by
1628 UINTN, then ASSERT().
1629 If String is NULL, then ASSERT().
1630 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1631 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1634 @param String Pointer to a Null-terminated ASCII string.
1641 AsciiStrDecimalToUintn (
1642 IN CONST CHAR8
*String
1647 ASSERT (String
!= NULL
);
1648 ASSERT (AsciiStrLen (String
) < PcdGet32 (PcdMaximumAsciiStringLength
));
1651 // Ignore the pad spaces (space or tab)
1653 while ((*String
== ' ') || (*String
== '\t')) {
1658 // Ignore leading Zeros after the spaces
1660 while (*String
== '0') {
1666 while (InternalAsciiIsDecimalDigitCharacter (*String
)) {
1668 // If the number represented by String overflows according
1669 // to the range defined by UINTN, then ASSERT().
1671 ASSERT ((Result
< QUIENT_MAX_UINTN_DIVIDED_BY_10
) ||
1672 ((Result
== QUIENT_MAX_UINTN_DIVIDED_BY_10
) &&
1673 (*String
- '0') <= REMINDER_MAX_UINTN_DIVIDED_BY_10
)
1676 Result
= Result
* 10 + (*String
- '0');
1685 Convert a Null-terminated ASCII decimal string to a value of type
1688 This function returns a value of type UINT64 by interpreting the contents
1689 of the ASCII string String as a decimal number. The format of the input
1690 ASCII string String is:
1692 [spaces] [decimal digits].
1694 The valid decimal digit character is in the range [0-9]. The function will
1695 ignore the pad space, which includes spaces or tab characters, before the digits.
1696 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1697 function stops at the first character that is a not a valid decimal character or
1698 Null-terminator, whichever on comes first.
1700 If String has only pad spaces, then 0 is returned.
1701 If String has no pad spaces or valid decimal digits, then 0 is returned.
1702 If the number represented by String overflows according to the range defined by
1703 UINT64, then ASSERT().
1704 If String is NULL, then ASSERT().
1705 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1706 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1709 @param String Pointer to a Null-terminated ASCII string.
1716 AsciiStrDecimalToUint64 (
1717 IN CONST CHAR8
*String
1722 ASSERT (String
!= NULL
);
1723 ASSERT (AsciiStrLen (String
) < PcdGet32 (PcdMaximumAsciiStringLength
));
1726 // Ignore the pad spaces (space or tab)
1728 while ((*String
== ' ') || (*String
== '\t')) {
1733 // Ignore leading Zeros after the spaces
1735 while (*String
== '0') {
1741 while (InternalAsciiIsDecimalDigitCharacter (*String
)) {
1743 // If the number represented by String overflows according
1744 // to the range defined by UINTN, then ASSERT().
1746 ASSERT ((Result
< QUIENT_MAX_UINT64_DIVIDED_BY_10
) ||
1747 ((Result
== QUIENT_MAX_UINT64_DIVIDED_BY_10
) &&
1748 (*String
- '0') <= REMINDER_MAX_UINT64_DIVIDED_BY_10
)
1751 Result
= MultU64x32 (Result
, 10) + (*String
- '0');
1759 Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.
1761 This function returns a value of type UINTN by interpreting the contents of
1762 the ASCII string String as a hexadecimal number. The format of the input ASCII
1765 [spaces][zeros][x][hexadecimal digits].
1767 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1768 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1769 appears in the input string, it must be prefixed with at least one 0. The function
1770 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1771 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1772 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1773 digit. Then, the function stops at the first character that is a not a valid
1774 hexadecimal character or Null-terminator, whichever on comes first.
1776 If String has only pad spaces, then 0 is returned.
1777 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1780 If the number represented by String overflows according to the range defined by UINTN,
1782 If String is NULL, then ASSERT().
1783 If PcdMaximumAsciiStringLength is not zero,
1784 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1785 the Null-terminator, then ASSERT().
1787 @param String Pointer to a Null-terminated ASCII string.
1794 AsciiStrHexToUintn (
1795 IN CONST CHAR8
*String
1800 ASSERT (String
!= NULL
);
1801 ASSERT (AsciiStrLen (String
) < PcdGet32 (PcdMaximumAsciiStringLength
));
1804 // Ignore the pad spaces (space or tab)
1806 while ((*String
== ' ') || (*String
== '\t')) {
1811 // Ignore leading Zeros after the spaces
1813 while (*String
== '0') {
1817 if (AsciiToUpper (*String
) == 'X') {
1818 ASSERT (*(String
- 1) == '0');
1819 if (*(String
- 1) != '0') {
1830 while (InternalAsciiIsHexaDecimalDigitCharacter (*String
)) {
1832 // If the Hex Number represented by String overflows according
1833 // to the range defined by UINTN, then ASSERT().
1835 ASSERT ((Result
< QUIENT_MAX_UINTN_DIVIDED_BY_16
) ||
1836 ((Result
== QUIENT_MAX_UINTN_DIVIDED_BY_16
) &&
1837 (InternalAsciiHexCharToUintn (*String
) <= REMINDER_MAX_UINTN_DIVIDED_BY_16
))
1840 Result
= (Result
<< 4) + InternalAsciiHexCharToUintn (*String
);
1849 Convert a Null-terminated ASCII hexadecimal string to a value of type UINT64.
1851 This function returns a value of type UINT64 by interpreting the contents of
1852 the ASCII string String as a hexadecimal number. The format of the input ASCII
1855 [spaces][zeros][x][hexadecimal digits].
1857 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1858 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1859 appears in the input string, it must be prefixed with at least one 0. The function
1860 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1861 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1862 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1863 digit. Then, the function stops at the first character that is a not a valid
1864 hexadecimal character or Null-terminator, whichever on comes first.
1866 If String has only pad spaces, then 0 is returned.
1867 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1870 If the number represented by String overflows according to the range defined by UINT64,
1872 If String is NULL, then ASSERT().
1873 If PcdMaximumAsciiStringLength is not zero,
1874 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1875 the Null-terminator, then ASSERT().
1877 @param String Pointer to a Null-terminated ASCII string.
1884 AsciiStrHexToUint64 (
1885 IN CONST CHAR8
*String
1890 ASSERT (String
!= NULL
);
1891 ASSERT (AsciiStrLen (String
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
1894 // Ignore the pad spaces (space or tab) and leading Zeros
1897 // Ignore the pad spaces (space or tab)
1899 while ((*String
== ' ') || (*String
== '\t')) {
1904 // Ignore leading Zeros after the spaces
1906 while (*String
== '0') {
1910 if (AsciiToUpper (*String
) == 'X') {
1911 ASSERT (*(String
- 1) == '0');
1912 if (*(String
- 1) != '0') {
1923 while (InternalAsciiIsHexaDecimalDigitCharacter (*String
)) {
1925 // If the Hex Number represented by String overflows according
1926 // to the range defined by UINTN, then ASSERT().
1928 ASSERT ((Result
< QUIENT_MAX_UINT64_DIVIDED_BY_16
) ||
1929 ((Result
== QUIENT_MAX_UINT64_DIVIDED_BY_16
) &&
1930 (InternalAsciiHexCharToUintn (*String
) <= REMINDER_MAX_UINT64_DIVIDED_BY_16
))
1933 Result
= LShiftU64 (Result
, 4) + InternalAsciiHexCharToUintn (*String
);
1942 Convert one Null-terminated ASCII string to a Null-terminated
1943 Unicode string and returns the Unicode string.
1945 This function converts the contents of the ASCII string Source to the Unicode
1946 string Destination, and returns Destination. The function terminates the
1947 Unicode string Destination by appending a Null-terminator character at the end.
1948 The caller is responsible to make sure Destination points to a buffer with size
1949 equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.
1951 If Destination is NULL, then ASSERT().
1952 If Destination is not aligned on a 16-bit boundary, then ASSERT().
1953 If Source is NULL, then ASSERT().
1954 If Source and Destination overlap, then ASSERT().
1955 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1956 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1958 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
1959 PcdMaximumUnicodeStringLength ASCII characters not including the
1960 Null-terminator, then ASSERT().
1962 @param Source Pointer to a Null-terminated ASCII string.
1963 @param Destination Pointer to a Null-terminated Unicode string.
1970 AsciiStrToUnicodeStr (
1971 IN CONST CHAR8
*Source
,
1972 OUT CHAR16
*Destination
1975 ASSERT (Destination
!= NULL
);
1976 ASSERT (Source
!= NULL
);
1979 // Source and Destination should not overlap
1981 ASSERT ((UINTN
) ((CHAR8
*) Destination
- Source
) > AsciiStrLen (Source
));
1982 ASSERT ((UINTN
) (Source
- (CHAR8
*) Destination
) > (AsciiStrLen (Source
) * sizeof (CHAR16
)));
1985 // If PcdMaximumAsciiStringLength is not zero,
1986 // length of Source should not more than PcdMaximumUnicodeStringLength
1988 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1989 ASSERT (AsciiStrLen (Source
) < PcdGet32 (PcdMaximumAsciiStringLength
));
1992 while (*Source
!= '\0') {
1993 *(Destination
++) = (CHAR16
) *(Source
++);
1996 // End the Destination with a NULL.
1998 *Destination
= '\0';
2004 Converts an 8-bit value to an 8-bit BCD value.
2006 Converts the 8-bit value specified by Value to BCD. The BCD value is
2009 If Value >= 100, then ASSERT().
2011 @param Value The 8-bit value to convert to BCD. Range 0..99.
2013 @return The BCD value
2022 ASSERT (Value
< 100);
2023 return (UINT8
) (((Value
/ 10) << 4) | (Value
% 10));
2027 Converts an 8-bit BCD value to an 8-bit value.
2029 Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit
2032 If Value >= 0xA0, then ASSERT().
2033 If (Value & 0x0F) >= 0x0A, then ASSERT().
2035 @param Value The 8-bit BCD value to convert to an 8-bit value.
2037 @return The 8-bit value is returned.
2046 ASSERT (Value
< 0xa0);
2047 ASSERT ((Value
& 0xf) < 0xa);
2048 return (UINT8
) ((Value
>> 4) * 10 + (Value
& 0xf));