Commit | Line | Data |
---|---|---|
9d1e7de0 JI |
1 | /* |
2 | * compat/compat_uuid.h | |
3 | * | |
4 | * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
22 | * SOFTWARE. | |
23 | */ | |
24 | ||
6a9af156 MJ |
25 | #ifdef __APPLE__ |
26 | /* | |
27 | * On macOS, we need a dummy symbol so that the linker won't | |
28 | * complain of an empty table of contents. | |
29 | */ | |
30 | BT_HIDDEN | |
31 | int bt_uuid_dummy_symbol; | |
32 | #endif /* __APPLE__ */ | |
33 | ||
20eee76e | 34 | #ifdef __MINGW32__ |
9d1e7de0 | 35 | |
20eee76e | 36 | #include <rpc.h> |
9d1e7de0 | 37 | #include <stdlib.h> |
578e048b | 38 | #include "compat/uuid.h" |
9d1e7de0 JI |
39 | |
40 | /* MinGW does not provide byteswap - implement our own version. */ | |
41 | static | |
42 | void swap(unsigned char *ptr, unsigned int i, unsigned int j) | |
43 | { | |
44 | unsigned char tmp; | |
45 | ||
46 | tmp = ptr[i]; | |
47 | ptr[i] = ptr[j]; | |
48 | ptr[j] = tmp; | |
49 | } | |
50 | ||
51 | static | |
52 | void fix_uuid_endian(unsigned char * ptr) | |
53 | { | |
20eee76e MJ |
54 | swap(ptr, 0, 3); |
55 | swap(ptr, 1, 2); | |
56 | swap(ptr, 4, 5); | |
57 | swap(ptr, 6, 7); | |
9d1e7de0 JI |
58 | } |
59 | ||
19dd40db | 60 | int bt_uuid_generate(unsigned char *uuid_out) |
9d1e7de0 JI |
61 | { |
62 | RPC_STATUS status; | |
63 | ||
20eee76e | 64 | status = UuidCreate((UUID *) uuid_out); |
9d1e7de0 JI |
65 | if (status == RPC_S_OK) |
66 | return 0; | |
67 | else | |
68 | return -1; | |
69 | } | |
70 | ||
19dd40db | 71 | int bt_uuid_unparse(const unsigned char *uuid_in, char *str_out) |
9d1e7de0 JI |
72 | { |
73 | RPC_STATUS status; | |
74 | unsigned char *alloc_str; | |
75 | int ret; | |
76 | unsigned char copy_of_uuid_in[BABELTRACE_UUID_LEN]; | |
77 | ||
78 | /* make a modifyable copy of uuid_in */ | |
79 | memcpy(copy_of_uuid_in, uuid_in, BABELTRACE_UUID_LEN); | |
80 | ||
81 | fix_uuid_endian(copy_of_uuid_in); | |
20eee76e | 82 | status = UuidToString((UUID *) copy_of_uuid_in, &alloc_str); |
9d1e7de0 JI |
83 | |
84 | if (status == RPC_S_OK) { | |
85 | strncpy(str_out, (char *) alloc_str, BABELTRACE_UUID_STR_LEN); | |
86 | str_out[BABELTRACE_UUID_STR_LEN - 1] = '\0'; | |
87 | ret = 0; | |
88 | } else { | |
89 | ret = -1; | |
90 | } | |
91 | RpcStringFree(&alloc_str); | |
92 | return ret; | |
93 | } | |
94 | ||
19dd40db | 95 | int bt_uuid_parse(const char *str_in, unsigned char *uuid_out) |
9d1e7de0 JI |
96 | { |
97 | RPC_STATUS status; | |
98 | ||
99 | status = UuidFromString((unsigned char *) str_in, | |
20eee76e | 100 | (UUID *) uuid_out); |
9d1e7de0 JI |
101 | fix_uuid_endian(uuid_out); |
102 | ||
103 | if (status == RPC_S_OK) | |
104 | return 0; | |
105 | else | |
106 | return -1; | |
107 | } | |
108 | ||
19dd40db | 109 | int bt_uuid_compare(const unsigned char *uuid_a, |
9d1e7de0 JI |
110 | const unsigned char *uuid_b) |
111 | { | |
112 | RPC_STATUS status; | |
113 | ||
20eee76e | 114 | return !UuidCompare((UUID *) uuid_a, (UUID *) uuid_b, &status) ? 0 : -1; |
9d1e7de0 | 115 | } |
20eee76e MJ |
116 | |
117 | #endif |