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"
38 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
58 } PROPERTYSECTIONHEADER
;
80 #define SECT_HDR_SIZE (sizeof(PROPERTYSECTIONHEADER))
82 static const WCHAR szSumInfo
[] = { 5 ,'S','u','m','m','a','r','y',
83 'I','n','f','o','r','m','a','t','i','o','n',0 };
85 static void free_prop( PROPVARIANT
*prop
)
87 if (prop
->vt
== VT_LPSTR
)
88 msi_free( prop
->u
.pszVal
);
92 static void MSI_CloseSummaryInfo( MSIOBJECTHDR
*arg
)
94 MSISUMMARYINFO
*si
= (MSISUMMARYINFO
*) arg
;
97 for( i
= 0; i
< MSI_MAX_PROPS
; i
++ )
98 free_prop( &si
->property
[i
] );
99 msiobj_release( &si
->db
->hdr
);
102 static UINT
get_type( UINT uiProperty
)
120 case PID_LASTPRINTED
:
122 case PID_LASTSAVE_DTM
:
134 static UINT
get_property_count( PROPVARIANT
*property
)
140 for( i
= 0; i
< MSI_MAX_PROPS
; i
++ )
141 if( property
[i
].vt
!= VT_EMPTY
)
146 /* FIXME: doesn't deal with endian conversion */
147 static void read_properties_from_data( PROPVARIANT
*prop
, LPBYTE data
, DWORD sz
)
152 PROPERTY_DATA
*propdata
;
153 PROPVARIANT
*property
;
154 PROPERTYIDOFFSET
*idofs
;
155 PROPERTYSECTIONHEADER
*section_hdr
;
157 section_hdr
= (PROPERTYSECTIONHEADER
*) &data
[0];
158 idofs
= (PROPERTYIDOFFSET
*) &data
[SECT_HDR_SIZE
];
160 /* now set all the properties */
161 for( i
= 0; i
< section_hdr
->cProperties
; i
++ )
163 type
= get_type( idofs
[i
].propid
);
164 if( type
== VT_EMPTY
)
166 ERR("propid %ld has unknown type\n", idofs
[i
].propid
);
170 propdata
= (PROPERTY_DATA
*) &data
[ idofs
[i
].dwOffset
];
172 /* check the type is the same as we expect */
173 if( type
!= propdata
->type
)
175 ERR("wrong type %d != %ld\n", type
, propdata
->type
);
179 /* check we don't run off the end of the data */
180 size
= sz
- idofs
[i
].dwOffset
- sizeof(DWORD
);
181 if( sizeof(DWORD
) > size
||
182 ( type
== VT_FILETIME
&& sizeof(FILETIME
) > size
) ||
183 ( type
== VT_LPSTR
&& (propdata
->u
.str
.len
+ sizeof(DWORD
)) > size
) )
185 ERR("not enough data\n");
189 if( idofs
[i
].propid
>= MSI_MAX_PROPS
)
191 ERR("Unknown property ID %ld\n", idofs
[i
].propid
);
195 property
= &prop
[ idofs
[i
].propid
];
198 if( type
== VT_LPSTR
)
200 LPSTR str
= msi_alloc( propdata
->u
.str
.len
);
201 memcpy( str
, propdata
->u
.str
.str
, propdata
->u
.str
.len
);
202 str
[ propdata
->u
.str
.len
- 1 ] = 0;
203 property
->u
.pszVal
= str
;
205 else if( type
== VT_FILETIME
)
206 property
->u
.filetime
= propdata
->u
.ft
;
207 else if( type
== VT_I2
)
208 property
->u
.iVal
= propdata
->u
.i2
;
209 else if( type
== VT_I4
)
210 property
->u
.lVal
= propdata
->u
.i4
;
214 static UINT
load_summary_info( MSISUMMARYINFO
*si
, IStream
*stm
)
216 UINT ret
= ERROR_FUNCTION_FAILED
;
217 PROPERTYSETHEADER set_hdr
;
218 FORMATIDOFFSET format_hdr
;
219 PROPERTYSECTIONHEADER section_hdr
;
225 TRACE("%p %p\n", si
, stm
);
227 /* read the header */
229 r
= IStream_Read( stm
, &set_hdr
, sz
, &count
);
230 if( FAILED(r
) || count
!= sz
)
233 if( set_hdr
.wByteOrder
!= 0xfffe )
235 ERR("property set not big-endian %04X\n", set_hdr
.wByteOrder
);
239 sz
= sizeof format_hdr
;
240 r
= IStream_Read( stm
, &format_hdr
, sz
, &count
);
241 if( FAILED(r
) || count
!= sz
)
244 /* check the format id is correct */
245 if( !IsEqualGUID( &FMTID_SummaryInformation
, &format_hdr
.fmtid
) )
248 /* seek to the location of the section */
249 ofs
.QuadPart
= format_hdr
.dwOffset
;
250 r
= IStream_Seek( stm
, ofs
, STREAM_SEEK_SET
, NULL
);
254 /* read the section itself */
256 r
= IStream_Read( stm
, §ion_hdr
, sz
, &count
);
257 if( FAILED(r
) || count
!= sz
)
260 if( section_hdr
.cProperties
> MSI_MAX_PROPS
)
262 ERR("too many properties %ld\n", section_hdr
.cProperties
);
266 data
= msi_alloc( section_hdr
.cbSection
);
270 memcpy( data
, §ion_hdr
, SECT_HDR_SIZE
);
272 /* read all the data in one go */
273 sz
= section_hdr
.cbSection
- SECT_HDR_SIZE
;
274 r
= IStream_Read( stm
, &data
[ SECT_HDR_SIZE
], sz
, &count
);
275 if( SUCCEEDED(r
) && count
== sz
)
276 read_properties_from_data( si
->property
, data
, sz
+ SECT_HDR_SIZE
);
278 ERR("failed to read properties %ld %ld\n", count
, sz
);
284 static DWORD
write_dword( LPBYTE data
, DWORD ofs
, DWORD val
)
288 data
[ofs
++] = val
&0xff;
289 data
[ofs
++] = (val
>>8)&0xff;
290 data
[ofs
++] = (val
>>16)&0xff;
291 data
[ofs
++] = (val
>>24)&0xff;
296 static DWORD
write_filetime( LPBYTE data
, DWORD ofs
, LPFILETIME ft
)
298 write_dword( data
, ofs
, ft
->dwLowDateTime
);
299 write_dword( data
, ofs
+ 4, ft
->dwHighDateTime
);
303 static DWORD
write_string( LPBYTE data
, DWORD ofs
, LPCSTR str
)
305 DWORD len
= lstrlenA( str
) + 1;
306 write_dword( data
, ofs
, len
);
308 memcpy( &data
[ofs
+ 4], str
, len
);
309 return (7 + len
) & ~3;
312 static UINT
write_property_to_data( PROPVARIANT
*prop
, LPBYTE data
)
316 if( prop
->vt
== VT_EMPTY
)
320 sz
+= write_dword( data
, sz
, prop
->vt
);
324 sz
+= write_dword( data
, sz
, prop
->u
.iVal
);
327 sz
+= write_dword( data
, sz
, prop
->u
.lVal
);
330 sz
+= write_filetime( data
, sz
, &prop
->u
.filetime
);
333 sz
+= write_string( data
, sz
, prop
->u
.pszVal
);
339 static UINT
save_summary_info( MSISUMMARYINFO
* si
, IStream
*stm
)
341 UINT ret
= ERROR_FUNCTION_FAILED
;
342 PROPERTYSETHEADER set_hdr
;
343 FORMATIDOFFSET format_hdr
;
344 PROPERTYSECTIONHEADER section_hdr
;
345 PROPERTYIDOFFSET idofs
[MSI_MAX_PROPS
];
351 /* write the header */
353 memset( &set_hdr
, 0, sz
);
354 set_hdr
.wByteOrder
= 0xfffe;
356 set_hdr
.dwOSVer
= 0x00020005; /* build 5, platform id 2 */
357 /* set_hdr.clsID is {00000000-0000-0000-0000-000000000000} */
358 set_hdr
.reserved
= 1;
359 r
= IStream_Write( stm
, &set_hdr
, sz
, &count
);
360 if( FAILED(r
) || count
!= sz
)
363 /* write the format header */
364 sz
= sizeof format_hdr
;
365 memcpy( &format_hdr
.fmtid
, &FMTID_SummaryInformation
, sizeof (FMTID
) );
366 format_hdr
.dwOffset
= sizeof format_hdr
+ sizeof set_hdr
;
367 r
= IStream_Write( stm
, &format_hdr
, sz
, &count
);
368 if( FAILED(r
) || count
!= sz
)
371 /* add up how much space the data will take and calculate the offsets */
372 section_hdr
.cbSection
= sizeof section_hdr
;
373 section_hdr
.cbSection
+= (get_property_count( si
->property
) * sizeof idofs
[0]);
374 section_hdr
.cProperties
= 0;
376 for( i
= 0; i
< MSI_MAX_PROPS
; i
++ )
378 sz
= write_property_to_data( &si
->property
[i
], NULL
);
381 idofs
[ section_hdr
.cProperties
].propid
= i
;
382 idofs
[ section_hdr
.cProperties
].dwOffset
= section_hdr
.cbSection
;
383 section_hdr
.cProperties
++;
384 section_hdr
.cbSection
+= sz
;
387 data
= msi_alloc_zero( section_hdr
.cbSection
);
390 memcpy( &data
[sz
], §ion_hdr
, sizeof section_hdr
);
391 sz
+= sizeof section_hdr
;
393 memcpy( &data
[sz
], idofs
, section_hdr
.cProperties
* sizeof idofs
[0] );
394 sz
+= section_hdr
.cProperties
* sizeof idofs
[0];
396 /* write out the data */
397 for( i
= 0; i
< MSI_MAX_PROPS
; i
++ )
398 sz
+= write_property_to_data( &si
->property
[i
], &data
[sz
] );
400 r
= IStream_Write( stm
, data
, sz
, &count
);
402 if( FAILED(r
) || count
!= sz
)
405 return ERROR_SUCCESS
;
408 MSISUMMARYINFO
*MSI_GetSummaryInformationW( MSIDATABASE
*db
, UINT uiUpdateCount
)
415 TRACE("%p %d\n", db
, uiUpdateCount
);
417 si
= alloc_msiobject( MSIHANDLETYPE_SUMMARYINFO
,
418 sizeof (MSISUMMARYINFO
), MSI_CloseSummaryInfo
);
422 msiobj_addref( &db
->hdr
);
424 memset( &si
->property
, 0, sizeof si
->property
);
425 si
->update_count
= uiUpdateCount
;
427 /* read the stream... if we fail, we'll start with an empty property set */
428 grfMode
= STGM_READ
| STGM_SHARE_EXCLUSIVE
;
429 r
= IStorage_OpenStream( si
->db
->storage
, szSumInfo
, 0, grfMode
, 0, &stm
);
432 load_summary_info( si
, stm
);
433 IStream_Release( stm
);
439 UINT WINAPI
MsiGetSummaryInformationW( MSIHANDLE hDatabase
,
440 LPCWSTR szDatabase
, UINT uiUpdateCount
, MSIHANDLE
*pHandle
)
444 UINT ret
= ERROR_FUNCTION_FAILED
;
446 TRACE("%ld %s %d %p\n", hDatabase
, debugstr_w(szDatabase
),
447 uiUpdateCount
, pHandle
);
450 return ERROR_INVALID_PARAMETER
;
454 ret
= MSI_OpenDatabaseW( szDatabase
, NULL
, &db
);
455 if( ret
!= ERROR_SUCCESS
)
460 db
= msihandle2msiinfo( hDatabase
, MSIHANDLETYPE_DATABASE
);
462 return ERROR_INVALID_PARAMETER
;
465 si
= MSI_GetSummaryInformationW( db
, uiUpdateCount
);
468 *pHandle
= alloc_msihandle( &si
->hdr
);
471 msiobj_release( &si
->hdr
);
475 msiobj_release( &db
->hdr
);
480 UINT WINAPI
MsiGetSummaryInformationA(MSIHANDLE hDatabase
,
481 LPCSTR szDatabase
, UINT uiUpdateCount
, MSIHANDLE
*pHandle
)
483 LPWSTR szwDatabase
= NULL
;
486 TRACE("%ld %s %d %p\n", hDatabase
, debugstr_a(szDatabase
),
487 uiUpdateCount
, pHandle
);
491 szwDatabase
= strdupAtoW( szDatabase
);
493 return ERROR_FUNCTION_FAILED
;
496 ret
= MsiGetSummaryInformationW(hDatabase
, szwDatabase
, uiUpdateCount
, pHandle
);
498 msi_free( szwDatabase
);
503 UINT WINAPI
MsiSummaryInfoGetPropertyCount(MSIHANDLE hSummaryInfo
, UINT
*pCount
)
507 TRACE("%ld %p\n",hSummaryInfo
, pCount
);
509 si
= msihandle2msiinfo( hSummaryInfo
, MSIHANDLETYPE_SUMMARYINFO
);
511 return ERROR_INVALID_HANDLE
;
514 *pCount
= get_property_count( si
->property
);
515 msiobj_release( &si
->hdr
);
517 return ERROR_SUCCESS
;
520 static UINT
get_prop( MSIHANDLE handle
, UINT uiProperty
, UINT
*puiDataType
,
521 INT
*piValue
, FILETIME
*pftValue
, awstring
*str
, DWORD
*pcchValueBuf
)
525 UINT ret
= ERROR_SUCCESS
;
527 TRACE("%ld %d %p %p %p %p %p\n", handle
, uiProperty
, puiDataType
,
528 piValue
, pftValue
, str
, pcchValueBuf
);
530 si
= msihandle2msiinfo( handle
, MSIHANDLETYPE_SUMMARYINFO
);
532 return ERROR_INVALID_HANDLE
;
534 if ( uiProperty
>= MSI_MAX_PROPS
)
536 *puiDataType
= VT_EMPTY
;
540 prop
= &si
->property
[uiProperty
];
543 *puiDataType
= prop
->vt
;
549 *piValue
= prop
->u
.iVal
;
553 *piValue
= prop
->u
.lVal
;
562 len
= MultiByteToWideChar( CP_ACP
, 0, prop
->u
.pszVal
, -1,
563 str
->str
.w
, *pcchValueBuf
);
568 len
= lstrlenA( prop
->u
.pszVal
);
570 lstrcpynA(str
->str
.a
, prop
->u
.pszVal
, *pcchValueBuf
);
572 if (len
>= *pcchValueBuf
)
573 ret
= ERROR_MORE_DATA
;
579 memcpy(pftValue
, &prop
->u
.filetime
, sizeof (FILETIME
) );
584 FIXME("Unknown property variant type\n");
587 msiobj_release( &si
->hdr
);
591 LPWSTR
msi_suminfo_dup_string( MSISUMMARYINFO
*si
, UINT uiProperty
)
595 if ( uiProperty
>= MSI_MAX_PROPS
)
597 prop
= &si
->property
[uiProperty
];
598 if( prop
->vt
!= VT_LPSTR
)
600 return strdupAtoW( prop
->u
.pszVal
);
603 UINT WINAPI
MsiSummaryInfoGetPropertyA(
604 MSIHANDLE handle
, UINT uiProperty
, UINT
*puiDataType
, INT
*piValue
,
605 FILETIME
*pftValue
, LPSTR szValueBuf
, DWORD
*pcchValueBuf
)
609 TRACE("%ld %d %p %p %p %p %p\n", handle
, uiProperty
, puiDataType
,
610 piValue
, pftValue
, szValueBuf
, pcchValueBuf
);
613 str
.str
.a
= szValueBuf
;
615 return get_prop( handle
, uiProperty
, puiDataType
, piValue
,
616 pftValue
, &str
, pcchValueBuf
);
619 UINT WINAPI
MsiSummaryInfoGetPropertyW(
620 MSIHANDLE handle
, UINT uiProperty
, UINT
*puiDataType
, INT
*piValue
,
621 FILETIME
*pftValue
, LPWSTR szValueBuf
, DWORD
*pcchValueBuf
)
625 TRACE("%ld %d %p %p %p %p %p\n", handle
, uiProperty
, puiDataType
,
626 piValue
, pftValue
, szValueBuf
, pcchValueBuf
);
629 str
.str
.w
= szValueBuf
;
631 return get_prop( handle
, uiProperty
, puiDataType
, piValue
,
632 pftValue
, &str
, pcchValueBuf
);
635 static UINT
set_prop( MSIHANDLE handle
, UINT uiProperty
, UINT uiDataType
,
636 INT iValue
, FILETIME
* pftValue
, awcstring
*str
)
640 UINT type
, len
, ret
= ERROR_SUCCESS
;
642 TRACE("%ld %u %u %i %p %p\n", handle
, uiProperty
, uiDataType
,
643 iValue
, pftValue
, str
);
645 type
= get_type( uiProperty
);
646 if( type
== VT_EMPTY
|| type
!= uiDataType
)
647 return ERROR_DATATYPE_MISMATCH
;
649 if( uiDataType
== VT_LPSTR
&& !str
->str
.w
)
650 return ERROR_INVALID_PARAMETER
;
652 if( uiDataType
== VT_FILETIME
&& !pftValue
)
653 return ERROR_INVALID_PARAMETER
;
655 si
= msihandle2msiinfo( handle
, MSIHANDLETYPE_SUMMARYINFO
);
657 return ERROR_INVALID_HANDLE
;
659 prop
= &si
->property
[uiProperty
];
661 if( prop
->vt
== VT_EMPTY
)
663 if( !si
->update_count
)
665 ret
= ERROR_FUNCTION_FAILED
;
670 else if( prop
->vt
!= type
)
678 prop
->u
.lVal
= iValue
;
681 prop
->u
.iVal
= iValue
;
684 memcpy( &prop
->u
.filetime
, pftValue
, sizeof prop
->u
.filetime
);
689 len
= WideCharToMultiByte( CP_ACP
, 0, str
->str
.w
, -1,
690 NULL
, 0, NULL
, NULL
);
691 prop
->u
.pszVal
= msi_alloc( len
);
692 WideCharToMultiByte( CP_ACP
, 0, str
->str
.w
, -1,
693 prop
->u
.pszVal
, len
, NULL
, NULL
);
697 len
= lstrlenA( str
->str
.a
) + 1;
698 prop
->u
.pszVal
= msi_alloc( len
);
699 lstrcpyA( prop
->u
.pszVal
, str
->str
.a
);
705 msiobj_release( &si
->hdr
);
709 UINT WINAPI
MsiSummaryInfoSetPropertyW( MSIHANDLE handle
, UINT uiProperty
,
710 UINT uiDataType
, INT iValue
, FILETIME
* pftValue
, LPCWSTR szValue
)
714 TRACE("%ld %u %u %i %p %s\n", handle
, uiProperty
, uiDataType
,
715 iValue
, pftValue
, debugstr_w(szValue
) );
719 return set_prop( handle
, uiProperty
, uiDataType
, iValue
, pftValue
, &str
);
722 UINT WINAPI
MsiSummaryInfoSetPropertyA( MSIHANDLE handle
, UINT uiProperty
,
723 UINT uiDataType
, INT iValue
, FILETIME
* pftValue
, LPCSTR szValue
)
727 TRACE("%ld %u %u %i %p %s\n", handle
, uiProperty
, uiDataType
,
728 iValue
, pftValue
, debugstr_a(szValue
) );
732 return set_prop( handle
, uiProperty
, uiDataType
, iValue
, pftValue
, &str
);
735 UINT WINAPI
MsiSummaryInfoPersist( MSIHANDLE handle
)
741 UINT ret
= ERROR_FUNCTION_FAILED
;
743 TRACE("%ld\n", handle
);
745 si
= msihandle2msiinfo( handle
, MSIHANDLETYPE_SUMMARYINFO
);
747 return ERROR_INVALID_HANDLE
;
749 grfMode
= STGM_CREATE
| STGM_READWRITE
| STGM_SHARE_EXCLUSIVE
;
750 r
= IStorage_CreateStream( si
->db
->storage
, szSumInfo
, grfMode
, 0, 0, &stm
);
753 ret
= save_summary_info( si
, stm
);
754 IStream_Release( stm
);
756 msiobj_release( &si
->hdr
);