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
26 #define PRSPEC_PROPID (1)
33 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
60 } PROPERTYSECTIONHEADER
;
82 #define SECT_HDR_SIZE (sizeof(PROPERTYSECTIONHEADER))
84 static const WCHAR szSumInfo
[] = { 5 ,'S','u','m','m','a','r','y',
85 'I','n','f','o','r','m','a','t','i','o','n',0 };
87 static void free_prop( PROPVARIANT
*prop
)
89 if (prop
->vt
== VT_LPSTR
)
90 msi_free( prop
->u
.pszVal
);
94 static void MSI_CloseSummaryInfo( MSIOBJECTHDR
*arg
)
96 MSISUMMARYINFO
*si
= (MSISUMMARYINFO
*) arg
;
99 for( i
= 0; i
< MSI_MAX_PROPS
; i
++ )
100 free_prop( &si
->property
[i
] );
101 msiobj_release( &si
->db
->hdr
);
104 static UINT
get_type( UINT uiProperty
)
122 case PID_LASTPRINTED
:
124 case PID_LASTSAVE_DTM
:
136 static UINT
get_property_count( PROPVARIANT
*property
)
142 for( i
= 0; i
< MSI_MAX_PROPS
; i
++ )
143 if( property
[i
].vt
!= VT_EMPTY
)
148 /* FIXME: doesn't deal with endian conversion */
149 static void read_properties_from_data( PROPVARIANT
*prop
, LPBYTE data
, DWORD sz
)
154 PROPERTY_DATA
*propdata
;
155 PROPVARIANT
*property
;
156 PROPERTYIDOFFSET
*idofs
;
157 PROPERTYSECTIONHEADER
*section_hdr
;
159 section_hdr
= (PROPERTYSECTIONHEADER
*) &data
[0];
160 idofs
= (PROPERTYIDOFFSET
*) &data
[SECT_HDR_SIZE
];
162 /* now set all the properties */
163 for( i
= 0; i
< section_hdr
->cProperties
; i
++ )
165 type
= get_type( idofs
[i
].propid
);
166 if( type
== VT_EMPTY
)
168 ERR("propid %ld has unknown type\n", idofs
[i
].propid
);
172 propdata
= (PROPERTY_DATA
*) &data
[ idofs
[i
].dwOffset
];
174 /* check the type is the same as we expect */
175 if( type
!= propdata
->type
)
177 ERR("wrong type %d != %ld\n", type
, propdata
->type
);
181 /* check we don't run off the end of the data */
182 size
= sz
- idofs
[i
].dwOffset
- sizeof(DWORD
);
183 if( sizeof(DWORD
) > size
||
184 ( type
== VT_FILETIME
&& sizeof(FILETIME
) > size
) ||
185 ( type
== VT_LPSTR
&& (propdata
->u
.str
.len
+ sizeof(DWORD
)) > size
) )
187 ERR("not enough data\n");
191 if( idofs
[i
].propid
>= MSI_MAX_PROPS
)
193 ERR("Unknown property ID %ld\n", idofs
[i
].propid
);
197 property
= &prop
[ idofs
[i
].propid
];
200 if( type
== VT_LPSTR
)
202 LPSTR str
= msi_alloc( propdata
->u
.str
.len
);
203 memcpy( str
, propdata
->u
.str
.str
, propdata
->u
.str
.len
);
204 str
[ propdata
->u
.str
.len
- 1 ] = 0;
205 property
->u
.pszVal
= str
;
207 else if( type
== VT_FILETIME
)
208 property
->u
.filetime
= propdata
->u
.ft
;
209 else if( type
== VT_I2
)
210 property
->u
.iVal
= propdata
->u
.i2
;
211 else if( type
== VT_I4
)
212 property
->u
.lVal
= propdata
->u
.i4
;
216 static UINT
load_summary_info( MSISUMMARYINFO
*si
, IStream
*stm
)
218 UINT ret
= ERROR_FUNCTION_FAILED
;
219 PROPERTYSETHEADER set_hdr
;
220 FORMATIDOFFSET format_hdr
;
221 PROPERTYSECTIONHEADER section_hdr
;
227 TRACE("%p %p\n", si
, stm
);
229 /* read the header */
231 r
= IStream_Read( stm
, &set_hdr
, sz
, &count
);
232 if( FAILED(r
) || count
!= sz
)
235 if( set_hdr
.wByteOrder
!= 0xfffe )
237 ERR("property set not big-endian %04X\n", set_hdr
.wByteOrder
);
241 sz
= sizeof format_hdr
;
242 r
= IStream_Read( stm
, &format_hdr
, sz
, &count
);
243 if( FAILED(r
) || count
!= sz
)
246 /* check the format id is correct */
247 if( !IsEqualGUID( &FMTID_SummaryInformation
, &format_hdr
.fmtid
) )
250 /* seek to the location of the section */
251 ofs
.QuadPart
= format_hdr
.dwOffset
;
252 r
= IStream_Seek( stm
, ofs
, STREAM_SEEK_SET
, NULL
);
256 /* read the section itself */
258 r
= IStream_Read( stm
, §ion_hdr
, sz
, &count
);
259 if( FAILED(r
) || count
!= sz
)
262 if( section_hdr
.cProperties
> MSI_MAX_PROPS
)
264 ERR("too many properties %ld\n", section_hdr
.cProperties
);
268 data
= msi_alloc( section_hdr
.cbSection
);
272 memcpy( data
, §ion_hdr
, SECT_HDR_SIZE
);
274 /* read all the data in one go */
275 sz
= section_hdr
.cbSection
- SECT_HDR_SIZE
;
276 r
= IStream_Read( stm
, &data
[ SECT_HDR_SIZE
], sz
, &count
);
277 if( SUCCEEDED(r
) && count
== sz
)
278 read_properties_from_data( si
->property
, data
, sz
+ SECT_HDR_SIZE
);
280 ERR("failed to read properties %ld %ld\n", count
, sz
);
286 static DWORD
write_dword( LPBYTE data
, DWORD ofs
, DWORD val
)
290 data
[ofs
++] = val
&0xff;
291 data
[ofs
++] = (val
>>8)&0xff;
292 data
[ofs
++] = (val
>>16)&0xff;
293 data
[ofs
++] = (val
>>24)&0xff;
298 static DWORD
write_filetime( LPBYTE data
, DWORD ofs
, LPFILETIME ft
)
300 write_dword( data
, ofs
, ft
->dwLowDateTime
);
301 write_dword( data
, ofs
+ 4, ft
->dwHighDateTime
);
305 static DWORD
write_string( LPBYTE data
, DWORD ofs
, LPCSTR str
)
307 DWORD len
= lstrlenA( str
) + 1;
308 write_dword( data
, ofs
, len
);
310 memcpy( &data
[ofs
+ 4], str
, len
);
311 return (7 + len
) & ~3;
314 static UINT
write_property_to_data( PROPVARIANT
*prop
, LPBYTE data
)
318 if( prop
->vt
== VT_EMPTY
)
322 sz
+= write_dword( data
, sz
, prop
->vt
);
326 sz
+= write_dword( data
, sz
, prop
->u
.iVal
);
329 sz
+= write_dword( data
, sz
, prop
->u
.lVal
);
332 sz
+= write_filetime( data
, sz
, &prop
->u
.filetime
);
335 sz
+= write_string( data
, sz
, prop
->u
.pszVal
);
341 static UINT
save_summary_info( MSISUMMARYINFO
* si
, IStream
*stm
)
343 UINT ret
= ERROR_FUNCTION_FAILED
;
344 PROPERTYSETHEADER set_hdr
;
345 FORMATIDOFFSET format_hdr
;
346 PROPERTYSECTIONHEADER section_hdr
;
347 PROPERTYIDOFFSET idofs
[MSI_MAX_PROPS
];
353 /* write the header */
355 memset( &set_hdr
, 0, sz
);
356 set_hdr
.wByteOrder
= 0xfffe;
358 set_hdr
.dwOSVer
= 0x00020005; /* build 5, platform id 2 */
359 /* set_hdr.clsID is {00000000-0000-0000-0000-000000000000} */
360 set_hdr
.reserved
= 1;
361 r
= IStream_Write( stm
, &set_hdr
, sz
, &count
);
362 if( FAILED(r
) || count
!= sz
)
365 /* write the format header */
366 sz
= sizeof format_hdr
;
367 memcpy( &format_hdr
.fmtid
, &FMTID_SummaryInformation
, sizeof (FMTID
) );
368 format_hdr
.dwOffset
= sizeof format_hdr
+ sizeof set_hdr
;
369 r
= IStream_Write( stm
, &format_hdr
, sz
, &count
);
370 if( FAILED(r
) || count
!= sz
)
373 /* add up how much space the data will take and calculate the offsets */
374 section_hdr
.cbSection
= sizeof section_hdr
;
375 section_hdr
.cbSection
+= (get_property_count( si
->property
) * sizeof idofs
[0]);
376 section_hdr
.cProperties
= 0;
378 for( i
= 0; i
< MSI_MAX_PROPS
; i
++ )
380 sz
= write_property_to_data( &si
->property
[i
], NULL
);
383 idofs
[ section_hdr
.cProperties
].propid
= i
;
384 idofs
[ section_hdr
.cProperties
].dwOffset
= section_hdr
.cbSection
;
385 section_hdr
.cProperties
++;
386 section_hdr
.cbSection
+= sz
;
389 data
= msi_alloc_zero( section_hdr
.cbSection
);
392 memcpy( &data
[sz
], §ion_hdr
, sizeof section_hdr
);
393 sz
+= sizeof section_hdr
;
395 memcpy( &data
[sz
], idofs
, section_hdr
.cProperties
* sizeof idofs
[0] );
396 sz
+= section_hdr
.cProperties
* sizeof idofs
[0];
398 /* write out the data */
399 for( i
= 0; i
< MSI_MAX_PROPS
; i
++ )
400 sz
+= write_property_to_data( &si
->property
[i
], &data
[sz
] );
402 r
= IStream_Write( stm
, data
, sz
, &count
);
404 if( FAILED(r
) || count
!= sz
)
407 return ERROR_SUCCESS
;
410 MSISUMMARYINFO
*MSI_GetSummaryInformationW( MSIDATABASE
*db
, UINT uiUpdateCount
)
417 TRACE("%p %d\n", db
, uiUpdateCount
);
419 si
= alloc_msiobject( MSIHANDLETYPE_SUMMARYINFO
,
420 sizeof (MSISUMMARYINFO
), MSI_CloseSummaryInfo
);
424 msiobj_addref( &db
->hdr
);
426 memset( &si
->property
, 0, sizeof si
->property
);
427 si
->update_count
= uiUpdateCount
;
429 /* read the stream... if we fail, we'll start with an empty property set */
430 grfMode
= STGM_READ
| STGM_SHARE_EXCLUSIVE
;
431 r
= IStorage_OpenStream( si
->db
->storage
, szSumInfo
, 0, grfMode
, 0, &stm
);
434 load_summary_info( si
, stm
);
435 IStream_Release( stm
);
441 UINT WINAPI
MsiGetSummaryInformationW( MSIHANDLE hDatabase
,
442 LPCWSTR szDatabase
, UINT uiUpdateCount
, MSIHANDLE
*pHandle
)
446 UINT ret
= ERROR_FUNCTION_FAILED
;
448 TRACE("%ld %s %d %p\n", hDatabase
, debugstr_w(szDatabase
),
449 uiUpdateCount
, pHandle
);
452 return ERROR_INVALID_PARAMETER
;
456 ret
= MSI_OpenDatabaseW( szDatabase
, NULL
, &db
);
457 if( ret
!= ERROR_SUCCESS
)
462 db
= msihandle2msiinfo( hDatabase
, MSIHANDLETYPE_DATABASE
);
464 return ERROR_INVALID_PARAMETER
;
467 si
= MSI_GetSummaryInformationW( db
, uiUpdateCount
);
470 *pHandle
= alloc_msihandle( &si
->hdr
);
473 msiobj_release( &si
->hdr
);
477 msiobj_release( &db
->hdr
);
482 UINT WINAPI
MsiGetSummaryInformationA(MSIHANDLE hDatabase
,
483 LPCSTR szDatabase
, UINT uiUpdateCount
, MSIHANDLE
*pHandle
)
485 LPWSTR szwDatabase
= NULL
;
488 TRACE("%ld %s %d %p\n", hDatabase
, debugstr_a(szDatabase
),
489 uiUpdateCount
, pHandle
);
493 szwDatabase
= strdupAtoW( szDatabase
);
495 return ERROR_FUNCTION_FAILED
;
498 ret
= MsiGetSummaryInformationW(hDatabase
, szwDatabase
, uiUpdateCount
, pHandle
);
500 msi_free( szwDatabase
);
505 UINT WINAPI
MsiSummaryInfoGetPropertyCount(MSIHANDLE hSummaryInfo
, UINT
*pCount
)
509 TRACE("%ld %p\n",hSummaryInfo
, pCount
);
511 si
= msihandle2msiinfo( hSummaryInfo
, MSIHANDLETYPE_SUMMARYINFO
);
513 return ERROR_INVALID_HANDLE
;
516 *pCount
= get_property_count( si
->property
);
517 msiobj_release( &si
->hdr
);
519 return ERROR_SUCCESS
;
522 static UINT
get_prop( MSIHANDLE handle
, UINT uiProperty
, UINT
*puiDataType
,
523 INT
*piValue
, FILETIME
*pftValue
, awstring
*str
, DWORD
*pcchValueBuf
)
527 UINT ret
= ERROR_SUCCESS
;
529 TRACE("%ld %d %p %p %p %p %p\n", handle
, uiProperty
, puiDataType
,
530 piValue
, pftValue
, str
, pcchValueBuf
);
532 si
= msihandle2msiinfo( handle
, MSIHANDLETYPE_SUMMARYINFO
);
534 return ERROR_INVALID_HANDLE
;
536 if ( uiProperty
>= MSI_MAX_PROPS
)
538 *puiDataType
= VT_EMPTY
;
542 prop
= &si
->property
[uiProperty
];
545 *puiDataType
= prop
->vt
;
551 *piValue
= prop
->u
.iVal
;
555 *piValue
= prop
->u
.lVal
;
564 len
= MultiByteToWideChar( CP_ACP
, 0, prop
->u
.pszVal
, -1,
565 str
->str
.w
, *pcchValueBuf
);
570 len
= lstrlenA( prop
->u
.pszVal
);
572 lstrcpynA(str
->str
.a
, prop
->u
.pszVal
, *pcchValueBuf
);
574 if (len
>= *pcchValueBuf
)
575 ret
= ERROR_MORE_DATA
;
581 memcpy(pftValue
, &prop
->u
.filetime
, sizeof (FILETIME
) );
586 FIXME("Unknown property variant type\n");
589 msiobj_release( &si
->hdr
);
593 LPWSTR
msi_suminfo_dup_string( MSISUMMARYINFO
*si
, UINT uiProperty
)
597 if ( uiProperty
>= MSI_MAX_PROPS
)
599 prop
= &si
->property
[uiProperty
];
600 if( prop
->vt
!= VT_LPSTR
)
602 return strdupAtoW( prop
->u
.pszVal
);
605 UINT WINAPI
MsiSummaryInfoGetPropertyA(
606 MSIHANDLE handle
, UINT uiProperty
, UINT
*puiDataType
, INT
*piValue
,
607 FILETIME
*pftValue
, LPSTR szValueBuf
, DWORD
*pcchValueBuf
)
611 TRACE("%ld %d %p %p %p %p %p\n", handle
, uiProperty
, puiDataType
,
612 piValue
, pftValue
, szValueBuf
, pcchValueBuf
);
615 str
.str
.a
= szValueBuf
;
617 return get_prop( handle
, uiProperty
, puiDataType
, piValue
,
618 pftValue
, &str
, pcchValueBuf
);
621 UINT WINAPI
MsiSummaryInfoGetPropertyW(
622 MSIHANDLE handle
, UINT uiProperty
, UINT
*puiDataType
, INT
*piValue
,
623 FILETIME
*pftValue
, LPWSTR szValueBuf
, DWORD
*pcchValueBuf
)
627 TRACE("%ld %d %p %p %p %p %p\n", handle
, uiProperty
, puiDataType
,
628 piValue
, pftValue
, szValueBuf
, pcchValueBuf
);
631 str
.str
.w
= szValueBuf
;
633 return get_prop( handle
, uiProperty
, puiDataType
, piValue
,
634 pftValue
, &str
, pcchValueBuf
);
637 static UINT
set_prop( MSIHANDLE handle
, UINT uiProperty
, UINT uiDataType
,
638 INT iValue
, FILETIME
* pftValue
, awcstring
*str
)
642 UINT type
, len
, ret
= ERROR_SUCCESS
;
644 TRACE("%ld %u %u %i %p %p\n", handle
, uiProperty
, uiDataType
,
645 iValue
, pftValue
, str
);
647 type
= get_type( uiProperty
);
648 if( type
== VT_EMPTY
|| type
!= uiDataType
)
649 return ERROR_DATATYPE_MISMATCH
;
651 if( uiDataType
== VT_LPSTR
&& !str
->str
.w
)
652 return ERROR_INVALID_PARAMETER
;
654 if( uiDataType
== VT_FILETIME
&& !pftValue
)
655 return ERROR_INVALID_PARAMETER
;
657 si
= msihandle2msiinfo( handle
, MSIHANDLETYPE_SUMMARYINFO
);
659 return ERROR_INVALID_HANDLE
;
661 prop
= &si
->property
[uiProperty
];
663 if( prop
->vt
== VT_EMPTY
)
665 if( !si
->update_count
)
667 ret
= ERROR_FUNCTION_FAILED
;
672 else if( prop
->vt
!= type
)
680 prop
->u
.lVal
= iValue
;
683 prop
->u
.iVal
= iValue
;
686 memcpy( &prop
->u
.filetime
, pftValue
, sizeof prop
->u
.filetime
);
691 len
= WideCharToMultiByte( CP_ACP
, 0, str
->str
.w
, -1,
692 NULL
, 0, NULL
, NULL
);
693 prop
->u
.pszVal
= msi_alloc( len
);
694 WideCharToMultiByte( CP_ACP
, 0, str
->str
.w
, -1,
695 prop
->u
.pszVal
, len
, NULL
, NULL
);
699 len
= lstrlenA( str
->str
.a
) + 1;
700 prop
->u
.pszVal
= msi_alloc( len
);
701 lstrcpyA( prop
->u
.pszVal
, str
->str
.a
);
707 msiobj_release( &si
->hdr
);
711 UINT WINAPI
MsiSummaryInfoSetPropertyW( MSIHANDLE handle
, UINT uiProperty
,
712 UINT uiDataType
, INT iValue
, FILETIME
* pftValue
, LPCWSTR szValue
)
716 TRACE("%ld %u %u %i %p %s\n", handle
, uiProperty
, uiDataType
,
717 iValue
, pftValue
, debugstr_w(szValue
) );
721 return set_prop( handle
, uiProperty
, uiDataType
, iValue
, pftValue
, &str
);
724 UINT WINAPI
MsiSummaryInfoSetPropertyA( MSIHANDLE handle
, UINT uiProperty
,
725 UINT uiDataType
, INT iValue
, FILETIME
* pftValue
, LPCSTR szValue
)
729 TRACE("%ld %u %u %i %p %s\n", handle
, uiProperty
, uiDataType
,
730 iValue
, pftValue
, debugstr_a(szValue
) );
734 return set_prop( handle
, uiProperty
, uiDataType
, iValue
, pftValue
, &str
);
737 UINT WINAPI
MsiSummaryInfoPersist( MSIHANDLE handle
)
743 UINT ret
= ERROR_FUNCTION_FAILED
;
745 TRACE("%ld\n", handle
);
747 si
= msihandle2msiinfo( handle
, MSIHANDLETYPE_SUMMARYINFO
);
749 return ERROR_INVALID_HANDLE
;
751 grfMode
= STGM_CREATE
| STGM_READWRITE
| STGM_SHARE_EXCLUSIVE
;
752 r
= IStorage_CreateStream( si
->db
->storage
, szSumInfo
, grfMode
, 0, 0, &stm
);
755 ret
= save_summary_info( si
, stm
);
756 IStream_Release( stm
);
758 msiobj_release( &si
->hdr
);