|
| 1 | +/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ |
| 2 | +/* If you are missing that file, acquire a complete release at teeworlds.com. */ |
| 3 | +#include <base/hash_ctxt.h> |
| 4 | +#include <base/system.h> |
| 5 | + |
| 6 | +#include "uuid.h" |
| 7 | + |
| 8 | +static const Uuid TEEWORLDS_NAMESPACE = {{// "e05ddaaa-c4e6-4cfb-b642-5d48e80c0029" |
| 9 | + 0xe0, 0x5d, 0xda, 0xaa, 0xc4, 0xe6, 0x4c, 0xfb, |
| 10 | + 0xb6, 0x42, 0x5d, 0x48, 0xe8, 0x0c, 0x00, 0x29}}; |
| 11 | + |
| 12 | +const Uuid UUID_ZEROED = {{// "00000000-0000-0000-0000-000000000000" |
| 13 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 14 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}; |
| 15 | + |
| 16 | +Uuid random_uuid() |
| 17 | +{ |
| 18 | + Uuid Result; |
| 19 | + secure_random_fill(&Result, sizeof(Result)); |
| 20 | + |
| 21 | + // set version 4 (UUID is randomly generated) |
| 22 | + Result.m_aData[6] &= 0x0f; |
| 23 | + Result.m_aData[6] |= 0x40; |
| 24 | + |
| 25 | + // set variant 1 (RFC 4122) |
| 26 | + Result.m_aData[8] &= 0x3f; |
| 27 | + Result.m_aData[8] |= 0x80; |
| 28 | + |
| 29 | + return Result; |
| 30 | +} |
| 31 | + |
| 32 | +Uuid calculate_uuid(const char *name) |
| 33 | +{ |
| 34 | + MD5_CTX Md5; |
| 35 | + MD5_DIGEST Digest; |
| 36 | + Uuid Result; |
| 37 | + |
| 38 | + md5_init(&Md5); |
| 39 | + md5_update(&Md5, TEEWORLDS_NAMESPACE.m_aData, sizeof(TEEWORLDS_NAMESPACE.m_aData)); |
| 40 | + // Without terminating NUL. |
| 41 | + md5_update(&Md5, name, str_length(name)); |
| 42 | + |
| 43 | + Digest = md5_finish(&Md5); |
| 44 | + |
| 45 | + for(unsigned i = 0; i < sizeof(Result.m_aData); i++) |
| 46 | + { |
| 47 | + Result.m_aData[i] = Digest.data[i]; |
| 48 | + } |
| 49 | + |
| 50 | + // set version 3 (UUID is generated by MD5 hashing a namespace identifier and a name) |
| 51 | + Result.m_aData[6] &= 0x0f; |
| 52 | + Result.m_aData[6] |= 0x30; |
| 53 | + |
| 54 | + // set variant 1 (RFC 4122) |
| 55 | + Result.m_aData[8] &= 0x3f; |
| 56 | + Result.m_aData[8] |= 0x80; |
| 57 | + |
| 58 | + return Result; |
| 59 | +} |
| 60 | + |
| 61 | +void format_uuid(Uuid uuid, char *buffer, int size) |
| 62 | +{ |
| 63 | + unsigned char *p = uuid.m_aData; |
| 64 | + str_format(buffer, size, |
| 65 | + "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
| 66 | + p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], |
| 67 | + p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); |
| 68 | +} |
| 69 | + |
| 70 | +int uuid_comp(Uuid uuid1, Uuid uuid2) |
| 71 | +{ |
| 72 | + return mem_comp(uuid1.m_aData, uuid2.m_aData, sizeof(uuid1.m_aData)); |
| 73 | +} |
| 74 | + |
| 75 | +int ParseUuid(Uuid *uuid, const char *buffer) |
| 76 | +{ |
| 77 | + char copy[UUID_MAXSTRSIZE]; |
| 78 | + if(str_length(buffer) + 1 != UUID_MAXSTRSIZE) |
| 79 | + { |
| 80 | + return 2; |
| 81 | + } |
| 82 | + str_copy(copy, buffer, sizeof(copy)); |
| 83 | + // 01234567-9012-4567-9012-456789012345 |
| 84 | + if(copy[8] != '-' || copy[13] != '-' || copy[18] != '-' || copy[23] != '-') |
| 85 | + { |
| 86 | + return 1; |
| 87 | + } |
| 88 | + copy[8] = copy[13] = copy[18] = copy[23] = 0; |
| 89 | + if(str_hex_decode(uuid->m_aData + 0, 4, copy + 0) || |
| 90 | + str_hex_decode(uuid->m_aData + 4, 2, copy + 9) || |
| 91 | + str_hex_decode(uuid->m_aData + 6, 2, copy + 14) || |
| 92 | + str_hex_decode(uuid->m_aData + 8, 2, copy + 19) || |
| 93 | + str_hex_decode(uuid->m_aData + 10, 6, copy + 24)) |
| 94 | + { |
| 95 | + return 1; |
| 96 | + } |
| 97 | + return 0; |
| 98 | +} |
0 commit comments