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 | ||
18fc0d90 MJ |
25 | #define BT_LOG_TAG "COMPAT-UUID" |
26 | #include "logging.h" | |
27 | ||
20eee76e | 28 | #ifdef __MINGW32__ |
9d1e7de0 | 29 | |
20eee76e | 30 | #include <rpc.h> |
9d1e7de0 | 31 | #include <stdlib.h> |
3d9990ac | 32 | #include <babeltrace/compat/uuid-internal.h> |
9d1e7de0 JI |
33 | |
34 | /* MinGW does not provide byteswap - implement our own version. */ | |
35 | static | |
36 | void swap(unsigned char *ptr, unsigned int i, unsigned int j) | |
37 | { | |
38 | unsigned char tmp; | |
39 | ||
40 | tmp = ptr[i]; | |
41 | ptr[i] = ptr[j]; | |
42 | ptr[j] = tmp; | |
43 | } | |
44 | ||
45 | static | |
46 | void fix_uuid_endian(unsigned char * ptr) | |
47 | { | |
20eee76e MJ |
48 | swap(ptr, 0, 3); |
49 | swap(ptr, 1, 2); | |
50 | swap(ptr, 4, 5); | |
51 | swap(ptr, 6, 7); | |
9d1e7de0 JI |
52 | } |
53 | ||
19dd40db | 54 | int bt_uuid_generate(unsigned char *uuid_out) |
9d1e7de0 JI |
55 | { |
56 | RPC_STATUS status; | |
57 | ||
20eee76e | 58 | status = UuidCreate((UUID *) uuid_out); |
9d1e7de0 JI |
59 | if (status == RPC_S_OK) |
60 | return 0; | |
61 | else | |
62 | return -1; | |
63 | } | |
64 | ||
19dd40db | 65 | int bt_uuid_unparse(const unsigned char *uuid_in, char *str_out) |
9d1e7de0 JI |
66 | { |
67 | RPC_STATUS status; | |
68 | unsigned char *alloc_str; | |
69 | int ret; | |
70 | unsigned char copy_of_uuid_in[BABELTRACE_UUID_LEN]; | |
71 | ||
72 | /* make a modifyable copy of uuid_in */ | |
73 | memcpy(copy_of_uuid_in, uuid_in, BABELTRACE_UUID_LEN); | |
74 | ||
75 | fix_uuid_endian(copy_of_uuid_in); | |
20eee76e | 76 | status = UuidToString((UUID *) copy_of_uuid_in, &alloc_str); |
9d1e7de0 JI |
77 | |
78 | if (status == RPC_S_OK) { | |
79 | strncpy(str_out, (char *) alloc_str, BABELTRACE_UUID_STR_LEN); | |
80 | str_out[BABELTRACE_UUID_STR_LEN - 1] = '\0'; | |
81 | ret = 0; | |
82 | } else { | |
83 | ret = -1; | |
84 | } | |
85 | RpcStringFree(&alloc_str); | |
86 | return ret; | |
87 | } | |
88 | ||
19dd40db | 89 | int bt_uuid_parse(const char *str_in, unsigned char *uuid_out) |
9d1e7de0 JI |
90 | { |
91 | RPC_STATUS status; | |
92 | ||
93 | status = UuidFromString((unsigned char *) str_in, | |
20eee76e | 94 | (UUID *) uuid_out); |
9d1e7de0 JI |
95 | fix_uuid_endian(uuid_out); |
96 | ||
97 | if (status == RPC_S_OK) | |
98 | return 0; | |
99 | else | |
100 | return -1; | |
101 | } | |
102 | ||
19dd40db | 103 | int bt_uuid_compare(const unsigned char *uuid_a, |
9d1e7de0 JI |
104 | const unsigned char *uuid_b) |
105 | { | |
106 | RPC_STATUS status; | |
107 | ||
20eee76e | 108 | return !UuidCompare((UUID *) uuid_a, (UUID *) uuid_b, &status) ? 0 : -1; |
9d1e7de0 | 109 | } |
20eee76e MJ |
110 | |
111 | #endif |