[cpp-sp] branch main updated: CPPSP-10 Need to remove deprecated use of std::codecvt_utf8_utf16 in IIS plugin

Rod Widdowson rdw at steadingsoftware.com
Mon Apr 28 15:18:25 UTC 2025


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch main
in repository cpp-sp.

View the commit online:
http://git.shibboleth.net/view/?p=cpp-sp.git;a=commit;h=74045dc2caf2a73a9a93aa85df0f6b520e844a13

The following commit(s) were added to refs/heads/main by this push:
     new 74045dc2 CPPSP-10 Need to remove deprecated use of std::codecvt_utf8_utf16 in IIS plugin
74045dc2 is described below

commit 74045dc2caf2a73a9a93aa85df0f6b520e844a13
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Mon Apr 28 14:13:42 2025 +0100

    CPPSP-10 Need to remove deprecated use of std::codecvt_utf8_utf16 in IIS plugin
    
    https://shibboleth.atlassian.net/browse/CPPSP-10
    
    A bit cumbersome because the different places do different logging
---
 Projects/vc22/iis.vcxproj                       | 17 ++++----
 iis7_shib/IIS7Request.cpp                       | 55 +++++++++++++++++++++----
 iis7_shib/ShibHttpModule.cpp                    | 30 ++++++++++++--
 iis7_shib/ShibUser.cpp                          |  6 +--
 iis7_shib/headers/IIS7Request.hpp               |  3 +-
 iis7_shib/headers/IIS7_shib.hpp                 |  3 +-
 iis7_shib/headers/ShibUser.hpp                  |  2 +-
 iis7_shib/register.cpp                          |  2 -
 shibsp/remoting/impl/WinHTTPRemotingService.cpp |  4 +-
 9 files changed, 86 insertions(+), 36 deletions(-)

diff --git a/Projects/vc22/iis.vcxproj b/Projects/vc22/iis.vcxproj
index 778f9eae..e370535f 100644
--- a/Projects/vc22/iis.vcxproj
+++ b/Projects/vc22/iis.vcxproj
@@ -146,8 +146,8 @@
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
     <ClCompile>
       <WarningLevel>Level3</WarningLevel>
+      <PreprocessorDefinitions>WIN32;_DEBUG;IIS_EXPORTS;_WINDOWS;_USRDLL;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>	
       <SDLCheck>true</SDLCheck>
-      <PreprocessorDefinitions>_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING;WIN32;_DEBUG;IIS_EXPORTS;_WINDOWS;_USRDLL;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <ConformanceMode>true</ConformanceMode>
       <LanguageStandard>stdcpp17</LanguageStandard>
     </ClCompile>
@@ -167,8 +167,8 @@
       <WarningLevel>Level3</WarningLevel>
       <FunctionLevelLinking>true</FunctionLevelLinking>
       <IntrinsicFunctions>true</IntrinsicFunctions>
+      <PreprocessorDefinitions>WIN32;NDEBUG;IIS_EXPORTS;_WINDOWS;_USRDLL;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <SDLCheck>true</SDLCheck>
-      <PreprocessorDefinitions>_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING;WIN32;NDEBUG;IIS_EXPORTS;_WINDOWS;_USRDLL;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <ConformanceMode>true</ConformanceMode>
       <LanguageStandard>stdcpp17</LanguageStandard>
     </ClCompile>
@@ -188,8 +188,8 @@
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
     <ClCompile>
       <WarningLevel>Level3</WarningLevel>
+      <PreprocessorDefinitions>_DEBUG;IIS_EXPORTS;_WINDOWS;_USRDLL;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <SDLCheck>true</SDLCheck>
-      <PreprocessorDefinitions>_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING;_DEBUG;IIS_EXPORTS;_WINDOWS;_USRDLL;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <ConformanceMode>true</ConformanceMode>
       <LanguageStandard>stdcpp17</LanguageStandard>
     </ClCompile>
@@ -208,8 +208,7 @@
     <ClCompile>
       <WarningLevel>Level3</WarningLevel>
       <SDLCheck>true</SDLCheck>
-      <PreprocessorDefinitions>_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING;_DEBUG;IIS_EXPORTS;_WINDOWS;_USRDLL;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
-      <ConformanceMode>true</ConformanceMode>
+      <PreprocessorDefinitions>_DEBUG;IIS_EXPORTS;_WINDOWS;_USRDLL;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>      <ConformanceMode>true</ConformanceMode>
       <LanguageStandard>stdcpp17</LanguageStandard>
     </ClCompile>
     <Link>
@@ -229,8 +228,7 @@
       <FunctionLevelLinking>true</FunctionLevelLinking>
       <IntrinsicFunctions>true</IntrinsicFunctions>
       <SDLCheck>true</SDLCheck>
-      <PreprocessorDefinitions>_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING;NDEBUG;IIS_EXPORTS;_WINDOWS;_USRDLL;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
-      <ConformanceMode>true</ConformanceMode>
+      <PreprocessorDefinitions>NDEBUG;IIS_EXPORTS;_WINDOWS;_USRDLL;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>      <ConformanceMode>true</ConformanceMode>
       <LanguageStandard>stdcpp17</LanguageStandard>
     </ClCompile>
     <Link>
@@ -251,8 +249,7 @@
       <WarningLevel>Level3</WarningLevel>
       <FunctionLevelLinking>true</FunctionLevelLinking>
       <IntrinsicFunctions>true</IntrinsicFunctions>
-      <SDLCheck>true</SDLCheck>
-      <PreprocessorDefinitions>_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING;NDEBUG;IIS_EXPORTS;_WINDOWS;_USRDLL;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <PreprocessorDefinitions>NDEBUG;IIS_EXPORTS;_WINDOWS;_USRDLL;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>      <SDLCheck>true</SDLCheck>
       <ConformanceMode>true</ConformanceMode>
       <LanguageStandard>stdcpp17</LanguageStandard>
     </ClCompile>
@@ -272,4 +269,4 @@
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
\ No newline at end of file
+</Project>
diff --git a/iis7_shib/IIS7Request.cpp b/iis7_shib/IIS7Request.cpp
index 56569678..8ab26353 100644
--- a/iis7_shib/IIS7Request.cpp
+++ b/iis7_shib/IIS7Request.cpp
@@ -27,7 +27,6 @@
 #include <shibsp/exceptions.h>
 #include <shibsp/util/Misc.h>
 
-#include <codecvt> // 16 bit to 8 bit chars
 #include "IIS7Request.hpp"
 #include "ShibHttpModule.hpp"
 #include "ShibUser.hpp"
@@ -162,9 +161,9 @@ void IIS7Request::setHeader(const char* name, const char* value)
             throwError("setHeader (Header)", hr);
         }
     }
+
     if (isUseVariables()) {
-        std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
-        const wstring wValue(converter.from_bytes(value));
+        const wstring wValue(utf8ToUtf16(value));
         const HRESULT hr(m_ctx->SetServerVariable(const_cast<char*>(name), wValue.c_str()));
         if (FAILED(hr)) {
             throwError("setHeader (Variable)", hr);
@@ -174,7 +173,7 @@ void IIS7Request::setHeader(const char* name, const char* value)
             const string str(value);
             boost::tokenizer<boost::escaped_list_separator<char>> tok(str, boost::escaped_list_separator<char>('\\', ';', '"'));
             for (boost::tokenizer<boost::escaped_list_separator<char>>::iterator it = tok.begin(); it != tok.end(); ++it) {
-                m_roles.insert(converter.from_bytes(*it));
+                m_roles.insert(utf8ToUtf16(it->c_str()));
             }
         }
     }
@@ -191,9 +190,8 @@ void IIS7Request::setRemoteUser(const char* user)
     if (auth) {
         string authnRole(m_site.getString(ModuleConfig::AUTHENTICATED_ROLE_PROP_NAME,
             ModuleConfig::AUTHENTICATED_ROLE_PROP_DEFAULT));
-        std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
-        m_roles.insert(converter.from_bytes(authnRole));
-        auth->SetUser(new ShibUser(user, m_roles));
+        m_roles.insert(utf8ToUtf16(authnRole.c_str()));
+        auth->SetUser(new ShibUser(utf8ToUtf16(user), m_roles));
     }
     else {
         log(Priority::SHIB_ERROR, "attempt to set REMOTE_USER in an inappropriate context");
@@ -275,8 +273,7 @@ string IIS7Request::getSecureHeader(const char* name) const
         DWORD len;
         HRESULT hr = m_ctx->GetServerVariable(name, &p, &len);
         if (SUCCEEDED(hr) && p) {
-            std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
-            return converter.to_bytes(p);
+            return utf16ToUtf8(p);
         }
         return "";
     }
@@ -450,6 +447,46 @@ long IIS7Request::sendRedirect(const char* url)
     return RQ_NOTIFICATION_FINISH_REQUEST;
 }
 
+wstring IIS7Request::utf8ToUtf16(const char* input) const {
+    DWORD sizeNeeded = MultiByteToWideChar(CP_UTF8, 0, input, -1, nullptr, 0);
+
+    LPWSTR output = new WCHAR[sizeNeeded + 1];
+    if (output == nullptr) {
+        log(Priority::SHIB_CRIT, "Out of memory allocating conversion buffer");
+        throw runtime_error("Utf8toUtf16 conversion failed");
+    }
+    ZeroMemory(output, sizeof(WCHAR) * (sizeNeeded + 1));
+
+    if (MultiByteToWideChar(CP_UTF8, 0, input, -1, output, sizeNeeded) == 0) {
+        log(Priority::SHIB_CRIT, "MultiByteToWideChar failure");
+        throw runtime_error("Utf8toUtf16 conversion failed");
+    }
+
+    wstring result(output);
+    delete[] output;
+    return result;
+}
+
+string IIS7Request::utf16ToUtf8(const wstring input) const {
+    DWORD sizeNeeded = WideCharToMultiByte(CP_UTF8, 0, input.c_str(), -1, nullptr, 0, nullptr, nullptr);
+
+    LPCH output = new CHAR[sizeNeeded + 1];
+    if (output == nullptr) {
+        log(Priority::SHIB_CRIT, "Out of memory allocating conversion buffer");
+        throw runtime_error("Utf16toUtf8 conversion failed");
+    }
+    ZeroMemory(output, sizeNeeded + 1);
+
+    if (WideCharToMultiByte(CP_UTF8, 0, input.c_str(), -1, output, sizeNeeded, nullptr, nullptr) == 0) {
+        log(Priority::SHIB_CRIT, "WideCharToMultiByte failure");
+        throw runtime_error("Utf16toUtf8 conversion failed");
+    }
+
+    string result(output);
+    delete[] output;
+    return result;
+}
+
 void IIS7Request::logFatal(const string& operation, HRESULT hr) const
 {
     string msg(operation + " failed: " + boost::lexical_cast<string>(hr));
diff --git a/iis7_shib/ShibHttpModule.cpp b/iis7_shib/ShibHttpModule.cpp
index 50a102c5..78ced932 100644
--- a/iis7_shib/ShibHttpModule.cpp
+++ b/iis7_shib/ShibHttpModule.cpp
@@ -31,8 +31,6 @@
 #include <boost/algorithm/string.hpp>
 #include <boost/lexical_cast.hpp>
 
-#include <codecvt>
-
 using namespace Config;
 using namespace std;
 
@@ -47,9 +45,33 @@ ShibHttpModule::DoHandler(
     if (!site)
         return RQ_NOTIFICATION_CONTINUE;
 
+    //
+    // Standard Windows UTFS -> wstring convert with different error handling
+    //
     string prefix(site->getString(ModuleConfig::HANDLER_PREFIX_PROP_NAME, ModuleConfig::HANDLER_PREFIX_PROP_DEFAULT));
-    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
-    wstring handlerPrefix = converter.from_bytes(prefix);
+    DWORD sizeNeeded = MultiByteToWideChar(CP_UTF8, 0, prefix.c_str(), -1, nullptr, 0);
+
+    LPWSTR output = new WCHAR[sizeNeeded + 1];
+    if (output == nullptr) {
+        (void)pHttpContext->GetResponse()->SetStatus(static_cast<USHORT>(HTTPResponse::SHIBSP_HTTP_STATUS_ERROR), 
+                                                     "Fatal Server Error: MultiByteToWideChar failed",
+                                                     0,
+                                                     HRESULT_FROM_WIN32(GetLastError()));
+        return RQ_NOTIFICATION_FINISH_REQUEST;
+    }
+    ZeroMemory(output, sizeof(WCHAR) * (sizeNeeded + 1));
+
+    if (MultiByteToWideChar(CP_UTF8, 0, prefix.c_str(), -1, output, sizeNeeded) == 0) {
+        (void)pHttpContext->GetResponse()->SetStatus(static_cast<USHORT>(HTTPResponse::SHIBSP_HTTP_STATUS_ERROR),
+                                                     "Fatal Server Error: MultiByteToWideChar failed",
+                                                     0,
+                                                     HRESULT_FROM_WIN32(GetLastError()));
+        return RQ_NOTIFICATION_FINISH_REQUEST;
+    }
+
+    wstring handlerPrefix(output);
+    delete[] output;
+
 
     // Quickly check the URL.
     // Calling GetScriptName is safe here since we don't care about "visible to other filters" paths, just our path.
diff --git a/iis7_shib/ShibUser.cpp b/iis7_shib/ShibUser.cpp
index 1b43d7ae..d39a3a06 100644
--- a/iis7_shib/ShibUser.cpp
+++ b/iis7_shib/ShibUser.cpp
@@ -20,13 +20,9 @@
 
 #include "IIS7_shib.hpp"
 #include "ShibUser.hpp"
-#include <codecvt> // 16 bit to 8 bit and vice versa chars
 
-
-ShibUser::ShibUser(std::string name, set<wstring> roles) : m_refCount(1), m_roles(roles)
+ShibUser::ShibUser(std::wstring name, set<wstring> roles) : m_refCount(1), m_roles(roles), m_username(name)
 {
-    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
-    m_username = converter.from_bytes(string(name));
 }
 
 PCWSTR
diff --git a/iis7_shib/headers/IIS7Request.hpp b/iis7_shib/headers/IIS7Request.hpp
index ef975dc1..d8e109ef 100644
--- a/iis7_shib/headers/IIS7Request.hpp
+++ b/iis7_shib/headers/IIS7Request.hpp
@@ -93,5 +93,6 @@ protected:
 private:
     void logFatal(const string& operation, HRESULT hr) const;
     void throwError(const string& operation, HRESULT hr) const;
-
+    wstring utf8ToUtf16(const char* input) const;
+    string  utf16ToUtf8(const wstring input) const;
 };
diff --git a/iis7_shib/headers/IIS7_shib.hpp b/iis7_shib/headers/IIS7_shib.hpp
index 53c2632c..145f6f36 100644
--- a/iis7_shib/headers/IIS7_shib.hpp
+++ b/iis7_shib/headers/IIS7_shib.hpp
@@ -62,4 +62,5 @@ BOOL LogEvent(
     WORD  wType,
     DWORD  dwEventID,
     Priority::Value priority,
-    LPCSTR  message);
\ No newline at end of file
+    LPCSTR  message);
+
diff --git a/iis7_shib/headers/ShibUser.hpp b/iis7_shib/headers/ShibUser.hpp
index 09c82586..8d1d226c 100644
--- a/iis7_shib/headers/ShibUser.hpp
+++ b/iis7_shib/headers/ShibUser.hpp
@@ -26,7 +26,7 @@ class ShibUser : public IHttpUser {
     // Also, a testbed for Roles Based AuthN.
 
 public:
-    ShibUser(std::string username, set<wstring> roles);
+    ShibUser(wstring username, set<wstring> roles);
             
     PCWSTR
     GetRemoteUserName(
diff --git a/iis7_shib/register.cpp b/iis7_shib/register.cpp
index 51d576b6..690b95ff 100644
--- a/iis7_shib/register.cpp
+++ b/iis7_shib/register.cpp
@@ -20,8 +20,6 @@
 // Project
 #include "IIS7_shib.hpp"
 #include "ShibHttpModule.hpp"
-#pragma warning(disable: 4996)
-#include <codecvt> // 16 bit to 8 bit and vice versa chars
 #include <boost/algorithm/string.hpp>
 #include <boost/lexical_cast.hpp>
 #include "NativeEventLog.h"
diff --git a/shibsp/remoting/impl/WinHTTPRemotingService.cpp b/shibsp/remoting/impl/WinHTTPRemotingService.cpp
index 205dfc8e..3f7c7793 100644
--- a/shibsp/remoting/impl/WinHTTPRemotingService.cpp
+++ b/shibsp/remoting/impl/WinHTTPRemotingService.cpp
@@ -29,7 +29,6 @@
 #include "util/BoostPropertySet.h"
 
 #include <stdexcept>
-#include <codecvt> // 16 bit to 8 bit chars
 #include <boost/algorithm/string.hpp>
 #include <boost/property_tree/ptree.hpp>
 
@@ -106,12 +105,11 @@ namespace shibsp {
 };
 
 wstring WinHTTPRemotingService::utf8ToUtf16(const char* input) const {
-    // IDK why this cannot be exbedded.
     DWORD sizeNeeded = MultiByteToWideChar(CP_UTF8, 0, input, -1, nullptr, 0);
 
     LPWSTR output = new WCHAR[sizeNeeded + 1];
     if (output == nullptr) {
-        m_log.crit("Out of memrory allocating %d butes for conversion buffer", sizeNeeded + 1);
+        m_log.crit("Out of memory allocating %d bytes for conversion buffer", sizeNeeded + 1);
         throw runtime_error("Utf8toUtf16 conversion failed");
     }
     ZeroMemory(output, sizeof(WCHAR) * (sizeNeeded + 1));

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list