| 1 | #ifndef _BABELTRACE_UUID_H |
| 2 | #define _BABELTRACE_UUID_H |
| 3 | |
| 4 | /* |
| 5 | * babeltrace/uuid.h |
| 6 | * |
| 7 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | */ |
| 19 | |
| 20 | #include <config.h> |
| 21 | |
| 22 | /* Includes final \0. */ |
| 23 | #define BABELTRACE_UUID_STR_LEN 37 |
| 24 | #define BABELTRACE_UUID_LEN 16 |
| 25 | |
| 26 | #ifdef BABELTRACE_HAVE_LIBUUID |
| 27 | #include <uuid/uuid.h> |
| 28 | |
| 29 | static inline |
| 30 | int babeltrace_uuid_generate(unsigned char *uuid_out) |
| 31 | { |
| 32 | uuid_generate(uuid_out); |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static inline |
| 37 | int babeltrace_uuid_unparse(const unsigned char *uuid_in, char *str_out) |
| 38 | { |
| 39 | uuid_unparse(uuid_in, str_out); |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static inline |
| 44 | int babeltrace_uuid_parse(const char *str_in, unsigned char *uuid_out) |
| 45 | { |
| 46 | return uuid_parse(str_in, uuid_out); |
| 47 | } |
| 48 | |
| 49 | static inline |
| 50 | int babeltrace_uuid_compare(const unsigned char *uuid_a, |
| 51 | const unsigned char *uuid_b) |
| 52 | { |
| 53 | return uuid_compare(uuid_a, uuid_b); |
| 54 | } |
| 55 | |
| 56 | #elif defined(BABELTRACE_HAVE_LIBC_UUID) |
| 57 | #include <uuid.h> |
| 58 | #include <stdint.h> |
| 59 | #include <string.h> |
| 60 | #include <stdlib.h> |
| 61 | |
| 62 | static inline |
| 63 | int babeltrace_uuid_generate(unsigned char *uuid_out) |
| 64 | { |
| 65 | uint32_t status; |
| 66 | |
| 67 | uuid_create((uuid_t *) uuid_out, &status); |
| 68 | if (status == uuid_s_ok) |
| 69 | return 0; |
| 70 | else |
| 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | static inline |
| 75 | int babeltrace_uuid_unparse(const unsigned char *uuid_in, char *str_out) |
| 76 | { |
| 77 | uint32_t status; |
| 78 | char *alloc_str; |
| 79 | int ret; |
| 80 | |
| 81 | uuid_to_string((uuid_t *) uuid_in, &alloc_str, &status); |
| 82 | if (status == uuid_s_ok) { |
| 83 | strcpy(str_out, alloc_str); |
| 84 | ret = 0; |
| 85 | } else { |
| 86 | ret = -1; |
| 87 | } |
| 88 | free(alloc_str); |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | static inline |
| 93 | int babeltrace_uuid_parse(const char *str_in, unsigned char *uuid_out) |
| 94 | { |
| 95 | uint32_t status; |
| 96 | |
| 97 | uuid_from_string(str_in, (uuid_t *) uuid_out, &status); |
| 98 | if (status == uuid_s_ok) |
| 99 | return 0; |
| 100 | else |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | static inline |
| 105 | int babeltrace_uuid_compare(const unsigned char *uuid_a, |
| 106 | const unsigned char *uuid_b) |
| 107 | { |
| 108 | uint32_t status; |
| 109 | |
| 110 | uuid_compare((uuid_t *) uuid_a, (uuid_t *) uuid_b, &status); |
| 111 | if (status == uuid_s_ok) |
| 112 | return 0; |
| 113 | else |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | #else |
| 118 | #error "Babeltrace needs to have a UUID generator configured." |
| 119 | #endif |
| 120 | |
| 121 | #endif /* _BABELTRACE_UUID_H */ |