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