| 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 | STDMETHODIMP UtilSearchParseFromXml( |
| 7 | __in UTIL_SEARCHES* pSearches, |
| 8 | __in IXMLDOMNode* pixnBootstrapperExtension |
| 9 | ) |
| 10 | { |
| 11 | HRESULT hr = S_OK; |
| 12 | IXMLDOMNodeList* pixnNodes = NULL; |
| 13 | IXMLDOMNode* pixnNode = NULL; |
| 14 | DWORD cNodes = 0; |
| 15 | BSTR bstrNodeName = NULL; |
| 16 | LPWSTR scz = NULL; |
| 17 | |
| 18 | // Select Util search nodes. |
| 19 | hr = XmlSelectNodes(pixnBootstrapperExtension, L"WixWindowsFeatureSearch", &pixnNodes); |
| 20 | BextExitOnFailure(hr, "Failed to select Util search nodes."); |
| 21 | |
| 22 | // Get Util search node count. |
| 23 | hr = pixnNodes->get_length((long*)&cNodes); |
| 24 | BextExitOnFailure(hr, "Failed to get Util search node count."); |
| 25 | |
| 26 | if (!cNodes) |
| 27 | { |
| 28 | ExitFunction(); |
| 29 | } |
| 30 | |
| 31 | // Allocate memory for searches. |
| 32 | pSearches->rgSearches = (UTIL_SEARCH*)MemAlloc(sizeof(UTIL_SEARCH) * cNodes, TRUE); |
| 33 | BextExitOnNull(pSearches->rgSearches, hr, E_OUTOFMEMORY, "Failed to allocate memory for search structs."); |
| 34 | |
| 35 | pSearches->cSearches = cNodes; |
| 36 | |
| 37 | // Parse search elements. |
| 38 | for (DWORD i = 0; i < cNodes; ++i) |
| 39 | { |
| 40 | UTIL_SEARCH* pSearch = &pSearches->rgSearches[i]; |
| 41 | |
| 42 | hr = XmlNextElement(pixnNodes, &pixnNode, &bstrNodeName); |
| 43 | BextExitOnFailure(hr, "Failed to get next node."); |
| 44 | |
| 45 | // @Id |
| 46 | hr = XmlGetAttributeEx(pixnNode, L"Id", &pSearch->sczId); |
| 47 | BextExitOnFailure(hr, "Failed to get @Id."); |
| 48 | |
| 49 | // Read type specific attributes. |
| 50 | if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, bstrNodeName, -1, L"WixWindowsFeatureSearch", -1)) |
| 51 | { |
| 52 | pSearch->Type = UTIL_SEARCH_TYPE_WINDOWS_FEATURE_SEARCH; |
| 53 | |
| 54 | // @Type |
| 55 | hr = XmlGetAttributeEx(pixnNode, L"Type", &scz); |
| 56 | BextExitOnFailure(hr, "Failed to get @Type."); |
| 57 | |
| 58 | if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"sha2CodeSigning", -1)) |
| 59 | { |
| 60 | pSearch->WindowsFeatureSearch.type = UTIL_WINDOWS_FEATURE_SEARCH_TYPE_SHA2_CODE_SIGNING; |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | BextExitWithRootFailure(hr, E_INVALIDARG, "Invalid value for @Type: %ls", scz); |
| 65 | } |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | BextExitWithRootFailure(hr, E_UNEXPECTED, "Unexpected element name: %ls", bstrNodeName); |
| 70 | } |
| 71 | |
| 72 | // prepare next iteration |
| 73 | ReleaseNullObject(pixnNode); |
| 74 | ReleaseNullBSTR(bstrNodeName); |
| 75 | } |
| 76 | |
| 77 | LExit: |
| 78 | ReleaseStr(scz); |
| 79 | ReleaseBSTR(bstrNodeName); |
| 80 | ReleaseObject(pixnNode); |
| 81 | ReleaseObject(pixnNodes); |
| 82 | |
| 83 | return hr; |
| 84 | } |
| 85 | |
| 86 | void UtilSearchUninitialize( |
| 87 | __in UTIL_SEARCHES* pSearches |
| 88 | ) |
| 89 | { |
| 90 | if (pSearches->rgSearches) |
| 91 | { |
| 92 | for (DWORD i = 0; i < pSearches->cSearches; ++i) |
| 93 | { |
| 94 | UTIL_SEARCH* pSearch = &pSearches->rgSearches[i]; |
| 95 | |
| 96 | ReleaseStr(pSearch->sczId); |
| 97 | } |
| 98 | MemFree(pSearches->rgSearches); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | STDMETHODIMP UtilSearchExecute( |
| 103 | __in UTIL_SEARCHES* pSearches, |
| 104 | __in LPCWSTR wzSearchId, |
| 105 | __in LPCWSTR wzVariable, |
| 106 | __in IBootstrapperExtensionEngine* pEngine |
| 107 | ) |
| 108 | { |
| 109 | HRESULT hr = S_OK; |
| 110 | UTIL_SEARCH* pSearch = NULL; |
| 111 | |
| 112 | hr = UtilSearchFindById(pSearches, wzSearchId, &pSearch); |
| 113 | BextExitOnFailure(hr, "Search id '%ls' is unknown to the util extension.", wzSearchId); |
| 114 | |
| 115 | switch (pSearch->Type) |
| 116 | { |
| 117 | case UTIL_SEARCH_TYPE_WINDOWS_FEATURE_SEARCH: |
| 118 | switch (pSearch->WindowsFeatureSearch.type) |
| 119 | { |
| 120 | case UTIL_WINDOWS_FEATURE_SEARCH_TYPE_SHA2_CODE_SIGNING: |
| 121 | hr = UtilPerformDetectSHA2CodeSigning(wzVariable, pSearch, pEngine); |
| 122 | break; |
| 123 | default: |
| 124 | hr = E_UNEXPECTED; |
| 125 | } |
| 126 | break; |
| 127 | default: |
| 128 | hr = E_UNEXPECTED; |
| 129 | } |
| 130 | |
| 131 | LExit: |
| 132 | return hr; |
| 133 | } |
| 134 | |
| 135 | STDMETHODIMP UtilSearchFindById( |
| 136 | __in UTIL_SEARCHES* pSearches, |
| 137 | __in LPCWSTR wzId, |
| 138 | __out UTIL_SEARCH** ppSearch |
| 139 | ) |
| 140 | { |
| 141 | HRESULT hr = S_OK; |
| 142 | |
| 143 | for (DWORD i = 0; i < pSearches->cSearches; ++i) |
| 144 | { |
| 145 | UTIL_SEARCH* pSearch = &pSearches->rgSearches[i]; |
| 146 | |
| 147 | if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, pSearch->sczId, -1, wzId, -1)) |
| 148 | { |
| 149 | *ppSearch = pSearch; |
| 150 | ExitFunction1(hr = S_OK); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | hr = E_NOTFOUND; |
| 155 | |
| 156 | LExit: |
| 157 | return hr; |
| 158 | } |