From: JP Ikaheimonen Date: Mon, 8 Jul 2013 14:01:44 +0000 (-0400) Subject: Add MinGW implementation of UUID functions X-Git-Tag: v1.2.0-rc1~74^2~19 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=9d1e7de0dd0572e8071e0bd620c1c569e8b5396b Add MinGW implementation of UUID functions Add implementation of the UUID functions for MinGW. The Windows UUID standard has a different endian from what we expect, so also do endian conversion for the UUID data types as needed. Add the MinGW implementation file to the build system. In configure.ac, assume that MinGW UUID functions exist. [ Edit by Mathieu Desnoyers: coding style fixes, use strncmp rather than strcmp. Use already existing defines rather than hardcoded integers. ] Signed-off-by: Mathieu Desnoyers --- diff --git a/Makefile.am b/Makefile.am index b25b58fe..cdfad5a8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,7 +2,7 @@ AM_CFLAGS = $(PACKAGE_CFLAGS) -I$(top_srcdir)/include ACLOCAL_AMFLAGS = -I m4 -SUBDIRS = include types lib formats converter tests doc extras +SUBDIRS = include types compat lib formats converter tests doc extras dist_doc_DATA = ChangeLog LICENSE mit-license.txt gpl-2.0.txt \ std-ext-lib.txt diff --git a/compat/Makefile.am b/compat/Makefile.am new file mode 100644 index 00000000..79be1c81 --- /dev/null +++ b/compat/Makefile.am @@ -0,0 +1,12 @@ +AM_CFLAGS = $(PACKAGE_CFLAGS) -I$(top_srcdir)/include + +lib_LTLIBRARIES = libcompat.la + +libcompat_la_SOURCES = + +libcompat_la_LDFLAGS = \ + -Wl,--no-as-needed + +if BABELTRACE_BUILD_WITH_MINGW +libcompat_la_SOURCES += compat_uuid.c +endif diff --git a/compat/compat_uuid.c b/compat/compat_uuid.c new file mode 100644 index 00000000..6216f799 --- /dev/null +++ b/compat/compat_uuid.c @@ -0,0 +1,111 @@ +/* + * compat/compat_uuid.h + * + * Copyright (C) 2013 Mathieu Desnoyers + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* This file is only built under MINGW32 */ + +#include +#include +#include + +/* MinGW does not provide byteswap - implement our own version. */ +static +void swap(unsigned char *ptr, unsigned int i, unsigned int j) +{ + unsigned char tmp; + + tmp = ptr[i]; + ptr[i] = ptr[j]; + ptr[j] = tmp; +} + +static +void fix_uuid_endian(unsigned char * ptr) +{ + swap(ptr, 0, 3) + swap(ptr, 1, 2) + swap(ptr, 4, 5) + swap(ptr, 6, 7) +} + +int babeltrace_uuid_generate(unsigned char *uuid_out) +{ + RPC_STATUS status; + + status = UuidCreate((struct UUID *)uuid_out); + if (status == RPC_S_OK) + return 0; + else + return -1; +} + +int babeltrace_uuid_unparse(const unsigned char *uuid_in, char *str_out) +{ + RPC_STATUS status; + unsigned char *alloc_str; + int ret; + unsigned char copy_of_uuid_in[BABELTRACE_UUID_LEN]; + + /* make a modifyable copy of uuid_in */ + memcpy(copy_of_uuid_in, uuid_in, BABELTRACE_UUID_LEN); + + fix_uuid_endian(copy_of_uuid_in); + status = UuidToString((struct UUID *) copy_of_uuid_in, &alloc_str); + + if (status == RPC_S_OK) { + strncpy(str_out, (char *) alloc_str, BABELTRACE_UUID_STR_LEN); + str_out[BABELTRACE_UUID_STR_LEN - 1] = '\0'; + ret = 0; + } else { + ret = -1; + } + RpcStringFree(&alloc_str); + return ret; +} + +int babeltrace_uuid_parse(const char *str_in, unsigned char *uuid_out) +{ + RPC_STATUS status; + + status = UuidFromString((unsigned char *) str_in, + (struct UUID *) uuid_out); + fix_uuid_endian(uuid_out); + + if (status == RPC_S_OK) + return 0; + else + return -1; +} + +int babeltrace_uuid_compare(const unsigned char *uuid_a, + const unsigned char *uuid_b) +{ + RPC_STATUS status; + + if (!UuidCompare((struct UUID *) uuid_a, (struct UUID *) uuid_b, + &status)) { + return 0; + } else { + return -1; + } +} diff --git a/configure.ac b/configure.ac index 8a636075..a22b1b93 100644 --- a/configure.ac +++ b/configure.ac @@ -60,7 +60,10 @@ AC_CHECK_LIB([uuid], [uuid_generate], have_libc_uuid=yes ], [ - AC_MSG_ERROR([Cannot find libuuid uuid_generate nor libc uuid_create. Use [LDFLAGS]=-Ldir to specify their location.]) + # for MinGW32 we have our own internal implemenation of uuid using Windows functions. + if test "x$MINGW32" = xno; then + AC_MSG_ERROR([Cannot find libuuid uuid_generate nor libc uuid_create. Use [LDFLAGS]=-Ldir to specify their location.]) + fi ]) ] ) @@ -102,6 +105,7 @@ AC_SUBST(babeltracectfincludedir) AC_CONFIG_FILES([ Makefile types/Makefile + compat/Makefile formats/Makefile formats/ctf/Makefile formats/ctf/types/Makefile diff --git a/converter/Makefile.am b/converter/Makefile.am index 8ef71a23..3248d443 100644 --- a/converter/Makefile.am +++ b/converter/Makefile.am @@ -13,6 +13,7 @@ babeltrace_LDFLAGS = -Wl,--no-as-needed babeltrace_LDADD = \ $(top_builddir)/lib/libbabeltrace.la \ $(top_builddir)/formats/ctf/libbabeltrace-ctf.la \ + $(top_builddir)/compat/libcompat.la \ $(top_builddir)/formats/ctf-text/libbabeltrace-ctf-text.la \ $(top_builddir)/formats/ctf-metadata/libbabeltrace-ctf-metadata.la \ $(top_builddir)/formats/bt-dummy/libbabeltrace-dummy.la @@ -21,7 +22,8 @@ babeltrace_log_SOURCES = babeltrace-log.c babeltrace_log_LDADD = \ $(top_builddir)/lib/libbabeltrace.la \ - $(top_builddir)/formats/ctf/libbabeltrace-ctf.la + $(top_builddir)/formats/ctf/libbabeltrace-ctf.la \ + $(top_builddir)/compat/libcompat.la if BABELTRACE_BUILD_WITH_LIBUUID babeltrace_log_LDADD += -luuid diff --git a/include/babeltrace/compat/uuid.h b/include/babeltrace/compat/uuid.h index 2ce74670..c41cc3ec 100644 --- a/include/babeltrace/compat/uuid.h +++ b/include/babeltrace/compat/uuid.h @@ -1,8 +1,8 @@ -#ifndef _BABELTRACE_UUID_H -#define _BABELTRACE_UUID_H +#ifndef _BABELTRACE_COMPAT_UUID_H +#define _BABELTRACE_COMPAT_UUID_H /* - * babeltrace/uuid.h + * babeltrace/compat/uuid.h * * Copyright (C) 2011 Mathieu Desnoyers * @@ -122,8 +122,16 @@ int babeltrace_uuid_compare(const unsigned char *uuid_a, return -1; } +#elif defined(__MINGW32__) + +int babeltrace_uuid_generate(unsigned char *uuid_out); +int babeltrace_uuid_unparse(const unsigned char *uuid_in, char *str_out); +int babeltrace_uuid_parse(const char *str_in, unsigned char *uuid_out); +int babeltrace_uuid_compare(const unsigned char *uuid_a, + const unsigned char *uuid_b); + #else #error "Babeltrace needs to have a UUID generator configured." #endif -#endif /* _BABELTRACE_UUID_H */ +#endif /* _BABELTRACE_COMPAT_UUID_H */ diff --git a/lib/Makefile.am b/lib/Makefile.am index 7ffb1646..fa470c09 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -14,4 +14,5 @@ libbabeltrace_la_SOURCES = babeltrace.c \ libbabeltrace_la_LDFLAGS = \ -Wl,--no-as-needed \ prio_heap/libprio_heap.la \ - $(top_builddir)/types/libbabeltrace_types.la + $(top_builddir)/types/libbabeltrace_types.la \ + $(top_builddir)/compat/libcompat.la