summaryrefslogtreecommitdiffstats
path: root/src/UUID.cpp
blob: e150b36039a395f8898faa03c020fd0d09b959f2 (plain) (blame)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// UUID.h

// Defines the cUUID class representing a Universally Unique Identifier

#include "Globals.h"
#include "UUID.h"

#include "polarssl/md5.h"


/** UUID normalised in textual form. */
struct sShortUUID
{
	char Data[32]{};
	bool IsValid = false;
};

/** Returns the given UUID in shortened form with IsValid indicating success.
Doesn't check digits are hexadecimal but does check dashes if long form. */
static sShortUUID ShortenUUID(const AString & a_StringUUID)
{
	sShortUUID UUID;
	switch (a_StringUUID.size())
	{
		case 32:
		{
			// Already a short UUID
			std::memcpy(UUID.Data, a_StringUUID.data(), 32);
			UUID.IsValid = true;
			break;
		}
		case 36:
		{
			// Long UUID, confirm dashed
			if (
				(a_StringUUID[ 8] != '-') ||
				(a_StringUUID[13] != '-') ||
				(a_StringUUID[18] != '-') ||
				(a_StringUUID[23] != '-')
			)
			{
				break;
			}

			// Copy everying but the dashes from the string
			std::memcpy(UUID.Data,      a_StringUUID.data(),       8);
			std::memcpy(UUID.Data +  8, a_StringUUID.data() +  9,  4);
			std::memcpy(UUID.Data + 12, a_StringUUID.data() + 14,  4);
			std::memcpy(UUID.Data + 16, a_StringUUID.data() + 19,  4);
			std::memcpy(UUID.Data + 20, a_StringUUID.data() + 24, 12);
			UUID.IsValid = true;
		}
		default: break;
	}
	return UUID;
}





/** Returns the integer value of the hex digit or 0xff if invalid. */
static Byte FromHexDigit(char a_Hex)
{
	if (('0' <= a_Hex) && (a_Hex <= '9'))
	{
		return static_cast<Byte>(a_Hex - '0');
	}
	if (('a' <= a_Hex) && (a_Hex <= 'f'))
	{
		return static_cast<Byte>(a_Hex - 'a');
	}
	if (('A' <= a_Hex) && (a_Hex <= 'F'))
	{
		return static_cast<Byte>(a_Hex - 'A');
	}
	return 0xff;
}





/** From a number in the range [0, 16), returns the corresponding hex digit in lowercase. */
static char ToHexDigit(UInt8 a_Nibble)
{
	ASSERT((a_Nibble & 0xf0) == 0);
	return static_cast<char>(
		(a_Nibble < 10) ?
		('0' + a_Nibble) :
		('a' + (a_Nibble - 10))
	);
}





////////////////////////////////////////////////////////////////////////////////
// cUUID:

bool cUUID::FromString(const AString & a_StringUUID)
{
	sShortUUID Norm = ShortenUUID(a_StringUUID);
	if (!Norm.IsValid)
	{
		return false;
	}

	std::array<Byte, 16> ParsedUUID{{0}};
	for (size_t i = 0; i != m_UUID.size(); ++i)
	{
		Byte HighNibble = FromHexDigit(Norm.Data[2 * i    ]);
		Byte LowNibble  = FromHexDigit(Norm.Data[2 * i + 1]);
		if ((HighNibble > 0x0f) || (LowNibble > 0x0f))
		{
			// Invalid hex digit
			return false;
		}

		ParsedUUID[i] = static_cast<Byte>((HighNibble << 4) | LowNibble);
	}

	// Parsed successfully
	m_UUID = ParsedUUID;
	return true;
}





AString cUUID::ToShortString() const
{
	AString ShortString(32, '\0');
	for (size_t i = 0; i != m_UUID.size(); ++i)
	{
		Byte HighNibble = (m_UUID[i] >> 4) & 0x0f;
		Byte LowNibble  = m_UUID[i] & 0x0f;

		ShortString[2 * i    ] = ToHexDigit(HighNibble);
		ShortString[2 * i + 1] = ToHexDigit(LowNibble);
	}
	return ShortString;
}





AString cUUID::ToLongString() const
{
	AString LongString = ToShortString();
	LongString.reserve(36);

	// Convert to long form by inserting the dashes
	auto First = LongString.begin();
	LongString.insert(First +  8, '-');
	LongString.insert(First + 13, '-');
	LongString.insert(First + 18, '-');
	LongString.insert(First + 23, '-');

	return LongString;
}





UInt8 cUUID::Version() const
{
	return static_cast<UInt8>((m_UUID[6] >> 4) & 0x0f);
}





UInt8 cUUID::Variant() const
{
	const Byte VariantBits = static_cast<Byte>((m_UUID[9] >> 5) & 0x07);

	/* Variant bits format:
		bits | variant |      Description
		-----|---------|----------------------
		0xx  |    0    |        Obsolete
		10x  |    1    |     Standard UUID
		110  |    2    | Microsoft Legacy GUID
		111  |    3    |        Reserved
	*/

	if ((VariantBits & 0x04) == 0)
	{
		return 0;
	}
	else if ((VariantBits & 0x02) == 0)
	{
		return 1;
	}
	else if ((VariantBits & 0x01) == 0)
	{
		return 2;
	}
	else
	{
		return 3;
	}
}





std::array<Byte, 16> cUUID::ToRaw() const
{
	std::array<Byte, 16> Raw(m_UUID);
	if (Variant() == 2)
	{
		// Convert to microsoft mixed-endian format
		// First 3 components are host-endian, last 2 are network
		auto First = reinterpret_cast<UInt32 *>(Raw.data());
		*First = ntohl(*First);

		auto Second = reinterpret_cast<UInt16 *>(&Raw[4]);
		*Second = ntohs(*Second);

		auto Third = Second + 1;
		*Third = ntohs(*Third);
	}

	return Raw;
}





void cUUID::FromRaw(const std::array<Byte, 16> & a_Raw)
{
	m_UUID = a_Raw;
	if (Variant() != 2)
	{
		// Standard big-endian formats
		return;
	}

	// Convert from microsoft mixed-endian format
	// First 3 components are host-endian, last 2 are network
	auto First = reinterpret_cast<UInt32 *>(m_UUID.data());
	*First = htonl(*First);

	auto Second = reinterpret_cast<UInt16 *>(&m_UUID[4]);
	*Second = htons(*Second);

	auto Third = Second + 1;
	*Third = htons(*Third);
}





cUUID cUUID::GenerateVersion3(const AString & a_Name)
{
	cUUID UUID;
	// Generate an md5 checksum, and use it as base for the ID:
	const Byte * ByteString = reinterpret_cast<const Byte *>(a_Name.data());
	md5(ByteString, a_Name.length(), UUID.m_UUID.data());

	// Insert version number
	UUID.m_UUID[6] = (UUID.m_UUID[6] & 0x0f) | 0x30;

	/* Insert variant number
		Note that by using 1000 instead of 10xx we are losing 2 bits
		but this is needed for compatibility with the old string uuid generator */
	UUID.m_UUID[8] = (UUID.m_UUID[8] & 0x0f) | 0x80;

	return UUID;
}