Remove unused NONAMELESS defines.
[wine.git] / dlls / msi / suminfo.c
blob93ecc9f5e04baddaa3f949e98aa869d013e94a2e
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 #include "stdio.h"
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "winnls.h"
29 #include "shlwapi.h"
30 #include "wine/debug.h"
31 #include "wine/exception.h"
32 #include "msi.h"
33 #include "msiquery.h"
34 #include "msidefs.h"
35 #include "objidl.h"
36 #include "propvarutil.h"
38 #include "msipriv.h"
39 #include "winemsi.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
43 #include "pshpack1.h"
45 struct property_set_header
47 WORD wByteOrder;
48 WORD wFormat;
49 DWORD dwOSVer;
50 CLSID clsID;
51 DWORD reserved;
54 struct format_id_offset
56 FMTID fmtid;
57 DWORD dwOffset;
60 struct property_section_header
62 DWORD cbSection;
63 DWORD cProperties;
66 struct property_id_offset
68 DWORD propid;
69 DWORD dwOffset;
72 struct property_data
74 DWORD type;
75 union {
76 INT i4;
77 SHORT i2;
78 FILETIME ft;
79 struct {
80 DWORD len;
81 BYTE str[1];
82 } str;
83 } u;
86 #include "poppack.h"
88 static HRESULT (WINAPI *pPropVariantChangeType)
89 (PROPVARIANT *ppropvarDest, REFPROPVARIANT propvarSrc,
90 PROPVAR_CHANGE_FLAGS flags, VARTYPE vt);
92 #define SECT_HDR_SIZE (sizeof(struct property_section_header))
94 static void free_prop( PROPVARIANT *prop )
96 if (prop->vt == VT_LPSTR )
97 free( prop->pszVal );
98 prop->vt = VT_EMPTY;
101 static void MSI_CloseSummaryInfo( MSIOBJECTHDR *arg )
103 MSISUMMARYINFO *si = (MSISUMMARYINFO *) arg;
104 DWORD i;
106 for( i = 0; i < MSI_MAX_PROPS; i++ )
107 free_prop( &si->property[i] );
108 IStorage_Release( si->storage );
111 static UINT get_type( UINT uiProperty )
113 switch( uiProperty )
115 case PID_CODEPAGE:
116 return VT_I2;
118 case PID_SUBJECT:
119 case PID_AUTHOR:
120 case PID_KEYWORDS:
121 case PID_COMMENTS:
122 case PID_TEMPLATE:
123 case PID_LASTAUTHOR:
124 case PID_REVNUMBER:
125 case PID_APPNAME:
126 case PID_TITLE:
127 return VT_LPSTR;
129 case PID_LASTPRINTED:
130 case PID_CREATE_DTM:
131 case PID_LASTSAVE_DTM:
132 return VT_FILETIME;
134 case PID_WORDCOUNT:
135 case PID_CHARCOUNT:
136 case PID_SECURITY:
137 case PID_PAGECOUNT:
138 return VT_I4;
140 return VT_EMPTY;
143 static UINT get_property_count( const PROPVARIANT *property )
145 UINT i, n = 0;
147 if( !property )
148 return n;
149 for( i = 0; i < MSI_MAX_PROPS; i++ )
150 if( property[i].vt != VT_EMPTY )
151 n++;
152 return n;
155 static UINT propvar_changetype(PROPVARIANT *changed, PROPVARIANT *property, VARTYPE vt)
157 HRESULT hr;
158 HMODULE propsys = LoadLibraryA("propsys.dll");
159 pPropVariantChangeType = (void *)GetProcAddress(propsys, "PropVariantChangeType");
161 if (!pPropVariantChangeType)
163 ERR("PropVariantChangeType function missing!\n");
164 return ERROR_FUNCTION_FAILED;
167 hr = pPropVariantChangeType(changed, property, 0, vt);
168 return (hr == S_OK) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
171 /* FIXME: doesn't deal with endian conversion */
172 static void read_properties_from_data( PROPVARIANT *prop, LPBYTE data, DWORD sz )
174 UINT type;
175 DWORD i, size;
176 struct property_data *propdata;
177 PROPVARIANT property, *ptr;
178 PROPVARIANT changed;
179 struct property_id_offset *idofs;
180 struct property_section_header *section_hdr;
182 section_hdr = (struct property_section_header *) &data[0];
183 idofs = (struct property_id_offset *)&data[SECT_HDR_SIZE];
185 /* now set all the properties */
186 for( i = 0; i < section_hdr->cProperties; i++ )
188 if( idofs[i].propid >= MSI_MAX_PROPS )
190 ERR( "unknown property ID %lu\n", idofs[i].propid );
191 break;
194 type = get_type( idofs[i].propid );
195 if( type == VT_EMPTY )
197 ERR( "propid %lu has unknown type\n", idofs[i].propid );
198 break;
201 propdata = (struct property_data *)&data[ idofs[i].dwOffset ];
203 /* check we don't run off the end of the data */
204 size = sz - idofs[i].dwOffset - sizeof(DWORD);
205 if( sizeof(DWORD) > size ||
206 ( propdata->type == VT_FILETIME && sizeof(FILETIME) > size ) ||
207 ( propdata->type == VT_LPSTR && (propdata->u.str.len + sizeof(DWORD)) > size ) )
209 ERR("not enough data\n");
210 break;
213 property.vt = propdata->type;
214 if( propdata->type == VT_LPSTR )
216 char *str = malloc( propdata->u.str.len );
217 memcpy( str, propdata->u.str.str, propdata->u.str.len );
218 str[ propdata->u.str.len - 1 ] = 0;
219 property.pszVal = str;
221 else if( propdata->type == VT_FILETIME )
222 property.filetime = propdata->u.ft;
223 else if( propdata->type == VT_I2 )
224 property.iVal = propdata->u.i2;
225 else if( propdata->type == VT_I4 )
226 property.lVal = propdata->u.i4;
228 /* check the type is the same as we expect */
229 if( type != propdata->type )
231 propvar_changetype(&changed, &property, type);
232 ptr = &changed;
234 else
235 ptr = &property;
237 prop[ idofs[i].propid ] = *ptr;
241 static UINT load_summary_info( MSISUMMARYINFO *si, IStream *stm )
243 struct property_set_header set_hdr;
244 struct format_id_offset format_hdr;
245 struct property_section_header section_hdr;
246 LPBYTE data = NULL;
247 LARGE_INTEGER ofs;
248 ULONG count, sz;
249 HRESULT r;
251 TRACE("%p %p\n", si, stm);
253 /* read the header */
254 sz = sizeof set_hdr;
255 r = IStream_Read( stm, &set_hdr, sz, &count );
256 if( FAILED(r) || count != sz )
257 return ERROR_FUNCTION_FAILED;
259 if( set_hdr.wByteOrder != 0xfffe )
261 ERR("property set not big-endian %04X\n", set_hdr.wByteOrder);
262 return ERROR_FUNCTION_FAILED;
265 sz = sizeof format_hdr;
266 r = IStream_Read( stm, &format_hdr, sz, &count );
267 if( FAILED(r) || count != sz )
268 return ERROR_FUNCTION_FAILED;
270 /* check the format id is correct */
271 if( !IsEqualGUID( &FMTID_SummaryInformation, &format_hdr.fmtid ) )
272 return ERROR_FUNCTION_FAILED;
274 /* seek to the location of the section */
275 ofs.QuadPart = format_hdr.dwOffset;
276 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, NULL );
277 if( FAILED(r) )
278 return ERROR_FUNCTION_FAILED;
280 /* read the section itself */
281 sz = SECT_HDR_SIZE;
282 r = IStream_Read( stm, &section_hdr, sz, &count );
283 if( FAILED(r) || count != sz )
284 return ERROR_FUNCTION_FAILED;
286 if( section_hdr.cProperties > MSI_MAX_PROPS )
288 ERR( "too many properties %lu\n", section_hdr.cProperties );
289 return ERROR_FUNCTION_FAILED;
292 data = malloc( section_hdr.cbSection );
293 if( !data )
294 return ERROR_FUNCTION_FAILED;
296 memcpy( data, &section_hdr, SECT_HDR_SIZE );
298 /* read all the data in one go */
299 sz = section_hdr.cbSection - SECT_HDR_SIZE;
300 r = IStream_Read( stm, &data[ SECT_HDR_SIZE ], sz, &count );
301 if( SUCCEEDED(r) && count == sz )
302 read_properties_from_data( si->property, data, sz + SECT_HDR_SIZE );
303 else
304 ERR( "failed to read properties %lu %lu\n", count, sz );
306 free( data );
307 return ERROR_SUCCESS;
310 static DWORD write_dword( LPBYTE data, DWORD ofs, DWORD val )
312 if( data )
314 data[ofs++] = val&0xff;
315 data[ofs++] = (val>>8)&0xff;
316 data[ofs++] = (val>>16)&0xff;
317 data[ofs++] = (val>>24)&0xff;
319 return 4;
322 static DWORD write_filetime( LPBYTE data, DWORD ofs, const FILETIME *ft )
324 write_dword( data, ofs, ft->dwLowDateTime );
325 write_dword( data, ofs + 4, ft->dwHighDateTime );
326 return 8;
329 static DWORD write_string( LPBYTE data, DWORD ofs, LPCSTR str )
331 DWORD len = lstrlenA( str ) + 1;
332 write_dword( data, ofs, len );
333 if( data )
334 memcpy( &data[ofs + 4], str, len );
335 return (7 + len) & ~3;
338 static UINT write_property_to_data( const PROPVARIANT *prop, LPBYTE data )
340 DWORD sz = 0;
342 if( prop->vt == VT_EMPTY )
343 return sz;
345 /* add the type */
346 sz += write_dword( data, sz, prop->vt );
347 switch( prop->vt )
349 case VT_I2:
350 sz += write_dword( data, sz, prop->iVal );
351 break;
352 case VT_I4:
353 sz += write_dword( data, sz, prop->lVal );
354 break;
355 case VT_FILETIME:
356 sz += write_filetime( data, sz, &prop->filetime );
357 break;
358 case VT_LPSTR:
359 sz += write_string( data, sz, prop->pszVal );
360 break;
362 return sz;
365 static UINT save_summary_info( const MSISUMMARYINFO * si, IStream *stm )
367 UINT ret = ERROR_FUNCTION_FAILED;
368 struct property_set_header set_hdr;
369 struct format_id_offset format_hdr;
370 struct property_section_header section_hdr;
371 struct property_id_offset idofs[MSI_MAX_PROPS];
372 LPBYTE data = NULL;
373 ULONG count, sz;
374 HRESULT r;
375 int i;
377 /* write the header */
378 sz = sizeof set_hdr;
379 memset( &set_hdr, 0, sz );
380 set_hdr.wByteOrder = 0xfffe;
381 set_hdr.wFormat = 0;
382 set_hdr.dwOSVer = 0x00020005; /* build 5, platform id 2 */
383 /* set_hdr.clsID is {00000000-0000-0000-0000-000000000000} */
384 set_hdr.reserved = 1;
385 r = IStream_Write( stm, &set_hdr, sz, &count );
386 if( FAILED(r) || count != sz )
387 return ret;
389 /* write the format header */
390 sz = sizeof format_hdr;
391 format_hdr.fmtid = FMTID_SummaryInformation;
392 format_hdr.dwOffset = sizeof format_hdr + sizeof set_hdr;
393 r = IStream_Write( stm, &format_hdr, sz, &count );
394 if( FAILED(r) || count != sz )
395 return ret;
397 /* add up how much space the data will take and calculate the offsets */
398 section_hdr.cbSection = sizeof section_hdr;
399 section_hdr.cbSection += (get_property_count( si->property ) * sizeof idofs[0]);
400 section_hdr.cProperties = 0;
401 for( i = 0; i < MSI_MAX_PROPS; i++ )
403 sz = write_property_to_data( &si->property[i], NULL );
404 if( !sz )
405 continue;
406 idofs[ section_hdr.cProperties ].propid = i;
407 idofs[ section_hdr.cProperties ].dwOffset = section_hdr.cbSection;
408 section_hdr.cProperties++;
409 section_hdr.cbSection += sz;
412 data = calloc( 1, section_hdr.cbSection );
414 sz = 0;
415 memcpy( &data[sz], &section_hdr, sizeof section_hdr );
416 sz += sizeof section_hdr;
418 memcpy( &data[sz], idofs, section_hdr.cProperties * sizeof idofs[0] );
419 sz += section_hdr.cProperties * sizeof idofs[0];
421 /* write out the data */
422 for( i = 0; i < MSI_MAX_PROPS; i++ )
423 sz += write_property_to_data( &si->property[i], &data[sz] );
425 r = IStream_Write( stm, data, sz, &count );
426 free( data );
427 if( FAILED(r) || count != sz )
428 return ret;
430 return ERROR_SUCCESS;
433 static MSISUMMARYINFO *create_suminfo( IStorage *stg, UINT update_count )
435 MSISUMMARYINFO *si;
437 if (!(si = alloc_msiobject( MSIHANDLETYPE_SUMMARYINFO, sizeof(MSISUMMARYINFO), MSI_CloseSummaryInfo )))
438 return NULL;
440 si->update_count = update_count;
441 IStorage_AddRef( stg );
442 si->storage = stg;
444 return si;
447 UINT msi_get_suminfo( IStorage *stg, UINT uiUpdateCount, MSISUMMARYINFO **ret )
449 IStream *stm;
450 MSISUMMARYINFO *si;
451 HRESULT hr;
452 UINT r;
454 TRACE("%p, %u\n", stg, uiUpdateCount);
456 if (!(si = create_suminfo( stg, uiUpdateCount ))) return ERROR_OUTOFMEMORY;
458 hr = IStorage_OpenStream( si->storage, L"\5SummaryInformation", 0, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &stm );
459 if (FAILED( hr ))
461 msiobj_release( &si->hdr );
462 return ERROR_FUNCTION_FAILED;
465 r = load_summary_info( si, stm );
466 IStream_Release( stm );
467 if (r != ERROR_SUCCESS)
469 msiobj_release( &si->hdr );
470 return r;
473 *ret = si;
474 return ERROR_SUCCESS;
477 UINT msi_get_db_suminfo( MSIDATABASE *db, UINT uiUpdateCount, MSISUMMARYINFO **ret )
479 IStream *stm;
480 MSISUMMARYINFO *si;
481 UINT r;
483 if (!(si = create_suminfo( db->storage, uiUpdateCount ))) return ERROR_OUTOFMEMORY;
485 r = msi_get_stream( db, L"\5SummaryInformation", &stm );
486 if (r != ERROR_SUCCESS)
488 msiobj_release( &si->hdr );
489 return r;
492 r = load_summary_info( si, stm );
493 IStream_Release( stm );
494 if (r != ERROR_SUCCESS)
496 msiobj_release( &si->hdr );
497 return r;
500 *ret = si;
501 return ERROR_SUCCESS;
504 UINT WINAPI MsiGetSummaryInformationW( MSIHANDLE hDatabase, const WCHAR *szDatabase, UINT uiUpdateCount,
505 MSIHANDLE *pHandle )
507 MSISUMMARYINFO *si;
508 MSIDATABASE *db;
509 UINT ret;
511 TRACE( "%lu, %s, %u, %p\n", hDatabase, debugstr_w(szDatabase), uiUpdateCount, pHandle );
513 if( !pHandle )
514 return ERROR_INVALID_PARAMETER;
516 if( szDatabase && szDatabase[0] )
518 LPCWSTR persist = uiUpdateCount ? MSIDBOPEN_TRANSACT : MSIDBOPEN_READONLY;
520 ret = MSI_OpenDatabaseW( szDatabase, persist, &db );
521 if( ret != ERROR_SUCCESS )
522 return ret;
524 else
526 db = msihandle2msiinfo( hDatabase, MSIHANDLETYPE_DATABASE );
527 if( !db )
529 MSIHANDLE remote, remote_suminfo;
531 if (!(remote = msi_get_remote(hDatabase)))
532 return ERROR_INVALID_HANDLE;
534 __TRY
536 ret = remote_DatabaseGetSummaryInformation(remote, uiUpdateCount, &remote_suminfo);
538 __EXCEPT(rpc_filter)
540 ret = GetExceptionCode();
542 __ENDTRY
544 if (!ret)
545 *pHandle = alloc_msi_remote_handle(remote_suminfo);
547 return ret;
551 ret = msi_get_suminfo( db->storage, uiUpdateCount, &si );
552 if (ret != ERROR_SUCCESS)
553 ret = msi_get_db_suminfo( db, uiUpdateCount, &si );
554 if (ret != ERROR_SUCCESS)
556 if ((si = create_suminfo( db->storage, uiUpdateCount )))
557 ret = ERROR_SUCCESS;
560 if (ret == ERROR_SUCCESS)
562 *pHandle = alloc_msihandle( &si->hdr );
563 if( *pHandle )
564 ret = ERROR_SUCCESS;
565 else
566 ret = ERROR_NOT_ENOUGH_MEMORY;
567 msiobj_release( &si->hdr );
570 msiobj_release( &db->hdr );
571 return ret;
574 UINT WINAPI MsiGetSummaryInformationA( MSIHANDLE hDatabase, const char *szDatabase, UINT uiUpdateCount,
575 MSIHANDLE *pHandle )
577 WCHAR *szwDatabase = NULL;
578 UINT ret;
580 TRACE( "%lu, %s, %u, %p\n", hDatabase, debugstr_a(szDatabase), uiUpdateCount, pHandle );
582 if( szDatabase )
584 szwDatabase = strdupAtoW( szDatabase );
585 if( !szwDatabase )
586 return ERROR_FUNCTION_FAILED;
589 ret = MsiGetSummaryInformationW(hDatabase, szwDatabase, uiUpdateCount, pHandle);
591 free( szwDatabase );
593 return ret;
596 UINT WINAPI MsiSummaryInfoGetPropertyCount( MSIHANDLE hSummaryInfo, UINT *pCount )
598 MSISUMMARYINFO *si;
600 TRACE( "%lu, %p\n", hSummaryInfo, pCount );
602 si = msihandle2msiinfo( hSummaryInfo, MSIHANDLETYPE_SUMMARYINFO );
603 if( !si )
605 MSIHANDLE remote;
606 UINT ret;
608 if (!(remote = msi_get_remote( hSummaryInfo )))
609 return ERROR_INVALID_HANDLE;
611 __TRY
613 ret = remote_SummaryInfoGetPropertyCount( remote, pCount );
615 __EXCEPT(rpc_filter)
617 ret = GetExceptionCode();
619 __ENDTRY
621 return ret;
624 if( pCount )
625 *pCount = get_property_count( si->property );
626 msiobj_release( &si->hdr );
628 return ERROR_SUCCESS;
631 static UINT get_prop( MSISUMMARYINFO *si, UINT uiProperty, UINT *puiDataType, INT *piValue,
632 FILETIME *pftValue, awstring *str, DWORD *pcchValueBuf)
634 PROPVARIANT *prop;
635 UINT ret = ERROR_SUCCESS;
637 prop = &si->property[uiProperty];
639 if( puiDataType )
640 *puiDataType = prop->vt;
642 switch( prop->vt )
644 case VT_I2:
645 if( piValue )
646 *piValue = prop->iVal;
647 break;
648 case VT_I4:
649 if( piValue )
650 *piValue = prop->lVal;
651 break;
652 case VT_LPSTR:
653 if( pcchValueBuf )
655 DWORD len = 0;
657 if( str->unicode )
659 len = MultiByteToWideChar( CP_ACP, 0, prop->pszVal, -1, NULL, 0 ) - 1;
660 MultiByteToWideChar( CP_ACP, 0, prop->pszVal, -1, str->str.w, *pcchValueBuf );
662 else
664 len = lstrlenA( prop->pszVal );
665 if( str->str.a )
666 lstrcpynA(str->str.a, prop->pszVal, *pcchValueBuf );
668 if (len >= *pcchValueBuf)
669 ret = ERROR_MORE_DATA;
670 *pcchValueBuf = len;
672 break;
673 case VT_FILETIME:
674 if( pftValue )
675 *pftValue = prop->filetime;
676 break;
677 case VT_EMPTY:
678 break;
679 default:
680 FIXME("Unknown property variant type\n");
681 break;
683 return ret;
686 LPWSTR msi_suminfo_dup_string( MSISUMMARYINFO *si, UINT uiProperty )
688 PROPVARIANT *prop;
690 if ( uiProperty >= MSI_MAX_PROPS )
691 return NULL;
692 prop = &si->property[uiProperty];
693 if( prop->vt != VT_LPSTR )
694 return NULL;
695 return strdupAtoW( prop->pszVal );
698 INT msi_suminfo_get_int32( MSISUMMARYINFO *si, UINT uiProperty )
700 PROPVARIANT *prop;
702 if ( uiProperty >= MSI_MAX_PROPS )
703 return -1;
704 prop = &si->property[uiProperty];
705 if( prop->vt != VT_I4 )
706 return -1;
707 return prop->lVal;
710 LPWSTR msi_get_suminfo_product( IStorage *stg )
712 MSISUMMARYINFO *si;
713 LPWSTR prod;
714 UINT r;
716 r = msi_get_suminfo( stg, 0, &si );
717 if (r != ERROR_SUCCESS)
719 ERR("no summary information!\n");
720 return NULL;
722 prod = msi_suminfo_dup_string( si, PID_REVNUMBER );
723 msiobj_release( &si->hdr );
724 return prod;
727 UINT WINAPI MsiSummaryInfoGetPropertyA( MSIHANDLE handle, UINT uiProperty, UINT *puiDataType, INT *piValue,
728 FILETIME *pftValue, char *szValueBuf, DWORD *pcchValueBuf )
730 MSISUMMARYINFO *si;
731 awstring str;
732 UINT r;
734 TRACE( "%lu, %u, %p, %p, %p, %p, %p\n", handle, uiProperty, puiDataType, piValue, pftValue, szValueBuf,
735 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( MSIHANDLE handle, UINT uiProperty, UINT *puiDataType, INT *piValue,
779 FILETIME *pftValue, WCHAR *szValueBuf, DWORD *pcchValueBuf )
781 MSISUMMARYINFO *si;
782 awstring str;
783 UINT r;
785 TRACE( "%lu, %u, %p, %p, %p, %p, %p\n", handle, uiProperty, puiDataType, piValue, pftValue, szValueBuf,
786 pcchValueBuf );
788 if (uiProperty >= MSI_MAX_PROPS)
790 if (puiDataType) *puiDataType = VT_EMPTY;
791 return ERROR_UNKNOWN_PROPERTY;
794 if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
796 MSIHANDLE remote;
797 WCHAR *buf = NULL;
799 if (!(remote = msi_get_remote( handle )))
800 return ERROR_INVALID_HANDLE;
802 __TRY
804 r = remote_SummaryInfoGetProperty( remote, uiProperty, puiDataType, piValue, pftValue, &buf );
806 __EXCEPT(rpc_filter)
808 r = GetExceptionCode();
810 __ENDTRY
812 if (!r && buf)
813 r = msi_strncpyW( buf, -1, szValueBuf, pcchValueBuf );
815 midl_user_free( buf );
816 return r;
819 str.unicode = TRUE;
820 str.str.w = szValueBuf;
822 r = get_prop( si, uiProperty, puiDataType, piValue, pftValue, &str, pcchValueBuf );
823 msiobj_release( &si->hdr );
824 return r;
827 static UINT set_prop( MSISUMMARYINFO *si, UINT uiProperty, UINT type,
828 INT iValue, FILETIME *pftValue, awcstring *str )
830 PROPVARIANT *prop;
831 UINT len;
833 TRACE("%p, %u, %u, %d, %p, %p\n", si, uiProperty, type, iValue, pftValue, str );
835 prop = &si->property[uiProperty];
837 if( prop->vt == VT_EMPTY )
839 if( !si->update_count )
840 return ERROR_FUNCTION_FAILED;
842 si->update_count--;
844 else if( prop->vt != type )
845 return ERROR_SUCCESS;
847 free_prop( prop );
848 prop->vt = type;
849 switch( type )
851 case VT_I4:
852 prop->lVal = iValue;
853 break;
854 case VT_I2:
855 prop->iVal = iValue;
856 break;
857 case VT_FILETIME:
858 prop->filetime = *pftValue;
859 break;
860 case VT_LPSTR:
861 if( str->unicode )
863 len = WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
864 NULL, 0, NULL, NULL );
865 prop->pszVal = malloc( len );
866 WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
867 prop->pszVal, len, NULL, NULL );
869 else
871 len = lstrlenA( str->str.a ) + 1;
872 prop->pszVal = malloc( len );
873 lstrcpyA( prop->pszVal, str->str.a );
875 break;
878 return ERROR_SUCCESS;
881 static UINT suminfo_set_prop( MSISUMMARYINFO *si, UINT uiProperty, UINT uiDataType, INT iValue, FILETIME *pftValue,
882 awcstring *str )
884 UINT type = get_type( uiProperty );
885 if( type == VT_EMPTY || type != uiDataType )
886 return ERROR_DATATYPE_MISMATCH;
888 if( uiDataType == VT_LPSTR && !str->str.a )
889 return ERROR_INVALID_PARAMETER;
891 if( uiDataType == VT_FILETIME && !pftValue )
892 return ERROR_INVALID_PARAMETER;
894 return set_prop( si, uiProperty, type, iValue, pftValue, str );
897 UINT WINAPI MsiSummaryInfoSetPropertyW( MSIHANDLE handle, UINT uiProperty, UINT uiDataType, INT iValue,
898 FILETIME *pftValue, const WCHAR *szValue )
900 awcstring str;
901 MSISUMMARYINFO *si;
902 UINT ret;
904 TRACE( "%lu, %u, %u, %d, %p, %s\n", handle, uiProperty, uiDataType, iValue, pftValue, debugstr_w(szValue) );
906 if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
908 MSIHANDLE remote;
910 if ((remote = msi_get_remote( handle )))
912 WARN("MsiSummaryInfoSetProperty not allowed during a custom action!\n");
913 return ERROR_FUNCTION_FAILED;
916 return ERROR_INVALID_HANDLE;
919 str.unicode = TRUE;
920 str.str.w = szValue;
922 ret = suminfo_set_prop( si, uiProperty, uiDataType, iValue, pftValue, &str );
923 msiobj_release( &si->hdr );
924 return ret;
927 UINT WINAPI MsiSummaryInfoSetPropertyA( MSIHANDLE handle, UINT uiProperty, UINT uiDataType, INT iValue,
928 FILETIME *pftValue, const char *szValue )
930 awcstring str;
931 MSISUMMARYINFO *si;
932 UINT ret;
934 TRACE( "%lu, %u, %u, %d, %p, %s\n", handle, uiProperty, uiDataType, iValue, pftValue, debugstr_a(szValue) );
936 if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
938 MSIHANDLE remote;
940 if ((remote = msi_get_remote( handle )))
942 WARN("MsiSummaryInfoSetProperty not allowed during a custom action!\n");
943 return ERROR_FUNCTION_FAILED;
946 return ERROR_INVALID_HANDLE;
949 str.unicode = FALSE;
950 str.str.a = szValue;
952 ret = suminfo_set_prop( si, uiProperty, uiDataType, iValue, pftValue, &str );
953 msiobj_release( &si->hdr );
954 return ret;
957 static UINT suminfo_persist( MSISUMMARYINFO *si )
959 UINT ret = ERROR_FUNCTION_FAILED;
960 IStream *stm = NULL;
961 DWORD grfMode;
962 HRESULT r;
964 grfMode = STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
965 r = IStorage_CreateStream( si->storage, L"\5SummaryInformation", grfMode, 0, 0, &stm );
966 if( SUCCEEDED(r) )
968 ret = save_summary_info( si, stm );
969 IStream_Release( stm );
971 return ret;
974 static void parse_filetime( LPCWSTR str, FILETIME *ft )
976 SYSTEMTIME lt, utc;
977 const WCHAR *p = str;
978 WCHAR *end;
980 memset( &lt, 0, sizeof(lt) );
982 /* YYYY/MM/DD hh:mm:ss */
984 while (iswspace( *p )) p++;
986 lt.wYear = wcstol( p, &end, 10 );
987 if (*end != '/') return;
988 p = end + 1;
990 lt.wMonth = wcstol( p, &end, 10 );
991 if (*end != '/') return;
992 p = end + 1;
994 lt.wDay = wcstol( p, &end, 10 );
995 if (*end != ' ') return;
996 p = end + 1;
998 while (iswspace( *p )) p++;
1000 lt.wHour = wcstol( p, &end, 10 );
1001 if (*end != ':') return;
1002 p = end + 1;
1004 lt.wMinute = wcstol( p, &end, 10 );
1005 if (*end != ':') return;
1006 p = end + 1;
1008 lt.wSecond = wcstol( p, &end, 10 );
1010 TzSpecificLocalTimeToSystemTime( NULL, &lt, &utc );
1011 SystemTimeToFileTime( &utc, ft );
1014 static UINT parse_prop( LPCWSTR prop, LPCWSTR value, UINT *pid, INT *int_value,
1015 FILETIME *ft_value, awcstring *str_value )
1017 *pid = wcstol( prop, NULL, 10 );
1018 switch (*pid)
1020 case PID_CODEPAGE:
1021 case PID_WORDCOUNT:
1022 case PID_CHARCOUNT:
1023 case PID_SECURITY:
1024 case PID_PAGECOUNT:
1025 *int_value = wcstol( value, NULL, 10 );
1026 break;
1028 case PID_LASTPRINTED:
1029 case PID_CREATE_DTM:
1030 case PID_LASTSAVE_DTM:
1031 parse_filetime( value, ft_value );
1032 break;
1034 case PID_SUBJECT:
1035 case PID_AUTHOR:
1036 case PID_KEYWORDS:
1037 case PID_COMMENTS:
1038 case PID_TEMPLATE:
1039 case PID_LASTAUTHOR:
1040 case PID_REVNUMBER:
1041 case PID_APPNAME:
1042 case PID_TITLE:
1043 str_value->str.w = value;
1044 str_value->unicode = TRUE;
1045 break;
1047 default:
1048 WARN("unhandled prop id %u\n", *pid);
1049 return ERROR_FUNCTION_FAILED;
1052 return ERROR_SUCCESS;
1055 UINT msi_add_suminfo( MSIDATABASE *db, LPWSTR **records, int num_records, int num_columns )
1057 UINT r;
1058 int i, j;
1059 MSISUMMARYINFO *si;
1061 r = msi_get_suminfo( db->storage, num_records * (num_columns / 2), &si );
1062 if (r != ERROR_SUCCESS)
1064 if (!(si = create_suminfo( db->storage, num_records * (num_columns / 2) )))
1065 return ERROR_OUTOFMEMORY;
1066 r = ERROR_SUCCESS;
1069 for (i = 0; i < num_records; i++)
1071 for (j = 0; j < num_columns; j += 2)
1073 UINT pid;
1074 INT int_value = 0;
1075 FILETIME ft_value;
1076 awcstring str_value;
1078 r = parse_prop( records[i][j], records[i][j + 1], &pid, &int_value, &ft_value, &str_value );
1079 if (r != ERROR_SUCCESS)
1080 goto end;
1082 r = set_prop( si, pid, get_type(pid), int_value, &ft_value, &str_value );
1083 if (r != ERROR_SUCCESS)
1084 goto end;
1088 end:
1089 if (r == ERROR_SUCCESS)
1090 r = suminfo_persist( si );
1092 msiobj_release( &si->hdr );
1093 return r;
1096 static UINT save_prop( MSISUMMARYINFO *si, HANDLE handle, UINT row )
1098 static const char fmt_systemtime[] = "%04u/%02u/%02u %02u:%02u:%02u";
1099 char data[36]; /* largest string: YYYY/MM/DD hh:mm:ss */
1100 static const char fmt_begin[] = "%u\t";
1101 static const char data_end[] = "\r\n";
1102 static const char fmt_int[] = "%u";
1103 UINT r, data_type;
1104 SYSTEMTIME system_time;
1105 FILETIME file_time;
1106 INT int_value;
1107 awstring str;
1108 DWORD len, sz;
1110 str.unicode = FALSE;
1111 str.str.a = NULL;
1112 len = 0;
1113 r = get_prop( si, row, &data_type, &int_value, &file_time, &str, &len );
1114 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
1115 return r;
1116 if (data_type == VT_EMPTY)
1117 return ERROR_SUCCESS; /* property not set */
1118 sz = sprintf( data, fmt_begin, row );
1119 if (!WriteFile( handle, data, sz, &sz, NULL ))
1120 return ERROR_WRITE_FAULT;
1122 switch( data_type )
1124 case VT_I2:
1125 case VT_I4:
1126 sz = sprintf( data, fmt_int, int_value );
1127 if (!WriteFile( handle, data, sz, &sz, NULL ))
1128 return ERROR_WRITE_FAULT;
1129 break;
1130 case VT_LPSTR:
1131 len++;
1132 if (!(str.str.a = malloc( len )))
1133 return ERROR_OUTOFMEMORY;
1134 r = get_prop( si, row, NULL, NULL, NULL, &str, &len );
1135 if (r != ERROR_SUCCESS)
1137 free( str.str.a );
1138 return r;
1140 sz = len;
1141 if (!WriteFile( handle, str.str.a, sz, &sz, NULL ))
1143 free( str.str.a );
1144 return ERROR_WRITE_FAULT;
1146 free( str.str.a );
1147 break;
1148 case VT_FILETIME:
1149 if (!FileTimeToSystemTime( &file_time, &system_time ))
1150 return ERROR_FUNCTION_FAILED;
1151 sz = sprintf( data, fmt_systemtime, system_time.wYear, system_time.wMonth,
1152 system_time.wDay, system_time.wHour, system_time.wMinute,
1153 system_time.wSecond );
1154 if (!WriteFile( handle, data, sz, &sz, NULL ))
1155 return ERROR_WRITE_FAULT;
1156 break;
1157 case VT_EMPTY:
1158 /* cannot reach here, property not set */
1159 break;
1160 default:
1161 FIXME( "Unknown property variant type\n" );
1162 return ERROR_FUNCTION_FAILED;
1165 sz = ARRAY_SIZE(data_end) - 1;
1166 if (!WriteFile( handle, data_end, sz, &sz, NULL ))
1167 return ERROR_WRITE_FAULT;
1169 return ERROR_SUCCESS;
1172 UINT msi_export_suminfo( MSIDATABASE *db, HANDLE handle )
1174 UINT i, r, num_rows;
1175 MSISUMMARYINFO *si;
1177 r = msi_get_suminfo( db->storage, 0, &si );
1178 if (r != ERROR_SUCCESS)
1179 r = msi_get_db_suminfo( db, 0, &si );
1180 if (r != ERROR_SUCCESS)
1181 return r;
1183 num_rows = get_property_count( si->property );
1184 if (!num_rows)
1186 msiobj_release( &si->hdr );
1187 return ERROR_FUNCTION_FAILED;
1190 for (i = 0; i < num_rows; i++)
1192 r = save_prop( si, handle, i );
1193 if (r != ERROR_SUCCESS)
1195 msiobj_release( &si->hdr );
1196 return r;
1200 msiobj_release( &si->hdr );
1201 return ERROR_SUCCESS;
1204 UINT WINAPI MsiSummaryInfoPersist( MSIHANDLE handle )
1206 MSISUMMARYINFO *si;
1207 UINT ret;
1209 TRACE( "%lu\n", handle );
1211 si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
1212 if( !si )
1213 return ERROR_INVALID_HANDLE;
1215 ret = suminfo_persist( si );
1217 msiobj_release( &si->hdr );
1218 return ret;
1221 UINT WINAPI MsiCreateTransformSummaryInfoA( MSIHANDLE db, MSIHANDLE db_ref, const char *transform, int error,
1222 int validation )
1224 UINT r;
1225 WCHAR *transformW = NULL;
1227 TRACE( "%lu, %lu, %s, %d, %d\n", db, db_ref, debugstr_a(transform), error, validation );
1229 if (transform && !(transformW = strdupAtoW( transform )))
1230 return ERROR_OUTOFMEMORY;
1232 r = MsiCreateTransformSummaryInfoW( db, db_ref, transformW, error, validation );
1233 free( transformW );
1234 return r;
1237 UINT WINAPI MsiCreateTransformSummaryInfoW( MSIHANDLE db, MSIHANDLE db_ref, const WCHAR *transform, int error,
1238 int validation )
1240 FIXME( "%lu, %lu, %s, %d, %d\n", db, db_ref, debugstr_w(transform), error, validation );
1241 return ERROR_FUNCTION_FAILED;
1244 UINT msi_load_suminfo_properties( MSIPACKAGE *package )
1246 MSISUMMARYINFO *si;
1247 WCHAR *package_code;
1248 UINT r;
1249 DWORD 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 = malloc( 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 free( package_code );
1283 msiobj_release( &si->hdr );
1284 return r;
1287 r = msi_set_property( package->db, L"PackageCode", package_code, len );
1288 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;