xref: /freebsd/contrib/wpa/src/utils/uuid.c (revision fed1ca4b719c56c930f2259d80663cd34be812bb)
1 /*
2  * Universally Unique IDentifier (UUID)
3  * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 
11 #include "common.h"
12 #include "uuid.h"
13 
14 int uuid_str2bin(const char *str, u8 *bin)
15 {
16 	const char *pos;
17 	u8 *opos;
18 
19 	pos = str;
20 	opos = bin;
21 
22 	if (hexstr2bin(pos, opos, 4))
23 		return -1;
24 	pos += 8;
25 	opos += 4;
26 
27 	if (*pos++ != '-' || hexstr2bin(pos, opos, 2))
28 		return -1;
29 	pos += 4;
30 	opos += 2;
31 
32 	if (*pos++ != '-' || hexstr2bin(pos, opos, 2))
33 		return -1;
34 	pos += 4;
35 	opos += 2;
36 
37 	if (*pos++ != '-' || hexstr2bin(pos, opos, 2))
38 		return -1;
39 	pos += 4;
40 	opos += 2;
41 
42 	if (*pos++ != '-' || hexstr2bin(pos, opos, 6))
43 		return -1;
44 
45 	return 0;
46 }
47 
48 
49 int uuid_bin2str(const u8 *bin, char *str, size_t max_len)
50 {
51 	int len;
52 	len = os_snprintf(str, max_len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
53 			  "%02x%02x-%02x%02x%02x%02x%02x%02x",
54 			  bin[0], bin[1], bin[2], bin[3],
55 			  bin[4], bin[5], bin[6], bin[7],
56 			  bin[8], bin[9], bin[10], bin[11],
57 			  bin[12], bin[13], bin[14], bin[15]);
58 	if (os_snprintf_error(max_len, len))
59 		return -1;
60 	return 0;
61 }
62 
63 
64 int is_nil_uuid(const u8 *uuid)
65 {
66 	int i;
67 	for (i = 0; i < UUID_LEN; i++)
68 		if (uuid[i])
69 			return 0;
70 	return 1;
71 }
72