Commit | Line | Data |
---|---|---|
9d1e7de0 JI |
1 | #ifndef _BABELTRACE_COMPAT_UUID_H |
2 | #define _BABELTRACE_COMPAT_UUID_H | |
a4dfa07b MD |
3 | |
4 | /* | |
9d1e7de0 | 5 | * babeltrace/compat/uuid.h |
eb31c5e6 | 6 | * |
a4dfa07b MD |
7 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
8 | * | |
eb31c5e6 MD |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | * of this software and associated documentation files (the "Software"), to deal | |
11 | * in the Software without restriction, including without limitation the rights | |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | * copies of the Software, and to permit persons to whom the Software is | |
14 | * furnished to do so, subject to the following conditions: | |
a4dfa07b | 15 | * |
eb31c5e6 MD |
16 | * The above copyright notice and this permission notice shall be included in |
17 | * all copies or substantial portions of the Software. | |
c462e188 MD |
18 | * |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
25 | * SOFTWARE. | |
a4dfa07b MD |
26 | */ |
27 | ||
a4dfa07b MD |
28 | /* Includes final \0. */ |
29 | #define BABELTRACE_UUID_STR_LEN 37 | |
30 | #define BABELTRACE_UUID_LEN 16 | |
31 | ||
32 | #ifdef BABELTRACE_HAVE_LIBUUID | |
33 | #include <uuid/uuid.h> | |
34 | ||
35 | static inline | |
19dd40db | 36 | int bt_uuid_generate(unsigned char *uuid_out) |
a4dfa07b MD |
37 | { |
38 | uuid_generate(uuid_out); | |
39 | return 0; | |
40 | } | |
41 | ||
8a01ca57 MJ |
42 | /* Sun's libuuid lacks const qualifiers */ |
43 | #if defined(__sun__) | |
44 | static inline | |
19dd40db | 45 | int bt_uuid_unparse(const unsigned char *uuid_in, char *str_out) |
8a01ca57 MJ |
46 | { |
47 | uuid_unparse((unsigned char *) uuid_in, str_out); | |
48 | return 0; | |
49 | } | |
50 | ||
51 | static inline | |
19dd40db | 52 | int bt_uuid_parse(const char *str_in, unsigned char *uuid_out) |
8a01ca57 MJ |
53 | { |
54 | return uuid_parse((char *) str_in, uuid_out); | |
55 | } | |
56 | ||
57 | static inline | |
19dd40db | 58 | int bt_uuid_compare(const unsigned char *uuid_a, |
8a01ca57 MJ |
59 | const unsigned char *uuid_b) |
60 | { | |
61 | return uuid_compare((unsigned char *) uuid_a, | |
62 | (unsigned char *) uuid_b); | |
63 | } | |
64 | #else | |
a4dfa07b | 65 | static inline |
19dd40db | 66 | int bt_uuid_unparse(const unsigned char *uuid_in, char *str_out) |
a4dfa07b | 67 | { |
57a9f43a MD |
68 | uuid_unparse(uuid_in, str_out); |
69 | return 0; | |
a4dfa07b MD |
70 | } |
71 | ||
72 | static inline | |
19dd40db | 73 | int bt_uuid_parse(const char *str_in, unsigned char *uuid_out) |
a4dfa07b MD |
74 | { |
75 | return uuid_parse(str_in, uuid_out); | |
76 | } | |
77 | ||
78 | static inline | |
19dd40db | 79 | int bt_uuid_compare(const unsigned char *uuid_a, |
a4dfa07b MD |
80 | const unsigned char *uuid_b) |
81 | { | |
82 | return uuid_compare(uuid_a, uuid_b); | |
83 | } | |
8a01ca57 | 84 | #endif |
a4dfa07b | 85 | |
f2c00dfa | 86 | #elif defined(BABELTRACE_HAVE_LIBC_UUID) |
a4dfa07b MD |
87 | #include <uuid.h> |
88 | #include <stdint.h> | |
57a9f43a MD |
89 | #include <string.h> |
90 | #include <stdlib.h> | |
a4dfa07b MD |
91 | |
92 | static inline | |
19dd40db | 93 | int bt_uuid_generate(unsigned char *uuid_out) |
a4dfa07b MD |
94 | { |
95 | uint32_t status; | |
96 | ||
97 | uuid_create((uuid_t *) uuid_out, &status); | |
98 | if (status == uuid_s_ok) | |
99 | return 0; | |
100 | else | |
101 | return -1; | |
102 | } | |
103 | ||
104 | static inline | |
19dd40db | 105 | int bt_uuid_unparse(const unsigned char *uuid_in, char *str_out) |
a4dfa07b MD |
106 | { |
107 | uint32_t status; | |
57a9f43a MD |
108 | char *alloc_str; |
109 | int ret; | |
110 | ||
111 | uuid_to_string((uuid_t *) uuid_in, &alloc_str, &status); | |
112 | if (status == uuid_s_ok) { | |
113 | strcpy(str_out, alloc_str); | |
114 | ret = 0; | |
115 | } else { | |
116 | ret = -1; | |
117 | } | |
118 | free(alloc_str); | |
119 | return ret; | |
a4dfa07b MD |
120 | } |
121 | ||
122 | static inline | |
19dd40db | 123 | int bt_uuid_parse(const char *str_in, unsigned char *uuid_out) |
a4dfa07b MD |
124 | { |
125 | uint32_t status; | |
126 | ||
127 | uuid_from_string(str_in, (uuid_t *) uuid_out, &status); | |
128 | if (status == uuid_s_ok) | |
129 | return 0; | |
130 | else | |
131 | return -1; | |
132 | } | |
133 | ||
134 | static inline | |
19dd40db | 135 | int bt_uuid_compare(const unsigned char *uuid_a, |
a4dfa07b MD |
136 | const unsigned char *uuid_b) |
137 | { | |
138 | uint32_t status; | |
139 | ||
140 | uuid_compare((uuid_t *) uuid_a, (uuid_t *) uuid_b, &status); | |
141 | if (status == uuid_s_ok) | |
142 | return 0; | |
143 | else | |
144 | return -1; | |
145 | } | |
146 | ||
9d1e7de0 JI |
147 | #elif defined(__MINGW32__) |
148 | ||
19dd40db JG |
149 | int bt_uuid_generate(unsigned char *uuid_out); |
150 | int bt_uuid_unparse(const unsigned char *uuid_in, char *str_out); | |
151 | int bt_uuid_parse(const char *str_in, unsigned char *uuid_out); | |
152 | int bt_uuid_compare(const unsigned char *uuid_a, | |
9d1e7de0 JI |
153 | const unsigned char *uuid_b); |
154 | ||
a4dfa07b MD |
155 | #else |
156 | #error "Babeltrace needs to have a UUID generator configured." | |
157 | #endif | |
158 | ||
9d1e7de0 | 159 | #endif /* _BABELTRACE_COMPAT_UUID_H */ |