From 809cd7851d20d423fc1e9dd9df6cd107d343d7fc Mon Sep 17 00:00:00 2001
From: Biswapriyo Nath <nathbap...@gmail.com>
Date: Fri, 11 Mar 2022 12:47:59 +0530
Subject: [PATCH] crt: Add comsupp library

Required for unrar project

Signed-off-by: Biswapriyo Nath <nathbap...@gmail.com>
---
 mingw-w64-crt/Makefile.am        | 17 +++++++
 mingw-w64-crt/libsrc/comsupp.cpp | 84 ++++++++++++++++++++++++++++++++
 2 files changed, 101 insertions(+)
 create mode 100644 mingw-w64-crt/libsrc/comsupp.cpp

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index 305d1b9..6b3382c 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -95,6 +95,7 @@ src_dfp_math =
 endif
 
 src_libbits=libsrc/bits.c
+src_libcomsupp=libsrc/comsupp.cpp
 src_libshell32=libsrc/shell32.c
 src_libdinput=libsrc/dinput_kbd.c libsrc/dinput_joy.c libsrc/dinput_joy2.c 
libsrc/dinput_mouse.c libsrc/dinput_mouse2.c
 src_libdinput8=libsrc/dinput_private.h libsrc/dinput_joy.c 
libsrc/dinput_joy2.c libsrc/dinput_mouse.c libsrc/dinput_mouse2.c 
libsrc/dinput_kbd.c
@@ -702,6 +703,10 @@ lib32_LIBRARIES += lib32/libbits.a
 lib32_libbits_a_SOURCES = $(src_libbits)
 lib32_libbits_a_CPPFLAGS=$(CPPFLAGS32) $(sysincludes)
 
+lib32_LIBRARIES += lib32/libcomsupp.a
+lib32_libcomsupp_a_SOURCES = $(src_libcomsupp)
+lib32_libcomsupp_a_CPPFLAGS=$(CPPFLAGS32) $(sysincludes)
+
 lib32_LIBRARIES += lib32/libshell32.a
 lib32_libshell32_a_SOURCES = $(src_libshell32)
 lib32_libshell32_a_AR = $(DTLIB32) && $(AR) $(ARFLAGS)
@@ -1033,6 +1038,10 @@ lib64_LIBRARIES += lib64/libbits.a
 lib64_libbits_a_SOURCES = $(src_libbits)
 lib64_libbits_a_CPPFLAGS=$(CPPFLAGS64) $(sysincludes)
 
+lib64_LIBRARIES += lib64/libcomsupp.a
+lib64_libcomsupp_a_SOURCES = $(src_libcomsupp)
+lib64_libcomsupp_a_CPPFLAGS=$(CPPFLAGS64) $(sysincludes)
+
 lib64_LIBRARIES += lib64/libshell32.a
 lib64_libshell32_a_SOURCES = $(src_libshell32)
 lib64_libshell32_a_CPPFLAGS=$(CPPFLAGS64) $(sysincludes)
@@ -1367,6 +1376,10 @@ libarm32_LIBRARIES += libarm32/libbits.a
 libarm32_libbits_a_SOURCES = $(src_libbits)
 libarm32_libbits_a_CPPFLAGS=$(CPPFLAGSARM32) $(sysincludes)
 
+libarm32_LIBRARIES += libarm32/libcomsupp.a
+libarm32_libcomsupp_a_SOURCES = $(src_libcomsupp)
+libarm32_libcomsupp_a_CPPFLAGS=$(CPPFLAGSARM32) $(sysincludes)
+
 libarm32_LIBRARIES += libarm32/libshell32.a
 libarm32_libshell32_a_SOURCES = $(src_libshell32)
 libarm32_libshell32_a_AR = $(DTDEFARM32) $(top_srcdir)/lib-common/shell32.def 
&& $(AR) $(ARFLAGS)
@@ -1649,6 +1662,10 @@ libarm64_LIBRARIES += libarm64/libbits.a
 libarm64_libbits_a_SOURCES = $(src_libbits)
 libarm64_libbits_a_CPPFLAGS=$(CPPFLAGSARM64) $(sysincludes)
 
+libarm64_LIBRARIES += libarm64/libcomsupp.a
+libarm64_libcomsupp_a_SOURCES = $(src_libcomsupp)
+libarm64_libcomsupp_a_CPPFLAGS=$(CPPFLAGSARM64) $(sysincludes)
+
 libarm64_LIBRARIES += libarm64/libshell32.a
 libarm64_libshell32_a_SOURCES = $(src_libshell32)
 libarm64_libshell32_a_AR = $(DTDEFARM64) $(top_srcdir)/lib-common/shell32.def 
&& $(AR) $(ARFLAGS)
diff --git a/mingw-w64-crt/libsrc/comsupp.cpp b/mingw-w64-crt/libsrc/comsupp.cpp
new file mode 100644
index 0000000..2d3d193
--- /dev/null
+++ b/mingw-w64-crt/libsrc/comsupp.cpp
@@ -0,0 +1,84 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+
+#include <windows.h>
+#include <comdef.h>
+
+/* From 
https://github.com/reactos/reactos/blob/master/sdk/lib/comsupp/comsupp.cpp */
+namespace _com_util {
+  BSTR WINAPI ConvertStringToBSTR(const char *pSrc) {
+    DWORD cwch;
+    BSTR wsOut(NULL);
+
+    if (!pSrc)
+      return NULL;
+
+    /* Compute the needed size with the NULL terminator */
+    cwch = ::MultiByteToWideChar(CP_ACP, 0, pSrc, -1, NULL, 0);
+    if (cwch == 0)
+      return NULL;
+
+    /* Allocate the BSTR (without the NULL terminator) */
+    wsOut = ::SysAllocStringLen(NULL, cwch - 1);
+    if (!wsOut) {
+      ::_com_issue_error(HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY));
+      return NULL;
+    }
+
+    /* Convert the string */
+    if (::MultiByteToWideChar(CP_ACP, 0, pSrc, -1, wsOut, cwch) == 0) {
+      /* We failed, clean everything up */
+      cwch = ::GetLastError();
+
+      ::SysFreeString(wsOut);
+      wsOut = NULL;
+
+      ::_com_issue_error(!IS_ERROR(cwch) ? HRESULT_FROM_WIN32(cwch) : cwch);
+    }
+
+    return wsOut;
+  }
+
+  char* WINAPI ConvertBSTRToString(BSTR pSrc) {
+    DWORD cb, cwch;
+    char *szOut = NULL;
+
+    if (!pSrc)
+      return NULL;
+
+    /* Retrieve the size of the BSTR with the NULL terminator */
+    cwch = ::SysStringLen(pSrc) + 1;
+
+    /* Compute the needed size with the NULL terminator */
+    cb = ::WideCharToMultiByte(CP_ACP, 0, pSrc, cwch, NULL, 0, NULL, NULL);
+    if (cb == 0) {
+      cwch = ::GetLastError();
+      ::_com_issue_error(!IS_ERROR(cwch) ? HRESULT_FROM_WIN32(cwch) : cwch);
+      return NULL;
+    }
+
+    /* Allocate the string */
+    szOut = (char*)::operator new(cb * sizeof(char));
+    if (!szOut) {
+      ::_com_issue_error(HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY));
+      return NULL;
+    }
+
+    /* Convert the string and NULL-terminate */
+    szOut[cb - 1] = '\0';
+    if (::WideCharToMultiByte(CP_ACP, 0, pSrc, cwch, szOut, cb, NULL, NULL) == 
0) {
+      /* We failed, clean everything up */
+      cwch = ::GetLastError();
+
+      ::operator delete(szOut);
+      szOut = NULL;
+
+      ::_com_issue_error(!IS_ERROR(cwch) ? HRESULT_FROM_WIN32(cwch) : cwch);
+    }
+
+    return szOut;
+  }
+}
-- 
2.35.1

_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to