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