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