winhttp: Cache and reuse persistent HTTP connections.
[wine.git] / dlls / msi / suminfo.c
blob451fd16b81a3f6f7df62b39e593449acdcb4de93
1 /*
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
21 #include <stdarg.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "shlwapi.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33 #include "msi.h"
34 #include "msiquery.h"
35 #include "msidefs.h"
36 #include "msipriv.h"
37 #include "objidl.h"
38 #include "propvarutil.h"
39 #include "msiserver.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
43 #include "pshpack1.h"
45 typedef struct {
46 WORD wByteOrder;
47 WORD wFormat;
48 DWORD dwOSVer;
49 CLSID clsID;
50 DWORD reserved;
51 } PROPERTYSETHEADER;
53 typedef struct {
54 FMTID fmtid;
55 DWORD dwOffset;
56 } FORMATIDOFFSET;
58 typedef struct {
59 DWORD cbSection;
60 DWORD cProperties;
61 } PROPERTYSECTIONHEADER;
63 typedef struct {
64 DWORD propid;
65 DWORD dwOffset;
66 } PROPERTYIDOFFSET;
68 typedef struct {
69 DWORD type;
70 union {
71 INT i4;
72 SHORT i2;
73 FILETIME ft;
74 struct {
75 DWORD len;
76 BYTE str[1];
77 } str;
78 } u;
79 } PROPERTY_DATA;
81 #include "poppack.h"
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 );
93 prop->vt = VT_EMPTY;
96 static void MSI_CloseSummaryInfo( MSIOBJECTHDR *arg )
98 MSISUMMARYINFO *si = (MSISUMMARYINFO *) arg;
99 DWORD i;
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 )
108 switch( uiProperty )
110 case PID_CODEPAGE:
111 return VT_I2;
113 case PID_SUBJECT:
114 case PID_AUTHOR:
115 case PID_KEYWORDS:
116 case PID_COMMENTS:
117 case PID_TEMPLATE:
118 case PID_LASTAUTHOR:
119 case PID_REVNUMBER:
120 case PID_APPNAME:
121 case PID_TITLE:
122 return VT_LPSTR;
124 case PID_LASTPRINTED:
125 case PID_CREATE_DTM:
126 case PID_LASTSAVE_DTM:
127 return VT_FILETIME;
129 case PID_WORDCOUNT:
130 case PID_CHARCOUNT:
131 case PID_SECURITY:
132 case PID_PAGECOUNT:
133 return VT_I4;
135 return VT_EMPTY;
138 static UINT get_property_count( const PROPVARIANT *property )
140 UINT i, n = 0;
142 if( !property )
143 return n;
144 for( i = 0; i < MSI_MAX_PROPS; i++ )
145 if( property[i].vt != VT_EMPTY )
146 n++;
147 return n;
150 static UINT propvar_changetype(PROPVARIANT *changed, PROPVARIANT *property, VARTYPE vt)
152 HRESULT hr;
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 )
169 UINT type;
170 DWORD i, size;
171 PROPERTY_DATA *propdata;
172 PROPVARIANT property, *ptr;
173 PROPVARIANT changed;
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 );
186 break;
189 type = get_type( idofs[i].propid );
190 if( type == VT_EMPTY )
192 ERR("propid %d has unknown type\n", idofs[i].propid);
193 break;
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");
205 break;
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);
227 ptr = &changed;
229 else
230 ptr = &property;
232 prop[ idofs[i].propid ] = *ptr;
236 static UINT load_summary_info( MSISUMMARYINFO *si, IStream *stm )
238 PROPERTYSETHEADER set_hdr;
239 FORMATIDOFFSET format_hdr;
240 PROPERTYSECTIONHEADER section_hdr;
241 LPBYTE data = NULL;
242 LARGE_INTEGER ofs;
243 ULONG count, sz;
244 HRESULT r;
246 TRACE("%p %p\n", si, stm);
248 /* read the header */
249 sz = sizeof set_hdr;
250 r = IStream_Read( stm, &set_hdr, sz, &count );
251 if( FAILED(r) || count != sz )
252 return ERROR_FUNCTION_FAILED;
254 if( set_hdr.wByteOrder != 0xfffe )
256 ERR("property set not big-endian %04X\n", set_hdr.wByteOrder);
257 return ERROR_FUNCTION_FAILED;
260 sz = sizeof format_hdr;
261 r = IStream_Read( stm, &format_hdr, sz, &count );
262 if( FAILED(r) || count != sz )
263 return ERROR_FUNCTION_FAILED;
265 /* check the format id is correct */
266 if( !IsEqualGUID( &FMTID_SummaryInformation, &format_hdr.fmtid ) )
267 return ERROR_FUNCTION_FAILED;
269 /* seek to the location of the section */
270 ofs.QuadPart = format_hdr.dwOffset;
271 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, NULL );
272 if( FAILED(r) )
273 return ERROR_FUNCTION_FAILED;
275 /* read the section itself */
276 sz = SECT_HDR_SIZE;
277 r = IStream_Read( stm, &section_hdr, sz, &count );
278 if( FAILED(r) || count != sz )
279 return ERROR_FUNCTION_FAILED;
281 if( section_hdr.cProperties > MSI_MAX_PROPS )
283 ERR("too many properties %d\n", section_hdr.cProperties);
284 return ERROR_FUNCTION_FAILED;
287 data = msi_alloc( section_hdr.cbSection);
288 if( !data )
289 return ERROR_FUNCTION_FAILED;
291 memcpy( data, &section_hdr, SECT_HDR_SIZE );
293 /* read all the data in one go */
294 sz = section_hdr.cbSection - SECT_HDR_SIZE;
295 r = IStream_Read( stm, &data[ SECT_HDR_SIZE ], sz, &count );
296 if( SUCCEEDED(r) && count == sz )
297 read_properties_from_data( si->property, data, sz + SECT_HDR_SIZE );
298 else
299 ERR("failed to read properties %d %d\n", count, sz);
301 msi_free( data );
302 return ERROR_SUCCESS;
305 static DWORD write_dword( LPBYTE data, DWORD ofs, DWORD val )
307 if( data )
309 data[ofs++] = val&0xff;
310 data[ofs++] = (val>>8)&0xff;
311 data[ofs++] = (val>>16)&0xff;
312 data[ofs++] = (val>>24)&0xff;
314 return 4;
317 static DWORD write_filetime( LPBYTE data, DWORD ofs, const FILETIME *ft )
319 write_dword( data, ofs, ft->dwLowDateTime );
320 write_dword( data, ofs + 4, ft->dwHighDateTime );
321 return 8;
324 static DWORD write_string( LPBYTE data, DWORD ofs, LPCSTR str )
326 DWORD len = lstrlenA( str ) + 1;
327 write_dword( data, ofs, len );
328 if( data )
329 memcpy( &data[ofs + 4], str, len );
330 return (7 + len) & ~3;
333 static UINT write_property_to_data( const PROPVARIANT *prop, LPBYTE data )
335 DWORD sz = 0;
337 if( prop->vt == VT_EMPTY )
338 return sz;
340 /* add the type */
341 sz += write_dword( data, sz, prop->vt );
342 switch( prop->vt )
344 case VT_I2:
345 sz += write_dword( data, sz, prop->u.iVal );
346 break;
347 case VT_I4:
348 sz += write_dword( data, sz, prop->u.lVal );
349 break;
350 case VT_FILETIME:
351 sz += write_filetime( data, sz, &prop->u.filetime );
352 break;
353 case VT_LPSTR:
354 sz += write_string( data, sz, prop->u.pszVal );
355 break;
357 return sz;
360 static UINT save_summary_info( const MSISUMMARYINFO * si, IStream *stm )
362 UINT ret = ERROR_FUNCTION_FAILED;
363 PROPERTYSETHEADER set_hdr;
364 FORMATIDOFFSET format_hdr;
365 PROPERTYSECTIONHEADER section_hdr;
366 PROPERTYIDOFFSET idofs[MSI_MAX_PROPS];
367 LPBYTE data = NULL;
368 ULONG count, sz;
369 HRESULT r;
370 int i;
372 /* write the header */
373 sz = sizeof set_hdr;
374 memset( &set_hdr, 0, sz );
375 set_hdr.wByteOrder = 0xfffe;
376 set_hdr.wFormat = 0;
377 set_hdr.dwOSVer = 0x00020005; /* build 5, platform id 2 */
378 /* set_hdr.clsID is {00000000-0000-0000-0000-000000000000} */
379 set_hdr.reserved = 1;
380 r = IStream_Write( stm, &set_hdr, sz, &count );
381 if( FAILED(r) || count != sz )
382 return ret;
384 /* write the format header */
385 sz = sizeof format_hdr;
386 format_hdr.fmtid = FMTID_SummaryInformation;
387 format_hdr.dwOffset = sizeof format_hdr + sizeof set_hdr;
388 r = IStream_Write( stm, &format_hdr, sz, &count );
389 if( FAILED(r) || count != sz )
390 return ret;
392 /* add up how much space the data will take and calculate the offsets */
393 section_hdr.cbSection = sizeof section_hdr;
394 section_hdr.cbSection += (get_property_count( si->property ) * sizeof idofs[0]);
395 section_hdr.cProperties = 0;
396 for( i = 0; i < MSI_MAX_PROPS; i++ )
398 sz = write_property_to_data( &si->property[i], NULL );
399 if( !sz )
400 continue;
401 idofs[ section_hdr.cProperties ].propid = i;
402 idofs[ section_hdr.cProperties ].dwOffset = section_hdr.cbSection;
403 section_hdr.cProperties++;
404 section_hdr.cbSection += sz;
407 data = msi_alloc_zero( section_hdr.cbSection );
409 sz = 0;
410 memcpy( &data[sz], &section_hdr, sizeof section_hdr );
411 sz += sizeof section_hdr;
413 memcpy( &data[sz], idofs, section_hdr.cProperties * sizeof idofs[0] );
414 sz += section_hdr.cProperties * sizeof idofs[0];
416 /* write out the data */
417 for( i = 0; i < MSI_MAX_PROPS; i++ )
418 sz += write_property_to_data( &si->property[i], &data[sz] );
420 r = IStream_Write( stm, data, sz, &count );
421 msi_free( data );
422 if( FAILED(r) || count != sz )
423 return ret;
425 return ERROR_SUCCESS;
428 static MSISUMMARYINFO *create_suminfo( IStorage *stg, UINT update_count )
430 MSISUMMARYINFO *si;
432 if (!(si = alloc_msiobject( MSIHANDLETYPE_SUMMARYINFO, sizeof(MSISUMMARYINFO), MSI_CloseSummaryInfo )))
433 return NULL;
435 si->update_count = update_count;
436 IStorage_AddRef( stg );
437 si->storage = stg;
439 return si;
442 UINT msi_get_suminfo( IStorage *stg, UINT uiUpdateCount, MSISUMMARYINFO **ret )
444 IStream *stm;
445 MSISUMMARYINFO *si;
446 HRESULT hr;
447 UINT r;
449 TRACE("%p, %u\n", stg, uiUpdateCount);
451 if (!(si = create_suminfo( stg, uiUpdateCount ))) return ERROR_OUTOFMEMORY;
453 hr = IStorage_OpenStream( si->storage, szSumInfo, 0, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &stm );
454 if (FAILED( hr ))
456 msiobj_release( &si->hdr );
457 return ERROR_FUNCTION_FAILED;
460 r = load_summary_info( si, stm );
461 IStream_Release( stm );
462 if (r != ERROR_SUCCESS)
464 msiobj_release( &si->hdr );
465 return r;
468 *ret = si;
469 return ERROR_SUCCESS;
472 UINT msi_get_db_suminfo( MSIDATABASE *db, UINT uiUpdateCount, MSISUMMARYINFO **ret )
474 IStream *stm;
475 MSISUMMARYINFO *si;
476 UINT r;
478 if (!(si = create_suminfo( db->storage, uiUpdateCount ))) return ERROR_OUTOFMEMORY;
480 r = msi_get_stream( db, szSumInfo, &stm );
481 if (r != ERROR_SUCCESS)
483 msiobj_release( &si->hdr );
484 return r;
487 r = load_summary_info( si, stm );
488 IStream_Release( stm );
489 if (r != ERROR_SUCCESS)
491 msiobj_release( &si->hdr );
492 return r;
495 *ret = si;
496 return ERROR_SUCCESS;
499 UINT WINAPI MsiGetSummaryInformationW( MSIHANDLE hDatabase,
500 LPCWSTR szDatabase, UINT uiUpdateCount, MSIHANDLE *pHandle )
502 MSISUMMARYINFO *si;
503 MSIDATABASE *db;
504 UINT ret;
506 TRACE("%d %s %d %p\n", hDatabase, debugstr_w(szDatabase),
507 uiUpdateCount, pHandle);
509 if( !pHandle )
510 return ERROR_INVALID_PARAMETER;
512 if( szDatabase && szDatabase[0] )
514 LPCWSTR persist = uiUpdateCount ? MSIDBOPEN_TRANSACT : MSIDBOPEN_READONLY;
516 ret = MSI_OpenDatabaseW( szDatabase, persist, &db );
517 if( ret != ERROR_SUCCESS )
518 return ret;
520 else
522 db = msihandle2msiinfo( hDatabase, MSIHANDLETYPE_DATABASE );
523 if( !db )
525 HRESULT hr;
526 IWineMsiRemoteDatabase *remote_database;
528 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hDatabase );
529 if ( !remote_database )
530 return ERROR_INVALID_HANDLE;
532 hr = IWineMsiRemoteDatabase_GetSummaryInformation( remote_database,
533 uiUpdateCount, pHandle );
534 IWineMsiRemoteDatabase_Release( remote_database );
536 if (FAILED(hr))
538 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
539 return HRESULT_CODE(hr);
541 return ERROR_FUNCTION_FAILED;
544 return ERROR_SUCCESS;
548 ret = msi_get_suminfo( db->storage, uiUpdateCount, &si );
549 if (ret != ERROR_SUCCESS)
550 ret = msi_get_db_suminfo( db, uiUpdateCount, &si );
551 if (ret != ERROR_SUCCESS)
553 if ((si = create_suminfo( db->storage, uiUpdateCount )))
554 ret = ERROR_SUCCESS;
557 if (ret == ERROR_SUCCESS)
559 *pHandle = alloc_msihandle( &si->hdr );
560 if( *pHandle )
561 ret = ERROR_SUCCESS;
562 else
563 ret = ERROR_NOT_ENOUGH_MEMORY;
564 msiobj_release( &si->hdr );
567 msiobj_release( &db->hdr );
568 return ret;
571 UINT WINAPI MsiGetSummaryInformationA(MSIHANDLE hDatabase,
572 LPCSTR szDatabase, UINT uiUpdateCount, MSIHANDLE *pHandle)
574 LPWSTR szwDatabase = NULL;
575 UINT ret;
577 TRACE("%d %s %d %p\n", hDatabase, debugstr_a(szDatabase),
578 uiUpdateCount, pHandle);
580 if( szDatabase )
582 szwDatabase = strdupAtoW( szDatabase );
583 if( !szwDatabase )
584 return ERROR_FUNCTION_FAILED;
587 ret = MsiGetSummaryInformationW(hDatabase, szwDatabase, uiUpdateCount, pHandle);
589 msi_free( szwDatabase );
591 return ret;
594 UINT WINAPI MsiSummaryInfoGetPropertyCount(MSIHANDLE hSummaryInfo, PUINT pCount)
596 MSISUMMARYINFO *si;
598 TRACE("%d %p\n", hSummaryInfo, pCount);
600 si = msihandle2msiinfo( hSummaryInfo, MSIHANDLETYPE_SUMMARYINFO );
601 if( !si )
602 return ERROR_INVALID_HANDLE;
604 if( pCount )
605 *pCount = get_property_count( si->property );
606 msiobj_release( &si->hdr );
608 return ERROR_SUCCESS;
611 static UINT get_prop( MSISUMMARYINFO *si, UINT uiProperty, UINT *puiDataType, INT *piValue,
612 FILETIME *pftValue, awstring *str, DWORD *pcchValueBuf)
614 PROPVARIANT *prop;
615 UINT ret = ERROR_SUCCESS;
617 prop = &si->property[uiProperty];
619 if( puiDataType )
620 *puiDataType = prop->vt;
622 switch( prop->vt )
624 case VT_I2:
625 if( piValue )
626 *piValue = prop->u.iVal;
627 break;
628 case VT_I4:
629 if( piValue )
630 *piValue = prop->u.lVal;
631 break;
632 case VT_LPSTR:
633 if( pcchValueBuf )
635 DWORD len = 0;
637 if( str->unicode )
639 len = MultiByteToWideChar( CP_ACP, 0, prop->u.pszVal, -1, NULL, 0 ) - 1;
640 MultiByteToWideChar( CP_ACP, 0, prop->u.pszVal, -1, str->str.w, *pcchValueBuf );
642 else
644 len = lstrlenA( prop->u.pszVal );
645 if( str->str.a )
646 lstrcpynA(str->str.a, prop->u.pszVal, *pcchValueBuf );
648 if (len >= *pcchValueBuf)
649 ret = ERROR_MORE_DATA;
650 *pcchValueBuf = len;
652 break;
653 case VT_FILETIME:
654 if( pftValue )
655 *pftValue = prop->u.filetime;
656 break;
657 case VT_EMPTY:
658 break;
659 default:
660 FIXME("Unknown property variant type\n");
661 break;
663 return ret;
666 LPWSTR msi_suminfo_dup_string( MSISUMMARYINFO *si, UINT uiProperty )
668 PROPVARIANT *prop;
670 if ( uiProperty >= MSI_MAX_PROPS )
671 return NULL;
672 prop = &si->property[uiProperty];
673 if( prop->vt != VT_LPSTR )
674 return NULL;
675 return strdupAtoW( prop->u.pszVal );
678 INT msi_suminfo_get_int32( MSISUMMARYINFO *si, UINT uiProperty )
680 PROPVARIANT *prop;
682 if ( uiProperty >= MSI_MAX_PROPS )
683 return -1;
684 prop = &si->property[uiProperty];
685 if( prop->vt != VT_I4 )
686 return -1;
687 return prop->u.lVal;
690 LPWSTR msi_get_suminfo_product( IStorage *stg )
692 MSISUMMARYINFO *si;
693 LPWSTR prod;
694 UINT r;
696 r = msi_get_suminfo( stg, 0, &si );
697 if (r != ERROR_SUCCESS)
699 ERR("no summary information!\n");
700 return NULL;
702 prod = msi_suminfo_dup_string( si, PID_REVNUMBER );
703 msiobj_release( &si->hdr );
704 return prod;
707 UINT WINAPI MsiSummaryInfoGetPropertyA(
708 MSIHANDLE handle, UINT uiProperty, PUINT puiDataType, LPINT piValue,
709 FILETIME *pftValue, LPSTR szValueBuf, LPDWORD pcchValueBuf)
711 MSISUMMARYINFO *si;
712 awstring str;
713 UINT r;
715 TRACE("%u, %u, %p, %p, %p, %p, %p\n", handle, uiProperty, puiDataType,
716 piValue, pftValue, szValueBuf, pcchValueBuf );
718 if (uiProperty >= MSI_MAX_PROPS)
720 if (puiDataType) *puiDataType = VT_EMPTY;
721 return ERROR_UNKNOWN_PROPERTY;
724 if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
725 return ERROR_INVALID_HANDLE;
727 str.unicode = FALSE;
728 str.str.a = szValueBuf;
730 r = get_prop( si, uiProperty, puiDataType, piValue, pftValue, &str, pcchValueBuf );
731 msiobj_release( &si->hdr );
732 return r;
735 UINT WINAPI MsiSummaryInfoGetPropertyW(
736 MSIHANDLE handle, UINT uiProperty, PUINT puiDataType, LPINT piValue,
737 FILETIME *pftValue, LPWSTR szValueBuf, LPDWORD pcchValueBuf)
739 MSISUMMARYINFO *si;
740 awstring str;
741 UINT r;
743 TRACE("%u, %u, %p, %p, %p, %p, %p\n", handle, uiProperty, puiDataType,
744 piValue, pftValue, szValueBuf, pcchValueBuf );
746 if (uiProperty >= MSI_MAX_PROPS)
748 if (puiDataType) *puiDataType = VT_EMPTY;
749 return ERROR_UNKNOWN_PROPERTY;
752 if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
753 return ERROR_INVALID_HANDLE;
755 str.unicode = TRUE;
756 str.str.w = szValueBuf;
758 r = get_prop( si, uiProperty, puiDataType, piValue, pftValue, &str, pcchValueBuf );
759 msiobj_release( &si->hdr );
760 return r;
763 static UINT set_prop( MSISUMMARYINFO *si, UINT uiProperty, UINT type,
764 INT iValue, FILETIME *pftValue, awcstring *str )
766 PROPVARIANT *prop;
767 UINT len;
769 TRACE("%p, %u, %u, %d, %p, %p\n", si, uiProperty, type, iValue, pftValue, str );
771 prop = &si->property[uiProperty];
773 if( prop->vt == VT_EMPTY )
775 if( !si->update_count )
776 return ERROR_FUNCTION_FAILED;
778 si->update_count--;
780 else if( prop->vt != type )
781 return ERROR_SUCCESS;
783 free_prop( prop );
784 prop->vt = type;
785 switch( type )
787 case VT_I4:
788 prop->u.lVal = iValue;
789 break;
790 case VT_I2:
791 prop->u.iVal = iValue;
792 break;
793 case VT_FILETIME:
794 prop->u.filetime = *pftValue;
795 break;
796 case VT_LPSTR:
797 if( str->unicode )
799 len = WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
800 NULL, 0, NULL, NULL );
801 prop->u.pszVal = msi_alloc( len );
802 WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
803 prop->u.pszVal, len, NULL, NULL );
805 else
807 len = lstrlenA( str->str.a ) + 1;
808 prop->u.pszVal = msi_alloc( len );
809 lstrcpyA( prop->u.pszVal, str->str.a );
811 break;
814 return ERROR_SUCCESS;
817 UINT WINAPI MsiSummaryInfoSetPropertyW( MSIHANDLE handle, UINT uiProperty, UINT uiDataType,
818 INT iValue, FILETIME *pftValue, LPCWSTR szValue )
820 awcstring str;
821 MSISUMMARYINFO *si;
822 UINT type, ret;
824 TRACE("%u, %u, %u, %d, %p, %s\n", handle, uiProperty, uiDataType, iValue, pftValue,
825 debugstr_w(szValue) );
827 type = get_type( uiProperty );
828 if( type == VT_EMPTY || type != uiDataType )
829 return ERROR_DATATYPE_MISMATCH;
831 if( uiDataType == VT_LPSTR && !szValue )
832 return ERROR_INVALID_PARAMETER;
834 if( uiDataType == VT_FILETIME && !pftValue )
835 return ERROR_INVALID_PARAMETER;
837 if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
838 return ERROR_INVALID_HANDLE;
840 str.unicode = TRUE;
841 str.str.w = szValue;
843 ret = set_prop( si, uiProperty, type, iValue, pftValue, &str );
844 msiobj_release( &si->hdr );
845 return ret;
848 UINT WINAPI MsiSummaryInfoSetPropertyA( MSIHANDLE handle, UINT uiProperty, UINT uiDataType,
849 INT iValue, FILETIME *pftValue, LPCSTR szValue )
851 awcstring str;
852 MSISUMMARYINFO *si;
853 UINT type, ret;
855 TRACE("%u, %u, %u, %d, %p, %s\n", handle, uiProperty, uiDataType, iValue, pftValue,
856 debugstr_a(szValue) );
858 type = get_type( uiProperty );
859 if( type == VT_EMPTY || type != uiDataType )
860 return ERROR_DATATYPE_MISMATCH;
862 if( uiDataType == VT_LPSTR && !szValue )
863 return ERROR_INVALID_PARAMETER;
865 if( uiDataType == VT_FILETIME && !pftValue )
866 return ERROR_INVALID_PARAMETER;
868 if (!(si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO )))
869 return ERROR_INVALID_HANDLE;
871 str.unicode = FALSE;
872 str.str.a = szValue;
874 ret = set_prop( si, uiProperty, uiDataType, iValue, pftValue, &str );
875 msiobj_release( &si->hdr );
876 return ret;
879 static UINT suminfo_persist( MSISUMMARYINFO *si )
881 UINT ret = ERROR_FUNCTION_FAILED;
882 IStream *stm = NULL;
883 DWORD grfMode;
884 HRESULT r;
886 grfMode = STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
887 r = IStorage_CreateStream( si->storage, szSumInfo, grfMode, 0, 0, &stm );
888 if( SUCCEEDED(r) )
890 ret = save_summary_info( si, stm );
891 IStream_Release( stm );
893 return ret;
896 static void parse_filetime( LPCWSTR str, FILETIME *ft )
898 SYSTEMTIME lt, utc;
899 const WCHAR *p = str;
900 WCHAR *end;
902 memset( &lt, 0, sizeof(lt) );
904 /* YYYY/MM/DD hh:mm:ss */
906 while (isspaceW( *p )) p++;
908 lt.wYear = strtolW( p, &end, 10 );
909 if (*end != '/') return;
910 p = end + 1;
912 lt.wMonth = strtolW( p, &end, 10 );
913 if (*end != '/') return;
914 p = end + 1;
916 lt.wDay = strtolW( p, &end, 10 );
917 if (*end != ' ') return;
918 p = end + 1;
920 while (isspaceW( *p )) p++;
922 lt.wHour = strtolW( p, &end, 10 );
923 if (*end != ':') return;
924 p = end + 1;
926 lt.wMinute = strtolW( p, &end, 10 );
927 if (*end != ':') return;
928 p = end + 1;
930 lt.wSecond = strtolW( p, &end, 10 );
932 TzSpecificLocalTimeToSystemTime( NULL, &lt, &utc );
933 SystemTimeToFileTime( &utc, ft );
936 static UINT parse_prop( LPCWSTR prop, LPCWSTR value, UINT *pid, INT *int_value,
937 FILETIME *ft_value, awcstring *str_value )
939 *pid = atoiW( prop );
940 switch (*pid)
942 case PID_CODEPAGE:
943 case PID_WORDCOUNT:
944 case PID_CHARCOUNT:
945 case PID_SECURITY:
946 case PID_PAGECOUNT:
947 *int_value = atoiW( value );
948 break;
950 case PID_LASTPRINTED:
951 case PID_CREATE_DTM:
952 case PID_LASTSAVE_DTM:
953 parse_filetime( value, ft_value );
954 break;
956 case PID_SUBJECT:
957 case PID_AUTHOR:
958 case PID_KEYWORDS:
959 case PID_COMMENTS:
960 case PID_TEMPLATE:
961 case PID_LASTAUTHOR:
962 case PID_REVNUMBER:
963 case PID_APPNAME:
964 case PID_TITLE:
965 str_value->str.w = value;
966 str_value->unicode = TRUE;
967 break;
969 default:
970 WARN("unhandled prop id %u\n", *pid);
971 return ERROR_FUNCTION_FAILED;
974 return ERROR_SUCCESS;
977 UINT msi_add_suminfo( MSIDATABASE *db, LPWSTR **records, int num_records, int num_columns )
979 UINT r;
980 int i, j;
981 MSISUMMARYINFO *si;
983 r = msi_get_suminfo( db->storage, num_records * (num_columns / 2), &si );
984 if (r != ERROR_SUCCESS)
986 if (!(si = create_suminfo( db->storage, num_records * (num_columns / 2) )))
987 return ERROR_OUTOFMEMORY;
988 r = ERROR_SUCCESS;
991 for (i = 0; i < num_records; i++)
993 for (j = 0; j < num_columns; j += 2)
995 UINT pid;
996 INT int_value = 0;
997 FILETIME ft_value;
998 awcstring str_value;
1000 r = parse_prop( records[i][j], records[i][j + 1], &pid, &int_value, &ft_value, &str_value );
1001 if (r != ERROR_SUCCESS)
1002 goto end;
1004 r = set_prop( si, pid, get_type(pid), int_value, &ft_value, &str_value );
1005 if (r != ERROR_SUCCESS)
1006 goto end;
1010 end:
1011 if (r == ERROR_SUCCESS)
1012 r = suminfo_persist( si );
1014 msiobj_release( &si->hdr );
1015 return r;
1018 UINT WINAPI MsiSummaryInfoPersist( MSIHANDLE handle )
1020 MSISUMMARYINFO *si;
1021 UINT ret;
1023 TRACE("%d\n", handle );
1025 si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
1026 if( !si )
1027 return ERROR_INVALID_HANDLE;
1029 ret = suminfo_persist( si );
1031 msiobj_release( &si->hdr );
1032 return ret;
1035 UINT WINAPI MsiCreateTransformSummaryInfoA( MSIHANDLE db, MSIHANDLE db_ref, LPCSTR transform, int error, int validation )
1037 UINT r;
1038 WCHAR *transformW = NULL;
1040 TRACE("%u, %u, %s, %d, %d\n", db, db_ref, debugstr_a(transform), error, validation);
1042 if (transform && !(transformW = strdupAtoW( transform )))
1043 return ERROR_OUTOFMEMORY;
1045 r = MsiCreateTransformSummaryInfoW( db, db_ref, transformW, error, validation );
1046 msi_free( transformW );
1047 return r;
1050 UINT WINAPI MsiCreateTransformSummaryInfoW( MSIHANDLE db, MSIHANDLE db_ref, LPCWSTR transform, int error, int validation )
1052 FIXME("%u, %u, %s, %d, %d\n", db, db_ref, debugstr_w(transform), error, validation);
1053 return ERROR_FUNCTION_FAILED;
1056 UINT msi_load_suminfo_properties( MSIPACKAGE *package )
1058 static const WCHAR packagecodeW[] = {'P','a','c','k','a','g','e','C','o','d','e',0};
1059 MSISUMMARYINFO *si;
1060 WCHAR *package_code;
1061 UINT r, len;
1062 awstring str;
1063 INT count;
1065 r = msi_get_suminfo( package->db->storage, 0, &si );
1066 if (r != ERROR_SUCCESS)
1068 r = msi_get_db_suminfo( package->db, 0, &si );
1069 if (r != ERROR_SUCCESS)
1071 ERR("Unable to open summary information stream %u\n", r);
1072 return r;
1076 str.unicode = TRUE;
1077 str.str.w = NULL;
1078 len = 0;
1079 r = get_prop( si, PID_REVNUMBER, NULL, NULL, NULL, &str, &len );
1080 if (r != ERROR_MORE_DATA)
1082 WARN("Unable to query revision number %u\n", r);
1083 msiobj_release( &si->hdr );
1084 return ERROR_FUNCTION_FAILED;
1087 len++;
1088 if (!(package_code = msi_alloc( len * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
1089 str.str.w = package_code;
1091 r = get_prop( si, PID_REVNUMBER, NULL, NULL, NULL, &str, &len );
1092 if (r != ERROR_SUCCESS)
1094 msi_free( package_code );
1095 msiobj_release( &si->hdr );
1096 return r;
1099 r = msi_set_property( package->db, packagecodeW, package_code, len );
1100 msi_free( package_code );
1102 count = 0;
1103 get_prop( si, PID_WORDCOUNT, NULL, &count, NULL, NULL, NULL );
1104 package->WordCount = count;
1106 msiobj_release( &si->hdr );
1107 return r;