1189251Ssam/*
2189251Ssam * Universally Unique IDentifier (UUID)
3189251Ssam * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
4189251Ssam *
5252726Srpaulo * This software may be distributed under the terms of the BSD license.
6252726Srpaulo * See README for more details.
7189251Ssam */
8189251Ssam
9189251Ssam#include "includes.h"
10189251Ssam
11189251Ssam#include "common.h"
12189251Ssam#include "uuid.h"
13189251Ssam
14189251Ssamint uuid_str2bin(const char *str, u8 *bin)
15189251Ssam{
16189251Ssam	const char *pos;
17189251Ssam	u8 *opos;
18189251Ssam
19189251Ssam	pos = str;
20189251Ssam	opos = bin;
21189251Ssam
22189251Ssam	if (hexstr2bin(pos, opos, 4))
23189251Ssam		return -1;
24189251Ssam	pos += 8;
25189251Ssam	opos += 4;
26189251Ssam
27189251Ssam	if (*pos++ != '-' || hexstr2bin(pos, opos, 2))
28189251Ssam		return -1;
29189251Ssam	pos += 4;
30189251Ssam	opos += 2;
31189251Ssam
32189251Ssam	if (*pos++ != '-' || hexstr2bin(pos, opos, 2))
33189251Ssam		return -1;
34189251Ssam	pos += 4;
35189251Ssam	opos += 2;
36189251Ssam
37189251Ssam	if (*pos++ != '-' || hexstr2bin(pos, opos, 2))
38189251Ssam		return -1;
39189251Ssam	pos += 4;
40189251Ssam	opos += 2;
41189251Ssam
42189251Ssam	if (*pos++ != '-' || hexstr2bin(pos, opos, 6))
43189251Ssam		return -1;
44189251Ssam
45189251Ssam	return 0;
46189251Ssam}
47189251Ssam
48189251Ssam
49189251Ssamint uuid_bin2str(const u8 *bin, char *str, size_t max_len)
50189251Ssam{
51189251Ssam	int len;
52189251Ssam	len = os_snprintf(str, max_len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
53189251Ssam			  "%02x%02x-%02x%02x%02x%02x%02x%02x",
54189251Ssam			  bin[0], bin[1], bin[2], bin[3],
55189251Ssam			  bin[4], bin[5], bin[6], bin[7],
56189251Ssam			  bin[8], bin[9], bin[10], bin[11],
57189251Ssam			  bin[12], bin[13], bin[14], bin[15]);
58189251Ssam	if (len < 0 || (size_t) len >= max_len)
59189251Ssam		return -1;
60189251Ssam	return 0;
61189251Ssam}
62189251Ssam
63189251Ssam
64189251Ssamint is_nil_uuid(const u8 *uuid)
65189251Ssam{
66189251Ssam	int i;
67189251Ssam	for (i = 0; i < UUID_LEN; i++)
68189251Ssam		if (uuid[i])
69189251Ssam			return 0;
70189251Ssam	return 1;
71189251Ssam}
72