LibreOffice Module setup_native (master) 1
upgrade.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include "shlxtmsi.hxx"
21
22#include <malloc.h>
23#include <assert.h>
24
25namespace
26{
27 // The provided GUID must be without surrounding '{}'
28 std::wstring GetGuidPart(const std::wstring& guid, int index)
29 {
30 assert((guid.length() == 36) && "No GUID or wrong format!");
31 assert(((index > -1) && (index < 5)) && "Out of range!");
32
33 if (index == 0) return std::wstring(guid.c_str(), 8);
34 if (index == 1) return std::wstring(guid.c_str() + 9, 4);
35 if (index == 2) return std::wstring(guid.c_str() + 14, 4);
36 if (index == 3) return std::wstring(guid.c_str() + 19, 4);
37 if (index == 4) return std::wstring(guid.c_str() + 24, 12);
38
39 return std::wstring();
40 }
41
42 void Swap(wchar_t* p1, wchar_t* p2)
43 {
44 wchar_t tmp = *p1;
45 *p1 = *p2;
46 *p2 = tmp;
47 }
48
49 std::wstring Invert(const std::wstring& str)
50 {
51 wchar_t* buff = static_cast<wchar_t*>(_alloca(str.length()*sizeof(wchar_t)));
52 wcsncpy(buff, str.c_str(), str.length());
53
54 wchar_t* front = buff;
55 wchar_t* back = buff + str.length() - 1;
56
57 while (front < back)
58 Swap(front++, back--);
59
60 return std::wstring(buff, str.length());
61 }
62
63 // Convert the upgrade code (which is a GUID) according
64 // to the way the windows installer does when writing it
65 // to the registry
66 // The first 8 bytes will be inverted, from the last
67 // 8 bytes always the nibbles will be inverted for further
68 // details look in the MSDN under compressed registry keys
69 std::wstring ConvertGuid(const std::wstring& guid)
70 {
71 std::wstring convertedGuid;
72
73 std::wstring part = GetGuidPart(guid, 0);
74 convertedGuid = Invert(part);
75
76 part = GetGuidPart(guid, 1);
77 convertedGuid += Invert(part);
78
79 part = GetGuidPart(guid, 2);
80 convertedGuid += Invert(part);
81
82 part = GetGuidPart(guid, 3);
83 convertedGuid += Invert(std::wstring(part.c_str(), 2));
84 convertedGuid += Invert(std::wstring(part.c_str() + 2, 2));
85
86 part = GetGuidPart(guid, 4);
87 int pos = 0;
88 for (int i = 0; i < 6; i++)
89 {
90 convertedGuid += Invert(std::wstring(part.c_str() + pos, 2));
91 pos += 2;
92 }
93 return convertedGuid;
94 }
95
96 bool IsSetMsiPropertyW(MSIHANDLE handle, const std::wstring& sProperty)
97 {
98 return (GetMsiPropertyW(handle, sProperty).length() > 0);
99 }
100
101 void SetMsiPropertyW(MSIHANDLE handle, const std::wstring& sProperty)
102 {
103 MsiSetPropertyW(handle, sProperty.c_str(), L"1");
104 }
105
106 bool RegistryKeyHasUpgradeSubKey(
107 HKEY hRootKey, const std::wstring& regKey, const std::wstring& upgradeKey)
108 {
109 HKEY hKey;
110 if (RegOpenKeyW(hRootKey, regKey.c_str(), &hKey) == ERROR_SUCCESS)
111 {
112 DWORD nSubKeys;
113 DWORD lLongestSubKey;
114
115 if (RegQueryInfoKeyW(
116 hKey, nullptr, nullptr, nullptr, &nSubKeys, &lLongestSubKey, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr) == ERROR_SUCCESS)
117 {
118 LPWSTR buffer = static_cast<LPWSTR>(_alloca((lLongestSubKey + 1)*sizeof(WCHAR)));
119
120 for (DWORD i = 0; i < nSubKeys; i++)
121 {
122 LONG ret = RegEnumKeyW(hKey, i, buffer, lLongestSubKey + 1);
123 if ((ret == ERROR_SUCCESS) && (buffer == upgradeKey))
124 return true;
125 }
126 }
127 }
128 return false;
129 }
130} // namespace
131
132extern "C" __declspec(dllexport) UINT __stdcall SetProductInstallMode(MSIHANDLE handle)
133{
134 std::wstring upgradeCode = GetMsiPropertyW(handle, L"UpgradeCode");
135 upgradeCode = ConvertGuid(std::wstring(upgradeCode.c_str() + 1, upgradeCode.length() - 2));
136
137 // MessageBoxW(NULL, upgradeCode.c_str(), "Debug", MB_OK);
138
139 if (RegistryKeyHasUpgradeSubKey(
140 HKEY_CURRENT_USER,
141 L"Software\\Microsoft\\Installer\\UpgradeCodes",
142 upgradeCode) && IsSetMsiPropertyW(handle, L"ALLUSERS"))
143 {
144 UnsetMsiPropertyW(handle, L"ALLUSERS");
145 // MessageBoxW(NULL, L"ALLUSERS removed", L"DEBUG", MB_OK);
146 }
147 else if (RegistryKeyHasUpgradeSubKey(
148 HKEY_LOCAL_MACHINE,
149 L"Software\\Classes\\Installer\\UpgradeCodes",
150 upgradeCode) && !IsSetMsiPropertyW(handle, L"ALLUSERS"))
151 {
152 SetMsiPropertyW(handle, L"ALLUSERS");
153 // MessageBoxW(NULL, L"ALLUSERS set", L"DEBUG", MB_OK);
154 }
155 return ERROR_SUCCESS;
156}
157
158/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
int i
Size & Swap(Size &rSize)
LONG
static std::wstring GetMsiPropertyW(MSIHANDLE handle, const std::wstring &sProperty)
Definition: shlxtmsi.hxx:29
static void SetMsiPropertyW(MSIHANDLE handle, const std::wstring &sProperty, const std::wstring &sValue)
Definition: shlxtmsi.hxx:47
static void UnsetMsiPropertyW(MSIHANDLE handle, const std::wstring &sProperty)
Definition: shlxtmsi.hxx:52
__declspec(dllexport) UINT __stdcall SetProductInstallMode(MSIHANDLE handle)
Definition: upgrade.cxx:132
size_t pos