Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 1 | /* |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 2 | * Copyright (c) 2020-2023, Arm Limited and Contributors. All rights reserved. |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include "uuid.h" |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 8 | #include <assert.h> |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 9 | #include <string.h> |
| 10 | #include <ctype.h> |
Gabor Toth | 7728fad | 2024-08-06 13:53:57 +0200 | [diff] [blame] | 11 | #include <compiler.h> |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 12 | |
| 13 | static uint8_t hex_to_nibble(char hex) |
| 14 | { |
| 15 | uint8_t nibble = 0; |
| 16 | |
| 17 | if (hex >= '0' && hex <= '9') { |
| 18 | nibble = hex - '0'; |
| 19 | } |
| 20 | else { |
| 21 | nibble = ((hex | 0x20) - 'a') + 10; |
| 22 | } |
| 23 | |
| 24 | return nibble; |
| 25 | } |
| 26 | |
Julian Hall | 1df937d | 2023-03-21 10:34:36 +0000 | [diff] [blame] | 27 | static char nibble_to_hex(uint8_t nibble) |
| 28 | { |
| 29 | char hex; |
| 30 | |
| 31 | nibble &= 0x0f; |
| 32 | |
| 33 | if (nibble <= 9) |
| 34 | hex = '0' + nibble; |
| 35 | else |
| 36 | hex = 'a' + nibble - 10; |
| 37 | |
| 38 | return hex; |
| 39 | } |
| 40 | |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 41 | static uint8_t hex_to_byte(const char *hex) |
| 42 | { |
| 43 | /* Takes a validated input and returns the byte value */ |
| 44 | uint8_t byte = hex_to_nibble(hex[0]) << 4; |
| 45 | byte |= (hex_to_nibble(hex[1]) & 0x0f); |
| 46 | return byte; |
| 47 | } |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 48 | |
| 49 | size_t uuid_is_valid(const char *canonical_form) |
| 50 | { |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 51 | size_t valid_chars = 0; |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 52 | |
Julian Hall | 1df937d | 2023-03-21 10:34:36 +0000 | [diff] [blame] | 53 | /* Note that a valid canonical uuid may be part of a longer string |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 54 | * such as a urn. |
| 55 | */ |
Gabor Toth | 983264f | 2024-01-23 09:16:24 +0100 | [diff] [blame] | 56 | if (!memchr(canonical_form, '\0', UUID_CANONICAL_FORM_LEN)) { |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 57 | size_t i; |
| 58 | valid_chars = UUID_CANONICAL_FORM_LEN; |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 59 | |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 60 | for (i = 0; i < UUID_CANONICAL_FORM_LEN; ++i) { |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 61 | |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 62 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 63 | if (canonical_form[i] != '-') return 0; |
| 64 | } |
| 65 | else { |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 66 | if (!isxdigit((int)canonical_form[i])) return 0; |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 70 | |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 71 | return valid_chars; |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 74 | bool uuid_is_equal(const uint8_t *octets_a, const uint8_t *octets_b) |
| 75 | { |
| 76 | return memcmp(octets_a, octets_b, UUID_OCTETS_LEN) == 0; |
| 77 | } |
| 78 | |
| 79 | bool uuid_is_nil(const uint8_t *octets) |
| 80 | { |
| 81 | return memcmp(uuid_get_nil()->octets, octets, UUID_OCTETS_LEN) == 0; |
| 82 | } |
| 83 | |
| 84 | const struct uuid_octets *uuid_get_nil(void) |
| 85 | { |
| 86 | static const struct uuid_octets nil_uuid = {0}; |
| 87 | |
| 88 | return &nil_uuid; |
| 89 | } |
| 90 | |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 91 | size_t uuid_parse_to_octets(const char *canonical_form, uint8_t *buf, size_t buf_size) |
| 92 | { |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 93 | size_t octet_index = 0; |
| 94 | const char *pos; |
| 95 | size_t valid_chars = uuid_is_valid(canonical_form); |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 96 | |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 97 | if ((buf_size < UUID_OCTETS_LEN) || |
| 98 | (valid_chars != UUID_CANONICAL_FORM_LEN)) { |
| 99 | /* Invalid input */ |
| 100 | return 0; |
| 101 | } |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 102 | |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 103 | /* |
| 104 | * UUID string has been validates as having the following form: |
| 105 | * xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx |
| 106 | * 4 2 2 2 6 |
| 107 | */ |
| 108 | pos = &canonical_form[0]; |
| 109 | while (octet_index < 4) { |
| 110 | buf[octet_index++] = hex_to_byte(pos); |
| 111 | pos += 2; |
| 112 | } |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 113 | |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 114 | pos = &canonical_form[9]; |
| 115 | while (octet_index < 6) { |
| 116 | buf[octet_index++] = hex_to_byte(pos); |
| 117 | pos += 2; |
| 118 | } |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 119 | |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 120 | pos = &canonical_form[14]; |
| 121 | while (octet_index < 8) { |
| 122 | buf[octet_index++] = hex_to_byte(pos); |
| 123 | pos += 2; |
| 124 | } |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 125 | |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 126 | pos = &canonical_form[19]; |
| 127 | while (octet_index < 10) { |
| 128 | buf[octet_index++] = hex_to_byte(pos); |
| 129 | pos += 2; |
| 130 | } |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 131 | |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 132 | pos = &canonical_form[24]; |
| 133 | while (octet_index < 16) { |
| 134 | buf[octet_index++] = hex_to_byte(pos); |
| 135 | pos += 2; |
| 136 | } |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 137 | |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 138 | return valid_chars; |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 142 | * The byte order is reversed for the integer sections of the UUID. Converts |
| 143 | * from standard to GUID octet representations an visa versa. |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 144 | */ |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 145 | void uuid_reverse_octets(const struct uuid_octets *input_octets, |
Julian Hall | 7c2ae0c | 2022-09-29 08:27:07 +0100 | [diff] [blame] | 146 | uint8_t *buf, size_t buf_size) |
| 147 | { |
| 148 | if (buf_size >= UUID_OCTETS_LEN) { |
| 149 | /* Reverse bytes in each section */ |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 150 | buf[0] = input_octets->octets[3]; |
| 151 | buf[1] = input_octets->octets[2]; |
| 152 | buf[2] = input_octets->octets[1]; |
| 153 | buf[3] = input_octets->octets[0]; |
Julian Hall | 7c2ae0c | 2022-09-29 08:27:07 +0100 | [diff] [blame] | 154 | |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 155 | buf[4] = input_octets->octets[5]; |
| 156 | buf[5] = input_octets->octets[4]; |
Julian Hall | 7c2ae0c | 2022-09-29 08:27:07 +0100 | [diff] [blame] | 157 | |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 158 | buf[6] = input_octets->octets[7]; |
| 159 | buf[7] = input_octets->octets[6]; |
Julian Hall | 7c2ae0c | 2022-09-29 08:27:07 +0100 | [diff] [blame] | 160 | |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 161 | buf[8] = input_octets->octets[8]; |
| 162 | buf[9] = input_octets->octets[9]; |
Julian Hall | 7c2ae0c | 2022-09-29 08:27:07 +0100 | [diff] [blame] | 163 | |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 164 | buf[10] = input_octets->octets[10]; |
| 165 | buf[11] = input_octets->octets[11]; |
| 166 | buf[12] = input_octets->octets[12]; |
| 167 | buf[13] = input_octets->octets[13]; |
| 168 | buf[14] = input_octets->octets[14]; |
| 169 | buf[15] = input_octets->octets[15]; |
Julian Hall | 7c2ae0c | 2022-09-29 08:27:07 +0100 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 173 | size_t uuid_parse_to_guid_octets(const char *canonical_form, |
Julian Hall | 7c2ae0c | 2022-09-29 08:27:07 +0100 | [diff] [blame] | 174 | uint8_t *buf, size_t buf_size) |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 175 | { |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 176 | size_t valid_chars; |
Julian Hall | 7c2ae0c | 2022-09-29 08:27:07 +0100 | [diff] [blame] | 177 | struct uuid_octets standard_encoding; |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 178 | |
Julian Hall | 7c2ae0c | 2022-09-29 08:27:07 +0100 | [diff] [blame] | 179 | valid_chars = uuid_parse_to_octets(canonical_form, |
| 180 | standard_encoding.octets, sizeof(standard_encoding.octets)); |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 181 | |
Julian Hall | 7c2ae0c | 2022-09-29 08:27:07 +0100 | [diff] [blame] | 182 | if (valid_chars == UUID_CANONICAL_FORM_LEN) { |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 183 | |
Julian Hall | 7c2ae0c | 2022-09-29 08:27:07 +0100 | [diff] [blame] | 184 | uuid_reverse_octets(&standard_encoding, buf, buf_size); |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 185 | } |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 186 | |
Julian Hall | 7d7b24c | 2021-08-13 13:40:38 +0100 | [diff] [blame] | 187 | return valid_chars; |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 188 | } |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 189 | |
| 190 | void uuid_octets_from_canonical(struct uuid_octets *uuid_octets, |
| 191 | const char *canonical_form) |
| 192 | { |
Gabor Toth | 7728fad | 2024-08-06 13:53:57 +0200 | [diff] [blame] | 193 | __maybe_unused size_t valid_chars = uuid_parse_to_octets(canonical_form, |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 194 | uuid_octets->octets, sizeof(uuid_octets->octets)); |
| 195 | |
| 196 | /* Input string is assumed to be valid. Should not be used if canonical |
| 197 | * string originates from an untrusted source. |
| 198 | */ |
| 199 | assert(valid_chars == UUID_CANONICAL_FORM_LEN); |
| 200 | } |
| 201 | |
| 202 | void uuid_guid_octets_from_canonical(struct uuid_octets *uuid_octets, |
| 203 | const char *canonical_form) |
| 204 | { |
Gabor Toth | 7728fad | 2024-08-06 13:53:57 +0200 | [diff] [blame] | 205 | __maybe_unused size_t valid_chars = uuid_parse_to_guid_octets(canonical_form, |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 206 | uuid_octets->octets, sizeof(uuid_octets->octets)); |
| 207 | |
| 208 | assert(valid_chars == UUID_CANONICAL_FORM_LEN); |
| 209 | } |
Julian Hall | 1df937d | 2023-03-21 10:34:36 +0000 | [diff] [blame] | 210 | |
| 211 | void uuid_canonical_from_octets(struct uuid_canonical *canonical_form, |
| 212 | const struct uuid_octets *uuid_octets) |
| 213 | { |
| 214 | unsigned int octet_index = 0; |
| 215 | unsigned int char_index = 0; |
| 216 | |
| 217 | while (octet_index < UUID_OCTETS_LEN) { |
| 218 | |
| 219 | canonical_form->characters[char_index++] = |
| 220 | nibble_to_hex(uuid_octets->octets[octet_index] >> 4); |
| 221 | |
| 222 | canonical_form->characters[char_index++] = |
| 223 | nibble_to_hex(uuid_octets->octets[octet_index] & 0x0f); |
| 224 | |
| 225 | ++octet_index; |
| 226 | |
| 227 | if ((octet_index == 4) || |
| 228 | (octet_index == 6) || |
| 229 | (octet_index == 8) || |
| 230 | (octet_index == 10)) |
| 231 | canonical_form->characters[char_index++] = '-'; |
| 232 | } |
| 233 | |
| 234 | canonical_form->characters[char_index] = '\0'; |
| 235 | } |
| 236 | |
| 237 | void uuid_canonical_from_guid_octets(struct uuid_canonical *canonical_form, |
| 238 | const struct uuid_octets *uuid_octets) |
| 239 | { |
| 240 | struct uuid_octets reversed_octets; |
| 241 | |
| 242 | uuid_reverse_octets(uuid_octets, reversed_octets.octets, sizeof(reversed_octets.octets)); |
| 243 | uuid_canonical_from_octets(canonical_form, &reversed_octets); |
| 244 | } |