xref: /dpdk/lib/eal/include/rte_uuid.h (revision 719834a6849e1daf4a70ff7742bbcc3ae7e25607)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright (C) 1996, 1997, 1998 Theodore Ts'o.
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson /**
599a2dd95SBruce Richardson  * @file
699a2dd95SBruce Richardson  *
799a2dd95SBruce Richardson  * UUID related functions originally from libuuid
899a2dd95SBruce Richardson  */
999a2dd95SBruce Richardson 
1099a2dd95SBruce Richardson #ifndef _RTE_UUID_H_
1199a2dd95SBruce Richardson #define _RTE_UUID_H_
1299a2dd95SBruce Richardson 
1399a2dd95SBruce Richardson #include <stdbool.h>
1499a2dd95SBruce Richardson #include <stddef.h>
1599a2dd95SBruce Richardson #include <string.h>
1699a2dd95SBruce Richardson 
17*719834a6SMattias Rönnblom #ifdef __cplusplus
18*719834a6SMattias Rönnblom extern "C" {
19*719834a6SMattias Rönnblom #endif
20*719834a6SMattias Rönnblom 
2199a2dd95SBruce Richardson /**
2299a2dd95SBruce Richardson  * Struct describing a Universal Unique Identifier
2399a2dd95SBruce Richardson  */
2499a2dd95SBruce Richardson typedef unsigned char rte_uuid_t[16];
2599a2dd95SBruce Richardson 
2699a2dd95SBruce Richardson /**
2799a2dd95SBruce Richardson  * Helper for defining UUID values for id tables.
2899a2dd95SBruce Richardson  */
2999a2dd95SBruce Richardson #define RTE_UUID_INIT(a, b, c, d, e) {		\
3099a2dd95SBruce Richardson 	((a) >> 24) & 0xff, ((a) >> 16) & 0xff,	\
3199a2dd95SBruce Richardson 	((a) >> 8) & 0xff, (a) & 0xff,		\
3299a2dd95SBruce Richardson 	((b) >> 8) & 0xff, (b) & 0xff,		\
3399a2dd95SBruce Richardson 	((c) >> 8) & 0xff, (c) & 0xff,		\
3499a2dd95SBruce Richardson 	((d) >> 8) & 0xff, (d) & 0xff,		\
3599a2dd95SBruce Richardson 	((e) >> 40) & 0xff, ((e) >> 32) & 0xff, \
3699a2dd95SBruce Richardson 	((e) >> 24) & 0xff, ((e) >> 16) & 0xff, \
3799a2dd95SBruce Richardson 	((e) >> 8) & 0xff, (e) & 0xff		\
3899a2dd95SBruce Richardson }
3999a2dd95SBruce Richardson 
4061c7dfe7SJerin Jacob /** UUID string length */
4161c7dfe7SJerin Jacob #define RTE_UUID_STRLEN	(36 + 1)
4261c7dfe7SJerin Jacob 
4399a2dd95SBruce Richardson /**
4499a2dd95SBruce Richardson  * Test if UUID is all zeros.
4599a2dd95SBruce Richardson  *
4699a2dd95SBruce Richardson  * @param uu
4799a2dd95SBruce Richardson  *    The uuid to check.
4899a2dd95SBruce Richardson  * @return
4999a2dd95SBruce Richardson  *    true if uuid is NULL value, false otherwise
5099a2dd95SBruce Richardson  */
5199a2dd95SBruce Richardson bool rte_uuid_is_null(const rte_uuid_t uu);
5299a2dd95SBruce Richardson 
5399a2dd95SBruce Richardson /**
5499a2dd95SBruce Richardson  * Copy uuid.
5599a2dd95SBruce Richardson  *
5699a2dd95SBruce Richardson  * @param dst
5799a2dd95SBruce Richardson  *    Destination uuid
5899a2dd95SBruce Richardson  * @param src
5999a2dd95SBruce Richardson  *    Source uuid
6099a2dd95SBruce Richardson  */
6199a2dd95SBruce Richardson static inline void rte_uuid_copy(rte_uuid_t dst, const rte_uuid_t src)
6299a2dd95SBruce Richardson {
6399a2dd95SBruce Richardson 	memcpy(dst, src, sizeof(rte_uuid_t));
6499a2dd95SBruce Richardson }
6599a2dd95SBruce Richardson 
6699a2dd95SBruce Richardson /**
6799a2dd95SBruce Richardson  * Compare two UUID's
6899a2dd95SBruce Richardson  *
6999a2dd95SBruce Richardson  * @param a
7099a2dd95SBruce Richardson  *    A UUID to compare
7199a2dd95SBruce Richardson  * @param b
7299a2dd95SBruce Richardson  *    A UUID to compare
7399a2dd95SBruce Richardson  * @return
7499a2dd95SBruce Richardson  *   returns an integer less than, equal to, or greater than zero if UUID a is
7599a2dd95SBruce Richardson  *   is less than, equal, or greater than UUID b.
7699a2dd95SBruce Richardson  */
7799a2dd95SBruce Richardson int	rte_uuid_compare(const rte_uuid_t a, const rte_uuid_t b);
7899a2dd95SBruce Richardson 
7999a2dd95SBruce Richardson /**
8099a2dd95SBruce Richardson  * Extract UUID from string
8199a2dd95SBruce Richardson  *
8299a2dd95SBruce Richardson  * @param in
8399a2dd95SBruce Richardson  *    Pointer to string of characters to convert
8499a2dd95SBruce Richardson  * @param uu
8599a2dd95SBruce Richardson  *    Destination UUID
8699a2dd95SBruce Richardson  * @return
8799a2dd95SBruce Richardson  *    Returns 0 on success, and -1 if string is not a valid UUID.
8899a2dd95SBruce Richardson  */
8999a2dd95SBruce Richardson int	rte_uuid_parse(const char *in, rte_uuid_t uu);
9099a2dd95SBruce Richardson 
9199a2dd95SBruce Richardson /**
9299a2dd95SBruce Richardson  * Convert UUID to string
9399a2dd95SBruce Richardson  *
9499a2dd95SBruce Richardson  * @param uu
9599a2dd95SBruce Richardson  *    UUID to format
9699a2dd95SBruce Richardson  * @param out
9799a2dd95SBruce Richardson  *    Resulting string buffer
9899a2dd95SBruce Richardson  * @param len
9999a2dd95SBruce Richardson  *    Sizeof the available string buffer
10099a2dd95SBruce Richardson  */
10199a2dd95SBruce Richardson void	rte_uuid_unparse(const rte_uuid_t uu, char *out, size_t len);
10299a2dd95SBruce Richardson 
10399a2dd95SBruce Richardson #ifdef __cplusplus
10499a2dd95SBruce Richardson }
10599a2dd95SBruce Richardson #endif
10699a2dd95SBruce Richardson 
10799a2dd95SBruce Richardson #endif /* RTE_UUID_H */
108