/** @file\r
Unicode and ASCII string primatives.\r
\r
- Copyright (c) 2006 - 2008, Intel Corporation<BR>\r
- All rights reserved. This program and the accompanying materials\r
+ Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
+ This program and the accompanying materials\r
are licensed and made available under the terms and conditions of the BSD License\r
which accompanies this distribution. The full text of the license may be found at\r
- http://opensource.org/licenses/bsd-license.php\r
+ http://opensource.org/licenses/bsd-license.php.\r
\r
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
\r
#include "BaseLibInternals.h"\r
\r
-#define QUOTIENT_MAX_UINTN_DIVIDED_BY_10 ((UINTN) -1 / 10)\r
-#define REMAINDER_MAX_UINTN_DIVIDED_BY_10 ((UINTN) -1 % 10)\r
-\r
-#define QUOTIENT_MAX_UINTN_DIVIDED_BY_16 ((UINTN) -1 / 16)\r
-#define REMAINDER_MAX_UINTN_DIVIDED_BY_16 ((UINTN) -1 % 16)\r
-\r
-#define QUOTIENT_MAX_UINT64_DIVIDED_BY_10 ((UINT64) -1 / 10)\r
-#define REMAINDER_MAX_UINT64_DIVIDED_BY_10 ((UINT64) -1 % 10)\r
-\r
-#define QUOTIENT_MAX_UINT64_DIVIDED_BY_16 ((UINT64) -1 / 16)\r
-#define REMAINDER_MAX_UINT64_DIVIDED_BY_16 ((UINT64) -1 % 16)\r
+#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
\r
/**\r
+ [ATTENTION] This function will be deprecated for security reason.\r
+\r
Copies one Null-terminated Unicode string to another Null-terminated Unicode\r
string and returns the new Unicode string.\r
\r
If Source is not aligned on a 16-bit boundary, then ASSERT().\r
If Source and Destination overlap, then ASSERT().\r
If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
- PcdMaximumUnicodeStringLength Unicode characters not including the\r
+ PcdMaximumUnicodeStringLength Unicode characters, not including the\r
Null-terminator, then ASSERT().\r
\r
- @param Destination Pointer to a Null-terminated Unicode string.\r
- @param Source Pointer to a Null-terminated Unicode string.\r
+ @param Destination A pointer to a Null-terminated Unicode string.\r
+ @param Source A pointer to a Null-terminated Unicode string.\r
\r
@return Destination.\r
\r
}\r
\r
/**\r
+ [ATTENTION] This function will be deprecated for security reason.\r
+\r
Copies up to a specified length from one Null-terminated Unicode string to \r
another Null-terminated Unicode string and returns the new Unicode string.\r
\r
If Length > 0 and Destination is NULL, then ASSERT().\r
If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().\r
If Length > 0 and Source is NULL, then ASSERT().\r
- If Length > 0 and Source is not aligned on a 16-bit bounadry, then ASSERT().\r
+ If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().\r
If Source and Destination overlap, then ASSERT().\r
+ If PcdMaximumUnicodeStringLength is not zero, and Length is greater than \r
+ PcdMaximumUnicodeStringLength, then ASSERT().\r
If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
- PcdMaximumUnicodeStringLength Unicode characters not including the\r
- Null-terminator, then ASSERT().\r
+ PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
+ then ASSERT().\r
\r
- @param Destination Pointer to a Null-terminated Unicode string.\r
- @param Source Pointer to a Null-terminated Unicode string.\r
- @param Length Maximum number of Unicode characters to copy.\r
+ @param Destination A pointer to a Null-terminated Unicode string.\r
+ @param Source A pointer to a Null-terminated Unicode string.\r
+ @param Length The maximum number of Unicode characters to copy.\r
\r
@return Destination.\r
\r
ASSERT ((UINTN)(Destination - Source) > StrLen (Source));\r
ASSERT ((UINTN)(Source - Destination) >= Length);\r
\r
+ if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r
+ ASSERT (Length <= PcdGet32 (PcdMaximumUnicodeStringLength));\r
+ }\r
+\r
ReturnValue = Destination;\r
\r
while ((*Source != L'\0') && (Length > 0)) {\r
ZeroMem (Destination, Length * sizeof (*Destination));\r
return ReturnValue;\r
}\r
+#endif\r
\r
/**\r
Returns the length of a Null-terminated Unicode string.\r
If String is NULL, then ASSERT().\r
If String is not aligned on a 16-bit boundary, then ASSERT().\r
If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
- PcdMaximumUnicodeStringLength Unicode characters not including the\r
+ PcdMaximumUnicodeStringLength Unicode characters, not including the\r
Null-terminator, then ASSERT().\r
\r
- @param String Pointer to a Null-terminated Unicode string.\r
+ @param String A pointer to a Null-terminated Unicode string.\r
\r
@return The length of String.\r
\r
If String is NULL, then ASSERT().\r
If String is not aligned on a 16-bit boundary, then ASSERT().\r
If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
- PcdMaximumUnicodeStringLength Unicode characters not including the\r
+ PcdMaximumUnicodeStringLength Unicode characters, not including the\r
Null-terminator, then ASSERT().\r
\r
- @param String Pointer to a Null-terminated Unicode string.\r
+ @param String A pointer to a Null-terminated Unicode string.\r
\r
@return The size of String.\r
\r
If SecondString is NULL, then ASSERT().\r
If SecondString is not aligned on a 16-bit boundary, then ASSERT().\r
If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more\r
- than PcdMaximumUnicodeStringLength Unicode characters not including the\r
+ than PcdMaximumUnicodeStringLength Unicode characters, not including the\r
Null-terminator, then ASSERT().\r
If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more\r
- than PcdMaximumUnicodeStringLength Unicode characters not including the\r
+ than PcdMaximumUnicodeStringLength Unicode characters, not including the\r
Null-terminator, then ASSERT().\r
\r
- @param FirstString Pointer to a Null-terminated Unicode string.\r
- @param SecondString Pointer to a Null-terminated Unicode string.\r
+ @param FirstString A pointer to a Null-terminated Unicode string.\r
+ @param SecondString A pointer to a Null-terminated Unicode string.\r
\r
@retval 0 FirstString is identical to SecondString.\r
@return others FirstString is not identical to SecondString.\r
/**\r
Compares up to a specified length the contents of two Null-terminated Unicode strings,\r
and returns the difference between the first mismatched Unicode characters.\r
-\r
+ \r
This function compares the Null-terminated Unicode string FirstString to the\r
Null-terminated Unicode string SecondString. At most, Length Unicode\r
characters will be compared. If Length is 0, then 0 is returned. If\r
subtracted from the first mismatched Unicode character in FirstString.\r
\r
If Length > 0 and FirstString is NULL, then ASSERT().\r
- If Length > 0 and FirstString is not aligned on a 16-bit bounadary, then ASSERT().\r
+ If Length > 0 and FirstString is not aligned on a 16-bit boundary, then ASSERT().\r
If Length > 0 and SecondString is NULL, then ASSERT().\r
- If Length > 0 and SecondString is not aligned on a 16-bit bounadary, then ASSERT().\r
- If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more\r
- than PcdMaximumUnicodeStringLength Unicode characters not including the\r
- Null-terminator, then ASSERT().\r
- If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more\r
- than PcdMaximumUnicodeStringLength Unicode characters not including the\r
- Null-terminator, then ASSERT().\r
+ If Length > 0 and SecondString is not aligned on a 16-bit boundary, then ASSERT().\r
+ If PcdMaximumUnicodeStringLength is not zero, and Length is greater than\r
+ PcdMaximumUnicodeStringLength, then ASSERT().\r
+ If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more than\r
+ PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
+ then ASSERT().\r
+ If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more than\r
+ PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
+ then ASSERT().\r
\r
- @param FirstString Pointer to a Null-terminated Unicode string.\r
- @param SecondString Pointer to a Null-terminated Unicode string.\r
- @param Length Maximum number of Unicode characters to compare.\r
+ @param FirstString A pointer to a Null-terminated Unicode string.\r
+ @param SecondString A pointer to a Null-terminated Unicode string.\r
+ @param Length The maximum number of Unicode characters to compare.\r
\r
@retval 0 FirstString is identical to SecondString.\r
@return others FirstString is not identical to SecondString.\r
ASSERT (StrSize (FirstString) != 0);\r
ASSERT (StrSize (SecondString) != 0);\r
\r
+ if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r
+ ASSERT (Length <= PcdGet32 (PcdMaximumUnicodeStringLength));\r
+ }\r
+\r
while ((*FirstString != L'\0') &&\r
(*FirstString == *SecondString) &&\r
(Length > 1)) {\r
return *FirstString - *SecondString;\r
}\r
\r
+#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
+\r
/**\r
+ [ATTENTION] This function will be deprecated for security reason.\r
+\r
Concatenates one Null-terminated Unicode string to another Null-terminated\r
Unicode string, and returns the concatenated Unicode string.\r
\r
results are undefined.\r
\r
If Destination is NULL, then ASSERT().\r
- If Destination is not aligned on a 16-bit bounadary, then ASSERT().\r
+ If Destination is not aligned on a 16-bit boundary, then ASSERT().\r
If Source is NULL, then ASSERT().\r
- If Source is not aligned on a 16-bit bounadary, then ASSERT().\r
+ If Source is not aligned on a 16-bit boundary, then ASSERT().\r
If Source and Destination overlap, then ASSERT().\r
If PcdMaximumUnicodeStringLength is not zero, and Destination contains more\r
- than PcdMaximumUnicodeStringLength Unicode characters not including the\r
+ than PcdMaximumUnicodeStringLength Unicode characters, not including the\r
Null-terminator, then ASSERT().\r
If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
- PcdMaximumUnicodeStringLength Unicode characters not including the\r
+ PcdMaximumUnicodeStringLength Unicode characters, not including the\r
Null-terminator, then ASSERT().\r
If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination\r
and Source results in a Unicode string with more than\r
- PcdMaximumUnicodeStringLength Unicode characters not including the\r
+ PcdMaximumUnicodeStringLength Unicode characters, not including the\r
Null-terminator, then ASSERT().\r
\r
- @param Destination Pointer to a Null-terminated Unicode string.\r
- @param Source Pointer to a Null-terminated Unicode string.\r
+ @param Destination A pointer to a Null-terminated Unicode string.\r
+ @param Source A pointer to a Null-terminated Unicode string.\r
\r
@return Destination.\r
\r
}\r
\r
/**\r
+ [ATTENTION] This function will be deprecated for security reason.\r
+\r
Concatenates up to a specified length one Null-terminated Unicode to the end \r
of another Null-terminated Unicode string, and returns the concatenated \r
Unicode string.\r
If Length > 0 and Source is NULL, then ASSERT().\r
If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().\r
If Source and Destination overlap, then ASSERT().\r
+ If PcdMaximumUnicodeStringLength is not zero, and Length is greater than \r
+ PcdMaximumUnicodeStringLength, then ASSERT().\r
If PcdMaximumUnicodeStringLength is not zero, and Destination contains more\r
- than PcdMaximumUnicodeStringLength Unicode characters not including the\r
+ than PcdMaximumUnicodeStringLength Unicode characters, not including the\r
Null-terminator, then ASSERT().\r
If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
- PcdMaximumUnicodeStringLength Unicode characters not including the\r
+ PcdMaximumUnicodeStringLength Unicode characters, not including the\r
Null-terminator, then ASSERT().\r
If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination\r
- and Source results in a Unicode string with more than\r
- PcdMaximumUnicodeStringLength Unicode characters not including the\r
- Null-terminator, then ASSERT().\r
+ and Source results in a Unicode string with more than PcdMaximumUnicodeStringLength\r
+ Unicode characters, not including the Null-terminator, then ASSERT().\r
\r
- @param Destination Pointer to a Null-terminated Unicode string.\r
- @param Source Pointer to a Null-terminated Unicode string.\r
- @param Length Maximum number of Unicode characters to concatenate from\r
+ @param Destination A pointer to a Null-terminated Unicode string.\r
+ @param Source A pointer to a Null-terminated Unicode string.\r
+ @param Length The maximum number of Unicode characters to concatenate from\r
Source.\r
\r
@return Destination.\r
IN UINTN Length\r
)\r
{\r
- StrnCpy (Destination + StrLen (Destination), Source, Length);\r
+ UINTN DestinationLen;\r
+\r
+ DestinationLen = StrLen (Destination);\r
+ StrnCpy (Destination + DestinationLen, Source, Length);\r
+ Destination[DestinationLen + Length] = L'\0';\r
\r
//\r
// Size of the resulting string should never be zero.\r
ASSERT (StrSize (Destination) != 0);\r
return Destination;\r
}\r
+#endif\r
\r
/**\r
Returns the first occurrence of a Null-terminated Unicode sub-string\r
\r
If PcdMaximumUnicodeStringLength is not zero, and SearchString\r
or String contains more than PcdMaximumUnicodeStringLength Unicode\r
- characters not including the Null-terminator, then ASSERT().\r
+ characters, not including the Null-terminator, then ASSERT().\r
\r
- @param String Pointer to a Null-terminated Unicode string.\r
- @param SearchString Pointer to a Null-terminated Unicode string to search for.\r
+ @param String A pointer to a Null-terminated Unicode string.\r
+ @param SearchString A pointer to a Null-terminated Unicode string to search for.\r
\r
@retval NULL If the SearchString does not appear in String.\r
@return others If there is a match.\r
CHAR16 *\r
EFIAPI\r
StrStr (\r
- IN CONST CHAR16 *String,\r
- IN CONST CHAR16 *SearchString\r
+ IN CONST CHAR16 *String,\r
+ IN CONST CHAR16 *SearchString\r
)\r
{\r
CONST CHAR16 *FirstMatch;\r
ASSERT (StrSize (String) != 0);\r
ASSERT (StrSize (SearchString) != 0);\r
\r
- while (*String != '\0') {\r
+ if (*SearchString == L'\0') {\r
+ return (CHAR16 *) String;\r
+ }\r
+\r
+ while (*String != L'\0') {\r
SearchStringTmp = SearchString;\r
FirstMatch = String;\r
\r
while ((*String == *SearchStringTmp) \r
- && (*SearchStringTmp != '\0') \r
- && (*String != '\0')) {\r
+ && (*String != L'\0')) {\r
String++;\r
SearchStringTmp++;\r
} \r
\r
- if (*SearchStringTmp == '\0') {\r
+ if (*SearchStringTmp == L'\0') {\r
return (CHAR16 *) FirstMatch;\r
}\r
\r
- if (SearchStringTmp == SearchString) {\r
- //\r
- // If no character from SearchString match,\r
- // move the pointer to the String under search\r
- // by one character.\r
- //\r
- String++;\r
+ if (*String == L'\0') {\r
+ return NULL;\r
}\r
+\r
+ String = FirstMatch + 1;\r
}\r
\r
return NULL;\r
of the Unicode string specified by String as a decimal number. The format\r
of the input Unicode string String is:\r
\r
- [spaces] [decimal digits].\r
+ [spaces] [decimal digits].\r
\r
The valid decimal digit character is in the range [0-9]. The\r
function will ignore the pad space, which includes spaces or\r
to the range defined by UINTN, then ASSERT().\r
\r
If PcdMaximumUnicodeStringLength is not zero, and String contains\r
- more than PcdMaximumUnicodeStringLength Unicode characters not including\r
+ more than PcdMaximumUnicodeStringLength Unicode characters, not including\r
the Null-terminator, then ASSERT().\r
\r
- @param String Pointer to a Null-terminated Unicode string.\r
+ @param String A pointer to a Null-terminated Unicode string.\r
\r
@retval Value translated from String.\r
\r
UINTN\r
EFIAPI\r
StrDecimalToUintn (\r
- IN CONST CHAR16 *String\r
+ IN CONST CHAR16 *String\r
)\r
{\r
UINTN Result;\r
// If the number represented by String overflows according \r
// to the range defined by UINTN, then ASSERT().\r
//\r
- ASSERT ((Result < QUOTIENT_MAX_UINTN_DIVIDED_BY_10) ||\r
- ((Result == QUOTIENT_MAX_UINTN_DIVIDED_BY_10) &&\r
- (*String - L'0') <= REMAINDER_MAX_UINTN_DIVIDED_BY_10)\r
- );\r
+ ASSERT (Result <= ((((UINTN) ~0) - (*String - L'0')) / 10));\r
\r
Result = Result * 10 + (*String - L'0');\r
String++;\r
of the Unicode string specified by String as a decimal number. The format\r
of the input Unicode string String is:\r
\r
- [spaces] [decimal digits].\r
+ [spaces] [decimal digits].\r
\r
The valid decimal digit character is in the range [0-9]. The\r
function will ignore the pad space, which includes spaces or\r
to the range defined by UINT64, then ASSERT().\r
\r
If PcdMaximumUnicodeStringLength is not zero, and String contains\r
- more than PcdMaximumUnicodeStringLength Unicode characters not including\r
+ more than PcdMaximumUnicodeStringLength Unicode characters, not including\r
the Null-terminator, then ASSERT().\r
\r
- @param String Pointer to a Null-terminated Unicode string.\r
+ @param String A pointer to a Null-terminated Unicode string.\r
\r
@retval Value translated from String.\r
\r
UINT64\r
EFIAPI\r
StrDecimalToUint64 (\r
- IN CONST CHAR16 *String\r
+ IN CONST CHAR16 *String\r
)\r
{\r
UINT64 Result;\r
// If the number represented by String overflows according \r
// to the range defined by UINTN, then ASSERT().\r
//\r
- ASSERT ((Result < QUOTIENT_MAX_UINT64_DIVIDED_BY_10) || \r
- ((Result == QUOTIENT_MAX_UINT64_DIVIDED_BY_10) && \r
- (*String - L'0') <= REMAINDER_MAX_UINT64_DIVIDED_BY_10)\r
- );\r
+ ASSERT (Result <= DivU64x32 (((UINT64) ~0) - (*String - L'0') , 10));\r
\r
Result = MultU64x32 (Result, 10) + (*String - L'0');\r
String++;\r
UINTN, then ASSERT().\r
\r
If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
- PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator,\r
+ PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
then ASSERT().\r
\r
- @param String Pointer to a Null-terminated Unicode string.\r
+ @param String A pointer to a Null-terminated Unicode string.\r
\r
@retval Value translated from String.\r
\r
UINTN\r
EFIAPI\r
StrHexToUintn (\r
- IN CONST CHAR16 *String\r
+ IN CONST CHAR16 *String\r
)\r
{\r
UINTN Result;\r
}\r
\r
if (InternalCharToUpper (*String) == L'X') {\r
- ASSERT (*(String - 1) == L'0');\r
if (*(String - 1) != L'0') {\r
return 0;\r
}\r
// If the Hex Number represented by String overflows according \r
// to the range defined by UINTN, then ASSERT().\r
//\r
- ASSERT ((Result < QUOTIENT_MAX_UINTN_DIVIDED_BY_16) ||\r
- ((Result == QUOTIENT_MAX_UINTN_DIVIDED_BY_16) && \r
- (InternalHexCharToUintn (*String) <= REMAINDER_MAX_UINTN_DIVIDED_BY_16))\r
- );\r
+ ASSERT (Result <= ((((UINTN) ~0) - InternalHexCharToUintn (*String)) >> 4));\r
\r
Result = (Result << 4) + InternalHexCharToUintn (*String);\r
String++;\r
UINT64, then ASSERT().\r
\r
If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
- PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator,\r
+ PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
then ASSERT().\r
\r
- @param String Pointer to a Null-terminated Unicode string.\r
+ @param String A pointer to a Null-terminated Unicode string.\r
\r
@retval Value translated from String.\r
\r
- **/\r
+**/\r
UINT64\r
EFIAPI\r
StrHexToUint64 (\r
- IN CONST CHAR16 *String\r
+ IN CONST CHAR16 *String\r
)\r
{\r
UINT64 Result;\r
// If the Hex Number represented by String overflows according \r
// to the range defined by UINTN, then ASSERT().\r
//\r
- ASSERT ((Result < QUOTIENT_MAX_UINT64_DIVIDED_BY_16)|| \r
- ((Result == QUOTIENT_MAX_UINT64_DIVIDED_BY_16) && \r
- (InternalHexCharToUintn (*String) <= REMAINDER_MAX_UINT64_DIVIDED_BY_16))\r
- );\r
+ ASSERT (Result <= RShiftU64 (((UINT64) ~0) - InternalHexCharToUintn (*String) , 4));\r
\r
Result = LShiftU64 (Result, 4);\r
Result = Result + InternalHexCharToUintn (*String);\r
(Char >= 'a' && Char <= 'f'));\r
}\r
\r
+#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
+\r
/**\r
+ [ATTENTION] This function is deprecated for security reason.\r
+\r
Convert a Null-terminated Unicode string to a Null-terminated\r
ASCII string and returns the ASCII string.\r
\r
to the ASCII string Destination by copying the lower 8 bits of\r
each Unicode character. It returns Destination.\r
\r
+ The caller is responsible to make sure Destination points to a buffer with size\r
+ equal or greater than ((StrLen (Source) + 1) * sizeof (CHAR8)) in bytes.\r
+\r
If any Unicode characters in Source contain non-zero value in\r
the upper 8 bits, then ASSERT().\r
\r
If Source and Destination overlap, then ASSERT().\r
\r
If PcdMaximumUnicodeStringLength is not zero, and Source contains\r
- more than PcdMaximumUnicodeStringLength Unicode characters not including\r
+ more than PcdMaximumUnicodeStringLength Unicode characters, not including\r
the Null-terminator, then ASSERT().\r
\r
If PcdMaximumAsciiStringLength is not zero, and Source contains more\r
- than PcdMaximumAsciiStringLength Unicode characters not including the\r
+ than PcdMaximumAsciiStringLength Unicode characters, not including the\r
Null-terminator, then ASSERT().\r
\r
- @param Source Pointer to a Null-terminated Unicode string.\r
- @param Destination Pointer to a Null-terminated ASCII string.\r
+ @param Source A pointer to a Null-terminated Unicode string.\r
+ @param Destination A pointer to a Null-terminated ASCII string.\r
\r
@return Destination.\r
\r
CHAR8 *\r
EFIAPI\r
UnicodeStrToAsciiStr (\r
- IN CONST CHAR16 *Source,\r
- OUT CHAR8 *Destination\r
+ IN CONST CHAR16 *Source,\r
+ OUT CHAR8 *Destination\r
)\r
{\r
CHAR8 *ReturnValue;\r
//\r
// Source and Destination should not overlap\r
//\r
- ASSERT ((UINTN) ((CHAR16 *) Destination - Source) > StrLen (Source));\r
+ ASSERT ((UINTN) (Destination - (CHAR8 *) Source) >= StrSize (Source));\r
ASSERT ((UINTN) ((CHAR8 *) Source - Destination) > StrLen (Source));\r
\r
\r
return ReturnValue;\r
}\r
\r
-\r
/**\r
+ [ATTENTION] This function will be deprecated for security reason.\r
+\r
Copies one Null-terminated ASCII string to another Null-terminated ASCII\r
string and returns the new ASCII string.\r
\r
If Source is NULL, then ASSERT().\r
If Source and Destination overlap, then ASSERT().\r
If PcdMaximumAsciiStringLength is not zero and Source contains more than\r
- PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+ PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
then ASSERT().\r
\r
- @param Destination Pointer to a Null-terminated ASCII string.\r
- @param Source Pointer to a Null-terminated ASCII string.\r
+ @param Destination A pointer to a Null-terminated ASCII string.\r
+ @param Source A pointer to a Null-terminated ASCII string.\r
\r
@return Destination\r
\r
}\r
\r
/**\r
+ [ATTENTION] This function will be deprecated for security reason.\r
+\r
Copies up to a specified length one Null-terminated ASCII string to another \r
Null-terminated ASCII string and returns the new ASCII string.\r
\r
If Destination is NULL, then ASSERT().\r
If Source is NULL, then ASSERT().\r
If Source and Destination overlap, then ASSERT().\r
+ If PcdMaximumAsciiStringLength is not zero, and Length is greater than \r
+ PcdMaximumAsciiStringLength, then ASSERT().\r
If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
- PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+ PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
then ASSERT().\r
\r
- @param Destination Pointer to a Null-terminated ASCII string.\r
- @param Source Pointer to a Null-terminated ASCII string.\r
- @param Length Maximum number of ASCII characters to copy.\r
+ @param Destination A pointer to a Null-terminated ASCII string.\r
+ @param Source A pointer to a Null-terminated ASCII string.\r
+ @param Length The maximum number of ASCII characters to copy.\r
\r
@return Destination\r
\r
ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));\r
ASSERT ((UINTN)(Source - Destination) >= Length);\r
\r
+ if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
+ ASSERT (Length <= PcdGet32 (PcdMaximumAsciiStringLength));\r
+ }\r
+\r
ReturnValue = Destination;\r
\r
while (*Source != 0 && Length > 0) {\r
ZeroMem (Destination, Length * sizeof (*Destination));\r
return ReturnValue;\r
}\r
+#endif\r
\r
/**\r
Returns the length of a Null-terminated ASCII string.\r
If Length > 0 and Destination is NULL, then ASSERT().\r
If Length > 0 and Source is NULL, then ASSERT().\r
If PcdMaximumAsciiStringLength is not zero and String contains more than\r
- PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+ PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
then ASSERT().\r
\r
- @param String Pointer to a Null-terminated ASCII string.\r
+ @param String A pointer to a Null-terminated ASCII string.\r
\r
@return The length of String.\r
\r
\r
If String is NULL, then ASSERT().\r
If PcdMaximumAsciiStringLength is not zero and String contains more than\r
- PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+ PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
then ASSERT().\r
\r
- @param String Pointer to a Null-terminated ASCII string.\r
+ @param String A pointer to a Null-terminated ASCII string.\r
\r
@return The size of String.\r
\r
If FirstString is NULL, then ASSERT().\r
If SecondString is NULL, then ASSERT().\r
If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r
- PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+ PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
then ASSERT().\r
If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r
- than PcdMaximumAsciiStringLength ASCII characters not including the\r
+ than PcdMaximumAsciiStringLength ASCII characters, not including the\r
Null-terminator, then ASSERT().\r
\r
- @param FirstString Pointer to a Null-terminated ASCII string.\r
- @param SecondString Pointer to a Null-terminated ASCII string.\r
+ @param FirstString A pointer to a Null-terminated ASCII string.\r
+ @param SecondString A pointer to a Null-terminated ASCII string.\r
\r
@retval ==0 FirstString is identical to SecondString.\r
@retval !=0 FirstString is not identical to SecondString.\r
**/\r
CHAR8\r
EFIAPI\r
-AsciiToUpper (\r
+InternalBaseLibAsciiToUpper (\r
IN CHAR8 Chr\r
)\r
{\r
return Char - '0';\r
}\r
\r
- return (UINTN) (10 + AsciiToUpper (Char) - 'A');\r
+ return (UINTN) (10 + InternalBaseLibAsciiToUpper (Char) - 'A');\r
}\r
\r
\r
If FirstString is NULL, then ASSERT().\r
If SecondString is NULL, then ASSERT().\r
If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r
- PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+ PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
then ASSERT().\r
If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r
- than PcdMaximumAsciiStringLength ASCII characters not including the\r
+ than PcdMaximumAsciiStringLength ASCII characters, not including the\r
Null-terminator, then ASSERT().\r
\r
- @param FirstString Pointer to a Null-terminated ASCII string.\r
- @param SecondString Pointer to a Null-terminated ASCII string.\r
+ @param FirstString A pointer to a Null-terminated ASCII string.\r
+ @param SecondString A pointer to a Null-terminated ASCII string.\r
\r
@retval ==0 FirstString is identical to SecondString using case insensitive\r
comparisons.\r
ASSERT (AsciiStrSize (FirstString));\r
ASSERT (AsciiStrSize (SecondString));\r
\r
- UpperFirstString = AsciiToUpper (*FirstString);\r
- UpperSecondString = AsciiToUpper (*SecondString);\r
+ UpperFirstString = InternalBaseLibAsciiToUpper (*FirstString);\r
+ UpperSecondString = InternalBaseLibAsciiToUpper (*SecondString);\r
while ((*FirstString != '\0') && (UpperFirstString == UpperSecondString)) {\r
FirstString++;\r
SecondString++;\r
- UpperFirstString = AsciiToUpper (*FirstString);\r
- UpperSecondString = AsciiToUpper (*SecondString);\r
+ UpperFirstString = InternalBaseLibAsciiToUpper (*FirstString);\r
+ UpperSecondString = InternalBaseLibAsciiToUpper (*SecondString);\r
}\r
\r
return UpperFirstString - UpperSecondString;\r
\r
If Length > 0 and FirstString is NULL, then ASSERT().\r
If Length > 0 and SecondString is NULL, then ASSERT().\r
- If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r
- PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+ If PcdMaximumAsciiStringLength is not zero, and Length is greater than \r
+ PcdMaximumAsciiStringLength, then ASSERT().\r
+ If PcdMaximumAsciiStringLength is not zero, and FirstString contains more than\r
+ PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
then ASSERT().\r
- If PcdMaximumAsciiStringLength is not zero and SecondString contains more than\r
- PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+ If PcdMaximumAsciiStringLength is not zero, and SecondString contains more than\r
+ PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
then ASSERT().\r
\r
- @param FirstString Pointer to a Null-terminated ASCII string.\r
- @param SecondString Pointer to a Null-terminated ASCII string.\r
- @param Length Maximum number of ASCII characters for compare.\r
-\r
+ @param FirstString A pointer to a Null-terminated ASCII string.\r
+ @param SecondString A pointer to a Null-terminated ASCII string.\r
+ @param Length The maximum number of ASCII characters for compare.\r
+ \r
@retval ==0 FirstString is identical to SecondString.\r
@retval !=0 FirstString is not identical to SecondString.\r
\r
ASSERT (AsciiStrSize (FirstString));\r
ASSERT (AsciiStrSize (SecondString));\r
\r
+ if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
+ ASSERT (Length <= PcdGet32 (PcdMaximumAsciiStringLength));\r
+ }\r
+\r
while ((*FirstString != '\0') &&\r
(*FirstString == *SecondString) &&\r
(Length > 1)) {\r
return *FirstString - *SecondString;\r
}\r
\r
+#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
+\r
/**\r
+ [ATTENTION] This function will be deprecated for security reason.\r
+\r
Concatenates one Null-terminated ASCII string to another Null-terminated\r
ASCII string, and returns the concatenated ASCII string.\r
\r
If Destination is NULL, then ASSERT().\r
If Source is NULL, then ASSERT().\r
If PcdMaximumAsciiStringLength is not zero and Destination contains more than\r
- PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+ PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
then ASSERT().\r
If PcdMaximumAsciiStringLength is not zero and Source contains more than\r
- PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+ PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
then ASSERT().\r
If PcdMaximumAsciiStringLength is not zero and concatenating Destination and\r
Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r
ASCII characters, then ASSERT().\r
\r
- @param Destination Pointer to a Null-terminated ASCII string.\r
- @param Source Pointer to a Null-terminated ASCII string.\r
+ @param Destination A pointer to a Null-terminated ASCII string.\r
+ @param Source A pointer to a Null-terminated ASCII string.\r
\r
@return Destination\r
\r
}\r
\r
/**\r
+ [ATTENTION] This function will be deprecated for security reason.\r
+\r
Concatenates up to a specified length one Null-terminated ASCII string to \r
the end of another Null-terminated ASCII string, and returns the \r
concatenated ASCII string.\r
If Length > 0 and Destination is NULL, then ASSERT().\r
If Length > 0 and Source is NULL, then ASSERT().\r
If Source and Destination overlap, then ASSERT().\r
+ If PcdMaximumAsciiStringLength is not zero, and Length is greater than\r
+ PcdMaximumAsciiStringLength, then ASSERT().\r
If PcdMaximumAsciiStringLength is not zero, and Destination contains more than\r
- PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+ PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
then ASSERT().\r
If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
- PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+ PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
then ASSERT().\r
If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and\r
Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r
- ASCII characters not including the Null-terminator, then ASSERT().\r
+ ASCII characters, not including the Null-terminator, then ASSERT().\r
\r
- @param Destination Pointer to a Null-terminated ASCII string.\r
- @param Source Pointer to a Null-terminated ASCII string.\r
- @param Length Maximum number of ASCII characters to concatenate from\r
+ @param Destination A pointer to a Null-terminated ASCII string.\r
+ @param Source A pointer to a Null-terminated ASCII string.\r
+ @param Length The maximum number of ASCII characters to concatenate from\r
Source.\r
\r
@return Destination\r
IN UINTN Length\r
)\r
{\r
- AsciiStrnCpy (Destination + AsciiStrLen (Destination), Source, Length);\r
+ UINTN DestinationLen;\r
+\r
+ DestinationLen = AsciiStrLen (Destination);\r
+ AsciiStrnCpy (Destination + DestinationLen, Source, Length);\r
+ Destination[DestinationLen + Length] = '\0';\r
\r
//\r
// Size of the resulting string should never be zero.\r
ASSERT (AsciiStrSize (Destination) != 0);\r
return Destination;\r
}\r
+#endif\r
\r
/**\r
Returns the first occurrence of a Null-terminated ASCII sub-string\r
String contains more than PcdMaximumAsciiStringLength Unicode characters\r
not including the Null-terminator, then ASSERT().\r
\r
- @param String Pointer to a Null-terminated ASCII string.\r
- @param SearchString Pointer to a Null-terminated ASCII string to search for.\r
+ @param String A pointer to a Null-terminated ASCII string.\r
+ @param SearchString A pointer to a Null-terminated ASCII string to search for.\r
\r
@retval NULL If the SearchString does not appear in String.\r
@retval others If there is a match return the first occurrence of SearchingString.\r
CHAR8 *\r
EFIAPI\r
AsciiStrStr (\r
- IN CONST CHAR8 *String,\r
- IN CONST CHAR8 *SearchString\r
+ IN CONST CHAR8 *String,\r
+ IN CONST CHAR8 *SearchString\r
)\r
{\r
CONST CHAR8 *FirstMatch;\r
ASSERT (AsciiStrSize (String) != 0);\r
ASSERT (AsciiStrSize (SearchString) != 0);\r
\r
+ if (*SearchString == '\0') {\r
+ return (CHAR8 *) String;\r
+ }\r
+\r
while (*String != '\0') {\r
SearchStringTmp = SearchString;\r
FirstMatch = String;\r
\r
while ((*String == *SearchStringTmp) \r
- && (*SearchStringTmp != '\0') \r
&& (*String != '\0')) {\r
String++;\r
SearchStringTmp++;\r
return (CHAR8 *) FirstMatch;\r
}\r
\r
- if (SearchStringTmp == SearchString) {\r
- //\r
- // If no character from SearchString match,\r
- // move the pointer to the String under search\r
- // by one character.\r
- //\r
- String++;\r
+ if (*String == '\0') {\r
+ return NULL;\r
}\r
\r
+ String = FirstMatch + 1;\r
}\r
\r
return NULL;\r
PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
then ASSERT().\r
\r
- @param String Pointer to a Null-terminated ASCII string.\r
+ @param String A pointer to a Null-terminated ASCII string.\r
\r
@retval Value translated from String.\r
\r
// If the number represented by String overflows according \r
// to the range defined by UINTN, then ASSERT().\r
//\r
- ASSERT ((Result < QUOTIENT_MAX_UINTN_DIVIDED_BY_10) ||\r
- ((Result == QUOTIENT_MAX_UINTN_DIVIDED_BY_10) && \r
- (*String - '0') <= REMAINDER_MAX_UINTN_DIVIDED_BY_10)\r
- );\r
+ ASSERT (Result <= ((((UINTN) ~0) - (*String - L'0')) / 10));\r
\r
Result = Result * 10 + (*String - '0');\r
String++;\r
PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
then ASSERT().\r
\r
- @param String Pointer to a Null-terminated ASCII string.\r
+ @param String A pointer to a Null-terminated ASCII string.\r
\r
@retval Value translated from String.\r
\r
UINT64\r
EFIAPI\r
AsciiStrDecimalToUint64 (\r
- IN CONST CHAR8 *String\r
+ IN CONST CHAR8 *String\r
)\r
{\r
UINT64 Result;\r
// If the number represented by String overflows according \r
// to the range defined by UINTN, then ASSERT().\r
//\r
- ASSERT ((Result < QUOTIENT_MAX_UINT64_DIVIDED_BY_10) || \r
- ((Result == QUOTIENT_MAX_UINT64_DIVIDED_BY_10) && \r
- (*String - '0') <= REMAINDER_MAX_UINT64_DIVIDED_BY_10)\r
- );\r
+ ASSERT (Result <= DivU64x32 (((UINT64) ~0) - (*String - L'0') , 10));\r
\r
Result = MultU64x32 (Result, 10) + (*String - '0');\r
String++;\r
and String contains more than PcdMaximumAsciiStringLength ASCII characters not including\r
the Null-terminator, then ASSERT().\r
\r
- @param String Pointer to a Null-terminated ASCII string.\r
+ @param String A pointer to a Null-terminated ASCII string.\r
\r
@retval Value translated from String.\r
\r
UINTN\r
EFIAPI\r
AsciiStrHexToUintn (\r
- IN CONST CHAR8 *String\r
+ IN CONST CHAR8 *String\r
)\r
{\r
UINTN Result;\r
String++;\r
}\r
\r
- if (AsciiToUpper (*String) == 'X') {\r
+ if (InternalBaseLibAsciiToUpper (*String) == 'X') {\r
ASSERT (*(String - 1) == '0');\r
if (*(String - 1) != '0') {\r
return 0;\r
// If the Hex Number represented by String overflows according \r
// to the range defined by UINTN, then ASSERT().\r
//\r
- ASSERT ((Result < QUOTIENT_MAX_UINTN_DIVIDED_BY_16) ||\r
- ((Result == QUOTIENT_MAX_UINTN_DIVIDED_BY_16) && \r
- (InternalAsciiHexCharToUintn (*String) <= REMAINDER_MAX_UINTN_DIVIDED_BY_16))\r
- );\r
+ ASSERT (Result <= ((((UINTN) ~0) - InternalHexCharToUintn (*String)) >> 4));\r
\r
Result = (Result << 4) + InternalAsciiHexCharToUintn (*String);\r
String++;\r
and String contains more than PcdMaximumAsciiStringLength ASCII characters not including\r
the Null-terminator, then ASSERT().\r
\r
- @param String Pointer to a Null-terminated ASCII string.\r
+ @param String A pointer to a Null-terminated ASCII string.\r
\r
@retval Value translated from String.\r
\r
UINT64\r
EFIAPI\r
AsciiStrHexToUint64 (\r
- IN CONST CHAR8 *String\r
+ IN CONST CHAR8 *String\r
)\r
{\r
UINT64 Result;\r
String++;\r
}\r
\r
- if (AsciiToUpper (*String) == 'X') {\r
+ if (InternalBaseLibAsciiToUpper (*String) == 'X') {\r
ASSERT (*(String - 1) == '0');\r
if (*(String - 1) != '0') {\r
return 0;\r
// If the Hex Number represented by String overflows according \r
// to the range defined by UINTN, then ASSERT().\r
//\r
- ASSERT ((Result < QUOTIENT_MAX_UINT64_DIVIDED_BY_16) ||\r
- ((Result == QUOTIENT_MAX_UINT64_DIVIDED_BY_16) && \r
- (InternalAsciiHexCharToUintn (*String) <= REMAINDER_MAX_UINT64_DIVIDED_BY_16))\r
- );\r
+ ASSERT (Result <= RShiftU64 (((UINT64) ~0) - InternalHexCharToUintn (*String) , 4));\r
\r
Result = LShiftU64 (Result, 4);\r
Result = Result + InternalAsciiHexCharToUintn (*String);\r
return Result;\r
}\r
\r
+#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
\r
/**\r
+ [ATTENTION] This function is deprecated for security reason.\r
+\r
Convert one Null-terminated ASCII string to a Null-terminated\r
Unicode string and returns the Unicode string.\r
\r
PcdMaximumUnicodeStringLength ASCII characters not including the\r
Null-terminator, then ASSERT().\r
\r
- @param Source Pointer to a Null-terminated ASCII string.\r
- @param Destination Pointer to a Null-terminated Unicode string.\r
+ @param Source A pointer to a Null-terminated ASCII string.\r
+ @param Destination A pointer to a Null-terminated Unicode string.\r
\r
@return Destination.\r
\r
// Source and Destination should not overlap\r
//\r
ASSERT ((UINTN) ((CHAR8 *) Destination - Source) > AsciiStrLen (Source));\r
- ASSERT ((UINTN) (Source - (CHAR8 *) Destination) > (AsciiStrLen (Source) * sizeof (CHAR16)));\r
+ ASSERT ((UINTN) (Source - (CHAR8 *) Destination) >= (AsciiStrSize (Source) * sizeof (CHAR16)));\r
+\r
\r
- \r
ReturnValue = Destination;\r
while (*Source != '\0') {\r
*(Destination++) = (CHAR16) *(Source++);\r
return ReturnValue;\r
}\r
\r
+#endif\r
+\r
/**\r
Converts an 8-bit value to an 8-bit BCD value.\r
\r
ASSERT ((Value & 0xf) < 0xa);\r
return (UINT8) ((Value >> 4) * 10 + (Value & 0xf));\r
}\r
-\r
-\r
-/**\r
- Convert a nibble in the low 4 bits of a byte to a Unicode hexadecimal character.\r
-\r
- This function converts a nibble in the low 4 bits of a byte to a Unicode hexadecimal \r
- character For example, the nibble 0x01 and 0x0A will converted to L'1' and L'A' \r
- respectively.\r
-\r
- The upper nibble in the input byte will be masked off.\r
-\r
- @param Nibble The nibble which is in the low 4 bits of the input byte.\r
-\r
- @retval CHAR16 The Unicode hexadecimal character.\r
-\r
-**/\r
-CHAR16\r
-EFIAPI\r
-NibbleToHexChar (\r
- IN UINT8 Nibble\r
- )\r
-{\r
- Nibble &= 0x0F;\r
- if (Nibble <= 0x9) {\r
- return (CHAR16)(Nibble + L'0');\r
- }\r
-\r
- return (CHAR16)(Nibble - 0xA + L'A');\r
-}\r
-\r
-/** \r
- Convert binary buffer to a Unicode String in a specified sequence. \r
-\r
- This function converts bytes in the memory block pointed by Buffer to a Unicode String Str. \r
- Each byte will be represented by two Unicode characters. For example, byte 0xA1 will \r
- be converted into two Unicode character L'A' and L'1'. In the output String, the Unicode Character \r
- for the Most Significant Nibble will be put before the Unicode Character for the Least Significant\r
- Nibble. The output string for the buffer containing a single byte 0xA1 will be L"A1". \r
- For a buffer with multiple bytes, the Unicode character produced by the first byte will be put into the \r
- the last character in the output string. The one next to first byte will be put into the\r
- character before the last character. This rules applies to the rest of the bytes. The Unicode\r
- character by the last byte will be put into the first character in the output string. For example,\r
- the input buffer for a 64-bits unsigned integer 0x12345678abcdef1234 will be converted to\r
- a Unicode string equal to L"12345678abcdef1234".\r
-\r
- @param String On input, String is pointed to the buffer allocated for the convertion.\r
- @param StringLen The Length of String buffer to hold the output String. The length must include the tailing '\0' character.\r
- The StringLen required to convert a N bytes Buffer will be a least equal to or greater \r
- than 2*N + 1.\r
- @param Buffer The pointer to a input buffer.\r
- @param BufferSizeInBytes Length in bytes of the input buffer.\r
-\r
-\r
- @retval EFI_SUCCESS The convertion is successful. All bytes in Buffer has been convert to the corresponding\r
- Unicode character and placed into the right place in String.\r
- @retval EFI_BUFFER_TOO_SMALL StringSizeInBytes is smaller than 2 * N + 1the number of bytes required to\r
- complete the convertion. \r
-**/\r
-RETURN_STATUS\r
-EFIAPI\r
-BufToHexString (\r
- IN OUT CHAR16 *String,\r
- IN OUT UINTN *StringLen,\r
- IN CONST UINT8 *Buffer,\r
- IN UINTN BufferSizeInBytes\r
- )\r
-{\r
- UINTN Idx;\r
- UINT8 Byte;\r
- UINTN StrLen;\r
-\r
- //\r
- // Make sure string is either passed or allocate enough.\r
- // It takes 2 Unicode characters (4 bytes) to represent 1 byte of the binary buffer.\r
- // Plus the Unicode termination character.\r
- //\r
- StrLen = BufferSizeInBytes * 2;\r
- if (StrLen > ((*StringLen) - 1)) {\r
- *StringLen = StrLen + 1;\r
- return RETURN_BUFFER_TOO_SMALL;\r
- }\r
-\r
- *StringLen = StrLen + 1;\r
- //\r
- // Ends the string.\r
- //\r
- String[StrLen] = L'\0'; \r
-\r
- for (Idx = 0; Idx < BufferSizeInBytes; Idx++) {\r
-\r
- Byte = Buffer[Idx];\r
- String[StrLen - 1 - Idx * 2] = NibbleToHexChar (Byte);\r
- String[StrLen - 2 - Idx * 2] = NibbleToHexChar ((UINT8)(Byte >> 4));\r
- }\r
-\r
- return RETURN_SUCCESS;\r
-}\r
-\r
-\r
-/**\r
- Convert a Unicode string consisting of hexadecimal characters to a output byte buffer.\r
-\r
- This function converts a Unicode string consisting of characters in the range of Hexadecimal\r
- character (L'0' to L'9', L'A' to L'F' and L'a' to L'f') to a output byte buffer. The function will stop\r
- at the first non-hexadecimal character or the NULL character. The convertion process can be\r
- simply viewed as the reverse operations defined by BufToHexString. Two Unicode characters will be \r
- converted into one byte. The first Unicode character represents the Most Significant Nibble and the\r
- second Unicode character represents the Least Significant Nibble in the output byte. \r
- The first pair of Unicode characters represents the last byte in the output buffer. The second pair of Unicode \r
- characters represent the the byte preceding the last byte. This rule applies to the rest pairs of bytes. \r
- The last pair represent the first byte in the output buffer. \r
-\r
- For example, a Unciode String L"12345678" will be converted into a buffer wil the following bytes \r
- (first byte is the byte in the lowest memory address): "0x78, 0x56, 0x34, 0x12".\r
-\r
- If String has N valid hexadecimal characters for conversion, the caller must make sure Buffer is at least \r
- N/2 (if N is even) or (N+1)/2 (if N if odd) bytes. \r
-\r
- @param Buffer The output buffer allocated by the caller.\r
- @param BufferSizeInBytes On input, the size in bytes of Buffer. On output, it is updated to \r
- contain the size of the Buffer which is actually used for the converstion.\r
- For Unicode string with 2*N hexadecimal characters (not including the \r
- tailing NULL character), N bytes of Buffer will be used for the output.\r
- @param String The input hexadecimal string.\r
- @param ConvertedStrLen The number of hexadecimal characters used to produce content in output\r
- buffer Buffer.\r
-\r
- @retval RETURN_BUFFER_TOO_SMALL The input BufferSizeInBytes is too small to hold the output. BufferSizeInBytes\r
- will be updated to the size required for the converstion.\r
- @retval RETURN_SUCCESS The convertion is successful or the first Unicode character from String\r
- is hexadecimal. If ConvertedStrLen is not NULL, it is updated\r
- to the number of hexadecimal character used for the converstion.\r
-**/\r
-RETURN_STATUS\r
-EFIAPI\r
-HexStringToBuf (\r
- OUT UINT8 *Buffer, \r
- IN OUT UINTN *BufferSizeInBytes,\r
- IN CONST CHAR16 *String,\r
- OUT UINTN *ConvertedStrLen OPTIONAL\r
- )\r
-{\r
- UINTN HexCnt;\r
- UINTN Idx;\r
- UINTN BufferLength;\r
- UINT8 Digit;\r
- UINT8 Byte;\r
-\r
- //\r
- // Find out how many hex characters the string has.\r
- //\r
- for (Idx = 0, HexCnt = 0; IsHexDigit (&Digit, String[Idx]); Idx++, HexCnt++);\r
-\r
- if (HexCnt == 0) {\r
- *ConvertedStrLen = 0;\r
- return RETURN_SUCCESS;\r
- }\r
- //\r
- // Two Unicode characters make up 1 buffer byte. Round up.\r
- //\r
- BufferLength = (HexCnt + 1) / 2; \r
-\r
- //\r
- // Test if buffer is passed enough.\r
- //\r
- if (BufferLength > (*BufferSizeInBytes)) {\r
- *BufferSizeInBytes = BufferLength;\r
- return RETURN_BUFFER_TOO_SMALL;\r
- }\r
-\r
- *BufferSizeInBytes = BufferLength;\r
-\r
- for (Idx = 0; Idx < HexCnt; Idx++) {\r
-\r
- IsHexDigit (&Digit, String[HexCnt - 1 - Idx]);\r
-\r
- //\r
- // For odd charaters, write the lower nibble for each buffer byte,\r
- // and for even characters, the upper nibble.\r
- //\r
- if ((Idx & 1) == 0) {\r
- Byte = Digit;\r
- } else {\r
- Byte = Buffer[Idx / 2];\r
- Byte &= 0x0F;\r
- Byte = (UINT8) (Byte | Digit << 4);\r
- }\r
-\r
- Buffer[Idx / 2] = Byte;\r
- }\r
-\r
- if (ConvertedStrLen != NULL) {\r
- *ConvertedStrLen = HexCnt;\r
- }\r
-\r
- return RETURN_SUCCESS;\r
-}\r
-\r
-\r
-/**\r
- Test if a Unicode character is a hexadecimal digit. If true, the input\r
- Unicode character is converted to a byte. \r
-\r
- This function tests if a Unicode character is a hexadecimal digit. If true, the input\r
- Unicode character is converted to a byte. For example, Unicode character\r
- L'A' will be converted to 0x0A. \r
-\r
- If Digit is NULL, then ASSERT.\r
-\r
- @param Digit The output hexadecimal digit.\r
-\r
- @param Char The input Unicode character.\r
-\r
- @retval TRUE Char is in the range of Hexadecimal number. Digit is updated\r
- to the byte value of the number.\r
- @retval FALSE Char is not in the range of Hexadecimal number. Digit is keep\r
- intact.\r
-\r
-**/\r
-BOOLEAN\r
-EFIAPI\r
-IsHexDigit (\r
- OUT UINT8 *Digit,\r
- IN CHAR16 Char\r
- )\r
-{\r
- ASSERT (Digit != NULL);\r
- \r
- if ((Char >= L'0') && (Char <= L'9')) {\r
- *Digit = (UINT8) (Char - L'0');\r
- return TRUE;\r
- }\r
-\r
- if ((Char >= L'A') && (Char <= L'F')) {\r
- *Digit = (UINT8) (Char - L'A' + 0x0A);\r
- return TRUE;\r
- }\r
-\r
- if ((Char >= L'a') && (Char <= L'f')) {\r
- *Digit = (UINT8) (Char - L'a' + 0x0A);\r
- return TRUE;\r
- }\r
-\r
- return FALSE;\r
-}\r
-\r
-\r