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"
37 #include "msiserver.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
59 } PROPERTYSECTIONHEADER
;
81 #define SECT_HDR_SIZE (sizeof(PROPERTYSECTIONHEADER))
83 static const WCHAR szSumInfo
[] = { 5 ,'S','u','m','m','a','r','y',
84 'I','n','f','o','r','m','a','t','i','o','n',0 };
86 static void free_prop( PROPVARIANT
*prop
)
88 if (prop
->vt
== VT_LPSTR
)
89 msi_free( prop
->u
.pszVal
);
93 static void MSI_CloseSummaryInfo( MSIOBJECTHDR
*arg
)
95 MSISUMMARYINFO
*si
= (MSISUMMARYINFO
*) arg
;
98 for( i
= 0; i
< MSI_MAX_PROPS
; i
++ )
99 free_prop( &si
->property
[i
] );
100 IStorage_Release( si
->storage
);
103 static UINT
get_type( UINT uiProperty
)
121 case PID_LASTPRINTED
:
123 case PID_LASTSAVE_DTM
:
135 static UINT
get_property_count( const PROPVARIANT
*property
)
141 for( i
= 0; i
< MSI_MAX_PROPS
; i
++ )
142 if( property
[i
].vt
!= VT_EMPTY
)
147 /* FIXME: doesn't deal with endian conversion */
148 static void read_properties_from_data( PROPVARIANT
*prop
, LPBYTE data
, DWORD sz
)
153 PROPERTY_DATA
*propdata
;
154 PROPVARIANT
*property
;
155 PROPERTYIDOFFSET
*idofs
;
156 PROPERTYSECTIONHEADER
*section_hdr
;
158 section_hdr
= (PROPERTYSECTIONHEADER
*) &data
[0];
159 idofs
= (PROPERTYIDOFFSET
*) &data
[SECT_HDR_SIZE
];
161 /* now set all the properties */
162 for( i
= 0; i
< section_hdr
->cProperties
; i
++ )
164 type
= get_type( idofs
[i
].propid
);
165 if( type
== VT_EMPTY
)
167 ERR("propid %d has unknown type\n", idofs
[i
].propid
);
171 propdata
= (PROPERTY_DATA
*) &data
[ idofs
[i
].dwOffset
];
173 /* check the type is the same as we expect */
174 if( type
!= propdata
->type
)
176 ERR("wrong type %d != %d\n", type
, propdata
->type
);
180 /* check we don't run off the end of the data */
181 size
= sz
- idofs
[i
].dwOffset
- sizeof(DWORD
);
182 if( sizeof(DWORD
) > size
||
183 ( type
== VT_FILETIME
&& sizeof(FILETIME
) > size
) ||
184 ( type
== VT_LPSTR
&& (propdata
->u
.str
.len
+ sizeof(DWORD
)) > size
) )
186 ERR("not enough data\n");
190 if( idofs
[i
].propid
>= MSI_MAX_PROPS
)
192 ERR("Unknown property ID %d\n", idofs
[i
].propid
);
196 property
= &prop
[ idofs
[i
].propid
];
199 if( type
== VT_LPSTR
)
201 LPSTR str
= msi_alloc( propdata
->u
.str
.len
);
202 memcpy( str
, propdata
->u
.str
.str
, propdata
->u
.str
.len
);
203 str
[ propdata
->u
.str
.len
- 1 ] = 0;
204 property
->u
.pszVal
= str
;
206 else if( type
== VT_FILETIME
)
207 property
->u
.filetime
= propdata
->u
.ft
;
208 else if( type
== VT_I2
)
209 property
->u
.iVal
= propdata
->u
.i2
;
210 else if( type
== VT_I4
)
211 property
->u
.lVal
= propdata
->u
.i4
;
215 static UINT
load_summary_info( MSISUMMARYINFO
*si
, IStream
*stm
)
217 UINT ret
= ERROR_FUNCTION_FAILED
;
218 PROPERTYSETHEADER set_hdr
;
219 FORMATIDOFFSET format_hdr
;
220 PROPERTYSECTIONHEADER section_hdr
;
226 TRACE("%p %p\n", si
, stm
);
228 /* read the header */
230 r
= IStream_Read( stm
, &set_hdr
, sz
, &count
);
231 if( FAILED(r
) || count
!= sz
)
234 if( set_hdr
.wByteOrder
!= 0xfffe )
236 ERR("property set not big-endian %04X\n", set_hdr
.wByteOrder
);
240 sz
= sizeof format_hdr
;
241 r
= IStream_Read( stm
, &format_hdr
, sz
, &count
);
242 if( FAILED(r
) || count
!= sz
)
245 /* check the format id is correct */
246 if( !IsEqualGUID( &FMTID_SummaryInformation
, &format_hdr
.fmtid
) )
249 /* seek to the location of the section */
250 ofs
.QuadPart
= format_hdr
.dwOffset
;
251 r
= IStream_Seek( stm
, ofs
, STREAM_SEEK_SET
, NULL
);
255 /* read the section itself */
257 r
= IStream_Read( stm
, §ion_hdr
, sz
, &count
);
258 if( FAILED(r
) || count
!= sz
)
261 if( section_hdr
.cProperties
> MSI_MAX_PROPS
)
263 ERR("too many properties %d\n", section_hdr
.cProperties
);
267 data
= msi_alloc( section_hdr
.cbSection
);
271 memcpy( data
, §ion_hdr
, SECT_HDR_SIZE
);
273 /* read all the data in one go */
274 sz
= section_hdr
.cbSection
- SECT_HDR_SIZE
;
275 r
= IStream_Read( stm
, &data
[ SECT_HDR_SIZE
], sz
, &count
);
276 if( SUCCEEDED(r
) && count
== sz
)
277 read_properties_from_data( si
->property
, data
, sz
+ SECT_HDR_SIZE
);
279 ERR("failed to read properties %d %d\n", count
, sz
);
285 static DWORD
write_dword( LPBYTE data
, DWORD ofs
, DWORD val
)
289 data
[ofs
++] = val
&0xff;
290 data
[ofs
++] = (val
>>8)&0xff;
291 data
[ofs
++] = (val
>>16)&0xff;
292 data
[ofs
++] = (val
>>24)&0xff;
297 static DWORD
write_filetime( LPBYTE data
, DWORD ofs
, const FILETIME
*ft
)
299 write_dword( data
, ofs
, ft
->dwLowDateTime
);
300 write_dword( data
, ofs
+ 4, ft
->dwHighDateTime
);
304 static DWORD
write_string( LPBYTE data
, DWORD ofs
, LPCSTR str
)
306 DWORD len
= lstrlenA( str
) + 1;
307 write_dword( data
, ofs
, len
);
309 memcpy( &data
[ofs
+ 4], str
, len
);
310 return (7 + len
) & ~3;
313 static UINT
write_property_to_data( const PROPVARIANT
*prop
, LPBYTE data
)
317 if( prop
->vt
== VT_EMPTY
)
321 sz
+= write_dword( data
, sz
, prop
->vt
);
325 sz
+= write_dword( data
, sz
, prop
->u
.iVal
);
328 sz
+= write_dword( data
, sz
, prop
->u
.lVal
);
331 sz
+= write_filetime( data
, sz
, &prop
->u
.filetime
);
334 sz
+= write_string( data
, sz
, prop
->u
.pszVal
);
340 static UINT
save_summary_info( const MSISUMMARYINFO
* si
, IStream
*stm
)
342 UINT ret
= ERROR_FUNCTION_FAILED
;
343 PROPERTYSETHEADER set_hdr
;
344 FORMATIDOFFSET format_hdr
;
345 PROPERTYSECTIONHEADER section_hdr
;
346 PROPERTYIDOFFSET idofs
[MSI_MAX_PROPS
];
352 /* write the header */
354 memset( &set_hdr
, 0, sz
);
355 set_hdr
.wByteOrder
= 0xfffe;
357 set_hdr
.dwOSVer
= 0x00020005; /* build 5, platform id 2 */
358 /* set_hdr.clsID is {00000000-0000-0000-0000-000000000000} */
359 set_hdr
.reserved
= 1;
360 r
= IStream_Write( stm
, &set_hdr
, sz
, &count
);
361 if( FAILED(r
) || count
!= sz
)
364 /* write the format header */
365 sz
= sizeof format_hdr
;
366 memcpy( &format_hdr
.fmtid
, &FMTID_SummaryInformation
, sizeof (FMTID
) );
367 format_hdr
.dwOffset
= sizeof format_hdr
+ sizeof set_hdr
;
368 r
= IStream_Write( stm
, &format_hdr
, sz
, &count
);
369 if( FAILED(r
) || count
!= sz
)
372 /* add up how much space the data will take and calculate the offsets */
373 section_hdr
.cbSection
= sizeof section_hdr
;
374 section_hdr
.cbSection
+= (get_property_count( si
->property
) * sizeof idofs
[0]);
375 section_hdr
.cProperties
= 0;
377 for( i
= 0; i
< MSI_MAX_PROPS
; i
++ )
379 sz
= write_property_to_data( &si
->property
[i
], NULL
);
382 idofs
[ section_hdr
.cProperties
].propid
= i
;
383 idofs
[ section_hdr
.cProperties
].dwOffset
= section_hdr
.cbSection
;
384 section_hdr
.cProperties
++;
385 section_hdr
.cbSection
+= sz
;
388 data
= msi_alloc_zero( section_hdr
.cbSection
);
391 memcpy( &data
[sz
], §ion_hdr
, sizeof section_hdr
);
392 sz
+= sizeof section_hdr
;
394 memcpy( &data
[sz
], idofs
, section_hdr
.cProperties
* sizeof idofs
[0] );
395 sz
+= section_hdr
.cProperties
* sizeof idofs
[0];
397 /* write out the data */
398 for( i
= 0; i
< MSI_MAX_PROPS
; i
++ )
399 sz
+= write_property_to_data( &si
->property
[i
], &data
[sz
] );
401 r
= IStream_Write( stm
, data
, sz
, &count
);
403 if( FAILED(r
) || count
!= sz
)
406 return ERROR_SUCCESS
;
409 MSISUMMARYINFO
*MSI_GetSummaryInformationW( IStorage
*stg
, UINT uiUpdateCount
)
416 TRACE("%p %d\n", stg
, uiUpdateCount
);
418 si
= alloc_msiobject( MSIHANDLETYPE_SUMMARYINFO
,
419 sizeof (MSISUMMARYINFO
), MSI_CloseSummaryInfo
);
423 memset( &si
->property
, 0, sizeof si
->property
);
424 si
->update_count
= uiUpdateCount
;
425 IStorage_AddRef( stg
);
428 /* read the stream... if we fail, we'll start with an empty property set */
429 grfMode
= STGM_READ
| STGM_SHARE_EXCLUSIVE
;
430 r
= IStorage_OpenStream( si
->storage
, szSumInfo
, 0, grfMode
, 0, &stm
);
433 load_summary_info( si
, stm
);
434 IStream_Release( stm
);
440 UINT WINAPI
MsiGetSummaryInformationW( MSIHANDLE hDatabase
,
441 LPCWSTR szDatabase
, UINT uiUpdateCount
, MSIHANDLE
*pHandle
)
445 UINT ret
= ERROR_FUNCTION_FAILED
;
447 TRACE("%ld %s %d %p\n", hDatabase
, debugstr_w(szDatabase
),
448 uiUpdateCount
, pHandle
);
451 return ERROR_INVALID_PARAMETER
;
455 ret
= MSI_OpenDatabaseW( szDatabase
, NULL
, &db
);
456 if( ret
!= ERROR_SUCCESS
)
461 db
= msihandle2msiinfo( hDatabase
, MSIHANDLETYPE_DATABASE
);
465 IWineMsiRemoteDatabase
*remote_database
;
467 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( hDatabase
);
468 if ( !remote_database
)
469 return ERROR_INVALID_HANDLE
;
471 hr
= IWineMsiRemoteDatabase_GetSummaryInformation( remote_database
,
472 uiUpdateCount
, pHandle
);
473 IWineMsiRemoteDatabase_Release( remote_database
);
477 if (HRESULT_FACILITY(hr
) == FACILITY_WIN32
)
478 return HRESULT_CODE(hr
);
480 return ERROR_FUNCTION_FAILED
;
483 return ERROR_SUCCESS
;
487 si
= MSI_GetSummaryInformationW( db
->storage
, uiUpdateCount
);
490 *pHandle
= alloc_msihandle( &si
->hdr
);
494 ret
= ERROR_NOT_ENOUGH_MEMORY
;
495 msiobj_release( &si
->hdr
);
499 msiobj_release( &db
->hdr
);
504 UINT WINAPI
MsiGetSummaryInformationA(MSIHANDLE hDatabase
,
505 LPCSTR szDatabase
, UINT uiUpdateCount
, MSIHANDLE
*pHandle
)
507 LPWSTR szwDatabase
= NULL
;
510 TRACE("%ld %s %d %p\n", hDatabase
, debugstr_a(szDatabase
),
511 uiUpdateCount
, pHandle
);
515 szwDatabase
= strdupAtoW( szDatabase
);
517 return ERROR_FUNCTION_FAILED
;
520 ret
= MsiGetSummaryInformationW(hDatabase
, szwDatabase
, uiUpdateCount
, pHandle
);
522 msi_free( szwDatabase
);
527 UINT WINAPI
MsiSummaryInfoGetPropertyCount(MSIHANDLE hSummaryInfo
, PUINT pCount
)
531 TRACE("%ld %p\n", hSummaryInfo
, pCount
);
533 si
= msihandle2msiinfo( hSummaryInfo
, MSIHANDLETYPE_SUMMARYINFO
);
535 return ERROR_INVALID_HANDLE
;
538 *pCount
= get_property_count( si
->property
);
539 msiobj_release( &si
->hdr
);
541 return ERROR_SUCCESS
;
544 static UINT
get_prop( MSIHANDLE handle
, UINT uiProperty
, UINT
*puiDataType
,
545 INT
*piValue
, FILETIME
*pftValue
, awstring
*str
, DWORD
*pcchValueBuf
)
549 UINT ret
= ERROR_SUCCESS
;
551 TRACE("%ld %d %p %p %p %p %p\n", handle
, uiProperty
, puiDataType
,
552 piValue
, pftValue
, str
, pcchValueBuf
);
554 if ( uiProperty
>= MSI_MAX_PROPS
)
556 if (puiDataType
) *puiDataType
= VT_EMPTY
;
557 return ERROR_UNKNOWN_PROPERTY
;
560 si
= msihandle2msiinfo( handle
, MSIHANDLETYPE_SUMMARYINFO
);
562 return ERROR_INVALID_HANDLE
;
564 prop
= &si
->property
[uiProperty
];
567 *puiDataType
= prop
->vt
;
573 *piValue
= prop
->u
.iVal
;
577 *piValue
= prop
->u
.lVal
;
586 len
= MultiByteToWideChar( CP_ACP
, 0, prop
->u
.pszVal
, -1,
587 str
->str
.w
, *pcchValueBuf
);
592 len
= lstrlenA( prop
->u
.pszVal
);
594 lstrcpynA(str
->str
.a
, prop
->u
.pszVal
, *pcchValueBuf
);
596 if (len
>= *pcchValueBuf
)
597 ret
= ERROR_MORE_DATA
;
603 memcpy(pftValue
, &prop
->u
.filetime
, sizeof (FILETIME
) );
608 FIXME("Unknown property variant type\n");
611 msiobj_release( &si
->hdr
);
615 LPWSTR
msi_suminfo_dup_string( MSISUMMARYINFO
*si
, UINT uiProperty
)
619 if ( uiProperty
>= MSI_MAX_PROPS
)
621 prop
= &si
->property
[uiProperty
];
622 if( prop
->vt
!= VT_LPSTR
)
624 return strdupAtoW( prop
->u
.pszVal
);
627 LPWSTR
msi_get_suminfo_product( IStorage
*stg
)
632 si
= MSI_GetSummaryInformationW( stg
, 0 );
635 ERR("no summary information!\n");
638 prod
= msi_suminfo_dup_string( si
, PID_REVNUMBER
);
639 msiobj_release( &si
->hdr
);
643 UINT WINAPI
MsiSummaryInfoGetPropertyA(
644 MSIHANDLE handle
, UINT uiProperty
, PUINT puiDataType
, LPINT piValue
,
645 FILETIME
*pftValue
, LPSTR szValueBuf
, LPDWORD pcchValueBuf
)
649 TRACE("%ld %d %p %p %p %p %p\n", handle
, uiProperty
, puiDataType
,
650 piValue
, pftValue
, szValueBuf
, pcchValueBuf
);
653 str
.str
.a
= szValueBuf
;
655 return get_prop( handle
, uiProperty
, puiDataType
, piValue
,
656 pftValue
, &str
, pcchValueBuf
);
659 UINT WINAPI
MsiSummaryInfoGetPropertyW(
660 MSIHANDLE handle
, UINT uiProperty
, PUINT puiDataType
, LPINT piValue
,
661 FILETIME
*pftValue
, LPWSTR szValueBuf
, LPDWORD pcchValueBuf
)
665 TRACE("%ld %d %p %p %p %p %p\n", handle
, uiProperty
, puiDataType
,
666 piValue
, pftValue
, szValueBuf
, pcchValueBuf
);
669 str
.str
.w
= szValueBuf
;
671 return get_prop( handle
, uiProperty
, puiDataType
, piValue
,
672 pftValue
, &str
, pcchValueBuf
);
675 static UINT
set_prop( MSIHANDLE handle
, UINT uiProperty
, UINT uiDataType
,
676 INT iValue
, FILETIME
* pftValue
, awcstring
*str
)
680 UINT type
, len
, ret
= ERROR_SUCCESS
;
682 TRACE("%ld %u %u %i %p %p\n", handle
, uiProperty
, uiDataType
,
683 iValue
, pftValue
, str
);
685 type
= get_type( uiProperty
);
686 if( type
== VT_EMPTY
|| type
!= uiDataType
)
687 return ERROR_DATATYPE_MISMATCH
;
689 if( uiDataType
== VT_LPSTR
&& !str
->str
.w
)
690 return ERROR_INVALID_PARAMETER
;
692 if( uiDataType
== VT_FILETIME
&& !pftValue
)
693 return ERROR_INVALID_PARAMETER
;
695 si
= msihandle2msiinfo( handle
, MSIHANDLETYPE_SUMMARYINFO
);
697 return ERROR_INVALID_HANDLE
;
699 prop
= &si
->property
[uiProperty
];
701 if( prop
->vt
== VT_EMPTY
)
703 if( !si
->update_count
)
705 ret
= ERROR_FUNCTION_FAILED
;
710 else if( prop
->vt
!= type
)
718 prop
->u
.lVal
= iValue
;
721 prop
->u
.iVal
= iValue
;
724 memcpy( &prop
->u
.filetime
, pftValue
, sizeof prop
->u
.filetime
);
729 len
= WideCharToMultiByte( CP_ACP
, 0, str
->str
.w
, -1,
730 NULL
, 0, NULL
, NULL
);
731 prop
->u
.pszVal
= msi_alloc( len
);
732 WideCharToMultiByte( CP_ACP
, 0, str
->str
.w
, -1,
733 prop
->u
.pszVal
, len
, NULL
, NULL
);
737 len
= lstrlenA( str
->str
.a
) + 1;
738 prop
->u
.pszVal
= msi_alloc( len
);
739 lstrcpyA( prop
->u
.pszVal
, str
->str
.a
);
745 msiobj_release( &si
->hdr
);
749 UINT WINAPI
MsiSummaryInfoSetPropertyW( MSIHANDLE handle
, UINT uiProperty
,
750 UINT uiDataType
, INT iValue
, FILETIME
* pftValue
, LPCWSTR szValue
)
754 TRACE("%ld %u %u %i %p %s\n", handle
, uiProperty
, uiDataType
,
755 iValue
, pftValue
, debugstr_w(szValue
) );
759 return set_prop( handle
, uiProperty
, uiDataType
, iValue
, pftValue
, &str
);
762 UINT WINAPI
MsiSummaryInfoSetPropertyA( MSIHANDLE handle
, UINT uiProperty
,
763 UINT uiDataType
, INT iValue
, FILETIME
* pftValue
, LPCSTR szValue
)
767 TRACE("%ld %u %u %i %p %s\n", handle
, uiProperty
, uiDataType
,
768 iValue
, pftValue
, debugstr_a(szValue
) );
772 return set_prop( handle
, uiProperty
, uiDataType
, iValue
, pftValue
, &str
);
775 UINT WINAPI
MsiSummaryInfoPersist( MSIHANDLE handle
)
781 UINT ret
= ERROR_FUNCTION_FAILED
;
783 TRACE("%ld\n", handle
);
785 si
= msihandle2msiinfo( handle
, MSIHANDLETYPE_SUMMARYINFO
);
787 return ERROR_INVALID_HANDLE
;
789 grfMode
= STGM_CREATE
| STGM_READWRITE
| STGM_SHARE_EXCLUSIVE
;
790 r
= IStorage_CreateStream( si
->storage
, szSumInfo
, grfMode
, 0, 0, &stm
);
793 ret
= save_summary_info( si
, stm
);
794 IStream_Release( stm
);
796 msiobj_release( &si
->hdr
);