registry.c 82.9 KB
Newer Older
1 2 3
/*
 * Implementation of the Microsoft Installer (msi.dll)
 *
4
 * Copyright 2005 Mike McCormack for CodeWeavers
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright 2005 Aric Stewart for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 */

#include <stdarg.h>

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "winreg.h"
#include "winnls.h"
#include "shlwapi.h"
#include "wine/debug.h"
#include "msi.h"
#include "msipriv.h"
#include "wincrypt.h"
#include "wine/unicode.h"
#include "winver.h"
#include "winuser.h"
38
#include "sddl.h"
39 40 41 42 43 44 45 46

WINE_DEFAULT_DEBUG_CHANNEL(msi);

/* 
 * This module will be all the helper functions for registry access by the
 * installer bits. 
 */

47
static const WCHAR szUserDataFeatures_fmt[] = {
48 49 50 51
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
    'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
    '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s','\\','F','e','a','t','u','r','e','s',0};
52

53
static const WCHAR szUserDataComp_fmt[] = {
54 55 56 57
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
    'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
    '%','s','\\','C','o','m','p','o','n','e','n','t','s','\\','%','s',0};
58

59
static const WCHAR szUserDataComponents_fmt[] = {
60 61 62 63
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
    'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
    '%','s','\\','C','o','m','p','o','n','e','n','t','s',0};
64

65
static const WCHAR szUserDataProd_fmt[] = {
66 67 68 69
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
    'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
    '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s',0};
70

71
static const WCHAR szUserDataProducts_fmt[] = {
72 73 74 75
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
    'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
    '%','s','\\','P','r','o','d','u','c','t','s',0};
76

77
static const WCHAR szUserDataPatch_fmt[] = {
78 79 80 81
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
    'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
    '%','s','\\','P','a','t','c','h','e','s','\\','%','s',0};
82

83
static const WCHAR szUserDataPatches_fmt[] = {
84 85 86 87
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
    'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
    '%','s','\\','P','a','t','c','h','e','s',0};
88

89
static const WCHAR szUserDataProductPatches_fmt[] = {
90 91 92 93
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
    'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
    '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s','\\','P','a','t','c','h','e','s',0};
94

95
static const WCHAR szInstallProperties_fmt[] = {
96 97 98 99 100
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
    'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
    '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s','\\',
    'I','n','s','t','a','l','l','P','r','o','p','e','r','t','i','e','s',0};
101

102
static const WCHAR szInstaller_LocalManagedProd_fmt[] = {
103 104 105 106
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
    'I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d','\\','%','s','\\',
    'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\','%','s',0};
107

108
static const WCHAR szInstaller_LocalManagedFeat_fmt[] = {
109 110 111 112
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
    'I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d','\\','%','s','\\',
    'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\','%','s',0};
113

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
static const WCHAR szInstaller_Products[] = {
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
    'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};

static const WCHAR szInstaller_Patches[] = {
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
    'I','n','s','t','a','l','l','e','r','\\','P','a','t','c','h','e','s',0};

static const WCHAR szInstaller_LocalClassesProducts[] = {
    'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
    'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};

static const WCHAR szInstaller_LocalClassesFeatures[] = {
    'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
    'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s',0};

132 133 134 135 136 137 138 139 140
static const WCHAR szInstaller_LocalClassesProd[] = {
    'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
    'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\',0};

static const WCHAR szInstaller_LocalClassesFeat[] = {
    'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
    'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};

static const WCHAR szInstaller_ClassesUpgradeCode[] = {
Hans Leidekker's avatar
Hans Leidekker committed
141
    'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
142 143
    'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};

144
static const WCHAR szInstaller_ClassesUpgradeCodes[] = {
Hans Leidekker's avatar
Hans Leidekker committed
145
    'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
146 147
    'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s',0};

148
static const WCHAR szInstaller_Features[] = {
149
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
    'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};

static const WCHAR szInstaller_UpgradeCodes[] = {
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
    'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};

static const WCHAR szInstaller_UserUpgradeCodes[] = {
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};

static const WCHAR szUninstall[] = {
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
    'U','n','i','n','s','t','a','l','l','\\',0};

static const WCHAR szUninstall_32node[] = {
    'S','o','f','t','w','a','r','e','\\','W','o','w','6','4','3','2','N','o','d','e','\\',
    'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
    'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','U','n','i','n','s','t','a','l','l','\\',0};

static const WCHAR szUserComponents[] = {
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'I','n','s','t','a','l','l','e','r','\\','C','o','m','p','o','n','e','n','t','s','\\',0};

176 177 178 179
static const WCHAR szInstaller_Components[] = {
    'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
    'I','n','s','t','a','l','l','e','r','\\','C','o','m','p','o','n','e','n','t','s','\\',0};

180 181 182 183 184 185 186 187 188 189 190
static const WCHAR szUserFeatures[] = {
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};

static const WCHAR szUserProducts[] = {
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\',0};

static const WCHAR szUserPatches[] = {
    'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
    'I','n','s','t','a','l','l','e','r','\\','P','a','t','c','h','e','s','\\',0};
191

192 193 194 195
BOOL unsquash_guid(LPCWSTR in, LPWSTR out)
{
    DWORD i,n=0;

196 197 198
    if (lstrlenW(in) != 32)
        return FALSE;

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    out[n++]='{';
    for(i=0; i<8; i++)
        out[n++] = in[7-i];
    out[n++]='-';
    for(i=0; i<4; i++)
        out[n++] = in[11-i];
    out[n++]='-';
    for(i=0; i<4; i++)
        out[n++] = in[15-i];
    out[n++]='-';
    for(i=0; i<2; i++)
    {
        out[n++] = in[17+i*2];
        out[n++] = in[16+i*2];
    }
    out[n++]='-';
    for( ; i<8; i++)
    {
        out[n++] = in[17+i*2];
        out[n++] = in[16+i*2];
    }
    out[n++]='}';
    out[n]=0;
    return TRUE;
}

BOOL squash_guid(LPCWSTR in, LPWSTR out)
{
227 228
    DWORD i,n=1;
    GUID guid;
229

230 231
    out[0] = 0;

232
    if (FAILED(CLSIDFromString((LPCOLESTR)in, &guid)))
233
        return FALSE;
234

235 236
    for(i=0; i<8; i++)
        out[7-i] = in[n++];
237
    n++;
238 239
    for(i=0; i<4; i++)
        out[11-i] = in[n++];
240
    n++;
241 242
    for(i=0; i<4; i++)
        out[15-i] = in[n++];
243
    n++;
244 245 246 247 248
    for(i=0; i<2; i++)
    {
        out[17+i*2] = in[n++];
        out[16+i*2] = in[n++];
    }
249
    n++;
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
    for( ; i<8; i++)
    {
        out[17+i*2] = in[n++];
        out[16+i*2] = in[n++];
    }
    out[32]=0;
    return TRUE;
}


/* tables for encoding and decoding base85 */
static const unsigned char table_dec85[0x80] = {
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0x00,0xff,0xff,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0xff,
0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0xff,0xff,0xff,0x16,0xff,0x17,
0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0x34,0x35,0x36,
0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,
0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0xff,0x53,0x54,0xff,
};

static const char table_enc85[] =
"!$%&'()*+,-.0123456789=?@ABCDEFGHIJKLMNO"
"PQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwx"
"yz{}~";

/*
 *  Converts a base85 encoded guid into a GUID pointer
 *  Base85 encoded GUIDs should be 20 characters long.
 *
 *  returns TRUE if successful, FALSE if not
 */
BOOL decode_base85_guid( LPCWSTR str, GUID *guid )
{
    DWORD i, val = 0, base = 1, *p;

287 288 289
    if (!str)
        return FALSE;

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    p = (DWORD*) guid;
    for( i=0; i<20; i++ )
    {
        if( (i%5) == 0 )
        {
            val = 0;
            base = 1;
        }
        val += table_dec85[str[i]] * base;
        if( str[i] >= 0x80 )
            return FALSE;
        if( table_dec85[str[i]] == 0xff )
            return FALSE;
        if( (i%5) == 4 )
            p[i/5] = val;
        base *= 85;
    }
    return TRUE;
}

/*
 *  Encodes a base85 guid given a GUID pointer
 *  Caller should provide a 21 character buffer for the encoded string.
 *
 *  returns TRUE if successful, FALSE if not
 */
BOOL encode_base85_guid( GUID *guid, LPWSTR str )
{
    unsigned int x, *p, i;

    p = (unsigned int*) guid;
    for( i=0; i<4; i++ )
    {
        x = p[i];
        *str++ = table_enc85[x%85];
        x = x/85;
        *str++ = table_enc85[x%85];
        x = x/85;
        *str++ = table_enc85[x%85];
        x = x/85;
        *str++ = table_enc85[x%85];
        x = x/85;
        *str++ = table_enc85[x%85];
    }
    *str = 0;

    return TRUE;
}

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
DWORD msi_version_str_to_dword(LPCWSTR p)
{
    DWORD major, minor = 0, build = 0, version = 0;

    if (!p)
        return version;

    major = atoiW(p);

    p = strchrW(p, '.');
    if (p)
    {
        minor = atoiW(p+1);
        p = strchrW(p+1, '.');
        if (p)
            build = atoiW(p+1);
    }

    return MAKELONG(build, MAKEWORD(minor, major));
}

360 361
LONG msi_reg_set_val_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
{
362
    DWORD len;
363
    if (!value) value = szEmpty;
364
    len = (lstrlenW(value) + 1) * sizeof (WCHAR);
365
    return RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)value, len );
366 367 368 369 370 371 372
}

LONG msi_reg_set_val_multi_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
{
    LPCWSTR p = value;
    while (*p) p += lstrlenW(p) + 1;
    return RegSetValueExW( hkey, name, 0, REG_MULTI_SZ,
373
                           (const BYTE *)value, (p + 1 - value) * sizeof(WCHAR) );
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
}

LONG msi_reg_set_val_dword( HKEY hkey, LPCWSTR name, DWORD val )
{
    return RegSetValueExW( hkey, name, 0, REG_DWORD, (LPBYTE)&val, sizeof (DWORD) );
}

LONG msi_reg_set_subkey_val( HKEY hkey, LPCWSTR path, LPCWSTR name, LPCWSTR val )
{
    HKEY hsubkey = 0;
    LONG r;

    r = RegCreateKeyW( hkey, path, &hsubkey );
    if (r != ERROR_SUCCESS)
        return r;
    r = msi_reg_set_val_str( hsubkey, name, val );
    RegCloseKey( hsubkey );
    return r;
}
393

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
LPWSTR msi_reg_get_val_str( HKEY hkey, LPCWSTR name )
{
    DWORD len = 0;
    LPWSTR val;
    LONG r;

    r = RegQueryValueExW(hkey, name, NULL, NULL, NULL, &len);
    if (r != ERROR_SUCCESS)
        return NULL;

    len += sizeof (WCHAR);
    val = msi_alloc( len );
    if (!val)
        return NULL;
    val[0] = 0;
    RegQueryValueExW(hkey, name, NULL, NULL, (LPBYTE) val, &len);
    return val;
}

BOOL msi_reg_get_val_dword( HKEY hkey, LPCWSTR name, DWORD *val)
{
    DWORD type, len = sizeof (DWORD);
    LONG r = RegQueryValueExW(hkey, name, NULL, &type, (LPBYTE) val, &len);
    return r == ERROR_SUCCESS && type == REG_DWORD;
}

420
static WCHAR *get_user_sid(void)
421 422
{
    HANDLE token;
423 424 425
    DWORD size = 256;
    TOKEN_USER *user;
    WCHAR *ret;
426

427 428 429 430 431
    if (!OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &token )) return NULL;
    if (!(user = msi_alloc( size )))
    {
        CloseHandle( token );
        return NULL;
432
    }
433 434 435 436 437 438 439 440 441
    if (!GetTokenInformation( token, TokenUser, user, size, &size ))
    {
        msi_free( user );
        if (GetLastError() != ERROR_INSUFFICIENT_BUFFER || !(user = msi_alloc( size )))
        {
            CloseHandle( token );
            return NULL;
        }
        GetTokenInformation( token, TokenUser, user, size, &size );
442
    }
443 444 445 446 447 448 449 450
    CloseHandle( token );
    if (!ConvertSidToStringSidW( user->User.Sid, &ret ))
    {
        msi_free( user );
        return NULL;
    }
    msi_free( user );
    return ret;
451 452
}

453
UINT MSIREG_OpenUninstallKey(const WCHAR *product, enum platform platform, HKEY *key, BOOL create)
454 455 456
{
    WCHAR keypath[0x200];

457
    TRACE("%s\n", debugstr_w(product));
458

459
    if (is_64bit && platform == PLATFORM_INTEL)
460 461 462 463
    {
        strcpyW(keypath, szUninstall_32node);
        strcatW(keypath, product);
    }
464
    else
465 466 467 468
    {
        strcpyW(keypath, szUninstall);
        strcatW(keypath, product);
    }
469 470
    if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, KEY_ALL_ACCESS, NULL, key, NULL);
    return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, KEY_ALL_ACCESS, key);
471 472
}

473
UINT MSIREG_DeleteUninstallKey(const WCHAR *product, enum platform platform)
474 475 476
{
    WCHAR keypath[0x200];

477
    TRACE("%s\n", debugstr_w(product));
478

479
    if (is_64bit && platform == PLATFORM_INTEL)
480 481 482 483
    {
        strcpyW(keypath, szUninstall_32node);
        strcatW(keypath, product);
    }
484
    else
485 486 487 488
    {
        strcpyW(keypath, szUninstall);
        strcatW(keypath, product);
    }
489 490 491
    return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
}

492
UINT MSIREG_OpenProductKey(LPCWSTR szProduct, LPCWSTR szUserSid, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
493
{
494
    HKEY root = HKEY_LOCAL_MACHINE;
495
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
496
    WCHAR *usersid = NULL, squashed_pc[SQUASHED_GUID_SIZE], keypath[MAX_PATH];
497

498 499
    if (!squash_guid( szProduct, squashed_pc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szProduct), debugstr_w(squashed_pc));
500

501 502
    if (context == MSIINSTALLCONTEXT_MACHINE)
    {
503
        strcpyW(keypath, szInstaller_LocalClassesProd);
504
        strcatW( keypath, squashed_pc );
505 506 507 508
    }
    else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
    {
        root = HKEY_CURRENT_USER;
509
        strcpyW(keypath, szUserProducts);
510
        strcatW( keypath, squashed_pc );
511 512 513
    }
    else
    {
514
        if (!szUserSid)
515
        {
516
            if (!(usersid = get_user_sid()))
517
            {
518 519
                ERR("Failed to retrieve user SID\n");
                return ERROR_FUNCTION_FAILED;
520 521
            }
            szUserSid = usersid;
522
        }
523
        sprintfW( keypath, szInstaller_LocalManagedProd_fmt, szUserSid, squashed_pc );
524 525
        LocalFree(usersid);
    }
526 527
    if (create) return RegCreateKeyExW(root, keypath, 0, NULL, 0, access, NULL, key, NULL);
    return RegOpenKeyExW(root, keypath, 0, access, key);
528 529
}

530 531
UINT MSIREG_DeleteUserProductKey(LPCWSTR szProduct)
{
532
    WCHAR squashed_pc[SQUASHED_GUID_SIZE], keypath[0x200];
533

534 535
    if (!squash_guid( szProduct, squashed_pc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szProduct), debugstr_w(squashed_pc));
536

537
    strcpyW(keypath, szUserProducts);
538
    strcatW( keypath, squashed_pc );
539 540 541
    return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
}

542
UINT MSIREG_OpenUserPatchesKey(LPCWSTR szPatch, HKEY *key, BOOL create)
543
{
544
    WCHAR squashed_pc[SQUASHED_GUID_SIZE], keypath[0x200];
545

546 547
    if (!squash_guid( szPatch, squashed_pc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szPatch), debugstr_w(squashed_pc));
548

549
    strcpyW(keypath, szUserPatches);
550
    strcatW( keypath, squashed_pc );
551

552 553
    if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
    return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
554 555
}

556 557
UINT MSIREG_OpenFeaturesKey(LPCWSTR szProduct, LPCWSTR szUserSid, MSIINSTALLCONTEXT context,
                            HKEY *key, BOOL create)
558
{
559
    HKEY root = HKEY_LOCAL_MACHINE;
560
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
561
    WCHAR squashed_pc[SQUASHED_GUID_SIZE], keypath[MAX_PATH], *usersid = NULL;
562

563 564
    if (!squash_guid( szProduct, squashed_pc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szProduct), debugstr_w(squashed_pc));
565

566 567
    if (context == MSIINSTALLCONTEXT_MACHINE)
    {
568
        strcpyW(keypath, szInstaller_LocalClassesFeat);
569
        strcatW( keypath, squashed_pc );
570 571 572 573
    }
    else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
    {
        root = HKEY_CURRENT_USER;
574
        strcpyW(keypath, szUserFeatures);
575
        strcatW( keypath, squashed_pc );
576 577 578
    }
    else
    {
579
        if (!szUserSid)
580
        {
581 582 583 584 585 586
            if (!(usersid = get_user_sid()))
            {
                ERR("Failed to retrieve user SID\n");
                return ERROR_FUNCTION_FAILED;
            }
            szUserSid = usersid;
587
        }
588
        sprintfW( keypath, szInstaller_LocalManagedFeat_fmt, szUserSid, squashed_pc );
589 590
        LocalFree(usersid);
    }
591 592
    if (create) return RegCreateKeyExW(root, keypath, 0, NULL, 0, access, NULL, key, NULL);
    return RegOpenKeyExW(root, keypath, 0, access, key);
593 594
}

595 596
UINT MSIREG_DeleteUserFeaturesKey(LPCWSTR szProduct)
{
597
    WCHAR squashed_pc[SQUASHED_GUID_SIZE], keypath[0x200];
598

599 600
    if (!squash_guid( szProduct, squashed_pc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szProduct), debugstr_w(squashed_pc));
601

602
    strcpyW(keypath, szUserFeatures);
603
    strcatW( keypath, squashed_pc );
604 605 606
    return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
}

607
static UINT MSIREG_OpenInstallerFeaturesKey(LPCWSTR szProduct, HKEY *key, BOOL create)
608
{
609
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
610
    WCHAR squashed_pc[SQUASHED_GUID_SIZE], keypath[0x200];
611

612 613
    if (!squash_guid( szProduct, squashed_pc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szProduct), debugstr_w(squashed_pc));
614

615
    strcpyW(keypath, szInstaller_Features);
616
    strcatW( keypath, squashed_pc );
617

618 619
    if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
    return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
620 621
}

622 623
UINT MSIREG_OpenUserDataFeaturesKey(LPCWSTR szProduct, LPCWSTR szUserSid, MSIINSTALLCONTEXT context,
                                    HKEY *key, BOOL create)
624
{
625
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
626
    WCHAR squashed_pc[SQUASHED_GUID_SIZE], keypath[0x200], *usersid = NULL;
627

628 629
    if (!squash_guid( szProduct, squashed_pc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szProduct), debugstr_w(squashed_pc));
630

631
    if (context == MSIINSTALLCONTEXT_MACHINE)
632
    {
633
        sprintfW( keypath, szUserDataFeatures_fmt, szLocalSid, squashed_pc );
634 635
    }
    else
636
    {
637
        if (!szUserSid)
638
        {
639 640 641 642 643 644
            if (!(usersid = get_user_sid()))
            {
                ERR("Failed to retrieve user SID\n");
                return ERROR_FUNCTION_FAILED;
            }
            szUserSid = usersid;
645
        }
646
        sprintfW( keypath, szUserDataFeatures_fmt, szUserSid, squashed_pc );
647 648
        LocalFree(usersid);
    }
649 650
    if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
    return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
651 652
}

653
UINT MSIREG_OpenUserComponentsKey(LPCWSTR szComponent, HKEY *key, BOOL create)
654
{
655
    WCHAR squashed_cc[SQUASHED_GUID_SIZE], keypath[0x200];
656
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
657
    UINT ret;
658

659 660
    if (!squash_guid( szComponent, squashed_cc)) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szComponent), debugstr_w(squashed_cc));
661

662
    strcpyW(keypath, szUserComponents);
663
    strcatW( keypath, squashed_cc );
664

665
    if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
666 667 668 669
    ret = RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
    if (ret != ERROR_FILE_NOT_FOUND) return ret;

    strcpyW(keypath, szInstaller_Components);
670
    strcatW( keypath, squashed_cc );
671
    return RegOpenKeyExW( HKEY_LOCAL_MACHINE, keypath, 0, access, key );
672 673
}

674
UINT MSIREG_OpenUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid, HKEY *key, BOOL create)
675
{
676
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
677
    WCHAR *usersid, squashed_comp[SQUASHED_GUID_SIZE], keypath[0x200];
678

679 680
    if (!squash_guid( szComponent, squashed_comp )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szComponent), debugstr_w(squashed_comp));
681

682
    if (!szUserSid)
683
    {
684
        if (!(usersid = get_user_sid()))
685
        {
686 687
            ERR("Failed to retrieve user SID\n");
            return ERROR_FUNCTION_FAILED;
688
        }
689
        sprintfW( keypath, szUserDataComp_fmt, usersid, squashed_comp );
690 691 692
        LocalFree(usersid);
    }
    else
693
        sprintfW( keypath, szUserDataComp_fmt, szUserSid, squashed_comp );
694

695 696
    if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
    return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
697 698
}

699
UINT MSIREG_DeleteUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid)
700
{
701
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
702
    WCHAR *usersid, squashed_comp[SQUASHED_GUID_SIZE], keypath[0x200];
703 704
    HKEY hkey;
    LONG r;
705

706 707
    if (!squash_guid( szComponent, squashed_comp )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szComponent), debugstr_w(squashed_comp));
708

709
    if (!szUserSid)
710
    {
711
        if (!(usersid = get_user_sid()))
712
        {
713 714
            ERR("Failed to retrieve user SID\n");
            return ERROR_FUNCTION_FAILED;
715
        }
716
        sprintfW(keypath, szUserDataComponents_fmt, usersid);
717 718 719
        LocalFree(usersid);
    }
    else
720
        sprintfW(keypath, szUserDataComponents_fmt, szUserSid);
721

722
    if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
723
    r = RegDeleteTreeW( hkey, squashed_comp );
724 725
    RegCloseKey(hkey);
    return r;
726 727
}

728
UINT MSIREG_OpenUserDataProductKey(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext, LPCWSTR szUserSid, HKEY *key, BOOL create)
729
{
730
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
731
    WCHAR *usersid, squashed_pc[SQUASHED_GUID_SIZE], keypath[0x200];
732

733 734
    if (!squash_guid( szProduct, squashed_pc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szProduct), debugstr_w(squashed_pc));
735

736
    if (dwContext == MSIINSTALLCONTEXT_MACHINE)
737
        sprintfW( keypath, szUserDataProd_fmt, szLocalSid, squashed_pc );
738
    else if (szUserSid)
739
        sprintfW( keypath, szUserDataProd_fmt, szUserSid, squashed_pc );
740
    else
741
    {
742
        if (!(usersid = get_user_sid()))
743
        {
744 745
            ERR("Failed to retrieve user SID\n");
            return ERROR_FUNCTION_FAILED;
746
        }
747
        sprintfW( keypath, szUserDataProd_fmt, usersid, squashed_pc );
748 749
        LocalFree(usersid);
    }
750 751
    if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
    return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
752 753
}

754
UINT MSIREG_OpenUserDataPatchKey(LPCWSTR szPatch, MSIINSTALLCONTEXT dwContext, HKEY *key, BOOL create)
755
{
756
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
757
    WCHAR *usersid, squashed_patch[SQUASHED_GUID_SIZE], keypath[0x200];
758

759 760
    if (!squash_guid( szPatch, squashed_patch )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szPatch), debugstr_w(squashed_patch));
761

762
    if (dwContext == MSIINSTALLCONTEXT_MACHINE)
763
        sprintfW( keypath, szUserDataPatch_fmt, szLocalSid, squashed_patch );
764
    else
765
    {
766
        if (!(usersid = get_user_sid()))
767
        {
768 769
            ERR("Failed to retrieve user SID\n");
            return ERROR_FUNCTION_FAILED;
770
        }
771
        sprintfW( keypath, szUserDataPatch_fmt, usersid, squashed_patch );
772 773
        LocalFree(usersid);
    }
774 775
    if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
    return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
776 777
}

778 779
UINT MSIREG_DeleteUserDataPatchKey(LPCWSTR patch, MSIINSTALLCONTEXT context)
{
780
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
781
    WCHAR *usersid, squashed_patch[SQUASHED_GUID_SIZE], keypath[0x200];
782 783
    HKEY hkey;
    LONG r;
784

785 786
    if (!squash_guid( patch, squashed_patch )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(patch), debugstr_w(squashed_patch));
787 788

    if (context == MSIINSTALLCONTEXT_MACHINE)
789
        sprintfW(keypath, szUserDataPatches_fmt, szLocalSid);
790 791
    else
    {
792
        if (!(usersid = get_user_sid()))
793
        {
794 795
            ERR("Failed to retrieve user SID\n");
            return ERROR_FUNCTION_FAILED;
796
        }
797
        sprintfW(keypath, szUserDataPatches_fmt, usersid);
798 799
        LocalFree(usersid);
    }
800
    if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
801
    r = RegDeleteTreeW( hkey, squashed_patch );
802 803
    RegCloseKey(hkey);
    return r;
804 805
}

806
UINT MSIREG_OpenUserDataProductPatchesKey(LPCWSTR product, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
807
{
808
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
809
    WCHAR *usersid, squashed_product[SQUASHED_GUID_SIZE], keypath[0x200];
810

811 812
    if (!squash_guid( product, squashed_product )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(product), debugstr_w(squashed_product));
813 814

    if (context == MSIINSTALLCONTEXT_MACHINE)
815
        sprintfW( keypath, szUserDataProductPatches_fmt, szLocalSid, squashed_product );
816 817
    else
    {
818
        if (!(usersid = get_user_sid()))
819
        {
820 821
            ERR("Failed to retrieve user SID\n");
            return ERROR_FUNCTION_FAILED;
822
        }
823
        sprintfW( keypath, szUserDataProductPatches_fmt, usersid, squashed_product );
824 825
        LocalFree(usersid);
    }
826 827
    if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
    return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
828 829
}

830
UINT MSIREG_OpenInstallProps(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext, LPCWSTR szUserSid, HKEY *key, BOOL create)
831
{
832
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
833
    WCHAR *usersid, squashed_pc[SQUASHED_GUID_SIZE], keypath[0x200];
834

835 836
    if (!squash_guid( szProduct, squashed_pc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szProduct), debugstr_w(squashed_pc));
837

838
    if (dwContext == MSIINSTALLCONTEXT_MACHINE)
839
        sprintfW( keypath, szInstallProperties_fmt, szLocalSid, squashed_pc );
840
    else if (szUserSid)
841
        sprintfW( keypath, szInstallProperties_fmt, szUserSid, squashed_pc );
842
    else
843
    {
844
        if (!(usersid = get_user_sid()))
845
        {
846 847
            ERR("Failed to retrieve user SID\n");
            return ERROR_FUNCTION_FAILED;
848
        }
849
        sprintfW( keypath, szInstallProperties_fmt, usersid, squashed_pc );
850
        LocalFree(usersid);
851
    }
852 853
    if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
    return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
854 855
}

856
UINT MSIREG_DeleteUserDataProductKey(LPCWSTR szProduct, MSIINSTALLCONTEXT context)
857
{
858
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
859
    WCHAR *usersid, squashed_pc[SQUASHED_GUID_SIZE], keypath[0x200];
860 861
    HKEY hkey;
    LONG r;
862

863 864
    if (!squash_guid( szProduct, squashed_pc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szProduct), debugstr_w(squashed_pc));
865

866 867 868
    if (context == MSIINSTALLCONTEXT_MACHINE)
        sprintfW(keypath, szUserDataProducts_fmt, szLocalSid);
    else
869
    {
870 871 872 873 874 875 876
        if (!(usersid = get_user_sid()))
        {
            ERR("Failed to retrieve user SID\n");
            return ERROR_FUNCTION_FAILED;
        }
        sprintfW(keypath, szUserDataProducts_fmt, usersid);
        LocalFree(usersid);
877
    }
878

879
    if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
880
    r = RegDeleteTreeW( hkey, squashed_pc );
881 882
    RegCloseKey(hkey);
    return r;
883 884 885 886
}

UINT MSIREG_DeleteProductKey(LPCWSTR szProduct)
{
887
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
888
    WCHAR squashed_pc[SQUASHED_GUID_SIZE];
889 890
    HKEY hkey;
    LONG r;
891

892 893
    if (!squash_guid( szProduct, squashed_pc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szProduct), debugstr_w(squashed_pc));
894

895
    if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_Products, 0, access, &hkey)) return ERROR_SUCCESS;
896
    r = RegDeleteTreeW( hkey, squashed_pc );
897 898
    RegCloseKey(hkey);
    return r;
899 900
}

901
UINT MSIREG_OpenPatchesKey(LPCWSTR szPatch, HKEY *key, BOOL create)
902
{
903
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
904
    WCHAR squashed_pc[SQUASHED_GUID_SIZE], keypath[0x200];
905

906 907
    if (!squash_guid( szPatch, squashed_pc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szPatch), debugstr_w(squashed_pc));
908

909
    sprintfW( keypath, szInstaller_Patches, squashed_pc );
910

911 912
    if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
    return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
913 914
}

915
UINT MSIREG_OpenUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY *key, BOOL create)
916
{
917
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
918
    WCHAR squashed_uc[SQUASHED_GUID_SIZE], keypath[0x200];
919

920 921
    if (!squash_guid( szUpgradeCode, squashed_uc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szUpgradeCode), debugstr_w(squashed_uc));
922

923
    strcpyW(keypath, szInstaller_UpgradeCodes);
924
    strcatW( keypath, squashed_uc );
925

926 927
    if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
    return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
928 929
}

930 931
UINT MSIREG_OpenUserUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
{
932
    WCHAR squashed_uc[SQUASHED_GUID_SIZE], keypath[0x200];
933

934 935
    if (!squash_guid( szUpgradeCode, squashed_uc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szUpgradeCode), debugstr_w(squashed_uc));
936

937
    strcpyW(keypath, szInstaller_UserUpgradeCodes);
938
    strcatW( keypath, squashed_uc );
939

940 941
    if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
    return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
942 943
}

944 945
UINT MSIREG_DeleteUpgradeCodesKey( const WCHAR *code )
{
946 947 948 949
    WCHAR squashed_code[SQUASHED_GUID_SIZE];
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
    HKEY hkey;
    LONG ret;
950 951 952 953

    if (!squash_guid( code, squashed_code )) return ERROR_FUNCTION_FAILED;
    TRACE( "%s squashed %s\n", debugstr_w(code), debugstr_w(squashed_code) );

954 955 956 957
    if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, szInstaller_UpgradeCodes, 0, access, &hkey )) return ERROR_SUCCESS;
    ret = RegDeleteTreeW( hkey, squashed_code );
    RegCloseKey( hkey );
    return ret;
958 959
}

960 961
UINT MSIREG_DeleteUserUpgradeCodesKey(LPCWSTR szUpgradeCode)
{
962
    WCHAR squashed_uc[SQUASHED_GUID_SIZE], keypath[0x200];
963

964 965
    if (!squash_guid( szUpgradeCode, squashed_uc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szUpgradeCode), debugstr_w(squashed_uc));
966

967
    strcpyW(keypath, szInstaller_UserUpgradeCodes);
968
    strcatW( keypath, squashed_uc );
969 970 971
    return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
}

972 973
UINT MSIREG_DeleteLocalClassesProductKey(LPCWSTR szProductCode)
{
974
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
975
    WCHAR squashed_pc[SQUASHED_GUID_SIZE];
976 977
    HKEY hkey;
    LONG r;
978

979 980
    if (!squash_guid( szProductCode, squashed_pc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szProductCode), debugstr_w(squashed_pc));
981

982
    if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_LocalClassesProducts, 0, access, &hkey)) return ERROR_SUCCESS;
983
    r = RegDeleteTreeW( hkey, squashed_pc );
984 985
    RegCloseKey(hkey);
    return r;
986 987 988 989
}

UINT MSIREG_DeleteLocalClassesFeaturesKey(LPCWSTR szProductCode)
{
990
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
991
    WCHAR squashed_pc[SQUASHED_GUID_SIZE];
992 993
    HKEY hkey;
    LONG r;
994

995 996
    if (!squash_guid( szProductCode, squashed_pc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szProductCode), debugstr_w(squashed_pc));
997

998
    if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_LocalClassesFeatures, 0, access, &hkey)) return ERROR_SUCCESS;
999
    r = RegDeleteTreeW( hkey, squashed_pc );
1000 1001
    RegCloseKey(hkey);
    return r;
1002 1003
}

1004
UINT MSIREG_OpenClassesUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY *key, BOOL create)
1005
{
1006
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
1007
    WCHAR squashed_uc[SQUASHED_GUID_SIZE], keypath[0x200];
1008

1009 1010
    if (!squash_guid( szUpgradeCode, squashed_uc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szUpgradeCode), debugstr_w(squashed_uc));
1011

1012
    strcpyW(keypath, szInstaller_ClassesUpgradeCode);
1013
    strcatW( keypath, squashed_uc );
1014

Hans Leidekker's avatar
Hans Leidekker committed
1015 1016
    if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
    return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
1017 1018
}

1019 1020
UINT MSIREG_DeleteClassesUpgradeCodesKey(LPCWSTR szUpgradeCode)
{
1021
    REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
1022
    WCHAR squashed_uc[SQUASHED_GUID_SIZE];
1023 1024
    HKEY hkey;
    LONG r;
1025

1026 1027
    if (!squash_guid( szUpgradeCode, squashed_uc )) return ERROR_FUNCTION_FAILED;
    TRACE("%s squashed %s\n", debugstr_w(szUpgradeCode), debugstr_w(squashed_uc));
1028

Hans Leidekker's avatar
Hans Leidekker committed
1029
    if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_ClassesUpgradeCodes, 0, access, &hkey)) return ERROR_SUCCESS;
1030
    r = RegDeleteTreeW( hkey, squashed_uc );
1031 1032
    RegCloseKey(hkey);
    return r;
1033 1034
}

1035 1036 1037 1038 1039
/*************************************************************************
 *  MsiDecomposeDescriptorW   [MSI.@]
 *
 * Decomposes an MSI descriptor into product, feature and component parts.
 * An MSI descriptor is a string of the form:
1040 1041
 *   [base 85 guid] [feature code] '>' [base 85 guid] or
 *   [base 85 guid] [feature code] '<'
1042 1043 1044
 *
 * PARAMS
 *   szDescriptor  [I]  the descriptor to decompose
1045 1046 1047
 *   szProduct     [O]  buffer of MAX_FEATURE_CHARS+1 for the product guid
 *   szFeature     [O]  buffer of MAX_FEATURE_CHARS+1 for the feature code
 *   szComponent   [O]  buffer of MAX_FEATURE_CHARS+1 for the component guid
1048 1049 1050 1051 1052 1053 1054 1055
 *   pUsed         [O]  the length of the descriptor
 *
 * RETURNS
 *   ERROR_SUCCESS             if everything worked correctly
 *   ERROR_INVALID_PARAMETER   if the descriptor was invalid
 *
 */
UINT WINAPI MsiDecomposeDescriptorW( LPCWSTR szDescriptor, LPWSTR szProduct,
1056
                LPWSTR szFeature, LPWSTR szComponent, LPDWORD pUsed )
1057
{
1058 1059
    UINT len;
    const WCHAR *p;
1060 1061 1062 1063 1064
    GUID product, component;

    TRACE("%s %p %p %p %p\n", debugstr_w(szDescriptor), szProduct,
          szFeature, szComponent, pUsed);

1065
    if (!decode_base85_guid( szDescriptor, &product ))
1066 1067 1068 1069
        return ERROR_INVALID_PARAMETER;

    TRACE("product %s\n", debugstr_guid( &product ));

1070 1071 1072
    if (!(p = strchrW( &szDescriptor[20], '>' )))
        p = strchrW( &szDescriptor[20], '<' );
    if (!p)
1073 1074 1075 1076 1077 1078
        return ERROR_INVALID_PARAMETER;

    len = (p - &szDescriptor[20]);
    if( len > MAX_FEATURE_CHARS )
        return ERROR_INVALID_PARAMETER;

1079
    TRACE("feature %s\n", debugstr_wn( &szDescriptor[20], len ));
1080

1081 1082 1083 1084 1085 1086
    if (*p == '>')
    {
        if (!decode_base85_guid( p+1, &component ))
            return ERROR_INVALID_PARAMETER;
        TRACE( "component %s\n", debugstr_guid(&component) );
    }
1087

1088 1089 1090
    if (szProduct)
        StringFromGUID2( &product, szProduct, MAX_FEATURE_CHARS+1 );
    if (szComponent)
1091 1092 1093 1094 1095 1096
    {
        if (*p == '>')
            StringFromGUID2( &component, szComponent, MAX_FEATURE_CHARS+1 );
        else
            szComponent[0] = 0;
    }
1097 1098 1099 1100 1101
    if (szFeature)
    {
        memcpy( szFeature, &szDescriptor[20], len*sizeof(WCHAR) );
        szFeature[len] = 0;
    }
1102 1103 1104

    len = p - szDescriptor + 1;
    if (*p == '>') len += 20;
1105 1106

    TRACE("length = %d\n", len);
1107
    if (pUsed) *pUsed = len;
1108 1109 1110 1111 1112

    return ERROR_SUCCESS;
}

UINT WINAPI MsiDecomposeDescriptorA( LPCSTR szDescriptor, LPSTR szProduct,
1113
                LPSTR szFeature, LPSTR szComponent, LPDWORD pUsed )
1114 1115 1116 1117
{
    WCHAR product[MAX_FEATURE_CHARS+1];
    WCHAR feature[MAX_FEATURE_CHARS+1];
    WCHAR component[MAX_FEATURE_CHARS+1];
1118
    LPWSTR str = NULL, p = NULL, f = NULL, c = NULL;
1119
    UINT r;
1120 1121 1122 1123

    TRACE("%s %p %p %p %p\n", debugstr_a(szDescriptor), szProduct,
          szFeature, szComponent, pUsed);

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
    str = strdupAtoW( szDescriptor );
    if( szDescriptor && !str )
        return ERROR_OUTOFMEMORY;

    if (szProduct)
        p = product;
    if (szFeature)
        f = feature;
    if (szComponent)
        c = component;
1134

1135
    r = MsiDecomposeDescriptorW( str, p, f, c, pUsed );
1136

1137 1138 1139 1140 1141 1142 1143 1144 1145
    if (r == ERROR_SUCCESS)
    {
        WideCharToMultiByte( CP_ACP, 0, p, -1,
                             szProduct, MAX_FEATURE_CHARS+1, NULL, NULL );
        WideCharToMultiByte( CP_ACP, 0, f, -1,
                             szFeature, MAX_FEATURE_CHARS+1, NULL, NULL );
        WideCharToMultiByte( CP_ACP, 0, c, -1,
                             szComponent, MAX_FEATURE_CHARS+1, NULL, NULL );
    }
1146

1147
    msi_free( str );
1148 1149 1150

    return r;
}
1151 1152 1153 1154 1155 1156

UINT WINAPI MsiEnumProductsA(DWORD index, LPSTR lpguid)
{
    DWORD r;
    WCHAR szwGuid[GUID_SIZE];

1157 1158
    TRACE("%d %p\n", index, lpguid);

1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
    if (NULL == lpguid)
        return ERROR_INVALID_PARAMETER;
    r = MsiEnumProductsW(index, szwGuid);
    if( r == ERROR_SUCCESS )
        WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);

    return r;
}

UINT WINAPI MsiEnumProductsW(DWORD index, LPWSTR lpguid)
{
1170
    TRACE("%d %p\n", index, lpguid);
1171 1172 1173 1174

    if (NULL == lpguid)
        return ERROR_INVALID_PARAMETER;

1175 1176
    return MsiEnumProductsExW( NULL, szAllSid, MSIINSTALLCONTEXT_ALL, index, lpguid,
                               NULL, NULL, NULL );
1177 1178 1179 1180 1181 1182 1183 1184 1185
}

UINT WINAPI MsiEnumFeaturesA(LPCSTR szProduct, DWORD index, 
      LPSTR szFeature, LPSTR szParent)
{
    DWORD r;
    WCHAR szwFeature[GUID_SIZE], szwParent[GUID_SIZE];
    LPWSTR szwProduct = NULL;

1186
    TRACE("%s %d %p %p\n", debugstr_a(szProduct), index, szFeature, szParent);
1187 1188 1189

    if( szProduct )
    {
1190 1191 1192
        szwProduct = strdupAtoW( szProduct );
        if( !szwProduct )
            return ERROR_OUTOFMEMORY;
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
    }

    r = MsiEnumFeaturesW(szwProduct, index, szwFeature, szwParent);
    if( r == ERROR_SUCCESS )
    {
        WideCharToMultiByte(CP_ACP, 0, szwFeature, -1,
                            szFeature, GUID_SIZE, NULL, NULL);
        WideCharToMultiByte(CP_ACP, 0, szwParent, -1,
                            szParent, GUID_SIZE, NULL, NULL);
    }

1204
    msi_free( szwProduct);
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214

    return r;
}

UINT WINAPI MsiEnumFeaturesW(LPCWSTR szProduct, DWORD index, 
      LPWSTR szFeature, LPWSTR szParent)
{
    HKEY hkeyProduct = 0;
    DWORD r, sz;

1215
    TRACE("%s %d %p %p\n", debugstr_w(szProduct), index, szFeature, szParent);
1216

1217 1218 1219
    if( !szProduct )
        return ERROR_INVALID_PARAMETER;

1220
    r = MSIREG_OpenInstallerFeaturesKey(szProduct,&hkeyProduct,FALSE);
1221
    if( r != ERROR_SUCCESS )
1222
        return ERROR_NO_MORE_ITEMS;
1223 1224 1225

    sz = GUID_SIZE;
    r = RegEnumValueW(hkeyProduct, index, szFeature, &sz, NULL, NULL, NULL, NULL);
1226
    RegCloseKey(hkeyProduct);
1227 1228 1229 1230 1231 1232 1233 1234 1235

    return r;
}

UINT WINAPI MsiEnumComponentsA(DWORD index, LPSTR lpguid)
{
    DWORD r;
    WCHAR szwGuid[GUID_SIZE];

1236 1237 1238
    TRACE("%u, %p\n", index, lpguid);

    if (!lpguid) return ERROR_INVALID_PARAMETER;
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248

    r = MsiEnumComponentsW(index, szwGuid);
    if( r == ERROR_SUCCESS )
        WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);

    return r;
}

UINT WINAPI MsiEnumComponentsW(DWORD index, LPWSTR lpguid)
{
1249
    TRACE("%u, %p\n", index, lpguid);
1250

1251
    if (!lpguid) return ERROR_INVALID_PARAMETER;
1252

1253
    return MsiEnumComponentsExW( szAllSid, MSIINSTALLCONTEXT_ALL, index, lpguid, NULL, NULL, NULL );
1254 1255
}

1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
UINT WINAPI MsiEnumComponentsExA( LPCSTR user_sid, DWORD ctx, DWORD index, CHAR guid[39],
                                  MSIINSTALLCONTEXT *installed_ctx, LPSTR sid, LPDWORD sid_len )
{
    UINT r;
    WCHAR *user_sidW = NULL, *sidW = NULL, guidW[GUID_SIZE];

    TRACE("%s, %u, %u, %p, %p, %p, %p\n", debugstr_a(user_sid), ctx, index, guid, installed_ctx,
          sid, sid_len);

    if (sid && !sid_len) return ERROR_INVALID_PARAMETER;
    if (user_sid && !(user_sidW = strdupAtoW( user_sid ))) return ERROR_OUTOFMEMORY;
    if (sid && !(sidW = msi_alloc( *sid_len * sizeof(WCHAR) )))
    {
        msi_free( user_sidW );
        return ERROR_OUTOFMEMORY;
    }
    r = MsiEnumComponentsExW( user_sidW, ctx, index, guidW, installed_ctx, sidW, sid_len );
    if (r == ERROR_SUCCESS)
    {
        if (guid) WideCharToMultiByte( CP_ACP, 0, guidW, GUID_SIZE, guid, GUID_SIZE, NULL, NULL );
        if (sid) WideCharToMultiByte( CP_ACP, 0, sidW, *sid_len + 1, sid, *sid_len + 1, NULL, NULL );
    }
    msi_free( user_sidW );
    msi_free( sidW );
    return r;
}

static UINT fetch_machine_component( DWORD ctx, DWORD index, DWORD *idx, WCHAR guid[39],
                                     MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
{
    static const WCHAR componentsW[] =
        {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
         'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
         'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
         'S','-','1','-','5','-','1','8','\\','C','o','m','p','o','n','e','n','t','s',0};
    UINT r = ERROR_SUCCESS;
1292
    WCHAR component[SQUASHED_GUID_SIZE];
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
    DWORD i = 0, len_component;
    REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
    HKEY key_components;

    if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, componentsW, 0, access, &key_components ))
        return ERROR_NO_MORE_ITEMS;

    len_component = sizeof(component)/sizeof(component[0]);
    while (!RegEnumKeyExW( key_components, i, component, &len_component, NULL, NULL, NULL, NULL ))
    {
        if (*idx == index) goto found;
        (*idx)++;
        len_component = sizeof(component)/sizeof(component[0]);
        i++;
    }
    RegCloseKey( key_components );
    return ERROR_NO_MORE_ITEMS;

found:
    if (sid_len)
    {
        if (*sid_len < 1)
        {
            *sid_len = 1;
            r = ERROR_MORE_DATA;
        }
        else if (sid)
        {
            *sid_len = 0;
            sid[0] = 0;
        }
    }
    if (guid) unsquash_guid( component, guid );
    if (installed_ctx) *installed_ctx = MSIINSTALLCONTEXT_MACHINE;
    RegCloseKey( key_components );
    return r;
}

static UINT fetch_user_component( const WCHAR *usersid, DWORD ctx, DWORD index, DWORD *idx,
                                  WCHAR guid[39], MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid,
                                  LPDWORD sid_len )
{
    static const WCHAR userdataW[] =
        {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
         'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
         'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a',0};
    static const WCHAR componentsW[] = {'\\','C','o','m','p','o','n','e','n','t','s',0};
    UINT r = ERROR_SUCCESS;
1341
    WCHAR path[MAX_PATH], component[SQUASHED_GUID_SIZE], user[128];
1342 1343 1344 1345
    DWORD i = 0, j = 0, len_component, len_user;
    REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
    HKEY key_users, key_components;

1346
    if (ctx == MSIINSTALLCONTEXT_USERMANAGED) /* FIXME: where to find these? */
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
        return ERROR_NO_MORE_ITEMS;

    if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, userdataW, 0, access, &key_users ))
        return ERROR_NO_MORE_ITEMS;

    len_user = sizeof(user)/sizeof(user[0]);
    while (!RegEnumKeyExW( key_users, i, user, &len_user, NULL, NULL, NULL, NULL ))
    {
        if ((strcmpW( usersid, szAllSid ) && strcmpW( usersid, user )) ||
            !strcmpW( szLocalSid, user ))
        {
            i++;
            len_user = sizeof(user)/sizeof(user[0]);
            continue;
        }
        strcpyW( path, user );
        strcatW( path, componentsW );
        if (RegOpenKeyExW( key_users, path, 0, access, &key_components ))
        {
            i++;
            len_user = sizeof(user)/sizeof(user[0]);
            continue;
        }
        len_component = sizeof(component)/sizeof(component[0]);
        while (!RegEnumKeyExW( key_components, j, component, &len_component, NULL, NULL, NULL, NULL ))
        {
            if (*idx == index) goto found;
            (*idx)++;
            len_component = sizeof(component)/sizeof(component[0]);
            j++;
        }
        RegCloseKey( key_components );
        len_user = sizeof(user)/sizeof(user[0]);
        i++;
    }
    RegCloseKey( key_users );
    return ERROR_NO_MORE_ITEMS;

found:
    if (sid_len)
    {
        if (*sid_len < len_user + 1)
        {
            *sid_len = len_user + 1;
            r = ERROR_MORE_DATA;
        }
        else if (sid)
        {
            *sid_len = len_user;
            strcpyW( sid, user );
        }
    }
    if (guid) unsquash_guid( component, guid );
    if (installed_ctx) *installed_ctx = ctx;
    RegCloseKey( key_components );
    RegCloseKey( key_users );
    return r;
}

static UINT enum_components( const WCHAR *usersid, DWORD ctx, DWORD index, DWORD *idx, WCHAR guid[39],
                             MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
{
    UINT r = ERROR_NO_MORE_ITEMS;
    WCHAR *user = NULL;

    if (!usersid)
    {
        usersid = user = get_user_sid();
        if (!user) return ERROR_FUNCTION_FAILED;
    }
    if (ctx & MSIINSTALLCONTEXT_USERMANAGED)
    {
        r = fetch_user_component( usersid, MSIINSTALLCONTEXT_USERMANAGED, index, idx, guid,
                                  installed_ctx, sid, sid_len );
        if (r != ERROR_NO_MORE_ITEMS) goto done;
    }
    if (ctx & MSIINSTALLCONTEXT_USERUNMANAGED)
    {
        r = fetch_user_component( usersid, MSIINSTALLCONTEXT_USERUNMANAGED, index, idx, guid,
                                  installed_ctx, sid, sid_len );
        if (r != ERROR_NO_MORE_ITEMS) goto done;
    }
    if (ctx & MSIINSTALLCONTEXT_MACHINE)
    {
        r = fetch_machine_component( MSIINSTALLCONTEXT_MACHINE, index, idx, guid, installed_ctx,
                                     sid, sid_len );
        if (r != ERROR_NO_MORE_ITEMS) goto done;
    }

done:
    LocalFree( user );
    return r;
}

UINT WINAPI MsiEnumComponentsExW( LPCWSTR user_sid, DWORD ctx, DWORD index, WCHAR guid[39],
                                  MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
{
    UINT r;
    DWORD idx = 0;
    static DWORD last_index;

    TRACE("%s, %u, %u, %p, %p, %p, %p\n", debugstr_w(user_sid), ctx, index, guid, installed_ctx,
          sid, sid_len);

    if ((sid && !sid_len) || !ctx || (user_sid && ctx == MSIINSTALLCONTEXT_MACHINE))
        return ERROR_INVALID_PARAMETER;

    if (index && index - last_index != 1)
        return ERROR_INVALID_PARAMETER;

    if (!index) last_index = 0;

    r = enum_components( user_sid, ctx, index, &idx, guid, installed_ctx, sid, sid_len );
    if (r == ERROR_SUCCESS)
        last_index = index;
    else
        last_index = 0;

    return r;
}

1468 1469 1470 1471 1472 1473
UINT WINAPI MsiEnumClientsA(LPCSTR szComponent, DWORD index, LPSTR szProduct)
{
    DWORD r;
    WCHAR szwProduct[GUID_SIZE];
    LPWSTR szwComponent = NULL;

1474
    TRACE("%s %d %p\n", debugstr_a(szComponent), index, szProduct);
1475

1476 1477 1478
    if ( !szProduct )
        return ERROR_INVALID_PARAMETER;

1479 1480
    if( szComponent )
    {
1481 1482 1483
        szwComponent = strdupAtoW( szComponent );
        if( !szwComponent )
            return ERROR_OUTOFMEMORY;
1484 1485 1486 1487 1488 1489 1490 1491 1492
    }

    r = MsiEnumClientsW(szComponent?szwComponent:NULL, index, szwProduct);
    if( r == ERROR_SUCCESS )
    {
        WideCharToMultiByte(CP_ACP, 0, szwProduct, -1,
                            szProduct, GUID_SIZE, NULL, NULL);
    }

1493
    msi_free( szwComponent);
1494 1495 1496 1497 1498 1499 1500 1501

    return r;
}

UINT WINAPI MsiEnumClientsW(LPCWSTR szComponent, DWORD index, LPWSTR szProduct)
{
    HKEY hkeyComp = 0;
    DWORD r, sz;
1502
    WCHAR szValName[SQUASHED_GUID_SIZE];
1503

1504
    TRACE("%s %d %p\n", debugstr_w(szComponent), index, szProduct);
1505

1506
    if (!szComponent || !*szComponent || !szProduct)
1507 1508
        return ERROR_INVALID_PARAMETER;

1509
    if (MSIREG_OpenUserDataComponentKey(szComponent, NULL, &hkeyComp, FALSE) != ERROR_SUCCESS &&
1510
        MSIREG_OpenUserDataComponentKey(szComponent, szLocalSid, &hkeyComp, FALSE) != ERROR_SUCCESS)
1511
        return ERROR_UNKNOWN_COMPONENT;
1512

1513
    /* see if there are any products at all */
1514
    sz = SQUASHED_GUID_SIZE;
1515 1516 1517 1518
    r = RegEnumValueW(hkeyComp, 0, szValName, &sz, NULL, NULL, NULL, NULL);
    if (r != ERROR_SUCCESS)
    {
        RegCloseKey(hkeyComp);
1519 1520 1521 1522

        if (index != 0)
            return ERROR_INVALID_PARAMETER;

1523 1524 1525
        return ERROR_UNKNOWN_COMPONENT;
    }

1526
    sz = SQUASHED_GUID_SIZE;
1527
    r = RegEnumValueW(hkeyComp, index, szValName, &sz, NULL, NULL, NULL, NULL);
1528
    if( r == ERROR_SUCCESS )
Hans Leidekker's avatar
Hans Leidekker committed
1529
    {
1530
        unsquash_guid(szValName, szProduct);
Hans Leidekker's avatar
Hans Leidekker committed
1531 1532
        TRACE("-> %s\n", debugstr_w(szProduct));
    }
1533
    RegCloseKey(hkeyComp);
1534 1535 1536
    return r;
}

1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
UINT WINAPI MsiEnumClientsExA(LPCSTR component, LPCSTR usersid, DWORD ctx, DWORD index,
                              CHAR installed_product[GUID_SIZE],
                              MSIINSTALLCONTEXT *installed_ctx, LPSTR sid, LPDWORD sid_len)
{
    FIXME("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_a(component), debugstr_a(usersid),
          ctx, index, installed_product, installed_ctx, sid, sid_len);
    return ERROR_ACCESS_DENIED;
}

UINT WINAPI MsiEnumClientsExW(LPCWSTR component, LPCWSTR usersid, DWORD ctx, DWORD index,
                              WCHAR installed_product[GUID_SIZE],
                              MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len)
{
    FIXME("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_w(component), debugstr_w(usersid),
          ctx, index, installed_product, installed_ctx, sid, sid_len);
    return ERROR_ACCESS_DENIED;
}

1555
static UINT MSI_EnumComponentQualifiers( LPCWSTR szComponent, DWORD iIndex,
1556 1557
                awstring *lpQualBuf, LPDWORD pcchQual,
                awstring *lpAppBuf, LPDWORD pcchAppBuf )
1558
{
1559
    DWORD name_sz, val_sz, name_max, val_max, type, ofs;
1560 1561 1562 1563
    LPWSTR name = NULL, val = NULL;
    UINT r, r2;
    HKEY key;

1564
    TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
          lpQualBuf, pcchQual, lpAppBuf, pcchAppBuf);

    if (!szComponent)
        return ERROR_INVALID_PARAMETER;

    r = MSIREG_OpenUserComponentsKey( szComponent, &key, FALSE );
    if (r != ERROR_SUCCESS)
        return ERROR_UNKNOWN_COMPONENT;

    /* figure out how big the name is we want to return */
1575 1576 1577 1578
    name_max = 0x10;
    r = ERROR_OUTOFMEMORY;
    name = msi_alloc( name_max * sizeof(WCHAR) );
    if (!name)
1579 1580
        goto end;

1581 1582 1583
    val_max = 0x10;
    r = ERROR_OUTOFMEMORY;
    val = msi_alloc( val_max );
1584 1585 1586
    if (!val)
        goto end;

1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
    /* loop until we allocate enough memory */
    while (1)
    {
        name_sz = name_max;
        val_sz = val_max;
        r = RegEnumValueW( key, iIndex, name, &name_sz,
                           NULL, &type, (LPBYTE)val, &val_sz );
        if (r == ERROR_SUCCESS)
            break;
        if (r != ERROR_MORE_DATA)
            goto end;
 
        if (type != REG_MULTI_SZ)
        {
1601
            ERR("component data has wrong type (%d)\n", type);
1602 1603 1604 1605
            goto end;
        }

        r = ERROR_OUTOFMEMORY;
1606
        if (name_sz + 1 >= name_max)
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
        {
            name_max *= 2;
            msi_free( name );
            name = msi_alloc( name_max * sizeof (WCHAR) );
            if (!name)
                goto end;
            continue;
        }
        if (val_sz > val_max)
        {
            val_max = val_sz + sizeof (WCHAR);
1618
            msi_free( val );
1619 1620 1621 1622 1623
            val = msi_alloc( val_max * sizeof (WCHAR) );
            if (!val)
                goto end;
            continue;
        }
1624
        ERR("should be enough data, but isn't %d %d\n", name_sz, val_sz );
1625
        goto end;
1626
    }
1627 1628 1629 1630 1631 1632 1633 1634

    ofs = 0;
    r = MsiDecomposeDescriptorW( val, NULL, NULL, NULL, &ofs );
    if (r != ERROR_SUCCESS)
        goto end;

    TRACE("Providing %s and %s\n", debugstr_w(name), debugstr_w(val+ofs));

1635 1636
    r = msi_strcpy_to_awstring( name, -1, lpQualBuf, pcchQual );
    r2 = msi_strcpy_to_awstring( val+ofs, -1, lpAppBuf, pcchAppBuf );
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647

    if (r2 != ERROR_SUCCESS)
        r = r2;

end:
    msi_free(val);
    msi_free(name);
    RegCloseKey(key);
    return r;
}

1648 1649 1650
/*************************************************************************
 *  MsiEnumComponentQualifiersA [MSI.@]
 */
1651
UINT WINAPI MsiEnumComponentQualifiersA( LPCSTR szComponent, DWORD iIndex,
1652 1653
                LPSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
                LPSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1654
{
1655 1656 1657
    awstring qual, appdata;
    LPWSTR comp;
    UINT r;
1658

1659
    TRACE("%s %08x %p %p %p %p\n", debugstr_a(szComponent), iIndex,
1660 1661
          lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
          pcchApplicationDataBuf);
1662

1663 1664 1665
    comp = strdupAtoW( szComponent );
    if (szComponent && !comp)
        return ERROR_OUTOFMEMORY;
1666

1667 1668
    qual.unicode = FALSE;
    qual.str.a = lpQualifierBuf;
1669

1670 1671
    appdata.unicode = FALSE;
    appdata.str.a = lpApplicationDataBuf;
1672

1673 1674 1675 1676
    r = MSI_EnumComponentQualifiers( comp, iIndex,
              &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
    msi_free( comp );
    return r;
1677 1678
}

1679 1680 1681
/*************************************************************************
 *  MsiEnumComponentQualifiersW [MSI.@]
 */
1682
UINT WINAPI MsiEnumComponentQualifiersW( LPCWSTR szComponent, DWORD iIndex,
1683 1684
                LPWSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
                LPWSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1685
{
1686
    awstring qual, appdata;
1687

1688
    TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1689 1690
          lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
          pcchApplicationDataBuf);
1691

1692 1693
    qual.unicode = TRUE;
    qual.str.w = lpQualifierBuf;
1694

1695 1696
    appdata.unicode = TRUE;
    appdata.str.w = lpApplicationDataBuf;
1697

1698 1699
    return MSI_EnumComponentQualifiers( szComponent, iIndex,
                 &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1700 1701
}

1702 1703 1704 1705
/*************************************************************************
 *  MsiEnumRelatedProductsW   [MSI.@]
 *
 */
1706 1707 1708
UINT WINAPI MsiEnumRelatedProductsW(LPCWSTR szUpgradeCode, DWORD dwReserved,
                                    DWORD iProductIndex, LPWSTR lpProductBuf)
{
1709
    UINT r;
1710
    HKEY hkey;
1711 1712
    WCHAR szKeyName[SQUASHED_GUID_SIZE];
    DWORD dwSize = sizeof(szKeyName)/sizeof(szKeyName[0]);
1713

1714
    TRACE("%s %u %u %p\n", debugstr_w(szUpgradeCode), dwReserved,
1715
          iProductIndex, lpProductBuf);
1716 1717 1718 1719 1720 1721

    if (NULL == szUpgradeCode)
        return ERROR_INVALID_PARAMETER;
    if (NULL == lpProductBuf)
        return ERROR_INVALID_PARAMETER;

1722 1723 1724
    r = MSIREG_OpenUpgradeCodesKey(szUpgradeCode, &hkey, FALSE);
    if (r != ERROR_SUCCESS)
        return ERROR_NO_MORE_ITEMS;
1725

1726
    r = RegEnumValueW(hkey, iProductIndex, szKeyName, &dwSize, NULL, NULL, NULL, NULL);
1727 1728
    if( r == ERROR_SUCCESS )
        unsquash_guid(szKeyName, lpProductBuf);
1729 1730
    RegCloseKey(hkey);

1731
    return r;
1732 1733
}

1734 1735 1736 1737
/*************************************************************************
 *  MsiEnumRelatedProductsA   [MSI.@]
 *
 */
1738 1739 1740
UINT WINAPI MsiEnumRelatedProductsA(LPCSTR szUpgradeCode, DWORD dwReserved,
                                    DWORD iProductIndex, LPSTR lpProductBuf)
{
1741 1742 1743
    LPWSTR szwUpgradeCode = NULL;
    WCHAR productW[GUID_SIZE];
    UINT r;
1744

1745
    TRACE("%s %u %u %p\n", debugstr_a(szUpgradeCode), dwReserved,
1746
          iProductIndex, lpProductBuf);
1747

1748 1749 1750 1751 1752 1753
    if (szUpgradeCode)
    {
        szwUpgradeCode = strdupAtoW( szUpgradeCode );
        if( !szwUpgradeCode )
            return ERROR_OUTOFMEMORY;
    }
1754

1755 1756 1757 1758 1759 1760
    r = MsiEnumRelatedProductsW( szwUpgradeCode, dwReserved,
                                 iProductIndex, productW );
    if (r == ERROR_SUCCESS)
    {
        WideCharToMultiByte( CP_ACP, 0, productW, GUID_SIZE,
                             lpProductBuf, GUID_SIZE, NULL, NULL );
1761
    }
1762
    msi_free( szwUpgradeCode);
1763
    return r;
1764
}
1765

1766 1767 1768 1769 1770 1771
/***********************************************************************
 * MsiEnumPatchesExA            [MSI.@]
 */
UINT WINAPI MsiEnumPatchesExA(LPCSTR szProductCode, LPCSTR szUserSid,
        DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPSTR szPatchCode,
        LPSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
1772
        LPSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
1773
{
1774 1775 1776 1777 1778 1779 1780 1781 1782
    LPWSTR prodcode = NULL;
    LPWSTR usersid = NULL;
    LPWSTR targsid = NULL;
    WCHAR patch[GUID_SIZE];
    WCHAR targprod[GUID_SIZE];
    DWORD len;
    UINT r;

    TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
1783 1784 1785
          debugstr_a(szProductCode), debugstr_a(szUserSid), dwContext, dwFilter,
          dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
          szTargetUserSid, pcchTargetUserSid);
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842

    if (szTargetUserSid && !pcchTargetUserSid)
        return ERROR_INVALID_PARAMETER;

    if (szProductCode) prodcode = strdupAtoW(szProductCode);
    if (szUserSid) usersid = strdupAtoW(szUserSid);

    r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
                          patch, targprod, pdwTargetProductContext,
                          NULL, &len);
    if (r != ERROR_SUCCESS)
        goto done;

    WideCharToMultiByte(CP_ACP, 0, patch, -1, szPatchCode,
                        GUID_SIZE, NULL, NULL);
    WideCharToMultiByte(CP_ACP, 0, targprod, -1, szTargetProductCode,
                        GUID_SIZE, NULL, NULL);

    if (!szTargetUserSid)
    {
        if (pcchTargetUserSid)
            *pcchTargetUserSid = len;

        goto done;
    }

    targsid = msi_alloc(++len * sizeof(WCHAR));
    if (!targsid)
    {
        r = ERROR_OUTOFMEMORY;
        goto done;
    }

    r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
                          patch, targprod, pdwTargetProductContext,
                          targsid, &len);
    if (r != ERROR_SUCCESS || !szTargetUserSid)
        goto done;

    WideCharToMultiByte(CP_ACP, 0, targsid, -1, szTargetUserSid,
                        *pcchTargetUserSid, NULL, NULL);

    len = lstrlenW(targsid);
    if (*pcchTargetUserSid < len + 1)
    {
        r = ERROR_MORE_DATA;
        *pcchTargetUserSid = len * sizeof(WCHAR);
    }
    else
        *pcchTargetUserSid = len;

done:
    msi_free(prodcode);
    msi_free(usersid);
    msi_free(targsid);

    return r;
1843 1844
}

1845
static UINT msi_get_patch_state(LPCWSTR prodcode, LPCWSTR usersid,
1846
                                MSIINSTALLCONTEXT context,
1847 1848 1849 1850 1851 1852 1853 1854
                                LPWSTR patch, MSIPATCHSTATE *state)
{
    DWORD type, val, size;
    HKEY prod, hkey = 0;
    HKEY udpatch = 0;
    LONG res;
    UINT r = ERROR_NO_MORE_ITEMS;

1855 1856
    *state = MSIPATCHSTATE_INVALID;

1857 1858
    r = MSIREG_OpenUserDataProductKey(prodcode, context,
                                      usersid, &prod, FALSE);
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
    if (r != ERROR_SUCCESS)
        return ERROR_NO_MORE_ITEMS;

    res = RegOpenKeyExW(prod, szPatches, 0, KEY_READ, &hkey);
    if (res != ERROR_SUCCESS)
        goto done;

    res = RegOpenKeyExW(hkey, patch, 0, KEY_READ, &udpatch);
    if (res != ERROR_SUCCESS)
        goto done;

    size = sizeof(DWORD);
    res = RegGetValueW(udpatch, NULL, szState, RRF_RT_DWORD, &type, &val, &size);
    if (res != ERROR_SUCCESS ||
        val < MSIPATCHSTATE_APPLIED || val > MSIPATCHSTATE_REGISTERED)
    {
        r = ERROR_BAD_CONFIGURATION;
        goto done;
    }

    *state = val;
    r = ERROR_SUCCESS;

done:
    RegCloseKey(udpatch);
    RegCloseKey(hkey);
    RegCloseKey(prod);

    return r;
}

static UINT msi_check_product_patches(LPCWSTR prodcode, LPCWSTR usersid,
        MSIINSTALLCONTEXT context, DWORD filter, DWORD index, DWORD *idx,
        LPWSTR patch, LPWSTR targetprod, MSIINSTALLCONTEXT *targetctx,
1893
        LPWSTR targetsid, DWORD *sidsize, LPWSTR *transforms)
1894
{
1895
    MSIPATCHSTATE state = MSIPATCHSTATE_INVALID;
1896 1897 1898 1899 1900 1901 1902
    LPWSTR ptr, patches = NULL;
    HKEY prod, patchkey = 0;
    HKEY localprod = 0, localpatch = 0;
    DWORD type, size;
    LONG res;
    UINT temp, r = ERROR_NO_MORE_ITEMS;

1903 1904
    if (MSIREG_OpenProductKey(prodcode, usersid, context,
                              &prod, FALSE) != ERROR_SUCCESS)
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
        return ERROR_NO_MORE_ITEMS;

    size = 0;
    res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type, NULL,
                       &size);
    if (res != ERROR_SUCCESS)
        goto done;

    if (type != REG_MULTI_SZ)
    {
        r = ERROR_BAD_CONFIGURATION;
        goto done;
    }

    patches = msi_alloc(size);
    if (!patches)
    {
        r = ERROR_OUTOFMEMORY;
        goto done;
    }

    res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type,
                       patches, &size);
    if (res != ERROR_SUCCESS)
        goto done;

1931
    for (ptr = patches; *ptr && r == ERROR_NO_MORE_ITEMS; ptr += lstrlenW(ptr) + 1)
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
    {
        if (!unsquash_guid(ptr, patch))
        {
            r = ERROR_BAD_CONFIGURATION;
            goto done;
        }

        size = 0;
        res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
                           &type, NULL, &size);
        if (res != ERROR_SUCCESS)
            continue;

1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959
        if (transforms)
        {
            *transforms = msi_alloc(size);
            if (!*transforms)
            {
                r = ERROR_OUTOFMEMORY;
                goto done;
            }

            res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
                               &type, *transforms, &size);
            if (res != ERROR_SUCCESS)
                continue;
        }

1960 1961 1962 1963
        if (context == MSIINSTALLCONTEXT_USERMANAGED)
        {
            if (!(filter & MSIPATCHSTATE_APPLIED))
            {
1964 1965
                temp = msi_get_patch_state(prodcode, usersid, context,
                                           ptr, &state);
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
                if (temp == ERROR_BAD_CONFIGURATION)
                {
                    r = ERROR_BAD_CONFIGURATION;
                    goto done;
                }

                if (temp != ERROR_SUCCESS || !(filter & state))
                    continue;
            }
        }
        else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
        {
            if (!(filter & MSIPATCHSTATE_APPLIED))
            {
1980 1981
                temp = msi_get_patch_state(prodcode, usersid, context,
                                           ptr, &state);
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
                if (temp == ERROR_BAD_CONFIGURATION)
                {
                    r = ERROR_BAD_CONFIGURATION;
                    goto done;
                }

                if (temp != ERROR_SUCCESS || !(filter & state))
                    continue;
            }
            else
            {
1993 1994
                temp = MSIREG_OpenUserDataPatchKey(patch, context,
                                                   &patchkey, FALSE);
1995 1996 1997 1998 1999 2000 2001 2002 2003
                RegCloseKey(patchkey);
                if (temp != ERROR_SUCCESS)
                    continue;
            }
        }
        else if (context == MSIINSTALLCONTEXT_MACHINE)
        {
            usersid = szEmpty;

2004
            if (MSIREG_OpenUserDataProductKey(prodcode, context, NULL, &localprod, FALSE) == ERROR_SUCCESS &&
2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
                RegOpenKeyExW(localprod, szPatches, 0, KEY_READ, &localpatch) == ERROR_SUCCESS &&
                RegOpenKeyExW(localpatch, ptr, 0, KEY_READ, &patchkey) == ERROR_SUCCESS)
            {
                res = RegGetValueW(patchkey, NULL, szState, RRF_RT_REG_DWORD,
                                   &type, &state, &size);

                if (!(filter & state))
                    res = ERROR_NO_MORE_ITEMS;

                RegCloseKey(patchkey);
            }

            RegCloseKey(localpatch);
            RegCloseKey(localprod);

            if (res != ERROR_SUCCESS)
                continue;
        }

        if (*idx < index)
        {
            (*idx)++;
            continue;
        }

        r = ERROR_SUCCESS;
2031 2032
        if (targetprod)
            lstrcpyW(targetprod, prodcode);
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058

        if (targetctx)
            *targetctx = context;

        if (targetsid)
        {
            lstrcpynW(targetsid, usersid, *sidsize);
            if (lstrlenW(usersid) >= *sidsize)
                r = ERROR_MORE_DATA;
        }

        if (sidsize)
        {
            *sidsize = lstrlenW(usersid);
            if (!targetsid)
                *sidsize *= sizeof(WCHAR);
        }
    }

done:
    RegCloseKey(prod);
    msi_free(patches);

    return r;
}

2059 2060 2061 2062 2063 2064
static UINT msi_enum_patches(LPCWSTR szProductCode, LPCWSTR szUserSid,
        DWORD dwContext, DWORD dwFilter, DWORD dwIndex, DWORD *idx,
        LPWSTR szPatchCode, LPWSTR szTargetProductCode,
        MSIINSTALLCONTEXT *pdwTargetProductContext, LPWSTR szTargetUserSid,
        LPDWORD pcchTargetUserSid, LPWSTR *szTransforms)
{
2065
    LPWSTR usersid = NULL;
2066
    UINT r = ERROR_INVALID_PARAMETER;
2067

2068 2069
    if (!szUserSid)
    {
2070 2071
        szUserSid = usersid = get_user_sid();
        if (!usersid) return ERROR_FUNCTION_FAILED;
2072 2073
    }

2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110
    if (dwContext & MSIINSTALLCONTEXT_USERMANAGED)
    {
        r = msi_check_product_patches(szProductCode, szUserSid,
                                      MSIINSTALLCONTEXT_USERMANAGED, dwFilter,
                                      dwIndex, idx, szPatchCode,
                                      szTargetProductCode,
                                      pdwTargetProductContext, szTargetUserSid,
                                      pcchTargetUserSid, szTransforms);
        if (r != ERROR_NO_MORE_ITEMS)
            goto done;
    }

    if (dwContext & MSIINSTALLCONTEXT_USERUNMANAGED)
    {
        r = msi_check_product_patches(szProductCode, szUserSid,
                                      MSIINSTALLCONTEXT_USERUNMANAGED, dwFilter,
                                      dwIndex, idx, szPatchCode,
                                      szTargetProductCode,
                                      pdwTargetProductContext, szTargetUserSid,
                                      pcchTargetUserSid, szTransforms);
        if (r != ERROR_NO_MORE_ITEMS)
            goto done;
    }

    if (dwContext & MSIINSTALLCONTEXT_MACHINE)
    {
        r = msi_check_product_patches(szProductCode, szUserSid,
                                      MSIINSTALLCONTEXT_MACHINE, dwFilter,
                                      dwIndex, idx, szPatchCode,
                                      szTargetProductCode,
                                      pdwTargetProductContext, szTargetUserSid,
                                      pcchTargetUserSid, szTransforms);
        if (r != ERROR_NO_MORE_ITEMS)
            goto done;
    }

done:
2111
    LocalFree(usersid);
2112 2113 2114
    return r;
}

2115
/***********************************************************************
2116
 * MsiEnumPatchesExW            [MSI.@]
2117 2118 2119 2120
 */
UINT WINAPI MsiEnumPatchesExW(LPCWSTR szProductCode, LPCWSTR szUserSid,
        DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPWSTR szPatchCode,
        LPWSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
2121
        LPWSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
2122
{
2123
    WCHAR squashed_pc[SQUASHED_GUID_SIZE];
2124 2125 2126
    DWORD idx = 0;
    UINT r;

2127
    static DWORD last_index;
2128 2129

    TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
2130 2131 2132
          debugstr_w(szProductCode), debugstr_w(szUserSid), dwContext, dwFilter,
          dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
          szTargetUserSid, pcchTargetUserSid);
2133

2134
    if (!szProductCode || !squash_guid( szProductCode, squashed_pc ))
2135 2136
        return ERROR_INVALID_PARAMETER;

2137
    if (szUserSid && !strcmpW( szUserSid, szLocalSid ))
2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
        return ERROR_INVALID_PARAMETER;

    if (dwContext & MSIINSTALLCONTEXT_MACHINE && szUserSid)
        return ERROR_INVALID_PARAMETER;

    if (dwContext <= MSIINSTALLCONTEXT_NONE ||
        dwContext > MSIINSTALLCONTEXT_ALL)
        return ERROR_INVALID_PARAMETER;

    if (dwFilter <= MSIPATCHSTATE_INVALID || dwFilter > MSIPATCHSTATE_ALL)
        return ERROR_INVALID_PARAMETER;

    if (dwIndex && dwIndex - last_index != 1)
        return ERROR_INVALID_PARAMETER;

    if (dwIndex == 0)
        last_index = 0;

2156 2157 2158 2159
    r = msi_enum_patches(szProductCode, szUserSid, dwContext, dwFilter,
                         dwIndex, &idx, szPatchCode, szTargetProductCode,
                         pdwTargetProductContext, szTargetUserSid,
                         pcchTargetUserSid, NULL);
2160 2161 2162

    if (r == ERROR_SUCCESS)
        last_index = dwIndex;
2163 2164
    else
        last_index = 0;
2165 2166

    return r;
2167 2168
}

2169 2170 2171
/***********************************************************************
 * MsiEnumPatchesA            [MSI.@]
 */
2172
UINT WINAPI MsiEnumPatchesA(LPCSTR szProduct, DWORD iPatchIndex,
2173
        LPSTR lpPatchBuf, LPSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2174
{
2175
    LPWSTR product, transforms;
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
    WCHAR patch[GUID_SIZE];
    DWORD len;
    UINT r;

    TRACE("(%s %d %p %p %p)\n", debugstr_a(szProduct), iPatchIndex,
          lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);

    if (!szProduct || !lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
        return ERROR_INVALID_PARAMETER;

    product = strdupAtoW(szProduct);
    if (!product)
        return ERROR_OUTOFMEMORY;

2190 2191
    len = *pcchTransformsBuf;
    transforms = msi_alloc( len * sizeof(WCHAR) );
2192 2193 2194 2195 2196 2197 2198
    if (!transforms)
    {
        r = ERROR_OUTOFMEMORY;
        goto done;
    }

    r = MsiEnumPatchesW(product, iPatchIndex, patch, transforms, &len);
2199
    if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
2200 2201 2202 2203 2204
        goto done;

    WideCharToMultiByte(CP_ACP, 0, patch, -1, lpPatchBuf,
                        GUID_SIZE, NULL, NULL);

2205 2206 2207
    if (!WideCharToMultiByte(CP_ACP, 0, transforms, -1, lpTransformsBuf,
                             *pcchTransformsBuf, NULL, NULL))
        r = ERROR_MORE_DATA;
2208

2209
    if (r == ERROR_MORE_DATA)
2210 2211
    {
        lpTransformsBuf[*pcchTransformsBuf - 1] = '\0';
2212
        *pcchTransformsBuf = len * 2;
2213 2214
    }
    else
2215
        *pcchTransformsBuf = strlen( lpTransformsBuf );
2216 2217 2218 2219 2220 2221

done:
    msi_free(transforms);
    msi_free(product);

    return r;
2222 2223 2224 2225 2226
}

/***********************************************************************
 * MsiEnumPatchesW            [MSI.@]
 */
2227
UINT WINAPI MsiEnumPatchesW(LPCWSTR szProduct, DWORD iPatchIndex,
2228
        LPWSTR lpPatchBuf, LPWSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2229
{
2230
    WCHAR *transforms = NULL, squashed_pc[SQUASHED_GUID_SIZE];
2231 2232 2233 2234 2235 2236 2237
    HKEY prod;
    DWORD idx = 0;
    UINT r;

    TRACE("(%s %d %p %p %p)\n", debugstr_w(szProduct), iPatchIndex,
          lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);

2238
    if (!szProduct || !squash_guid( szProduct, squashed_pc ))
2239 2240 2241 2242 2243
        return ERROR_INVALID_PARAMETER;

    if (!lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
        return ERROR_INVALID_PARAMETER;

2244
    if (MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED,
2245
                              &prod, FALSE) != ERROR_SUCCESS &&
2246
        MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
2247
                              &prod, FALSE) != ERROR_SUCCESS &&
2248
        MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_MACHINE,
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263
                              &prod, FALSE) != ERROR_SUCCESS)
        return ERROR_UNKNOWN_PRODUCT;

    RegCloseKey(prod);

    r = msi_enum_patches(szProduct, NULL, MSIINSTALLCONTEXT_ALL,
                         MSIPATCHSTATE_ALL, iPatchIndex, &idx, lpPatchBuf,
                         NULL, NULL, NULL, NULL, &transforms);
    if (r != ERROR_SUCCESS)
        goto done;

    lstrcpynW(lpTransformsBuf, transforms, *pcchTransformsBuf);
    if (*pcchTransformsBuf <= lstrlenW(transforms))
    {
        r = ERROR_MORE_DATA;
2264
        *pcchTransformsBuf = lstrlenW(transforms);
2265 2266 2267 2268 2269 2270 2271
    }
    else
        *pcchTransformsBuf = lstrlenW(transforms);

done:
    msi_free(transforms);
    return r;
2272
}
2273

2274 2275 2276
UINT WINAPI MsiEnumProductsExA( LPCSTR product, LPCSTR usersid, DWORD ctx, DWORD index,
                                CHAR installed_product[GUID_SIZE],
                                MSIINSTALLCONTEXT *installed_ctx, LPSTR sid, LPDWORD sid_len )
2277
{
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318
    UINT r;
    WCHAR installed_productW[GUID_SIZE], *productW = NULL, *usersidW = NULL, *sidW = NULL;

    TRACE("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_a(product), debugstr_a(usersid),
          ctx, index, installed_product, installed_ctx, sid, sid_len);

    if (sid && !sid_len) return ERROR_INVALID_PARAMETER;
    if (product && !(productW = strdupAtoW( product ))) return ERROR_OUTOFMEMORY;
    if (usersid && !(usersidW = strdupAtoW( usersid )))
    {
        msi_free( productW );
        return ERROR_OUTOFMEMORY;
    }
    if (sid && !(sidW = msi_alloc( *sid_len * sizeof(WCHAR) )))
    {
        msi_free( usersidW );
        msi_free( productW );
        return ERROR_OUTOFMEMORY;
    }
    r = MsiEnumProductsExW( productW, usersidW, ctx, index, installed_productW,
                            installed_ctx, sidW, sid_len );
    if (r == ERROR_SUCCESS)
    {
        if (installed_product) WideCharToMultiByte( CP_ACP, 0, installed_productW, GUID_SIZE,
                                                    installed_product, GUID_SIZE, NULL, NULL );
        if (sid) WideCharToMultiByte( CP_ACP, 0, sidW, *sid_len + 1, sid, *sid_len + 1, NULL, NULL );
    }
    msi_free( productW );
    msi_free( usersidW );
    msi_free( sidW );
    return r;
}

static UINT fetch_machine_product( const WCHAR *match, DWORD index, DWORD *idx,
                                   WCHAR installed_product[GUID_SIZE],
                                   MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
{
    static const WCHAR productsW[] =
        {'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
         'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
    UINT r;
2319
    WCHAR product[SQUASHED_GUID_SIZE];
2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341
    DWORD i = 0, len;
    REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
    HKEY key;

    if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, productsW, 0, access, &key ))
        return ERROR_NO_MORE_ITEMS;

    len = sizeof(product)/sizeof(product[0]);
    while (!RegEnumKeyExW( key, i, product, &len, NULL, NULL, NULL, NULL ))
    {
        if (match && strcmpW( match, product ))
        {
            i++;
            len = sizeof(product)/sizeof(product[0]);
            continue;
        }
        if (*idx == index) goto found;
        (*idx)++;
        len = sizeof(product)/sizeof(product[0]);
        i++;
    }
    RegCloseKey( key );
2342
    return ERROR_NO_MORE_ITEMS;
2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362

found:
    if (sid_len && *sid_len < 1)
    {
        *sid_len = 1;
        r = ERROR_MORE_DATA;
    }
    else
    {
        if (installed_product) unsquash_guid( product, installed_product );
        if (installed_ctx) *installed_ctx = MSIINSTALLCONTEXT_MACHINE;
        if (sid)
        {
            sid[0] = 0;
            *sid_len = 0;
        }
        r = ERROR_SUCCESS;
    }
    RegCloseKey( key );
    return r;
2363 2364
}

2365 2366 2367
static UINT fetch_user_product( const WCHAR *match, const WCHAR *usersid, DWORD ctx, DWORD index,
                                DWORD *idx, WCHAR installed_product[GUID_SIZE],
                                MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2368
{
2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379
    static const WCHAR managedW[] =
        {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
         'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s',
         'i','o','n','\\','I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d',0};
    static const WCHAR managed_productsW[] =
        {'\\','I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
    static const WCHAR unmanaged_productsW[] =
        {'\\','S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
         'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
    UINT r;
    const WCHAR *subkey;
2380
    WCHAR path[MAX_PATH], product[SQUASHED_GUID_SIZE], user[128];
2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409
    DWORD i = 0, j = 0, len_product, len_user;
    REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
    HKEY key_users, key_products;

    if (ctx == MSIINSTALLCONTEXT_USERMANAGED)
    {
        subkey = managed_productsW;
        if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, managedW, 0, access, &key_users ))
            return ERROR_NO_MORE_ITEMS;
    }
    else if (ctx == MSIINSTALLCONTEXT_USERUNMANAGED)
    {
        subkey = unmanaged_productsW;
        if (RegOpenKeyExW( HKEY_USERS, NULL, 0, access, &key_users ))
            return ERROR_NO_MORE_ITEMS;
    }
    else return ERROR_INVALID_PARAMETER;

    len_user = sizeof(user)/sizeof(user[0]);
    while (!RegEnumKeyExW( key_users, i, user, &len_user, NULL, NULL, NULL, NULL ))
    {
        if (strcmpW( usersid, user ) && strcmpW( usersid, szAllSid ))
        {
            i++;
            len_user = sizeof(user)/sizeof(user[0]);
            continue;
        }
        strcpyW( path, user );
        strcatW( path, subkey );
2410
        if (RegOpenKeyExW( key_users, path, 0, access, &key_products ))
2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434
        {
            i++;
            len_user = sizeof(user)/sizeof(user[0]);
            continue;
        }
        len_product = sizeof(product)/sizeof(product[0]);
        while (!RegEnumKeyExW( key_products, j, product, &len_product, NULL, NULL, NULL, NULL ))
        {
            if (match && strcmpW( match, product ))
            {
                j++;
                len_product = sizeof(product)/sizeof(product[0]);
                continue;
            }
            if (*idx == index) goto found;
            (*idx)++;
            len_product = sizeof(product)/sizeof(product[0]);
            j++;
        }
        RegCloseKey( key_products );
        len_user = sizeof(user)/sizeof(user[0]);
        i++;
    }
    RegCloseKey( key_users );
2435
    return ERROR_NO_MORE_ITEMS;
2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521

found:
    if (sid_len && *sid_len <= len_user)
    {
        *sid_len = len_user;
        r = ERROR_MORE_DATA;
    }
    else
    {
        if (installed_product) unsquash_guid( product, installed_product );
        if (installed_ctx) *installed_ctx = ctx;
        if (sid)
        {
            strcpyW( sid, user );
            *sid_len = len_user;
        }
        r = ERROR_SUCCESS;
    }
    RegCloseKey( key_products );
    RegCloseKey( key_users );
    return r;
}

static UINT enum_products( const WCHAR *product, const WCHAR *usersid, DWORD ctx, DWORD index,
                           DWORD *idx, WCHAR installed_product[GUID_SIZE],
                           MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
{
    UINT r = ERROR_NO_MORE_ITEMS;
    WCHAR *user = NULL;

    if (!usersid)
    {
        usersid = user = get_user_sid();
        if (!user) return ERROR_FUNCTION_FAILED;
    }
    if (ctx & MSIINSTALLCONTEXT_MACHINE)
    {
        r = fetch_machine_product( product, index, idx, installed_product, installed_ctx,
                                   sid, sid_len );
        if (r != ERROR_NO_MORE_ITEMS) goto done;
    }
    if (ctx & MSIINSTALLCONTEXT_USERUNMANAGED)
    {
        r = fetch_user_product( product, usersid, MSIINSTALLCONTEXT_USERUNMANAGED, index,
                                idx, installed_product, installed_ctx, sid, sid_len );
        if (r != ERROR_NO_MORE_ITEMS) goto done;
    }
    if (ctx & MSIINSTALLCONTEXT_USERMANAGED)
    {
        r = fetch_user_product( product, usersid, MSIINSTALLCONTEXT_USERMANAGED, index,
                                idx, installed_product, installed_ctx, sid, sid_len );
        if (r != ERROR_NO_MORE_ITEMS) goto done;
    }

done:
    LocalFree( user );
    return r;
}

UINT WINAPI MsiEnumProductsExW( LPCWSTR product, LPCWSTR usersid, DWORD ctx, DWORD index,
                                WCHAR installed_product[GUID_SIZE],
                                MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
{
    UINT r;
    DWORD idx = 0;
    static DWORD last_index;

    TRACE("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_w(product), debugstr_w(usersid),
          ctx, index, installed_product, installed_ctx, sid, sid_len);

    if ((sid && !sid_len) || !ctx || (usersid && ctx == MSIINSTALLCONTEXT_MACHINE))
        return ERROR_INVALID_PARAMETER;

    if (index && index - last_index != 1)
        return ERROR_INVALID_PARAMETER;

    if (!index) last_index = 0;

    r = enum_products( product, usersid, ctx, index, &idx, installed_product, installed_ctx,
                       sid, sid_len );
    if (r == ERROR_SUCCESS)
        last_index = index;
    else
        last_index = 0;

    return r;
2522
}