summaryrefslogtreecommitdiff
path: root/src/lib/hexstrtobin.c
blob: 58dea8e349222af7ac787f73785d934ce7724948 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* SPDX-License-Identifier: GPL-2.0-only */

#include <ctype.h>
#include <lib.h>

size_t hexstrtobin(const char *str, uint8_t *buf, size_t len)
{
	size_t count, ptr = 0;
	uint8_t byte;

	for (byte = count = 0; str && *str; str++) {
		uint8_t c = *str;

		if (!isxdigit(c))
			continue;
		if (isdigit(c))
			c -= '0';
		else
			c = tolower(c) - 'a' + 10;

		byte <<= 4;
		byte |= c;

		if (++count > 1) {
			if (ptr >= len)
				return ptr;
			buf[ptr++] = byte;
			byte = count = 0;
		}
	}

	return ptr;
}