UPSTREAM: hexstrtobin: Add a library function to decode ASCII hex into binary
This function will turn a string of ASCII hex characters into an array
of bytes. It will ignore any non-ASCII-hex characters in the input
string and decode up to len bytes of data from it.
This can be used for turning MAC addresses or UUID strings into binary
for storage or further processing.
Sample usage:
uint8_t buf[6];
hexstrtobin("00:0e:c6:81:72:01", buf, sizeof(buf));
acpigen_emit_stream(buf, sizeof(buf));
BUG=None
BRANCH=None
TEST=None
Change-Id: I2de9bd28ae8c42cdca09eec11a3bba497a52988c
Original-Signed-off-by: Duncan Laurie <dlaurie@chromium.org>
Original-Reviewed-on: https://review.coreboot.org/14837
Original-Tested-by: build bot (Jenkins)
Original-Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Signed-off-by: Aaron Durbin <adurbin@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/346987
Commit-Ready: Martin Roth <martinroth@chromium.org>
Tested-by: Martin Roth <martinroth@chromium.org>
Reviewed-by: Martin Roth <martinroth@chromium.org>
This commit is contained in:
parent
be5b71df6f
commit
5f9d247b66
3 changed files with 58 additions and 0 deletions
|
|
@ -44,6 +44,19 @@ int checkstack(void *top_of_stack, int core);
|
|||
void hexdump(const void *memory, size_t length);
|
||||
void hexdump32(char LEVEL, const void *d, size_t len);
|
||||
|
||||
/*
|
||||
* hexstrtobin - Turn a string of ASCII hex characters into binary
|
||||
*
|
||||
* @str: String of hex characters to parse
|
||||
* @buf: Buffer to store the resulting bytes into
|
||||
* @len: Maximum length of buffer to fill
|
||||
*
|
||||
* Defined in src/lib/hexstrtobin.c
|
||||
* Ignores non-hex characters in the string.
|
||||
* Returns the number of bytes that have been put in the buffer.
|
||||
*/
|
||||
size_t hexstrtobin(const char *str, uint8_t *buf, size_t len);
|
||||
|
||||
#if !defined(__ROMCC__)
|
||||
/* Count Leading Zeroes: clz(0) == 32, clz(0xf) == 28, clz(1 << 31) == 0 */
|
||||
static inline int clz(u32 x) { return x ? __builtin_clz(x) : sizeof(x) * 8; }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue