From ac7e320cb8326befd240020db709151884c889bc Mon Sep 17 00:00:00 2001 From: lgao4 Date: Mon, 1 Dec 2008 02:32:12 +0000 Subject: [PATCH] Remove NibbleToHexChar() function from BaseLib Move IsHexDigit, BufToHexString, HexStringToBuf from BaseLib to MdeModulePkg IfrSupportLib. The reason is: 1) IsHexDigit function provides the logic to check Hex Digit and convert it to Decimal value, which is required by IScsi LUN and HII user input. But this logic is not provided by any functions in MdeLib. So, it can't be deleted. It is moved to IfrSupportLib. 2) BufToHexString function converts a array of buffers to hex string. If the buffer length is less than sizeof (UINT64), it can be directly replaced by UnicodeValueToString(). But HII modules may use BufToHexString to convert the buffers whose length > sizeof (UINT64). For example: .\MdeModulePkg\Universal\HiiDatabaseDxe\ConfigRouting.c line 201, 1148 .\Universal\SetupBrowserDxe\Setup.c line line 1457, 1503 Like this case, it is not easy to use UnicodeValueToString to replace BufToHexString. So, BufToHexString is still kept. Because such usages are in HII modules, this function is moved to IfrSupportLib. 3) HexStringToBuf is moved to IfrSupportLib. The reason is similar to BufToHexString. git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@6782 6f19259b-4bc3-4df7-8a09-765794883524 --- MdeModulePkg/Include/Library/IfrSupportLib.h | 112 ++++++++ MdeModulePkg/Library/DxeNetLib/DxeNetLib.c | 6 +- .../Library/UefiIfrSupportLib/UefiIfrForm.c | 215 +++++++++++++++ .../DevicePathDxe/DevicePathFromText.c | 78 +++--- .../Universal/Network/IScsiDxe/IScsiMisc.c | 12 +- .../Universal/SetupBrowserDxe/Expression.c | 9 +- MdePkg/Include/Library/BaseLib.h | 129 --------- MdePkg/Library/BaseLib/String.c | 247 ------------------ 8 files changed, 379 insertions(+), 429 deletions(-) diff --git a/MdeModulePkg/Include/Library/IfrSupportLib.h b/MdeModulePkg/Include/Library/IfrSupportLib.h index 73ddcaab78..2caa6f0dca 100644 --- a/MdeModulePkg/Include/Library/IfrSupportLib.h +++ b/MdeModulePkg/Include/Library/IfrSupportLib.h @@ -764,4 +764,116 @@ IfrLibCreatePopUp2 ( ) ; +/** + Test if a Unicode character is a hexadecimal digit. If true, the input + Unicode character is converted to a byte. + + This function tests if a Unicode character is a hexadecimal digit. If true, the input + Unicode character is converted to a byte. For example, Unicode character + L'A' will be converted to 0x0A. + + If Digit is NULL, then ASSERT. + + @param Digit The output hexadecimal digit. + + @param Char The input Unicode character. + + @retval TRUE Char is in the range of Hexadecimal number. Digit is updated + to the byte value of the number. + @retval FALSE Char is not in the range of Hexadecimal number. Digit is keep + intact. + +**/ +BOOLEAN +EFIAPI +IsHexDigit ( + OUT UINT8 *Digit, + IN CHAR16 Char + ) +; + + +/** + Convert binary buffer to a Unicode String in a specified sequence. + + This function converts bytes in the memory block pointed by Buffer to a Unicode String Str. + Each byte will be represented by two Unicode characters. For example, byte 0xA1 will + be converted into two Unicode character L'A' and L'1'. In the output String, the Unicode Character + for the Most Significant Nibble will be put before the Unicode Character for the Least Significant + Nibble. The output string for the buffer containing a single byte 0xA1 will be L"A1". + For a buffer with multiple bytes, the Unicode character produced by the first byte will be put into the + the last character in the output string. The one next to first byte will be put into the + character before the last character. This rules applies to the rest of the bytes. The Unicode + character by the last byte will be put into the first character in the output string. For example, + the input buffer for a 64-bits unsigned integer 0x12345678abcdef1234 will be converted to + a Unicode string equal to L"12345678abcdef1234". + + @param String On input, String is pointed to the buffer allocated for the convertion. + @param StringLen The Length of String buffer to hold the output String. The length must include the tailing '\0' character. + The StringLen required to convert a N bytes Buffer will be a least equal to or greater + than 2*N + 1. + @param Buffer The pointer to a input buffer. + @param BufferSizeInBytes Length in bytes of the input buffer. + + + @retval EFI_SUCCESS The convertion is successful. All bytes in Buffer has been convert to the corresponding + Unicode character and placed into the right place in String. + @retval EFI_BUFFER_TOO_SMALL StringSizeInBytes is smaller than 2 * N + 1the number of bytes required to + complete the convertion. +**/ +RETURN_STATUS +EFIAPI +BufToHexString ( + IN OUT CHAR16 *String, + IN OUT UINTN *StringLen, + IN CONST UINT8 *Buffer, + IN UINTN BufferSizeInBytes + ) +; + + +/** + Convert a Unicode string consisting of hexadecimal characters to a output byte buffer. + + This function converts a Unicode string consisting of characters in the range of Hexadecimal + 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 + at the first non-hexadecimal character or the NULL character. The convertion process can be + simply viewed as the reverse operations defined by BufToHexString. Two Unicode characters will be + converted into one byte. The first Unicode character represents the Most Significant Nibble and the + second Unicode character represents the Least Significant Nibble in the output byte. + The first pair of Unicode characters represents the last byte in the output buffer. The second pair of Unicode + characters represent the the byte preceding the last byte. This rule applies to the rest pairs of bytes. + The last pair represent the first byte in the output buffer. + + For example, a Unciode String L"12345678" will be converted into a buffer wil the following bytes + (first byte is the byte in the lowest memory address): "0x78, 0x56, 0x34, 0x12". + + If String has N valid hexadecimal characters for conversion, the caller must make sure Buffer is at least + N/2 (if N is even) or (N+1)/2 (if N if odd) bytes. + + @param Buffer The output buffer allocated by the caller. + @param BufferSizeInBytes On input, the size in bytes of Buffer. On output, it is updated to + contain the size of the Buffer which is actually used for the converstion. + For Unicode string with 2*N hexadecimal characters (not including the + tailing NULL character), N bytes of Buffer will be used for the output. + @param String The input hexadecimal string. + @param ConvertedStrLen The number of hexadecimal characters used to produce content in output + buffer Buffer. + + @retval RETURN_BUFFER_TOO_SMALL The input BufferSizeInBytes is too small to hold the output. BufferSizeInBytes + will be updated to the size required for the converstion. + @retval RETURN_SUCCESS The convertion is successful or the first Unicode character from String + is hexadecimal. If ConvertedStrLen is not NULL, it is updated + to the number of hexadecimal character used for the converstion. +**/ +RETURN_STATUS +EFIAPI +HexStringToBuf ( + OUT UINT8 *Buffer, + IN OUT UINTN *BufferSizeInBytes, + IN CONST CHAR16 *String, + OUT UINTN *ConvertedStrLen OPTIONAL + ) +; + #endif diff --git a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c index 4eeab90e05..1bf7e1f198 100644 --- a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c +++ b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c @@ -42,6 +42,8 @@ Abstract: EFI_DPC_PROTOCOL *mDpc = NULL; +GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mNetLibHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; + // // All the supported IP4 maskes in host byte order. // @@ -1094,8 +1096,8 @@ NetLibGetMacString ( // Convert the mac address into a unicode string. // for (Index = 0; Index < Mode->HwAddressSize; Index++) { - MacAddress[Index * 2] = NibbleToHexChar ((UINT8) (Mode->CurrentAddress.Addr[Index] >> 4)); - MacAddress[Index * 2 + 1] = NibbleToHexChar (Mode->CurrentAddress.Addr[Index]); + MacAddress[Index * 2] = (CHAR16) mNetLibHexStr[(Mode->CurrentAddress.Addr[Index] >> 4) & 0x0F]; + MacAddress[Index * 2 + 1] = (CHAR16) mNetLibHexStr[Mode->CurrentAddress.Addr[Index] & 0x0F]; } MacAddress[Mode->HwAddressSize * 2] = L'\0'; diff --git a/MdeModulePkg/Library/UefiIfrSupportLib/UefiIfrForm.c b/MdeModulePkg/Library/UefiIfrSupportLib/UefiIfrForm.c index 878898ffea..15bd2a1950 100644 --- a/MdeModulePkg/Library/UefiIfrSupportLib/UefiIfrForm.c +++ b/MdeModulePkg/Library/UefiIfrSupportLib/UefiIfrForm.c @@ -17,6 +17,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. CONST EFI_FORM_BROWSER2_PROTOCOL *mFormBrowser2 = NULL; CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *mIfrSupportLibHiiConfigRouting = NULL; +GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mIfrSupportLibHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; /** This function locate FormBrowser2 protocols for later usage. @@ -1391,3 +1392,217 @@ SetBrowserData ( FreePool (ConfigRequest); return Status; } + +/** + Test if a Unicode character is a hexadecimal digit. If true, the input + Unicode character is converted to a byte. + + This function tests if a Unicode character is a hexadecimal digit. If true, the input + Unicode character is converted to a byte. For example, Unicode character + L'A' will be converted to 0x0A. + + If Digit is NULL, then ASSERT. + + @param Digit The output hexadecimal digit. + + @param Char The input Unicode character. + + @retval TRUE Char is in the range of Hexadecimal number. Digit is updated + to the byte value of the number. + @retval FALSE Char is not in the range of Hexadecimal number. Digit is keep + intact. + +**/ +BOOLEAN +EFIAPI +IsHexDigit ( + OUT UINT8 *Digit, + IN CHAR16 Char + ) +{ + ASSERT (Digit != NULL); + + if ((Char >= L'0') && (Char <= L'9')) { + *Digit = (UINT8) (Char - L'0'); + return TRUE; + } + + if ((Char >= L'A') && (Char <= L'F')) { + *Digit = (UINT8) (Char - L'A' + 0x0A); + return TRUE; + } + + if ((Char >= L'a') && (Char <= L'f')) { + *Digit = (UINT8) (Char - L'a' + 0x0A); + return TRUE; + } + + return FALSE; +} + +/** + Convert binary buffer to a Unicode String in a specified sequence. + + This function converts bytes in the memory block pointed by Buffer to a Unicode String Str. + Each byte will be represented by two Unicode characters. For example, byte 0xA1 will + be converted into two Unicode character L'A' and L'1'. In the output String, the Unicode Character + for the Most Significant Nibble will be put before the Unicode Character for the Least Significant + Nibble. The output string for the buffer containing a single byte 0xA1 will be L"A1". + For a buffer with multiple bytes, the Unicode character produced by the first byte will be put into the + the last character in the output string. The one next to first byte will be put into the + character before the last character. This rules applies to the rest of the bytes. The Unicode + character by the last byte will be put into the first character in the output string. For example, + the input buffer for a 64-bits unsigned integer 0x12345678abcdef1234 will be converted to + a Unicode string equal to L"12345678abcdef1234". + + @param String On input, String is pointed to the buffer allocated for the convertion. + @param StringLen The Length of String buffer to hold the output String. The length must include the tailing '\0' character. + The StringLen required to convert a N bytes Buffer will be a least equal to or greater + than 2*N + 1. + @param Buffer The pointer to a input buffer. + @param BufferSizeInBytes Length in bytes of the input buffer. + + + @retval EFI_SUCCESS The convertion is successful. All bytes in Buffer has been convert to the corresponding + Unicode character and placed into the right place in String. + @retval EFI_BUFFER_TOO_SMALL StringSizeInBytes is smaller than 2 * N + 1the number of bytes required to + complete the convertion. +**/ +RETURN_STATUS +EFIAPI +BufToHexString ( + IN OUT CHAR16 *String, + IN OUT UINTN *StringLen, + IN CONST UINT8 *Buffer, + IN UINTN BufferSizeInBytes + ) +{ + UINTN Idx; + UINT8 Byte; + UINTN StrLen; + + // + // Make sure string is either passed or allocate enough. + // It takes 2 Unicode characters (4 bytes) to represent 1 byte of the binary buffer. + // Plus the Unicode termination character. + // + StrLen = BufferSizeInBytes * 2; + if (StrLen > ((*StringLen) - 1)) { + *StringLen = StrLen + 1; + return RETURN_BUFFER_TOO_SMALL; + } + + *StringLen = StrLen + 1; + // + // Ends the string. + // + String[StrLen] = L'\0'; + + for (Idx = 0; Idx < BufferSizeInBytes; Idx++) { + Byte = Buffer[Idx]; + String[StrLen - 1 - Idx * 2] = mIfrSupportLibHexStr [Byte & 0xF]; + String[StrLen - 2 - Idx * 2] = mIfrSupportLibHexStr [Byte >> 4]; + } + + return RETURN_SUCCESS; +} + + +/** + Convert a Unicode string consisting of hexadecimal characters to a output byte buffer. + + This function converts a Unicode string consisting of characters in the range of Hexadecimal + 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 + at the first non-hexadecimal character or the NULL character. The convertion process can be + simply viewed as the reverse operations defined by BufToHexString. Two Unicode characters will be + converted into one byte. The first Unicode character represents the Most Significant Nibble and the + second Unicode character represents the Least Significant Nibble in the output byte. + The first pair of Unicode characters represents the last byte in the output buffer. The second pair of Unicode + characters represent the the byte preceding the last byte. This rule applies to the rest pairs of bytes. + The last pair represent the first byte in the output buffer. + + For example, a Unciode String L"12345678" will be converted into a buffer wil the following bytes + (first byte is the byte in the lowest memory address): "0x78, 0x56, 0x34, 0x12". + + If String has N valid hexadecimal characters for conversion, the caller must make sure Buffer is at least + N/2 (if N is even) or (N+1)/2 (if N if odd) bytes. + + @param Buffer The output buffer allocated by the caller. + @param BufferSizeInBytes On input, the size in bytes of Buffer. On output, it is updated to + contain the size of the Buffer which is actually used for the converstion. + For Unicode string with 2*N hexadecimal characters (not including the + tailing NULL character), N bytes of Buffer will be used for the output. + @param String The input hexadecimal string. + @param ConvertedStrLen The number of hexadecimal characters used to produce content in output + buffer Buffer. + + @retval RETURN_BUFFER_TOO_SMALL The input BufferSizeInBytes is too small to hold the output. BufferSizeInBytes + will be updated to the size required for the converstion. + @retval RETURN_SUCCESS The convertion is successful or the first Unicode character from String + is hexadecimal. If ConvertedStrLen is not NULL, it is updated + to the number of hexadecimal character used for the converstion. +**/ +RETURN_STATUS +EFIAPI +HexStringToBuf ( + OUT UINT8 *Buffer, + IN OUT UINTN *BufferSizeInBytes, + IN CONST CHAR16 *String, + OUT UINTN *ConvertedStrLen OPTIONAL + ) +{ + UINTN HexCnt; + UINTN Idx; + UINTN BufferLength; + UINT8 Digit; + UINT8 Byte; + + // + // Find out how many hex characters the string has. + // + for (Idx = 0, HexCnt = 0; IsHexDigit (&Digit, String[Idx]); Idx++, HexCnt++); + + if (HexCnt == 0) { + *ConvertedStrLen = 0; + return RETURN_SUCCESS; + } + // + // Two Unicode characters make up 1 buffer byte. Round up. + // + BufferLength = (HexCnt + 1) / 2; + + // + // Test if buffer is passed enough. + // + if (BufferLength > (*BufferSizeInBytes)) { + *BufferSizeInBytes = BufferLength; + return RETURN_BUFFER_TOO_SMALL; + } + + *BufferSizeInBytes = BufferLength; + + for (Idx = 0; Idx < HexCnt; Idx++) { + + IsHexDigit (&Digit, String[HexCnt - 1 - Idx]); + + // + // For odd charaters, write the lower nibble for each buffer byte, + // and for even characters, the upper nibble. + // + if ((Idx & 1) == 0) { + Byte = Digit; + } else { + Byte = Buffer[Idx / 2]; + Byte &= 0x0F; + Byte = (UINT8) (Byte | Digit << 4); + } + + Buffer[Idx / 2] = Byte; + } + + if (ConvertedStrLen != NULL) { + *ConvertedStrLen = HexCnt; + } + + return RETURN_SUCCESS; +} diff --git a/MdeModulePkg/Universal/DevicePathDxe/DevicePathFromText.c b/MdeModulePkg/Universal/DevicePathDxe/DevicePathFromText.c index e183abbfe0..dbd371ce70 100644 --- a/MdeModulePkg/Universal/DevicePathDxe/DevicePathFromText.c +++ b/MdeModulePkg/Universal/DevicePathDxe/DevicePathFromText.c @@ -308,19 +308,7 @@ Xtoi ( IN CHAR16 *Str ) { - UINTN Rvalue; - UINTN Length; - - ASSERT (Str != NULL); - - // - // convert hex digits - // - Rvalue = 0; - Length = sizeof (UINTN); - HexStringToBuf ((UINT8 *) &Rvalue, &Length, Str, NULL); - - return Rvalue; + return StrHexToUintn (Str); } /** @@ -337,11 +325,7 @@ Xtoi64 ( OUT UINT64 *Data ) { - UINTN Length; - - *Data = 0; - Length = sizeof (UINT64); - HexStringToBuf ((UINT8 *) Data, &Length, Str, NULL); + *Data = StrHexToUint64 (Str); } /** @@ -524,7 +508,15 @@ StrToBuf ( for(Index = 0; Index < StrLength; Index++, Str++) { - IsHexDigit (&Digit, *Str); + if ((*Str >= L'a') && (*Str <= L'f')) { + Digit = (UINT8) (*Str - L'a' + 0x0A); + } else if ((*Str >= L'A') && (*Str <= L'F')) { + Digit = (UINT8) (*Str - L'A' + 0x0A); + } else if ((*Str >= L'0') && (*Str <= L'9')) { + Digit = (UINT8) (*Str - L'0'); + } else { + return EFI_INVALID_PARAMETER; + } // // For odd charaters, write the upper nibble for each buffer byte, @@ -546,6 +538,7 @@ StrToBuf ( /** Converts a string to GUID value. + Guid Format is xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx @param Str The registry format GUID string that contains the GUID value. @param Guid A pointer to the converted GUID value. @@ -561,46 +554,51 @@ StrToGuid ( OUT EFI_GUID *Guid ) { - UINTN BufferLength; - UINTN ConvertedStrLen; - EFI_STATUS Status; - - BufferLength = sizeof (Guid->Data1); - Status = HexStringToBuf ((UINT8 *) &Guid->Data1, &BufferLength, Str, &ConvertedStrLen); - if (EFI_ERROR (Status)) { - return Status; + // + // Get the first UINT32 data + // + Guid->Data1 = (UINT32) StrHexToUint64 (Str); + while (!IS_HYPHEN (*Str) && !IS_NULL (*Str)) { + Str ++; } - Str += ConvertedStrLen; + if (IS_HYPHEN (*Str)) { Str++; } else { return EFI_UNSUPPORTED; } - - BufferLength = sizeof (Guid->Data2); - Status = HexStringToBuf ((UINT8 *) &Guid->Data2, &BufferLength, Str, &ConvertedStrLen); - if (EFI_ERROR (Status)) { - return Status; + + // + // Get the second UINT16 data + // + Guid->Data2 = (UINT16) StrHexToUint64 (Str); + while (!IS_HYPHEN (*Str) && !IS_NULL (*Str)) { + Str ++; } - Str += ConvertedStrLen; + if (IS_HYPHEN (*Str)) { Str++; } else { return EFI_UNSUPPORTED; } - - BufferLength = sizeof (Guid->Data3); - Status = HexStringToBuf ((UINT8 *) &Guid->Data3, &BufferLength, Str, &ConvertedStrLen); - if (EFI_ERROR (Status)) { - return Status; + + // + // Get the third UINT16 data + // + Guid->Data3 = (UINT16) StrHexToUint64 (Str); + while (!IS_HYPHEN (*Str) && !IS_NULL (*Str)) { + Str ++; } - Str += ConvertedStrLen; + if (IS_HYPHEN (*Str)) { Str++; } else { return EFI_UNSUPPORTED; } + // + // Get the followin 8 bytes data + // StrToBuf (&Guid->Data4[0], 2, Str); // // Skip 2 byte hex chars diff --git a/MdeModulePkg/Universal/Network/IScsiDxe/IScsiMisc.c b/MdeModulePkg/Universal/Network/IScsiDxe/IScsiMisc.c index 53de070a2a..7cbf362275 100644 --- a/MdeModulePkg/Universal/Network/IScsiDxe/IScsiMisc.c +++ b/MdeModulePkg/Universal/Network/IScsiDxe/IScsiMisc.c @@ -22,7 +22,7 @@ Abstract: #include "IScsiImpl.h" -CONST CHAR8 IScsiHexString[] = "0123456789ABCDEFabcdef"; +GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 IScsiHexString[] = "0123456789ABCDEFabcdef"; /** Removes (trims) specified leading and trailing characters from a string. @@ -216,9 +216,9 @@ IScsiLunToUnicodeStr ( StrCpy (TempStr, L"0-"); } else { TempStr[0] = (CHAR16) IScsiHexString[Lun[2 * Index] >> 4]; - TempStr[1] = (CHAR16) IScsiHexString[Lun[2 * Index] & 0xf]; + TempStr[1] = (CHAR16) IScsiHexString[Lun[2 * Index] & 0x0F]; TempStr[2] = (CHAR16) IScsiHexString[Lun[2 * Index + 1] >> 4]; - TempStr[3] = (CHAR16) IScsiHexString[Lun[2 * Index + 1] & 0xf]; + TempStr[3] = (CHAR16) IScsiHexString[Lun[2 * Index + 1] & 0x0F]; TempStr[4] = L'-'; TempStr[5] = 0; @@ -388,8 +388,8 @@ IScsiMacAddrToStr ( UINT32 Index; for (Index = 0; Index < Len; Index++) { - Str[3 * Index] = NibbleToHexChar ((UINT8) (Mac->Addr[Index] >> 4)); - Str[3 * Index + 1] = NibbleToHexChar (Mac->Addr[Index]); + Str[3 * Index] = (CHAR16) IScsiHexString[(Mac->Addr[Index] >> 4) & 0x0F]; + Str[3 * Index + 1] = (CHAR16) IScsiHexString[Mac->Addr[Index] & 0x0F]; Str[3 * Index + 2] = L'-'; } @@ -441,7 +441,7 @@ IScsiBinToHex ( for (Index = 0; Index < BinLength; Index++) { HexStr[Index * 2 + 2] = IScsiHexString[BinBuffer[Index] >> 4]; - HexStr[Index * 2 + 3] = IScsiHexString[BinBuffer[Index] & 0xf]; + HexStr[Index * 2 + 3] = IScsiHexString[BinBuffer[Index] & 0x0F]; } HexStr[Index * 2 + 2] = '\0'; diff --git a/MdeModulePkg/Universal/SetupBrowserDxe/Expression.c b/MdeModulePkg/Universal/SetupBrowserDxe/Expression.c index a56c7318b4..5044fd1642 100644 --- a/MdeModulePkg/Universal/SetupBrowserDxe/Expression.c +++ b/MdeModulePkg/Universal/SetupBrowserDxe/Expression.c @@ -619,7 +619,6 @@ IfrToUint ( EFI_HII_VALUE Value; CHAR16 *String; CHAR16 *StringPtr; - UINTN BufferSize; Status = PopExpression (&Value); if (EFI_ERROR (Status)) { @@ -636,19 +635,19 @@ IfrToUint ( if (String == NULL) { return EFI_NOT_FOUND; } - + IfrStrToUpper (String); StringPtr = StrStr (String, L"0X"); if (StringPtr != NULL) { // // Hex string // - BufferSize = sizeof (UINT64); - Status = HexStringToBuf ((UINT8 *) &Result->Value.u64, &BufferSize, StringPtr + 2, NULL); + Result->Value.u64 = StrHexToUint64 (String); } else { // - // BUGBUG: Need handle decimal string + // decimal string // + Result->Value.u64 = StrDecimalToUint64 (String); } FreePool (String); } else { diff --git a/MdePkg/Include/Library/BaseLib.h b/MdePkg/Include/Library/BaseLib.h index 5c569ce78a..cf4e189608 100644 --- a/MdePkg/Include/Library/BaseLib.h +++ b/MdePkg/Include/Library/BaseLib.h @@ -602,135 +602,6 @@ StrHexToUint64 ( IN CONST CHAR16 *String ); -/** - Convert a nibble in the low 4 bits of a byte to a Unicode hexadecimal character. - - This function converts a nibble in the low 4 bits of a byte to a Unicode hexadecimal - character For example, the nibble 0x01 and 0x0A will converted to L'1' and L'A' - respectively. - - The upper nibble in the input byte will be masked off. - - @param Nibble The nibble which is in the low 4 bits of the input byte. - - @retval CHAR16 The Unicode hexadecimal character. - -**/ -CHAR16 -EFIAPI -NibbleToHexChar ( - IN UINT8 Nibble - ); - -/** - Convert binary buffer to a Unicode String in a specified sequence. - - This function converts bytes in the memory block pointed by Buffer to a Unicode String Str. - Each byte will be represented by two Unicode characters. For example, byte 0xA1 will - be converted into two Unicode character L'A' and L'1'. In the output String, the Unicode Character - for the Most Significant Nibble will be put before the Unicode Character for the Least Significant - Nibble. The output string for the buffer containing a single byte 0xA1 will be L"A1". - For a buffer with multiple bytes, the Unicode character produced by the first byte will be put into the - the last character in the output string. The one next to first byte will be put into the - character before the last character. This rules applies to the rest of the bytes. The Unicode - character by the last byte will be put into the first character in the output string. For example, - the input buffer for a 64-bits unsigned integer 0x12345678abcdef1234 will be converted to - a Unicode string equal to L"12345678abcdef1234". - - @param String On input, String is pointed to the buffer allocated for the convertion. - @param StringLen The Length of String buffer to hold the output String. The length must include the tailing '\0' character. - The StringLen required to convert a N bytes Buffer will be a least equal to or greater - than 2*N + 1. - @param Buffer The pointer to a input buffer. - @param BufferSizeInBytes Length in bytes of the input buffer. - - - @retval EFI_SUCCESS The convertion is successful. All bytes in Buffer has been convert to the corresponding - Unicode character and placed into the right place in String. - @retval EFI_BUFFER_TOO_SMALL StringSizeInBytes is smaller than 2 * N + 1the number of bytes required to - complete the convertion. -**/ -RETURN_STATUS -EFIAPI -BufToHexString ( - IN OUT CHAR16 *String, - IN OUT UINTN *StringLen, - IN CONST UINT8 *Buffer, - IN UINTN BufferSizeInBytes - ); - - -/** - Convert a Unicode string consisting of hexadecimal characters to a output byte buffer. - - This function converts a Unicode string consisting of characters in the range of Hexadecimal - 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 - at the first non-hexadecimal character or the NULL character. The convertion process can be - simply viewed as the reverse operations defined by BufToHexString. Two Unicode characters will be - converted into one byte. The first Unicode character represents the Most Significant Nibble and the - second Unicode character represents the Least Significant Nibble in the output byte. - The first pair of Unicode characters represents the last byte in the output buffer. The second pair of Unicode - characters represent the the byte preceding the last byte. This rule applies to the rest pairs of bytes. - The last pair represent the first byte in the output buffer. - - For example, a Unciode String L"12345678" will be converted into a buffer wil the following bytes - (first byte is the byte in the lowest memory address): "0x78, 0x56, 0x34, 0x12". - - If String has N valid hexadecimal characters for conversion, the caller must make sure Buffer is at least - N/2 (if N is even) or (N+1)/2 (if N if odd) bytes. - - @param Buffer The output buffer allocated by the caller. - @param BufferSizeInBytes On input, the size in bytes of Buffer. On output, it is updated to - contain the size of the Buffer which is actually used for the converstion. - For Unicode string with 2*N hexadecimal characters (not including the - tailing NULL character), N bytes of Buffer will be used for the output. - @param String The input hexadecimal string. - @param ConvertedStrLen The number of hexadecimal characters used to produce content in output - buffer Buffer. - - @retval RETURN_BUFFER_TOO_SMALL The input BufferSizeInBytes is too small to hold the output. BufferSizeInBytes - will be updated to the size required for the converstion. - @retval RETURN_SUCCESS The convertion is successful or the first Unicode character from String - is hexadecimal. If ConvertedStrLen is not NULL, it is updated - to the number of hexadecimal character used for the converstion. -**/ -RETURN_STATUS -EFIAPI -HexStringToBuf ( - OUT UINT8 *Buffer, - IN OUT UINTN *BufferSizeInBytes, - IN CONST CHAR16 *String, - OUT UINTN *ConvertedStrLen OPTIONAL - ); - - -/** - Test if a Unicode character is a hexadecimal digit. If true, the input - Unicode character is converted to a byte. - - This function tests if a Unicode character is a hexadecimal digit. If true, the input - Unicode character is converted to a byte. For example, Unicode character - L'A' will be converted to 0x0A. - - If Digit is NULL, then ASSERT. - - @param Digit The output hexadecimal digit. - - @param Char The input Unicode character. - - @retval TRUE Char is in the range of Hexadecimal number. Digit is updated - to the byte value of the number. - @retval FALSE Char is not in the range of Hexadecimal number. Digit is keep - intact. - -**/ -BOOLEAN -EFIAPI -IsHexDigit ( - OUT UINT8 *Digit, - IN CHAR16 Char - ); - /** Convert a Null-terminated Unicode string to a Null-terminated ASCII string and returns the ASCII string. diff --git a/MdePkg/Library/BaseLib/String.c b/MdePkg/Library/BaseLib/String.c index e056fa06cb..fb3211d77c 100644 --- a/MdePkg/Library/BaseLib/String.c +++ b/MdePkg/Library/BaseLib/String.c @@ -2074,250 +2074,3 @@ BcdToDecimal8 ( ASSERT ((Value & 0xf) < 0xa); return (UINT8) ((Value >> 4) * 10 + (Value & 0xf)); } - - -/** - Convert a nibble in the low 4 bits of a byte to a Unicode hexadecimal character. - - This function converts a nibble in the low 4 bits of a byte to a Unicode hexadecimal - character For example, the nibble 0x01 and 0x0A will converted to L'1' and L'A' - respectively. - - The upper nibble in the input byte will be masked off. - - @param Nibble The nibble which is in the low 4 bits of the input byte. - - @retval CHAR16 The Unicode hexadecimal character. - -**/ -CHAR16 -EFIAPI -NibbleToHexChar ( - IN UINT8 Nibble - ) -{ - Nibble &= 0x0F; - if (Nibble <= 0x9) { - return (CHAR16)(Nibble + L'0'); - } - - return (CHAR16)(Nibble - 0xA + L'A'); -} - -/** - Convert binary buffer to a Unicode String in a specified sequence. - - This function converts bytes in the memory block pointed by Buffer to a Unicode String Str. - Each byte will be represented by two Unicode characters. For example, byte 0xA1 will - be converted into two Unicode character L'A' and L'1'. In the output String, the Unicode Character - for the Most Significant Nibble will be put before the Unicode Character for the Least Significant - Nibble. The output string for the buffer containing a single byte 0xA1 will be L"A1". - For a buffer with multiple bytes, the Unicode character produced by the first byte will be put into the - the last character in the output string. The one next to first byte will be put into the - character before the last character. This rules applies to the rest of the bytes. The Unicode - character by the last byte will be put into the first character in the output string. For example, - the input buffer for a 64-bits unsigned integer 0x12345678abcdef1234 will be converted to - a Unicode string equal to L"12345678abcdef1234". - - @param String On input, String is pointed to the buffer allocated for the convertion. - @param StringLen The Length of String buffer to hold the output String. The length must include the tailing '\0' character. - The StringLen required to convert a N bytes Buffer will be a least equal to or greater - than 2*N + 1. - @param Buffer The pointer to a input buffer. - @param BufferSizeInBytes Length in bytes of the input buffer. - - - @retval EFI_SUCCESS The convertion is successful. All bytes in Buffer has been convert to the corresponding - Unicode character and placed into the right place in String. - @retval EFI_BUFFER_TOO_SMALL StringSizeInBytes is smaller than 2 * N + 1the number of bytes required to - complete the convertion. -**/ -RETURN_STATUS -EFIAPI -BufToHexString ( - IN OUT CHAR16 *String, - IN OUT UINTN *StringLen, - IN CONST UINT8 *Buffer, - IN UINTN BufferSizeInBytes - ) -{ - UINTN Idx; - UINT8 Byte; - UINTN StrLen; - - // - // Make sure string is either passed or allocate enough. - // It takes 2 Unicode characters (4 bytes) to represent 1 byte of the binary buffer. - // Plus the Unicode termination character. - // - StrLen = BufferSizeInBytes * 2; - if (StrLen > ((*StringLen) - 1)) { - *StringLen = StrLen + 1; - return RETURN_BUFFER_TOO_SMALL; - } - - *StringLen = StrLen + 1; - // - // Ends the string. - // - String[StrLen] = L'\0'; - - for (Idx = 0; Idx < BufferSizeInBytes; Idx++) { - - Byte = Buffer[Idx]; - String[StrLen - 1 - Idx * 2] = NibbleToHexChar (Byte); - String[StrLen - 2 - Idx * 2] = NibbleToHexChar ((UINT8)(Byte >> 4)); - } - - return RETURN_SUCCESS; -} - - -/** - Convert a Unicode string consisting of hexadecimal characters to a output byte buffer. - - This function converts a Unicode string consisting of characters in the range of Hexadecimal - 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 - at the first non-hexadecimal character or the NULL character. The convertion process can be - simply viewed as the reverse operations defined by BufToHexString. Two Unicode characters will be - converted into one byte. The first Unicode character represents the Most Significant Nibble and the - second Unicode character represents the Least Significant Nibble in the output byte. - The first pair of Unicode characters represents the last byte in the output buffer. The second pair of Unicode - characters represent the the byte preceding the last byte. This rule applies to the rest pairs of bytes. - The last pair represent the first byte in the output buffer. - - For example, a Unciode String L"12345678" will be converted into a buffer wil the following bytes - (first byte is the byte in the lowest memory address): "0x78, 0x56, 0x34, 0x12". - - If String has N valid hexadecimal characters for conversion, the caller must make sure Buffer is at least - N/2 (if N is even) or (N+1)/2 (if N if odd) bytes. - - @param Buffer The output buffer allocated by the caller. - @param BufferSizeInBytes On input, the size in bytes of Buffer. On output, it is updated to - contain the size of the Buffer which is actually used for the converstion. - For Unicode string with 2*N hexadecimal characters (not including the - tailing NULL character), N bytes of Buffer will be used for the output. - @param String The input hexadecimal string. - @param ConvertedStrLen The number of hexadecimal characters used to produce content in output - buffer Buffer. - - @retval RETURN_BUFFER_TOO_SMALL The input BufferSizeInBytes is too small to hold the output. BufferSizeInBytes - will be updated to the size required for the converstion. - @retval RETURN_SUCCESS The convertion is successful or the first Unicode character from String - is hexadecimal. If ConvertedStrLen is not NULL, it is updated - to the number of hexadecimal character used for the converstion. -**/ -RETURN_STATUS -EFIAPI -HexStringToBuf ( - OUT UINT8 *Buffer, - IN OUT UINTN *BufferSizeInBytes, - IN CONST CHAR16 *String, - OUT UINTN *ConvertedStrLen OPTIONAL - ) -{ - UINTN HexCnt; - UINTN Idx; - UINTN BufferLength; - UINT8 Digit; - UINT8 Byte; - - // - // Find out how many hex characters the string has. - // - for (Idx = 0, HexCnt = 0; IsHexDigit (&Digit, String[Idx]); Idx++, HexCnt++); - - if (HexCnt == 0) { - *ConvertedStrLen = 0; - return RETURN_SUCCESS; - } - // - // Two Unicode characters make up 1 buffer byte. Round up. - // - BufferLength = (HexCnt + 1) / 2; - - // - // Test if buffer is passed enough. - // - if (BufferLength > (*BufferSizeInBytes)) { - *BufferSizeInBytes = BufferLength; - return RETURN_BUFFER_TOO_SMALL; - } - - *BufferSizeInBytes = BufferLength; - - for (Idx = 0; Idx < HexCnt; Idx++) { - - IsHexDigit (&Digit, String[HexCnt - 1 - Idx]); - - // - // For odd charaters, write the lower nibble for each buffer byte, - // and for even characters, the upper nibble. - // - if ((Idx & 1) == 0) { - Byte = Digit; - } else { - Byte = Buffer[Idx / 2]; - Byte &= 0x0F; - Byte = (UINT8) (Byte | Digit << 4); - } - - Buffer[Idx / 2] = Byte; - } - - if (ConvertedStrLen != NULL) { - *ConvertedStrLen = HexCnt; - } - - return RETURN_SUCCESS; -} - - -/** - Test if a Unicode character is a hexadecimal digit. If true, the input - Unicode character is converted to a byte. - - This function tests if a Unicode character is a hexadecimal digit. If true, the input - Unicode character is converted to a byte. For example, Unicode character - L'A' will be converted to 0x0A. - - If Digit is NULL, then ASSERT. - - @param Digit The output hexadecimal digit. - - @param Char The input Unicode character. - - @retval TRUE Char is in the range of Hexadecimal number. Digit is updated - to the byte value of the number. - @retval FALSE Char is not in the range of Hexadecimal number. Digit is keep - intact. - -**/ -BOOLEAN -EFIAPI -IsHexDigit ( - OUT UINT8 *Digit, - IN CHAR16 Char - ) -{ - ASSERT (Digit != NULL); - - if ((Char >= L'0') && (Char <= L'9')) { - *Digit = (UINT8) (Char - L'0'); - return TRUE; - } - - if ((Char >= L'A') && (Char <= L'F')) { - *Digit = (UINT8) (Char - L'A' + 0x0A); - return TRUE; - } - - if ((Char >= L'a') && (Char <= L'f')) { - *Digit = (UINT8) (Char - L'a' + 0x0A); - return TRUE; - } - - return FALSE; -} - - -- 2.39.2