summaryrefslogtreecommitdiffstats
path: root/lib/uuid.c
blob: 3af3a7dfaeca51bc469e6badd3857fdb26b8ce00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright 2011 Calxeda, Inc.
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <linux/ctype.h>
#include <errno.h>
#include <common.h>

#define UUID_STR_LEN		36

/*
 * UUID - Universally Unique IDentifier - 128 bits unique number.
 *        There are 5 versions and one variant of UUID defined by RFC4122
 *        specification. Depends on version uuid number base on a time,
 *        host name, MAC address or random data.
 *
 * UUID binary format (16 bytes):
 *
 * 4B-2B-2B-2B-6B (big endian - network byte order)
 *
 * UUID string is 36 length of characters (36 bytes):
 *
 * 0        9    14   19   24
 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
 *    be     be   be   be       be
 *
 * where x is a hexadecimal character. Fields are separated by '-'s.
 * When converting to a binary UUID, le means the field should be converted
 * to little endian and be means it should be converted to big endian.
 *
 * UUID is also used as GUID (Globally Unique Identifier) with the same binary
 * format but it differs in string format like below.
 *
 * GUID:
 * 0        9    14   19   24
 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
 *    le     le   le   be       be
 *
 * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
 */

int uuid_str_valid(const char *uuid)
{
	int i, valid;

	if (uuid == NULL)
		return 0;

	for (i = 0, valid = 1; uuid[i] && valid; i++) {
		switch (i) {
		case 8: case 13: case 18: case 23:
			valid = (uuid[i] == '-');
			break;
		default:
			valid = isxdigit(uuid[i]);
			break;
		}
	}

	if (i != 36 || !valid)
		return 0;

	return 1;
}

int uuid_str_to_bin(char *uuid, unsigned char *out)
{
	uint16_t tmp16;
	uint32_t tmp32;
	uint64_t tmp64;

	if (!uuid || !out)
		return -EINVAL;

	if (strlen(uuid) != UUID_STR_LEN)
		return -EINVAL;

	tmp32 = cpu_to_le32(simple_strtoul(uuid, NULL, 16));
	memcpy(out, &tmp32, 4);

	tmp16 = cpu_to_le16(simple_strtoul(uuid + 9, NULL, 16));
	memcpy(out + 4, &tmp16, 2);

	tmp16 = cpu_to_le16(simple_strtoul(uuid + 14, NULL, 16));
	memcpy(out + 6, &tmp16, 2);

	tmp16 = cpu_to_be16(simple_strtoul(uuid + 19, NULL, 16));
	memcpy(out + 8, &tmp16, 2);

	tmp64 = cpu_to_be64(simple_strtoull(uuid + 24, NULL, 16));
	memcpy(out + 10, (char *)&tmp64 + 2, 6);

	return 0;
}

void uuid_bin_to_str(unsigned char *uuid, char *str)
{
	static const u8 le[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11,
				  12, 13, 14, 15};
	int i;

	for (i = 0; i < 16; i++) {
		sprintf(str, "%02x", uuid[le[i]]);
		str += 2;
		switch (i) {
		case 3:
		case 5:
		case 7:
		case 9:
			*str++ = '-';
			break;
		}
	}
}
OpenPOWER on IntegriCloud