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