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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #define NONAMELESSUNION
31 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msi);
40 #define MSI_MAX_PROPS 19
60 } PROPERTYSECTIONHEADER;
90 typedef struct tagMSISUMMARYINFO
95 PROPVARIANT property[MSI_MAX_PROPS];
98 static const WCHAR szSumInfo[] = { 5 ,'S','u','m','m','a','r','y',
99 'I','n','f','o','r','m','a','t','i','o','n',0 };
101 static void free_prop( PROPVARIANT *prop )
103 if (prop->vt == VT_LPSTR )
104 HeapFree( GetProcessHeap(), 0, prop->u.pszVal );
108 static void MSI_CloseSummaryInfo( MSIOBJECTHDR *arg )
110 MSISUMMARYINFO *si = (MSISUMMARYINFO *) arg;
113 for( i = 0; i < MSI_MAX_PROPS; i++ )
114 free_prop( &si->property[i] );
115 msiobj_release( &si->db->hdr );
118 static UINT get_type( UINT uiProperty )
136 case PID_LASTPRINTED:
138 case PID_LASTSAVE_DTM:
150 static UINT get_property_count( PROPVARIANT *property )
156 for( i = 0; i < MSI_MAX_PROPS; i++ )
157 if( property[i].vt != VT_EMPTY )
162 /* FIXME: doesn't deal with endian conversion */
163 static void read_properties_from_data( PROPVARIANT *prop,
164 PROPERTYIDOFFSET *idofs, DWORD count, LPBYTE data, DWORD sz )
169 PROPERTY_DATA *propdata;
170 PROPVARIANT *property;
172 /* now set all the properties */
173 for( i = 0; i < count; i++ )
175 type = get_type( idofs[i].propid );
176 if( type == VT_EMPTY )
178 ERR("propid %ld has unknown type\n", idofs[i].propid);
182 propdata = (PROPERTY_DATA*) &data[idofs[i].dwOffset];
184 /* check the type is the same as we expect */
185 if( type != propdata->type )
191 /* check we don't run off the end of the data */
192 size = sz - idofs[i].dwOffset - sizeof(DWORD);
193 if( sizeof(DWORD) > size ||
194 ( type == VT_FILETIME && sizeof(FILETIME) > size ) ||
195 ( type == VT_LPSTR && (propdata->u.str.len + sizeof(DWORD)) > size ) )
197 ERR("not enough data\n");
201 property = &prop[ idofs[i].propid ];
204 if( type == VT_LPSTR )
206 LPSTR str = HeapAlloc( GetProcessHeap(), 0, propdata->u.str.len );
207 memcpy( str, propdata->u.str.str, propdata->u.str.len );
208 str[ propdata->u.str.len - 1 ] = 0;
209 property->u.pszVal = str;
211 else if( type == VT_FILETIME )
212 property->u.filetime = propdata->u.ft;
213 else if( type == VT_I2 )
214 property->u.iVal = propdata->u.i2;
215 else if( type == VT_I4 )
216 property->u.lVal = propdata->u.i4;
220 static UINT load_summary_info( MSISUMMARYINFO *si, IStream *stm )
222 UINT ret = ERROR_FUNCTION_FAILED;
223 PROPERTYSETHEADER set_hdr;
224 FORMATIDOFFSET format_hdr;
225 PROPERTYSECTIONHEADER section_hdr;
226 PROPERTYIDOFFSET idofs[MSI_MAX_PROPS];
232 TRACE("%p %p\n", si, stm);
234 /* read the header */
236 r = IStream_Read( stm, &set_hdr, sz, &count );
237 if( FAILED(r) || count != sz )
240 if( set_hdr.wByteOrder != 0xfffe )
242 ERR("property set not big-endian %04X\n", set_hdr.wByteOrder);
246 sz = sizeof format_hdr;
247 r = IStream_Read( stm, &format_hdr, sz, &count );
248 if( FAILED(r) || count != sz )
251 /* check the format id is correct */
252 if( !IsEqualGUID( &FMTID_SummaryInformation, &format_hdr.fmtid ) )
255 /* seek to the location of the section */
256 ofs.QuadPart = format_hdr.dwOffset;
257 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, NULL );
261 /* read the section itself */
262 sz = sizeof section_hdr;
263 r = IStream_Read( stm, §ion_hdr, sz, &count );
264 if( FAILED(r) || count != sz )
267 if( section_hdr.cProperties > MSI_MAX_PROPS )
269 ERR("too many properties %ld\n", section_hdr.cProperties);
273 /* read the offsets */
274 sz = sizeof idofs[0] * section_hdr.cProperties;
275 r = IStream_Read( stm, idofs, sz, &count );
276 if( FAILED(r) || count != sz )
279 /* read all the data in one go */
280 sz = section_hdr.cbSection;
281 data = HeapAlloc( GetProcessHeap(), 0, sz );
284 r = IStream_Read( stm, data, sz, &count );
285 if( SUCCEEDED(r) && count == sz )
287 read_properties_from_data( si->property, idofs,
288 section_hdr.cProperties, data, sz );
291 HeapFree( GetProcessHeap(), 0, data );
295 static DWORD write_dword( LPBYTE data, DWORD ofs, DWORD val )
299 data[ofs++] = val&0xff;
300 data[ofs++] = (val>>8)&0xff;
301 data[ofs++] = (val>>16)&0xff;
302 data[ofs++] = (val>>24)&0xff;
307 static DWORD write_filetime( LPBYTE data, DWORD ofs, LPFILETIME ft )
309 write_dword( data, ofs, ft->dwLowDateTime );
310 write_dword( data, ofs + 4, ft->dwHighDateTime );
314 static DWORD write_string( LPBYTE data, DWORD ofs, LPCSTR str )
316 DWORD len = lstrlenA( str ) + 1;
317 write_dword( data, ofs, len );
319 lstrcpyA( &data[ofs + 4], str );
320 return (7 + len) & ~3;
323 static UINT write_property_to_data( PROPVARIANT *prop, LPBYTE data )
327 if( prop->vt == VT_EMPTY )
331 sz += write_dword( data, sz, prop->vt );
335 sz += write_dword( data, sz, prop->u.iVal );
338 sz += write_dword( data, sz, prop->u.lVal );
341 sz += write_filetime( data, sz, &prop->u.filetime );
344 sz += write_string( data, sz, prop->u.pszVal );
350 static UINT save_summary_info( MSISUMMARYINFO * si, IStream *stm )
352 UINT ret = ERROR_FUNCTION_FAILED;
353 PROPERTYSETHEADER set_hdr;
354 FORMATIDOFFSET format_hdr;
355 PROPERTYSECTIONHEADER section_hdr;
356 PROPERTYIDOFFSET idofs[MSI_MAX_PROPS];
362 /* write the header */
364 memset( &set_hdr, 0, sz );
365 set_hdr.wByteOrder = 0xfffe;
367 set_hdr.dwOSVer = 0x00020005; /* build 5, platform id 2 */
368 /* set_hdr.clsID is {00000000-0000-0000-0000-000000000000} */
369 set_hdr.reserved = 1;
370 r = IStream_Write( stm, &set_hdr, sz, &count );
371 if( FAILED(r) || count != sz )
374 /* write the format header */
375 sz = sizeof format_hdr;
376 memcpy( &format_hdr.fmtid, &FMTID_SummaryInformation, sizeof (FMTID) );
377 format_hdr.dwOffset = sizeof format_hdr + sizeof set_hdr;
378 r = IStream_Write( stm, &format_hdr, sz, &count );
379 if( FAILED(r) || count != sz )
382 /* add up how much space the data will take and calculate the offsets */
383 section_hdr.cbSection = sizeof section_hdr;
384 section_hdr.cbSection += (get_property_count( si->property ) * sizeof idofs[0]);
385 section_hdr.cProperties = 0;
387 for( i = 0; i < MSI_MAX_PROPS; i++ )
389 sz = write_property_to_data( &si->property[i], NULL );
392 idofs[ section_hdr.cProperties ].propid = i;
393 idofs[ section_hdr.cProperties ].dwOffset = section_hdr.cbSection;
394 section_hdr.cProperties++;
395 section_hdr.cbSection += sz;
398 data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, section_hdr.cbSection );
401 memcpy( &data[sz], §ion_hdr, sizeof section_hdr );
402 sz += sizeof section_hdr;
404 memcpy( &data[sz], idofs, section_hdr.cProperties * sizeof idofs[0] );
405 sz += section_hdr.cProperties * sizeof idofs[0];
407 /* write out the data */
408 for( i = 0; i < MSI_MAX_PROPS; i++ )
409 sz += write_property_to_data( &si->property[i], &data[sz] );
411 r = IStream_Write( stm, data, sz, &count );
412 HeapFree( GetProcessHeap(), 0, data );
413 if( FAILED(r) || count != sz )
416 return ERROR_SUCCESS;
419 UINT WINAPI MsiGetSummaryInformationW( MSIHANDLE hDatabase,
420 LPCWSTR szDatabase, UINT uiUpdateCount, MSIHANDLE *pHandle )
422 UINT ret = ERROR_SUCCESS;
430 TRACE("%ld %s %d %p\n", hDatabase, debugstr_w(szDatabase),
431 uiUpdateCount, pHandle);
434 return ERROR_INVALID_PARAMETER;
440 res = MSI_OpenDatabaseW(szDatabase, NULL, &db);
441 if( res != ERROR_SUCCESS )
446 db = msihandle2msiinfo(hDatabase, MSIHANDLETYPE_DATABASE);
448 return ERROR_INVALID_PARAMETER;
451 si = alloc_msiobject( MSIHANDLETYPE_SUMMARYINFO,
452 sizeof (MSISUMMARYINFO), MSI_CloseSummaryInfo );
455 ret = ERROR_FUNCTION_FAILED;
459 msiobj_addref( &db->hdr );
461 memset( &si->property, 0, sizeof si->property );
462 si->update_count = uiUpdateCount;
464 /* read the stream... if we fail, we'll start with an empty property set */
465 grfMode = STGM_READ | STGM_SHARE_EXCLUSIVE;
466 r = IStorage_OpenStream( si->db->storage, szSumInfo, 0, grfMode, 0, &stm );
469 load_summary_info( si, stm );
470 IStream_Release( stm );
473 handle = alloc_msihandle( &si->hdr );
477 ret = ERROR_FUNCTION_FAILED;
478 msiobj_release( &si->hdr );
482 msiobj_release(&db->hdr);
487 UINT WINAPI MsiGetSummaryInformationA(MSIHANDLE hDatabase,
488 LPCSTR szDatabase, UINT uiUpdateCount, MSIHANDLE *pHandle)
490 LPWSTR szwDatabase = NULL;
493 TRACE("%ld %s %d %p\n", hDatabase, debugstr_a(szDatabase),
494 uiUpdateCount, pHandle);
498 szwDatabase = strdupAtoW( szDatabase );
500 return ERROR_FUNCTION_FAILED;
503 ret = MsiGetSummaryInformationW(hDatabase, szwDatabase, uiUpdateCount, pHandle);
505 HeapFree( GetProcessHeap(), 0, szwDatabase );
510 UINT WINAPI MsiSummaryInfoGetPropertyCount(MSIHANDLE hSummaryInfo, UINT *pCount)
514 TRACE("%ld %p\n",hSummaryInfo, pCount);
516 si = msihandle2msiinfo( hSummaryInfo, MSIHANDLETYPE_SUMMARYINFO );
518 return ERROR_INVALID_HANDLE;
521 *pCount = get_property_count( si->property );
522 msiobj_release( &si->hdr );
524 return ERROR_SUCCESS;
527 static UINT get_prop( MSIHANDLE handle, UINT uiProperty, UINT *puiDataType,
528 INT *piValue, FILETIME *pftValue, awstring *str, DWORD *pcchValueBuf)
534 TRACE("%ld %d %p %p %p %p %p\n", handle, uiProperty, puiDataType,
535 piValue, pftValue, str, pcchValueBuf);
537 type = get_type( uiProperty );
541 si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
543 return ERROR_INVALID_HANDLE;
545 prop = &si->property[uiProperty];
546 if( prop->vt != type )
553 *piValue = prop->u.iVal;
557 *piValue = prop->u.lVal;
566 len = MultiByteToWideChar( CP_ACP, 0, prop->u.pszVal, -1,
567 str->str.w, *pcchValueBuf );
571 len = lstrlenA( prop->u.pszVal );
573 lstrcpynA(str->str.a, prop->u.pszVal, *pcchValueBuf );
580 memcpy(pftValue, &prop->u.filetime, sizeof (FILETIME) );
585 FIXME("Unknown property variant type\n");
589 msiobj_release( &si->hdr );
590 return ERROR_SUCCESS;
593 UINT WINAPI MsiSummaryInfoGetPropertyA(
594 MSIHANDLE handle, UINT uiProperty, UINT *puiDataType, INT *piValue,
595 FILETIME *pftValue, LPSTR szValueBuf, DWORD *pcchValueBuf)
599 TRACE("%ld %d %p %p %p %p %p\n", handle, uiProperty, puiDataType,
600 piValue, pftValue, szValueBuf, pcchValueBuf );
603 str.str.a = szValueBuf;
605 return get_prop( handle, uiProperty, puiDataType, piValue,
606 pftValue, &str, pcchValueBuf );
609 UINT WINAPI MsiSummaryInfoGetPropertyW(
610 MSIHANDLE handle, UINT uiProperty, UINT *puiDataType, INT *piValue,
611 FILETIME *pftValue, LPWSTR szValueBuf, DWORD *pcchValueBuf)
615 TRACE("%ld %d %p %p %p %p %p\n", handle, uiProperty, puiDataType,
616 piValue, pftValue, szValueBuf, pcchValueBuf );
619 str.str.w = szValueBuf;
621 return get_prop( handle, uiProperty, puiDataType, piValue,
622 pftValue, &str, pcchValueBuf );
625 static UINT set_prop( MSIHANDLE handle, UINT uiProperty, UINT uiDataType,
626 INT iValue, FILETIME* pftValue, awstring *str )
630 UINT type, len, ret = ERROR_SUCCESS;
632 TRACE("%ld %u %u %i %p %p\n", handle, uiProperty, uiDataType,
633 iValue, pftValue, str );
635 type = get_type( uiProperty );
636 if( type == VT_EMPTY || type != uiDataType )
637 return ERROR_DATATYPE_MISMATCH;
639 if( uiDataType == VT_LPSTR && !str->str.w )
640 return ERROR_INVALID_PARAMETER;
642 if( uiDataType == VT_FILETIME && !pftValue )
643 return ERROR_INVALID_PARAMETER;
645 si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
647 return ERROR_INVALID_HANDLE;
649 prop = &si->property[uiProperty];
651 if( prop->vt == VT_EMPTY )
653 if( !si->update_count )
655 ret = ERROR_FUNCTION_FAILED;
660 else if( prop->vt != type )
668 prop->u.lVal = iValue;
671 prop->u.iVal = iValue;
674 memcpy( &prop->u.filetime, pftValue, sizeof prop->u.filetime );
679 len = WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
680 NULL, 0, NULL, NULL );
681 prop->u.pszVal = HeapAlloc( GetProcessHeap(), 0, len );
682 WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
683 prop->u.pszVal, len, NULL, NULL );
687 len = lstrlenA( str->str.a ) + 1;
688 prop->u.pszVal = HeapAlloc( GetProcessHeap(), 0, len );
689 lstrcpyA( prop->u.pszVal, str->str.a );
695 msiobj_release( &si->hdr );
699 UINT WINAPI MsiSummaryInfoSetPropertyW( MSIHANDLE handle, UINT uiProperty,
700 UINT uiDataType, INT iValue, FILETIME* pftValue, LPWSTR szValue )
704 TRACE("%ld %u %u %i %p %s\n", handle, uiProperty, uiDataType,
705 iValue, pftValue, debugstr_w(szValue) );
709 return set_prop( handle, uiProperty, uiDataType, iValue, pftValue, &str );
712 UINT WINAPI MsiSummaryInfoSetPropertyA( MSIHANDLE handle, UINT uiProperty,
713 UINT uiDataType, INT iValue, FILETIME* pftValue, LPSTR szValue )
717 TRACE("%ld %u %u %i %p %s\n", handle, uiProperty, uiDataType,
718 iValue, pftValue, debugstr_a(szValue) );
722 return set_prop( handle, uiProperty, uiDataType, iValue, pftValue, &str );
725 UINT WINAPI MsiSummaryInfoPersist( MSIHANDLE handle )
731 UINT ret = ERROR_FUNCTION_FAILED;
733 TRACE("%ld\n", handle );
735 si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
737 return ERROR_INVALID_HANDLE;
739 grfMode = STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
740 r = IStorage_CreateStream( si->db->storage, szSumInfo, grfMode, 0, 0, &stm );
743 ret = save_summary_info( si, stm );
744 IStream_Release( stm );
746 msiobj_release( &si->hdr );