spd/lp5: Add Hynix memory part
[coreboot2.git] / src / lib / hexstrtobin.c
blob58dea8e349222af7ac787f73785d934ce7724948
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <ctype.h>
4 #include <lib.h>
6 size_t hexstrtobin(const char *str, uint8_t *buf, size_t len)
8 size_t count, ptr = 0;
9 uint8_t byte;
11 for (byte = count = 0; str && *str; str++) {
12 uint8_t c = *str;
14 if (!isxdigit(c))
15 continue;
16 if (isdigit(c))
17 c -= '0';
18 else
19 c = tolower(c) - 'a' + 10;
21 byte <<= 4;
22 byte |= c;
24 if (++count > 1) {
25 if (ptr >= len)
26 return ptr;
27 buf[ptr++] = byte;
28 byte = count = 0;
32 return ptr;