1) Add BufToHexString, HexStringToBuf and IsHexDigit to BaseLib.

2) Remove the duplicated functions implementation  from the modules that reference these APIs 

git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@5283 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
qwang12
2008-05-23 05:30:08 +00:00
parent d9e5c1fffb
commit 36fe40c2ea
21 changed files with 146 additions and 974 deletions

View File

@ -271,134 +271,6 @@ GetNextDeviceNodeStr (
return ReturnStr;
}
STATIC
BOOLEAN
IsHexDigit (
OUT UINT8 *Digit,
IN CHAR16 Char
)
/*++
Routine Description:
Determines if a Unicode character is a hexadecimal digit.
The test is case insensitive.
Arguments:
Digit - Pointer to byte that receives the value of the hex character.
Char - Unicode character to test.
Returns:
TRUE - If the character is a hexadecimal digit.
FALSE - Otherwise.
--*/
{
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;
}
STATIC
EFI_STATUS
HexStringToBuf (
IN OUT UINT8 *Buf,
IN OUT UINTN *Len,
IN CHAR16 *Str,
OUT UINTN *ConvertedStrLen OPTIONAL
)
/*++
Routine Description:
Converts Unicode string to binary buffer.
The conversion may be partial.
The first character in the string that is not hex digit stops the conversion.
At a minimum, any blob of data could be represented as a hex string.
Arguments:
Buf - Pointer to buffer that receives the data.
Len - Length in bytes of the buffer to hold converted data.
If routine return with EFI_SUCCESS, containing length of converted data.
If routine return with EFI_BUFFER_TOO_SMALL, containg length of buffer desired.
Str - String to be converted from.
ConvertedStrLen - Length of the Hex String consumed.
Returns:
EFI_SUCCESS: Routine Success.
EFI_BUFFER_TOO_SMALL: The buffer is too small to hold converted data.
EFI_
--*/
{
UINTN HexCnt;
UINTN Idx;
UINTN BufferLength;
UINT8 Digit;
UINT8 Byte;
Digit = 0;
//
// Find out how many hex characters the string has.
//
for (Idx = 0, HexCnt = 0; IsHexDigit (&Digit, Str[Idx]); Idx++, HexCnt++);
if (HexCnt == 0) {
*Len = 0;
return EFI_SUCCESS;
}
//
// Two Unicode characters make up 1 buffer byte. Round up.
//
BufferLength = (HexCnt + 1) / 2;
//
// Test if buffer is passed enough.
//
if (BufferLength > (*Len)) {
*Len = BufferLength;
return EFI_BUFFER_TOO_SMALL;
}
*Len = BufferLength;
for (Idx = 0; Idx < HexCnt; Idx++) {
IsHexDigit (&Digit, Str[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 = Buf[Idx / 2];
Byte &= 0x0F;
Byte = (UINT8) (Byte | Digit << 4);
}
Buf[Idx / 2] = Byte;
}
if (ConvertedStrLen != NULL) {
*ConvertedStrLen = HexCnt;
}
return EFI_SUCCESS;
}
STATIC
CHAR16 *