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 | |
Julian Hall | 93df76a | 2022-09-22 17:30:06 +0100 | [diff] [blame] | 7 | #ifndef COMMON_UUID_H |
| 8 | #define COMMON_UUID_H |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 9 | |
| 10 | #include <stddef.h> |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 11 | #include <stdbool.h> |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 12 | #include <stdint.h> |
| 13 | |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 14 | #define UUID_OCTETS_LEN (16) |
| 15 | #define UUID_CANONICAL_FORM_LEN (36) |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 16 | |
| 17 | |
| 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
| 22 | /* |
| 23 | * Structure for holding an octet representation of a UUID. |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 24 | * |
| 25 | * There are two common byte orderings for octet representations of UUIDs. |
| 26 | * RFC4122 specifies that integer fields from the UUID data structure |
| 27 | * should be presented with most significant bytes first. An alternative |
| 28 | * byte ordering, used by UEFI, presents integer values with the reverse |
| 29 | * byte order (little Endian). The UEFI specification uses the GUID rather |
| 30 | * than UUID acronym. To distinguish between the two octet representations, |
| 31 | * functions that assume the little Endian octet encoding include 'guid' in |
| 32 | * the function name. |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 33 | */ |
| 34 | struct uuid_octets |
| 35 | { |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 36 | uint8_t octets[UUID_OCTETS_LEN]; |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | /* |
| 40 | * Structure for holding an canonical string representation of a UUID. |
| 41 | */ |
| 42 | struct uuid_canonical |
| 43 | { |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 44 | char characters[UUID_CANONICAL_FORM_LEN + 1]; |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | /* |
Julian Hall | 7c2ae0c | 2022-09-29 08:27:07 +0100 | [diff] [blame] | 48 | * Check if uuid string in canonical form is valid. Returns the number of |
| 49 | * valid characters. This will either be UUID_CANONICAL_FORM_LEN or zero |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 50 | * if the string is invalid in some way. |
| 51 | */ |
| 52 | size_t uuid_is_valid(const char *canonical_form); |
| 53 | |
| 54 | /* |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 55 | * Check if two octet uuid representations are equal |
| 56 | */ |
| 57 | bool uuid_is_equal(const uint8_t *octets_a, const uint8_t *octets_b); |
| 58 | |
| 59 | /* |
| 60 | * Check if octets represent a nil uuid |
| 61 | */ |
| 62 | bool uuid_is_nil(const uint8_t *octets); |
| 63 | |
| 64 | /* |
| 65 | * Return a const nil uuid |
| 66 | */ |
| 67 | const struct uuid_octets *uuid_get_nil(void); |
| 68 | |
| 69 | /* |
Julian Hall | 7c2ae0c | 2022-09-29 08:27:07 +0100 | [diff] [blame] | 70 | * Parses a uuid string in canonical string form, outputting as an array of bytes |
| 71 | * in the standard big endian byte order. Returns the number of characters parsed |
| 72 | * from the input string. Returns zero if there is a parsing error. |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 73 | */ |
Julian Hall | 7c2ae0c | 2022-09-29 08:27:07 +0100 | [diff] [blame] | 74 | size_t uuid_parse_to_octets(const char *canonical_form, |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 75 | uint8_t *buf, size_t buf_size); |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 76 | |
| 77 | /* |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 78 | * Parses a uuid string in canonical string form, outputting as an array of bytes |
| 79 | * in little endian byte order (GUID order). |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 80 | */ |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 81 | size_t uuid_parse_to_guid_octets(const char *canonical_form, |
| 82 | uint8_t *buf, size_t buf_size); |
Julian Hall | 7c2ae0c | 2022-09-29 08:27:07 +0100 | [diff] [blame] | 83 | |
| 84 | /* |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 85 | * Convert from one octet representation to the other. Works both ways. |
Julian Hall | 7c2ae0c | 2022-09-29 08:27:07 +0100 | [diff] [blame] | 86 | */ |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 87 | void uuid_reverse_octets(const struct uuid_octets *input_octets, |
| 88 | uint8_t *buf, size_t buf_size); |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 89 | |
Julian Hall | 31d4fdd | 2022-12-01 16:52:01 +0000 | [diff] [blame] | 90 | /* |
| 91 | * Converts a valid canonical uuid string to the standard octet byte order. Should only |
| 92 | * be used if the input canonical string is trusted to be valid. Will assert if it's not. |
| 93 | */ |
| 94 | void uuid_octets_from_canonical(struct uuid_octets *uuid_octets, |
| 95 | const char *canonical_form); |
| 96 | |
| 97 | /* |
| 98 | * Converts a valid canonical uuid string to the GUID octet byte order. Should only |
| 99 | * be used if the input canonical string is trusted to be valid. Will assert if it's not. |
| 100 | */ |
| 101 | void uuid_guid_octets_from_canonical(struct uuid_octets *uuid_octets, |
| 102 | const char *canonical_form); |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 103 | |
Julian Hall | 1df937d | 2023-03-21 10:34:36 +0000 | [diff] [blame] | 104 | /* |
| 105 | * Converts an octet representation in standard byte order to a canonical string. |
| 106 | */ |
| 107 | void uuid_canonical_from_octets(struct uuid_canonical *canonical_form, |
| 108 | const struct uuid_octets *uuid_octets); |
| 109 | |
| 110 | /* |
| 111 | * Converts an octet representation in GUID byte order to a canonical string. |
| 112 | */ |
| 113 | void uuid_canonical_from_guid_octets(struct uuid_canonical *canonical_form, |
| 114 | const struct uuid_octets *uuid_octets); |
| 115 | |
| 116 | |
Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame] | 117 | #ifdef __cplusplus |
| 118 | } |
| 119 | #endif |
| 120 | |
Julian Hall | 93df76a | 2022-09-22 17:30:06 +0100 | [diff] [blame] | 121 | #endif /* COMMON_UUID_H */ |