xref: /spdk/include/spdk/uuid.h (revision 65b4e17c6736ae69784017a5d5557443b6997899)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2018 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 /** \file
7  * UUID types and functions
8  */
9 
10 #ifndef SPDK_UUID_H
11 #define SPDK_UUID_H
12 
13 #include "spdk/stdinc.h"
14 
15 #include "spdk/assert.h"
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 struct spdk_uuid {
22 	union {
23 		uint8_t raw[16];
24 	} u;
25 };
26 SPDK_STATIC_ASSERT(sizeof(struct spdk_uuid) == 16, "Incorrect size");
27 
28 #define SPDK_UUID_STRING_LEN 37 /* 36 characters + null terminator */
29 
30 /**
31  * Convert UUID in textual format into a spdk_uuid.
32  *
33  * \param[out] uuid User-provided UUID buffer.
34  * \param uuid_str UUID in textual format in C string.
35  *
36  * \return 0 on success, or negative errno on failure.
37  */
38 int spdk_uuid_parse(struct spdk_uuid *uuid, const char *uuid_str);
39 
40 /**
41  * Convert UUID in spdk_uuid into lowercase textual format.
42  *
43  * \param uuid_str User-provided string buffer to write the textual format into.
44  * \param uuid_str_size Size of uuid_str buffer. Must be at least SPDK_UUID_STRING_LEN.
45  * \param uuid UUID to convert to textual format.
46  *
47  * \return 0 on success, or negative errno on failure.
48  */
49 int spdk_uuid_fmt_lower(char *uuid_str, size_t uuid_str_size, const struct spdk_uuid *uuid);
50 
51 /**
52  * Compare two UUIDs.
53  *
54  * \param u1 UUID 1.
55  * \param u2 UUID 2.
56  *
57  * \return 0 if u1 == u2, less than 0 if u1 < u2, greater than 0 if u1 > u2.
58  */
59 int spdk_uuid_compare(const struct spdk_uuid *u1, const struct spdk_uuid *u2);
60 
61 /**
62  * Generate a new UUID.
63  *
64  * \param[out] uuid User-provided UUID buffer to fill.
65  */
66 void spdk_uuid_generate(struct spdk_uuid *uuid);
67 
68 /**
69  * Generate a new UUID using SHA1 hash.
70  *
71  * \param[out] uuid User-provided UUID buffer to fill.
72  * \param ns_uuid Well-known namespace UUID for generated UUID.
73  * \param name Arbitrary, binary string.
74  * \param len Length of binary string.
75  *
76  * \return 0 on success, or negative errno on failure.
77  */
78 int spdk_uuid_generate_sha1(struct spdk_uuid *uuid, struct spdk_uuid *ns_uuid, const char *name,
79 			    size_t len);
80 
81 /**
82  * Copy a UUID.
83  *
84  * \param src Source UUID to copy from.
85  * \param dst Destination UUID to store.
86  */
87 void spdk_uuid_copy(struct spdk_uuid *dst, const struct spdk_uuid *src);
88 
89 /**
90  * Compare the UUID to the NULL value (all bits equal to zero).
91  *
92  * \param uuid The UUID to test.
93  *
94  * \return true if uuid is equal to the NULL value, false if not.
95  */
96 bool spdk_uuid_is_null(const struct spdk_uuid *uuid);
97 
98 /**
99  * Set the value of UUID to the NULL value.
100  *
101  * \param uuid The UUID to set.
102  */
103 void spdk_uuid_set_null(struct spdk_uuid *uuid);
104 
105 #ifdef __cplusplus
106 }
107 #endif
108 
109 #endif
110