riched32/tests: Remove the todo_wine logic where appropriate.
[wine] / dlls / msi / suminfo.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2002, 2005 Mike McCormack for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * 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 #include <stdarg.h>
22
23 #define COBJMACROS
24 #define NONAMELESSUNION
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "shlwapi.h"
31 #include "wine/debug.h"
32 #include "msi.h"
33 #include "msiquery.h"
34 #include "msidefs.h"
35 #include "msipriv.h"
36 #include "objidl.h"
37 #include "propvarutil.h"
38 #include "msiserver.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(msi);
41
42 #include "pshpack1.h"
43
44 typedef struct { 
45     WORD wByteOrder;
46     WORD wFormat;
47     DWORD dwOSVer;
48     CLSID clsID;
49     DWORD reserved;
50 } PROPERTYSETHEADER;
51
52 typedef struct { 
53     FMTID fmtid;
54     DWORD dwOffset;
55 } FORMATIDOFFSET;
56
57 typedef struct { 
58     DWORD cbSection;
59     DWORD cProperties;
60 } PROPERTYSECTIONHEADER; 
61  
62 typedef struct { 
63     DWORD propid;
64     DWORD dwOffset;
65 } PROPERTYIDOFFSET; 
66
67 typedef struct {
68     DWORD type;
69     union {
70         INT i4;
71         SHORT i2;
72         FILETIME ft;
73         struct {
74             DWORD len;
75             BYTE str[1];
76         } str;
77     } u;
78 } PROPERTY_DATA;
79  
80 #include "poppack.h"
81
82 static HRESULT (WINAPI *pPropVariantChangeType)
83     (PROPVARIANT *ppropvarDest, REFPROPVARIANT propvarSrc,
84      PROPVAR_CHANGE_FLAGS flags, VARTYPE vt);
85
86 #define SECT_HDR_SIZE (sizeof(PROPERTYSECTIONHEADER))
87
88 static const WCHAR szSumInfo[] = { 5 ,'S','u','m','m','a','r','y',
89                        'I','n','f','o','r','m','a','t','i','o','n',0 };
90
91 static void free_prop( PROPVARIANT *prop )
92 {
93     if (prop->vt == VT_LPSTR )
94         msi_free( prop->u.pszVal );
95     prop->vt = VT_EMPTY;
96 }
97
98 static void MSI_CloseSummaryInfo( MSIOBJECTHDR *arg )
99 {
100     MSISUMMARYINFO *si = (MSISUMMARYINFO *) arg;
101     DWORD i;
102
103     for( i = 0; i < MSI_MAX_PROPS; i++ )
104         free_prop( &si->property[i] );
105     IStorage_Release( si->storage );
106 }
107
108 static UINT get_type( UINT uiProperty )
109 {
110     switch( uiProperty )
111     {
112     case PID_CODEPAGE:
113          return VT_I2;
114
115     case PID_SUBJECT:
116     case PID_AUTHOR:
117     case PID_KEYWORDS:
118     case PID_COMMENTS:
119     case PID_TEMPLATE:
120     case PID_LASTAUTHOR:
121     case PID_REVNUMBER:
122     case PID_APPNAME:
123     case PID_TITLE:
124          return VT_LPSTR;
125
126     case PID_LASTPRINTED:
127     case PID_CREATE_DTM:
128     case PID_LASTSAVE_DTM:
129          return VT_FILETIME;
130
131     case PID_WORDCOUNT:
132     case PID_CHARCOUNT:
133     case PID_SECURITY:
134     case PID_PAGECOUNT:
135          return VT_I4;
136     }
137     return VT_EMPTY;
138 }
139
140 static UINT get_property_count( const PROPVARIANT *property )
141 {
142     UINT i, n = 0;
143
144     if( !property )
145         return n;
146     for( i = 0; i < MSI_MAX_PROPS; i++ )
147         if( property[i].vt != VT_EMPTY )
148             n++;
149     return n;
150 }
151
152 static UINT propvar_changetype(PROPVARIANT *changed, PROPVARIANT *property, VARTYPE vt)
153 {
154     HRESULT hr;
155     HMODULE propsys = LoadLibraryA("propsys.dll");
156     pPropVariantChangeType = (void *)GetProcAddress(propsys, "PropVariantChangeType");
157
158     if (!pPropVariantChangeType)
159     {
160         ERR("PropVariantChangeType function missing!\n");
161         return ERROR_FUNCTION_FAILED;
162     }
163
164     hr = pPropVariantChangeType(changed, property, 0, vt);
165     return (hr == S_OK) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
166 }
167
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     PROPVARIANT property, *ptr;
175     PROPVARIANT changed;
176     PROPERTYIDOFFSET *idofs;
177     PROPERTYSECTIONHEADER *section_hdr;
178
179     section_hdr = (PROPERTYSECTIONHEADER*) &data[0];
180     idofs = (PROPERTYIDOFFSET*) &data[SECT_HDR_SIZE];
181
182     /* now set all the properties */
183     for( i = 0; i < section_hdr->cProperties; i++ )
184     {
185         if( idofs[i].propid >= MSI_MAX_PROPS )
186         {
187             ERR("Unknown property ID %d\n", idofs[i].propid );
188             break;
189         }
190
191         type = get_type( idofs[i].propid );
192         if( type == VT_EMPTY )
193         {
194             ERR("propid %d has unknown type\n", idofs[i].propid);
195             break;
196         }
197
198         propdata = (PROPERTY_DATA*) &data[ idofs[i].dwOffset ];
199
200         /* check we don't run off the end of the data */
201         size = sz - idofs[i].dwOffset - sizeof(DWORD);
202         if( sizeof(DWORD) > size ||
203             ( propdata->type == VT_FILETIME && sizeof(FILETIME) > size ) ||
204             ( propdata->type == VT_LPSTR && (propdata->u.str.len + sizeof(DWORD)) > size ) )
205         {
206             ERR("not enough data\n");
207             break;
208         }
209
210         property.vt = propdata->type;
211         if( propdata->type == VT_LPSTR )
212         {
213             LPSTR str = msi_alloc( propdata->u.str.len );
214             memcpy( str, propdata->u.str.str, propdata->u.str.len );
215             str[ propdata->u.str.len - 1 ] = 0;
216             property.u.pszVal = str;
217         }
218         else if( propdata->type == VT_FILETIME )
219             property.u.filetime = propdata->u.ft;
220         else if( propdata->type == VT_I2 )
221             property.u.iVal = propdata->u.i2;
222         else if( propdata->type == VT_I4 )
223             property.u.lVal = propdata->u.i4;
224
225         /* check the type is the same as we expect */
226         if( type != propdata->type )
227         {
228             propvar_changetype(&changed, &property, type);
229             ptr = &changed;
230         }
231         else
232             ptr = &property;
233
234         prop[ idofs[i].propid ] = *ptr;
235     }
236 }
237
238 static UINT load_summary_info( MSISUMMARYINFO *si, IStream *stm )
239 {
240     UINT ret = ERROR_FUNCTION_FAILED;
241     PROPERTYSETHEADER set_hdr;
242     FORMATIDOFFSET format_hdr;
243     PROPERTYSECTIONHEADER section_hdr;
244     LPBYTE data = NULL;
245     LARGE_INTEGER ofs;
246     ULONG count, sz;
247     HRESULT r;
248
249     TRACE("%p %p\n", si, stm);
250
251     /* read the header */
252     sz = sizeof set_hdr;
253     r = IStream_Read( stm, &set_hdr, sz, &count );
254     if( FAILED(r) || count != sz )
255         return ret;
256
257     if( set_hdr.wByteOrder != 0xfffe )
258     {
259         ERR("property set not big-endian %04X\n", set_hdr.wByteOrder);
260         return ret;
261     }
262
263     sz = sizeof format_hdr;
264     r = IStream_Read( stm, &format_hdr, sz, &count );
265     if( FAILED(r) || count != sz )
266         return ret;
267
268     /* check the format id is correct */
269     if( !IsEqualGUID( &FMTID_SummaryInformation, &format_hdr.fmtid ) )
270         return ret;
271
272     /* seek to the location of the section */
273     ofs.QuadPart = format_hdr.dwOffset;
274     r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, NULL );
275     if( FAILED(r) )
276         return ret;
277
278     /* read the section itself */
279     sz = SECT_HDR_SIZE;
280     r = IStream_Read( stm, &section_hdr, sz, &count );
281     if( FAILED(r) || count != sz )
282         return ret;
283
284     if( section_hdr.cProperties > MSI_MAX_PROPS )
285     {
286         ERR("too many properties %d\n", section_hdr.cProperties);
287         return ret;
288     }
289
290     data = msi_alloc( section_hdr.cbSection);
291     if( !data )
292         return ret;
293
294     memcpy( data, &section_hdr, SECT_HDR_SIZE );
295
296     /* read all the data in one go */
297     sz = section_hdr.cbSection - SECT_HDR_SIZE;
298     r = IStream_Read( stm, &data[ SECT_HDR_SIZE ], sz, &count );
299     if( SUCCEEDED(r) && count == sz )
300         read_properties_from_data( si->property, data, sz + SECT_HDR_SIZE );
301     else
302         ERR("failed to read properties %d %d\n", count, sz);
303
304     msi_free( data );
305     return ret;
306 }
307
308 static DWORD write_dword( LPBYTE data, DWORD ofs, DWORD val )
309 {
310     if( data )
311     {
312         data[ofs++] = val&0xff;
313         data[ofs++] = (val>>8)&0xff;
314         data[ofs++] = (val>>16)&0xff;
315         data[ofs++] = (val>>24)&0xff;
316     }
317     return 4;
318 }
319
320 static DWORD write_filetime( LPBYTE data, DWORD ofs, const FILETIME *ft )
321 {
322     write_dword( data, ofs, ft->dwLowDateTime );
323     write_dword( data, ofs + 4, ft->dwHighDateTime );
324     return 8;
325 }
326
327 static DWORD write_string( LPBYTE data, DWORD ofs, LPCSTR str )
328 {
329     DWORD len = lstrlenA( str ) + 1;
330     write_dword( data, ofs, len );
331     if( data )
332         memcpy( &data[ofs + 4], str, len );
333     return (7 + len) & ~3;
334 }
335
336 static UINT write_property_to_data( const PROPVARIANT *prop, LPBYTE data )
337 {
338     DWORD sz = 0;
339
340     if( prop->vt == VT_EMPTY )
341         return sz;
342
343     /* add the type */
344     sz += write_dword( data, sz, prop->vt );
345     switch( prop->vt )
346     {
347     case VT_I2:
348         sz += write_dword( data, sz, prop->u.iVal );
349         break;
350     case VT_I4:
351         sz += write_dword( data, sz, prop->u.lVal );
352         break;
353     case VT_FILETIME:
354         sz += write_filetime( data, sz, &prop->u.filetime );
355         break;
356     case VT_LPSTR:
357         sz += write_string( data, sz, prop->u.pszVal );
358         break;
359     }
360     return sz;
361 }
362
363 static UINT save_summary_info( const MSISUMMARYINFO * si, IStream *stm )
364 {
365     UINT ret = ERROR_FUNCTION_FAILED;
366     PROPERTYSETHEADER set_hdr;
367     FORMATIDOFFSET format_hdr;
368     PROPERTYSECTIONHEADER section_hdr;
369     PROPERTYIDOFFSET idofs[MSI_MAX_PROPS];
370     LPBYTE data = NULL;
371     ULONG count, sz;
372     HRESULT r;
373     int i;
374
375     /* write the header */
376     sz = sizeof set_hdr;
377     memset( &set_hdr, 0, sz );
378     set_hdr.wByteOrder = 0xfffe;
379     set_hdr.wFormat = 0;
380     set_hdr.dwOSVer = 0x00020005; /* build 5, platform id 2 */
381     /* set_hdr.clsID is {00000000-0000-0000-0000-000000000000} */
382     set_hdr.reserved = 1;
383     r = IStream_Write( stm, &set_hdr, sz, &count );
384     if( FAILED(r) || count != sz )
385         return ret;
386
387     /* write the format header */
388     sz = sizeof format_hdr;
389     format_hdr.fmtid = FMTID_SummaryInformation;
390     format_hdr.dwOffset = sizeof format_hdr + sizeof set_hdr;
391     r = IStream_Write( stm, &format_hdr, sz, &count );
392     if( FAILED(r) || count != sz )
393         return ret;
394
395     /* add up how much space the data will take and calculate the offsets */
396     section_hdr.cbSection = sizeof section_hdr;
397     section_hdr.cbSection += (get_property_count( si->property ) * sizeof idofs[0]);
398     section_hdr.cProperties = 0;
399     for( i = 0; i < MSI_MAX_PROPS; i++ )
400     {
401         sz = write_property_to_data( &si->property[i], NULL );
402         if( !sz )
403             continue;
404         idofs[ section_hdr.cProperties ].propid = i;
405         idofs[ section_hdr.cProperties ].dwOffset = section_hdr.cbSection;
406         section_hdr.cProperties++;
407         section_hdr.cbSection += sz;
408     }
409
410     data = msi_alloc_zero( section_hdr.cbSection );
411
412     sz = 0;
413     memcpy( &data[sz], &section_hdr, sizeof section_hdr );
414     sz += sizeof section_hdr;
415
416     memcpy( &data[sz], idofs, section_hdr.cProperties * sizeof idofs[0] );
417     sz += section_hdr.cProperties * sizeof idofs[0];
418
419     /* write out the data */
420     for( i = 0; i < MSI_MAX_PROPS; i++ )
421         sz += write_property_to_data( &si->property[i], &data[sz] );
422
423     r = IStream_Write( stm, data, sz, &count );
424     msi_free( data );
425     if( FAILED(r) || count != sz )
426         return ret;
427
428     return ERROR_SUCCESS;
429 }
430
431 MSISUMMARYINFO *MSI_GetSummaryInformationW( IStorage *stg, UINT uiUpdateCount )
432 {
433     IStream *stm = NULL;
434     MSISUMMARYINFO *si;
435     DWORD grfMode;
436     HRESULT r;
437
438     TRACE("%p %d\n", stg, uiUpdateCount );
439
440     si = alloc_msiobject( MSIHANDLETYPE_SUMMARYINFO, 
441                   sizeof (MSISUMMARYINFO), MSI_CloseSummaryInfo );
442     if( !si )
443         return si;
444
445     memset( si->property, 0, sizeof si->property );
446     si->update_count = uiUpdateCount;
447     IStorage_AddRef( stg );
448     si->storage = stg;
449
450     /* read the stream... if we fail, we'll start with an empty property set */
451     grfMode = STGM_READ | STGM_SHARE_EXCLUSIVE;
452     r = IStorage_OpenStream( si->storage, szSumInfo, 0, grfMode, 0, &stm );
453     if( SUCCEEDED(r) )
454     {
455         load_summary_info( si, stm );
456         IStream_Release( stm );
457     }
458
459     return si;
460 }
461
462 UINT WINAPI MsiGetSummaryInformationW( MSIHANDLE hDatabase, 
463               LPCWSTR szDatabase, UINT uiUpdateCount, MSIHANDLE *pHandle )
464 {
465     MSISUMMARYINFO *si;
466     MSIDATABASE *db;
467     UINT ret = ERROR_FUNCTION_FAILED;
468
469     TRACE("%ld %s %d %p\n", hDatabase, debugstr_w(szDatabase),
470            uiUpdateCount, pHandle);
471
472     if( !pHandle )
473         return ERROR_INVALID_PARAMETER;
474
475     if( szDatabase )
476     {
477         ret = MSI_OpenDatabaseW( szDatabase, NULL, &db );
478         if( ret != ERROR_SUCCESS )
479             return ret;
480     }
481     else
482     {
483         db = msihandle2msiinfo( hDatabase, MSIHANDLETYPE_DATABASE );
484         if( !db )
485         {
486             HRESULT hr;
487             IWineMsiRemoteDatabase *remote_database;
488
489             remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hDatabase );
490             if ( !remote_database )
491                 return ERROR_INVALID_HANDLE;
492
493             hr = IWineMsiRemoteDatabase_GetSummaryInformation( remote_database,
494                                                                uiUpdateCount, pHandle );
495             IWineMsiRemoteDatabase_Release( remote_database );
496
497             if (FAILED(hr))
498             {
499                 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
500                     return HRESULT_CODE(hr);
501
502                 return ERROR_FUNCTION_FAILED;
503             }
504
505             return ERROR_SUCCESS;
506         }
507     }
508
509     si = MSI_GetSummaryInformationW( db->storage, uiUpdateCount );
510     if (si)
511     {
512         *pHandle = alloc_msihandle( &si->hdr );
513         if( *pHandle )
514             ret = ERROR_SUCCESS;
515         else
516             ret = ERROR_NOT_ENOUGH_MEMORY;
517         msiobj_release( &si->hdr );
518     }
519
520     if( db )
521         msiobj_release( &db->hdr );
522
523     return ret;
524 }
525
526 UINT WINAPI MsiGetSummaryInformationA(MSIHANDLE hDatabase, 
527               LPCSTR szDatabase, UINT uiUpdateCount, MSIHANDLE *pHandle)
528 {
529     LPWSTR szwDatabase = NULL;
530     UINT ret;
531
532     TRACE("%ld %s %d %p\n", hDatabase, debugstr_a(szDatabase), 
533           uiUpdateCount, pHandle);
534
535     if( szDatabase )
536     {
537         szwDatabase = strdupAtoW( szDatabase );
538         if( !szwDatabase )
539             return ERROR_FUNCTION_FAILED;
540     }
541
542     ret = MsiGetSummaryInformationW(hDatabase, szwDatabase, uiUpdateCount, pHandle);
543
544     msi_free( szwDatabase );
545
546     return ret;
547 }
548
549 UINT WINAPI MsiSummaryInfoGetPropertyCount(MSIHANDLE hSummaryInfo, PUINT pCount)
550 {
551     MSISUMMARYINFO *si;
552
553     TRACE("%ld %p\n", hSummaryInfo, pCount);
554
555     si = msihandle2msiinfo( hSummaryInfo, MSIHANDLETYPE_SUMMARYINFO );
556     if( !si )
557         return ERROR_INVALID_HANDLE;
558
559     if( pCount )
560         *pCount = get_property_count( si->property );
561     msiobj_release( &si->hdr );
562
563     return ERROR_SUCCESS;
564 }
565
566 static UINT get_prop( MSIHANDLE handle, UINT uiProperty, UINT *puiDataType,
567           INT *piValue, FILETIME *pftValue, awstring *str, DWORD *pcchValueBuf)
568 {
569     MSISUMMARYINFO *si;
570     PROPVARIANT *prop;
571     UINT ret = ERROR_SUCCESS;
572
573     TRACE("%ld %d %p %p %p %p %p\n", handle, uiProperty, puiDataType,
574           piValue, pftValue, str, pcchValueBuf);
575
576     if ( uiProperty >= MSI_MAX_PROPS )
577     {
578         if (puiDataType) *puiDataType = VT_EMPTY;
579         return ERROR_UNKNOWN_PROPERTY;
580     }
581
582     si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
583     if( !si )
584         return ERROR_INVALID_HANDLE;
585
586     prop = &si->property[uiProperty];
587
588     if( puiDataType )
589         *puiDataType = prop->vt;
590
591     switch( prop->vt )
592     {
593     case VT_I2:
594         if( piValue )
595             *piValue = prop->u.iVal;
596         break;
597     case VT_I4:
598         if( piValue )
599             *piValue = prop->u.lVal;
600         break;
601     case VT_LPSTR:
602         if( pcchValueBuf )
603         {
604             DWORD len = 0;
605
606             if( str->unicode )
607             {
608                 len = MultiByteToWideChar( CP_ACP, 0, prop->u.pszVal, -1,
609                                            str->str.w, *pcchValueBuf );
610                 len--;
611             }
612             else
613             {
614                 len = lstrlenA( prop->u.pszVal );
615                 if( str->str.a )
616                     lstrcpynA(str->str.a, prop->u.pszVal, *pcchValueBuf );
617             }
618             if (len >= *pcchValueBuf)
619                 ret = ERROR_MORE_DATA;
620             *pcchValueBuf = len;
621         }
622         break;
623     case VT_FILETIME:
624         if( pftValue )
625             *pftValue = prop->u.filetime;
626         break;
627     case VT_EMPTY:
628         break;
629     default:
630         FIXME("Unknown property variant type\n");
631         break;
632     }
633     msiobj_release( &si->hdr );
634     return ret;
635 }
636
637 LPWSTR msi_suminfo_dup_string( MSISUMMARYINFO *si, UINT uiProperty )
638 {
639     PROPVARIANT *prop;
640
641     if ( uiProperty >= MSI_MAX_PROPS )
642         return NULL;
643     prop = &si->property[uiProperty];
644     if( prop->vt != VT_LPSTR )
645         return NULL;
646     return strdupAtoW( prop->u.pszVal );
647 }
648
649 LPWSTR msi_get_suminfo_product( IStorage *stg )
650 {
651     MSISUMMARYINFO *si;
652     LPWSTR prod;
653
654     si = MSI_GetSummaryInformationW( stg, 0 );
655     if (!si)
656     {
657         ERR("no summary information!\n");
658         return NULL;
659     }
660     prod = msi_suminfo_dup_string( si, PID_REVNUMBER );
661     msiobj_release( &si->hdr );
662     return prod;
663 }
664
665 UINT WINAPI MsiSummaryInfoGetPropertyA(
666       MSIHANDLE handle, UINT uiProperty, PUINT puiDataType, LPINT piValue,
667       FILETIME *pftValue, LPSTR szValueBuf, LPDWORD pcchValueBuf)
668 {
669     awstring str;
670
671     TRACE("%ld %d %p %p %p %p %p\n", handle, uiProperty, puiDataType,
672           piValue, pftValue, szValueBuf, pcchValueBuf );
673
674     str.unicode = FALSE;
675     str.str.a = szValueBuf;
676
677     return get_prop( handle, uiProperty, puiDataType, piValue,
678                      pftValue, &str, pcchValueBuf );
679 }
680
681 UINT WINAPI MsiSummaryInfoGetPropertyW(
682       MSIHANDLE handle, UINT uiProperty, PUINT puiDataType, LPINT piValue,
683       FILETIME *pftValue, LPWSTR szValueBuf, LPDWORD pcchValueBuf)
684 {
685     awstring str;
686
687     TRACE("%ld %d %p %p %p %p %p\n", handle, uiProperty, puiDataType,
688           piValue, pftValue, szValueBuf, pcchValueBuf );
689
690     str.unicode = TRUE;
691     str.str.w = szValueBuf;
692
693     return get_prop( handle, uiProperty, puiDataType, piValue,
694                      pftValue, &str, pcchValueBuf );
695 }
696
697 static UINT set_prop( MSIHANDLE handle, UINT uiProperty, UINT uiDataType,
698                INT iValue, FILETIME* pftValue, awcstring *str )
699 {
700     MSISUMMARYINFO *si;
701     PROPVARIANT *prop;
702     UINT type, len, ret = ERROR_SUCCESS;
703
704     TRACE("%ld %u %u %i %p %p\n", handle, uiProperty, uiDataType,
705           iValue, pftValue, str );
706
707     type = get_type( uiProperty );
708     if( type == VT_EMPTY || type != uiDataType )
709         return ERROR_DATATYPE_MISMATCH;
710
711     if( uiDataType == VT_LPSTR && !str->str.w )
712         return ERROR_INVALID_PARAMETER;
713
714     if( uiDataType == VT_FILETIME && !pftValue )
715         return ERROR_INVALID_PARAMETER;
716
717     si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
718     if( !si )
719         return ERROR_INVALID_HANDLE;
720
721     prop = &si->property[uiProperty];
722
723     if( prop->vt == VT_EMPTY )
724     {
725         if( !si->update_count )
726         {
727             ret = ERROR_FUNCTION_FAILED;
728             goto end;
729         }
730         si->update_count--;
731     }
732     else if( prop->vt != type )
733         goto end;
734
735     free_prop( prop );
736     prop->vt = type;
737     switch( type )
738     {
739     case VT_I4:
740         prop->u.lVal = iValue;
741         break;
742     case VT_I2:
743         prop->u.iVal = iValue;
744         break;
745     case VT_FILETIME:
746         prop->u.filetime = *pftValue;
747         break;
748     case VT_LPSTR:
749         if( str->unicode )
750         {
751             len = WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
752                                        NULL, 0, NULL, NULL );
753             prop->u.pszVal = msi_alloc( len );
754             WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
755                                  prop->u.pszVal, len, NULL, NULL );
756         }
757         else
758         {
759             len = lstrlenA( str->str.a ) + 1;
760             prop->u.pszVal = msi_alloc( len );
761             lstrcpyA( prop->u.pszVal, str->str.a );
762         }
763         break;
764     }
765
766 end:
767     msiobj_release( &si->hdr );
768     return ret;
769 }
770
771 UINT WINAPI MsiSummaryInfoSetPropertyW( MSIHANDLE handle, UINT uiProperty,
772                UINT uiDataType, INT iValue, FILETIME* pftValue, LPCWSTR szValue )
773 {
774     awcstring str;
775
776     TRACE("%ld %u %u %i %p %s\n", handle, uiProperty, uiDataType,
777           iValue, pftValue, debugstr_w(szValue) );
778
779     str.unicode = TRUE;
780     str.str.w = szValue;
781     return set_prop( handle, uiProperty, uiDataType, iValue, pftValue, &str );
782 }
783
784 UINT WINAPI MsiSummaryInfoSetPropertyA( MSIHANDLE handle, UINT uiProperty,
785                UINT uiDataType, INT iValue, FILETIME* pftValue, LPCSTR szValue )
786 {
787     awcstring str;
788
789     TRACE("%ld %u %u %i %p %s\n", handle, uiProperty, uiDataType,
790           iValue, pftValue, debugstr_a(szValue) );
791
792     str.unicode = FALSE;
793     str.str.a = szValue;
794     return set_prop( handle, uiProperty, uiDataType, iValue, pftValue, &str );
795 }
796
797 UINT WINAPI MsiSummaryInfoPersist( MSIHANDLE handle )
798 {
799     IStream *stm = NULL;
800     MSISUMMARYINFO *si;
801     DWORD grfMode;
802     HRESULT r;
803     UINT ret = ERROR_FUNCTION_FAILED;
804
805     TRACE("%ld\n", handle );
806
807     si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
808     if( !si )
809         return ERROR_INVALID_HANDLE;
810
811     grfMode = STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
812     r = IStorage_CreateStream( si->storage, szSumInfo, grfMode, 0, 0, &stm );
813     if( SUCCEEDED(r) )
814     {
815         ret = save_summary_info( si, stm );
816         IStream_Release( stm );
817     }
818     msiobj_release( &si->hdr );
819
820     return ret;
821 }