aboutsummaryrefslogtreecommitdiff
path: root/src/libs/dutil/WixToolset.DUtil/envutil.cpp
diff options
context:
space:
mode:
authorSean Hall <r.sean.hall@gmail.com>2022-06-03 17:49:33 -0500
committerSean Hall <r.sean.hall@gmail.com>2022-06-07 19:44:36 -0500
commit266b097c0b0a13dd4934f55f61cad62ffcbb953d (patch)
tree21400e8e1f7a6a5ebbc1abaacb40c472fc0b9fbc /src/libs/dutil/WixToolset.DUtil/envutil.cpp
parent584213c5ffeca09b3fe24bd5e92f73fd057ac642 (diff)
downloadwix-266b097c0b0a13dd4934f55f61cad62ffcbb953d.tar.gz
wix-266b097c0b0a13dd4934f55f61cad62ffcbb953d.tar.bz2
wix-266b097c0b0a13dd4934f55f61cad62ffcbb953d.zip
REG_EXPAND_SZ values are not necessarily a path.
Diffstat (limited to 'src/libs/dutil/WixToolset.DUtil/envutil.cpp')
-rw-r--r--src/libs/dutil/WixToolset.DUtil/envutil.cpp78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/libs/dutil/WixToolset.DUtil/envutil.cpp b/src/libs/dutil/WixToolset.DUtil/envutil.cpp
new file mode 100644
index 00000000..aa9da233
--- /dev/null
+++ b/src/libs/dutil/WixToolset.DUtil/envutil.cpp
@@ -0,0 +1,78 @@
1// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3#include "precomp.h"
4
5
6// Exit macros
7#define EnvExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_ENVUTIL, x, s, __VA_ARGS__)
8#define EnvExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_ENVUTIL, x, s, __VA_ARGS__)
9#define EnvExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_ENVUTIL, x, s, __VA_ARGS__)
10#define EnvExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_ENVUTIL, x, s, __VA_ARGS__)
11#define EnvExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_ENVUTIL, x, s, __VA_ARGS__)
12#define EnvExitWithRootFailure(x, e, s, ...) ExitWithRootFailureSource(DUTIL_SOURCE_ENVUTIL, x, e, s, __VA_ARGS__)
13#define EnvExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_ENVUTIL, x, s, __VA_ARGS__)
14#define EnvExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_ENVUTIL, p, x, e, s, __VA_ARGS__)
15#define EnvExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_ENVUTIL, p, x, s, __VA_ARGS__)
16#define EnvExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_ENVUTIL, p, x, e, s, __VA_ARGS__)
17#define EnvExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_ENVUTIL, p, x, s, __VA_ARGS__)
18#define EnvExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_ENVUTIL, e, x, s, __VA_ARGS__)
19#define EnvExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_ENVUTIL, g, x, s, __VA_ARGS__)
20
21#define ENV_GOOD_ENOUGH 64
22
23DAPI_(HRESULT) EnvExpandEnvironmentStrings(
24 __in LPCWSTR wzSource,
25 __out LPWSTR* psczExpanded,
26 __out_opt SIZE_T* pcchExpanded
27 )
28{
29 HRESULT hr = S_OK;
30 DWORD cch = 0;
31 DWORD cchExpanded = 0;
32 SIZE_T cchMax = 0;
33
34 if (*psczExpanded)
35 {
36 hr = StrMaxLength(*psczExpanded, &cchMax);
37 EnvExitOnFailure(hr, "Failed to get max length of input buffer.");
38
39 cchExpanded = (DWORD)min(DWORD_MAX, cchMax);
40 }
41 else
42 {
43 cchExpanded = ENV_GOOD_ENOUGH;
44
45 hr = StrAlloc(psczExpanded, cchExpanded);
46 EnvExitOnFailure(hr, "Failed to allocate space for expanded path.");
47 }
48
49 cch = ::ExpandEnvironmentStringsW(wzSource, *psczExpanded, cchExpanded);
50 if (!cch)
51 {
52 EnvExitWithLastError(hr, "Failed to expand environment variables in string: %ls", wzSource);
53 }
54 else if (cchExpanded < cch)
55 {
56 cchExpanded = cch;
57 hr = StrAlloc(psczExpanded, cchExpanded);
58 EnvExitOnFailure(hr, "Failed to re-allocate more space for expanded path.");
59
60 cch = ::ExpandEnvironmentStringsW(wzSource, *psczExpanded, cchExpanded);
61 if (!cch)
62 {
63 EnvExitWithLastError(hr, "Failed to expand environment variables in string: %ls", wzSource);
64 }
65 else if (cchExpanded < cch)
66 {
67 EnvExitWithRootFailure(hr, HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER), "Failed to allocate buffer for expanded string.");
68 }
69 }
70
71 if (pcchExpanded)
72 {
73 *pcchExpanded = cch;
74 }
75
76LExit:
77 return hr;
78}