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