sync msi to wine 1.1.31
[reactos.git] / reactos / dll / win32 / msi / suminfo.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2002, 2005 Mike McCormack for CodeWeavers
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24 #define NONAMELESSUNION
25
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"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
42
43 #include "pshpack1.h"
44
45 typedef struct {
46 WORD wByteOrder;
47 WORD wFormat;
48 DWORD dwOSVer;
49 CLSID clsID;
50 DWORD reserved;
51 } PROPERTYSETHEADER;
52
53 typedef struct {
54 FMTID fmtid;
55 DWORD dwOffset;
56 } FORMATIDOFFSET;
57
58 typedef struct {
59 DWORD cbSection;
60 DWORD cProperties;
61 } PROPERTYSECTIONHEADER;
62
63 typedef struct {
64 DWORD propid;
65 DWORD dwOffset;
66 } PROPERTYIDOFFSET;
67
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;
80
81 #include "poppack.h"
82
83 static HRESULT (WINAPI *pPropVariantChangeType)
84 (PROPVARIANT *ppropvarDest, REFPROPVARIANT propvarSrc,
85 PROPVAR_CHANGE_FLAGS flags, VARTYPE vt);
86
87 #define SECT_HDR_SIZE (sizeof(PROPERTYSECTIONHEADER))
88
89 static const WCHAR szSumInfo[] = { 5 ,'S','u','m','m','a','r','y',
90 'I','n','f','o','r','m','a','t','i','o','n',0 };
91
92 static void free_prop( PROPVARIANT *prop )
93 {
94 if (prop->vt == VT_LPSTR )
95 msi_free( prop->u.pszVal );
96 prop->vt = VT_EMPTY;
97 }
98
99 static void MSI_CloseSummaryInfo( MSIOBJECTHDR *arg )
100 {
101 MSISUMMARYINFO *si = (MSISUMMARYINFO *) arg;
102 DWORD i;
103
104 for( i = 0; i < MSI_MAX_PROPS; i++ )
105 free_prop( &si->property[i] );
106 IStorage_Release( si->storage );
107 }
108
109 static UINT get_type( UINT uiProperty )
110 {
111 switch( uiProperty )
112 {
113 case PID_CODEPAGE:
114 return VT_I2;
115
116 case PID_SUBJECT:
117 case PID_AUTHOR:
118 case PID_KEYWORDS:
119 case PID_COMMENTS:
120 case PID_TEMPLATE:
121 case PID_LASTAUTHOR:
122 case PID_REVNUMBER:
123 case PID_APPNAME:
124 case PID_TITLE:
125 return VT_LPSTR;
126
127 case PID_LASTPRINTED:
128 case PID_CREATE_DTM:
129 case PID_LASTSAVE_DTM:
130 return VT_FILETIME;
131
132 case PID_WORDCOUNT:
133 case PID_CHARCOUNT:
134 case PID_SECURITY:
135 case PID_PAGECOUNT:
136 return VT_I4;
137 }
138 return VT_EMPTY;
139 }
140
141 static UINT get_property_count( const PROPVARIANT *property )
142 {
143 UINT i, n = 0;
144
145 if( !property )
146 return n;
147 for( i = 0; i < MSI_MAX_PROPS; i++ )
148 if( property[i].vt != VT_EMPTY )
149 n++;
150 return n;
151 }
152
153 static UINT propvar_changetype(PROPVARIANT *changed, PROPVARIANT *property, VARTYPE vt)
154 {
155 HRESULT hr;
156 HMODULE propsys = LoadLibraryA("propsys.dll");
157 pPropVariantChangeType = (void *)GetProcAddress(propsys, "PropVariantChangeType");
158
159 if (!pPropVariantChangeType)
160 {
161 ERR("PropVariantChangeType function missing!\n");
162 return ERROR_FUNCTION_FAILED;
163 }
164
165 hr = pPropVariantChangeType(changed, property, 0, vt);
166 return (hr == S_OK) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
167 }
168
169 /* FIXME: doesn't deal with endian conversion */
170 static void read_properties_from_data( PROPVARIANT *prop, LPBYTE data, DWORD sz )
171 {
172 UINT type;
173 DWORD i, size;
174 PROPERTY_DATA *propdata;
175 PROPVARIANT property, *ptr;
176 PROPVARIANT changed;
177 PROPERTYIDOFFSET *idofs;
178 PROPERTYSECTIONHEADER *section_hdr;
179
180 section_hdr = (PROPERTYSECTIONHEADER*) &data[0];
181 idofs = (PROPERTYIDOFFSET*) &data[SECT_HDR_SIZE];
182
183 /* now set all the properties */
184 for( i = 0; i < section_hdr->cProperties; i++ )
185 {
186 if( idofs[i].propid >= MSI_MAX_PROPS )
187 {
188 ERR("Unknown property ID %d\n", idofs[i].propid );
189 break;
190 }
191
192 type = get_type( idofs[i].propid );
193 if( type == VT_EMPTY )
194 {
195 ERR("propid %d has unknown type\n", idofs[i].propid);
196 break;
197 }
198
199 propdata = (PROPERTY_DATA*) &data[ idofs[i].dwOffset ];
200
201 /* check we don't run off the end of the data */
202 size = sz - idofs[i].dwOffset - sizeof(DWORD);
203 if( sizeof(DWORD) > size ||
204 ( propdata->type == VT_FILETIME && sizeof(FILETIME) > size ) ||
205 ( propdata->type == VT_LPSTR && (propdata->u.str.len + sizeof(DWORD)) > size ) )
206 {
207 ERR("not enough data\n");
208 break;
209 }
210
211 property.vt = propdata->type;
212 if( propdata->type == VT_LPSTR )
213 {
214 LPSTR str = msi_alloc( propdata->u.str.len );
215 memcpy( str, propdata->u.str.str, propdata->u.str.len );
216 str[ propdata->u.str.len - 1 ] = 0;
217 property.u.pszVal = str;
218 }
219 else if( propdata->type == VT_FILETIME )
220 property.u.filetime = propdata->u.ft;
221 else if( propdata->type == VT_I2 )
222 property.u.iVal = propdata->u.i2;
223 else if( propdata->type == VT_I4 )
224 property.u.lVal = propdata->u.i4;
225
226 /* check the type is the same as we expect */
227 if( type != propdata->type )
228 {
229 propvar_changetype(&changed, &property, type);
230 ptr = &changed;
231 }
232 else
233 ptr = &property;
234
235 prop[ idofs[i].propid ] = *ptr;
236 }
237 }
238
239 static UINT load_summary_info( MSISUMMARYINFO *si, IStream *stm )
240 {
241 UINT ret = ERROR_FUNCTION_FAILED;
242 PROPERTYSETHEADER set_hdr;
243 FORMATIDOFFSET format_hdr;
244 PROPERTYSECTIONHEADER section_hdr;
245 LPBYTE data = NULL;
246 LARGE_INTEGER ofs;
247 ULONG count, sz;
248 HRESULT r;
249
250 TRACE("%p %p\n", si, stm);
251
252 /* read the header */
253 sz = sizeof set_hdr;
254 r = IStream_Read( stm, &set_hdr, sz, &count );
255 if( FAILED(r) || count != sz )
256 return ret;
257
258 if( set_hdr.wByteOrder != 0xfffe )
259 {
260 ERR("property set not big-endian %04X\n", set_hdr.wByteOrder);
261 return ret;
262 }
263
264 sz = sizeof format_hdr;
265 r = IStream_Read( stm, &format_hdr, sz, &count );
266 if( FAILED(r) || count != sz )
267 return ret;
268
269 /* check the format id is correct */
270 if( !IsEqualGUID( &FMTID_SummaryInformation, &format_hdr.fmtid ) )
271 return ret;
272
273 /* seek to the location of the section */
274 ofs.QuadPart = format_hdr.dwOffset;
275 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, NULL );
276 if( FAILED(r) )
277 return ret;
278
279 /* read the section itself */
280 sz = SECT_HDR_SIZE;
281 r = IStream_Read( stm, &section_hdr, sz, &count );
282 if( FAILED(r) || count != sz )
283 return ret;
284
285 if( section_hdr.cProperties > MSI_MAX_PROPS )
286 {
287 ERR("too many properties %d\n", section_hdr.cProperties);
288 return ret;
289 }
290
291 data = msi_alloc( section_hdr.cbSection);
292 if( !data )
293 return ret;
294
295 memcpy( data, &section_hdr, SECT_HDR_SIZE );
296
297 /* read all the data in one go */
298 sz = section_hdr.cbSection - SECT_HDR_SIZE;
299 r = IStream_Read( stm, &data[ SECT_HDR_SIZE ], sz, &count );
300 if( SUCCEEDED(r) && count == sz )
301 read_properties_from_data( si->property, data, sz + SECT_HDR_SIZE );
302 else
303 ERR("failed to read properties %d %d\n", count, sz);
304
305 msi_free( data );
306 return ret;
307 }
308
309 static DWORD write_dword( LPBYTE data, DWORD ofs, DWORD val )
310 {
311 if( data )
312 {
313 data[ofs++] = val&0xff;
314 data[ofs++] = (val>>8)&0xff;
315 data[ofs++] = (val>>16)&0xff;
316 data[ofs++] = (val>>24)&0xff;
317 }
318 return 4;
319 }
320
321 static DWORD write_filetime( LPBYTE data, DWORD ofs, const FILETIME *ft )
322 {
323 write_dword( data, ofs, ft->dwLowDateTime );
324 write_dword( data, ofs + 4, ft->dwHighDateTime );
325 return 8;
326 }
327
328 static DWORD write_string( LPBYTE data, DWORD ofs, LPCSTR str )
329 {
330 DWORD len = lstrlenA( str ) + 1;
331 write_dword( data, ofs, len );
332 if( data )
333 memcpy( &data[ofs + 4], str, len );
334 return (7 + len) & ~3;
335 }
336
337 static UINT write_property_to_data( const PROPVARIANT *prop, LPBYTE data )
338 {
339 DWORD sz = 0;
340
341 if( prop->vt == VT_EMPTY )
342 return sz;
343
344 /* add the type */
345 sz += write_dword( data, sz, prop->vt );
346 switch( prop->vt )
347 {
348 case VT_I2:
349 sz += write_dword( data, sz, prop->u.iVal );
350 break;
351 case VT_I4:
352 sz += write_dword( data, sz, prop->u.lVal );
353 break;
354 case VT_FILETIME:
355 sz += write_filetime( data, sz, &prop->u.filetime );
356 break;
357 case VT_LPSTR:
358 sz += write_string( data, sz, prop->u.pszVal );
359 break;
360 }
361 return sz;
362 }
363
364 static UINT save_summary_info( const MSISUMMARYINFO * si, IStream *stm )
365 {
366 UINT ret = ERROR_FUNCTION_FAILED;
367 PROPERTYSETHEADER set_hdr;
368 FORMATIDOFFSET format_hdr;
369 PROPERTYSECTIONHEADER section_hdr;
370 PROPERTYIDOFFSET idofs[MSI_MAX_PROPS];
371 LPBYTE data = NULL;
372 ULONG count, sz;
373 HRESULT r;
374 int i;
375
376 /* write the header */
377 sz = sizeof set_hdr;
378 memset( &set_hdr, 0, sz );
379 set_hdr.wByteOrder = 0xfffe;
380 set_hdr.wFormat = 0;
381 set_hdr.dwOSVer = 0x00020005; /* build 5, platform id 2 */
382 /* set_hdr.clsID is {00000000-0000-0000-0000-000000000000} */
383 set_hdr.reserved = 1;
384 r = IStream_Write( stm, &set_hdr, sz, &count );
385 if( FAILED(r) || count != sz )
386 return ret;
387
388 /* write the format header */
389 sz = sizeof format_hdr;
390 format_hdr.fmtid = FMTID_SummaryInformation;
391 format_hdr.dwOffset = sizeof format_hdr + sizeof set_hdr;
392 r = IStream_Write( stm, &format_hdr, sz, &count );
393 if( FAILED(r) || count != sz )
394 return ret;
395
396 /* add up how much space the data will take and calculate the offsets */
397 section_hdr.cbSection = sizeof section_hdr;
398 section_hdr.cbSection += (get_property_count( si->property ) * sizeof idofs[0]);
399 section_hdr.cProperties = 0;
400 for( i = 0; i < MSI_MAX_PROPS; i++ )
401 {
402 sz = write_property_to_data( &si->property[i], NULL );
403 if( !sz )
404 continue;
405 idofs[ section_hdr.cProperties ].propid = i;
406 idofs[ section_hdr.cProperties ].dwOffset = section_hdr.cbSection;
407 section_hdr.cProperties++;
408 section_hdr.cbSection += sz;
409 }
410
411 data = msi_alloc_zero( section_hdr.cbSection );
412
413 sz = 0;
414 memcpy( &data[sz], &section_hdr, sizeof section_hdr );
415 sz += sizeof section_hdr;
416
417 memcpy( &data[sz], idofs, section_hdr.cProperties * sizeof idofs[0] );
418 sz += section_hdr.cProperties * sizeof idofs[0];
419
420 /* write out the data */
421 for( i = 0; i < MSI_MAX_PROPS; i++ )
422 sz += write_property_to_data( &si->property[i], &data[sz] );
423
424 r = IStream_Write( stm, data, sz, &count );
425 msi_free( data );
426 if( FAILED(r) || count != sz )
427 return ret;
428
429 return ERROR_SUCCESS;
430 }
431
432 MSISUMMARYINFO *MSI_GetSummaryInformationW( IStorage *stg, UINT uiUpdateCount )
433 {
434 IStream *stm = NULL;
435 MSISUMMARYINFO *si;
436 DWORD grfMode;
437 HRESULT r;
438
439 TRACE("%p %d\n", stg, uiUpdateCount );
440
441 si = alloc_msiobject( MSIHANDLETYPE_SUMMARYINFO,
442 sizeof (MSISUMMARYINFO), MSI_CloseSummaryInfo );
443 if( !si )
444 return si;
445
446 memset( si->property, 0, sizeof si->property );
447 si->update_count = uiUpdateCount;
448 IStorage_AddRef( stg );
449 si->storage = stg;
450
451 /* read the stream... if we fail, we'll start with an empty property set */
452 grfMode = STGM_READ | STGM_SHARE_EXCLUSIVE;
453 r = IStorage_OpenStream( si->storage, szSumInfo, 0, grfMode, 0, &stm );
454 if( SUCCEEDED(r) )
455 {
456 load_summary_info( si, stm );
457 IStream_Release( stm );
458 }
459
460 return si;
461 }
462
463 UINT WINAPI MsiGetSummaryInformationW( MSIHANDLE hDatabase,
464 LPCWSTR szDatabase, UINT uiUpdateCount, MSIHANDLE *pHandle )
465 {
466 MSISUMMARYINFO *si;
467 MSIDATABASE *db;
468 UINT ret = ERROR_FUNCTION_FAILED;
469
470 TRACE("%d %s %d %p\n", hDatabase, debugstr_w(szDatabase),
471 uiUpdateCount, pHandle);
472
473 if( !pHandle )
474 return ERROR_INVALID_PARAMETER;
475
476 if( szDatabase )
477 {
478 LPCWSTR persist = uiUpdateCount ? MSIDBOPEN_TRANSACT : MSIDBOPEN_READONLY;
479
480 ret = MSI_OpenDatabaseW( szDatabase, persist, &db );
481 if( ret != ERROR_SUCCESS )
482 return ret;
483 }
484 else
485 {
486 db = msihandle2msiinfo( hDatabase, MSIHANDLETYPE_DATABASE );
487 if( !db )
488 {
489 HRESULT hr;
490 IWineMsiRemoteDatabase *remote_database;
491
492 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hDatabase );
493 if ( !remote_database )
494 return ERROR_INVALID_HANDLE;
495
496 hr = IWineMsiRemoteDatabase_GetSummaryInformation( remote_database,
497 uiUpdateCount, pHandle );
498 IWineMsiRemoteDatabase_Release( remote_database );
499
500 if (FAILED(hr))
501 {
502 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
503 return HRESULT_CODE(hr);
504
505 return ERROR_FUNCTION_FAILED;
506 }
507
508 return ERROR_SUCCESS;
509 }
510 }
511
512 si = MSI_GetSummaryInformationW( db->storage, uiUpdateCount );
513 if (si)
514 {
515 *pHandle = alloc_msihandle( &si->hdr );
516 if( *pHandle )
517 ret = ERROR_SUCCESS;
518 else
519 ret = ERROR_NOT_ENOUGH_MEMORY;
520 msiobj_release( &si->hdr );
521 }
522
523 if( db )
524 msiobj_release( &db->hdr );
525
526 return ret;
527 }
528
529 UINT WINAPI MsiGetSummaryInformationA(MSIHANDLE hDatabase,
530 LPCSTR szDatabase, UINT uiUpdateCount, MSIHANDLE *pHandle)
531 {
532 LPWSTR szwDatabase = NULL;
533 UINT ret;
534
535 TRACE("%d %s %d %p\n", hDatabase, debugstr_a(szDatabase),
536 uiUpdateCount, pHandle);
537
538 if( szDatabase )
539 {
540 szwDatabase = strdupAtoW( szDatabase );
541 if( !szwDatabase )
542 return ERROR_FUNCTION_FAILED;
543 }
544
545 ret = MsiGetSummaryInformationW(hDatabase, szwDatabase, uiUpdateCount, pHandle);
546
547 msi_free( szwDatabase );
548
549 return ret;
550 }
551
552 UINT WINAPI MsiSummaryInfoGetPropertyCount(MSIHANDLE hSummaryInfo, PUINT pCount)
553 {
554 MSISUMMARYINFO *si;
555
556 TRACE("%d %p\n", hSummaryInfo, pCount);
557
558 si = msihandle2msiinfo( hSummaryInfo, MSIHANDLETYPE_SUMMARYINFO );
559 if( !si )
560 return ERROR_INVALID_HANDLE;
561
562 if( pCount )
563 *pCount = get_property_count( si->property );
564 msiobj_release( &si->hdr );
565
566 return ERROR_SUCCESS;
567 }
568
569 static UINT get_prop( MSIHANDLE handle, UINT uiProperty, UINT *puiDataType,
570 INT *piValue, FILETIME *pftValue, awstring *str, DWORD *pcchValueBuf)
571 {
572 MSISUMMARYINFO *si;
573 PROPVARIANT *prop;
574 UINT ret = ERROR_SUCCESS;
575
576 TRACE("%d %d %p %p %p %p %p\n", handle, uiProperty, puiDataType,
577 piValue, pftValue, str, pcchValueBuf);
578
579 if ( uiProperty >= MSI_MAX_PROPS )
580 {
581 if (puiDataType) *puiDataType = VT_EMPTY;
582 return ERROR_UNKNOWN_PROPERTY;
583 }
584
585 si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
586 if( !si )
587 return ERROR_INVALID_HANDLE;
588
589 prop = &si->property[uiProperty];
590
591 if( puiDataType )
592 *puiDataType = prop->vt;
593
594 switch( prop->vt )
595 {
596 case VT_I2:
597 if( piValue )
598 *piValue = prop->u.iVal;
599 break;
600 case VT_I4:
601 if( piValue )
602 *piValue = prop->u.lVal;
603 break;
604 case VT_LPSTR:
605 if( pcchValueBuf )
606 {
607 DWORD len = 0;
608
609 if( str->unicode )
610 {
611 len = MultiByteToWideChar( CP_ACP, 0, prop->u.pszVal, -1,
612 str->str.w, *pcchValueBuf );
613 len--;
614 }
615 else
616 {
617 len = lstrlenA( prop->u.pszVal );
618 if( str->str.a )
619 lstrcpynA(str->str.a, prop->u.pszVal, *pcchValueBuf );
620 }
621 if (len >= *pcchValueBuf)
622 ret = ERROR_MORE_DATA;
623 *pcchValueBuf = len;
624 }
625 break;
626 case VT_FILETIME:
627 if( pftValue )
628 *pftValue = prop->u.filetime;
629 break;
630 case VT_EMPTY:
631 break;
632 default:
633 FIXME("Unknown property variant type\n");
634 break;
635 }
636 msiobj_release( &si->hdr );
637 return ret;
638 }
639
640 LPWSTR msi_suminfo_dup_string( MSISUMMARYINFO *si, UINT uiProperty )
641 {
642 PROPVARIANT *prop;
643
644 if ( uiProperty >= MSI_MAX_PROPS )
645 return NULL;
646 prop = &si->property[uiProperty];
647 if( prop->vt != VT_LPSTR )
648 return NULL;
649 return strdupAtoW( prop->u.pszVal );
650 }
651
652 LPWSTR msi_get_suminfo_product( IStorage *stg )
653 {
654 MSISUMMARYINFO *si;
655 LPWSTR prod;
656
657 si = MSI_GetSummaryInformationW( stg, 0 );
658 if (!si)
659 {
660 ERR("no summary information!\n");
661 return NULL;
662 }
663 prod = msi_suminfo_dup_string( si, PID_REVNUMBER );
664 msiobj_release( &si->hdr );
665 return prod;
666 }
667
668 UINT WINAPI MsiSummaryInfoGetPropertyA(
669 MSIHANDLE handle, UINT uiProperty, PUINT puiDataType, LPINT piValue,
670 FILETIME *pftValue, LPSTR szValueBuf, LPDWORD pcchValueBuf)
671 {
672 awstring str;
673
674 TRACE("%d %d %p %p %p %p %p\n", handle, uiProperty, puiDataType,
675 piValue, pftValue, szValueBuf, pcchValueBuf );
676
677 str.unicode = FALSE;
678 str.str.a = szValueBuf;
679
680 return get_prop( handle, uiProperty, puiDataType, piValue,
681 pftValue, &str, pcchValueBuf );
682 }
683
684 UINT WINAPI MsiSummaryInfoGetPropertyW(
685 MSIHANDLE handle, UINT uiProperty, PUINT puiDataType, LPINT piValue,
686 FILETIME *pftValue, LPWSTR szValueBuf, LPDWORD pcchValueBuf)
687 {
688 awstring str;
689
690 TRACE("%d %d %p %p %p %p %p\n", handle, uiProperty, puiDataType,
691 piValue, pftValue, szValueBuf, pcchValueBuf );
692
693 str.unicode = TRUE;
694 str.str.w = szValueBuf;
695
696 return get_prop( handle, uiProperty, puiDataType, piValue,
697 pftValue, &str, pcchValueBuf );
698 }
699
700 static UINT set_prop( MSISUMMARYINFO *si, UINT uiProperty, UINT type,
701 INT iValue, FILETIME* pftValue, awcstring *str )
702 {
703 PROPVARIANT *prop;
704 UINT len;
705
706 TRACE("%p %u %u %i %p %p\n", si, uiProperty, type, iValue,
707 pftValue, str );
708
709 prop = &si->property[uiProperty];
710
711 if( prop->vt == VT_EMPTY )
712 {
713 if( !si->update_count )
714 return ERROR_FUNCTION_FAILED;
715
716 si->update_count--;
717 }
718 else if( prop->vt != type )
719 return ERROR_SUCCESS;
720
721 free_prop( prop );
722 prop->vt = type;
723 switch( type )
724 {
725 case VT_I4:
726 prop->u.lVal = iValue;
727 break;
728 case VT_I2:
729 prop->u.iVal = iValue;
730 break;
731 case VT_FILETIME:
732 prop->u.filetime = *pftValue;
733 break;
734 case VT_LPSTR:
735 if( str->unicode )
736 {
737 len = WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
738 NULL, 0, NULL, NULL );
739 prop->u.pszVal = msi_alloc( len );
740 WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
741 prop->u.pszVal, len, NULL, NULL );
742 }
743 else
744 {
745 len = lstrlenA( str->str.a ) + 1;
746 prop->u.pszVal = msi_alloc( len );
747 lstrcpyA( prop->u.pszVal, str->str.a );
748 }
749 break;
750 }
751
752 return ERROR_SUCCESS;
753 }
754
755 UINT WINAPI MsiSummaryInfoSetPropertyW( MSIHANDLE handle, UINT uiProperty,
756 UINT uiDataType, INT iValue, FILETIME* pftValue, LPCWSTR szValue )
757 {
758 awcstring str;
759 MSISUMMARYINFO *si;
760 UINT type, ret;
761
762 TRACE("%d %u %u %i %p %s\n", handle, uiProperty, uiDataType,
763 iValue, pftValue, debugstr_w(szValue) );
764
765 type = get_type( uiProperty );
766 if( type == VT_EMPTY || type != uiDataType )
767 return ERROR_DATATYPE_MISMATCH;
768
769 if( uiDataType == VT_LPSTR && !szValue )
770 return ERROR_INVALID_PARAMETER;
771
772 if( uiDataType == VT_FILETIME && !pftValue )
773 return ERROR_INVALID_PARAMETER;
774
775 si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
776 if( !si )
777 return ERROR_INVALID_HANDLE;
778
779 str.unicode = TRUE;
780 str.str.w = szValue;
781 ret = set_prop( si, uiProperty, type, iValue, pftValue, &str );
782
783 msiobj_release( &si->hdr );
784 return ret;
785 }
786
787 UINT WINAPI MsiSummaryInfoSetPropertyA( MSIHANDLE handle, UINT uiProperty,
788 UINT uiDataType, INT iValue, FILETIME* pftValue, LPCSTR szValue )
789 {
790 awcstring str;
791 MSISUMMARYINFO *si;
792 UINT type, ret;
793
794 TRACE("%d %u %u %i %p %s\n", handle, uiProperty, uiDataType,
795 iValue, pftValue, debugstr_a(szValue) );
796
797 type = get_type( uiProperty );
798 if( type == VT_EMPTY || type != uiDataType )
799 return ERROR_DATATYPE_MISMATCH;
800
801 if( uiDataType == VT_LPSTR && !szValue )
802 return ERROR_INVALID_PARAMETER;
803
804 if( uiDataType == VT_FILETIME && !pftValue )
805 return ERROR_INVALID_PARAMETER;
806
807 si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
808 if( !si )
809 return ERROR_INVALID_HANDLE;
810
811 str.unicode = FALSE;
812 str.str.a = szValue;
813 ret = set_prop( si, uiProperty, uiDataType, iValue, pftValue, &str );
814
815 msiobj_release( &si->hdr );
816 return ret;
817 }
818
819 static UINT suminfo_persist( MSISUMMARYINFO *si )
820 {
821 UINT ret = ERROR_FUNCTION_FAILED;
822 IStream *stm = NULL;
823 DWORD grfMode;
824 HRESULT r;
825
826 grfMode = STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
827 r = IStorage_CreateStream( si->storage, szSumInfo, grfMode, 0, 0, &stm );
828 if( SUCCEEDED(r) )
829 {
830 ret = save_summary_info( si, stm );
831 IStream_Release( stm );
832 }
833 return ret;
834 }
835
836 static void parse_filetime( LPCWSTR str, FILETIME *ft )
837 {
838 SYSTEMTIME lt, utc;
839 const WCHAR *p = str;
840 WCHAR *end;
841
842 memset( &lt, 0, sizeof(lt) );
843
844 /* YYYY/MM/DD hh:mm:ss */
845
846 while (isspaceW( *p )) p++;
847
848 lt.wYear = strtolW( p, &end, 10 );
849 if (*end != '/') return;
850 p = end + 1;
851
852 lt.wMonth = strtolW( p, &end, 10 );
853 if (*end != '/') return;
854 p = end + 1;
855
856 lt.wDay = strtolW( p, &end, 10 );
857 if (*end != ' ') return;
858 p = end + 1;
859
860 while (isspaceW( *p )) p++;
861
862 lt.wHour = strtolW( p, &end, 10 );
863 if (*end != ':') return;
864 p = end + 1;
865
866 lt.wMinute = strtolW( p, &end, 10 );
867 if (*end != ':') return;
868 p = end + 1;
869
870 lt.wSecond = strtolW( p, &end, 10 );
871
872 TzSpecificLocalTimeToSystemTime( NULL, &lt, &utc );
873 SystemTimeToFileTime( &utc, ft );
874 }
875
876 static UINT parse_prop( LPCWSTR prop, LPCWSTR value, UINT *pid, INT *int_value,
877 FILETIME *ft_value, awcstring *str_value )
878 {
879 *pid = atoiW( prop );
880 switch (*pid)
881 {
882 case PID_CODEPAGE:
883 case PID_WORDCOUNT:
884 case PID_CHARCOUNT:
885 case PID_SECURITY:
886 case PID_PAGECOUNT:
887 *int_value = atoiW( value );
888 break;
889
890 case PID_LASTPRINTED:
891 case PID_CREATE_DTM:
892 case PID_LASTSAVE_DTM:
893 parse_filetime( value, ft_value );
894 break;
895
896 case PID_SUBJECT:
897 case PID_AUTHOR:
898 case PID_KEYWORDS:
899 case PID_COMMENTS:
900 case PID_TEMPLATE:
901 case PID_LASTAUTHOR:
902 case PID_REVNUMBER:
903 case PID_APPNAME:
904 case PID_TITLE:
905 str_value->str.w = value;
906 str_value->unicode = TRUE;
907 break;
908
909 default:
910 WARN("unhandled prop id %u\n", *pid);
911 return ERROR_FUNCTION_FAILED;
912 }
913
914 return ERROR_SUCCESS;
915 }
916
917 UINT msi_add_suminfo( MSIDATABASE *db, LPWSTR **records, int num_records, int num_columns )
918 {
919 UINT r = ERROR_FUNCTION_FAILED;
920 DWORD i, j;
921 MSISUMMARYINFO *si;
922
923 si = MSI_GetSummaryInformationW( db->storage, num_records * (num_columns / 2) );
924 if (!si)
925 {
926 ERR("no summary information!\n");
927 return ERROR_FUNCTION_FAILED;
928 }
929
930 for (i = 0; i < num_records; i++)
931 {
932 for (j = 0; j < num_columns; j += 2)
933 {
934 UINT pid;
935 INT int_value = 0;
936 FILETIME ft_value;
937 awcstring str_value;
938
939 r = parse_prop( records[i][j], records[i][j + 1], &pid, &int_value, &ft_value, &str_value );
940 if (r != ERROR_SUCCESS)
941 goto end;
942
943 r = set_prop( si, pid, get_type(pid), int_value, &ft_value, &str_value );
944 if (r != ERROR_SUCCESS)
945 goto end;
946 }
947 }
948
949 end:
950 if (r == ERROR_SUCCESS)
951 r = suminfo_persist( si );
952
953 msiobj_release( &si->hdr );
954 return r;
955 }
956
957 UINT WINAPI MsiSummaryInfoPersist( MSIHANDLE handle )
958 {
959 MSISUMMARYINFO *si;
960 UINT ret;
961
962 TRACE("%d\n", handle );
963
964 si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
965 if( !si )
966 return ERROR_INVALID_HANDLE;
967
968 ret = suminfo_persist( si );
969
970 msiobj_release( &si->hdr );
971 return ret;
972 }