1 /*
2  * Copyright (c) 2025 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  *   This file provides utility functions for the OpenThread module.
10  *
11  */
12 
13 #include <stddef.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <stdlib.h>
17 #include <errno.h>
18 
19 #include <openthread_utils.h>
20 
bytes_from_str(uint8_t * buf,int buf_len,const char * src)21 int bytes_from_str(uint8_t *buf, int buf_len, const char *src)
22 {
23 	if (!buf || !src) {
24 		return -EINVAL;
25 	}
26 
27 	size_t i;
28 	size_t src_len = strlen(src);
29 	char *endptr;
30 
31 	for (i = 0U; i < src_len; i++) {
32 		if (!isxdigit((unsigned char)src[i]) && src[i] != ':') {
33 			return -EINVAL;
34 		}
35 	}
36 
37 	(void)memset(buf, 0, buf_len);
38 
39 	for (i = 0U; i < (size_t)buf_len; i++) {
40 		buf[i] = (uint8_t)strtol(src, &endptr, 16);
41 		src = ++endptr;
42 	}
43 
44 	return 0;
45 }
46