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