diff options
author | qwang12 <qwang12@6f19259b-4bc3-4df7-8a09-765794883524> | 2008-05-23 05:25:44 +0000 |
---|---|---|
committer | qwang12 <qwang12@6f19259b-4bc3-4df7-8a09-765794883524> | 2008-05-23 05:25:44 +0000 |
commit | d9e5c1fffb22d39dd52ef23febe40cd4e2ee0965 (patch) | |
tree | 23b9c355ca02dbee8566b71685c63c319e9ee08d /MdePkg/Library/BaseLib/String.c | |
parent | 254ba2477fd0b101228da33db03efed95c6489ae (diff) | |
download | edk2-d9e5c1fffb22d39dd52ef23febe40cd4e2ee0965.tar.gz edk2-d9e5c1fffb22d39dd52ef23febe40cd4e2ee0965.tar.bz2 edk2-d9e5c1fffb22d39dd52ef23febe40cd4e2ee0965.zip |
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@5282 6f19259b-4bc3-4df7-8a09-765794883524
Diffstat (limited to 'MdePkg/Library/BaseLib/String.c')
-rw-r--r-- | MdePkg/Library/BaseLib/String.c | 239 |
1 files changed, 239 insertions, 0 deletions
diff --git a/MdePkg/Library/BaseLib/String.c b/MdePkg/Library/BaseLib/String.c index 94471f3329..909262e587 100644 --- a/MdePkg/Library/BaseLib/String.c +++ b/MdePkg/Library/BaseLib/String.c @@ -2073,3 +2073,242 @@ BcdToDecimal8 ( }
+/**
+ 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
+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 binary Buffer Buf 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 integrer 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 Lenth in bytes of the input buffer.
+
+
+ @retval EFI_SUCCESS The convertion is successfull. 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.
+
+ @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
+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;
+}
+
+
|