| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "../libnetdata.h" |
| 4 | |
| 5 | ND_UUID UUID_generate_from_hash(const void *payload, size_t payload_len) { |
| 6 | assert(sizeof(XXH128_hash_t) == sizeof(ND_UUID)); |
| 7 | |
| 8 | ND_UUID uuid = UUID_ZERO; |
| 9 | XXH128_hash_t *xxh3_128 = (XXH128_hash_t *)&uuid; |
| 10 | |
| 11 | // Hash the payload using XXH128 |
| 12 | // Assume xxh128_hash_function is your function to generate XXH128 hash |
| 13 | *xxh3_128 = XXH3_128bits(payload, payload_len); |
| 14 | |
| 15 | // Set the UUID version (here, setting it to 4) |
| 16 | uuid.uuid[6] = (uuid.uuid[6] & 0x0F) | 0x40; // Version 4 |
| 17 | |
| 18 | // Set the UUID variant (standard variant for UUID) |
| 19 | uuid.uuid[8] = (uuid.uuid[8] & 0x3F) | 0x80; // Variant is 10xxxxxx |
| 20 | |
| 21 | return uuid; |
| 22 | } |
| 23 | |
| 24 | void uuid_unparse_lower_compact(const nd_uuid_t uuid, char *out) { |
| 25 | static const char *hex_chars = "0123456789abcdef"; |
| 26 | for (int i = 0; i < 16; i++) { |
| 27 | out[i * 2] = hex_chars[(uuid[i] >> 4) & 0x0F]; |
| 28 | out[i * 2 + 1] = hex_chars[uuid[i] & 0x0F]; |
| 29 | } |
| 30 | out[32] = '\0'; // Null-terminate the string |
| 31 | } |
| 32 | |
| 33 | static inline void nd_uuid_unparse_full(const nd_uuid_t uuid, char *out, const char *hex_chars) { |
| 34 | int shifts = 0; |
| 35 | for (int i = 0; i < 16; i++) { |
| 36 | if (i == 4 || i == 6 || i == 8 || i == 10) { |
| 37 | out[i * 2 + shifts] = '-'; |
| 38 | shifts++; |
| 39 | } |
| 40 | out[i * 2 + shifts] = hex_chars[(uuid[i] >> 4) & 0x0F]; |
| 41 | out[i * 2 + 1 + shifts] = hex_chars[uuid[i] & 0x0F]; |
| 42 | } |
| 43 | out[36] = '\0'; // Null-terminate the string |
| 44 | } |
| 45 | |
| 46 | // Wrapper functions for lower and upper case hexadecimal representation |
| 47 | void nd_uuid_unparse_lower(const nd_uuid_t uuid, char *out) { |
| 48 | nd_uuid_unparse_full(uuid, out, "0123456789abcdef"); |
| 49 | } |
| 50 | |
| 51 | void nd_uuid_unparse_upper(const nd_uuid_t uuid, char *out) { |
| 52 | nd_uuid_unparse_full(uuid, out, "0123456789ABCDEF"); |
| 53 | } |
| 54 | |
| 55 | inline int uuid_parse_compact(const char *in, nd_uuid_t uuid) { |
| 56 | if (strlen(in) != 32) |
| 57 | return -1; // Invalid input length |
| 58 | |
| 59 | for (int i = 0; i < 16; i++) { |
| 60 | int high = hex_char_to_int(in[i * 2]); |
| 61 | int low = hex_char_to_int(in[i * 2 + 1]); |
| 62 | |
| 63 | if (high < 0 || low < 0) |
| 64 | return -1; // Invalid hexadecimal character |
| 65 | |
| 66 | uuid[i] = (high << 4) | low; |
| 67 | } |
| 68 | |
| 69 | return 0; // Success |
| 70 | } |
| 71 | |
| 72 | int uuid_parse_flexi(const char *in, nd_uuid_t uu) { |
| 73 | if(!in || !*in) |
| 74 | return -1; |
| 75 | |
| 76 | size_t hexCharCount = 0; |
| 77 | size_t hyphenCount = 0; |
| 78 | const char *s = in; |
| 79 | int byteIndex = 0; |
| 80 | nd_uuid_t uuid; // work on a temporary place, to not corrupt the previous value of uu if we fail |
| 81 | |
| 82 | while (*s && byteIndex < 16) { |
| 83 | if (*s == '-') { |
| 84 | s++; |
| 85 | hyphenCount++; |
| 86 | |
| 87 | if (unlikely(hyphenCount > 4)) |
| 88 | // Too many hyphens |
| 89 | return -2; |
| 90 | } |
| 91 | |
| 92 | if (likely(isxdigit((uint8_t)*s))) { |
| 93 | int high = hex_char_to_int(*s++); |
| 94 | hexCharCount++; |
| 95 | |
| 96 | if (likely(isxdigit((uint8_t)*s))) { |
| 97 | int low = hex_char_to_int(*s++); |
| 98 | hexCharCount++; |
| 99 | |
| 100 | uuid[byteIndex++] = (high << 4) | low; |
| 101 | } |
| 102 | else |
| 103 | // Not a valid UUID (expected a pair of hex digits) |
| 104 | return -3; |
| 105 | } |
| 106 | else |
| 107 | // Not a valid UUID |
| 108 | return -4; |
| 109 | } |
| 110 | |
| 111 | if (unlikely(byteIndex < 16)) |
| 112 | // Not enough data to form a UUID |
| 113 | return -5; |
| 114 | |
| 115 | if (unlikely(hexCharCount != 32)) |
| 116 | // wrong number of hex digits |
| 117 | return -6; |
| 118 | |
| 119 | if(unlikely(hyphenCount != 0 && hyphenCount != 4)) |
| 120 | // wrong number of hyphens |
| 121 | return -7; |
| 122 | |
| 123 | // copy the final value |
| 124 | memcpy(uu, uuid, sizeof(nd_uuid_t)); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | // ---------------------------------------------------------------------------- |
| 131 | // unit test |
| 132 | |
| 133 | static inline void remove_hyphens(const char *uuid_with_hyphens, char *uuid_without_hyphens) { |
| 134 | while (*uuid_with_hyphens) { |
| 135 | if (*uuid_with_hyphens != '-') { |
| 136 | *uuid_without_hyphens++ = *uuid_with_hyphens; |
| 137 | } |
| 138 | uuid_with_hyphens++; |
| 139 | } |
| 140 | *uuid_without_hyphens = '\0'; |
| 141 | } |
| 142 | |
| 143 | int uuid_unittest(void) { |
| 144 | const int num_tests = 100000; |
| 145 | int failed_tests = 0; |
| 146 | |
| 147 | int i; |
| 148 | for (i = 0; i < num_tests; i++) { |
| 149 | nd_uuid_t original_uuid, parsed_uuid; |
| 150 | char uuid_str_with_hyphens[UUID_STR_LEN], uuid_str_without_hyphens[UUID_COMPACT_STR_LEN]; |
| 151 | |
| 152 | // Generate a random UUID |
| 153 | switch(i % 2) { |
| 154 | case 0: |
| 155 | uuid_generate(original_uuid); |
| 156 | break; |
| 157 | |
| 158 | case 1: |
| 159 | uuid_generate_random(original_uuid); |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | // Unparse it with hyphens |
| 164 | bool lower = false; |
| 165 | switch(i % 3) { |
| 166 | case 0: |
| 167 | uuid_unparse_lower(original_uuid, uuid_str_with_hyphens); |
| 168 | lower = true; |
| 169 | break; |
| 170 | |
| 171 | case 1: |
| 172 | uuid_unparse(original_uuid, uuid_str_with_hyphens); |
| 173 | break; |
| 174 | |
| 175 | case 2: |
| 176 | uuid_unparse_upper(original_uuid, uuid_str_with_hyphens); |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | // Remove the hyphens |
| 181 | remove_hyphens(uuid_str_with_hyphens, uuid_str_without_hyphens); |
| 182 | |
| 183 | if(lower) { |
| 184 | char test[UUID_COMPACT_STR_LEN]; |
| 185 | uuid_unparse_lower_compact(original_uuid, test); |
| 186 | if(strcmp(test, uuid_str_without_hyphens) != 0) { |
| 187 | printf("uuid_unparse_lower_compact() failed, expected '%s', got '%s'\n", |
| 188 | uuid_str_without_hyphens, test); |
| 189 | failed_tests++; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Parse the UUID string with hyphens |
| 194 | int parse_result = uuid_parse_flexi(uuid_str_with_hyphens, parsed_uuid); |
| 195 | if (parse_result != 0) { |
| 196 | printf("uuid_parse_flexi() returned -1 (parsing error) for UUID with hyphens: %s\n", uuid_str_with_hyphens); |
| 197 | failed_tests++; |
| 198 | } else if (uuid_compare(original_uuid, parsed_uuid) != 0) { |
| 199 | printf("uuid_parse_flexi() parsed value mismatch for UUID with hyphens: %s\n", uuid_str_with_hyphens); |
| 200 | failed_tests++; |
| 201 | } |
| 202 | |
| 203 | // Parse the UUID string without hyphens |
| 204 | parse_result = uuid_parse_flexi(uuid_str_without_hyphens, parsed_uuid); |
| 205 | if (parse_result != 0) { |
| 206 | printf("uuid_parse_flexi() returned -1 (parsing error) for UUID without hyphens: %s\n", uuid_str_without_hyphens); |
| 207 | failed_tests++; |
| 208 | } |
| 209 | else if(uuid_compare(original_uuid, parsed_uuid) != 0) { |
| 210 | printf("uuid_parse_flexi() parsed value mismatch for UUID without hyphens: %s\n", uuid_str_without_hyphens); |
| 211 | failed_tests++; |
| 212 | } |
| 213 | |
| 214 | if(failed_tests) |
| 215 | break; |
| 216 | } |
| 217 | |
| 218 | printf("UUID: failed %d out of %d tests.\n", failed_tests, i); |
| 219 | return failed_tests; |
| 220 | } |