dwmapi: Clear DWM_TIMING_INFO structure before returning.
[wine.git] / dlls / msi / suminfo.c
blob296d554cfe0a8fedc56157aa479bf94783e420fd
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002, 2005 Mike McCormack for CodeWeavers
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.
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.
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
21 #include <stdarg.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
26 #include "stdio.h"
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winreg.h"
30 #include "winnls.h"
31 #include "shlwapi.h"
32 #include "wine/debug.h"
33 #include "wine/exception.h"
34 #include "msi.h"
35 #include "msiquery.h"
36 #include "msidefs.h"
37 #include "objidl.h"
38 #include "propvarutil.h"
40 #include "msipriv.h"
41 #include "winemsi.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(msi);
45 #include "pshpack1.h"
47 typedef struct {
48 WORD wByteOrder;
49 WORD wFormat;
50 DWORD dwOSVer;
51 CLSID clsID;
52 DWORD reserved;
53 } PROPERTYSETHEADER;
55 typedef struct {
56 FMTID fmtid;
57 DWORD dwOffset;
58 } FORMATIDOFFSET;
60 typedef struct {
61 DWORD cbSection;
62 DWORD cProperties;
63 } PROPERTYSECTIONHEADER;
65 typedef struct {
66 DWORD propid;
67 DWORD dwOffset;
68 } PROPERTYIDOFFSET;
70 typedef struct {
71 DWORD type;
72 union {
73 INT i4;
74 SHORT i2;
75 FILETIME ft;
76 struct {
77 DWORD len;
78 BYTE str[1];
79 } str;
80 } u;
81 } PROPERTY_DATA;
83 #include "poppack.h"
85 static HRESULT (WINAPI *pPropVariantChangeType)
86 (PROPVARIANT *ppropvarDest, REFPROPVARIANT propvarSrc,
87 PROPVAR_CHANGE_FLAGS flags, VARTYPE vt);
89 #define SECT_HDR_SIZE (sizeof(PROPERTYSECTIONHEADER))
91 static void free_prop( PROPVARIANT *prop )
93 if (prop->vt == VT_LPSTR )
94 msi_free( prop->pszVal );
95 prop->vt = VT_EMPTY;
98 static void MSI_CloseSummaryInfo( MSIOBJECTHDR *arg )
100 MSISUMMARYINFO *si = (MSISUMMARYINFO *) arg;
101 DWORD i;
103 for( i = 0; i < MSI_MAX_PROPS; i++ )
104 free_prop( &si->property[i] );
105 IStorage_Release( si->storage );
108 static UINT get_type( UINT uiProperty )
110 switch( uiProperty )
112 case PID_CODEPAGE:
113 return VT_I2;
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;
126 case PID_LASTPRINTED:
127 case PID_CREATE_DTM:
128 case PID_LASTSAVE_DTM:
129 return VT_FILETIME;
131 case PID_WORDCOUNT:
132 case PID_CHARCOUNT:
133 case PID_SECURITY:
134 case PID_PAGECOUNT:
135 return VT_I4;
137 return VT_EMPTY;
140 static UINT get_property_count( const PROPVARIANT *property )
142 UINT i, n = 0;
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;
152 static UINT propvar_changetype(PROPVARIANT *changed, PROPVARIANT *property, VARTYPE vt)
154 HRESULT hr;
155 HMODULE propsys = LoadLibraryA("propsys.dll");
156 pPropVariantChangeType = (void *)GetProcAddress(propsys, "PropVariantChangeType");
158 if (!pPropVariantChangeType)
160 ERR("PropVariantChangeType function missing!\n");
161 return ERROR_FUNCTION_FAILED;
164 hr = pPropVariantChangeType(changed, property, 0, vt);
165 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 )
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;
179 section_hdr = (PROPERTYSECTIONHEADER*) &data[0];
180 idofs = (PROPERTYIDOFFSET*) &data[SECT_HDR_SIZE];
182 /* now set all the properties */
183 for( i = 0; i < section_hdr->cProperties; i++ )
185 if( idofs[i].propid >= MSI_MAX_PROPS )
187 ERR( "unknown property ID %lu\n", idofs[i].propid );
188 break;
191 type = get_type( idofs[i].propid );
192 if( type == VT_EMPTY )
194 ERR( "propid %lu has unknown type\n", idofs[i].propid );
195 break;
198 propdata = (PROPERTY_DATA*) &data[ idofs[i].dwOffset ];
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 ) )
206 ERR("not enough data\n");
207 break;
210 property.vt = propdata->type;
211 if( propdata->type == VT_LPSTR )
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.pszVal = str;
218 else if( propdata->type == VT_FILETIME )
219 property.filetime = propdata->u.ft;
220 else if( propdata->type == VT_I2 )
221 property.iVal = propdata->u.i2;
222 else if( propdata->type == VT_I4 )
223 property.lVal = propdata->u.i4;
225 /* check the type is the same as we expect */
226 if( type != propdata->type )
228 propvar_changetype(&changed, &property, type);
229 ptr = &changed;
231 else
232 ptr = &property;
234 prop[ idofs[i].propid ] = *ptr;
238 static UINT load_summary_info( MSISUMMARYINFO *si, IStream *stm )
240 PROPERTYSETHEADER set_hdr;
241 FORMATIDOFFSET format_hdr;
242 PROPERTYSECTIONHEADER section_hdr;
243 LPBYTE data = NULL;
244 LARGE_INTEGER ofs;
245 ULONG count, sz;
246 HRESULT r;
248 TRACE("%p %p\n", si, stm);
250 /* read the header */
251 sz = sizeof set_hdr;
252 r = IStream_Read( stm, &set_hdr, sz, &count );
253 if( FAILED(r) || count != sz )
254 return ERROR_FUNCTION_FAILED;
256 if( set_hdr.wByteOrder != 0xfffe )
258 ERR("property set not big-endian %04X\n", set_hdr.wByteOrder);
259 return ERROR_FUNCTION_FAILED;
262 sz = sizeof format_hdr;
263 r = IStream_Read( stm, &format_hdr, sz, &count );
264 if( FAILED(r) || count != sz )
265 return ERROR_FUNCTION_FAILED;
267 /* check the format id is correct */
268 if( !IsEqualGUID( &FMTID_SummaryInformation, &format_hdr.fmtid ) )
269 return ERROR_FUNCTION_FAILED;
271 /* seek to the location of the section */
272 ofs.QuadPart = format_hdr.dwOffset;
273 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, NULL );
274 if( FAILED(r) )
275 return ERROR_FUNCTION_FAILED;
277 /* read the section itself */
278 sz = SECT_HDR_SIZE;
279 r = IStream_Read( stm, &section_hdr, sz, &count );
280 if( FAILED(r) || count != sz )
281 return ERROR_FUNCTION_FAILED;
283 if( section_hdr.cProperties > MSI_MAX_PROPS )
285 ERR( "too many properties %lu\n", section_hdr.cProperties );
286 return ERROR_FUNCTION_FAILED;
289 data = msi_alloc( section_hdr.cbSection);
290 if( !data )
291 return ERROR_FUNCTION_FAILED;
293 memcpy( data, &section_hdr, SECT_HDR_SIZE );
295 /* read all the data in one go */
296 sz = section_hdr.cbSection - SECT_HDR_SIZE;
297 r = IStream_Read( stm, &data[ SECT_HDR_SIZE ], sz, &count );
298 if( SUCCEEDED(r) && count == sz )
299 read_properties_from_data( si->property, data, sz + SECT_HDR_SIZE );
300 else
301 ERR( "failed to read properties %lu %lu\n", count, sz );
303 msi_free( data );
304 return ERROR_SUCCESS;
307 static DWORD write_dword( LPBYTE data, DWORD ofs, DWORD val )
309 if( data )
311 data[ofs++] = val&0xff;
312 data[ofs++] = (val>>8)&0xff;
313 data[ofs++] = (val>>16)&0xff;
314 data[ofs++] = (val>>24)&0xff;
316 return 4;
319 static DWORD write_filetime( LPBYTE data, DWORD ofs, const FILETIME *ft )
321 write_dword( data, ofs, ft->dwLowDateTime );
322 write_dword( data, ofs + 4, ft->dwHighDateTime );
323 return 8;
326 static DWORD write_string( LPBYTE data, DWORD ofs, LPCSTR str )
328 DWORD len = lstrlenA( str ) + 1;
329 write_dword( data, ofs, len );
330 if( data )
331 memcpy( &data[ofs + 4], str, len );
332 return (7 + len) & ~3;
335 static UINT write_property_to_data( const PROPVARIANT *prop, LPBYTE data )
337 DWORD sz = 0;
339 if( prop->vt == VT_EMPTY )
340 return sz;
342 /* add the type */
343 sz += write_dword( data, sz, prop->vt );
344 switch( prop->vt )
346 case VT_I2:
347 sz += write_dword( data, sz, prop->iVal );
348 break;
349 case VT_I4:
350 sz += write_dword( data, sz, prop->lVal );
351 break;
352 case VT_FILETIME:
353 sz += write_filetime( data, sz, &prop->filetime );
354 break;
355 case VT_LPSTR:
356 sz += write_string( data, sz, prop->pszVal );
357 break;
359 return sz;
362 static UINT save_summary_info( const MSISUMMARYINFO * si, IStream *stm )
364 UINT ret = ERROR_FUNCTION_FAILED;
365 PROPERTYSETHEADER set_hdr;
366 FORMATIDOFFSET format_hdr;
367 PROPERTYSECTIONHEADER section_hdr;
368 PROPERTYIDOFFSET idofs[MSI_MAX_PROPS];
369 LPBYTE data = NULL;
370 ULONG count, sz;
371 HRESULT r;
372 int i;
374 /* write the header */
375 sz = sizeof set_hdr;
376 memset( &set_hdr, 0, sz );
377 set_hdr.wByteOrder = 0xfffe;
378 set_hdr.wFormat = 0;
379 set_hdr.dwOSVer = 0x00020005; /* build 5, platform id 2 */
380 /* set_hdr.clsID is {00000000-0000-0000-0000-000000000000} */
381 set_hdr.reserved = 1;
382 r = IStream_Write( stm, &set_hdr, sz, &count );
383 if( FAILED(r) || count != sz )
384 return ret;
386 /* write the format header */
387 sz = sizeof format_hdr;
388 format_hdr.fmtid = FMTID_SummaryInformation;
389 format_hdr.dwOffset = sizeof format_hdr + sizeof set_hdr;
390 r = IStream_Write( stm, &format_hdr, sz, &count );
391 if( FAILED(r) || count != sz )
392 return ret;
394 /* add up how much space the data will take and calculate the offsets */
395 section_hdr.cbSection = sizeof section_hdr;
396 section_hdr.cbSection += (get_property_count( si->property ) * sizeof idofs[0]);
397 section_hdr.cProperties = 0;
398 for( i = 0; i < MSI_MAX_PROPS; i++ )
400 sz = write_property_to_data( &si->property[i], NULL );
401 if( !sz )
402 continue;
403 idofs[ section_hdr.cProperties ].propid = i;
404 idofs[ section_hdr.cProperties ].dwOffset = section_hdr.cbSection;
405 section_hdr.cProperties++;
406 section_hdr.cbSection += sz;
409 data = msi_alloc_zero( section_hdr.cbSection );
411 sz = 0;
412 memcpy( &data[sz], &section_hdr, sizeof section_hdr );
413 sz += sizeof section_hdr;
415 memcpy( &data[sz], idofs, section_hdr.cProperties * sizeof idofs[0] );
416 sz += section_hdr.cProperties * sizeof idofs[0];
418 /* write out the data */
419 for( i = 0; i < MSI_MAX_PROPS; i++ )
420 sz += write_property_to_data( &si->property[i], &data[sz] );
422 r = IStream_Write( stm, data, sz, &count );
423 msi_free( data );
424 if( FAILED(r) || count != sz )
425 return ret;
427 return ERROR_SUCCESS;
430 static MSISUMMARYINFO *create_suminfo( IStorage *stg, UINT update_count )
432 MSISUMMARYINFO *si;
434 if (!(si = alloc_msiobject( MSIHANDLETYPE_SUMMARYINFO, sizeof(MSISUMMARYINFO), MSI_CloseSummaryInfo )))
435 return NULL;
437 si->update_count = update_count;
438 IStorage_AddRef( stg );
439 si->storage = stg;
441 return si;
444 UINT msi_get_suminfo( IStorage *stg, UINT uiUpdateCount, MSISUMMARYINFO **ret )
446 IStream *stm;
447 MSISUMMARYINFO *si;
448 HRESULT hr;
449 UINT r;
451 TRACE("%p, %u\n", stg, uiUpdateCount);
453 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 ))
458 msiobj_release( &si->hdr );
459 return ERROR_FUNCTION_FAILED;
462 r = load_summary_info( si, stm );
463 IStream_Release( stm );
464 if (r != ERROR_SUCCESS)
466 msiobj_release( &si->hdr );
467 return r;
470 *ret = si;
471 return ERROR_SUCCESS;
474 UINT msi_get_db_suminfo( MSIDATABASE *db, UINT uiUpdateCount, MSISUMMARYINFO **ret )
476 IStream *stm;
477 MSISUMMARYINFO *si;
478 UINT r;
480 if (!(si = create_suminfo( db->storage, uiUpdateCount ))) return ERROR_OUTOFMEMORY;
482 r = msi_get_stream( db, L"\5SummaryInformation", &stm );
483 if (r != ERROR_SUCCESS)
485 msiobj_release( &si->hdr );
486 return r;
489 r = load_summary_info( si, stm );
490 IStream_Release( stm );
491 if (r != ERROR_SUCCESS)
493 msiobj_release( &si->hdr );
494 return r;
497 *ret = si;
498 return ERROR_SUCCESS;
501 UINT WINAPI MsiGetSummaryInformationW( MSIHANDLE hDatabase, const WCHAR *szDatabase, UINT uiUpdateCount,
502 MSIHANDLE *pHandle )
504 MSISUMMARYINFO *si;
505 MSIDATABASE *db;
506 UINT ret;
508 TRACE( "%lu, %s, %u, %p\n", hDatabase, debugstr_w(szDatabase), uiUpdateCount, pHandle );
510 if( !pHandle )
511 return ERROR_INVALID_PARAMETER;
513 if( szDatabase && szDatabase[0] )
515 LPCWSTR persist = uiUpdateCount ? MSIDBOPEN_TRANSACT : MSIDBOPEN_READONLY;
517 ret = MSI_OpenDatabaseW( szDatabase, persist, &db );
518 if( ret != ERROR_SUCCESS )
519 return ret;
521 else
523 db = msihandle2msiinfo( hDatabase, MSIHANDLETYPE_DATABASE );
524 if( !db )
526 MSIHANDLE remote, remote_suminfo;
528 if (!(remote = msi_get_remote(hDatabase)))
529 return ERROR_INVALID_HANDLE;
531 __TRY
533 ret = remote_DatabaseGetSummaryInformation(remote, uiUpdateCount, &remote_suminfo);
535 __EXCEPT(rpc_filter)
537 ret = GetExceptionCode();
539 __ENDTRY
541 if (!ret)
542 *pHandle = alloc_msi_remote_handle(remote_suminfo);
544 return ret;
548 ret = msi_get_suminfo( db->storage, uiUpdateCount, &si );
549 if (ret != ERROR_SUCCESS)
550 ret = msi_get_db_suminfo( db, uiUpdateCount, &si );
551 if (ret != ERROR_SUCCESS)
553 if ((si = create_suminfo( db->storage, uiUpdateCount )))
554 ret = ERROR_SUCCESS;
557 if (ret == ERROR_SUCCESS)
559 *pHandle = alloc_msihandle( &si->hdr );
560 if( *pHandle )
561 ret = ERROR_SUCCESS;
562 else
563 ret = ERROR_NOT_ENOUGH_MEMORY;
564 msiobj_release( &si->hdr );
567 msiobj_release( &db->hdr );
568 return ret;
571 UINT WINAPI MsiGetSummaryInformationA( MSIHANDLE hDatabase, const char *szDatabase, UINT uiUpdateCount,
572 MSIHANDLE *pHandle )
574 WCHAR *szwDatabase = NULL;
575 UINT ret;
577 TRACE( "%lu, %s, %u, %p\n", hDatabase, debugstr_a(szDatabase), uiUpdateCount, pHandle );
579 if( szDatabase )
581 szwDatabase = strdupAtoW( szDatabase );
582 if( !szwDatabase )
583 return ERROR_FUNCTION_FAILED;
586 ret = MsiGetSummaryInformationW(hDatabase, szwDatabase, uiUpdateCount, pHandle);
588 msi_free( szwDatabase );
590 return ret;
593 UINT WINAPI MsiSummaryInfoGetPropertyCount( MSIHANDLE hSummaryInfo, UINT *pCount )
595 MSISUMMARYINFO *si;
597 TRACE( "%lu, %p\n", hSummaryInfo, pCount );
599 si = msihandle2msiinfo( hSummaryInfo, MSIHANDLETYPE_SUMMARYINFO );
600 if( !si )
602 MSIHANDLE remote;
603 UINT ret;
605 if (!(remote = msi_get_remote( hSummaryInfo )))
606 return ERROR_INVALID_HANDLE;
608 __TRY
610 ret = remote_SummaryInfoGetPropertyCount( remote, pCount );
612 __EXCEPT(rpc_filter)
614 ret = GetExceptionCode();
616 __ENDTRY
618 return ret;
621 if( pCount )
622 *pCount = get_property_count( si->property );
623 msiobj_release( &si->hdr );
625 return ERROR_SUCCESS;
628 static UINT get_prop( MSISUMMARYINFO *si, UINT uiProperty, UINT *puiDataType, INT *piValue,
629 FILETIME *pftValue, awstring *str, DWORD *pcchValueBuf)
631 PROPVARIANT *prop;
632 UINT ret = ERROR_SUCCESS;
634 prop = &si->property[uiProperty];
636 if( puiDataType )
637 *puiDataType = prop->vt;
639 switch( prop->vt )
641 case VT_I2:
642 if( piValue )
643 *piValue = prop->iVal;
644 break;
645 case VT_I4:
646 if( piValue )
647 *piValue = prop->lVal;
648 break;
649 case VT_LPSTR:
650 if( pcchValueBuf )
652 DWORD len = 0;
654 if( str->unicode )
656 len = MultiByteToWideChar( CP_ACP, 0, prop->pszVal, -1, NULL, 0 ) - 1;
657 MultiByteToWideChar( CP_ACP, 0, prop->pszVal, -1, str->str.w, *pcchValueBuf );
659 else
661 len = lstrlenA( prop->pszVal );
662 if( str->str.a )
663 lstrcpynA(str->str.a, prop->pszVal, *pcchValueBuf );
665 if (len >= *pcchValueBuf)
666 ret = ERROR_MORE_DATA;
667 *pcchValueBuf = len;
669 break;
670 case VT_FILETIME:
671 if( pftValue )
672 *pftValue = prop->filetime;
673 break;
674 case VT_EMPTY:
675 break;
676 default:
677 FIXME("Unknown property variant type\n");
678 break;
680 return ret;
683 LPWSTR msi_suminfo_dup_string( MSISUMMARYINFO *si, UINT uiProperty )
685 PROPVARIANT *prop;
687 if ( uiProperty >= MSI_MAX_PROPS )
688 return NULL;
689 prop = &si->property[uiProperty];
690 if( prop->vt != VT_LPSTR )
691 return NULL;
692 return strdupAtoW( prop->pszVal );
695 INT msi_suminfo_get_int32( MSISUMMARYINFO *si, UINT uiProperty )
697 PROPVARIANT *prop;
699 if ( uiProperty >= MSI_MAX_PROPS )
700 return -1;
701 prop = &si->property[uiProperty];
702 if( prop->vt != VT_I4 )
703 return -1;
704 return prop->lVal;
707 LPWSTR msi_get_suminfo_product( IStorage *stg )
709 MSISUMMARYINFO *si;
710 LPWSTR prod;
711 UINT r;
713 r = msi_get_suminfo( stg, 0, &si );
714 if (r != ERROR_SUCCESS)
716 ERR("no summary information!\n");
717 return NULL;
719 prod = msi_suminfo_dup_string( si, PID_REVNUMBER );
720 msiobj_release( &si->hdr );
721 return prod;
724 UINT WINAPI MsiSummaryInfoGetPropertyA( MSIHANDLE handle, UINT uiProperty, UINT *puiDataType, INT *piValue,
725 FILETIME *pftValue, char *szValueBuf, DWORD *pcchValueBuf )
727 MSISUMMARYINFO *si;
728 awstring str;
729 UINT r;
731 TRACE( "%lu, %u, %p, %p, %p, %p, %p\n", handle, uiProperty, puiDataType, piValue, pftValue, szValueBuf,
732 pcchValueBuf );
734 if (uiProperty >= MSI_MAX_PROPS)
736 if (puiDataType) *puiDataType = VT_EMPTY;
737 return ERROR_UNKNOWN_PROPERTY;
740 if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
742 MSIHANDLE remote;
743 WCHAR *buf = NULL;
745 if (!(remote = msi_get_remote( handle )))
746 return ERROR_INVALID_HANDLE;
748 __TRY
750 r = remote_SummaryInfoGetProperty( remote, uiProperty, puiDataType, piValue, pftValue, &buf );
752 __EXCEPT(rpc_filter)
754 r = GetExceptionCode();
756 __ENDTRY
758 if (!r && buf)
760 r = msi_strncpyWtoA( buf, -1, szValueBuf, pcchValueBuf, TRUE );
763 midl_user_free( buf );
764 return r;
767 str.unicode = FALSE;
768 str.str.a = szValueBuf;
770 r = get_prop( si, uiProperty, puiDataType, piValue, pftValue, &str, pcchValueBuf );
771 msiobj_release( &si->hdr );
772 return r;
775 UINT WINAPI MsiSummaryInfoGetPropertyW( MSIHANDLE handle, UINT uiProperty, UINT *puiDataType, INT *piValue,
776 FILETIME *pftValue, WCHAR *szValueBuf, DWORD *pcchValueBuf )
778 MSISUMMARYINFO *si;
779 awstring str;
780 UINT r;
782 TRACE( "%lu, %u, %p, %p, %p, %p, %p\n", handle, uiProperty, puiDataType, piValue, pftValue, szValueBuf,
783 pcchValueBuf );
785 if (uiProperty >= MSI_MAX_PROPS)
787 if (puiDataType) *puiDataType = VT_EMPTY;
788 return ERROR_UNKNOWN_PROPERTY;
791 if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
793 MSIHANDLE remote;
794 WCHAR *buf = NULL;
796 if (!(remote = msi_get_remote( handle )))
797 return ERROR_INVALID_HANDLE;
799 __TRY
801 r = remote_SummaryInfoGetProperty( remote, uiProperty, puiDataType, piValue, pftValue, &buf );
803 __EXCEPT(rpc_filter)
805 r = GetExceptionCode();
807 __ENDTRY
809 if (!r && buf)
810 r = msi_strncpyW( buf, -1, szValueBuf, pcchValueBuf );
812 midl_user_free( buf );
813 return r;
816 str.unicode = TRUE;
817 str.str.w = szValueBuf;
819 r = get_prop( si, uiProperty, puiDataType, piValue, pftValue, &str, pcchValueBuf );
820 msiobj_release( &si->hdr );
821 return r;
824 static UINT set_prop( MSISUMMARYINFO *si, UINT uiProperty, UINT type,
825 INT iValue, FILETIME *pftValue, awcstring *str )
827 PROPVARIANT *prop;
828 UINT len;
830 TRACE("%p, %u, %u, %d, %p, %p\n", si, uiProperty, type, iValue, pftValue, str );
832 prop = &si->property[uiProperty];
834 if( prop->vt == VT_EMPTY )
836 if( !si->update_count )
837 return ERROR_FUNCTION_FAILED;
839 si->update_count--;
841 else if( prop->vt != type )
842 return ERROR_SUCCESS;
844 free_prop( prop );
845 prop->vt = type;
846 switch( type )
848 case VT_I4:
849 prop->lVal = iValue;
850 break;
851 case VT_I2:
852 prop->iVal = iValue;
853 break;
854 case VT_FILETIME:
855 prop->filetime = *pftValue;
856 break;
857 case VT_LPSTR:
858 if( str->unicode )
860 len = WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
861 NULL, 0, NULL, NULL );
862 prop->pszVal = msi_alloc( len );
863 WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
864 prop->pszVal, len, NULL, NULL );
866 else
868 len = lstrlenA( str->str.a ) + 1;
869 prop->pszVal = msi_alloc( len );
870 lstrcpyA( prop->pszVal, str->str.a );
872 break;
875 return ERROR_SUCCESS;
878 static UINT msi_set_prop( MSISUMMARYINFO *si, UINT uiProperty, UINT uiDataType,
879 INT iValue, FILETIME *pftValue, awcstring *str )
881 UINT type = get_type( uiProperty );
882 if( type == VT_EMPTY || type != uiDataType )
883 return ERROR_DATATYPE_MISMATCH;
885 if( uiDataType == VT_LPSTR && !str->str.a )
886 return ERROR_INVALID_PARAMETER;
888 if( uiDataType == VT_FILETIME && !pftValue )
889 return ERROR_INVALID_PARAMETER;
891 return set_prop( si, uiProperty, type, iValue, pftValue, str );
894 UINT WINAPI MsiSummaryInfoSetPropertyW( MSIHANDLE handle, UINT uiProperty, UINT uiDataType, INT iValue,
895 FILETIME *pftValue, const WCHAR *szValue )
897 awcstring str;
898 MSISUMMARYINFO *si;
899 UINT ret;
901 TRACE( "%lu, %u, %u, %d, %p, %s\n", handle, uiProperty, uiDataType, iValue, pftValue, debugstr_w(szValue) );
903 if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
905 MSIHANDLE remote;
907 if ((remote = msi_get_remote( handle )))
909 WARN("MsiSummaryInfoSetProperty not allowed during a custom action!\n");
910 return ERROR_FUNCTION_FAILED;
913 return ERROR_INVALID_HANDLE;
916 str.unicode = TRUE;
917 str.str.w = szValue;
919 ret = msi_set_prop( si, uiProperty, uiDataType, iValue, pftValue, &str );
920 msiobj_release( &si->hdr );
921 return ret;
924 UINT WINAPI MsiSummaryInfoSetPropertyA( MSIHANDLE handle, UINT uiProperty, UINT uiDataType, INT iValue,
925 FILETIME *pftValue, const char *szValue )
927 awcstring str;
928 MSISUMMARYINFO *si;
929 UINT ret;
931 TRACE( "%lu, %u, %u, %d, %p, %s\n", handle, uiProperty, uiDataType, iValue, pftValue, debugstr_a(szValue) );
933 if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
935 MSIHANDLE remote;
937 if ((remote = msi_get_remote( handle )))
939 WARN("MsiSummaryInfoSetProperty not allowed during a custom action!\n");
940 return ERROR_FUNCTION_FAILED;
943 return ERROR_INVALID_HANDLE;
946 str.unicode = FALSE;
947 str.str.a = szValue;
949 ret = msi_set_prop( si, uiProperty, uiDataType, iValue, pftValue, &str );
950 msiobj_release( &si->hdr );
951 return ret;
954 static UINT suminfo_persist( MSISUMMARYINFO *si )
956 UINT ret = ERROR_FUNCTION_FAILED;
957 IStream *stm = NULL;
958 DWORD grfMode;
959 HRESULT r;
961 grfMode = STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
962 r = IStorage_CreateStream( si->storage, L"\5SummaryInformation", grfMode, 0, 0, &stm );
963 if( SUCCEEDED(r) )
965 ret = save_summary_info( si, stm );
966 IStream_Release( stm );
968 return ret;
971 static void parse_filetime( LPCWSTR str, FILETIME *ft )
973 SYSTEMTIME lt, utc;
974 const WCHAR *p = str;
975 WCHAR *end;
977 memset( &lt, 0, sizeof(lt) );
979 /* YYYY/MM/DD hh:mm:ss */
981 while (iswspace( *p )) p++;
983 lt.wYear = wcstol( p, &end, 10 );
984 if (*end != '/') return;
985 p = end + 1;
987 lt.wMonth = wcstol( p, &end, 10 );
988 if (*end != '/') return;
989 p = end + 1;
991 lt.wDay = wcstol( p, &end, 10 );
992 if (*end != ' ') return;
993 p = end + 1;
995 while (iswspace( *p )) p++;
997 lt.wHour = wcstol( p, &end, 10 );
998 if (*end != ':') return;
999 p = end + 1;
1001 lt.wMinute = wcstol( p, &end, 10 );
1002 if (*end != ':') return;
1003 p = end + 1;
1005 lt.wSecond = wcstol( p, &end, 10 );
1007 TzSpecificLocalTimeToSystemTime( NULL, &lt, &utc );
1008 SystemTimeToFileTime( &utc, ft );
1011 static UINT parse_prop( LPCWSTR prop, LPCWSTR value, UINT *pid, INT *int_value,
1012 FILETIME *ft_value, awcstring *str_value )
1014 *pid = wcstol( prop, NULL, 10 );
1015 switch (*pid)
1017 case PID_CODEPAGE:
1018 case PID_WORDCOUNT:
1019 case PID_CHARCOUNT:
1020 case PID_SECURITY:
1021 case PID_PAGECOUNT:
1022 *int_value = wcstol( value, NULL, 10 );
1023 break;
1025 case PID_LASTPRINTED:
1026 case PID_CREATE_DTM:
1027 case PID_LASTSAVE_DTM:
1028 parse_filetime( value, ft_value );
1029 break;
1031 case PID_SUBJECT:
1032 case PID_AUTHOR:
1033 case PID_KEYWORDS:
1034 case PID_COMMENTS:
1035 case PID_TEMPLATE:
1036 case PID_LASTAUTHOR:
1037 case PID_REVNUMBER:
1038 case PID_APPNAME:
1039 case PID_TITLE:
1040 str_value->str.w = value;
1041 str_value->unicode = TRUE;
1042 break;
1044 default:
1045 WARN("unhandled prop id %u\n", *pid);
1046 return ERROR_FUNCTION_FAILED;
1049 return ERROR_SUCCESS;
1052 UINT msi_add_suminfo( MSIDATABASE *db, LPWSTR **records, int num_records, int num_columns )
1054 UINT r;
1055 int i, j;
1056 MSISUMMARYINFO *si;
1058 r = msi_get_suminfo( db->storage, num_records * (num_columns / 2), &si );
1059 if (r != ERROR_SUCCESS)
1061 if (!(si = create_suminfo( db->storage, num_records * (num_columns / 2) )))
1062 return ERROR_OUTOFMEMORY;
1063 r = ERROR_SUCCESS;
1066 for (i = 0; i < num_records; i++)
1068 for (j = 0; j < num_columns; j += 2)
1070 UINT pid;
1071 INT int_value = 0;
1072 FILETIME ft_value;
1073 awcstring str_value;
1075 r = parse_prop( records[i][j], records[i][j + 1], &pid, &int_value, &ft_value, &str_value );
1076 if (r != ERROR_SUCCESS)
1077 goto end;
1079 r = set_prop( si, pid, get_type(pid), int_value, &ft_value, &str_value );
1080 if (r != ERROR_SUCCESS)
1081 goto end;
1085 end:
1086 if (r == ERROR_SUCCESS)
1087 r = suminfo_persist( si );
1089 msiobj_release( &si->hdr );
1090 return r;
1093 static UINT save_prop( MSISUMMARYINFO *si, HANDLE handle, UINT row )
1095 static const char fmt_systemtime[] = "%04u/%02u/%02u %02u:%02u:%02u";
1096 char data[36]; /* largest string: YYYY/MM/DD hh:mm:ss */
1097 static const char fmt_begin[] = "%u\t";
1098 static const char data_end[] = "\r\n";
1099 static const char fmt_int[] = "%u";
1100 UINT r, data_type;
1101 SYSTEMTIME system_time;
1102 FILETIME file_time;
1103 INT int_value;
1104 awstring str;
1105 DWORD len, sz;
1107 str.unicode = FALSE;
1108 str.str.a = NULL;
1109 len = 0;
1110 r = get_prop( si, row, &data_type, &int_value, &file_time, &str, &len );
1111 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
1112 return r;
1113 if (data_type == VT_EMPTY)
1114 return ERROR_SUCCESS; /* property not set */
1115 sz = sprintf( data, fmt_begin, row );
1116 if (!WriteFile( handle, data, sz, &sz, NULL ))
1117 return ERROR_WRITE_FAULT;
1119 switch( data_type )
1121 case VT_I2:
1122 case VT_I4:
1123 sz = sprintf( data, fmt_int, int_value );
1124 if (!WriteFile( handle, data, sz, &sz, NULL ))
1125 return ERROR_WRITE_FAULT;
1126 break;
1127 case VT_LPSTR:
1128 len++;
1129 if (!(str.str.a = msi_alloc( len )))
1130 return ERROR_OUTOFMEMORY;
1131 r = get_prop( si, row, NULL, NULL, NULL, &str, &len );
1132 if (r != ERROR_SUCCESS)
1134 msi_free( str.str.a );
1135 return r;
1137 sz = len;
1138 if (!WriteFile( handle, str.str.a, sz, &sz, NULL ))
1140 msi_free( str.str.a );
1141 return ERROR_WRITE_FAULT;
1143 msi_free( str.str.a );
1144 break;
1145 case VT_FILETIME:
1146 if (!FileTimeToSystemTime( &file_time, &system_time ))
1147 return ERROR_FUNCTION_FAILED;
1148 sz = sprintf( data, fmt_systemtime, system_time.wYear, system_time.wMonth,
1149 system_time.wDay, system_time.wHour, system_time.wMinute,
1150 system_time.wSecond );
1151 if (!WriteFile( handle, data, sz, &sz, NULL ))
1152 return ERROR_WRITE_FAULT;
1153 break;
1154 case VT_EMPTY:
1155 /* cannot reach here, property not set */
1156 break;
1157 default:
1158 FIXME( "Unknown property variant type\n" );
1159 return ERROR_FUNCTION_FAILED;
1162 sz = ARRAY_SIZE(data_end) - 1;
1163 if (!WriteFile( handle, data_end, sz, &sz, NULL ))
1164 return ERROR_WRITE_FAULT;
1166 return ERROR_SUCCESS;
1169 UINT msi_export_suminfo( MSIDATABASE *db, HANDLE handle )
1171 UINT i, r, num_rows;
1172 MSISUMMARYINFO *si;
1174 r = msi_get_suminfo( db->storage, 0, &si );
1175 if (r != ERROR_SUCCESS)
1176 r = msi_get_db_suminfo( db, 0, &si );
1177 if (r != ERROR_SUCCESS)
1178 return r;
1180 num_rows = get_property_count( si->property );
1181 if (!num_rows)
1183 msiobj_release( &si->hdr );
1184 return ERROR_FUNCTION_FAILED;
1187 for (i = 0; i < num_rows; i++)
1189 r = save_prop( si, handle, i );
1190 if (r != ERROR_SUCCESS)
1192 msiobj_release( &si->hdr );
1193 return r;
1197 msiobj_release( &si->hdr );
1198 return ERROR_SUCCESS;
1201 UINT WINAPI MsiSummaryInfoPersist( MSIHANDLE handle )
1203 MSISUMMARYINFO *si;
1204 UINT ret;
1206 TRACE( "%lu\n", handle );
1208 si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
1209 if( !si )
1210 return ERROR_INVALID_HANDLE;
1212 ret = suminfo_persist( si );
1214 msiobj_release( &si->hdr );
1215 return ret;
1218 UINT WINAPI MsiCreateTransformSummaryInfoA( MSIHANDLE db, MSIHANDLE db_ref, const char *transform, int error,
1219 int validation )
1221 UINT r;
1222 WCHAR *transformW = NULL;
1224 TRACE( "%lu, %lu, %s, %d, %d\n", db, db_ref, debugstr_a(transform), error, validation );
1226 if (transform && !(transformW = strdupAtoW( transform )))
1227 return ERROR_OUTOFMEMORY;
1229 r = MsiCreateTransformSummaryInfoW( db, db_ref, transformW, error, validation );
1230 msi_free( transformW );
1231 return r;
1234 UINT WINAPI MsiCreateTransformSummaryInfoW( MSIHANDLE db, MSIHANDLE db_ref, const WCHAR *transform, int error,
1235 int validation )
1237 FIXME( "%lu, %lu, %s, %d, %d\n", db, db_ref, debugstr_w(transform), error, validation );
1238 return ERROR_FUNCTION_FAILED;
1241 UINT msi_load_suminfo_properties( MSIPACKAGE *package )
1243 MSISUMMARYINFO *si;
1244 WCHAR *package_code;
1245 UINT r;
1246 DWORD len;
1247 awstring str;
1248 INT count;
1250 r = msi_get_suminfo( package->db->storage, 0, &si );
1251 if (r != ERROR_SUCCESS)
1253 r = msi_get_db_suminfo( package->db, 0, &si );
1254 if (r != ERROR_SUCCESS)
1256 ERR("Unable to open summary information stream %u\n", r);
1257 return r;
1261 str.unicode = TRUE;
1262 str.str.w = NULL;
1263 len = 0;
1264 r = get_prop( si, PID_REVNUMBER, NULL, NULL, NULL, &str, &len );
1265 if (r != ERROR_MORE_DATA)
1267 WARN("Unable to query revision number %u\n", r);
1268 msiobj_release( &si->hdr );
1269 return ERROR_FUNCTION_FAILED;
1272 len++;
1273 if (!(package_code = msi_alloc( len * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
1274 str.str.w = package_code;
1276 r = get_prop( si, PID_REVNUMBER, NULL, NULL, NULL, &str, &len );
1277 if (r != ERROR_SUCCESS)
1279 msi_free( package_code );
1280 msiobj_release( &si->hdr );
1281 return r;
1284 r = msi_set_property( package->db, L"PackageCode", package_code, len );
1285 msi_free( package_code );
1287 count = 0;
1288 get_prop( si, PID_WORDCOUNT, NULL, &count, NULL, NULL, NULL );
1289 package->WordCount = count;
1291 msiobj_release( &si->hdr );
1292 return r;
1295 UINT __cdecl s_remote_SummaryInfoGetPropertyCount( MSIHANDLE suminfo, UINT *count )
1297 return MsiSummaryInfoGetPropertyCount( suminfo, count );
1300 UINT __cdecl s_remote_SummaryInfoGetProperty( MSIHANDLE suminfo, UINT property, UINT *type,
1301 INT *value, FILETIME *ft, LPWSTR *buf )
1303 WCHAR empty[1];
1304 DWORD size = 0;
1305 UINT r;
1307 r = MsiSummaryInfoGetPropertyW( suminfo, property, type, value, ft, empty, &size );
1308 if (r == ERROR_MORE_DATA)
1310 size++;
1311 *buf = midl_user_allocate( size * sizeof(WCHAR) );
1312 if (!*buf) return ERROR_OUTOFMEMORY;
1313 r = MsiSummaryInfoGetPropertyW( suminfo, property, type, value, ft, *buf, &size );
1315 return r;