| 1 | #ifndef _BABELTRACE_UUID_H |
| 2 | #define _BABELTRACE_UUID_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 6 | * |
| 7 | * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 8 | * OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 9 | * |
| 10 | * Permission is hereby granted to use or copy this program |
| 11 | * for any purpose, provided the above notices are retained on all copies. |
| 12 | * Permission to modify the code and to distribute modified code is granted, |
| 13 | * provided the above notices are retained, and a notice that the code was |
| 14 | * modified is included with the above copyright notice. |
| 15 | */ |
| 16 | |
| 17 | #include <config.h> |
| 18 | |
| 19 | /* Includes final \0. */ |
| 20 | #define BABELTRACE_UUID_STR_LEN 37 |
| 21 | #define BABELTRACE_UUID_LEN 16 |
| 22 | |
| 23 | #ifdef BABELTRACE_HAVE_LIBUUID |
| 24 | #include <uuid/uuid.h> |
| 25 | |
| 26 | static inline |
| 27 | int babeltrace_uuid_generate(unsigned char *uuid_out) |
| 28 | { |
| 29 | uuid_generate(uuid_out); |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | static inline |
| 34 | void babeltrace_uuid_unparse(const unsigned char *uuid_in, char *str_out) |
| 35 | { |
| 36 | return uuid_unparse(uuid_in, str_out); |
| 37 | } |
| 38 | |
| 39 | static inline |
| 40 | int babeltrace_uuid_parse(const char *str_in, unsigned char *uuid_out) |
| 41 | { |
| 42 | return uuid_parse(str_in, uuid_out); |
| 43 | } |
| 44 | |
| 45 | static inline |
| 46 | int babeltrace_uuid_compare(const unsigned char *uuid_a, |
| 47 | const unsigned char *uuid_b) |
| 48 | { |
| 49 | return uuid_compare(uuid_a, uuid_b); |
| 50 | } |
| 51 | |
| 52 | #elif defined(BABELTRACE_HAVE_LIBC_UUID) |
| 53 | #include <uuid.h> |
| 54 | #include <stdint.h> |
| 55 | |
| 56 | static inline |
| 57 | int babeltrace_uuid_generate(unsigned char *uuid_out) |
| 58 | { |
| 59 | uint32_t status; |
| 60 | |
| 61 | uuid_create((uuid_t *) uuid_out, &status); |
| 62 | if (status == uuid_s_ok) |
| 63 | return 0; |
| 64 | else |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | static inline |
| 69 | void babeltrace_uuid_unparse(const unsigned char *uuid_in, char *str_out) |
| 70 | { |
| 71 | uint32_t status; |
| 72 | |
| 73 | uuid_to_string((uuid_t *) uuid_in, str_out, &status); |
| 74 | if (status == uuid_s_ok) |
| 75 | return 0; |
| 76 | else |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | static inline |
| 81 | int babeltrace_uuid_parse(const char *str_in, unsigned char *uuid_out) |
| 82 | { |
| 83 | uint32_t status; |
| 84 | |
| 85 | uuid_from_string(str_in, (uuid_t *) uuid_out, &status); |
| 86 | if (status == uuid_s_ok) |
| 87 | return 0; |
| 88 | else |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | static inline |
| 93 | int babeltrace_uuid_compare(const unsigned char *uuid_a, |
| 94 | const unsigned char *uuid_b) |
| 95 | { |
| 96 | uint32_t status; |
| 97 | |
| 98 | uuid_compare((uuid_t *) uuid_a, (uuid_t *) uuid_b, &status); |
| 99 | if (status == uuid_s_ok) |
| 100 | return 0; |
| 101 | else |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | #else |
| 106 | #error "Babeltrace needs to have a UUID generator configured." |
| 107 | #endif |
| 108 | |
| 109 | #endif /* _BABELTRACE_UUID_H */ |