xref: /aosp_15_r20/external/coreboot/src/lib/uuid.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <stdint.h>
4 #include <lib.h>
5 #include <uuid.h>
6 
parse_uuid(uint8_t * const uuid,const char * const uuid_str)7 int parse_uuid(uint8_t *const uuid, const char *const uuid_str)
8 {
9 	const uint8_t order[] = { 3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15 };
10 	uint8_t uuid_binstr[UUID_LEN];
11 	unsigned int i;
12 
13 	if (strlen(uuid_str) != UUID_STRLEN)
14 		return -1;
15 	if (uuid_str[8] != '-' || uuid_str[13] != '-' ||
16 	    uuid_str[18] != '-' || uuid_str[23] != '-')
17 		return -1;
18 	if (hexstrtobin(uuid_str, uuid_binstr, UUID_LEN) != UUID_LEN)
19 		return -1;
20 	for (i = 0; i < UUID_LEN; ++i)
21 		uuid[i] = uuid_binstr[order[i]];
22 
23 	return 0;
24 }
25