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.
16 // Include common header file for this module.
20 #include "BaseLibInternals.h"
23 Copies one Null-terminated Unicode string to another Null-terminated Unicode
24 string and returns the new Unicode string.
26 This function copies the contents of the Unicode string Source to the Unicode
27 string Destination, and returns Destination. If Source and Destination
28 overlap, then the results are undefined.
30 If Destination is NULL, then ASSERT().
31 If Destination is not aligned on a 16-bit boundary, then ASSERT().
32 If Source is NULL, then ASSERT().
33 If Source is not aligned on a 16-bit boundary, then ASSERT().
34 If Source and Destination overlap, then ASSERT().
35 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
36 PcdMaximumUnicodeStringLength Unicode characters not including the
37 Null-terminator, then ASSERT().
39 @param Destination Pointer to a Null-terminated Unicode string.
40 @param Source Pointer to a Null-terminated Unicode string.
48 OUT CHAR16
*Destination
,
49 IN CONST CHAR16
*Source
55 // Destination cannot be NULL
57 ASSERT (Destination
!= NULL
);
58 ASSERT (((UINTN
) Destination
& 0x01) == 0);
61 // Destination and source cannot overlap
63 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
64 ASSERT ((UINTN
)(Source
- Destination
) > StrLen (Source
));
66 ReturnValue
= Destination
;
68 *(Destination
++) = *(Source
++);
75 Copies one Null-terminated Unicode string with a maximum length to another
76 Null-terminated Unicode string with a maximum length and returns the new
79 This function copies the contents of the Unicode string Source to the Unicode
80 string Destination, and returns Destination. At most, Length Unicode
81 characters are copied from Source to Destination. If Length is 0, then
82 Destination is returned unmodified. If Length is greater that the number of
83 Unicode characters in Source, then Destination is padded with Null Unicode
84 characters. If Source and Destination overlap, then the results are
87 If Length > 0 and Destination is NULL, then ASSERT().
88 If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().
89 If Length > 0 and Source is NULL, then ASSERT().
90 If Length > 0 and Source is not aligned on a 16-bit bounadry, then ASSERT().
91 If Source and Destination overlap, then ASSERT().
92 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
93 PcdMaximumUnicodeStringLength Unicode characters not including the
94 Null-terminator, then ASSERT().
96 @param Destination Pointer to a Null-terminated Unicode string.
97 @param Source Pointer to a Null-terminated Unicode string.
98 @param Length Maximum number of Unicode characters to copy.
106 OUT CHAR16
*Destination
,
107 IN CONST CHAR16
*Source
,
118 // Destination cannot be NULL if Length is not zero
120 ASSERT (Destination
!= NULL
);
121 ASSERT (((UINTN
) Destination
& 0x01) == 0);
124 // Destination and source cannot overlap
125 // Q: Does Source have to be NULL-terminated?
127 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
128 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
130 ReturnValue
= Destination
;
132 while ((*Source
!= L
'\0') && (Length
> 0)) {
133 *(Destination
++) = *(Source
++);
137 ZeroMem (Destination
, Length
* sizeof (*Destination
));
142 Returns the length of a Null-terminated Unicode string.
144 This function returns the number of Unicode characters in the Null-terminated
145 Unicode string specified by String.
147 If String is NULL, then ASSERT().
148 If String is not aligned on a 16-bit boundary, then ASSERT().
149 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
150 PcdMaximumUnicodeStringLength Unicode characters not including the
151 Null-terminator, then ASSERT().
153 @param String Pointer to a Null-terminated Unicode string.
155 @return The length of String.
161 IN CONST CHAR16
*String
166 ASSERT (String
!= NULL
);
167 ASSERT (((UINTN
) String
& 0x01) == 0);
169 for (Length
= 0; *String
!= L
'\0'; String
++, Length
++) {
171 // If PcdMaximumUnicodeStringLength is not zero,
172 // length should not more than PcdMaximumUnicodeStringLength
174 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
175 ASSERT (Length
< PcdGet32 (PcdMaximumUnicodeStringLength
));
182 Returns the size of a Null-terminated Unicode string in bytes, including the
185 This function returns the size, in bytes, of the Null-terminated Unicode
186 string specified by String.
188 If String is NULL, then ASSERT().
189 If String is not aligned on a 16-bit boundary, then ASSERT().
190 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
191 PcdMaximumUnicodeStringLength Unicode characters not including the
192 Null-terminator, then ASSERT().
194 @param String Pointer to a Null-terminated Unicode string.
196 @return The size of String.
202 IN CONST CHAR16
*String
205 return (StrLen (String
) + 1) * sizeof (*String
);
209 Compares two Null-terminated Unicode strings, and returns the difference
210 between the first mismatched Unicode characters.
212 This function compares the Null-terminated Unicode string FirstString to the
213 Null-terminated Unicode string SecondString. If FirstString is identical to
214 SecondString, then 0 is returned. Otherwise, the value returned is the first
215 mismatched Unicode character in SecondString subtracted from the first
216 mismatched Unicode character in FirstString.
218 If FirstString is NULL, then ASSERT().
219 If FirstString is not aligned on a 16-bit boundary, then ASSERT().
220 If SecondString is NULL, then ASSERT().
221 If SecondString is not aligned on a 16-bit boundary, then ASSERT().
222 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more
223 than PcdMaximumUnicodeStringLength Unicode characters not including the
224 Null-terminator, then ASSERT().
225 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more
226 than PcdMaximumUnicodeStringLength Unicode characters not including the
227 Null-terminator, then ASSERT().
229 @param FirstString Pointer to a Null-terminated Unicode string.
230 @param SecondString Pointer to a Null-terminated Unicode string.
232 @retval 0 FirstString is identical to SecondString.
233 @retval !=0 FirstString is not identical to SecondString.
239 IN CONST CHAR16
*FirstString
,
240 IN CONST CHAR16
*SecondString
244 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength
246 ASSERT (StrSize (FirstString
) != 0);
247 ASSERT (StrSize (SecondString
) != 0);
249 while ((*FirstString
!= L
'\0') && (*FirstString
== *SecondString
)) {
253 return *FirstString
- *SecondString
;
257 Compares two Null-terminated Unicode strings with maximum lengths, and
258 returns the difference between the first mismatched Unicode characters.
260 This function compares the Null-terminated Unicode string FirstString to the
261 Null-terminated Unicode string SecondString. At most, Length Unicode
262 characters will be compared. If Length is 0, then 0 is returned. If
263 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
264 value returned is the first mismatched Unicode character in SecondString
265 subtracted from the first mismatched Unicode character in FirstString.
267 If Length > 0 and FirstString is NULL, then ASSERT().
268 If Length > 0 and FirstString is not aligned on a 16-bit bounadary, then ASSERT().
269 If Length > 0 and SecondString is NULL, then ASSERT().
270 If Length > 0 and SecondString is not aligned on a 16-bit bounadary, then ASSERT().
271 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more
272 than PcdMaximumUnicodeStringLength Unicode characters not including the
273 Null-terminator, then ASSERT().
274 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more
275 than PcdMaximumUnicodeStringLength Unicode characters not including the
276 Null-terminator, then ASSERT().
278 @param FirstString Pointer to a Null-terminated Unicode string.
279 @param SecondString Pointer to a Null-terminated Unicode string.
280 @param Length Maximum number of Unicode characters to compare.
282 @retval 0 FirstString is identical to SecondString.
283 @retval !=0 FirstString is not identical to SecondString.
289 IN CONST CHAR16
*FirstString
,
290 IN CONST CHAR16
*SecondString
,
299 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
300 // Length tests are performed inside StrLen().
302 ASSERT (StrSize (FirstString
) != 0);
303 ASSERT (StrSize (SecondString
) != 0);
305 while ((*FirstString
!= L
'\0') &&
306 (*FirstString
== *SecondString
) &&
313 return *FirstString
- *SecondString
;
317 Concatenates one Null-terminated Unicode string to another Null-terminated
318 Unicode string, and returns the concatenated Unicode string.
320 This function concatenates two Null-terminated Unicode strings. The contents
321 of Null-terminated Unicode string Source are concatenated to the end of
322 Null-terminated Unicode string Destination. The Null-terminated concatenated
323 Unicode String is returned. If Source and Destination overlap, then the
324 results are undefined.
326 If Destination is NULL, then ASSERT().
327 If Source is NULL, then ASSERT().
328 If Source and Destination overlap, then ASSERT().
329 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
330 than PcdMaximumUnicodeStringLength Unicode characters not including the
331 Null-terminator, then ASSERT().
332 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
333 PcdMaximumUnicodeStringLength Unicode characters not including the
334 Null-terminator, then ASSERT().
335 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
336 and Source results in a Unicode string with more than
337 PcdMaximumUnicodeStringLength Unicode characters not including the
338 Null-terminator, then ASSERT().
340 @param Destination Pointer to a Null-terminated Unicode string.
341 @param Source Pointer to a Null-terminated Unicode string.
349 IN OUT CHAR16
*Destination
,
350 IN CONST CHAR16
*Source
353 StrCpy (Destination
+ StrLen (Destination
), Source
);
356 // Size of the resulting string should never be zero.
357 // PcdMaximumUnicodeStringLength is tested inside StrLen().
359 ASSERT (StrSize (Destination
) != 0);
364 Concatenates one Null-terminated Unicode string with a maximum length to the
365 end of another Null-terminated Unicode string, and returns the concatenated
368 This function concatenates two Null-terminated Unicode strings. The contents
369 of Null-terminated Unicode string Source are concatenated to the end of
370 Null-terminated Unicode string Destination, and Destination is returned. At
371 most, Length Unicode characters are concatenated from Source to the end of
372 Destination, and Destination is always Null-terminated. If Length is 0, then
373 Destination is returned unmodified. If Source and Destination overlap, then
374 the results are undefined.
376 If Destination is NULL, then ASSERT().
377 If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().
378 If Length > 0 and Source is NULL, then ASSERT().
379 If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().
380 If Source and Destination overlap, then ASSERT().
381 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
382 than PcdMaximumUnicodeStringLength Unicode characters not including the
383 Null-terminator, then ASSERT().
384 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
385 PcdMaximumUnicodeStringLength Unicode characters not including the
386 Null-terminator, then ASSERT().
387 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
388 and Source results in a Unicode string with more than
389 PcdMaximumUnicodeStringLength Unicode characters not including the
390 Null-terminator, then ASSERT().
392 @param Destination Pointer to a Null-terminated Unicode string.
393 @param Source Pointer to a Null-terminated Unicode string.
394 @param Length Maximum number of Unicode characters to concatenate from
403 IN OUT CHAR16
*Destination
,
404 IN CONST CHAR16
*Source
,
408 StrnCpy (Destination
+ StrLen (Destination
), Source
, Length
);
411 // Size of the resulting string should never be zero.
412 // PcdMaximumUnicodeStringLength is tested inside StrLen().
414 ASSERT (StrSize (Destination
) != 0);
419 Returns the first occurance of a Null-terminated Unicode sub-string
420 in a Null-terminated Unicode string.
422 This function scans the contents of the Null-terminated Unicode string
423 specified by String and returns the first occurrence of SearchString.
424 If SearchString is not found in String, then NULL is returned. If
425 the length of SearchString is zero, then String is
428 If String is NULL, then ASSERT().
429 If String is not aligned on a 16-bit boundary, then ASSERT().
430 If SearchString is NULL, then ASSERT().
431 If SearchString is not aligned on a 16-bit boundary, then ASSERT().
433 If PcdMaximumUnicodeStringLength is not zero, and SearchString
434 or String contains more than PcdMaximumUnicodeStringLength Unicode
435 characters not including the Null-terminator, then ASSERT().
437 @param String Pointer to a Null-terminated Unicode string.
438 @param SearchString Pointer to a Null-terminated Unicode string to search for.
440 @retval NULL If the SearchString does not appear in String.
441 @retval !NULL If there is a match.
447 IN CONST CHAR16
*String
,
448 IN CONST CHAR16
*SearchString
451 CONST CHAR16
*FirstMatch
;
452 CONST CHAR16
*SearchStringTmp
;
454 ASSERT (String
!= NULL
);
455 ASSERT (((UINTN
) String
& 0x01) == 0);
456 ASSERT (SearchString
!= NULL
);
457 ASSERT (((UINTN
) SearchString
& 0x01) == 0);
460 // If PcdMaximumUnicodeStringLength is not zero,
461 // length of String should not more than PcdMaximumUnicodeStringLength
463 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
464 ASSERT (StrLen (String
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
468 // If PcdMaximumUnicodeStringLength is not zero,
469 // length of SearchString should not more than PcdMaximumUnicodeStringLength
471 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
472 ASSERT (StrLen (SearchString
) < PcdGet32 (PcdMaximumAsciiStringLength
));
475 while (*String
!= '\0') {
476 SearchStringTmp
= SearchString
;
479 while ((*String
== *SearchStringTmp
)
480 && (*SearchStringTmp
!= '\0')
481 && (*String
!= '\0')) {
486 if (*SearchStringTmp
== '\0') {
487 return (CHAR16
*) FirstMatch
;
490 if (SearchStringTmp
== SearchString
) {
492 // If no character from SearchString match,
493 // move the pointer to the String under search
504 Check if a Unicode character is a decimal character.
506 This internal function checks if a Unicode character is a
507 decimal character. The valid decimal character is from
511 @param Char The character to check against.
513 @retval TRUE If the Char is a decmial character.
514 @retval FALSE Otherwise.
519 InternalIsDecimalDigitCharacter (
523 return (BOOLEAN
) (Char
>= L
'0' && Char
<= L
'9');
527 Convert a Unicode character to upper case only if
528 it maps to a valid small-case ASCII character.
530 This internal function only deal with Unicode character
531 which maps to a valid small-case ASII character, i.e.
532 L'a' to L'z'. For other Unicode character, the input character
533 is returned directly.
536 @param Char The character to convert.
538 @retval LowerCharacter If the Char is with range L'a' to L'z'.
539 @retval Unchanged Otherwise.
544 InternalCharToUpper (
548 if (Char
>= L
'a' && Char
<= L
'z') {
549 return (CHAR16
) (Char
- (L
'a' - L
'A'));
556 Convert a Unicode character to numerical value.
558 This internal function only deal with Unicode character
559 which maps to a valid hexadecimal ASII character, i.e.
560 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
561 Unicode character, the value returned does not make sense.
563 @param Char The character to convert.
565 @retval UINTN The numerical value converted.
570 InternalHexCharToUintn (
574 if (InternalIsDecimalDigitCharacter (Char
)) {
578 return (UINTN
) (10 + InternalCharToUpper (Char
) - L
'A');
582 Check if a Unicode character is a hexadecimal character.
584 This internal function checks if a Unicode character is a
585 decimal character. The valid hexadecimal character is
586 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
589 @param Char The character to check against.
591 @retval TRUE If the Char is a hexadecmial character.
592 @retval FALSE Otherwise.
597 InternalIsHexaDecimalDigitCharacter (
602 return (BOOLEAN
) (InternalIsDecimalDigitCharacter (Char
) ||
603 (Char
>= L
'A' && Char
<= L
'F') ||
604 (Char
>= L
'a' && Char
<= L
'f'));
608 Convert a Null-terminated Unicode decimal string to a value of
611 This function returns a value of type UINTN by interpreting the contents
612 of the Unicode string specified by String as a decimal number. The format
613 of the input Unicode string String is:
615 [spaces] [decimal digits].
617 The valid decimal digit character is in the range [0-9]. The
618 function will ignore the pad space, which includes spaces or
619 tab characters, before [decimal digits]. The running zero in the
620 beginning of [decimal digits] will be ignored. Then, the function
621 stops at the first character that is a not a valid decimal character
622 or a Null-terminator, whichever one comes first.
624 If String is NULL, then ASSERT().
625 If String is not aligned in a 16-bit boundary, then ASSERT().
626 If String has only pad spaces, then 0 is returned.
627 If String has no pad spaces or valid decimal digits,
629 If the number represented by String overflows according
630 to the range defined by UINTN, then ASSERT().
632 If PcdMaximumUnicodeStringLength is not zero, and String contains
633 more than PcdMaximumUnicodeStringLength Unicode characters not including
634 the Null-terminator, then ASSERT().
636 @param String Pointer to a Null-terminated Unicode string.
644 IN CONST CHAR16
*String
649 ASSERT (String
!= NULL
);
650 ASSERT (((UINTN
) String
& 0x01) == 0);
651 ASSERT (StrLen (String
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
654 // Ignore the pad spaces (space or tab)
656 while ((*String
== L
' ') || (*String
== L
'\t')) {
661 // Ignore leading Zeros after the spaces
663 while (*String
== L
'0') {
669 while (InternalIsDecimalDigitCharacter (*String
)) {
671 // If the number represented by String overflows according
672 // to the range defined by UINTN, then ASSERT().
674 ASSERT ((Result
< QUIENT_MAX_UINTN_DIVIDED_BY_10
) ||
675 ((Result
== QUIENT_MAX_UINTN_DIVIDED_BY_10
) &&
676 (*String
- L
'0') <= REMINDER_MAX_UINTN_DIVIDED_BY_10
)
679 Result
= Result
* 10 + (*String
- L
'0');
688 Convert a Null-terminated Unicode decimal string to a value of
691 This function returns a value of type UINT64 by interpreting the contents
692 of the Unicode string specified by String as a decimal number. The format
693 of the input Unicode string String is:
695 [spaces] [decimal digits].
697 The valid decimal digit character is in the range [0-9]. The
698 function will ignore the pad space, which includes spaces or
699 tab characters, before [decimal digits]. The running zero in the
700 beginning of [decimal digits] will be ignored. Then, the function
701 stops at the first character that is a not a valid decimal character
702 or a Null-terminator, whichever one comes first.
704 If String is NULL, then ASSERT().
705 If String is not aligned in a 16-bit boundary, then ASSERT().
706 If String has only pad spaces, then 0 is returned.
707 If String has no pad spaces or valid decimal digits,
709 If the number represented by String overflows according
710 to the range defined by UINT64, then ASSERT().
712 If PcdMaximumUnicodeStringLength is not zero, and String contains
713 more than PcdMaximumUnicodeStringLength Unicode characters not including
714 the Null-terminator, then ASSERT().
716 @param String Pointer to a Null-terminated Unicode string.
724 IN CONST CHAR16
*String
729 ASSERT (String
!= NULL
);
730 ASSERT (((UINTN
) String
& 0x01) == 0);
731 ASSERT (StrLen (String
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
734 // Ignore the pad spaces (space or tab)
736 while ((*String
== L
' ') || (*String
== L
'\t')) {
741 // Ignore leading Zeros after the spaces
743 while (*String
== L
'0') {
749 while (InternalIsDecimalDigitCharacter (*String
)) {
751 // If the number represented by String overflows according
752 // to the range defined by UINTN, then ASSERT().
754 ASSERT ((Result
< QUIENT_MAX_UINT64_DIVIDED_BY_10
) ||
755 ((Result
== QUIENT_MAX_UINT64_DIVIDED_BY_10
) &&
756 (*String
- L
'0') <= REMINDER_MAX_UINT64_DIVIDED_BY_10
)
759 Result
= MultU64x32 (Result
, 10) + (*String
- L
'0');
767 Convert a Null-terminated Unicode hexadecimal string to a value of type UINTN.
769 This function returns a value of type UINTN by interpreting the contents
770 of the Unicode string specified by String as a hexadecimal number.
771 The format of the input Unicode string String is:
773 [spaces][zeros][x][hexadecimal digits].
775 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
776 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
777 If "x" appears in the input string, it must be prefixed with at least one 0.
778 The function will ignore the pad space, which includes spaces or tab characters,
779 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
780 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
781 first valid hexadecimal digit. Then, the function stops at the first character that is
782 a not a valid hexadecimal character or NULL, whichever one comes first.
784 If String is NULL, then ASSERT().
785 If String is not aligned in a 16-bit boundary, then ASSERT().
786 If String has only pad spaces, then zero is returned.
787 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
788 then zero is returned.
789 If the number represented by String overflows according to the range defined by
790 UINTN, then ASSERT().
792 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
793 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator,
796 @param String Pointer to a Null-terminated Unicode string.
804 IN CONST CHAR16
*String
809 ASSERT (String
!= NULL
);
810 ASSERT (((UINTN
) String
& 0x01) == 0);
811 ASSERT (StrLen (String
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
814 // Ignore the pad spaces (space or tab)
816 while ((*String
== L
' ') || (*String
== L
'\t')) {
821 // Ignore leading Zeros after the spaces
823 while (*String
== L
'0') {
827 if (InternalCharToUpper (*String
) == L
'X') {
828 ASSERT (*(String
- 1) == L
'0');
829 if (*(String
- 1) != L
'0') {
840 while (InternalIsHexaDecimalDigitCharacter (*String
)) {
842 // If the Hex Number represented by String overflows according
843 // to the range defined by UINTN, then ASSERT().
845 ASSERT ((Result
< QUIENT_MAX_UINTN_DIVIDED_BY_16
) ||
846 ((Result
== QUIENT_MAX_UINTN_DIVIDED_BY_16
) &&
847 (InternalHexCharToUintn (*String
) <= REMINDER_MAX_UINTN_DIVIDED_BY_16
))
850 Result
= (Result
<< 4) + InternalHexCharToUintn (*String
);
859 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
861 This function returns a value of type UINT64 by interpreting the contents
862 of the Unicode string specified by String as a hexadecimal number.
863 The format of the input Unicode string String is
865 [spaces][zeros][x][hexadecimal digits].
867 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
868 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
869 If "x" appears in the input string, it must be prefixed with at least one 0.
870 The function will ignore the pad space, which includes spaces or tab characters,
871 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
872 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
873 first valid hexadecimal digit. Then, the function stops at the first character that is
874 a not a valid hexadecimal character or NULL, whichever one comes first.
876 If String is NULL, then ASSERT().
877 If String is not aligned in a 16-bit boundary, then ASSERT().
878 If String has only pad spaces, then zero is returned.
879 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
880 then zero is returned.
881 If the number represented by String overflows according to the range defined by
882 UINT64, then ASSERT().
884 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
885 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator,
888 @param String Pointer to a Null-terminated Unicode string.
896 IN CONST CHAR16
*String
901 ASSERT (String
!= NULL
);
902 ASSERT (((UINTN
) String
& 0x01) == 0);
903 ASSERT (StrLen (String
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
906 // Ignore the pad spaces (space or tab)
908 while ((*String
== L
' ') || (*String
== L
'\t')) {
913 // Ignore leading Zeros after the spaces
915 while (*String
== L
'0') {
919 if (InternalCharToUpper (*String
) == L
'X') {
920 ASSERT (*(String
- 1) == L
'0');
921 if (*(String
- 1) != L
'0') {
932 while (InternalIsHexaDecimalDigitCharacter (*String
)) {
934 // If the Hex Number represented by String overflows according
935 // to the range defined by UINTN, then ASSERT().
937 ASSERT ((Result
< QUIENT_MAX_UINT64_DIVIDED_BY_16
)||
938 ((Result
== QUIENT_MAX_UINT64_DIVIDED_BY_16
) &&
939 (InternalHexCharToUintn (*String
) <= REMINDER_MAX_UINT64_DIVIDED_BY_16
))
942 Result
= LShiftU64 (Result
, 4);
943 Result
= Result
+ InternalHexCharToUintn (*String
);
951 Check if a ASCII character is a decimal character.
953 This internal function checks if a Unicode character is a
954 decimal character. The valid decimal character is from
957 @param Char The character to check against.
959 @retval TRUE If the Char is a decmial character.
960 @retval FALSE Otherwise.
965 InternalAsciiIsDecimalDigitCharacter (
969 return (BOOLEAN
) (Char
>= '0' && Char
<= '9');
973 Check if a ASCII character is a hexadecimal character.
975 This internal function checks if a ASCII character is a
976 decimal character. The valid hexadecimal character is
977 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
980 @param Char The character to check against.
982 @retval TRUE If the Char is a hexadecmial character.
983 @retval FALSE Otherwise.
988 InternalAsciiIsHexaDecimalDigitCharacter (
993 return (BOOLEAN
) (InternalAsciiIsDecimalDigitCharacter (Char
) ||
994 (Char
>= 'A' && Char
<= 'F') ||
995 (Char
>= 'a' && Char
<= 'f'));
999 Convert a Null-terminated Unicode string to a Null-terminated
1000 ASCII string and returns the ASCII string.
1002 This function converts the content of the Unicode string Source
1003 to the ASCII string Destination by copying the lower 8 bits of
1004 each Unicode character. It returns Destination. The function terminates
1005 the ASCII string Destination by appending a Null-terminator character
1006 at the end. The caller is responsible to make sure Destination points
1007 to a buffer with size equal or greater than (StrLen (Source) + 1) in bytes.
1009 If Destination is NULL, then ASSERT().
1010 If Source is NULL, then ASSERT().
1011 If Source is not aligned on a 16-bit boundary, then ASSERT().
1012 If Source and Destination overlap, then ASSERT().
1014 If any Unicode characters in Source contain non-zero value in
1015 the upper 8 bits, then ASSERT().
1017 If PcdMaximumUnicodeStringLength is not zero, and Source contains
1018 more than PcdMaximumUnicodeStringLength Unicode characters not including
1019 the Null-terminator, then ASSERT().
1021 If PcdMaximumAsciiStringLength is not zero, and Source contains more
1022 than PcdMaximumAsciiStringLength Unicode characters not including the
1023 Null-terminator, then ASSERT().
1025 @param Source Pointer to a Null-terminated Unicode string.
1026 @param Destination Pointer to a Null-terminated ASCII string.
1033 UnicodeStrToAsciiStr (
1034 IN CONST CHAR16
*Source
,
1035 OUT CHAR8
*Destination
1038 ASSERT (Destination
!= NULL
);
1039 ASSERT (Source
!= NULL
);
1040 ASSERT (((UINTN
) Source
& 0x01) == 0);
1043 // Source and Destination should not overlap
1045 ASSERT ((UINTN
) ((CHAR16
*) Destination
- Source
) > StrLen (Source
));
1046 ASSERT ((UINTN
) ((CHAR8
*) Source
- Destination
) > StrLen (Source
));
1049 // If PcdMaximumUnicodeStringLength is not zero,
1050 // length of Source should not more than PcdMaximumUnicodeStringLength
1052 if (PcdGet32 (PcdMaximumUnicodeStringLength
) != 0) {
1053 ASSERT (StrLen (Source
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
1056 while (*Source
!= '\0') {
1058 // If any Unicode characters in Source contain
1059 // non-zero value in the upper 8 bits, then ASSERT().
1061 ASSERT (*Source
< 0x100);
1062 *(Destination
++) = (CHAR8
) *(Source
++);
1065 *Destination
= '\0';
1072 Copies one Null-terminated ASCII string to another Null-terminated ASCII
1073 string and returns the new ASCII string.
1075 This function copies the contents of the ASCII string Source to the ASCII
1076 string Destination, and returns Destination. If Source and Destination
1077 overlap, then the results are undefined.
1079 If Destination is NULL, then ASSERT().
1080 If Source is NULL, then ASSERT().
1081 If Source and Destination overlap, then ASSERT().
1082 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1083 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1086 @param Destination Pointer to a Null-terminated ASCII string.
1087 @param Source Pointer to a Null-terminated ASCII string.
1095 OUT CHAR8
*Destination
,
1096 IN CONST CHAR8
*Source
1102 // Destination cannot be NULL
1104 ASSERT (Destination
!= NULL
);
1107 // Destination and source cannot overlap
1109 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
1110 ASSERT ((UINTN
)(Source
- Destination
) > AsciiStrLen (Source
));
1112 ReturnValue
= Destination
;
1114 *(Destination
++) = *(Source
++);
1121 Copies one Null-terminated ASCII string with a maximum length to another
1122 Null-terminated ASCII string with a maximum length and returns the new ASCII
1125 This function copies the contents of the ASCII string Source to the ASCII
1126 string Destination, and returns Destination. At most, Length ASCII characters
1127 are copied from Source to Destination. If Length is 0, then Destination is
1128 returned unmodified. If Length is greater that the number of ASCII characters
1129 in Source, then Destination is padded with Null ASCII characters. If Source
1130 and Destination overlap, then the results are undefined.
1132 If Destination is NULL, then ASSERT().
1133 If Source is NULL, then ASSERT().
1134 If Source and Destination overlap, then ASSERT().
1135 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1136 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1139 @param Destination Pointer to a Null-terminated ASCII string.
1140 @param Source Pointer to a Null-terminated ASCII string.
1141 @param Length Maximum number of ASCII characters to copy.
1149 OUT CHAR8
*Destination
,
1150 IN CONST CHAR8
*Source
,
1161 // Destination cannot be NULL
1163 ASSERT (Destination
!= NULL
);
1166 // Destination and source cannot overlap
1168 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
1169 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
1171 ReturnValue
= Destination
;
1173 while (*Source
&& Length
> 0) {
1174 *(Destination
++) = *(Source
++);
1178 ZeroMem (Destination
, Length
* sizeof (*Destination
));
1183 Returns the length of a Null-terminated ASCII string.
1185 This function returns the number of ASCII characters in the Null-terminated
1186 ASCII string specified by String.
1188 If String is NULL, then ASSERT().
1189 If PcdMaximumAsciiStringLength is not zero and String contains more than
1190 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1193 @param String Pointer to a Null-terminated ASCII string.
1195 @return The length of String.
1201 IN CONST CHAR8
*String
1206 ASSERT (String
!= NULL
);
1208 for (Length
= 0; *String
!= '\0'; String
++, Length
++) {
1210 // If PcdMaximumUnicodeStringLength is not zero,
1211 // length should not more than PcdMaximumUnicodeStringLength
1213 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1214 ASSERT (Length
< PcdGet32 (PcdMaximumAsciiStringLength
));
1221 Returns the size of a Null-terminated ASCII string in bytes, including the
1224 This function returns the size, in bytes, of the Null-terminated ASCII string
1225 specified by String.
1227 If String is NULL, then ASSERT().
1228 If PcdMaximumAsciiStringLength is not zero and String contains more than
1229 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1232 @param String Pointer to a Null-terminated ASCII string.
1234 @return The size of String.
1240 IN CONST CHAR8
*String
1243 return (AsciiStrLen (String
) + 1) * sizeof (*String
);
1247 Compares two Null-terminated ASCII strings, and returns the difference
1248 between the first mismatched ASCII characters.
1250 This function compares the Null-terminated ASCII string FirstString to the
1251 Null-terminated ASCII string SecondString. If FirstString is identical to
1252 SecondString, then 0 is returned. Otherwise, the value returned is the first
1253 mismatched ASCII character in SecondString subtracted from the first
1254 mismatched ASCII character in FirstString.
1256 If FirstString is NULL, then ASSERT().
1257 If SecondString is NULL, then ASSERT().
1258 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1259 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1261 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
1262 than PcdMaximumAsciiStringLength ASCII characters not including the
1263 Null-terminator, then ASSERT().
1265 @param FirstString Pointer to a Null-terminated ASCII string.
1266 @param SecondString Pointer to a Null-terminated ASCII string.
1268 @retval 0 FirstString is identical to SecondString.
1269 @retval !=0 FirstString is not identical to SecondString.
1275 IN CONST CHAR8
*FirstString
,
1276 IN CONST CHAR8
*SecondString
1280 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1282 ASSERT (AsciiStrSize (FirstString
));
1283 ASSERT (AsciiStrSize (SecondString
));
1285 while ((*FirstString
!= '\0') && (*FirstString
== *SecondString
)) {
1290 return *FirstString
- *SecondString
;
1294 Converts a lowercase Ascii character to upper one
1296 If Chr is lowercase Ascii character, then converts it to upper one.
1298 If Value >= 0xA0, then ASSERT().
1299 If (Value & 0x0F) >= 0x0A, then ASSERT().
1301 @param chr one Ascii character
1303 @return The uppercase value of Ascii character
1312 return (UINT8
) ((Chr
>= 'a' && Chr
<= 'z') ? Chr
- ('a' - 'A') : Chr
);
1316 Convert a ASCII character to numerical value.
1318 This internal function only deal with Unicode character
1319 which maps to a valid hexadecimal ASII character, i.e.
1320 '0' to '9', 'a' to 'f' or 'A' to 'F'. For other
1321 ASCII character, the value returned does not make sense.
1323 @param Char The character to convert.
1325 @retval UINTN The numerical value converted.
1330 InternalAsciiHexCharToUintn (
1334 if (InternalIsDecimalDigitCharacter (Char
)) {
1338 return (UINTN
) (10 + AsciiToUpper (Char
) - 'A');
1343 Performs a case insensitive comparison of two Null-terminated ASCII strings,
1344 and returns the difference between the first mismatched ASCII characters.
1346 This function performs a case insensitive comparison of the Null-terminated
1347 ASCII string FirstString to the Null-terminated ASCII string SecondString. If
1348 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
1349 value returned is the first mismatched lower case ASCII character in
1350 SecondString subtracted from the first mismatched lower case ASCII character
1353 If FirstString is NULL, then ASSERT().
1354 If SecondString is NULL, then ASSERT().
1355 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1356 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1358 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
1359 than PcdMaximumAsciiStringLength ASCII characters not including the
1360 Null-terminator, then ASSERT().
1362 @param FirstString Pointer to a Null-terminated ASCII string.
1363 @param SecondString Pointer to a Null-terminated ASCII string.
1365 @retval 0 FirstString is identical to SecondString using case insensitive
1367 @retval !=0 FirstString is not identical to SecondString using case
1368 insensitive comparisons.
1374 IN CONST CHAR8
*FirstString
,
1375 IN CONST CHAR8
*SecondString
1378 CHAR8 UpperFirstString
;
1379 CHAR8 UpperSecondString
;
1382 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1384 ASSERT (AsciiStrSize (FirstString
));
1385 ASSERT (AsciiStrSize (SecondString
));
1387 UpperFirstString
= AsciiToUpper (*FirstString
);
1388 UpperSecondString
= AsciiToUpper (*SecondString
);
1389 while ((*FirstString
!= '\0') && (UpperFirstString
== UpperSecondString
)) {
1392 UpperFirstString
= AsciiToUpper (*FirstString
);
1393 UpperSecondString
= AsciiToUpper (*SecondString
);
1396 return UpperFirstString
- UpperSecondString
;
1400 Compares two Null-terminated ASCII strings with maximum lengths, and returns
1401 the difference between the first mismatched ASCII characters.
1403 This function compares the Null-terminated ASCII string FirstString to the
1404 Null-terminated ASCII string SecondString. At most, Length ASCII characters
1405 will be compared. If Length is 0, then 0 is returned. If FirstString is
1406 identical to SecondString, then 0 is returned. Otherwise, the value returned
1407 is the first mismatched ASCII character in SecondString subtracted from the
1408 first mismatched ASCII character in FirstString.
1410 If FirstString is NULL, then ASSERT().
1411 If SecondString is NULL, then ASSERT().
1412 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1413 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1415 If PcdMaximumAsciiStringLength is not zero and SecondString contains more than
1416 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1419 @param FirstString Pointer to a Null-terminated ASCII string.
1420 @param SecondString Pointer to a Null-terminated ASCII string.
1422 @retval 0 FirstString is identical to SecondString.
1423 @retval !=0 FirstString is not identical to SecondString.
1429 IN CONST CHAR8
*FirstString
,
1430 IN CONST CHAR8
*SecondString
,
1439 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1441 ASSERT (AsciiStrSize (FirstString
));
1442 ASSERT (AsciiStrSize (SecondString
));
1444 while ((*FirstString
!= '\0') &&
1445 (*FirstString
== *SecondString
) &&
1451 return *FirstString
- *SecondString
;
1455 Concatenates one Null-terminated ASCII string to another Null-terminated
1456 ASCII string, and returns the concatenated ASCII string.
1458 This function concatenates two Null-terminated ASCII strings. The contents of
1459 Null-terminated ASCII string Source are concatenated to the end of Null-
1460 terminated ASCII string Destination. The Null-terminated concatenated ASCII
1463 If Destination is NULL, then ASSERT().
1464 If Source is NULL, then ASSERT().
1465 If PcdMaximumAsciiStringLength is not zero and Destination contains more than
1466 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1468 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1469 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1471 If PcdMaximumAsciiStringLength is not zero and concatenating Destination and
1472 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1473 ASCII characters, then ASSERT().
1475 @param Destination Pointer to a Null-terminated ASCII string.
1476 @param Source Pointer to a Null-terminated ASCII string.
1484 IN OUT CHAR8
*Destination
,
1485 IN CONST CHAR8
*Source
1488 AsciiStrCpy (Destination
+ AsciiStrLen (Destination
), Source
);
1491 // Size of the resulting string should never be zero.
1492 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1494 ASSERT (AsciiStrSize (Destination
) != 0);
1499 Concatenates one Null-terminated ASCII string with a maximum length to the
1500 end of another Null-terminated ASCII string, and returns the concatenated
1503 This function concatenates two Null-terminated ASCII strings. The contents
1504 of Null-terminated ASCII string Source are concatenated to the end of Null-
1505 terminated ASCII string Destination, and Destination is returned. At most,
1506 Length ASCII characters are concatenated from Source to the end of
1507 Destination, and Destination is always Null-terminated. If Length is 0, then
1508 Destination is returned unmodified. If Source and Destination overlap, then
1509 the results are undefined.
1511 If Destination is NULL, then ASSERT().
1512 If Source is NULL, then ASSERT().
1513 If Source and Destination overlap, then ASSERT().
1514 If PcdMaximumAsciiStringLength is not zero, and Destination contains more than
1515 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1517 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1518 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1520 If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and
1521 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1522 ASCII characters not including the Null-terminator, then ASSERT().
1524 @param Destination Pointer to a Null-terminated ASCII string.
1525 @param Source Pointer to a Null-terminated ASCII string.
1526 @param Length Maximum number of ASCII characters to concatenate from
1535 IN OUT CHAR8
*Destination
,
1536 IN CONST CHAR8
*Source
,
1540 AsciiStrnCpy (Destination
+ AsciiStrLen (Destination
), Source
, Length
);
1543 // Size of the resulting string should never be zero.
1544 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1546 ASSERT (AsciiStrSize (Destination
) != 0);
1551 Returns the first occurance of a Null-terminated ASCII sub-string
1552 in a Null-terminated ASCII string.
1554 This function scans the contents of the ASCII string specified by String
1555 and returns the first occurrence of SearchString. If SearchString is not
1556 found in String, then NULL is returned. If the length of SearchString is zero,
1557 then String is returned.
1559 If String is NULL, then ASSERT().
1560 If SearchString is NULL, then ASSERT().
1562 If PcdMaximumAsciiStringLength is not zero, and SearchString or
1563 String contains more than PcdMaximumAsciiStringLength Unicode characters
1564 not including the Null-terminator, then ASSERT().
1566 @param String Pointer to a Null-terminated ASCII string.
1567 @param SearchString Pointer to a Null-terminated ASCII string to search for.
1569 @retval NULL If the SearchString does not appear in String.
1570 @retval !NULL If there is a match.
1576 IN CONST CHAR8
*String
,
1577 IN CONST CHAR8
*SearchString
1580 CONST CHAR8
*FirstMatch
;
1581 CONST CHAR8
*SearchStringTmp
;
1583 ASSERT (String
!= NULL
);
1584 ASSERT (SearchString
!= NULL
);
1587 // If PcdMaximumUnicodeStringLength is not zero,
1588 // length of String should not more than PcdMaximumUnicodeStringLength
1590 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1591 ASSERT (AsciiStrLen (String
) < PcdGet32 (PcdMaximumAsciiStringLength
));
1595 // If PcdMaximumUnicodeStringLength is not zero,
1596 // length of SearchString should not more than PcdMaximumUnicodeStringLength
1598 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
1599 ASSERT (AsciiStrLen (SearchString
) < PcdGet32 (PcdMaximumAsciiStringLength
));
1602 while (*String
!= '\0') {
1603 SearchStringTmp
= SearchString
;
1604 FirstMatch
= String
;
1606 while ((*String
== *SearchStringTmp
)
1607 && (*SearchStringTmp
!= '\0')
1608 && (*String
!= '\0')) {
1613 if (*SearchStringTmp
== '\0') {
1614 return (CHAR8
*) FirstMatch
;
1617 if (SearchStringTmp
== SearchString
) {
1619 // If no character from SearchString match,
1620 // move the pointer to the String under search
1621 // by one character.
1632 Convert a Null-terminated ASCII decimal string to a value of type
1635 This function returns a value of type UINTN by interpreting the contents
1636 of the ASCII string String as a decimal number. The format of the input
1637 ASCII string String is:
1639 [spaces] [decimal digits].
1641 The valid decimal digit character is in the range [0-9]. The function will
1642 ignore the pad space, which includes spaces or tab characters, before the digits.
1643 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1644 function stops at the first character that is a not a valid decimal character or
1645 Null-terminator, whichever on comes first.
1647 If String has only pad spaces, then 0 is returned.
1648 If String has no pad spaces or valid decimal digits, then 0 is returned.
1649 If the number represented by String overflows according to the range defined by
1650 UINTN, then ASSERT().
1651 If String is NULL, then ASSERT().
1652 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1653 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1656 @param String Pointer to a Null-terminated ASCII string.
1663 AsciiStrDecimalToUintn (
1664 IN CONST CHAR8
*String
1669 ASSERT (String
!= NULL
);
1670 ASSERT (AsciiStrLen (String
) < PcdGet32 (PcdMaximumAsciiStringLength
));
1673 // Ignore the pad spaces (space or tab)
1675 while ((*String
== ' ') || (*String
== '\t')) {
1680 // Ignore leading Zeros after the spaces
1682 while (*String
== '0') {
1688 while (InternalAsciiIsDecimalDigitCharacter (*String
)) {
1690 // If the number represented by String overflows according
1691 // to the range defined by UINTN, then ASSERT().
1693 ASSERT ((Result
< QUIENT_MAX_UINTN_DIVIDED_BY_10
) ||
1694 ((Result
== QUIENT_MAX_UINTN_DIVIDED_BY_10
) &&
1695 (*String
- '0') <= REMINDER_MAX_UINTN_DIVIDED_BY_10
)
1698 Result
= Result
* 10 + (*String
- '0');
1707 Convert a Null-terminated ASCII decimal string to a value of type
1710 This function returns a value of type UINT64 by interpreting the contents
1711 of the ASCII string String as a decimal number. The format of the input
1712 ASCII string String is:
1714 [spaces] [decimal digits].
1716 The valid decimal digit character is in the range [0-9]. The function will
1717 ignore the pad space, which includes spaces or tab characters, before the digits.
1718 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1719 function stops at the first character that is a not a valid decimal character or
1720 Null-terminator, whichever on comes first.
1722 If String has only pad spaces, then 0 is returned.
1723 If String has no pad spaces or valid decimal digits, then 0 is returned.
1724 If the number represented by String overflows according to the range defined by
1725 UINT64, then ASSERT().
1726 If String is NULL, then ASSERT().
1727 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1728 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1731 @param String Pointer to a Null-terminated ASCII string.
1738 AsciiStrDecimalToUint64 (
1739 IN CONST CHAR8
*String
1744 ASSERT (String
!= NULL
);
1745 ASSERT (AsciiStrLen (String
) < PcdGet32 (PcdMaximumAsciiStringLength
));
1748 // Ignore the pad spaces (space or tab)
1750 while ((*String
== ' ') || (*String
== '\t')) {
1755 // Ignore leading Zeros after the spaces
1757 while (*String
== '0') {
1763 while (InternalAsciiIsDecimalDigitCharacter (*String
)) {
1765 // If the number represented by String overflows according
1766 // to the range defined by UINTN, then ASSERT().
1768 ASSERT ((Result
< QUIENT_MAX_UINT64_DIVIDED_BY_10
) ||
1769 ((Result
== QUIENT_MAX_UINT64_DIVIDED_BY_10
) &&
1770 (*String
- '0') <= REMINDER_MAX_UINT64_DIVIDED_BY_10
)
1773 Result
= MultU64x32 (Result
, 10) + (*String
- '0');
1781 Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.
1783 This function returns a value of type UINTN by interpreting the contents of
1784 the ASCII string String as a hexadecimal number. The format of the input ASCII
1787 [spaces][zeros][x][hexadecimal digits].
1789 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1790 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1791 appears in the input string, it must be prefixed with at least one 0. The function
1792 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1793 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1794 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1795 digit. Then, the function stops at the first character that is a not a valid
1796 hexadecimal character or Null-terminator, whichever on comes first.
1798 If String has only pad spaces, then 0 is returned.
1799 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1802 If the number represented by String overflows according to the range defined by UINTN,
1804 If String is NULL, then ASSERT().
1805 If PcdMaximumAsciiStringLength is not zero,
1806 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1807 the Null-terminator, then ASSERT().
1809 @param String Pointer to a Null-terminated ASCII string.
1816 AsciiStrHexToUintn (
1817 IN CONST CHAR8
*String
1822 ASSERT (String
!= NULL
);
1823 ASSERT (AsciiStrLen (String
) < PcdGet32 (PcdMaximumAsciiStringLength
));
1826 // Ignore the pad spaces (space or tab)
1828 while ((*String
== ' ') || (*String
== '\t')) {
1833 // Ignore leading Zeros after the spaces
1835 while (*String
== '0') {
1839 if (AsciiToUpper (*String
) == 'X') {
1840 ASSERT (*(String
- 1) == '0');
1841 if (*(String
- 1) != '0') {
1852 while (InternalAsciiIsHexaDecimalDigitCharacter (*String
)) {
1854 // If the Hex Number represented by String overflows according
1855 // to the range defined by UINTN, then ASSERT().
1857 ASSERT ((Result
< QUIENT_MAX_UINTN_DIVIDED_BY_16
) ||
1858 ((Result
== QUIENT_MAX_UINTN_DIVIDED_BY_16
) &&
1859 (InternalAsciiHexCharToUintn (*String
) <= REMINDER_MAX_UINTN_DIVIDED_BY_16
))
1862 Result
= (Result
<< 4) + InternalAsciiHexCharToUintn (*String
);
1871 Convert a Null-terminated ASCII hexadecimal string to a value of type UINT64.
1873 This function returns a value of type UINT64 by interpreting the contents of
1874 the ASCII string String as a hexadecimal number. The format of the input ASCII
1877 [spaces][zeros][x][hexadecimal digits].
1879 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1880 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1881 appears in the input string, it must be prefixed with at least one 0. The function
1882 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1883 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1884 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1885 digit. Then, the function stops at the first character that is a not a valid
1886 hexadecimal character or Null-terminator, whichever on comes first.
1888 If String has only pad spaces, then 0 is returned.
1889 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1892 If the number represented by String overflows according to the range defined by UINT64,
1894 If String is NULL, then ASSERT().
1895 If PcdMaximumAsciiStringLength is not zero,
1896 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1897 the Null-terminator, then ASSERT().
1899 @param String Pointer to a Null-terminated ASCII string.
1906 AsciiStrHexToUint64 (
1907 IN CONST CHAR8
*String
1912 ASSERT (String
!= NULL
);
1913 ASSERT (AsciiStrLen (String
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
1916 // Ignore the pad spaces (space or tab) and leading Zeros
1919 // Ignore the pad spaces (space or tab)
1921 while ((*String
== ' ') || (*String
== '\t')) {
1926 // Ignore leading Zeros after the spaces
1928 while (*String
== '0') {
1932 if (AsciiToUpper (*String
) == 'X') {
1933 ASSERT (*(String
- 1) == '0');
1934 if (*(String
- 1) != '0') {
1945 while (InternalAsciiIsHexaDecimalDigitCharacter (*String
)) {
1947 // If the Hex Number represented by String overflows according
1948 // to the range defined by UINTN, then ASSERT().
1950 ASSERT ((Result
< QUIENT_MAX_UINT64_DIVIDED_BY_16
) ||
1951 ((Result
== QUIENT_MAX_UINT64_DIVIDED_BY_16
) &&
1952 (InternalAsciiHexCharToUintn (*String
) <= REMINDER_MAX_UINT64_DIVIDED_BY_16
))
1955 Result
= LShiftU64 (Result
, 4);
1956 Result
= Result
+ InternalAsciiHexCharToUintn (*String
);
1965 Convert one Null-terminated ASCII string to a Null-terminated
1966 Unicode string and returns the Unicode string.
1968 This function converts the contents of the ASCII string Source to the Unicode
1969 string Destination, and returns Destination. The function terminates the
1970 Unicode string Destination by appending a Null-terminator character at the end.
1971 The caller is responsible to make sure Destination points to a buffer with size
1972 equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.
1974 If Destination is NULL, then ASSERT().
1975 If Destination is not aligned on a 16-bit boundary, then ASSERT().
1976 If Source is NULL, then ASSERT().
1977 If Source and Destination overlap, then ASSERT().
1978 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1979 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1981 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
1982 PcdMaximumUnicodeStringLength ASCII characters not including the
1983 Null-terminator, then ASSERT().
1985 @param Source Pointer to a Null-terminated ASCII string.
1986 @param Destination Pointer to a Null-terminated Unicode string.
1993 AsciiStrToUnicodeStr (
1994 IN CONST CHAR8
*Source
,
1995 OUT CHAR16
*Destination
1998 ASSERT (Destination
!= NULL
);
1999 ASSERT (Source
!= NULL
);
2002 // Source and Destination should not overlap
2004 ASSERT ((UINTN
) ((CHAR8
*) Destination
- Source
) > AsciiStrLen (Source
));
2005 ASSERT ((UINTN
) (Source
- (CHAR8
*) Destination
) > (AsciiStrLen (Source
) * sizeof (CHAR16
)));
2008 // If PcdMaximumAsciiStringLength is not zero,
2009 // length of Source should not more than PcdMaximumUnicodeStringLength
2011 if (PcdGet32 (PcdMaximumAsciiStringLength
) != 0) {
2012 ASSERT (AsciiStrLen (Source
) < PcdGet32 (PcdMaximumAsciiStringLength
));
2015 while (*Source
!= '\0') {
2016 *(Destination
++) = (CHAR16
) *(Source
++);
2019 // End the Destination with a NULL.
2021 *Destination
= '\0';
2027 Converts an 8-bit value to an 8-bit BCD value.
2029 Converts the 8-bit value specified by Value to BCD. The BCD value is
2032 If Value >= 100, then ASSERT().
2034 @param Value The 8-bit value to convert to BCD. Range 0..99.
2036 @return The BCD value
2045 ASSERT (Value
< 100);
2046 return (UINT8
) (((Value
/ 10) << 4) | (Value
% 10));
2050 Converts an 8-bit BCD value to an 8-bit value.
2052 Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit
2055 If Value >= 0xA0, then ASSERT().
2056 If (Value & 0x0F) >= 0x0A, then ASSERT().
2058 @param Value The 8-bit BCD value to convert to an 8-bit value.
2060 @return The 8-bit value is returned.
2069 ASSERT (Value
< 0xa0);
2070 ASSERT ((Value
& 0xf) < 0xa);
2071 return (UINT8
) ((Value
>> 4) * 10 + (Value
& 0xf));