This file is part of MXE. See LICENSE.md for licensing information.

Submitted to upstream at
https://mailman.videolan.org/pipermail/libdvbpsi-devel/2014-June/000738.html
https://mailman.videolan.org/pipermail/libdvbpsi-devel/2014-June/000739.html

From ada90a3c9a5e8b1c8d214f9e9e82499b54060419 Mon Sep 17 00:00:00 2001
From: Timothy Gu <timothygu99@gmail.com>
Date: Tue, 24 Jun 2014 14:57:31 -0700
Subject: [PATCH 2/3] dvbpsi: fix when _GNU_SOURCE is not defined

Based on a patch by Guilherme Lima Bernal <lb-guilherme@live.com>.

Signed-off-by: Timothy Gu <timothygu99@gmail.com>
---
 src/dvbpsi.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/src/dvbpsi.c b/src/dvbpsi.c
index 89d4932..141e3df 100644
--- a/src/dvbpsi.c
+++ b/src/dvbpsi.c
@@ -529,14 +529,10 @@ void dvbpsi_message(dvbpsi_t *dvbpsi, const dvbpsi_msg_level_t level, const char
         msg = malloc(DVBPSI_MSG_SIZE);
         if (msg == NULL) {
             va_end(ap);
-            return;
-        }
-        if (snprintf(&msg, DVBPSI_MSG_SIZE, DVBPSI_MSG_FORMAT, ap) < 0) {
-            va_end(ap);
             free(msg);
             return;
         }
-        int err = vsnprintf(&msg, DVBPSI_MSG_SIZE, fmt, ap);
+        int err = vsnprintf(msg, DVBPSI_MSG_SIZE, fmt, ap);
 #endif
         va_end(ap);
         if (err > DVBPSI_MSG_NONE) {
@@ -575,20 +571,28 @@ void dvbpsi_message(dvbpsi_t *dvbpsi, const dvbpsi_msg_level_t level, const char
         free(msg);                                              \
     } while(0);
 #   else
-#       define DVBPSI_MSG_COMMON                                \
+#       define DVBPSI_MSG_COMMON(level)                         \
     do {                                                        \
         va_list ap;                                             \
         va_start(ap, fmt);                                      \
         char *msg = malloc(DVBPSI_MSG_SIZE);                    \
-        if (msg == NULL) {                                      \
+        char *tmp = malloc(DVBPSI_MSG_SIZE);                    \
+        if (msg == NULL || tmp == NULL) {                       \
             va_end(ap);                                         \
+            if (msg)                                            \
+                free(msg);                                      \
+            if (tmp)                                            \
+                free(tmp);                                      \
             return;                                             \
         }                                                       \
-        if (snprintf(&msg, DVBPSI_MSG_SIZE, DVBPSI_MSG_FORMAT, src) < 0) { \
+        if (vsnprintf(tmp, DVBPSI_MSG_SIZE, fmt, ap) < 0) {     \
             va_end(ap);                                         \
+            free(tmp);                                          \
+            free(msg);                                          \
             return;                                             \
         }                                                       \
-        int err = vsnprintf(&msg, DVBPSI_MSG_SIZE, fmt, ap);    \
+        int err = snprintf(msg, DVBPSI_MSG_SIZE, DVBPSI_MSG_FORMAT, src, tmp); \
+        free(tmp);                                              \
         va_end(ap);                                             \
         if (err > 0) {                                          \
             if (dvbpsi->pf_message)                             \
-- 
1.9.1

From a2117900002bc8f9e0f821d872f8cd70ece67634 Mon Sep 17 00:00:00 2001
From: Timothy Gu <timothygu99@gmail.com>
Date: Thu, 26 Jun 2014 13:41:02 -0700
Subject: [PATCH 3/3] Add a separate check for [v]asprintf() instead of
 checking for _GNU_SOURCE

Platforms like i686-pc-mingw32 defines _GNU_SOURCE but does not contain
the functions.

Signed-off-by: Timothy Gu <timothygu99@gmail.com>
---
 configure.ac | 21 +++++++++++++++++++++
 src/dvbpsi.c |  6 +++---
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/configure.ac b/configure.ac
index 2a97e5b..f95c186 100644
--- a/configure.ac
+++ b/configure.ac
@@ -100,6 +100,27 @@ if test "${ac_cv_cpp_variadic_macros}" != "no"; then
     AC_DEFINE(HAVE_VARIADIC_MACROS, 1, Support for variadic macros)
 fi
 
+dnl Check for asprintf(). Systems with asprintf() must also have vasprintf(),
+dnl so it is not checked separately.
+AC_CACHE_CHECK([for asprintf()],
+    [ac_cv_asprintf],
+    [AC_COMPILE_IFELSE([
+        AC_LANG_SOURCE([[
+            #include <stdio.h>
+            #include <stdlib.h>
+            int main(void) {
+                char *text = NULL;
+                int ret = asprintf(&text, "test");
+                if (ret >= 0) free(text);
+                return 0;
+            }
+        ]])],
+        ac_cv_asprintf=yes,
+        ac_cv_asprintf=no)])
+if test "${ac_cv_asprintf}" != "no"; then
+    AC_DEFINE(HAVE_ASPRINTF, 1, [Support for asprintf() and vasprintf()])
+fi
+
 dnl
 dnl Generate Makefiles and other output files
 dnl
diff --git a/src/dvbpsi.c b/src/dvbpsi.c
index 141e3df..29ae2b0 100644
--- a/src/dvbpsi.c
+++ b/src/dvbpsi.c
@@ -508,7 +508,7 @@ bool dvbpsi_packet_push(dvbpsi_t *p_dvbpsi, uint8_t* p_data)
  *  1 is warning and errors
  *  2 is debug, warning and errors
  *****************************************************************************/
-#if !defined(_GNU_SOURCE)
+#if !defined(HAVE_ASPRINTF)
 #   define DVBPSI_MSG_SIZE 1024
 #endif
 
@@ -523,7 +523,7 @@ void dvbpsi_message(dvbpsi_t *dvbpsi, const dvbpsi_msg_level_t level, const char
         va_list ap;
         va_start(ap, fmt);
         char *msg = NULL;
-#if defined(_GNU_SOURCE)
+#if defined(HAVE_ASPRINTF)
         int err = vasprintf(&msg, fmt, ap);
 #else
         msg = malloc(DVBPSI_MSG_SIZE);
@@ -545,7 +545,7 @@ void dvbpsi_message(dvbpsi_t *dvbpsi, const dvbpsi_msg_level_t level, const char
 #else
 
 /* Common code for printing messages */
-#   if defined(_GNU_SOURCE)
+#   if defined(HAVE_ASPRINTF)
 #       define DVBPSI_MSG_COMMON(level)                         \
     do {                                                        \
         va_list ap;                                             \
-- 
1.9.1