suminfo.c 33.8 KB
Newer Older
1 2 3
/*
 * Implementation of the Microsoft Installer (msi.dll)
 *
4
 * Copyright 2002, 2005 Mike McCormack for CodeWeavers
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21 22
#include <stdarg.h>

23 24 25
#define COBJMACROS
#define NONAMELESSUNION

26
#include "stdio.h"
27 28 29
#include "windef.h"
#include "winbase.h"
#include "winreg.h"
30
#include "winnls.h"
31 32
#include "shlwapi.h"
#include "wine/debug.h"
33
#include "wine/exception.h"
34 35
#include "msi.h"
#include "msiquery.h"
36
#include "msidefs.h"
37
#include "objidl.h"
38
#include "propvarutil.h"
39 40 41

#include "msipriv.h"
#include "winemsi.h"
42 43 44

WINE_DEFAULT_DEBUG_CHANNEL(msi);

45 46
#include "pshpack1.h"

47
typedef struct {
48 49 50 51 52 53 54
    WORD wByteOrder;
    WORD wFormat;
    DWORD dwOSVer;
    CLSID clsID;
    DWORD reserved;
} PROPERTYSETHEADER;

55
typedef struct {
56 57 58 59
    FMTID fmtid;
    DWORD dwOffset;
} FORMATIDOFFSET;

60
typedef struct {
61 62
    DWORD cbSection;
    DWORD cProperties;
63 64 65
} PROPERTYSECTIONHEADER;

typedef struct {
66 67
    DWORD propid;
    DWORD dwOffset;
68
} PROPERTYIDOFFSET;
69 70 71 72 73 74 75 76 77 78 79 80 81

typedef struct {
    DWORD type;
    union {
        INT i4;
        SHORT i2;
        FILETIME ft;
        struct {
            DWORD len;
            BYTE str[1];
        } str;
    } u;
} PROPERTY_DATA;
82

83 84
#include "poppack.h"

85 86 87 88
static HRESULT (WINAPI *pPropVariantChangeType)
    (PROPVARIANT *ppropvarDest, REFPROPVARIANT propvarSrc,
     PROPVAR_CHANGE_FLAGS flags, VARTYPE vt);

89 90
#define SECT_HDR_SIZE (sizeof(PROPERTYSECTIONHEADER))

91 92 93
static void free_prop( PROPVARIANT *prop )
{
    if (prop->vt == VT_LPSTR )
94
        msi_free( prop->u.pszVal );
95 96 97
    prop->vt = VT_EMPTY;
}

98
static void MSI_CloseSummaryInfo( MSIOBJECTHDR *arg )
99
{
100
    MSISUMMARYINFO *si = (MSISUMMARYINFO *) arg;
101 102 103 104
    DWORD i;

    for( i = 0; i < MSI_MAX_PROPS; i++ )
        free_prop( &si->property[i] );
105
    IStorage_Release( si->storage );
106 107
}

108
static UINT get_type( UINT uiProperty )
109
{
110 111 112 113
    switch( uiProperty )
    {
    case PID_CODEPAGE:
         return VT_I2;
114

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    case PID_SUBJECT:
    case PID_AUTHOR:
    case PID_KEYWORDS:
    case PID_COMMENTS:
    case PID_TEMPLATE:
    case PID_LASTAUTHOR:
    case PID_REVNUMBER:
    case PID_APPNAME:
    case PID_TITLE:
         return VT_LPSTR;

    case PID_LASTPRINTED:
    case PID_CREATE_DTM:
    case PID_LASTSAVE_DTM:
         return VT_FILETIME;

    case PID_WORDCOUNT:
    case PID_CHARCOUNT:
    case PID_SECURITY:
    case PID_PAGECOUNT:
         return VT_I4;
    }
    return VT_EMPTY;
}
139

140
static UINT get_property_count( const PROPVARIANT *property )
141 142 143 144 145
{
    UINT i, n = 0;

    if( !property )
        return n;
146
    for( i = 0; i < MSI_MAX_PROPS; i++ )
147 148 149 150
        if( property[i].vt != VT_EMPTY )
            n++;
    return n;
}
151

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
static UINT propvar_changetype(PROPVARIANT *changed, PROPVARIANT *property, VARTYPE vt)
{
    HRESULT hr;
    HMODULE propsys = LoadLibraryA("propsys.dll");
    pPropVariantChangeType = (void *)GetProcAddress(propsys, "PropVariantChangeType");

    if (!pPropVariantChangeType)
    {
        ERR("PropVariantChangeType function missing!\n");
        return ERROR_FUNCTION_FAILED;
    }

    hr = pPropVariantChangeType(changed, property, 0, vt);
    return (hr == S_OK) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
}

168
/* FIXME: doesn't deal with endian conversion */
169
static void read_properties_from_data( PROPVARIANT *prop, LPBYTE data, DWORD sz )
170 171
{
    UINT type;
172
    DWORD i, size;
173
    PROPERTY_DATA *propdata;
174 175
    PROPVARIANT property, *ptr;
    PROPVARIANT changed;
176 177 178 179 180
    PROPERTYIDOFFSET *idofs;
    PROPERTYSECTIONHEADER *section_hdr;

    section_hdr = (PROPERTYSECTIONHEADER*) &data[0];
    idofs = (PROPERTYIDOFFSET*) &data[SECT_HDR_SIZE];
181 182

    /* now set all the properties */
183
    for( i = 0; i < section_hdr->cProperties; i++ )
184
    {
185 186 187 188 189 190
        if( idofs[i].propid >= MSI_MAX_PROPS )
        {
            ERR("Unknown property ID %d\n", idofs[i].propid );
            break;
        }

191 192 193
        type = get_type( idofs[i].propid );
        if( type == VT_EMPTY )
        {
194
            ERR("propid %d has unknown type\n", idofs[i].propid);
195 196 197
            break;
        }

198
        propdata = (PROPERTY_DATA*) &data[ idofs[i].dwOffset ];
199 200 201 202

        /* check we don't run off the end of the data */
        size = sz - idofs[i].dwOffset - sizeof(DWORD);
        if( sizeof(DWORD) > size ||
203 204
            ( propdata->type == VT_FILETIME && sizeof(FILETIME) > size ) ||
            ( propdata->type == VT_LPSTR && (propdata->u.str.len + sizeof(DWORD)) > size ) )
205 206 207 208 209
        {
            ERR("not enough data\n");
            break;
        }

210 211
        property.vt = propdata->type;
        if( propdata->type == VT_LPSTR )
212
        {
213
            LPSTR str = msi_alloc( propdata->u.str.len );
214 215
            memcpy( str, propdata->u.str.str, propdata->u.str.len );
            str[ propdata->u.str.len - 1 ] = 0;
216 217 218 219 220 221 222 223 224 225 226 227 228 229
            property.u.pszVal = str;
        }
        else if( propdata->type == VT_FILETIME )
            property.u.filetime = propdata->u.ft;
        else if( propdata->type == VT_I2 )
            property.u.iVal = propdata->u.i2;
        else if( propdata->type == VT_I4 )
            property.u.lVal = propdata->u.i4;

        /* check the type is the same as we expect */
        if( type != propdata->type )
        {
            propvar_changetype(&changed, &property, type);
            ptr = &changed;
230
        }
231 232 233
        else
            ptr = &property;

234
        prop[ idofs[i].propid ] = *ptr;
235
    }
236
}
237

238 239 240 241 242 243 244 245 246
static UINT load_summary_info( MSISUMMARYINFO *si, IStream *stm )
{
    PROPERTYSETHEADER set_hdr;
    FORMATIDOFFSET format_hdr;
    PROPERTYSECTIONHEADER section_hdr;
    LPBYTE data = NULL;
    LARGE_INTEGER ofs;
    ULONG count, sz;
    HRESULT r;
247

248 249 250 251 252 253
    TRACE("%p %p\n", si, stm);

    /* read the header */
    sz = sizeof set_hdr;
    r = IStream_Read( stm, &set_hdr, sz, &count );
    if( FAILED(r) || count != sz )
254
        return ERROR_FUNCTION_FAILED;
255 256 257 258

    if( set_hdr.wByteOrder != 0xfffe )
    {
        ERR("property set not big-endian %04X\n", set_hdr.wByteOrder);
259
        return ERROR_FUNCTION_FAILED;
260 261 262 263 264
    }

    sz = sizeof format_hdr;
    r = IStream_Read( stm, &format_hdr, sz, &count );
    if( FAILED(r) || count != sz )
265
        return ERROR_FUNCTION_FAILED;
266 267 268

    /* check the format id is correct */
    if( !IsEqualGUID( &FMTID_SummaryInformation, &format_hdr.fmtid ) )
269
        return ERROR_FUNCTION_FAILED;
270 271 272 273 274

    /* seek to the location of the section */
    ofs.QuadPart = format_hdr.dwOffset;
    r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, NULL );
    if( FAILED(r) )
275
        return ERROR_FUNCTION_FAILED;
276 277

    /* read the section itself */
278
    sz = SECT_HDR_SIZE;
279 280
    r = IStream_Read( stm, &section_hdr, sz, &count );
    if( FAILED(r) || count != sz )
281
        return ERROR_FUNCTION_FAILED;
282 283 284

    if( section_hdr.cProperties > MSI_MAX_PROPS )
    {
285
        ERR("too many properties %d\n", section_hdr.cProperties);
286
        return ERROR_FUNCTION_FAILED;
287 288
    }

289
    data = msi_alloc( section_hdr.cbSection);
290
    if( !data )
291
        return ERROR_FUNCTION_FAILED;
292

293 294
    memcpy( data, &section_hdr, SECT_HDR_SIZE );

295
    /* read all the data in one go */
296 297
    sz = section_hdr.cbSection - SECT_HDR_SIZE;
    r = IStream_Read( stm, &data[ SECT_HDR_SIZE ], sz, &count );
298
    if( SUCCEEDED(r) && count == sz )
299 300
        read_properties_from_data( si->property, data, sz + SECT_HDR_SIZE );
    else
301
        ERR("failed to read properties %d %d\n", count, sz);
302

303
    msi_free( data );
304
    return ERROR_SUCCESS;
305 306
}

307 308 309 310 311 312 313 314 315 316 317 318
static DWORD write_dword( LPBYTE data, DWORD ofs, DWORD val )
{
    if( data )
    {
        data[ofs++] = val&0xff;
        data[ofs++] = (val>>8)&0xff;
        data[ofs++] = (val>>16)&0xff;
        data[ofs++] = (val>>24)&0xff;
    }
    return 4;
}

319
static DWORD write_filetime( LPBYTE data, DWORD ofs, const FILETIME *ft )
320 321 322 323 324 325 326 327 328 329 330
{
    write_dword( data, ofs, ft->dwLowDateTime );
    write_dword( data, ofs + 4, ft->dwHighDateTime );
    return 8;
}

static DWORD write_string( LPBYTE data, DWORD ofs, LPCSTR str )
{
    DWORD len = lstrlenA( str ) + 1;
    write_dword( data, ofs, len );
    if( data )
331
        memcpy( &data[ofs + 4], str, len );
332 333 334
    return (7 + len) & ~3;
}

335
static UINT write_property_to_data( const PROPVARIANT *prop, LPBYTE data )
336
{
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
    DWORD sz = 0;

    if( prop->vt == VT_EMPTY )
        return sz;

    /* add the type */
    sz += write_dword( data, sz, prop->vt );
    switch( prop->vt )
    {
    case VT_I2:
        sz += write_dword( data, sz, prop->u.iVal );
        break;
    case VT_I4:
        sz += write_dword( data, sz, prop->u.lVal );
        break;
    case VT_FILETIME:
        sz += write_filetime( data, sz, &prop->u.filetime );
        break;
    case VT_LPSTR:
        sz += write_string( data, sz, prop->u.pszVal );
        break;
    }
    return sz;
}

362
static UINT save_summary_info( const MSISUMMARYINFO * si, IStream *stm )
363 364 365 366 367 368 369 370
{
    UINT ret = ERROR_FUNCTION_FAILED;
    PROPERTYSETHEADER set_hdr;
    FORMATIDOFFSET format_hdr;
    PROPERTYSECTIONHEADER section_hdr;
    PROPERTYIDOFFSET idofs[MSI_MAX_PROPS];
    LPBYTE data = NULL;
    ULONG count, sz;
371
    HRESULT r;
372
    int i;
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

    /* write the header */
    sz = sizeof set_hdr;
    memset( &set_hdr, 0, sz );
    set_hdr.wByteOrder = 0xfffe;
    set_hdr.wFormat = 0;
    set_hdr.dwOSVer = 0x00020005; /* build 5, platform id 2 */
    /* set_hdr.clsID is {00000000-0000-0000-0000-000000000000} */
    set_hdr.reserved = 1;
    r = IStream_Write( stm, &set_hdr, sz, &count );
    if( FAILED(r) || count != sz )
        return ret;

    /* write the format header */
    sz = sizeof format_hdr;
388
    format_hdr.fmtid = FMTID_SummaryInformation;
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
    format_hdr.dwOffset = sizeof format_hdr + sizeof set_hdr;
    r = IStream_Write( stm, &format_hdr, sz, &count );
    if( FAILED(r) || count != sz )
        return ret;

    /* add up how much space the data will take and calculate the offsets */
    section_hdr.cbSection = sizeof section_hdr;
    section_hdr.cbSection += (get_property_count( si->property ) * sizeof idofs[0]);
    section_hdr.cProperties = 0;
    for( i = 0; i < MSI_MAX_PROPS; i++ )
    {
        sz = write_property_to_data( &si->property[i], NULL );
        if( !sz )
            continue;
        idofs[ section_hdr.cProperties ].propid = i;
        idofs[ section_hdr.cProperties ].dwOffset = section_hdr.cbSection;
        section_hdr.cProperties++;
        section_hdr.cbSection += sz;
    }

409
    data = msi_alloc_zero( section_hdr.cbSection );
410 411 412 413 414 415 416 417 418 419 420 421 422

    sz = 0;
    memcpy( &data[sz], &section_hdr, sizeof section_hdr );
    sz += sizeof section_hdr;

    memcpy( &data[sz], idofs, section_hdr.cProperties * sizeof idofs[0] );
    sz += section_hdr.cProperties * sizeof idofs[0];

    /* write out the data */
    for( i = 0; i < MSI_MAX_PROPS; i++ )
        sz += write_property_to_data( &si->property[i], &data[sz] );

    r = IStream_Write( stm, data, sz, &count );
423
    msi_free( data );
424 425 426 427 428 429
    if( FAILED(r) || count != sz )
        return ret;

    return ERROR_SUCCESS;
}

430
static MSISUMMARYINFO *create_suminfo( IStorage *stg, UINT update_count )
431 432
{
    MSISUMMARYINFO *si;
433

434 435
    if (!(si = alloc_msiobject( MSIHANDLETYPE_SUMMARYINFO, sizeof(MSISUMMARYINFO), MSI_CloseSummaryInfo )))
        return NULL;
436

437
    si->update_count = update_count;
438 439
    IStorage_AddRef( stg );
    si->storage = stg;
440

441 442 443 444 445 446 447 448 449 450 451 452 453 454
    return si;
}

UINT msi_get_suminfo( IStorage *stg, UINT uiUpdateCount, MSISUMMARYINFO **ret )
{
    IStream *stm;
    MSISUMMARYINFO *si;
    HRESULT hr;
    UINT r;

    TRACE("%p, %u\n", stg, uiUpdateCount);

    if (!(si = create_suminfo( stg, uiUpdateCount ))) return ERROR_OUTOFMEMORY;

455
    hr = IStorage_OpenStream( si->storage, L"\5SummaryInformation", 0, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &stm );
456
    if (FAILED( hr ))
457
    {
458 459
        msiobj_release( &si->hdr );
        return ERROR_FUNCTION_FAILED;
460 461
    }

462 463 464 465 466 467 468 469 470 471 472 473
    r = load_summary_info( si, stm );
    IStream_Release( stm );
    if (r != ERROR_SUCCESS)
    {
        msiobj_release( &si->hdr );
        return r;
    }

    *ret = si;
    return ERROR_SUCCESS;
}

474
UINT msi_get_db_suminfo( MSIDATABASE *db, UINT uiUpdateCount, MSISUMMARYINFO **ret )
475 476 477 478 479 480 481
{
    IStream *stm;
    MSISUMMARYINFO *si;
    UINT r;

    if (!(si = create_suminfo( db->storage, uiUpdateCount ))) return ERROR_OUTOFMEMORY;

482
    r = msi_get_stream( db, L"\5SummaryInformation", &stm );
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
    if (r != ERROR_SUCCESS)
    {
        msiobj_release( &si->hdr );
        return r;
    }

    r = load_summary_info( si, stm );
    IStream_Release( stm );
    if (r != ERROR_SUCCESS)
    {
        msiobj_release( &si->hdr );
        return r;
    }

    *ret = si;
    return ERROR_SUCCESS;
499 500
}

501
UINT WINAPI MsiGetSummaryInformationW( MSIHANDLE hDatabase,
502 503 504 505
              LPCWSTR szDatabase, UINT uiUpdateCount, MSIHANDLE *pHandle )
{
    MSISUMMARYINFO *si;
    MSIDATABASE *db;
506
    UINT ret;
507

508
    TRACE("%d %s %d %p\n", hDatabase, debugstr_w(szDatabase),
509 510 511 512 513
           uiUpdateCount, pHandle);

    if( !pHandle )
        return ERROR_INVALID_PARAMETER;

514
    if( szDatabase && szDatabase[0] )
515
    {
516 517 518
        LPCWSTR persist = uiUpdateCount ? MSIDBOPEN_TRANSACT : MSIDBOPEN_READONLY;

        ret = MSI_OpenDatabaseW( szDatabase, persist, &db );
519 520 521
        if( ret != ERROR_SUCCESS )
            return ret;
    }
522
    else
523 524 525
    {
        db = msihandle2msiinfo( hDatabase, MSIHANDLETYPE_DATABASE );
        if( !db )
526
        {
527
            MSIHANDLE remote, remote_suminfo;
528

529
            if (!(remote = msi_get_remote(hDatabase)))
530 531
                return ERROR_INVALID_HANDLE;

532 533 534 535 536 537 538 539 540 541
            __TRY
            {
                ret = remote_DatabaseGetSummaryInformation(remote, uiUpdateCount, &remote_suminfo);
            }
            __EXCEPT(rpc_filter)
            {
                ret = GetExceptionCode();
            }
            __ENDTRY

542 543
            if (!ret)
                *pHandle = alloc_msi_remote_handle(remote_suminfo);
544

545
            return ret;
546
        }
547 548
    }

549 550
    ret = msi_get_suminfo( db->storage, uiUpdateCount, &si );
    if (ret != ERROR_SUCCESS)
551
        ret = msi_get_db_suminfo( db, uiUpdateCount, &si );
552 553 554 555 556 557 558
    if (ret != ERROR_SUCCESS)
    {
        if ((si = create_suminfo( db->storage, uiUpdateCount )))
            ret = ERROR_SUCCESS;
    }

    if (ret == ERROR_SUCCESS)
559 560 561 562
    {
        *pHandle = alloc_msihandle( &si->hdr );
        if( *pHandle )
            ret = ERROR_SUCCESS;
563 564
        else
            ret = ERROR_NOT_ENOUGH_MEMORY;
565 566
        msiobj_release( &si->hdr );
    }
567

568
    msiobj_release( &db->hdr );
569 570 571
    return ret;
}

572
UINT WINAPI MsiGetSummaryInformationA(MSIHANDLE hDatabase,
573
              LPCSTR szDatabase, UINT uiUpdateCount, MSIHANDLE *pHandle)
574
{
575 576
    LPWSTR szwDatabase = NULL;
    UINT ret;
577

578
    TRACE("%d %s %d %p\n", hDatabase, debugstr_a(szDatabase),
579
          uiUpdateCount, pHandle);
580

581 582 583 584 585 586 587 588
    if( szDatabase )
    {
        szwDatabase = strdupAtoW( szDatabase );
        if( !szwDatabase )
            return ERROR_FUNCTION_FAILED;
    }

    ret = MsiGetSummaryInformationW(hDatabase, szwDatabase, uiUpdateCount, pHandle);
589

590
    msi_free( szwDatabase );
591 592

    return ret;
593 594
}

595
UINT WINAPI MsiSummaryInfoGetPropertyCount(MSIHANDLE hSummaryInfo, PUINT pCount)
596
{
597
    MSISUMMARYINFO *si;
598

599
    TRACE("%d %p\n", hSummaryInfo, pCount);
600

601 602
    si = msihandle2msiinfo( hSummaryInfo, MSIHANDLETYPE_SUMMARYINFO );
    if( !si )
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
    {
        MSIHANDLE remote;
        UINT ret;

        if (!(remote = msi_get_remote( hSummaryInfo )))
            return ERROR_INVALID_HANDLE;

        __TRY
        {
            ret = remote_SummaryInfoGetPropertyCount( remote, pCount );
        }
        __EXCEPT(rpc_filter)
        {
            ret = GetExceptionCode();
        }
        __ENDTRY

        return ret;
    }
622

623 624 625
    if( pCount )
        *pCount = get_property_count( si->property );
    msiobj_release( &si->hdr );
626

627 628 629
    return ERROR_SUCCESS;
}

630 631
static UINT get_prop( MSISUMMARYINFO *si, UINT uiProperty, UINT *puiDataType, INT *piValue,
                      FILETIME *pftValue, awstring *str, DWORD *pcchValueBuf)
632 633
{
    PROPVARIANT *prop;
634
    UINT ret = ERROR_SUCCESS;
635 636 637

    prop = &si->property[uiProperty];

638 639 640 641
    if( puiDataType )
        *puiDataType = prop->vt;

    switch( prop->vt )
642
    {
643 644 645 646
    case VT_I2:
        if( piValue )
            *piValue = prop->u.iVal;
        break;
647 648
    case VT_I4:
        if( piValue )
649
            *piValue = prop->u.lVal;
650 651
        break;
    case VT_LPSTR:
652
        if( pcchValueBuf )
653
        {
654 655 656 657
            DWORD len = 0;

            if( str->unicode )
            {
658 659
                len = MultiByteToWideChar( CP_ACP, 0, prop->u.pszVal, -1, NULL, 0 ) - 1;
                MultiByteToWideChar( CP_ACP, 0, prop->u.pszVal, -1, str->str.w, *pcchValueBuf );
660 661 662 663 664 665 666
            }
            else
            {
                len = lstrlenA( prop->u.pszVal );
                if( str->str.a )
                    lstrcpynA(str->str.a, prop->u.pszVal, *pcchValueBuf );
            }
667 668
            if (len >= *pcchValueBuf)
                ret = ERROR_MORE_DATA;
669
            *pcchValueBuf = len;
670 671 672 673
        }
        break;
    case VT_FILETIME:
        if( pftValue )
674
            *pftValue = prop->u.filetime;
675 676 677 678 679 680 681
        break;
    case VT_EMPTY:
        break;
    default:
        FIXME("Unknown property variant type\n");
        break;
    }
682
    return ret;
683 684
}

685 686 687 688 689 690 691 692 693 694 695 696
LPWSTR msi_suminfo_dup_string( MSISUMMARYINFO *si, UINT uiProperty )
{
    PROPVARIANT *prop;

    if ( uiProperty >= MSI_MAX_PROPS )
        return NULL;
    prop = &si->property[uiProperty];
    if( prop->vt != VT_LPSTR )
        return NULL;
    return strdupAtoW( prop->u.pszVal );
}

697 698 699 700 701 702 703 704 705 706 707 708
INT msi_suminfo_get_int32( MSISUMMARYINFO *si, UINT uiProperty )
{
    PROPVARIANT *prop;

    if ( uiProperty >= MSI_MAX_PROPS )
        return -1;
    prop = &si->property[uiProperty];
    if( prop->vt != VT_I4 )
        return -1;
    return prop->u.lVal;
}

709 710 711 712
LPWSTR msi_get_suminfo_product( IStorage *stg )
{
    MSISUMMARYINFO *si;
    LPWSTR prod;
713
    UINT r;
714

715 716
    r = msi_get_suminfo( stg, 0, &si );
    if (r != ERROR_SUCCESS)
717 718 719 720 721 722 723 724 725
    {
        ERR("no summary information!\n");
        return NULL;
    }
    prod = msi_suminfo_dup_string( si, PID_REVNUMBER );
    msiobj_release( &si->hdr );
    return prod;
}

726
UINT WINAPI MsiSummaryInfoGetPropertyA(
727 728
      MSIHANDLE handle, UINT uiProperty, PUINT puiDataType, LPINT piValue,
      FILETIME *pftValue, LPSTR szValueBuf, LPDWORD pcchValueBuf)
729
{
730
    MSISUMMARYINFO *si;
731
    awstring str;
732
    UINT r;
733

734
    TRACE("%u, %u, %p, %p, %p, %p, %p\n", handle, uiProperty, puiDataType,
735 736
          piValue, pftValue, szValueBuf, pcchValueBuf );

737 738 739 740 741 742 743
    if (uiProperty >= MSI_MAX_PROPS)
    {
        if (puiDataType) *puiDataType = VT_EMPTY;
        return ERROR_UNKNOWN_PROPERTY;
    }

    if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
    {
        MSIHANDLE remote;
        WCHAR *buf = NULL;

        if (!(remote = msi_get_remote( handle )))
            return ERROR_INVALID_HANDLE;

        __TRY
        {
            r = remote_SummaryInfoGetProperty( remote, uiProperty, puiDataType, piValue, pftValue, &buf );
        }
        __EXCEPT(rpc_filter)
        {
            r = GetExceptionCode();
        }
        __ENDTRY

761 762
        if (!r && buf)
        {
763
            r = msi_strncpyWtoA( buf, -1, szValueBuf, pcchValueBuf, TRUE );
764
        }
765 766 767 768

        midl_user_free( buf );
        return r;
    }
769

770 771 772
    str.unicode = FALSE;
    str.str.a = szValueBuf;

773 774 775
    r = get_prop( si, uiProperty, puiDataType, piValue, pftValue, &str, pcchValueBuf );
    msiobj_release( &si->hdr );
    return r;
776 777 778
}

UINT WINAPI MsiSummaryInfoGetPropertyW(
779 780
      MSIHANDLE handle, UINT uiProperty, PUINT puiDataType, LPINT piValue,
      FILETIME *pftValue, LPWSTR szValueBuf, LPDWORD pcchValueBuf)
781
{
782
    MSISUMMARYINFO *si;
783
    awstring str;
784
    UINT r;
785

786
    TRACE("%u, %u, %p, %p, %p, %p, %p\n", handle, uiProperty, puiDataType,
787
          piValue, pftValue, szValueBuf, pcchValueBuf );
788

789 790 791 792 793 794 795
    if (uiProperty >= MSI_MAX_PROPS)
    {
        if (puiDataType) *puiDataType = VT_EMPTY;
        return ERROR_UNKNOWN_PROPERTY;
    }

    if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
    {
        MSIHANDLE remote;
        WCHAR *buf = NULL;

        if (!(remote = msi_get_remote( handle )))
            return ERROR_INVALID_HANDLE;

        __TRY
        {
            r = remote_SummaryInfoGetProperty( remote, uiProperty, puiDataType, piValue, pftValue, &buf );
        }
        __EXCEPT(rpc_filter)
        {
            r = GetExceptionCode();
        }
        __ENDTRY

813
        if (!r && buf)
814 815 816 817 818
            r = msi_strncpyW( buf, -1, szValueBuf, pcchValueBuf );

        midl_user_free( buf );
        return r;
    }
819

820 821
    str.unicode = TRUE;
    str.str.w = szValueBuf;
822

823 824 825
    r = get_prop( si, uiProperty, puiDataType, piValue, pftValue, &str, pcchValueBuf );
    msiobj_release( &si->hdr );
    return r;
826 827
}

828
static UINT set_prop( MSISUMMARYINFO *si, UINT uiProperty, UINT type,
829
                      INT iValue, FILETIME *pftValue, awcstring *str )
830 831
{
    PROPVARIANT *prop;
832
    UINT len;
833

834
    TRACE("%p, %u, %u, %d, %p, %p\n", si, uiProperty, type, iValue, pftValue, str );
835

836
    prop = &si->property[uiProperty];
837

838
    if( prop->vt == VT_EMPTY )
839
    {
840
        if( !si->update_count )
841 842
            return ERROR_FUNCTION_FAILED;

843
        si->update_count--;
844
    }
845
    else if( prop->vt != type )
846
        return ERROR_SUCCESS;
847

848 849
    free_prop( prop );
    prop->vt = type;
850
    switch( type )
851 852
    {
    case VT_I4:
853
        prop->u.lVal = iValue;
854
        break;
855 856
    case VT_I2:
        prop->u.iVal = iValue;
857 858
        break;
    case VT_FILETIME:
859
        prop->u.filetime = *pftValue;
860
        break;
861 862 863 864 865
    case VT_LPSTR:
        if( str->unicode )
        {
            len = WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
                                       NULL, 0, NULL, NULL );
866
            prop->u.pszVal = msi_alloc( len );
867 868 869 870 871 872
            WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
                                 prop->u.pszVal, len, NULL, NULL );
        }
        else
        {
            len = lstrlenA( str->str.a ) + 1;
873
            prop->u.pszVal = msi_alloc( len );
874 875
            lstrcpyA( prop->u.pszVal, str->str.a );
        }
876 877 878
        break;
    }

879
    return ERROR_SUCCESS;
880
}
881

882 883
static UINT msi_set_prop( MSISUMMARYINFO *si, UINT uiProperty, UINT uiDataType,
                          INT iValue, FILETIME *pftValue, awcstring *str )
884
{
885
    UINT type = get_type( uiProperty );
886 887 888
    if( type == VT_EMPTY || type != uiDataType )
        return ERROR_DATATYPE_MISMATCH;

889
    if( uiDataType == VT_LPSTR && !str->str.a )
890 891 892 893 894
        return ERROR_INVALID_PARAMETER;

    if( uiDataType == VT_FILETIME && !pftValue )
        return ERROR_INVALID_PARAMETER;

895 896 897 898 899 900 901 902 903 904 905 906 907
    return set_prop( si, uiProperty, type, iValue, pftValue, str );
}

UINT WINAPI MsiSummaryInfoSetPropertyW( MSIHANDLE handle, UINT uiProperty, UINT uiDataType,
                                        INT iValue, FILETIME *pftValue, LPCWSTR szValue )
{
    awcstring str;
    MSISUMMARYINFO *si;
    UINT ret;

    TRACE("%u, %u, %u, %d, %p, %s\n", handle, uiProperty, uiDataType, iValue, pftValue,
          debugstr_w(szValue) );

908
    if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
909 910 911 912 913 914 915 916 917
    {
        MSIHANDLE remote;

        if ((remote = msi_get_remote( handle )))
        {
            WARN("MsiSummaryInfoSetProperty not allowed during a custom action!\n");
            return ERROR_FUNCTION_FAILED;
        }

918
        return ERROR_INVALID_HANDLE;
919
    }
920

921 922
    str.unicode = TRUE;
    str.str.w = szValue;
923

924
    ret = msi_set_prop( si, uiProperty, uiDataType, iValue, pftValue, &str );
925 926
    msiobj_release( &si->hdr );
    return ret;
927 928
}

929 930
UINT WINAPI MsiSummaryInfoSetPropertyA( MSIHANDLE handle, UINT uiProperty, UINT uiDataType,
                                        INT iValue, FILETIME *pftValue, LPCSTR szValue )
931
{
932
    awcstring str;
933
    MSISUMMARYINFO *si;
934
    UINT ret;
935

936 937
    TRACE("%u, %u, %u, %d, %p, %s\n", handle, uiProperty, uiDataType, iValue, pftValue,
          debugstr_a(szValue) );
938

939
    if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
940 941 942 943 944 945 946 947 948
    {
        MSIHANDLE remote;

        if ((remote = msi_get_remote( handle )))
        {
            WARN("MsiSummaryInfoSetProperty not allowed during a custom action!\n");
            return ERROR_FUNCTION_FAILED;
        }

949
        return ERROR_INVALID_HANDLE;
950
    }
951

952 953
    str.unicode = FALSE;
    str.str.a = szValue;
954

955
    ret = msi_set_prop( si, uiProperty, uiDataType, iValue, pftValue, &str );
956 957
    msiobj_release( &si->hdr );
    return ret;
958 959
}

960
static UINT suminfo_persist( MSISUMMARYINFO *si )
961
{
962
    UINT ret = ERROR_FUNCTION_FAILED;
963 964 965 966 967
    IStream *stm = NULL;
    DWORD grfMode;
    HRESULT r;

    grfMode = STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
968
    r = IStorage_CreateStream( si->storage, L"\5SummaryInformation", grfMode, 0, 0, &stm );
969 970 971 972 973
    if( SUCCEEDED(r) )
    {
        ret = save_summary_info( si, stm );
        IStream_Release( stm );
    }
974 975 976 977 978 979 980 981 982 983 984 985 986
    return ret;
}

static void parse_filetime( LPCWSTR str, FILETIME *ft )
{
    SYSTEMTIME lt, utc;
    const WCHAR *p = str;
    WCHAR *end;

    memset( &lt, 0, sizeof(lt) );

    /* YYYY/MM/DD hh:mm:ss */

987
    while (iswspace( *p )) p++;
988

989
    lt.wYear = wcstol( p, &end, 10 );
990 991 992
    if (*end != '/') return;
    p = end + 1;

993
    lt.wMonth = wcstol( p, &end, 10 );
994 995 996
    if (*end != '/') return;
    p = end + 1;

997
    lt.wDay = wcstol( p, &end, 10 );
998 999 1000
    if (*end != ' ') return;
    p = end + 1;

1001
    while (iswspace( *p )) p++;
1002

1003
    lt.wHour = wcstol( p, &end, 10 );
1004 1005 1006
    if (*end != ':') return;
    p = end + 1;

1007
    lt.wMinute = wcstol( p, &end, 10 );
1008 1009 1010
    if (*end != ':') return;
    p = end + 1;

1011
    lt.wSecond = wcstol( p, &end, 10 );
1012 1013 1014 1015 1016 1017 1018 1019

    TzSpecificLocalTimeToSystemTime( NULL, &lt, &utc );
    SystemTimeToFileTime( &utc, ft );
}

static UINT parse_prop( LPCWSTR prop, LPCWSTR value, UINT *pid, INT *int_value,
                        FILETIME *ft_value, awcstring *str_value )
{
1020
    *pid = wcstol( prop, NULL, 10 );
1021 1022 1023 1024 1025 1026 1027
    switch (*pid)
    {
    case PID_CODEPAGE:
    case PID_WORDCOUNT:
    case PID_CHARCOUNT:
    case PID_SECURITY:
    case PID_PAGECOUNT:
1028
        *int_value = wcstol( value, NULL, 10 );
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
        break;

    case PID_LASTPRINTED:
    case PID_CREATE_DTM:
    case PID_LASTSAVE_DTM:
        parse_filetime( value, ft_value );
        break;

    case PID_SUBJECT:
    case PID_AUTHOR:
    case PID_KEYWORDS:
    case PID_COMMENTS:
    case PID_TEMPLATE:
    case PID_LASTAUTHOR:
    case PID_REVNUMBER:
    case PID_APPNAME:
    case PID_TITLE:
        str_value->str.w = value;
        str_value->unicode = TRUE;
        break;

    default:
        WARN("unhandled prop id %u\n", *pid);
        return ERROR_FUNCTION_FAILED;
    }

    return ERROR_SUCCESS;
}

UINT msi_add_suminfo( MSIDATABASE *db, LPWSTR **records, int num_records, int num_columns )
{
1060
    UINT r;
1061
    int i, j;
1062 1063
    MSISUMMARYINFO *si;

1064 1065
    r = msi_get_suminfo( db->storage, num_records * (num_columns / 2), &si );
    if (r != ERROR_SUCCESS)
1066
    {
1067 1068 1069
        if (!(si = create_suminfo( db->storage, num_records * (num_columns / 2) )))
            return ERROR_OUTOFMEMORY;
        r = ERROR_SUCCESS;
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
    }

    for (i = 0; i < num_records; i++)
    {
        for (j = 0; j < num_columns; j += 2)
        {
            UINT pid;
            INT int_value = 0;
            FILETIME ft_value;
            awcstring str_value;

            r = parse_prop( records[i][j], records[i][j + 1], &pid, &int_value, &ft_value, &str_value );
            if (r != ERROR_SUCCESS)
                goto end;

            r = set_prop( si, pid, get_type(pid), int_value, &ft_value, &str_value );
            if (r != ERROR_SUCCESS)
                goto end;
        }
    }

end:
    if (r == ERROR_SUCCESS)
        r = suminfo_persist( si );

1095
    msiobj_release( &si->hdr );
1096 1097 1098
    return r;
}

1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
static UINT save_prop( MSISUMMARYINFO *si, HANDLE handle, UINT row )
{
    static const char fmt_systemtime[] = "%04u/%02u/%02u %02u:%02u:%02u";
    char data[36]; /* largest string: YYYY/MM/DD hh:mm:ss */
    static const char fmt_begin[] = "%u\t";
    static const char data_end[] = "\r\n";
    static const char fmt_int[] = "%u";
    UINT r, data_type, len;
    SYSTEMTIME system_time;
    FILETIME file_time;
    INT int_value;
    awstring str;
    DWORD sz;

    str.unicode = FALSE;
    str.str.a = NULL;
    len = 0;
    r = get_prop( si, row, &data_type, &int_value, &file_time, &str, &len );
    if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
        return r;
    if (data_type == VT_EMPTY)
        return ERROR_SUCCESS; /* property not set */
    sz = sprintf( data, fmt_begin, row );
    if (!WriteFile( handle, data, sz, &sz, NULL ))
        return ERROR_WRITE_FAULT;

    switch( data_type )
    {
    case VT_I2:
    case VT_I4:
        sz = sprintf( data, fmt_int, int_value );
        if (!WriteFile( handle, data, sz, &sz, NULL ))
            return ERROR_WRITE_FAULT;
        break;
    case VT_LPSTR:
        len++;
        if (!(str.str.a = msi_alloc( len )))
            return ERROR_OUTOFMEMORY;
        r = get_prop( si, row, NULL, NULL, NULL, &str, &len );
        if (r != ERROR_SUCCESS)
        {
            msi_free( str.str.a );
            return r;
        }
        sz = len;
        if (!WriteFile( handle, str.str.a, sz, &sz, NULL ))
        {
            msi_free( str.str.a );
            return ERROR_WRITE_FAULT;
        }
        msi_free( str.str.a );
        break;
    case VT_FILETIME:
        if (!FileTimeToSystemTime( &file_time, &system_time ))
            return ERROR_FUNCTION_FAILED;
        sz = sprintf( data, fmt_systemtime, system_time.wYear, system_time.wMonth,
                      system_time.wDay, system_time.wHour, system_time.wMinute,
                      system_time.wSecond );
        if (!WriteFile( handle, data, sz, &sz, NULL ))
            return ERROR_WRITE_FAULT;
        break;
    case VT_EMPTY:
        /* cannot reach here, property not set */
        break;
    default:
        FIXME( "Unknown property variant type\n" );
        return ERROR_FUNCTION_FAILED;
    }

    sz = ARRAY_SIZE(data_end) - 1;
    if (!WriteFile( handle, data_end, sz, &sz, NULL ))
        return ERROR_WRITE_FAULT;

    return ERROR_SUCCESS;
}

UINT msi_export_suminfo( MSIDATABASE *db, HANDLE handle )
{
    UINT i, r, num_rows;
    MSISUMMARYINFO *si;

    r = msi_get_suminfo( db->storage, 0, &si );
    if (r != ERROR_SUCCESS)
        r = msi_get_db_suminfo( db, 0, &si );
    if (r != ERROR_SUCCESS)
        return r;

    num_rows = get_property_count( si->property );
    if (!num_rows)
    {
        msiobj_release( &si->hdr );
        return ERROR_FUNCTION_FAILED;
    }

    for (i = 0; i < num_rows; i++)
    {
        r = save_prop( si, handle, i );
        if (r != ERROR_SUCCESS)
        {
            msiobj_release( &si->hdr );
            return r;
        }
    }

    msiobj_release( &si->hdr );
    return ERROR_SUCCESS;
}

1207 1208 1209 1210 1211 1212
UINT WINAPI MsiSummaryInfoPersist( MSIHANDLE handle )
{
    MSISUMMARYINFO *si;
    UINT ret;

    TRACE("%d\n", handle );
1213

1214 1215 1216 1217 1218 1219 1220
    si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
    if( !si )
        return ERROR_INVALID_HANDLE;

    ret = suminfo_persist( si );

    msiobj_release( &si->hdr );
1221
    return ret;
1222
}
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243

UINT WINAPI MsiCreateTransformSummaryInfoA( MSIHANDLE db, MSIHANDLE db_ref, LPCSTR transform, int error, int validation )
{
    UINT r;
    WCHAR *transformW = NULL;

    TRACE("%u, %u, %s, %d, %d\n", db, db_ref, debugstr_a(transform), error, validation);

    if (transform && !(transformW = strdupAtoW( transform )))
        return ERROR_OUTOFMEMORY;

    r = MsiCreateTransformSummaryInfoW( db, db_ref, transformW, error, validation );
    msi_free( transformW );
    return r;
}

UINT WINAPI MsiCreateTransformSummaryInfoW( MSIHANDLE db, MSIHANDLE db_ref, LPCWSTR transform, int error, int validation )
{
    FIXME("%u, %u, %s, %d, %d\n", db, db_ref, debugstr_w(transform), error, validation);
    return ERROR_FUNCTION_FAILED;
}
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 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

UINT msi_load_suminfo_properties( MSIPACKAGE *package )
{
    MSISUMMARYINFO *si;
    WCHAR *package_code;
    UINT r, len;
    awstring str;
    INT count;

    r = msi_get_suminfo( package->db->storage, 0, &si );
    if (r != ERROR_SUCCESS)
    {
        r = msi_get_db_suminfo( package->db, 0, &si );
        if (r != ERROR_SUCCESS)
        {
            ERR("Unable to open summary information stream %u\n", r);
            return r;
        }
    }

    str.unicode = TRUE;
    str.str.w = NULL;
    len = 0;
    r = get_prop( si, PID_REVNUMBER, NULL, NULL, NULL, &str, &len );
    if (r != ERROR_MORE_DATA)
    {
        WARN("Unable to query revision number %u\n", r);
        msiobj_release( &si->hdr );
        return ERROR_FUNCTION_FAILED;
    }

    len++;
    if (!(package_code = msi_alloc( len * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
    str.str.w = package_code;

    r = get_prop( si, PID_REVNUMBER, NULL, NULL, NULL, &str, &len );
    if (r != ERROR_SUCCESS)
    {
        msi_free( package_code );
        msiobj_release( &si->hdr );
        return r;
    }

1287
    r = msi_set_property( package->db, L"PackageCode", package_code, len );
1288 1289 1290 1291 1292 1293 1294 1295 1296
    msi_free( package_code );

    count = 0;
    get_prop( si, PID_WORDCOUNT, NULL, &count, NULL, NULL, NULL );
    package->WordCount = count;

    msiobj_release( &si->hdr );
    return r;
}
1297 1298 1299 1300 1301

UINT __cdecl s_remote_SummaryInfoGetPropertyCount( MSIHANDLE suminfo, UINT *count )
{
    return MsiSummaryInfoGetPropertyCount( suminfo, count );
}
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319

UINT __cdecl s_remote_SummaryInfoGetProperty( MSIHANDLE suminfo, UINT property, UINT *type,
                                              INT *value, FILETIME *ft, LPWSTR *buf )
{
    WCHAR empty[1];
    DWORD size = 0;
    UINT r;

    r = MsiSummaryInfoGetPropertyW( suminfo, property, type, value, ft, empty, &size );
    if (r == ERROR_MORE_DATA)
    {
        size++;
        *buf = midl_user_allocate( size * sizeof(WCHAR) );
        if (!*buf) return ERROR_OUTOFMEMORY;
        r = MsiSummaryInfoGetPropertyW( suminfo, property, type, value, ft, *buf, &size );
    }
    return r;
}