1#include "uuid_manager.h"
2
3#include <base/bytes.h>
4#include <base/dbg.h>
5#include <base/hash_ctxt.h>
6#include <base/mem.h>
7#include <base/secure.h>
8#include <base/str.h>
9
10#include <engine/shared/packer.h>
11
12#include <algorithm>
13#include <cstdio>
14
15static const CUuid TEEWORLDS_NAMESPACE = {.m_aData: {// "e05ddaaa-c4e6-4cfb-b642-5d48e80c0029"
16 0xe0, 0x5d, 0xda, 0xaa, 0xc4, 0xe6, 0x4c, 0xfb,
17 0xb6, 0x42, 0x5d, 0x48, 0xe8, 0x0c, 0x00, 0x29}};
18
19const CUuid UUID_ZEROED = {.m_aData: {// "00000000-0000-0000-0000-000000000000"
20 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
22
23CUuid RandomUuid()
24{
25 CUuid Result;
26 secure_random_fill(bytes: &Result, length: sizeof(Result));
27
28 // set version 4 (UUID is randomly generated)
29 Result.m_aData[6] &= 0x0f;
30 Result.m_aData[6] |= 0x40;
31
32 // set variant 1 (RFC 4122)
33 Result.m_aData[8] &= 0x3f;
34 Result.m_aData[8] |= 0x80;
35
36 return Result;
37}
38
39CUuid CalculateUuid(const char *pName)
40{
41 MD5_CTX Md5;
42 md5_init(ctxt: &Md5);
43 md5_update(ctxt: &Md5, data: TEEWORLDS_NAMESPACE.m_aData, data_len: sizeof(TEEWORLDS_NAMESPACE.m_aData));
44 // Without terminating NUL.
45 md5_update(ctxt: &Md5, data: (const unsigned char *)pName, data_len: str_length(str: pName));
46 MD5_DIGEST Digest = md5_finish(ctxt: &Md5);
47
48 CUuid Result;
49 for(unsigned i = 0; i < sizeof(Result.m_aData); i++)
50 {
51 Result.m_aData[i] = Digest.data[i];
52 }
53
54 // set version 3 (UUID is generated by MD5 hashing a namespace identifier and a name)
55 Result.m_aData[6] &= 0x0f;
56 Result.m_aData[6] |= 0x30;
57
58 // set variant 1 (RFC 4122)
59 Result.m_aData[8] &= 0x3f;
60 Result.m_aData[8] |= 0x80;
61
62 return Result;
63}
64
65void FormatUuid(CUuid Uuid, char *pBuffer, unsigned BufferLength)
66{
67 unsigned char *p = Uuid.m_aData;
68 str_format(buffer: pBuffer, buffer_size: BufferLength,
69 format: "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
70 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
71 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
72}
73
74int ParseUuid(CUuid *pUuid, const char *pBuffer)
75{
76 if(str_length(str: pBuffer) + 1 != UUID_MAXSTRSIZE)
77 {
78 return 2;
79 }
80 char aCopy[UUID_MAXSTRSIZE];
81 str_copy(dst&: aCopy, src: pBuffer);
82 // 01234567-9012-4567-9012-456789012345
83 if(aCopy[8] != '-' || aCopy[13] != '-' || aCopy[18] != '-' || aCopy[23] != '-')
84 {
85 return 1;
86 }
87 aCopy[8] = aCopy[13] = aCopy[18] = aCopy[23] = 0;
88 if(static_cast<bool>(str_hex_decode(dst: pUuid->m_aData + 0, dst_size: 4, src: aCopy + 0)) ||
89 str_hex_decode(dst: pUuid->m_aData + 4, dst_size: 2, src: aCopy + 9) ||
90 str_hex_decode(dst: pUuid->m_aData + 6, dst_size: 2, src: aCopy + 14) ||
91 str_hex_decode(dst: pUuid->m_aData + 8, dst_size: 2, src: aCopy + 19) ||
92 str_hex_decode(dst: pUuid->m_aData + 10, dst_size: 6, src: aCopy + 24))
93 {
94 return 1;
95 }
96 return 0;
97}
98
99void UuidToInts(const CUuid &Uuid, int *pInts)
100{
101 for(size_t IntIndex = 0; IntIndex < UUID_INT_SIZE; IntIndex++)
102 {
103 pInts[IntIndex] = bytes_be_to_uint(bytes: &Uuid.m_aData[IntIndex * sizeof(int32_t)]);
104 }
105}
106
107CUuid UuidFromInts(const int *pInts)
108{
109 CUuid Uuid;
110 for(size_t IntIndex = 0; IntIndex < UUID_INT_SIZE; IntIndex++)
111 {
112 uint_to_bytes_be(bytes: &Uuid.m_aData[IntIndex * sizeof(int32_t)], value: pInts[IntIndex]);
113 }
114 return Uuid;
115}
116
117bool CUuid::operator==(const CUuid &Other) const
118{
119 return mem_comp(a: this, b: &Other, size: sizeof(*this)) == 0;
120}
121
122bool CUuid::operator!=(const CUuid &Other) const
123{
124 return !(*this == Other);
125}
126
127bool CUuid::operator<(const CUuid &Other) const
128{
129 return mem_comp(a: this, b: &Other, size: sizeof(*this)) < 0;
130}
131
132static int GetIndex(int Id)
133{
134 return Id - OFFSET_UUID;
135}
136
137static int GetId(int Index)
138{
139 return Index + OFFSET_UUID;
140}
141
142void CUuidManager::RegisterName(int Id, const char *pName)
143{
144 dbg_assert(GetIndex(Id) == (int)m_vNames.size(), "names must be registered with increasing ID (got=%d want=%d)", GetIndex(Id), (int)m_vNames.size());
145 CName Name;
146 Name.m_pName = pName;
147 Name.m_Uuid = CalculateUuid(pName);
148 dbg_assert(LookupUuid(Name.m_Uuid) == UUID_UNKNOWN, "duplicate uuid %s", Name.m_pName);
149
150 m_vNames.push_back(x: Name);
151
152 CNameIndexed NameIndexed;
153 NameIndexed.m_Uuid = Name.m_Uuid;
154 NameIndexed.m_Id = GetIndex(Id);
155 m_vNamesSorted.insert(position: std::lower_bound(first: m_vNamesSorted.begin(), last: m_vNamesSorted.end(), val: NameIndexed), x: NameIndexed);
156}
157
158CUuid CUuidManager::GetUuid(int Id) const
159{
160 return m_vNames[GetIndex(Id)].m_Uuid;
161}
162
163const char *CUuidManager::GetName(int Id) const
164{
165 return m_vNames[GetIndex(Id)].m_pName;
166}
167
168int CUuidManager::LookupUuid(CUuid Uuid) const
169{
170 CNameIndexed Needle;
171 Needle.m_Uuid = Uuid;
172 Needle.m_Id = 0;
173 auto Range = std::equal_range(first: m_vNamesSorted.begin(), last: m_vNamesSorted.end(), val: Needle);
174 if(std::distance(first: Range.first, last: Range.second) == 1)
175 {
176 return GetId(Index: Range.first->m_Id);
177 }
178 return UUID_UNKNOWN;
179}
180
181int CUuidManager::NumUuids() const
182{
183 return m_vNames.size();
184}
185
186int CUuidManager::UnpackUuid(CUnpacker *pUnpacker) const
187{
188 CUuid Temp;
189 return UnpackUuid(pUnpacker, pOut: &Temp);
190}
191
192int CUuidManager::UnpackUuid(CUnpacker *pUnpacker, CUuid *pOut) const
193{
194 const CUuid *pUuid = (const CUuid *)pUnpacker->GetRaw(Size: sizeof(*pUuid));
195 if(pUuid == nullptr)
196 {
197 return UUID_INVALID;
198 }
199 *pOut = *pUuid;
200 return LookupUuid(Uuid: *pUuid);
201}
202
203void CUuidManager::PackUuid(int Id, CPacker *pPacker) const
204{
205 CUuid Uuid = GetUuid(Id);
206 pPacker->AddRaw(pData: &Uuid, Size: sizeof(Uuid));
207}
208
209void CUuidManager::DebugDump() const
210{
211 for(const auto &Name : m_vNames)
212 {
213 char aBuf[UUID_MAXSTRSIZE];
214 FormatUuid(Uuid: Name.m_Uuid, pBuffer: aBuf, BufferLength: sizeof(aBuf));
215 dbg_msg(sys: "uuid", fmt: "%s %s", aBuf, Name.m_pName);
216 }
217}
218