Add MinGW implementation of UUID functions
authorJP Ikaheimonen <jp_ikaheimonen@mentor.com>
Mon, 8 Jul 2013 14:01:44 +0000 (10:01 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 8 Jul 2013 14:01:44 +0000 (10:01 -0400)
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 <mathieu.desnoyers@efficios.com>
Makefile.am
compat/Makefile.am [new file with mode: 0644]
compat/compat_uuid.c [new file with mode: 0644]
configure.ac
converter/Makefile.am
include/babeltrace/compat/uuid.h
lib/Makefile.am

index b25b58fe3e4fa002e4cf361ebcc648f88d7cc95f..cdfad5a8b48c52a8ba007b2cceec3e8e5c5eb3a1 100644 (file)
@@ -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 (file)
index 0000000..79be1c8
--- /dev/null
@@ -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 (file)
index 0000000..6216f79
--- /dev/null
@@ -0,0 +1,111 @@
+/*
+ * compat/compat_uuid.h
+ *
+ * Copyright (C) 2013   Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *
+ * 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 <windows.h>
+#include <stdlib.h>
+#include <babeltrace/compat/uuid.h>
+
+/* 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;
+       }
+}
index 8a636075a756813a29b8ef88ed93dbc75574de63..a22b1b93a4cea225c228e61df84af070792fee8f 100644 (file)
@@ -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
index 8ef71a23ec91d7b1b22e9eb3299e4ed139b059d0..3248d443fa1d92acd7904706e6585bac0395b1d8 100644 (file)
@@ -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
index 2ce74670796506645c1527d48743e8f0f06d6a22..c41cc3ec1fc902240ac648c60f50fdc457d0b702 100644 (file)
@@ -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 <mathieu.desnoyers@efficios.com>
  *
@@ -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 */
index 7ffb164657d42004bff9da3a49591e622fecb6f2..fa470c09cf5f9b8f44706633600482214485c220 100644 (file)
@@ -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
This page took 0.039381 seconds and 4 git commands to generate.