New upstream version 18.08
[deb_dpdk.git] / lib / librte_eal / common / include / rte_uuid.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (C) 1996, 1997, 1998 Theodore Ts'o.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, and the entire permission notice in its entirety,
9  *    including the disclaimer of warranties.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote
14  *    products derived from this software without specific prior
15  *    written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
20  * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
23  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27  * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
28  * DAMAGE.
29  */
30 /**
31  * @file
32  *
33  * UUID related functions originally from libuuid
34  */
35
36 #ifndef _RTE_UUID_H_
37 #define _RTE_UUID_H_
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 #include <stdbool.h>
44
45 /**
46  * Struct describing a Universal Unique Identifer
47  */
48 typedef unsigned char rte_uuid_t[16];
49
50 /**
51  * Helper for defining UUID values for id tables.
52  */
53 #define RTE_UUID_INIT(a, b, c, d, e) {          \
54         ((a) >> 24) & 0xff, ((a) >> 16) & 0xff, \
55         ((a) >> 8) & 0xff, (a) & 0xff,          \
56         ((b) >> 8) & 0xff, (b) & 0xff,          \
57         ((c) >> 8) & 0xff, (c) & 0xff,          \
58         ((d) >> 8) & 0xff, (d) & 0xff,          \
59         ((e) >> 40) & 0xff, ((e) >> 32) & 0xff, \
60         ((e) >> 24) & 0xff, ((e) >> 16) & 0xff, \
61         ((e) >> 8) & 0xff, (e) & 0xff           \
62 }
63
64 /**
65  * Test if UUID is all zeros.
66  *
67  * @param uu
68  *    The uuid to check.
69  * @return
70  *    true if uuid is NULL value, false otherwise
71  */
72 bool rte_uuid_is_null(const rte_uuid_t uu);
73
74 /**
75  * Copy uuid.
76  *
77  * @param dst
78  *    Destination uuid
79  * @param src
80  *    Source uuid
81  */
82 static inline void rte_uuid_copy(rte_uuid_t dst, const rte_uuid_t src)
83 {
84         memcpy(dst, src, sizeof(rte_uuid_t));
85 }
86
87 /**
88  * Compare two UUID's
89  *
90  * @param a
91  *    A UUID to compare
92  * @param b
93  *    A UUID to compare
94  * @return
95  *   returns an integer less than, equal to, or greater than zero if UUID a is
96  *   is less than, equal, or greater than UUID b.
97  */
98 int     rte_uuid_compare(const rte_uuid_t a, const rte_uuid_t b);
99
100 /**
101  * Extract UUID from string
102  *
103  * @param in
104  *    Pointer to string of characters to convert
105  * @param uu
106  *    Destination UUID
107  * @return
108  *    Returns 0 on succes, and -1 if string is not a valid UUID.
109  */
110 int     rte_uuid_parse(const char *in, rte_uuid_t uu);
111
112 /**
113  * Convert UUID to string
114  *
115  * @param uu
116  *    UUID to format
117  * @param out
118  *    Resulting string buffer
119  * @param len
120  *    Sizeof the available string buffer
121  */
122 #define RTE_UUID_STRLEN (36 + 1)
123 void    rte_uuid_unparse(const rte_uuid_t uu, char *out, size_t len);
124
125 #ifdef __cplusplus
126 }
127 #endif
128
129 #endif /* RTE_UUID_H */