Convert a Null-terminated ASCII hexadecimal string to a byte array. This function outputs a byte array by interpreting the contents of the ASCII string specified by String in hexadecimal format. The format of the input ASCII string String is: [XX]* X is a hexadecimal digit character in the range [0-9], [a-f] and [A-F]. The function decodes every two hexadeci
| 3599 | @retval RETURN_BUFFER_TOO_SMALL If MaxBufferSize is less than (Length / 2). |
| 3600 | **/ |
| 3601 | RETURN_STATUS |
| 3602 | EFIAPI |
| 3603 | AsciiStrHexToBytes ( |
| 3604 | IN CONST CHAR8 *String, |
| 3605 | IN UINTN Length, |
| 3606 | OUT UINT8 *Buffer, |
| 3607 | IN UINTN MaxBufferSize |
| 3608 | ) |
| 3609 | { |
| 3610 | UINTN Index; |
| 3611 | |
| 3612 | // |
| 3613 | // 1. None of String or Buffer shall be a null pointer. |
| 3614 | // |
| 3615 | SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER); |
| 3616 | SAFE_STRING_CONSTRAINT_CHECK ((Buffer != NULL), RETURN_INVALID_PARAMETER); |
| 3617 | |
| 3618 | // |
| 3619 | // 2. Length shall not be greater than ASCII_RSIZE_MAX. |
| 3620 | // |
| 3621 | if (ASCII_RSIZE_MAX != 0) { |
| 3622 | SAFE_STRING_CONSTRAINT_CHECK ((Length <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER); |
| 3623 | } |
| 3624 | |
| 3625 | // |
| 3626 | // 3. Length shall not be odd. |
| 3627 | // |
| 3628 | SAFE_STRING_CONSTRAINT_CHECK (((Length & BIT0) == 0), RETURN_INVALID_PARAMETER); |
| 3629 | |
| 3630 | // |
| 3631 | // 4. MaxBufferSize shall equal to or greater than Length / 2. |
| 3632 | // |
| 3633 | SAFE_STRING_CONSTRAINT_CHECK ((MaxBufferSize >= Length / 2), RETURN_BUFFER_TOO_SMALL); |
| 3634 | |
| 3635 | // |
| 3636 | // 5. String shall not contains invalid hexadecimal digits. |
| 3637 | // |
| 3638 | for (Index = 0; Index < Length; Index++) { |
| 3639 | if (!InternalAsciiIsHexaDecimalDigitCharacter (String[Index])) { |
| 3640 | break; |
| 3641 | } |
| 3642 | } |
| 3643 | if (Index != Length) { |
| 3644 | return RETURN_UNSUPPORTED; |
| 3645 | } |
| 3646 | |
| 3647 | // |
| 3648 | // Convert the hex string to bytes. |
| 3649 | // |
| 3650 | for(Index = 0; Index < Length; Index++) { |
| 3651 | |
| 3652 | // |
| 3653 | // For even characters, write the upper nibble for each buffer byte, |
| 3654 | // and for even characters, the lower nibble. |
| 3655 | // |
| 3656 | if ((Index & BIT0) == 0) { |
| 3657 | Buffer[Index / 2] = (UINT8) InternalAsciiHexCharToUintn (String[Index]) << 4; |
| 3658 | } else { |
no test coverage detected