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