From 8b6f46af50651e0b1faf7adf94a179c6e16a05b1 Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Thu, 26 May 2022 17:31:36 -0500 Subject: PathEnsureQuoted is not generally useful. --- src/ext/NetFx/ca/netfxca.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++ src/ext/NetFx/ca/precomp.h | 1 + 2 files changed, 59 insertions(+) (limited to 'src/ext') diff --git a/src/ext/NetFx/ca/netfxca.cpp b/src/ext/NetFx/ca/netfxca.cpp index 1182464e..f0704790 100644 --- a/src/ext/NetFx/ca/netfxca.cpp +++ b/src/ext/NetFx/ca/netfxca.cpp @@ -280,6 +280,64 @@ LExit: return hr; } +// This has netfxca specific functionality, like turning " into "" and leaving an unescaped \ at the end of a directory. +static HRESULT PathEnsureQuoted( + __inout LPWSTR* ppszPath, + __in BOOL fDirectory + ) +{ + Assert(ppszPath && *ppszPath); + + HRESULT hr = S_OK; + size_t cchPath = 0; + + hr = ::StringCchLengthW(*ppszPath, STRSAFE_MAX_CCH, &cchPath); + ExitOnFailure(hr, "Failed to get the length of the path."); + + // Handle simple special cases. + if (0 == cchPath || (1 == cchPath && L'"' == (*ppszPath)[0])) + { + hr = StrAllocString(ppszPath, L"\"\"", 2); + ExitOnFailure(hr, "Failed to allocate a quoted empty string."); + + ExitFunction(); + } + + if (L'"' != (*ppszPath)[0]) + { + hr = StrAllocPrefix(ppszPath, L"\"", 1); + ExitOnFailure(hr, "Failed to allocate an opening quote."); + + // Add a char for the opening quote. + ++cchPath; + } + + if (L'"' != (*ppszPath)[cchPath - 1]) + { + hr = StrAllocConcat(ppszPath, L"\"", 1); + ExitOnFailure(hr, "Failed to allocate a closing quote."); + + // Add a char for the closing quote. + ++cchPath; + } + + if (fDirectory) + { + if (L'\\' != (*ppszPath)[cchPath - 2]) + { + // Change the last char to a backslash and re-append the closing quote. + (*ppszPath)[cchPath - 1] = L'\\'; + + hr = StrAllocConcat(ppszPath, L"\"", 1); + ExitOnFailure(hr, "Failed to allocate another closing quote after the backslash."); + } + } + +LExit: + + return hr; +} + static HRESULT CreateInstallCommand( __out LPWSTR* ppwzCommandLine, __in LPCWSTR pwzNgenPath, diff --git a/src/ext/NetFx/ca/precomp.h b/src/ext/NetFx/ca/precomp.h index 4a83c164..f7b537ed 100644 --- a/src/ext/NetFx/ca/precomp.h +++ b/src/ext/NetFx/ca/precomp.h @@ -4,6 +4,7 @@ #include #include +#include #include "wcautil.h" #include "fileutil.h" -- cgit v1.2.3-55-g6feb