blob: 4333ad2b4363029c5e26829ae858dd48b74f1e39 [file] [log] [blame]
Julian Halla7e89b02020-11-23 17:33:31 +01001/*
Julian Hall31d4fdd2022-12-01 16:52:01 +00002 * Copyright (c) 2020-2023, Arm Limited and Contributors. All rights reserved.
Julian Halla7e89b02020-11-23 17:33:31 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Julian Hall93df76a2022-09-22 17:30:06 +01007#ifndef COMMON_UUID_H
8#define COMMON_UUID_H
Julian Halla7e89b02020-11-23 17:33:31 +01009
10#include <stddef.h>
Julian Hall31d4fdd2022-12-01 16:52:01 +000011#include <stdbool.h>
Julian Halla7e89b02020-11-23 17:33:31 +010012#include <stdint.h>
13
Julian Hall31d4fdd2022-12-01 16:52:01 +000014#define UUID_OCTETS_LEN (16)
15#define UUID_CANONICAL_FORM_LEN (36)
Julian Halla7e89b02020-11-23 17:33:31 +010016
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22/*
23 * Structure for holding an octet representation of a UUID.
Julian Hall31d4fdd2022-12-01 16:52:01 +000024 *
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 Halla7e89b02020-11-23 17:33:31 +010033 */
34struct uuid_octets
35{
Julian Hall31d4fdd2022-12-01 16:52:01 +000036 uint8_t octets[UUID_OCTETS_LEN];
Julian Halla7e89b02020-11-23 17:33:31 +010037};
38
39/*
40 * Structure for holding an canonical string representation of a UUID.
41 */
42struct uuid_canonical
43{
Julian Hall31d4fdd2022-12-01 16:52:01 +000044 char characters[UUID_CANONICAL_FORM_LEN + 1];
Julian Halla7e89b02020-11-23 17:33:31 +010045};
46
47/*
Julian Hall7c2ae0c2022-09-29 08:27:07 +010048 * 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 Halla7e89b02020-11-23 17:33:31 +010050 * if the string is invalid in some way.
51 */
52size_t uuid_is_valid(const char *canonical_form);
53
54/*
Julian Hall31d4fdd2022-12-01 16:52:01 +000055 * Check if two octet uuid representations are equal
56 */
57bool uuid_is_equal(const uint8_t *octets_a, const uint8_t *octets_b);
58
59/*
60 * Check if octets represent a nil uuid
61 */
62bool uuid_is_nil(const uint8_t *octets);
63
64/*
65 * Return a const nil uuid
66 */
67const struct uuid_octets *uuid_get_nil(void);
68
69/*
Julian Hall7c2ae0c2022-09-29 08:27:07 +010070 * 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 Halla7e89b02020-11-23 17:33:31 +010073 */
Julian Hall7c2ae0c2022-09-29 08:27:07 +010074size_t uuid_parse_to_octets(const char *canonical_form,
Julian Hall31d4fdd2022-12-01 16:52:01 +000075 uint8_t *buf, size_t buf_size);
Julian Halla7e89b02020-11-23 17:33:31 +010076
77/*
Julian Hall31d4fdd2022-12-01 16:52:01 +000078 * Parses a uuid string in canonical string form, outputting as an array of bytes
79 * in little endian byte order (GUID order).
Julian Halla7e89b02020-11-23 17:33:31 +010080 */
Julian Hall31d4fdd2022-12-01 16:52:01 +000081size_t uuid_parse_to_guid_octets(const char *canonical_form,
82 uint8_t *buf, size_t buf_size);
Julian Hall7c2ae0c2022-09-29 08:27:07 +010083
84/*
Julian Hall31d4fdd2022-12-01 16:52:01 +000085 * Convert from one octet representation to the other. Works both ways.
Julian Hall7c2ae0c2022-09-29 08:27:07 +010086 */
Julian Hall31d4fdd2022-12-01 16:52:01 +000087void uuid_reverse_octets(const struct uuid_octets *input_octets,
88 uint8_t *buf, size_t buf_size);
Julian Halla7e89b02020-11-23 17:33:31 +010089
Julian Hall31d4fdd2022-12-01 16:52:01 +000090/*
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 */
94void 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 */
101void uuid_guid_octets_from_canonical(struct uuid_octets *uuid_octets,
102 const char *canonical_form);
Julian Halla7e89b02020-11-23 17:33:31 +0100103
Julian Hall1df937d2023-03-21 10:34:36 +0000104/*
105 * Converts an octet representation in standard byte order to a canonical string.
106 */
107void 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 */
113void uuid_canonical_from_guid_octets(struct uuid_canonical *canonical_form,
114 const struct uuid_octets *uuid_octets);
115
116
Julian Halla7e89b02020-11-23 17:33:31 +0100117#ifdef __cplusplus
118}
119#endif
120
Julian Hall93df76a2022-09-22 17:30:06 +0100121#endif /* COMMON_UUID_H */