winedbg: Don't dereference possibly NULL thread pointer.
[wine/zf.git] / dlls / msi / suminfo.c
blob19317713a3cb2659dff918045eddb2910b733573
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->u.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 %d\n", idofs[i].propid );
188 break;
191 type = get_type( idofs[i].propid );
192 if( type == VT_EMPTY )
194 ERR("propid %d 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.u.pszVal = str;
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;
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 %d\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 %d %d\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->u.iVal );
348 break;
349 case VT_I4:
350 sz += write_dword( data, sz, prop->u.lVal );
351 break;
352 case VT_FILETIME:
353 sz += write_filetime( data, sz, &prop->u.filetime );
354 break;
355 case VT_LPSTR:
356 sz += write_string( data, sz, prop->u.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,
502 LPCWSTR szDatabase, UINT uiUpdateCount, MSIHANDLE *pHandle )
504 MSISUMMARYINFO *si;
505 MSIDATABASE *db;
506 UINT ret;
508 TRACE("%d %s %d %p\n", hDatabase, debugstr_w(szDatabase),
509 uiUpdateCount, pHandle);
511 if( !pHandle )
512 return ERROR_INVALID_PARAMETER;
514 if( szDatabase && szDatabase[0] )
516 LPCWSTR persist = uiUpdateCount ? MSIDBOPEN_TRANSACT : MSIDBOPEN_READONLY;
518 ret = MSI_OpenDatabaseW( szDatabase, persist, &db );
519 if( ret != ERROR_SUCCESS )
520 return ret;
522 else
524 db = msihandle2msiinfo( hDatabase, MSIHANDLETYPE_DATABASE );
525 if( !db )
527 MSIHANDLE remote, remote_suminfo;
529 if (!(remote = msi_get_remote(hDatabase)))
530 return ERROR_INVALID_HANDLE;
532 __TRY
534 ret = remote_DatabaseGetSummaryInformation(remote, uiUpdateCount, &remote_suminfo);
536 __EXCEPT(rpc_filter)
538 ret = GetExceptionCode();
540 __ENDTRY
542 if (!ret)
543 *pHandle = alloc_msi_remote_handle(remote_suminfo);
545 return ret;
549 ret = msi_get_suminfo( db->storage, uiUpdateCount, &si );
550 if (ret != ERROR_SUCCESS)
551 ret = msi_get_db_suminfo( db, uiUpdateCount, &si );
552 if (ret != ERROR_SUCCESS)
554 if ((si = create_suminfo( db->storage, uiUpdateCount )))
555 ret = ERROR_SUCCESS;
558 if (ret == ERROR_SUCCESS)
560 *pHandle = alloc_msihandle( &si->hdr );
561 if( *pHandle )
562 ret = ERROR_SUCCESS;
563 else
564 ret = ERROR_NOT_ENOUGH_MEMORY;
565 msiobj_release( &si->hdr );
568 msiobj_release( &db->hdr );
569 return ret;
572 UINT WINAPI MsiGetSummaryInformationA(MSIHANDLE hDatabase,
573 LPCSTR szDatabase, UINT uiUpdateCount, MSIHANDLE *pHandle)
575 LPWSTR szwDatabase = NULL;
576 UINT ret;
578 TRACE("%d %s %d %p\n", hDatabase, debugstr_a(szDatabase),
579 uiUpdateCount, pHandle);
581 if( szDatabase )
583 szwDatabase = strdupAtoW( szDatabase );
584 if( !szwDatabase )
585 return ERROR_FUNCTION_FAILED;
588 ret = MsiGetSummaryInformationW(hDatabase, szwDatabase, uiUpdateCount, pHandle);
590 msi_free( szwDatabase );
592 return ret;
595 UINT WINAPI MsiSummaryInfoGetPropertyCount(MSIHANDLE hSummaryInfo, PUINT pCount)
597 MSISUMMARYINFO *si;
599 TRACE("%d %p\n", hSummaryInfo, pCount);
601 si = msihandle2msiinfo( hSummaryInfo, MSIHANDLETYPE_SUMMARYINFO );
602 if( !si )
604 MSIHANDLE remote;
605 UINT ret;
607 if (!(remote = msi_get_remote( hSummaryInfo )))
608 return ERROR_INVALID_HANDLE;
610 __TRY
612 ret = remote_SummaryInfoGetPropertyCount( remote, pCount );
614 __EXCEPT(rpc_filter)
616 ret = GetExceptionCode();
618 __ENDTRY
620 return ret;
623 if( pCount )
624 *pCount = get_property_count( si->property );
625 msiobj_release( &si->hdr );
627 return ERROR_SUCCESS;
630 static UINT get_prop( MSISUMMARYINFO *si, UINT uiProperty, UINT *puiDataType, INT *piValue,
631 FILETIME *pftValue, awstring *str, DWORD *pcchValueBuf)
633 PROPVARIANT *prop;
634 UINT ret = ERROR_SUCCESS;
636 prop = &si->property[uiProperty];
638 if( puiDataType )
639 *puiDataType = prop->vt;
641 switch( prop->vt )
643 case VT_I2:
644 if( piValue )
645 *piValue = prop->u.iVal;
646 break;
647 case VT_I4:
648 if( piValue )
649 *piValue = prop->u.lVal;
650 break;
651 case VT_LPSTR:
652 if( pcchValueBuf )
654 DWORD len = 0;
656 if( str->unicode )
658 len = MultiByteToWideChar( CP_ACP, 0, prop->u.pszVal, -1, NULL, 0 ) - 1;
659 MultiByteToWideChar( CP_ACP, 0, prop->u.pszVal, -1, str->str.w, *pcchValueBuf );
661 else
663 len = lstrlenA( prop->u.pszVal );
664 if( str->str.a )
665 lstrcpynA(str->str.a, prop->u.pszVal, *pcchValueBuf );
667 if (len >= *pcchValueBuf)
668 ret = ERROR_MORE_DATA;
669 *pcchValueBuf = len;
671 break;
672 case VT_FILETIME:
673 if( pftValue )
674 *pftValue = prop->u.filetime;
675 break;
676 case VT_EMPTY:
677 break;
678 default:
679 FIXME("Unknown property variant type\n");
680 break;
682 return ret;
685 LPWSTR msi_suminfo_dup_string( MSISUMMARYINFO *si, UINT uiProperty )
687 PROPVARIANT *prop;
689 if ( uiProperty >= MSI_MAX_PROPS )
690 return NULL;
691 prop = &si->property[uiProperty];
692 if( prop->vt != VT_LPSTR )
693 return NULL;
694 return strdupAtoW( prop->u.pszVal );
697 INT msi_suminfo_get_int32( MSISUMMARYINFO *si, UINT uiProperty )
699 PROPVARIANT *prop;
701 if ( uiProperty >= MSI_MAX_PROPS )
702 return -1;
703 prop = &si->property[uiProperty];
704 if( prop->vt != VT_I4 )
705 return -1;
706 return prop->u.lVal;
709 LPWSTR msi_get_suminfo_product( IStorage *stg )
711 MSISUMMARYINFO *si;
712 LPWSTR prod;
713 UINT r;
715 r = msi_get_suminfo( stg, 0, &si );
716 if (r != ERROR_SUCCESS)
718 ERR("no summary information!\n");
719 return NULL;
721 prod = msi_suminfo_dup_string( si, PID_REVNUMBER );
722 msiobj_release( &si->hdr );
723 return prod;
726 UINT WINAPI MsiSummaryInfoGetPropertyA(
727 MSIHANDLE handle, UINT uiProperty, PUINT puiDataType, LPINT piValue,
728 FILETIME *pftValue, LPSTR szValueBuf, LPDWORD pcchValueBuf)
730 MSISUMMARYINFO *si;
731 awstring str;
732 UINT r;
734 TRACE("%u, %u, %p, %p, %p, %p, %p\n", handle, uiProperty, puiDataType,
735 piValue, pftValue, szValueBuf, pcchValueBuf );
737 if (uiProperty >= MSI_MAX_PROPS)
739 if (puiDataType) *puiDataType = VT_EMPTY;
740 return ERROR_UNKNOWN_PROPERTY;
743 if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
745 MSIHANDLE remote;
746 WCHAR *buf = NULL;
748 if (!(remote = msi_get_remote( handle )))
749 return ERROR_INVALID_HANDLE;
751 __TRY
753 r = remote_SummaryInfoGetProperty( remote, uiProperty, puiDataType, piValue, pftValue, &buf );
755 __EXCEPT(rpc_filter)
757 r = GetExceptionCode();
759 __ENDTRY
761 if (!r && buf)
763 r = msi_strncpyWtoA( buf, -1, szValueBuf, pcchValueBuf, TRUE );
766 midl_user_free( buf );
767 return r;
770 str.unicode = FALSE;
771 str.str.a = szValueBuf;
773 r = get_prop( si, uiProperty, puiDataType, piValue, pftValue, &str, pcchValueBuf );
774 msiobj_release( &si->hdr );
775 return r;
778 UINT WINAPI MsiSummaryInfoGetPropertyW(
779 MSIHANDLE handle, UINT uiProperty, PUINT puiDataType, LPINT piValue,
780 FILETIME *pftValue, LPWSTR szValueBuf, LPDWORD pcchValueBuf)
782 MSISUMMARYINFO *si;
783 awstring str;
784 UINT r;
786 TRACE("%u, %u, %p, %p, %p, %p, %p\n", handle, uiProperty, puiDataType,
787 piValue, pftValue, szValueBuf, pcchValueBuf );
789 if (uiProperty >= MSI_MAX_PROPS)
791 if (puiDataType) *puiDataType = VT_EMPTY;
792 return ERROR_UNKNOWN_PROPERTY;
795 if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
797 MSIHANDLE remote;
798 WCHAR *buf = NULL;
800 if (!(remote = msi_get_remote( handle )))
801 return ERROR_INVALID_HANDLE;
803 __TRY
805 r = remote_SummaryInfoGetProperty( remote, uiProperty, puiDataType, piValue, pftValue, &buf );
807 __EXCEPT(rpc_filter)
809 r = GetExceptionCode();
811 __ENDTRY
813 if (!r && buf)
814 r = msi_strncpyW( buf, -1, szValueBuf, pcchValueBuf );
816 midl_user_free( buf );
817 return r;
820 str.unicode = TRUE;
821 str.str.w = szValueBuf;
823 r = get_prop( si, uiProperty, puiDataType, piValue, pftValue, &str, pcchValueBuf );
824 msiobj_release( &si->hdr );
825 return r;
828 static UINT set_prop( MSISUMMARYINFO *si, UINT uiProperty, UINT type,
829 INT iValue, FILETIME *pftValue, awcstring *str )
831 PROPVARIANT *prop;
832 UINT len;
834 TRACE("%p, %u, %u, %d, %p, %p\n", si, uiProperty, type, iValue, pftValue, str );
836 prop = &si->property[uiProperty];
838 if( prop->vt == VT_EMPTY )
840 if( !si->update_count )
841 return ERROR_FUNCTION_FAILED;
843 si->update_count--;
845 else if( prop->vt != type )
846 return ERROR_SUCCESS;
848 free_prop( prop );
849 prop->vt = type;
850 switch( type )
852 case VT_I4:
853 prop->u.lVal = iValue;
854 break;
855 case VT_I2:
856 prop->u.iVal = iValue;
857 break;
858 case VT_FILETIME:
859 prop->u.filetime = *pftValue;
860 break;
861 case VT_LPSTR:
862 if( str->unicode )
864 len = WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
865 NULL, 0, NULL, NULL );
866 prop->u.pszVal = msi_alloc( len );
867 WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
868 prop->u.pszVal, len, NULL, NULL );
870 else
872 len = lstrlenA( str->str.a ) + 1;
873 prop->u.pszVal = msi_alloc( len );
874 lstrcpyA( prop->u.pszVal, str->str.a );
876 break;
879 return ERROR_SUCCESS;
882 static UINT msi_set_prop( MSISUMMARYINFO *si, UINT uiProperty, UINT uiDataType,
883 INT iValue, FILETIME *pftValue, awcstring *str )
885 UINT type = get_type( uiProperty );
886 if( type == VT_EMPTY || type != uiDataType )
887 return ERROR_DATATYPE_MISMATCH;
889 if( uiDataType == VT_LPSTR && !str->str.a )
890 return ERROR_INVALID_PARAMETER;
892 if( uiDataType == VT_FILETIME && !pftValue )
893 return ERROR_INVALID_PARAMETER;
895 return set_prop( si, uiProperty, type, iValue, pftValue, str );
898 UINT WINAPI MsiSummaryInfoSetPropertyW( MSIHANDLE handle, UINT uiProperty, UINT uiDataType,
899 INT iValue, FILETIME *pftValue, LPCWSTR szValue )
901 awcstring str;
902 MSISUMMARYINFO *si;
903 UINT ret;
905 TRACE("%u, %u, %u, %d, %p, %s\n", handle, uiProperty, uiDataType, iValue, pftValue,
906 debugstr_w(szValue) );
908 if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
910 MSIHANDLE remote;
912 if ((remote = msi_get_remote( handle )))
914 WARN("MsiSummaryInfoSetProperty not allowed during a custom action!\n");
915 return ERROR_FUNCTION_FAILED;
918 return ERROR_INVALID_HANDLE;
921 str.unicode = TRUE;
922 str.str.w = szValue;
924 ret = msi_set_prop( si, uiProperty, uiDataType, iValue, pftValue, &str );
925 msiobj_release( &si->hdr );
926 return ret;
929 UINT WINAPI MsiSummaryInfoSetPropertyA( MSIHANDLE handle, UINT uiProperty, UINT uiDataType,
930 INT iValue, FILETIME *pftValue, LPCSTR szValue )
932 awcstring str;
933 MSISUMMARYINFO *si;
934 UINT ret;
936 TRACE("%u, %u, %u, %d, %p, %s\n", handle, uiProperty, uiDataType, iValue, pftValue,
937 debugstr_a(szValue) );
939 if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
941 MSIHANDLE remote;
943 if ((remote = msi_get_remote( handle )))
945 WARN("MsiSummaryInfoSetProperty not allowed during a custom action!\n");
946 return ERROR_FUNCTION_FAILED;
949 return ERROR_INVALID_HANDLE;
952 str.unicode = FALSE;
953 str.str.a = szValue;
955 ret = msi_set_prop( si, uiProperty, uiDataType, iValue, pftValue, &str );
956 msiobj_release( &si->hdr );
957 return ret;
960 static UINT suminfo_persist( MSISUMMARYINFO *si )
962 UINT ret = ERROR_FUNCTION_FAILED;
963 IStream *stm = NULL;
964 DWORD grfMode;
965 HRESULT r;
967 grfMode = STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
968 r = IStorage_CreateStream( si->storage, L"\5SummaryInformation", grfMode, 0, 0, &stm );
969 if( SUCCEEDED(r) )
971 ret = save_summary_info( si, stm );
972 IStream_Release( stm );
974 return ret;
977 static void parse_filetime( LPCWSTR str, FILETIME *ft )
979 SYSTEMTIME lt, utc;
980 const WCHAR *p = str;
981 WCHAR *end;
983 memset( &lt, 0, sizeof(lt) );
985 /* YYYY/MM/DD hh:mm:ss */
987 while (iswspace( *p )) p++;
989 lt.wYear = wcstol( p, &end, 10 );
990 if (*end != '/') return;
991 p = end + 1;
993 lt.wMonth = wcstol( p, &end, 10 );
994 if (*end != '/') return;
995 p = end + 1;
997 lt.wDay = wcstol( p, &end, 10 );
998 if (*end != ' ') return;
999 p = end + 1;
1001 while (iswspace( *p )) p++;
1003 lt.wHour = wcstol( p, &end, 10 );
1004 if (*end != ':') return;
1005 p = end + 1;
1007 lt.wMinute = wcstol( p, &end, 10 );
1008 if (*end != ':') return;
1009 p = end + 1;
1011 lt.wSecond = wcstol( p, &end, 10 );
1013 TzSpecificLocalTimeToSystemTime( NULL, &lt, &utc );
1014 SystemTimeToFileTime( &utc, ft );
1017 static UINT parse_prop( LPCWSTR prop, LPCWSTR value, UINT *pid, INT *int_value,
1018 FILETIME *ft_value, awcstring *str_value )
1020 *pid = wcstol( prop, NULL, 10 );
1021 switch (*pid)
1023 case PID_CODEPAGE:
1024 case PID_WORDCOUNT:
1025 case PID_CHARCOUNT:
1026 case PID_SECURITY:
1027 case PID_PAGECOUNT:
1028 *int_value = wcstol( value, NULL, 10 );
1029 break;
1031 case PID_LASTPRINTED:
1032 case PID_CREATE_DTM:
1033 case PID_LASTSAVE_DTM:
1034 parse_filetime( value, ft_value );
1035 break;
1037 case PID_SUBJECT:
1038 case PID_AUTHOR:
1039 case PID_KEYWORDS:
1040 case PID_COMMENTS:
1041 case PID_TEMPLATE:
1042 case PID_LASTAUTHOR:
1043 case PID_REVNUMBER:
1044 case PID_APPNAME:
1045 case PID_TITLE:
1046 str_value->str.w = value;
1047 str_value->unicode = TRUE;
1048 break;
1050 default:
1051 WARN("unhandled prop id %u\n", *pid);
1052 return ERROR_FUNCTION_FAILED;
1055 return ERROR_SUCCESS;
1058 UINT msi_add_suminfo( MSIDATABASE *db, LPWSTR **records, int num_records, int num_columns )
1060 UINT r;
1061 int i, j;
1062 MSISUMMARYINFO *si;
1064 r = msi_get_suminfo( db->storage, num_records * (num_columns / 2), &si );
1065 if (r != ERROR_SUCCESS)
1067 if (!(si = create_suminfo( db->storage, num_records * (num_columns / 2) )))
1068 return ERROR_OUTOFMEMORY;
1069 r = ERROR_SUCCESS;
1072 for (i = 0; i < num_records; i++)
1074 for (j = 0; j < num_columns; j += 2)
1076 UINT pid;
1077 INT int_value = 0;
1078 FILETIME ft_value;
1079 awcstring str_value;
1081 r = parse_prop( records[i][j], records[i][j + 1], &pid, &int_value, &ft_value, &str_value );
1082 if (r != ERROR_SUCCESS)
1083 goto end;
1085 r = set_prop( si, pid, get_type(pid), int_value, &ft_value, &str_value );
1086 if (r != ERROR_SUCCESS)
1087 goto end;
1091 end:
1092 if (r == ERROR_SUCCESS)
1093 r = suminfo_persist( si );
1095 msiobj_release( &si->hdr );
1096 return r;
1099 static UINT save_prop( MSISUMMARYINFO *si, HANDLE handle, UINT row )
1101 static const char fmt_systemtime[] = "%04u/%02u/%02u %02u:%02u:%02u";
1102 char data[36]; /* largest string: YYYY/MM/DD hh:mm:ss */
1103 static const char fmt_begin[] = "%u\t";
1104 static const char data_end[] = "\r\n";
1105 static const char fmt_int[] = "%u";
1106 UINT r, data_type, len;
1107 SYSTEMTIME system_time;
1108 FILETIME file_time;
1109 INT int_value;
1110 awstring str;
1111 DWORD sz;
1113 str.unicode = FALSE;
1114 str.str.a = NULL;
1115 len = 0;
1116 r = get_prop( si, row, &data_type, &int_value, &file_time, &str, &len );
1117 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
1118 return r;
1119 if (data_type == VT_EMPTY)
1120 return ERROR_SUCCESS; /* property not set */
1121 sz = sprintf( data, fmt_begin, row );
1122 if (!WriteFile( handle, data, sz, &sz, NULL ))
1123 return ERROR_WRITE_FAULT;
1125 switch( data_type )
1127 case VT_I2:
1128 case VT_I4:
1129 sz = sprintf( data, fmt_int, int_value );
1130 if (!WriteFile( handle, data, sz, &sz, NULL ))
1131 return ERROR_WRITE_FAULT;
1132 break;
1133 case VT_LPSTR:
1134 len++;
1135 if (!(str.str.a = msi_alloc( len )))
1136 return ERROR_OUTOFMEMORY;
1137 r = get_prop( si, row, NULL, NULL, NULL, &str, &len );
1138 if (r != ERROR_SUCCESS)
1140 msi_free( str.str.a );
1141 return r;
1143 sz = len;
1144 if (!WriteFile( handle, str.str.a, sz, &sz, NULL ))
1146 msi_free( str.str.a );
1147 return ERROR_WRITE_FAULT;
1149 msi_free( str.str.a );
1150 break;
1151 case VT_FILETIME:
1152 if (!FileTimeToSystemTime( &file_time, &system_time ))
1153 return ERROR_FUNCTION_FAILED;
1154 sz = sprintf( data, fmt_systemtime, system_time.wYear, system_time.wMonth,
1155 system_time.wDay, system_time.wHour, system_time.wMinute,
1156 system_time.wSecond );
1157 if (!WriteFile( handle, data, sz, &sz, NULL ))
1158 return ERROR_WRITE_FAULT;
1159 break;
1160 case VT_EMPTY:
1161 /* cannot reach here, property not set */
1162 break;
1163 default:
1164 FIXME( "Unknown property variant type\n" );
1165 return ERROR_FUNCTION_FAILED;
1168 sz = ARRAY_SIZE(data_end) - 1;
1169 if (!WriteFile( handle, data_end, sz, &sz, NULL ))
1170 return ERROR_WRITE_FAULT;
1172 return ERROR_SUCCESS;
1175 UINT msi_export_suminfo( MSIDATABASE *db, HANDLE handle )
1177 UINT i, r, num_rows;
1178 MSISUMMARYINFO *si;
1180 r = msi_get_suminfo( db->storage, 0, &si );
1181 if (r != ERROR_SUCCESS)
1182 r = msi_get_db_suminfo( db, 0, &si );
1183 if (r != ERROR_SUCCESS)
1184 return r;
1186 num_rows = get_property_count( si->property );
1187 if (!num_rows)
1189 msiobj_release( &si->hdr );
1190 return ERROR_FUNCTION_FAILED;
1193 for (i = 0; i < num_rows; i++)
1195 r = save_prop( si, handle, i );
1196 if (r != ERROR_SUCCESS)
1198 msiobj_release( &si->hdr );
1199 return r;
1203 msiobj_release( &si->hdr );
1204 return ERROR_SUCCESS;
1207 UINT WINAPI MsiSummaryInfoPersist( MSIHANDLE handle )
1209 MSISUMMARYINFO *si;
1210 UINT ret;
1212 TRACE("%d\n", handle );
1214 si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
1215 if( !si )
1216 return ERROR_INVALID_HANDLE;
1218 ret = suminfo_persist( si );
1220 msiobj_release( &si->hdr );
1221 return ret;
1224 UINT WINAPI MsiCreateTransformSummaryInfoA( MSIHANDLE db, MSIHANDLE db_ref, LPCSTR transform, int error, int validation )
1226 UINT r;
1227 WCHAR *transformW = NULL;
1229 TRACE("%u, %u, %s, %d, %d\n", db, db_ref, debugstr_a(transform), error, validation);
1231 if (transform && !(transformW = strdupAtoW( transform )))
1232 return ERROR_OUTOFMEMORY;
1234 r = MsiCreateTransformSummaryInfoW( db, db_ref, transformW, error, validation );
1235 msi_free( transformW );
1236 return r;
1239 UINT WINAPI MsiCreateTransformSummaryInfoW( MSIHANDLE db, MSIHANDLE db_ref, LPCWSTR transform, int error, int validation )
1241 FIXME("%u, %u, %s, %d, %d\n", db, db_ref, debugstr_w(transform), error, validation);
1242 return ERROR_FUNCTION_FAILED;
1245 UINT msi_load_suminfo_properties( MSIPACKAGE *package )
1247 MSISUMMARYINFO *si;
1248 WCHAR *package_code;
1249 UINT r, len;
1250 awstring str;
1251 INT count;
1253 r = msi_get_suminfo( package->db->storage, 0, &si );
1254 if (r != ERROR_SUCCESS)
1256 r = msi_get_db_suminfo( package->db, 0, &si );
1257 if (r != ERROR_SUCCESS)
1259 ERR("Unable to open summary information stream %u\n", r);
1260 return r;
1264 str.unicode = TRUE;
1265 str.str.w = NULL;
1266 len = 0;
1267 r = get_prop( si, PID_REVNUMBER, NULL, NULL, NULL, &str, &len );
1268 if (r != ERROR_MORE_DATA)
1270 WARN("Unable to query revision number %u\n", r);
1271 msiobj_release( &si->hdr );
1272 return ERROR_FUNCTION_FAILED;
1275 len++;
1276 if (!(package_code = msi_alloc( len * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
1277 str.str.w = package_code;
1279 r = get_prop( si, PID_REVNUMBER, NULL, NULL, NULL, &str, &len );
1280 if (r != ERROR_SUCCESS)
1282 msi_free( package_code );
1283 msiobj_release( &si->hdr );
1284 return r;
1287 r = msi_set_property( package->db, L"PackageCode", package_code, len );
1288 msi_free( package_code );
1290 count = 0;
1291 get_prop( si, PID_WORDCOUNT, NULL, &count, NULL, NULL, NULL );
1292 package->WordCount = count;
1294 msiobj_release( &si->hdr );
1295 return r;
1298 UINT __cdecl s_remote_SummaryInfoGetPropertyCount( MSIHANDLE suminfo, UINT *count )
1300 return MsiSummaryInfoGetPropertyCount( suminfo, count );
1303 UINT __cdecl s_remote_SummaryInfoGetProperty( MSIHANDLE suminfo, UINT property, UINT *type,
1304 INT *value, FILETIME *ft, LPWSTR *buf )
1306 WCHAR empty[1];
1307 DWORD size = 0;
1308 UINT r;
1310 r = MsiSummaryInfoGetPropertyW( suminfo, property, type, value, ft, empty, &size );
1311 if (r == ERROR_MORE_DATA)
1313 size++;
1314 *buf = midl_user_allocate( size * sizeof(WCHAR) );
1315 if (!*buf) return ERROR_OUTOFMEMORY;
1316 r = MsiSummaryInfoGetPropertyW( suminfo, property, type, value, ft, *buf, &size );
1318 return r;