-#ifndef DISABLE_UNUSED_HII_PROTOCOLS\r
-\r
-STATIC\r
-CHAR16\r
-NibbleToHexCharPrivate (\r
- IN UINT8 Nibble\r
- )\r
-/*++\r
-\r
- Routine Description:\r
- Converts the low nibble of a byte to hex unicode character.\r
-\r
- Arguments:\r
- Nibble - lower nibble of a byte.\r
-\r
- Returns:\r
- Hex unicode character between L'0' to L'f'.\r
-\r
---*/\r
-{\r
- Nibble &= 0x0F;\r
-\r
- if (Nibble <= 0x9) {\r
- return (CHAR16)(Nibble + L'0');\r
- }\r
-\r
- return (CHAR16)(Nibble - 0xA + L'a');\r
-}\r
-\r
-\r
-/**\r
- Converts Unicode string to binary buffer.\r
- The conversion may be partial.\r
- The first character in the string that is not hex digit stops the conversion.\r
- At a minimum, any blob of data could be represented as a hex string.\r
-\r
- @param Buf Pointer to buffer that receives the data.\r
- @param Len Length in bytes of the buffer to hold converted\r
- data. If routine return with EFI_SUCCESS,\r
- containing length of converted data. If routine\r
- return with EFI_BUFFER_TOO_SMALL, containg length\r
- of buffer desired.\r
- @param Str String to be converted from.\r
- @param ConvertedStrLen Length of the Hex String consumed.\r
-\r
- @retval EFI_SUCCESS Routine Success.\r
- @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold converted data.\r
-\r
-**/\r
-STATIC\r
-EFI_STATUS\r
-HexStringToBufPrivate (\r
- IN OUT UINT8 *Buf,\r
- IN OUT UINTN *Len,\r
- IN CHAR16 *Str,\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, Str[Idx]); Idx++, HexCnt++);\r
-\r
- if (HexCnt == 0) {\r
- *Len = 0;\r
- return EFI_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 > (*Len)) {\r
- *Len = BufferLength;\r
- return EFI_BUFFER_TOO_SMALL;\r
- }\r
-\r
- *Len = BufferLength;\r
-\r
- for (Idx = 0; Idx < HexCnt; Idx++) {\r
-\r
- IsHexDigit (&Digit, Str[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 = (UINT8) (Digit << 4);\r
- } else {\r
- Byte = Buf[Idx / 2];\r
- Byte &= 0xF0;\r
- Byte = (UINT8) (Byte | Digit);\r
- }\r
-\r
- Buf[Idx / 2] = Byte;\r
- }\r
-\r
- if (ConvertedStrLen != NULL) {\r
- *ConvertedStrLen = HexCnt;\r
- }\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-\r
-/**\r
- Converts binary buffer to Unicode string.\r
- At a minimum, any blob of data could be represented as a hex string.\r
-\r
- @param Str Pointer to the string.\r
- @param HexStringBufferLength Length in bytes of buffer to hold the hex string.\r
- Includes tailing '\0' character. If routine return\r
- with EFI_SUCCESS, containing length of hex string\r
- buffer. If routine return with\r
- EFI_BUFFER_TOO_SMALL, containg length of hex\r
- string buffer desired.\r
- @param Buf Buffer to be converted from.\r
- @param Len Length in bytes of the buffer to be converted.\r
- @param Flag If TRUE, encode the data in the same order as the\r
- it resides in the Buf. Else encode it in the\r
- reverse direction.\r
-\r
- @retval EFI_SUCCESS Routine success.\r
- @retval EFI_BUFFER_TOO_SMALL The hex string buffer is too small.\r
-\r
-**/\r
-STATIC\r
-EFI_STATUS\r
-BufToHexStringPrivate (\r
- IN OUT CHAR16 *Str,\r
- IN OUT UINTN *HexStringBufferLength,\r
- IN UINT8 *Buf,\r
- IN UINTN Len,\r
- IN BOOLEAN Flag\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 = Len * 2;\r
- if ((*HexStringBufferLength) < (StrLen + 1) * sizeof (CHAR16)) {\r
- *HexStringBufferLength = (StrLen + 1) * sizeof (CHAR16);\r
- return EFI_BUFFER_TOO_SMALL;\r
- }\r
-\r
- *HexStringBufferLength = (StrLen + 1) * sizeof (CHAR16);\r
-\r
- //\r
- // Ends the string.\r
- //\r
- Str[StrLen] = 0;\r
-\r
- for (Idx = 0; Idx < Len; Idx++) {\r
-\r
- Byte = Buf[Idx];\r
- if (Flag) {\r
- Str[Idx * 2] = NibbleToHexCharPrivate ((UINT8)(Byte >> 4));\r
- Str[Idx * 2 + 1] = NibbleToHexCharPrivate (Byte);\r
- } else {\r
- Str[StrLen - 1 - Idx * 2] = NibbleToHexCharPrivate (Byte);\r
- Str[StrLen - 2 - Idx * 2] = NibbleToHexCharPrivate ((UINT8)(Byte >> 4));\r
- }\r
- }\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-\r
-\r