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 | ||
28 | #include <config.h> | |
29 | ||
30 | /* Includes final \0. */ | |
31 | #define BABELTRACE_UUID_STR_LEN 37 | |
32 | #define BABELTRACE_UUID_LEN 16 | |
33 | ||
34 | #ifdef BABELTRACE_HAVE_LIBUUID | |
35 | #include <uuid/uuid.h> | |
36 | ||
37 | static inline | |
72a3bc16 | 38 | int bt_uuid_generate(unsigned char *uuid_out) |
a4dfa07b MD |
39 | { |
40 | uuid_generate(uuid_out); | |
41 | return 0; | |
42 | } | |
43 | ||
6c3e8784 MJ |
44 | /* Sun's libuuid lacks const qualifiers */ |
45 | #if defined(__sun__) | |
46 | static inline | |
72a3bc16 | 47 | int bt_uuid_unparse(const unsigned char *uuid_in, char *str_out) |
6c3e8784 MJ |
48 | { |
49 | uuid_unparse((unsigned char *) uuid_in, str_out); | |
50 | return 0; | |
51 | } | |
52 | ||
53 | static inline | |
72a3bc16 | 54 | int bt_uuid_parse(const char *str_in, unsigned char *uuid_out) |
6c3e8784 MJ |
55 | { |
56 | return uuid_parse((char *) str_in, uuid_out); | |
57 | } | |
58 | ||
59 | static inline | |
72a3bc16 | 60 | int bt_uuid_compare(const unsigned char *uuid_a, |
6c3e8784 MJ |
61 | const unsigned char *uuid_b) |
62 | { | |
63 | return uuid_compare((unsigned char *) uuid_a, | |
64 | (unsigned char *) uuid_b); | |
65 | } | |
66 | #else | |
a4dfa07b | 67 | static inline |
72a3bc16 | 68 | int bt_uuid_unparse(const unsigned char *uuid_in, char *str_out) |
a4dfa07b | 69 | { |
57a9f43a MD |
70 | uuid_unparse(uuid_in, str_out); |
71 | return 0; | |
a4dfa07b MD |
72 | } |
73 | ||
74 | static inline | |
72a3bc16 | 75 | int bt_uuid_parse(const char *str_in, unsigned char *uuid_out) |
a4dfa07b MD |
76 | { |
77 | return uuid_parse(str_in, uuid_out); | |
78 | } | |
79 | ||
80 | static inline | |
72a3bc16 | 81 | int bt_uuid_compare(const unsigned char *uuid_a, |
a4dfa07b MD |
82 | const unsigned char *uuid_b) |
83 | { | |
84 | return uuid_compare(uuid_a, uuid_b); | |
85 | } | |
6c3e8784 | 86 | #endif |
a4dfa07b | 87 | |
72a3bc16 | 88 | #elif defined(BT_HAVE_LIBC_UUID) |
a4dfa07b MD |
89 | #include <uuid.h> |
90 | #include <stdint.h> | |
57a9f43a MD |
91 | #include <string.h> |
92 | #include <stdlib.h> | |
a4dfa07b MD |
93 | |
94 | static inline | |
72a3bc16 | 95 | int bt_uuid_generate(unsigned char *uuid_out) |
a4dfa07b MD |
96 | { |
97 | uint32_t status; | |
98 | ||
99 | uuid_create((uuid_t *) uuid_out, &status); | |
100 | if (status == uuid_s_ok) | |
101 | return 0; | |
102 | else | |
103 | return -1; | |
104 | } | |
105 | ||
106 | static inline | |
72a3bc16 | 107 | int bt_uuid_unparse(const unsigned char *uuid_in, char *str_out) |
a4dfa07b MD |
108 | { |
109 | uint32_t status; | |
57a9f43a MD |
110 | char *alloc_str; |
111 | int ret; | |
112 | ||
113 | uuid_to_string((uuid_t *) uuid_in, &alloc_str, &status); | |
114 | if (status == uuid_s_ok) { | |
115 | strcpy(str_out, alloc_str); | |
116 | ret = 0; | |
117 | } else { | |
118 | ret = -1; | |
119 | } | |
120 | free(alloc_str); | |
121 | return ret; | |
a4dfa07b MD |
122 | } |
123 | ||
124 | static inline | |
72a3bc16 | 125 | int bt_uuid_parse(const char *str_in, unsigned char *uuid_out) |
a4dfa07b MD |
126 | { |
127 | uint32_t status; | |
128 | ||
129 | uuid_from_string(str_in, (uuid_t *) uuid_out, &status); | |
130 | if (status == uuid_s_ok) | |
131 | return 0; | |
132 | else | |
133 | return -1; | |
134 | } | |
135 | ||
136 | static inline | |
72a3bc16 | 137 | int bt_uuid_compare(const unsigned char *uuid_a, |
a4dfa07b MD |
138 | const unsigned char *uuid_b) |
139 | { | |
140 | uint32_t status; | |
141 | ||
142 | uuid_compare((uuid_t *) uuid_a, (uuid_t *) uuid_b, &status); | |
143 | if (status == uuid_s_ok) | |
144 | return 0; | |
145 | else | |
146 | return -1; | |
147 | } | |
148 | ||
9d1e7de0 JI |
149 | #elif defined(__MINGW32__) |
150 | ||
72a3bc16 JG |
151 | int bt_uuid_generate(unsigned char *uuid_out); |
152 | int bt_uuid_unparse(const unsigned char *uuid_in, char *str_out); | |
153 | int bt_uuid_parse(const char *str_in, unsigned char *uuid_out); | |
154 | int bt_uuid_compare(const unsigned char *uuid_a, | |
9d1e7de0 JI |
155 | const unsigned char *uuid_b); |
156 | ||
a4dfa07b MD |
157 | #else |
158 | #error "Babeltrace needs to have a UUID generator configured." | |
159 | #endif | |
160 | ||
9d1e7de0 | 161 | #endif /* _BABELTRACE_COMPAT_UUID_H */ |