record.c 24.7 KB
Newer Older
1 2 3
/*
 * Implementation of the Microsoft Installer (msi.dll)
 *
4
 * Copyright 2002-2004 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
#define COBJMACROS

25 26 27 28 29
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "winerror.h"
#include "wine/debug.h"
30
#include "wine/unicode.h"
31 32 33 34 35
#include "msi.h"
#include "msiquery.h"
#include "msipriv.h"
#include "objidl.h"
#include "winnls.h"
36
#include "ole2.h"
37

38 39 40
#include "winreg.h"
#include "shlwapi.h"

41 42
#include "query.h"

43
WINE_DEFAULT_DEBUG_CHANNEL(msidb);
44 45 46 47 48

#define MSIFIELD_NULL   0
#define MSIFIELD_INT    1
#define MSIFIELD_WSTR   3
#define MSIFIELD_STREAM 4
49
#define MSIFIELD_INTPTR 5
50

51
static void MSI_FreeField( MSIFIELD *field )
52 53 54 55 56
{
    switch( field->type )
    {
    case MSIFIELD_NULL:
    case MSIFIELD_INT:
57
    case MSIFIELD_INTPTR:
58 59
        break;
    case MSIFIELD_WSTR:
60
        msi_free( field->u.szwVal);
61 62 63 64 65 66 67 68 69
        break;
    case MSIFIELD_STREAM:
        IStream_Release( field->u.stream );
        break;
    default:
        ERR("Invalid field type %d\n", field->type);
    }
}

70
void MSI_CloseRecord( MSIOBJECTHDR *arg )
71 72 73 74
{
    MSIRECORD *rec = (MSIRECORD *) arg;
    UINT i;

75
    for( i=0; i<=rec->count; i++ )
76 77 78
        MSI_FreeField( &rec->fields[i] );
}

79
MSIRECORD *MSI_CreateRecord( UINT cParams )
80 81
{
    MSIRECORD *rec;
82
    UINT len;
83 84 85

    TRACE("%d\n", cParams);

86 87 88
    if( cParams>65535 )
        return NULL;

89 90 91
    len = sizeof (MSIRECORD) + sizeof (MSIFIELD)*cParams;
    rec = alloc_msiobject( MSIHANDLETYPE_RECORD, len, MSI_CloseRecord );
    if( rec )
92
        rec->count = cParams;
93 94 95
    return rec;
}

96
MSIHANDLE WINAPI MsiCreateRecord( UINT cParams )
97 98 99 100 101 102 103 104
{
    MSIRECORD *rec;
    MSIHANDLE ret = 0;

    TRACE("%d\n", cParams);

    rec = MSI_CreateRecord( cParams );
    if( rec )
105
    {
106
        ret = alloc_msihandle( &rec->hdr );
107 108
        msiobj_release( &rec->hdr );
    }
109 110
    return ret;
}
111

112
UINT MSI_RecordGetFieldCount( const MSIRECORD *rec )
113 114
{
    return rec->count;
115 116
}

117
UINT WINAPI MsiRecordGetFieldCount( MSIHANDLE handle )
118 119
{
    MSIRECORD *rec;
120
    UINT ret;
121

122
    TRACE("%d\n", handle );
123 124 125

    rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
    if( !rec )
126
        return -1;
127

128
    msiobj_lock( &rec->hdr );
129
    ret = MSI_RecordGetFieldCount( rec );
130
    msiobj_unlock( &rec->hdr );
131 132 133
    msiobj_release( &rec->hdr );

    return ret;
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
}

static BOOL string2intW( LPCWSTR str, int *out )
{
    int x = 0;
    LPCWSTR p = str;

    if( *p == '-' ) /* skip the minus sign */
        p++;
    while ( *p )
    {
        if( (*p < '0') || (*p > '9') )
            return FALSE;
        x *= 10;
        x += (*p - '0');
        p++;
    }

    if( str[0] == '-' ) /* check if it's negative */
        x = -x;
    *out = x; 

    return TRUE;
}

159 160
UINT MSI_RecordCopyField( MSIRECORD *in_rec, UINT in_n,
                          MSIRECORD *out_rec, UINT out_n )
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
{
    UINT r = ERROR_SUCCESS;

    msiobj_lock( &in_rec->hdr );

    if ( in_n > in_rec->count || out_n > out_rec->count )
        r = ERROR_FUNCTION_FAILED;
    else if ( in_rec != out_rec || in_n != out_n )
    {
        LPWSTR str;
        MSIFIELD *in, *out;

        in = &in_rec->fields[in_n];
        out = &out_rec->fields[out_n];

        switch ( in->type )
        {
        case MSIFIELD_NULL:
            break;
        case MSIFIELD_INT:
            out->u.iVal = in->u.iVal;
            break;
183 184 185
        case MSIFIELD_INTPTR:
            out->u.pVal = in->u.pVal;
            break;
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
        case MSIFIELD_WSTR:
            str = strdupW( in->u.szwVal );
            if ( !str )
                r = ERROR_OUTOFMEMORY;
            else
                out->u.szwVal = str;
            break;
        case MSIFIELD_STREAM:
            IStream_AddRef( in->u.stream );
            out->u.stream = in->u.stream;
            break;
        default:
            ERR("invalid field type %d\n", in->type);
        }
        if (r == ERROR_SUCCESS)
            out->type = in->type;
    }

    msiobj_unlock( &in_rec->hdr );

    return r;
}

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
INT_PTR MSI_RecordGetIntPtr( MSIRECORD *rec, UINT iField )
{
    int ret;

    TRACE( "%p %d\n", rec, iField );

    if( iField > rec->count )
        return MININT_PTR;

    switch( rec->fields[iField].type )
    {
    case MSIFIELD_INT:
        return rec->fields[iField].u.iVal;
    case MSIFIELD_INTPTR:
        return rec->fields[iField].u.pVal;
    case MSIFIELD_WSTR:
        if( string2intW( rec->fields[iField].u.szwVal, &ret ) )
            return ret;
        return MININT_PTR;
    default:
        break;
    }

    return MININT_PTR;
}

235
int MSI_RecordGetInteger( MSIRECORD *rec, UINT iField)
236 237 238
{
    int ret = 0;

239
    TRACE("%p %d\n", rec, iField );
240 241 242 243 244 245 246 247

    if( iField > rec->count )
        return MSI_NULL_INTEGER;

    switch( rec->fields[iField].type )
    {
    case MSIFIELD_INT:
        return rec->fields[iField].u.iVal;
248 249
    case MSIFIELD_INTPTR:
        return rec->fields[iField].u.pVal;
250 251 252 253 254 255 256 257 258 259 260
    case MSIFIELD_WSTR:
        if( string2intW( rec->fields[iField].u.szwVal, &ret ) )
            return ret;
        return MSI_NULL_INTEGER;
    default:
        break;
    }

    return MSI_NULL_INTEGER;
}

261
int WINAPI MsiRecordGetInteger( MSIHANDLE handle, UINT iField)
262 263 264 265
{
    MSIRECORD *rec;
    UINT ret;

266
    TRACE("%d %d\n", handle, iField );
267 268 269 270 271

    rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
    if( !rec )
        return MSI_NULL_INTEGER;

272
    msiobj_lock( &rec->hdr );
273
    ret = MSI_RecordGetInteger( rec, iField );
274
    msiobj_unlock( &rec->hdr );
275 276 277 278 279
    msiobj_release( &rec->hdr );

    return ret;
}

280 281 282 283 284
UINT WINAPI MsiRecordClearData( MSIHANDLE handle )
{
    MSIRECORD *rec;
    UINT i;

285
    TRACE("%d\n", handle );
286 287 288 289 290

    rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
    if( !rec )
        return ERROR_INVALID_HANDLE;

291
    msiobj_lock( &rec->hdr );
292 293 294 295 296 297
    for( i=0; i<=rec->count; i++)
    {
        MSI_FreeField( &rec->fields[i] );
        rec->fields[i].type = MSIFIELD_NULL;
        rec->fields[i].u.iVal = 0;
    }
298
    msiobj_unlock( &rec->hdr );
299
    msiobj_release( &rec->hdr );
300 301 302 303

    return ERROR_SUCCESS;
}

304 305 306 307 308 309 310 311 312 313 314 315 316 317
UINT MSI_RecordSetIntPtr( MSIRECORD *rec, UINT iField, INT_PTR pVal )
{
    TRACE("%p %u %ld\n", rec, iField, pVal);

    if( iField > rec->count )
        return ERROR_INVALID_PARAMETER;

    MSI_FreeField( &rec->fields[iField] );
    rec->fields[iField].type = MSIFIELD_INTPTR;
    rec->fields[iField].u.pVal = pVal;

    return ERROR_SUCCESS;
}

318
UINT MSI_RecordSetInteger( MSIRECORD *rec, UINT iField, int iVal )
319
{
320
    TRACE("%p %u %d\n", rec, iField, iVal);
321

322 323
    if( iField > rec->count )
        return ERROR_INVALID_PARAMETER;
324

325 326 327
    MSI_FreeField( &rec->fields[iField] );
    rec->fields[iField].type = MSIFIELD_INT;
    rec->fields[iField].u.iVal = iVal;
328 329 330 331

    return ERROR_SUCCESS;
}

332
UINT WINAPI MsiRecordSetInteger( MSIHANDLE handle, UINT iField, int iVal )
333 334
{
    MSIRECORD *rec;
335
    UINT ret;
336

337
    TRACE("%d %u %d\n", handle, iField, iVal);
338 339 340 341 342

    rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
    if( !rec )
        return ERROR_INVALID_HANDLE;

343
    msiobj_lock( &rec->hdr );
344
    ret = MSI_RecordSetInteger( rec, iField, iVal );
345
    msiobj_unlock( &rec->hdr );
346 347 348 349
    msiobj_release( &rec->hdr );
    return ret;
}

350
BOOL MSI_RecordIsNull( MSIRECORD *rec, UINT iField )
351 352 353 354 355
{
    BOOL r = TRUE;

    TRACE("%p %d\n", rec, iField );

356 357
    r = ( iField > rec->count ) ||
        ( rec->fields[iField].type == MSIFIELD_NULL );
358

359
    return r;
360 361
}

362
BOOL WINAPI MsiRecordIsNull( MSIHANDLE handle, UINT iField )
363 364
{
    MSIRECORD *rec;
365
    UINT ret;
366

367
    TRACE("%d %d\n", handle, iField );
368 369 370

    rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
    if( !rec )
371
        return 0;
372
    msiobj_lock( &rec->hdr );
373
    ret = MSI_RecordIsNull( rec, iField );
374
    msiobj_unlock( &rec->hdr );
375 376 377 378 379
    msiobj_release( &rec->hdr );
    return ret;

}

380 381
UINT MSI_RecordGetStringA(MSIRECORD *rec, UINT iField,
               LPSTR szValue, LPDWORD pcchValue)
382 383 384 385 386
{
    UINT len=0, ret;
    CHAR buffer[16];

    TRACE("%p %d %p %p\n", rec, iField, szValue, pcchValue);
387 388

    if( iField > rec->count )
389 390 391 392 393 394 395
    {
        if ( szValue && *pcchValue > 0 )
            szValue[0] = 0;

        *pcchValue = 0;
        return ERROR_SUCCESS;
    }
396 397 398 399 400 401 402

    ret = ERROR_SUCCESS;
    switch( rec->fields[iField].type )
    {
    case MSIFIELD_INT:
        wsprintfA(buffer, "%d", rec->fields[iField].u.iVal);
        len = lstrlenA( buffer );
403 404
        if (szValue)
            lstrcpynA(szValue, buffer, *pcchValue);
405 406 407 408
        break;
    case MSIFIELD_WSTR:
        len = WideCharToMultiByte( CP_ACP, 0, rec->fields[iField].u.szwVal, -1,
                             NULL, 0 , NULL, NULL);
409 410 411
        if (szValue)
            WideCharToMultiByte( CP_ACP, 0, rec->fields[iField].u.szwVal, -1,
                                 szValue, *pcchValue, NULL, NULL);
412
        if( szValue && *pcchValue && len>*pcchValue )
413 414 415
            szValue[*pcchValue-1] = 0;
        if( len )
            len--;
416
        break;
417
    case MSIFIELD_NULL:
418
        if( szValue && *pcchValue > 0 )
419
            szValue[0] = 0;
420
        break;
421 422 423 424 425
    default:
        ret = ERROR_INVALID_PARAMETER;
        break;
    }

426
    if( szValue && *pcchValue <= len )
427 428 429 430 431 432
        ret = ERROR_MORE_DATA;
    *pcchValue = len;

    return ret;
}

433 434
UINT WINAPI MsiRecordGetStringA(MSIHANDLE handle, UINT iField,
               LPSTR szValue, LPDWORD pcchValue)
435 436
{
    MSIRECORD *rec;
437 438
    UINT ret;

439
    TRACE("%d %d %p %p\n", handle, iField, szValue, pcchValue);
440 441 442

    rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
    if( !rec )
443
        return ERROR_INVALID_HANDLE;
444
    msiobj_lock( &rec->hdr );
445
    ret = MSI_RecordGetStringA( rec, iField, szValue, pcchValue);
446
    msiobj_unlock( &rec->hdr );
447 448 449
    msiobj_release( &rec->hdr );
    return ret;
}
450

451
const WCHAR *MSI_RecordGetString( const MSIRECORD *rec, UINT iField )
452
{
453 454 455
    if( iField > rec->count )
        return NULL;

456 457 458
    if( rec->fields[iField].type != MSIFIELD_WSTR )
        return NULL;

459 460 461
    return rec->fields[iField].u.szwVal;
}

462 463
UINT MSI_RecordGetStringW(MSIRECORD *rec, UINT iField,
               LPWSTR szValue, LPDWORD pcchValue)
464 465 466
{
    UINT len=0, ret;
    WCHAR buffer[16];
467
    static const WCHAR szFormat[] = { '%','d',0 };
468

469
    TRACE("%p %d %p %p\n", rec, iField, szValue, pcchValue);
470 471

    if( iField > rec->count )
472 473 474 475 476 477 478
    {
        if ( szValue && *pcchValue > 0 )
            szValue[0] = 0;

        *pcchValue = 0;
        return ERROR_SUCCESS;
    }
479 480 481 482 483 484 485

    ret = ERROR_SUCCESS;
    switch( rec->fields[iField].type )
    {
    case MSIFIELD_INT:
        wsprintfW(buffer, szFormat, rec->fields[iField].u.iVal);
        len = lstrlenW( buffer );
486 487
        if (szValue)
            lstrcpynW(szValue, buffer, *pcchValue);
488 489 490
        break;
    case MSIFIELD_WSTR:
        len = lstrlenW( rec->fields[iField].u.szwVal );
491 492
        if (szValue)
            lstrcpynW(szValue, rec->fields[iField].u.szwVal, *pcchValue);
493
        break;
494
    case MSIFIELD_NULL:
495
        if( szValue && *pcchValue > 0 )
496
            szValue[0] = 0;
497
        break;
498 499 500 501
    default:
        break;
    }

502
    if( szValue && *pcchValue <= len )
503 504 505 506 507 508
        ret = ERROR_MORE_DATA;
    *pcchValue = len;

    return ret;
}

509 510
UINT WINAPI MsiRecordGetStringW(MSIHANDLE handle, UINT iField,
               LPWSTR szValue, LPDWORD pcchValue)
511 512 513 514
{
    MSIRECORD *rec;
    UINT ret;

515
    TRACE("%d %d %p %p\n", handle, iField, szValue, pcchValue);
516 517 518 519 520

    rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
    if( !rec )
        return ERROR_INVALID_HANDLE;

521
    msiobj_lock( &rec->hdr );
522
    ret = MSI_RecordGetStringW( rec, iField, szValue, pcchValue );
523
    msiobj_unlock( &rec->hdr );
524 525 526 527
    msiobj_release( &rec->hdr );
    return ret;
}

528 529 530 531 532 533 534 535 536 537 538
static UINT msi_get_stream_size( IStream *stm )
{
    STATSTG stat;
    HRESULT r;

    r = IStream_Stat( stm, &stat, STATFLAG_NONAME );
    if( FAILED(r) )
        return 0;
    return stat.cbSize.QuadPart;
}

539
static UINT MSI_RecordDataSize(MSIRECORD *rec, UINT iField)
540
{
541 542 543 544 545 546 547 548 549 550 551 552 553
    TRACE("%p %d\n", rec, iField);

    if( iField > rec->count )
        return 0;

    switch( rec->fields[iField].type )
    {
    case MSIFIELD_INT:
        return sizeof (INT);
    case MSIFIELD_WSTR:
        return lstrlenW( rec->fields[iField].u.szwVal );
    case MSIFIELD_NULL:
        break;
554 555
    case MSIFIELD_STREAM:
        return msi_get_stream_size( rec->fields[iField].u.stream );
556
    }
557 558 559
    return 0;
}

560
UINT WINAPI MsiRecordDataSize(MSIHANDLE handle, UINT iField)
561 562 563 564
{
    MSIRECORD *rec;
    UINT ret;

565
    TRACE("%d %d\n", handle, iField);
566 567 568 569 570 571 572 573 574 575 576

    rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
    if( !rec )
        return 0;
    msiobj_lock( &rec->hdr );
    ret = MSI_RecordDataSize( rec, iField);
    msiobj_unlock( &rec->hdr );
    msiobj_release( &rec->hdr );
    return ret;
}

577
static UINT MSI_RecordSetStringA( MSIRECORD *rec, UINT iField, LPCSTR szValue )
578
{
579
    LPWSTR str;
580

581
    TRACE("%p %d %s\n", rec, iField, debugstr_a(szValue));
582 583 584 585 586

    if( iField > rec->count )
        return ERROR_INVALID_FIELD;

    MSI_FreeField( &rec->fields[iField] );
587
    if( szValue && szValue[0] )
588
    {
589
        str = strdupAtoW( szValue );
590 591 592 593 594 595 596 597
        rec->fields[iField].type = MSIFIELD_WSTR;
        rec->fields[iField].u.szwVal = str;
    }
    else
    {
        rec->fields[iField].type = MSIFIELD_NULL;
        rec->fields[iField].u.szwVal = NULL;
    }
598 599 600 601

    return 0;
}

602
UINT WINAPI MsiRecordSetStringA( MSIHANDLE handle, UINT iField, LPCSTR szValue )
603 604
{
    MSIRECORD *rec;
605
    UINT ret;
606

607
    TRACE("%d %d %s\n", handle, iField, debugstr_a(szValue));
608 609 610 611

    rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
    if( !rec )
        return ERROR_INVALID_HANDLE;
612
    msiobj_lock( &rec->hdr );
613
    ret = MSI_RecordSetStringA( rec, iField, szValue );
614
    msiobj_unlock( &rec->hdr );
615 616 617 618
    msiobj_release( &rec->hdr );
    return ret;
}

619
UINT MSI_RecordSetStringW( MSIRECORD *rec, UINT iField, LPCWSTR szValue )
620 621 622 623
{
    LPWSTR str;

    TRACE("%p %d %s\n", rec, iField, debugstr_w(szValue));
624 625 626 627 628

    if( iField > rec->count )
        return ERROR_INVALID_FIELD;

    MSI_FreeField( &rec->fields[iField] );
629

630
    if( szValue && szValue[0] )
631
    {
632
        str = strdupW( szValue );
633 634 635 636 637 638 639 640
        rec->fields[iField].type = MSIFIELD_WSTR;
        rec->fields[iField].u.szwVal = str;
    }
    else
    {
        rec->fields[iField].type = MSIFIELD_NULL;
        rec->fields[iField].u.szwVal = NULL;
    }
641 642 643 644

    return 0;
}

645
UINT WINAPI MsiRecordSetStringW( MSIHANDLE handle, UINT iField, LPCWSTR szValue )
646 647 648 649
{
    MSIRECORD *rec;
    UINT ret;

650
    TRACE("%d %d %s\n", handle, iField, debugstr_w(szValue));
651 652 653 654 655

    rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
    if( !rec )
        return ERROR_INVALID_HANDLE;

656
    msiobj_lock( &rec->hdr );
657
    ret = MSI_RecordSetStringW( rec, iField, szValue );
658
    msiobj_unlock( &rec->hdr );
659 660 661 662
    msiobj_release( &rec->hdr );
    return ret;
}

663
/* read the data in a file into an IStream */
664
static UINT RECORD_StreamFromFile(LPCWSTR szFile, IStream **pstm)
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
{
    DWORD sz, szHighWord = 0, read;
    HANDLE handle;
    HGLOBAL hGlob = 0;
    HRESULT hr;
    ULARGE_INTEGER ulSize;

    TRACE("reading %s\n", debugstr_w(szFile));

    /* read the file into memory */
    handle = CreateFileW(szFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
    if( handle == INVALID_HANDLE_VALUE )
        return GetLastError();
    sz = GetFileSize(handle, &szHighWord);
    if( sz != INVALID_FILE_SIZE && szHighWord == 0 )
    {
        hGlob = GlobalAlloc(GMEM_FIXED, sz);
        if( hGlob )
        {
            BOOL r = ReadFile(handle, hGlob, sz, &read, NULL);
            if( !r )
            {
                GlobalFree(hGlob);
                hGlob = 0;
            }
        }
    }
    CloseHandle(handle);
    if( !hGlob )
        return ERROR_FUNCTION_FAILED;

    /* make a stream out of it, and set the correct file size */
    hr = CreateStreamOnHGlobal(hGlob, TRUE, pstm);
    if( FAILED( hr ) )
    {
        GlobalFree(hGlob);
        return ERROR_FUNCTION_FAILED;
    }

    /* set the correct size - CreateStreamOnHGlobal screws it up */
    ulSize.QuadPart = sz;
    IStream_SetSize(*pstm, ulSize);

708
    TRACE("read %s, %d bytes into IStream %p\n", debugstr_w(szFile), sz, *pstm);
709 710 711 712

    return ERROR_SUCCESS;
}

713
UINT MSI_RecordSetStream(MSIRECORD *rec, UINT iField, IStream *stream)
714 715 716 717 718 719 720 721 722 723 724
{
    if ( (iField == 0) || (iField > rec->count) )
        return ERROR_INVALID_PARAMETER;

    MSI_FreeField( &rec->fields[iField] );
    rec->fields[iField].type = MSIFIELD_STREAM;
    rec->fields[iField].u.stream = stream;

    return ERROR_SUCCESS;
}

725
UINT MSI_RecordSetStreamFromFileW(MSIRECORD *rec, UINT iField, LPCWSTR szFilename)
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
{
    IStream *stm = NULL;
    HRESULT r;

    if( (iField == 0) || (iField > rec->count) )
        return ERROR_INVALID_PARAMETER;

    /* no filename means we should seek back to the start of the stream */
    if( !szFilename )
    {
        LARGE_INTEGER ofs;
        ULARGE_INTEGER cur;

        if( rec->fields[iField].type != MSIFIELD_STREAM )
            return ERROR_INVALID_FIELD;

        stm = rec->fields[iField].u.stream;
        if( !stm )
            return ERROR_INVALID_FIELD;

        ofs.QuadPart = 0;
        r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
        if( FAILED( r ) )
            return ERROR_FUNCTION_FAILED;
    }
    else
    {
        /* read the file into a stream and save the stream in the record */
        r = RECORD_StreamFromFile(szFilename, &stm);
        if( r != ERROR_SUCCESS )
            return r;

        /* if all's good, store it in the record */
759
        MSI_RecordSetStream(rec, iField, stm);
760 761 762 763 764
    }

    return ERROR_SUCCESS;
}

765
UINT WINAPI MsiRecordSetStreamA(MSIHANDLE hRecord, UINT iField, LPCSTR szFilename)
766
{
767
    LPWSTR wstr = NULL;
768
    UINT ret;
769

770
    TRACE("%d %d %s\n", hRecord, iField, debugstr_a(szFilename));
771 772 773

    if( szFilename )
    {
774 775 776
        wstr = strdupAtoW( szFilename );
        if( !wstr )
             return ERROR_OUTOFMEMORY;
777 778
    }
    ret = MsiRecordSetStreamW(hRecord, iField, wstr);
779
    msi_free(wstr);
780 781

    return ret;
782 783
}

784
UINT WINAPI MsiRecordSetStreamW(MSIHANDLE handle, UINT iField, LPCWSTR szFilename)
785
{
786 787 788
    MSIRECORD *rec;
    UINT ret;

789
    TRACE("%d %d %s\n", handle, iField, debugstr_w(szFilename));
790 791 792 793 794 795

    rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
    if( !rec )
        return ERROR_INVALID_HANDLE;

    msiobj_lock( &rec->hdr );
796
    ret = MSI_RecordSetStreamFromFileW( rec, iField, szFilename );
797 798 799
    msiobj_unlock( &rec->hdr );
    msiobj_release( &rec->hdr );
    return ret;
800 801
}

802
UINT MSI_RecordReadStream(MSIRECORD *rec, UINT iField, char *buf, LPDWORD sz)
803
{
804 805 806 807
    ULONG count;
    HRESULT r;
    IStream *stm;

808
    TRACE("%p %d %p %p\n", rec, iField, buf, sz);
809

810 811 812 813 814
    if( !sz )
        return ERROR_INVALID_PARAMETER;

    if( iField > rec->count)
        return ERROR_INVALID_PARAMETER;
815

816 817 818 819 820 821
    if ( rec->fields[iField].type == MSIFIELD_NULL )
    {
        *sz = 0;
        return ERROR_INVALID_DATA;
    }

822
    if( rec->fields[iField].type != MSIFIELD_STREAM )
823
        return ERROR_INVALID_DATATYPE;
824 825 826

    stm = rec->fields[iField].u.stream;
    if( !stm )
827
        return ERROR_INVALID_PARAMETER;
828 829 830 831 832 833 834 835 836

    /* if there's no buffer pointer, calculate the length to the end */
    if( !buf )
    {
        LARGE_INTEGER ofs;
        ULARGE_INTEGER end, cur;

        ofs.QuadPart = cur.QuadPart = 0;
        end.QuadPart = 0;
837
        IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
838 839 840 841 842 843 844 845 846 847 848 849
        IStream_Seek( stm, ofs, STREAM_SEEK_END, &end );
        ofs.QuadPart = cur.QuadPart;
        IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
        *sz = end.QuadPart - cur.QuadPart;

        return ERROR_SUCCESS;
    }

    /* read the data */
    count = 0;
    r = IStream_Read( stm, buf, *sz, &count );
    if( FAILED( r ) )
850 851
    {
        *sz = 0;
852
        return ERROR_FUNCTION_FAILED;
853
    }
854 855 856 857 858 859

    *sz = count;

    return ERROR_SUCCESS;
}

860
UINT WINAPI MsiRecordReadStream(MSIHANDLE handle, UINT iField, char *buf, LPDWORD sz)
861 862
{
    MSIRECORD *rec;
863
    UINT ret;
864

865
    TRACE("%d %d %p %p\n", handle, iField, buf, sz);
866 867 868 869

    rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
    if( !rec )
        return ERROR_INVALID_HANDLE;
870
    msiobj_lock( &rec->hdr );
871
    ret = MSI_RecordReadStream( rec, iField, buf, sz );
872
    msiobj_unlock( &rec->hdr );
873 874 875 876
    msiobj_release( &rec->hdr );
    return ret;
}

877
UINT MSI_RecordSetIStream( MSIRECORD *rec, UINT iField, IStream *stm )
878 879
{
    TRACE("%p %d %p\n", rec, iField, stm);
880 881 882 883 884 885 886 887 888 889 890

    if( iField > rec->count )
        return ERROR_INVALID_FIELD;

    MSI_FreeField( &rec->fields[iField] );

    rec->fields[iField].type = MSIFIELD_STREAM;
    rec->fields[iField].u.stream = stm;
    IStream_AddRef( stm );

    return ERROR_SUCCESS;
891
}
892

893
UINT MSI_RecordGetIStream( MSIRECORD *rec, UINT iField, IStream **pstm)
894 895 896 897 898 899 900 901 902 903 904 905 906 907
{
    TRACE("%p %d %p\n", rec, iField, pstm);

    if( iField > rec->count )
        return ERROR_INVALID_FIELD;

    if( rec->fields[iField].type != MSIFIELD_STREAM )
        return ERROR_INVALID_FIELD;

    *pstm = rec->fields[iField].u.stream;
    IStream_AddRef( *pstm );

    return ERROR_SUCCESS;
}
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940

static UINT msi_dump_stream_to_file( IStream *stm, LPCWSTR name )
{
    ULARGE_INTEGER size;
    LARGE_INTEGER pos;
    IStream *out;
    DWORD stgm;
    HRESULT r;

    stgm = STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_FAILIFTHERE;
    r = SHCreateStreamOnFileW( name, stgm, &out );
    if( FAILED( r ) )
        return ERROR_FUNCTION_FAILED;

    pos.QuadPart = 0;
    r = IStream_Seek( stm, pos, STREAM_SEEK_END, &size );
    if( FAILED( r ) )
        goto end;

    pos.QuadPart = 0;
    r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
    if( FAILED( r ) )
        goto end;

    r = IStream_CopyTo( stm, out, size, NULL, NULL );

end:
    IStream_Release( out );
    if( FAILED( r ) )
        return ERROR_FUNCTION_FAILED;
    return ERROR_SUCCESS;
}

941
UINT MSI_RecordStreamToFile( MSIRECORD *rec, UINT iField, LPCWSTR name )
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
{
    IStream *stm = NULL;
    UINT r;

    TRACE("%p %u %s\n", rec, iField, debugstr_w(name));

    msiobj_lock( &rec->hdr );

    r = MSI_RecordGetIStream( rec, iField, &stm );
    if( r == ERROR_SUCCESS )
    {
        r = msi_dump_stream_to_file( stm, name );
        IStream_Release( stm );
    }

    msiobj_unlock( &rec->hdr );

    return r;
}
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981

MSIRECORD *MSI_CloneRecord(MSIRECORD *rec)
{
    MSIRECORD *clone;
    UINT r, i, count;

    count = MSI_RecordGetFieldCount(rec);
    clone = MSI_CreateRecord(count);
    if (!clone)
        return NULL;

    for (i = 0; i <= count; i++)
    {
        if (rec->fields[i].type == MSIFIELD_STREAM)
        {
            if (FAILED(IStream_Clone(rec->fields[i].u.stream,
                                     &clone->fields[i].u.stream)))
            {
                msiobj_release(&clone->hdr);
                return NULL;
            }
982
            clone->fields[i].type = MSIFIELD_STREAM;
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
        }
        else
        {
            r = MSI_RecordCopyField(rec, i, clone, i);
            if (r != ERROR_SUCCESS)
            {
                msiobj_release(&clone->hdr);
                return NULL;
            }
        }
    }

    return clone;
}

998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
BOOL MSI_RecordsAreFieldsEqual(MSIRECORD *a, MSIRECORD *b, UINT field)
{
    if (a->fields[field].type != b->fields[field].type)
        return FALSE;

    switch (a->fields[field].type)
    {
        case MSIFIELD_NULL:
            break;

        case MSIFIELD_INT:
            if (a->fields[field].u.iVal != b->fields[field].u.iVal)
                return FALSE;
            break;

        case MSIFIELD_WSTR:
            if (strcmpW(a->fields[field].u.szwVal, b->fields[field].u.szwVal))
                return FALSE;
            break;

        case MSIFIELD_STREAM:
        default:
            return FALSE;
    }
    return TRUE;
}


1026 1027 1028 1029 1030 1031 1032 1033 1034
BOOL MSI_RecordsAreEqual(MSIRECORD *a, MSIRECORD *b)
{
    UINT i;

    if (a->count != b->count)
        return FALSE;

    for (i = 0; i <= a->count; i++)
    {
1035
        if (!MSI_RecordsAreFieldsEqual( a, b, i ))
1036 1037 1038 1039 1040
            return FALSE;
    }

    return TRUE;
}
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066

WCHAR *msi_dup_record_field( MSIRECORD *rec, INT field )
{
    DWORD sz = 0;
    WCHAR *str;
    UINT r;

    if (MSI_RecordIsNull( rec, field )) return NULL;

    r = MSI_RecordGetStringW( rec, field, NULL, &sz );
    if (r != ERROR_SUCCESS)
        return NULL;

    sz++;
    str = msi_alloc( sz * sizeof(WCHAR) );
    if (!str) return NULL;
    str[0] = 0;
    r = MSI_RecordGetStringW( rec, field, str, &sz );
    if (r != ERROR_SUCCESS)
    {
        ERR("failed to get string!\n");
        msi_free( str );
        return NULL;
    }
    return str;
}