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
24 #define NONAMELESSUNION
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
38 #include "propvarutil.h"
39 #include "msiserver.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
61 } PROPERTYSECTIONHEADER;
83 static HRESULT (WINAPI *pPropVariantChangeType)
84 (PROPVARIANT *ppropvarDest, REFPROPVARIANT propvarSrc,
85 PROPVAR_CHANGE_FLAGS flags, VARTYPE vt);
87 #define SECT_HDR_SIZE (sizeof(PROPERTYSECTIONHEADER))
89 static void free_prop( PROPVARIANT *prop )
91 if (prop->vt == VT_LPSTR )
92 msi_free( prop->u.pszVal );
96 static void MSI_CloseSummaryInfo( MSIOBJECTHDR *arg )
98 MSISUMMARYINFO *si = (MSISUMMARYINFO *) arg;
101 for( i = 0; i < MSI_MAX_PROPS; i++ )
102 free_prop( &si->property[i] );
103 IStorage_Release( si->storage );
106 static UINT get_type( UINT uiProperty )
124 case PID_LASTPRINTED:
126 case PID_LASTSAVE_DTM:
138 static UINT get_property_count( const PROPVARIANT *property )
144 for( i = 0; i < MSI_MAX_PROPS; i++ )
145 if( property[i].vt != VT_EMPTY )
150 static UINT propvar_changetype(PROPVARIANT *changed, PROPVARIANT *property, VARTYPE vt)
153 HMODULE propsys = LoadLibraryA("propsys.dll");
154 pPropVariantChangeType = (void *)GetProcAddress(propsys, "PropVariantChangeType");
156 if (!pPropVariantChangeType)
158 ERR("PropVariantChangeType function missing!\n");
159 return ERROR_FUNCTION_FAILED;
162 hr = pPropVariantChangeType(changed, property, 0, vt);
163 return (hr == S_OK) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
166 /* FIXME: doesn't deal with endian conversion */
167 static void read_properties_from_data( PROPVARIANT *prop, LPBYTE data, DWORD sz )
171 PROPERTY_DATA *propdata;
172 PROPVARIANT property, *ptr;
174 PROPERTYIDOFFSET *idofs;
175 PROPERTYSECTIONHEADER *section_hdr;
177 section_hdr = (PROPERTYSECTIONHEADER*) &data[0];
178 idofs = (PROPERTYIDOFFSET*) &data[SECT_HDR_SIZE];
180 /* now set all the properties */
181 for( i = 0; i < section_hdr->cProperties; i++ )
183 if( idofs[i].propid >= MSI_MAX_PROPS )
185 ERR("Unknown property ID %d\n", idofs[i].propid );
189 type = get_type( idofs[i].propid );
190 if( type == VT_EMPTY )
192 ERR("propid %d has unknown type\n", idofs[i].propid);
196 propdata = (PROPERTY_DATA*) &data[ idofs[i].dwOffset ];
198 /* check we don't run off the end of the data */
199 size = sz - idofs[i].dwOffset - sizeof(DWORD);
200 if( sizeof(DWORD) > size ||
201 ( propdata->type == VT_FILETIME && sizeof(FILETIME) > size ) ||
202 ( propdata->type == VT_LPSTR && (propdata->u.str.len + sizeof(DWORD)) > size ) )
204 ERR("not enough data\n");
208 property.vt = propdata->type;
209 if( propdata->type == VT_LPSTR )
211 LPSTR str = msi_alloc( propdata->u.str.len );
212 memcpy( str, propdata->u.str.str, propdata->u.str.len );
213 str[ propdata->u.str.len - 1 ] = 0;
214 property.u.pszVal = str;
216 else if( propdata->type == VT_FILETIME )
217 property.u.filetime = propdata->u.ft;
218 else if( propdata->type == VT_I2 )
219 property.u.iVal = propdata->u.i2;
220 else if( propdata->type == VT_I4 )
221 property.u.lVal = propdata->u.i4;
223 /* check the type is the same as we expect */
224 if( type != propdata->type )
226 propvar_changetype(&changed, &property, type);
232 prop[ idofs[i].propid ] = *ptr;
236 static UINT load_summary_info( MSISUMMARYINFO *si, IStream *stm )
238 UINT ret = ERROR_FUNCTION_FAILED;
239 PROPERTYSETHEADER set_hdr;
240 FORMATIDOFFSET format_hdr;
241 PROPERTYSECTIONHEADER section_hdr;
247 TRACE("%p %p\n", si, stm);
249 /* read the header */
251 r = IStream_Read( stm, &set_hdr, sz, &count );
252 if( FAILED(r) || count != sz )
255 if( set_hdr.wByteOrder != 0xfffe )
257 ERR("property set not big-endian %04X\n", set_hdr.wByteOrder);
261 sz = sizeof format_hdr;
262 r = IStream_Read( stm, &format_hdr, sz, &count );
263 if( FAILED(r) || count != sz )
266 /* check the format id is correct */
267 if( !IsEqualGUID( &FMTID_SummaryInformation, &format_hdr.fmtid ) )
270 /* seek to the location of the section */
271 ofs.QuadPart = format_hdr.dwOffset;
272 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, NULL );
276 /* read the section itself */
278 r = IStream_Read( stm, §ion_hdr, sz, &count );
279 if( FAILED(r) || count != sz )
282 if( section_hdr.cProperties > MSI_MAX_PROPS )
284 ERR("too many properties %d\n", section_hdr.cProperties);
288 data = msi_alloc( section_hdr.cbSection);
292 memcpy( data, §ion_hdr, SECT_HDR_SIZE );
294 /* read all the data in one go */
295 sz = section_hdr.cbSection - SECT_HDR_SIZE;
296 r = IStream_Read( stm, &data[ SECT_HDR_SIZE ], sz, &count );
297 if( SUCCEEDED(r) && count == sz )
298 read_properties_from_data( si->property, data, sz + SECT_HDR_SIZE );
300 ERR("failed to read properties %d %d\n", count, sz);
306 static DWORD write_dword( LPBYTE data, DWORD ofs, DWORD val )
310 data[ofs++] = val&0xff;
311 data[ofs++] = (val>>8)&0xff;
312 data[ofs++] = (val>>16)&0xff;
313 data[ofs++] = (val>>24)&0xff;
318 static DWORD write_filetime( LPBYTE data, DWORD ofs, const FILETIME *ft )
320 write_dword( data, ofs, ft->dwLowDateTime );
321 write_dword( data, ofs + 4, ft->dwHighDateTime );
325 static DWORD write_string( LPBYTE data, DWORD ofs, LPCSTR str )
327 DWORD len = lstrlenA( str ) + 1;
328 write_dword( data, ofs, len );
330 memcpy( &data[ofs + 4], str, len );
331 return (7 + len) & ~3;
334 static UINT write_property_to_data( const PROPVARIANT *prop, LPBYTE data )
338 if( prop->vt == VT_EMPTY )
342 sz += write_dword( data, sz, prop->vt );
346 sz += write_dword( data, sz, prop->u.iVal );
349 sz += write_dword( data, sz, prop->u.lVal );
352 sz += write_filetime( data, sz, &prop->u.filetime );
355 sz += write_string( data, sz, prop->u.pszVal );
361 static UINT save_summary_info( const MSISUMMARYINFO * si, IStream *stm )
363 UINT ret = ERROR_FUNCTION_FAILED;
364 PROPERTYSETHEADER set_hdr;
365 FORMATIDOFFSET format_hdr;
366 PROPERTYSECTIONHEADER section_hdr;
367 PROPERTYIDOFFSET idofs[MSI_MAX_PROPS];
373 /* write the header */
375 memset( &set_hdr, 0, sz );
376 set_hdr.wByteOrder = 0xfffe;
378 set_hdr.dwOSVer = 0x00020005; /* build 5, platform id 2 */
379 /* set_hdr.clsID is {00000000-0000-0000-0000-000000000000} */
380 set_hdr.reserved = 1;
381 r = IStream_Write( stm, &set_hdr, sz, &count );
382 if( FAILED(r) || count != sz )
385 /* write the format header */
386 sz = sizeof format_hdr;
387 format_hdr.fmtid = FMTID_SummaryInformation;
388 format_hdr.dwOffset = sizeof format_hdr + sizeof set_hdr;
389 r = IStream_Write( stm, &format_hdr, sz, &count );
390 if( FAILED(r) || count != sz )
393 /* add up how much space the data will take and calculate the offsets */
394 section_hdr.cbSection = sizeof section_hdr;
395 section_hdr.cbSection += (get_property_count( si->property ) * sizeof idofs[0]);
396 section_hdr.cProperties = 0;
397 for( i = 0; i < MSI_MAX_PROPS; i++ )
399 sz = write_property_to_data( &si->property[i], NULL );
402 idofs[ section_hdr.cProperties ].propid = i;
403 idofs[ section_hdr.cProperties ].dwOffset = section_hdr.cbSection;
404 section_hdr.cProperties++;
405 section_hdr.cbSection += sz;
408 data = msi_alloc_zero( section_hdr.cbSection );
411 memcpy( &data[sz], §ion_hdr, sizeof section_hdr );
412 sz += sizeof section_hdr;
414 memcpy( &data[sz], idofs, section_hdr.cProperties * sizeof idofs[0] );
415 sz += section_hdr.cProperties * sizeof idofs[0];
417 /* write out the data */
418 for( i = 0; i < MSI_MAX_PROPS; i++ )
419 sz += write_property_to_data( &si->property[i], &data[sz] );
421 r = IStream_Write( stm, data, sz, &count );
423 if( FAILED(r) || count != sz )
426 return ERROR_SUCCESS;
429 MSISUMMARYINFO *MSI_GetSummaryInformationW( IStorage *stg, UINT uiUpdateCount )
436 TRACE("%p %d\n", stg, uiUpdateCount );
438 si = alloc_msiobject( MSIHANDLETYPE_SUMMARYINFO,
439 sizeof (MSISUMMARYINFO), MSI_CloseSummaryInfo );
443 si->update_count = uiUpdateCount;
444 IStorage_AddRef( stg );
447 /* read the stream... if we fail, we'll start with an empty property set */
448 grfMode = STGM_READ | STGM_SHARE_EXCLUSIVE;
449 r = IStorage_OpenStream( si->storage, szSumInfo, 0, grfMode, 0, &stm );
452 load_summary_info( si, stm );
453 IStream_Release( stm );
459 UINT WINAPI MsiGetSummaryInformationW( MSIHANDLE hDatabase,
460 LPCWSTR szDatabase, UINT uiUpdateCount, MSIHANDLE *pHandle )
464 UINT ret = ERROR_FUNCTION_FAILED;
466 TRACE("%d %s %d %p\n", hDatabase, debugstr_w(szDatabase),
467 uiUpdateCount, pHandle);
470 return ERROR_INVALID_PARAMETER;
474 LPCWSTR persist = uiUpdateCount ? MSIDBOPEN_TRANSACT : MSIDBOPEN_READONLY;
476 ret = MSI_OpenDatabaseW( szDatabase, persist, &db );
477 if( ret != ERROR_SUCCESS )
482 db = msihandle2msiinfo( hDatabase, MSIHANDLETYPE_DATABASE );
486 IWineMsiRemoteDatabase *remote_database;
488 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hDatabase );
489 if ( !remote_database )
490 return ERROR_INVALID_HANDLE;
492 hr = IWineMsiRemoteDatabase_GetSummaryInformation( remote_database,
493 uiUpdateCount, pHandle );
494 IWineMsiRemoteDatabase_Release( remote_database );
498 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
499 return HRESULT_CODE(hr);
501 return ERROR_FUNCTION_FAILED;
504 return ERROR_SUCCESS;
508 si = MSI_GetSummaryInformationW( db->storage, uiUpdateCount );
511 *pHandle = alloc_msihandle( &si->hdr );
515 ret = ERROR_NOT_ENOUGH_MEMORY;
516 msiobj_release( &si->hdr );
520 msiobj_release( &db->hdr );
525 UINT WINAPI MsiGetSummaryInformationA(MSIHANDLE hDatabase,
526 LPCSTR szDatabase, UINT uiUpdateCount, MSIHANDLE *pHandle)
528 LPWSTR szwDatabase = NULL;
531 TRACE("%d %s %d %p\n", hDatabase, debugstr_a(szDatabase),
532 uiUpdateCount, pHandle);
536 szwDatabase = strdupAtoW( szDatabase );
538 return ERROR_FUNCTION_FAILED;
541 ret = MsiGetSummaryInformationW(hDatabase, szwDatabase, uiUpdateCount, pHandle);
543 msi_free( szwDatabase );
548 UINT WINAPI MsiSummaryInfoGetPropertyCount(MSIHANDLE hSummaryInfo, PUINT pCount)
552 TRACE("%d %p\n", hSummaryInfo, pCount);
554 si = msihandle2msiinfo( hSummaryInfo, MSIHANDLETYPE_SUMMARYINFO );
556 return ERROR_INVALID_HANDLE;
559 *pCount = get_property_count( si->property );
560 msiobj_release( &si->hdr );
562 return ERROR_SUCCESS;
565 static UINT get_prop( MSIHANDLE handle, UINT uiProperty, UINT *puiDataType,
566 INT *piValue, FILETIME *pftValue, awstring *str, DWORD *pcchValueBuf)
570 UINT ret = ERROR_SUCCESS;
572 TRACE("%d %d %p %p %p %p %p\n", handle, uiProperty, puiDataType,
573 piValue, pftValue, str, pcchValueBuf);
575 if ( uiProperty >= MSI_MAX_PROPS )
577 if (puiDataType) *puiDataType = VT_EMPTY;
578 return ERROR_UNKNOWN_PROPERTY;
581 si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
583 return ERROR_INVALID_HANDLE;
585 prop = &si->property[uiProperty];
588 *puiDataType = prop->vt;
594 *piValue = prop->u.iVal;
598 *piValue = prop->u.lVal;
607 len = MultiByteToWideChar( CP_ACP, 0, prop->u.pszVal, -1, NULL, 0 ) - 1;
608 MultiByteToWideChar( CP_ACP, 0, prop->u.pszVal, -1, str->str.w, *pcchValueBuf );
612 len = lstrlenA( prop->u.pszVal );
614 lstrcpynA(str->str.a, prop->u.pszVal, *pcchValueBuf );
616 if (len >= *pcchValueBuf)
617 ret = ERROR_MORE_DATA;
623 *pftValue = prop->u.filetime;
628 FIXME("Unknown property variant type\n");
631 msiobj_release( &si->hdr );
635 LPWSTR msi_suminfo_dup_string( MSISUMMARYINFO *si, UINT uiProperty )
639 if ( uiProperty >= MSI_MAX_PROPS )
641 prop = &si->property[uiProperty];
642 if( prop->vt != VT_LPSTR )
644 return strdupAtoW( prop->u.pszVal );
647 LPWSTR msi_get_suminfo_product( IStorage *stg )
652 si = MSI_GetSummaryInformationW( stg, 0 );
655 ERR("no summary information!\n");
658 prod = msi_suminfo_dup_string( si, PID_REVNUMBER );
659 msiobj_release( &si->hdr );
663 UINT WINAPI MsiSummaryInfoGetPropertyA(
664 MSIHANDLE handle, UINT uiProperty, PUINT puiDataType, LPINT piValue,
665 FILETIME *pftValue, LPSTR szValueBuf, LPDWORD pcchValueBuf)
669 TRACE("%d %d %p %p %p %p %p\n", handle, uiProperty, puiDataType,
670 piValue, pftValue, szValueBuf, pcchValueBuf );
673 str.str.a = szValueBuf;
675 return get_prop( handle, uiProperty, puiDataType, piValue,
676 pftValue, &str, pcchValueBuf );
679 UINT WINAPI MsiSummaryInfoGetPropertyW(
680 MSIHANDLE handle, UINT uiProperty, PUINT puiDataType, LPINT piValue,
681 FILETIME *pftValue, LPWSTR szValueBuf, LPDWORD pcchValueBuf)
685 TRACE("%d %d %p %p %p %p %p\n", handle, uiProperty, puiDataType,
686 piValue, pftValue, szValueBuf, pcchValueBuf );
689 str.str.w = szValueBuf;
691 return get_prop( handle, uiProperty, puiDataType, piValue,
692 pftValue, &str, pcchValueBuf );
695 static UINT set_prop( MSISUMMARYINFO *si, UINT uiProperty, UINT type,
696 INT iValue, FILETIME* pftValue, awcstring *str )
701 TRACE("%p %u %u %i %p %p\n", si, uiProperty, type, iValue,
704 prop = &si->property[uiProperty];
706 if( prop->vt == VT_EMPTY )
708 if( !si->update_count )
709 return ERROR_FUNCTION_FAILED;
713 else if( prop->vt != type )
714 return ERROR_SUCCESS;
721 prop->u.lVal = iValue;
724 prop->u.iVal = iValue;
727 prop->u.filetime = *pftValue;
732 len = WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
733 NULL, 0, NULL, NULL );
734 prop->u.pszVal = msi_alloc( len );
735 WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
736 prop->u.pszVal, len, NULL, NULL );
740 len = lstrlenA( str->str.a ) + 1;
741 prop->u.pszVal = msi_alloc( len );
742 lstrcpyA( prop->u.pszVal, str->str.a );
747 return ERROR_SUCCESS;
750 UINT WINAPI MsiSummaryInfoSetPropertyW( MSIHANDLE handle, UINT uiProperty,
751 UINT uiDataType, INT iValue, FILETIME* pftValue, LPCWSTR szValue )
757 TRACE("%d %u %u %i %p %s\n", handle, uiProperty, uiDataType,
758 iValue, pftValue, debugstr_w(szValue) );
760 type = get_type( uiProperty );
761 if( type == VT_EMPTY || type != uiDataType )
762 return ERROR_DATATYPE_MISMATCH;
764 if( uiDataType == VT_LPSTR && !szValue )
765 return ERROR_INVALID_PARAMETER;
767 if( uiDataType == VT_FILETIME && !pftValue )
768 return ERROR_INVALID_PARAMETER;
770 si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
772 return ERROR_INVALID_HANDLE;
776 ret = set_prop( si, uiProperty, type, iValue, pftValue, &str );
778 msiobj_release( &si->hdr );
782 UINT WINAPI MsiSummaryInfoSetPropertyA( MSIHANDLE handle, UINT uiProperty,
783 UINT uiDataType, INT iValue, FILETIME* pftValue, LPCSTR szValue )
789 TRACE("%d %u %u %i %p %s\n", handle, uiProperty, uiDataType,
790 iValue, pftValue, debugstr_a(szValue) );
792 type = get_type( uiProperty );
793 if( type == VT_EMPTY || type != uiDataType )
794 return ERROR_DATATYPE_MISMATCH;
796 if( uiDataType == VT_LPSTR && !szValue )
797 return ERROR_INVALID_PARAMETER;
799 if( uiDataType == VT_FILETIME && !pftValue )
800 return ERROR_INVALID_PARAMETER;
802 si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
804 return ERROR_INVALID_HANDLE;
808 ret = set_prop( si, uiProperty, uiDataType, iValue, pftValue, &str );
810 msiobj_release( &si->hdr );
814 static UINT suminfo_persist( MSISUMMARYINFO *si )
816 UINT ret = ERROR_FUNCTION_FAILED;
821 grfMode = STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
822 r = IStorage_CreateStream( si->storage, szSumInfo, grfMode, 0, 0, &stm );
825 ret = save_summary_info( si, stm );
826 IStream_Release( stm );
831 static void parse_filetime( LPCWSTR str, FILETIME *ft )
834 const WCHAR *p = str;
837 memset( <, 0, sizeof(lt) );
839 /* YYYY/MM/DD hh:mm:ss */
841 while (isspaceW( *p )) p++;
843 lt.wYear = strtolW( p, &end, 10 );
844 if (*end != '/') return;
847 lt.wMonth = strtolW( p, &end, 10 );
848 if (*end != '/') return;
851 lt.wDay = strtolW( p, &end, 10 );
852 if (*end != ' ') return;
855 while (isspaceW( *p )) p++;
857 lt.wHour = strtolW( p, &end, 10 );
858 if (*end != ':') return;
861 lt.wMinute = strtolW( p, &end, 10 );
862 if (*end != ':') return;
865 lt.wSecond = strtolW( p, &end, 10 );
867 TzSpecificLocalTimeToSystemTime( NULL, <, &utc );
868 SystemTimeToFileTime( &utc, ft );
871 static UINT parse_prop( LPCWSTR prop, LPCWSTR value, UINT *pid, INT *int_value,
872 FILETIME *ft_value, awcstring *str_value )
874 *pid = atoiW( prop );
882 *int_value = atoiW( value );
885 case PID_LASTPRINTED:
887 case PID_LASTSAVE_DTM:
888 parse_filetime( value, ft_value );
900 str_value->str.w = value;
901 str_value->unicode = TRUE;
905 WARN("unhandled prop id %u\n", *pid);
906 return ERROR_FUNCTION_FAILED;
909 return ERROR_SUCCESS;
912 UINT msi_add_suminfo( MSIDATABASE *db, LPWSTR **records, int num_records, int num_columns )
914 UINT r = ERROR_FUNCTION_FAILED;
918 si = MSI_GetSummaryInformationW( db->storage, num_records * (num_columns / 2) );
921 ERR("no summary information!\n");
922 return ERROR_FUNCTION_FAILED;
925 for (i = 0; i < num_records; i++)
927 for (j = 0; j < num_columns; j += 2)
934 r = parse_prop( records[i][j], records[i][j + 1], &pid, &int_value, &ft_value, &str_value );
935 if (r != ERROR_SUCCESS)
938 r = set_prop( si, pid, get_type(pid), int_value, &ft_value, &str_value );
939 if (r != ERROR_SUCCESS)
945 if (r == ERROR_SUCCESS)
946 r = suminfo_persist( si );
948 msiobj_release( &si->hdr );
952 UINT WINAPI MsiSummaryInfoPersist( MSIHANDLE handle )
957 TRACE("%d\n", handle );
959 si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
961 return ERROR_INVALID_HANDLE;
963 ret = suminfo_persist( si );
965 msiobj_release( &si->hdr );