0cc5976fd88959452c5edd8369e3de6c09bd1bf2
[babeltrace.git] / src / compat / uuid.c
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
25 #ifdef __APPLE__
26 #include "common/macros.h"
27
28 /*
29 * On macOS, we need a dummy symbol so that the linker won't
30 * complain of an empty table of contents.
31 */
32 BT_HIDDEN
33 int bt_uuid_dummy_symbol;
34 #endif /* __APPLE__ */
35
36 #ifdef __MINGW32__
37
38 #include <rpc.h>
39 #include <stdlib.h>
40 #include "compat/uuid.h"
41
42 /* MinGW does not provide byteswap - implement our own version. */
43 static
44 void swap(unsigned char *ptr, unsigned int i, unsigned int j)
45 {
46 unsigned char tmp;
47
48 tmp = ptr[i];
49 ptr[i] = ptr[j];
50 ptr[j] = tmp;
51 }
52
53 static
54 void fix_uuid_endian(unsigned char * ptr)
55 {
56 swap(ptr, 0, 3);
57 swap(ptr, 1, 2);
58 swap(ptr, 4, 5);
59 swap(ptr, 6, 7);
60 }
61
62 int bt_uuid_generate(unsigned char *uuid_out)
63 {
64 RPC_STATUS status;
65
66 status = UuidCreate((UUID *) uuid_out);
67 if (status == RPC_S_OK)
68 return 0;
69 else
70 return -1;
71 }
72
73 int bt_uuid_unparse(const unsigned char *uuid_in, char *str_out)
74 {
75 RPC_STATUS status;
76 unsigned char *alloc_str;
77 int ret;
78 unsigned char copy_of_uuid_in[BABELTRACE_UUID_LEN];
79
80 /* make a modifyable copy of uuid_in */
81 memcpy(copy_of_uuid_in, uuid_in, BABELTRACE_UUID_LEN);
82
83 fix_uuid_endian(copy_of_uuid_in);
84 status = UuidToString((UUID *) copy_of_uuid_in, &alloc_str);
85
86 if (status == RPC_S_OK) {
87 strncpy(str_out, (char *) alloc_str, BABELTRACE_UUID_STR_LEN);
88 str_out[BABELTRACE_UUID_STR_LEN - 1] = '\0';
89 ret = 0;
90 } else {
91 ret = -1;
92 }
93 RpcStringFree(&alloc_str);
94 return ret;
95 }
96
97 int bt_uuid_parse(const char *str_in, unsigned char *uuid_out)
98 {
99 RPC_STATUS status;
100
101 status = UuidFromString((unsigned char *) str_in,
102 (UUID *) uuid_out);
103 fix_uuid_endian(uuid_out);
104
105 if (status == RPC_S_OK)
106 return 0;
107 else
108 return -1;
109 }
110
111 int bt_uuid_compare(const unsigned char *uuid_a,
112 const unsigned char *uuid_b)
113 {
114 RPC_STATUS status;
115
116 return !UuidCompare((UUID *) uuid_a, (UUID *) uuid_b, &status) ? 0 : -1;
117 }
118
119 #endif
This page took 0.033105 seconds and 3 git commands to generate.