msi: Make sure target paths are normalized.
[wine] / dlls / msi / package.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2004 Aric Stewart 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 NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23 #define COBJMACROS
24
25 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "shlwapi.h"
31 #include "wingdi.h"
32 #include "wine/debug.h"
33 #include "msi.h"
34 #include "msiquery.h"
35 #include "objidl.h"
36 #include "wincrypt.h"
37 #include "winuser.h"
38 #include "wininet.h"
39 #include "winver.h"
40 #include "urlmon.h"
41 #include "shlobj.h"
42 #include "wine/unicode.h"
43 #include "objbase.h"
44 #include "msidefs.h"
45 #include "sddl.h"
46
47 #include "msipriv.h"
48 #include "msiserver.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(msi);
51
52 static void remove_tracked_tempfiles( MSIPACKAGE *package )
53 {
54     struct list *item, *cursor;
55
56     LIST_FOR_EACH_SAFE( item, cursor, &package->tempfiles )
57     {
58         MSITEMPFILE *temp = LIST_ENTRY( item, MSITEMPFILE, entry );
59
60         list_remove( &temp->entry );
61         TRACE("deleting temp file %s\n", debugstr_w( temp->Path ));
62         DeleteFileW( temp->Path );
63         msi_free( temp->Path );
64         msi_free( temp );
65     }
66 }
67
68 static void free_feature( MSIFEATURE *feature )
69 {
70     struct list *item, *cursor;
71
72     LIST_FOR_EACH_SAFE( item, cursor, &feature->Children )
73     {
74         FeatureList *fl = LIST_ENTRY( item, FeatureList, entry );
75         list_remove( &fl->entry );
76         msi_free( fl );
77     }
78
79     LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
80     {
81         ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
82         list_remove( &cl->entry );
83         msi_free( cl );
84     }
85     msi_free( feature->Feature );
86     msi_free( feature->Feature_Parent );
87     msi_free( feature->Directory );
88     msi_free( feature->Description );
89     msi_free( feature->Title );
90     msi_free( feature );
91 }
92
93 static void free_folder( MSIFOLDER *folder )
94 {
95     struct list *item, *cursor;
96
97     LIST_FOR_EACH_SAFE( item, cursor, &folder->children )
98     {
99         FolderList *fl = LIST_ENTRY( item, FolderList, entry );
100         list_remove( &fl->entry );
101         msi_free( fl );
102     }
103     msi_free( folder->Parent );
104     msi_free( folder->Directory );
105     msi_free( folder->TargetDefault );
106     msi_free( folder->SourceLongPath );
107     msi_free( folder->SourceShortPath );
108     msi_free( folder->ResolvedTarget );
109     msi_free( folder->ResolvedSource );
110     msi_free( folder );
111 }
112
113 static void free_extension( MSIEXTENSION *ext )
114 {
115     struct list *item, *cursor;
116
117     LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
118     {
119         MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );
120
121         list_remove( &verb->entry );
122         msi_free( verb->Verb );
123         msi_free( verb->Command );
124         msi_free( verb->Argument );
125         msi_free( verb );
126     }
127
128     msi_free( ext->Extension );
129     msi_free( ext->ProgIDText );
130     msi_free( ext );
131 }
132
133 static void free_assembly( MSIASSEMBLY *assembly )
134 {
135     msi_free( assembly->feature );
136     msi_free( assembly->manifest );
137     msi_free( assembly->application );
138     msi_free( assembly->display_name );
139     if (assembly->tempdir) RemoveDirectoryW( assembly->tempdir );
140     msi_free( assembly->tempdir );
141     msi_free( assembly );
142 }
143
144 void msi_free_action_script( MSIPACKAGE *package, UINT script )
145 {
146     UINT i;
147     for (i = 0; i < package->script->ActionCount[script]; i++)
148         msi_free( package->script->Actions[script][i] );
149
150     msi_free( package->script->Actions[script] );
151     package->script->Actions[script] = NULL;
152     package->script->ActionCount[script] = 0;
153 }
154
155 static void free_package_structures( MSIPACKAGE *package )
156 {
157     INT i;
158     struct list *item, *cursor;
159
160     LIST_FOR_EACH_SAFE( item, cursor, &package->features )
161     {
162         MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
163         list_remove( &feature->entry );
164         free_feature( feature );
165     }
166
167     LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
168     {
169         MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );
170         list_remove( &folder->entry );
171         free_folder( folder );
172     }
173
174     LIST_FOR_EACH_SAFE( item, cursor, &package->components )
175     {
176         MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
177
178         list_remove( &comp->entry );
179         msi_free( comp->Component );
180         msi_free( comp->ComponentId );
181         msi_free( comp->Directory );
182         msi_free( comp->Condition );
183         msi_free( comp->KeyPath );
184         msi_free( comp->FullKeypath );
185         if (comp->assembly) free_assembly( comp->assembly );
186         msi_free( comp );
187     }
188
189     LIST_FOR_EACH_SAFE( item, cursor, &package->files )
190     {
191         MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );
192
193         list_remove( &file->entry );
194         msi_free( file->File );
195         msi_free( file->FileName );
196         msi_free( file->ShortName );
197         msi_free( file->LongName );
198         msi_free( file->Version );
199         msi_free( file->Language );
200         msi_free( file->TargetPath );
201         msi_free( file );
202     }
203
204     /* clean up extension, progid, class and verb structures */
205     LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
206     {
207         MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );
208
209         list_remove( &cls->entry );
210         msi_free( cls->clsid );
211         msi_free( cls->Context );
212         msi_free( cls->Description );
213         msi_free( cls->FileTypeMask );
214         msi_free( cls->IconPath );
215         msi_free( cls->DefInprocHandler );
216         msi_free( cls->DefInprocHandler32 );
217         msi_free( cls->Argument );
218         msi_free( cls->ProgIDText );
219         msi_free( cls );
220     }
221
222     LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
223     {
224         MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
225
226         list_remove( &ext->entry );
227         free_extension( ext );
228     }
229
230     LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
231     {
232         MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
233
234         list_remove( &progid->entry );
235         msi_free( progid->ProgID );
236         msi_free( progid->Description );
237         msi_free( progid->IconPath );
238         msi_free( progid );
239     }
240
241     LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
242     {
243         MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
244
245         list_remove( &mt->entry );
246         msi_free( mt->suffix );
247         msi_free( mt->clsid );
248         msi_free( mt->ContentType );
249         msi_free( mt );
250     }
251
252     LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
253     {
254         MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );
255
256         list_remove( &appid->entry );
257         msi_free( appid->AppID );
258         msi_free( appid->RemoteServerName );
259         msi_free( appid->LocalServer );
260         msi_free( appid->ServiceParameters );
261         msi_free( appid->DllSurrogate );
262         msi_free( appid );
263     }
264
265     LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_info )
266     {
267         MSISOURCELISTINFO *info = LIST_ENTRY( item, MSISOURCELISTINFO, entry );
268
269         list_remove( &info->entry );
270         msi_free( info->value );
271         msi_free( info );
272     }
273
274     LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_media )
275     {
276         MSIMEDIADISK *info = LIST_ENTRY( item, MSIMEDIADISK, entry );
277
278         list_remove( &info->entry );
279         msi_free( info->volume_label );
280         msi_free( info->disk_prompt );
281         msi_free( info );
282     }
283
284     if (package->script)
285     {
286         for (i = 0; i < SCRIPT_MAX; i++)
287             msi_free_action_script( package, i );
288
289         for (i = 0; i < package->script->UniqueActionsCount; i++)
290             msi_free( package->script->UniqueActions[i] );
291
292         msi_free( package->script->UniqueActions );
293         msi_free( package->script );
294     }
295
296     LIST_FOR_EACH_SAFE( item, cursor, &package->binaries )
297     {
298         MSIBINARY *binary = LIST_ENTRY( item, MSIBINARY, entry );
299
300         list_remove( &binary->entry );
301         if (binary->module)
302             FreeLibrary( binary->module );
303         if (!DeleteFileW( binary->tmpfile ))
304             ERR("failed to delete %s (%u)\n", debugstr_w(binary->tmpfile), GetLastError());
305         msi_free( binary->source );
306         msi_free( binary->tmpfile );
307         msi_free( binary );
308     }
309
310     LIST_FOR_EACH_SAFE( item, cursor, &package->cabinet_streams )
311     {
312         MSICABINETSTREAM *cab = LIST_ENTRY( item, MSICABINETSTREAM, entry );
313
314         list_remove( &cab->entry );
315         IStorage_Release( cab->storage );
316         msi_free( cab->stream );
317         msi_free( cab );
318     }
319
320     LIST_FOR_EACH_SAFE( item, cursor, &package->patches )
321     {
322         MSIPATCHINFO *patch = LIST_ENTRY( item, MSIPATCHINFO, entry );
323
324         list_remove( &patch->entry );
325         if (patch->delete_on_close && !DeleteFileW( patch->localfile ))
326         {
327             ERR("failed to delete %s (%u)\n", debugstr_w(patch->localfile), GetLastError());
328         }
329         msi_free_patchinfo( patch );
330     }
331
332     msi_free( package->BaseURL );
333     msi_free( package->PackagePath );
334     msi_free( package->ProductCode );
335     msi_free( package->ActionFormat );
336     msi_free( package->LastAction );
337     msi_free( package->langids );
338
339     remove_tracked_tempfiles(package);
340
341     /* cleanup control event subscriptions */
342     ControlEvent_CleanupSubscriptions( package );
343 }
344
345 static void MSI_FreePackage( MSIOBJECTHDR *arg)
346 {
347     UINT i;
348     MSIPACKAGE *package = (MSIPACKAGE *)arg;
349
350     if( package->dialog )
351         msi_dialog_destroy( package->dialog );
352
353     msiobj_release( &package->db->hdr );
354     free_package_structures(package);
355     CloseHandle( package->log_file );
356
357     for (i = 0; i < CLR_VERSION_MAX; i++)
358         if (package->cache_net[i]) IAssemblyCache_Release( package->cache_net[i] );
359     if (package->cache_sxs) IAssemblyCache_Release( package->cache_sxs );
360
361     if (package->delete_on_close) DeleteFileW( package->localfile );
362     msi_free( package->localfile );
363 }
364
365 static UINT create_temp_property_table(MSIPACKAGE *package)
366 {
367     static const WCHAR query[] = {
368         'C','R','E','A','T','E',' ','T','A','B','L','E',' ',
369         '`','_','P','r','o','p','e','r','t','y','`',' ','(',' ',
370         '`','_','P','r','o','p','e','r','t','y','`',' ',
371         'C','H','A','R','(','5','6',')',' ','N','O','T',' ','N','U','L','L',' ',
372         'T','E','M','P','O','R','A','R','Y',',',' ',
373         '`','V','a','l','u','e','`',' ','C','H','A','R','(','9','8',')',' ',
374         'N','O','T',' ','N','U','L','L',' ','T','E','M','P','O','R','A','R','Y',
375         ' ','P','R','I','M','A','R','Y',' ','K','E','Y',' ',
376         '`','_','P','r','o','p','e','r','t','y','`',')',' ','H','O','L','D',0};
377     MSIQUERY *view;
378     UINT rc;
379
380     rc = MSI_DatabaseOpenViewW(package->db, query, &view);
381     if (rc != ERROR_SUCCESS)
382         return rc;
383
384     rc = MSI_ViewExecute(view, 0);
385     MSI_ViewClose(view);
386     msiobj_release(&view->hdr);
387     return rc;
388 }
389
390 UINT msi_clone_properties(MSIPACKAGE *package)
391 {
392     static const WCHAR query_select[] = {
393         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
394         '`','P','r','o','p','e','r','t','y','`',0};
395     static const WCHAR query_insert[] = {
396         'I','N','S','E','R','T',' ','I','N','T','O',' ',
397         '`','_','P','r','o','p','e','r','t','y','`',' ',
398         '(','`','_','P','r','o','p','e','r','t','y','`',',','`','V','a','l','u','e','`',')',' ',
399         'V','A','L','U','E','S',' ','(','?',',','?',')',0};
400     static const WCHAR query_update[] = {
401         'U','P','D','A','T','E',' ','`','_','P','r','o','p','e','r','t','y','`',' ',
402         'S','E','T',' ','`','V','a','l','u','e','`',' ','=',' ','?',' ',
403         'W','H','E','R','E',' ','`','_','P','r','o','p','e','r','t','y','`',' ','=',' ','?',0};
404     MSIQUERY *view_select;
405     UINT rc;
406
407     rc = MSI_DatabaseOpenViewW( package->db, query_select, &view_select );
408     if (rc != ERROR_SUCCESS)
409         return rc;
410
411     rc = MSI_ViewExecute( view_select, 0 );
412     if (rc != ERROR_SUCCESS)
413     {
414         MSI_ViewClose( view_select );
415         msiobj_release( &view_select->hdr );
416         return rc;
417     }
418
419     while (1)
420     {
421         MSIQUERY *view_insert, *view_update;
422         MSIRECORD *rec_select;
423
424         rc = MSI_ViewFetch( view_select, &rec_select );
425         if (rc != ERROR_SUCCESS)
426             break;
427
428         rc = MSI_DatabaseOpenViewW( package->db, query_insert, &view_insert );
429         if (rc != ERROR_SUCCESS)
430         {
431             msiobj_release( &rec_select->hdr );
432             continue;
433         }
434
435         rc = MSI_ViewExecute( view_insert, rec_select );
436         MSI_ViewClose( view_insert );
437         msiobj_release( &view_insert->hdr );
438         if (rc != ERROR_SUCCESS)
439         {
440             MSIRECORD *rec_update;
441
442             TRACE("insert failed, trying update\n");
443
444             rc = MSI_DatabaseOpenViewW( package->db, query_update, &view_update );
445             if (rc != ERROR_SUCCESS)
446             {
447                 WARN("open view failed %u\n", rc);
448                 msiobj_release( &rec_select->hdr );
449                 continue;
450             }
451
452             rec_update = MSI_CreateRecord( 2 );
453             MSI_RecordCopyField( rec_select, 1, rec_update, 2 );
454             MSI_RecordCopyField( rec_select, 2, rec_update, 1 );
455             rc = MSI_ViewExecute( view_update, rec_update );
456             if (rc != ERROR_SUCCESS)
457                 WARN("update failed %u\n", rc);
458
459             MSI_ViewClose( view_update );
460             msiobj_release( &view_update->hdr );
461             msiobj_release( &rec_update->hdr );
462         }
463
464         msiobj_release( &rec_select->hdr );
465     }
466
467     MSI_ViewClose( view_select );
468     msiobj_release( &view_select->hdr );
469     return rc;
470 }
471
472 /*
473  * set_installed_prop
474  *
475  * Sets the "Installed" property to indicate that
476  *  the product is installed for the current user.
477  */
478 static UINT set_installed_prop( MSIPACKAGE *package )
479 {
480     HKEY hkey;
481     UINT r;
482
483     if (!package->ProductCode) return ERROR_FUNCTION_FAILED;
484
485     r = MSIREG_OpenUninstallKey( package->ProductCode, package->platform, &hkey, FALSE );
486     if (r == ERROR_SUCCESS)
487     {
488         RegCloseKey( hkey );
489         msi_set_property( package->db, szInstalled, szOne );
490     }
491     return r;
492 }
493
494 static UINT set_user_sid_prop( MSIPACKAGE *package )
495 {
496     SID_NAME_USE use;
497     LPWSTR user_name;
498     LPWSTR sid_str = NULL, dom = NULL;
499     DWORD size, dom_size;
500     PSID psid = NULL;
501     UINT r = ERROR_FUNCTION_FAILED;
502
503     size = 0;
504     GetUserNameW( NULL, &size );
505
506     user_name = msi_alloc( (size + 1) * sizeof(WCHAR) );
507     if (!user_name)
508         return ERROR_OUTOFMEMORY;
509
510     if (!GetUserNameW( user_name, &size ))
511         goto done;
512
513     size = 0;
514     dom_size = 0;
515     LookupAccountNameW( NULL, user_name, NULL, &size, NULL, &dom_size, &use );
516
517     psid = msi_alloc( size );
518     dom = msi_alloc( dom_size*sizeof (WCHAR) );
519     if (!psid || !dom)
520     {
521         r = ERROR_OUTOFMEMORY;
522         goto done;
523     }
524
525     if (!LookupAccountNameW( NULL, user_name, psid, &size, dom, &dom_size, &use ))
526         goto done;
527
528     if (!ConvertSidToStringSidW( psid, &sid_str ))
529         goto done;
530
531     r = msi_set_property( package->db, szUserSID, sid_str );
532
533 done:
534     LocalFree( sid_str );
535     msi_free( dom );
536     msi_free( psid );
537     msi_free( user_name );
538
539     return r;
540 }
541
542 static LPWSTR get_fusion_filename(MSIPACKAGE *package)
543 {
544     HKEY netsetup;
545     LONG res;
546     LPWSTR file = NULL;
547     DWORD index = 0, size;
548     WCHAR ver[MAX_PATH];
549     WCHAR name[MAX_PATH];
550     WCHAR windir[MAX_PATH];
551
552     static const WCHAR fusion[] = {'f','u','s','i','o','n','.','d','l','l',0};
553     static const WCHAR sub[] = {
554         'S','o','f','t','w','a','r','e','\\',
555         'M','i','c','r','o','s','o','f','t','\\',
556         'N','E','T',' ','F','r','a','m','e','w','o','r','k',' ','S','e','t','u','p','\\',
557         'N','D','P',0
558     };
559     static const WCHAR subdir[] = {
560         'M','i','c','r','o','s','o','f','t','.','N','E','T','\\',
561         'F','r','a','m','e','w','o','r','k','\\',0
562     };
563
564     res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, sub, 0, KEY_ENUMERATE_SUB_KEYS, &netsetup);
565     if (res != ERROR_SUCCESS)
566         return NULL;
567
568     GetWindowsDirectoryW(windir, MAX_PATH);
569
570     ver[0] = '\0';
571     size = MAX_PATH;
572     while (RegEnumKeyExW(netsetup, index, name, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
573     {
574         index++;
575
576         /* verify existence of fusion.dll .Net 3.0 does not install a new one */
577         if (strcmpW( ver, name ) < 0)
578         {
579             LPWSTR check;
580             size = lstrlenW(windir) + lstrlenW(subdir) + lstrlenW(name) +lstrlenW(fusion) + 3;
581             check = msi_alloc(size * sizeof(WCHAR));
582
583             if (!check)
584             {
585                 msi_free(file);
586                 return NULL;
587             }
588
589             lstrcpyW(check, windir);
590             lstrcatW(check, szBackSlash);
591             lstrcatW(check, subdir);
592             lstrcatW(check, name);
593             lstrcatW(check, szBackSlash);
594             lstrcatW(check, fusion);
595
596             if(GetFileAttributesW(check) != INVALID_FILE_ATTRIBUTES)
597             {
598                 msi_free(file);
599                 file = check;
600                 lstrcpyW(ver, name);
601             }
602             else
603                 msi_free(check);
604         }
605     }
606
607     RegCloseKey(netsetup);
608     return file;
609 }
610
611 typedef struct tagLANGANDCODEPAGE
612 {
613   WORD wLanguage;
614   WORD wCodePage;
615 } LANGANDCODEPAGE;
616
617 static void set_msi_assembly_prop(MSIPACKAGE *package)
618 {
619     UINT val_len;
620     DWORD size, handle;
621     LPVOID version = NULL;
622     WCHAR buf[MAX_PATH];
623     LPWSTR fusion, verstr;
624     LANGANDCODEPAGE *translate;
625
626     static const WCHAR netasm[] = {
627         'M','s','i','N','e','t','A','s','s','e','m','b','l','y','S','u','p','p','o','r','t',0
628     };
629     static const WCHAR translation[] = {
630         '\\','V','a','r','F','i','l','e','I','n','f','o',
631         '\\','T','r','a','n','s','l','a','t','i','o','n',0
632     };
633     static const WCHAR verfmt[] = {
634         '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
635         '\\','%','0','4','x','%','0','4','x',
636         '\\','P','r','o','d','u','c','t','V','e','r','s','i','o','n',0
637     };
638
639     fusion = get_fusion_filename(package);
640     if (!fusion)
641         return;
642
643     size = GetFileVersionInfoSizeW(fusion, &handle);
644     if (!size) return;
645
646     version = msi_alloc(size);
647     if (!version) return;
648
649     if (!GetFileVersionInfoW(fusion, handle, size, version))
650         goto done;
651
652     if (!VerQueryValueW(version, translation, (LPVOID *)&translate, &val_len))
653         goto done;
654
655     sprintfW(buf, verfmt, translate[0].wLanguage, translate[0].wCodePage);
656
657     if (!VerQueryValueW(version, buf, (LPVOID *)&verstr, &val_len))
658         goto done;
659
660     if (!val_len || !verstr)
661         goto done;
662
663     msi_set_property(package->db, netasm, verstr);
664
665 done:
666     msi_free(fusion);
667     msi_free(version);
668 }
669
670 static VOID set_installer_properties(MSIPACKAGE *package)
671 {
672     WCHAR pth[MAX_PATH];
673     WCHAR *ptr;
674     OSVERSIONINFOEXW OSVersion;
675     MEMORYSTATUSEX msex;
676     DWORD verval, len;
677     WCHAR verstr[10], bufstr[20];
678     HDC dc;
679     HKEY hkey;
680     LPWSTR username, companyname;
681     SYSTEM_INFO sys_info;
682     SYSTEMTIME systemtime;
683     LANGID langid;
684
685     static const WCHAR szCommonFilesFolder[] = {'C','o','m','m','o','n','F','i','l','e','s','F','o','l','d','e','r',0};
686     static const WCHAR szProgramFilesFolder[] = {'P','r','o','g','r','a','m','F','i','l','e','s','F','o','l','d','e','r',0};
687     static const WCHAR szCommonAppDataFolder[] = {'C','o','m','m','o','n','A','p','p','D','a','t','a','F','o','l','d','e','r',0};
688     static const WCHAR szFavoritesFolder[] = {'F','a','v','o','r','i','t','e','s','F','o','l','d','e','r',0};
689     static const WCHAR szFontsFolder[] = {'F','o','n','t','s','F','o','l','d','e','r',0};
690     static const WCHAR szSendToFolder[] = {'S','e','n','d','T','o','F','o','l','d','e','r',0};
691     static const WCHAR szStartMenuFolder[] = {'S','t','a','r','t','M','e','n','u','F','o','l','d','e','r',0};
692     static const WCHAR szStartupFolder[] = {'S','t','a','r','t','u','p','F','o','l','d','e','r',0};
693     static const WCHAR szTemplateFolder[] = {'T','e','m','p','l','a','t','e','F','o','l','d','e','r',0};
694     static const WCHAR szDesktopFolder[] = {'D','e','s','k','t','o','p','F','o','l','d','e','r',0};
695     static const WCHAR szProgramMenuFolder[] = {'P','r','o','g','r','a','m','M','e','n','u','F','o','l','d','e','r',0};
696     static const WCHAR szAdminToolsFolder[] = {'A','d','m','i','n','T','o','o','l','s','F','o','l','d','e','r',0};
697     static const WCHAR szSystemFolder[] = {'S','y','s','t','e','m','F','o','l','d','e','r',0};
698     static const WCHAR szSystem16Folder[] = {'S','y','s','t','e','m','1','6','F','o','l','d','e','r',0};
699     static const WCHAR szLocalAppDataFolder[] = {'L','o','c','a','l','A','p','p','D','a','t','a','F','o','l','d','e','r',0};
700     static const WCHAR szMyPicturesFolder[] = {'M','y','P','i','c','t','u','r','e','s','F','o','l','d','e','r',0};
701     static const WCHAR szPersonalFolder[] = {'P','e','r','s','o','n','a','l','F','o','l','d','e','r',0};
702     static const WCHAR szWindowsVolume[] = {'W','i','n','d','o','w','s','V','o','l','u','m','e',0};
703     static const WCHAR szPrivileged[] = {'P','r','i','v','i','l','e','g','e','d',0};
704     static const WCHAR szVersion9x[] = {'V','e','r','s','i','o','n','9','X',0};
705     static const WCHAR szVersionNT[] = {'V','e','r','s','i','o','n','N','T',0};
706     static const WCHAR szMsiNTProductType[] = {'M','s','i','N','T','P','r','o','d','u','c','t','T','y','p','e',0};
707     static const WCHAR szFormat[] = {'%','l','i',0};
708     static const WCHAR szWindowsBuild[] = {'W','i','n','d','o','w','s','B','u','i','l','d',0};
709     static const WCHAR szServicePackLevel[] = {'S','e','r','v','i','c','e','P','a','c','k','L','e','v','e','l',0};
710     static const WCHAR szSix[] = {'6',0 };
711     static const WCHAR szVersionMsi[] = { 'V','e','r','s','i','o','n','M','s','i',0 };
712     static const WCHAR szVersionDatabase[] = { 'V','e','r','s','i','o','n','D','a','t','a','b','a','s','e',0 };
713     static const WCHAR szPhysicalMemory[] = { 'P','h','y','s','i','c','a','l','M','e','m','o','r','y',0 };
714     static const WCHAR szFormat2[] = {'%','l','i','.','%','l','i',0};
715     static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
716     static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
717     static const WCHAR szColorBits[] = {'C','o','l','o','r','B','i','t','s',0};
718     static const WCHAR szIntFormat[] = {'%','d',0};
719     static const WCHAR szMsiAMD64[] = { 'M','s','i','A','M','D','6','4',0 };
720     static const WCHAR szMsix64[] = { 'M','s','i','x','6','4',0 };
721     static const WCHAR szSystem64Folder[] = { 'S','y','s','t','e','m','6','4','F','o','l','d','e','r',0 };
722     static const WCHAR szCommonFiles64Folder[] = { 'C','o','m','m','o','n','F','i','l','e','s','6','4','F','o','l','d','e','r',0 };
723     static const WCHAR szProgramFiles64Folder[] = { 'P','r','o','g','r','a','m','F','i','l','e','s','6','4','F','o','l','d','e','r',0 };
724     static const WCHAR szVersionNT64[] = { 'V','e','r','s','i','o','n','N','T','6','4',0 };
725     static const WCHAR szUserInfo[] = {
726         'S','O','F','T','W','A','R','E','\\',
727         'M','i','c','r','o','s','o','f','t','\\',
728         'M','S',' ','S','e','t','u','p',' ','(','A','C','M','E',')','\\',
729         'U','s','e','r',' ','I','n','f','o',0
730     };
731     static const WCHAR szDefName[] = { 'D','e','f','N','a','m','e',0 };
732     static const WCHAR szDefCompany[] = { 'D','e','f','C','o','m','p','a','n','y',0 };
733     static const WCHAR szCurrentVersion[] = {
734         'S','O','F','T','W','A','R','E','\\',
735         'M','i','c','r','o','s','o','f','t','\\',
736         'W','i','n','d','o','w','s',' ','N','T','\\',
737         'C','u','r','r','e','n','t','V','e','r','s','i','o','n',0
738     };
739     static const WCHAR szRegisteredUser[] = {'R','e','g','i','s','t','e','r','e','d','O','w','n','e','r',0};
740     static const WCHAR szRegisteredOrganization[] = {
741         'R','e','g','i','s','t','e','r','e','d','O','r','g','a','n','i','z','a','t','i','o','n',0
742     };
743     static const WCHAR szUSERNAME[] = {'U','S','E','R','N','A','M','E',0};
744     static const WCHAR szCOMPANYNAME[] = {'C','O','M','P','A','N','Y','N','A','M','E',0};
745     static const WCHAR szDate[] = {'D','a','t','e',0};
746     static const WCHAR szTime[] = {'T','i','m','e',0};
747     static const WCHAR szUserLanguageID[] = {'U','s','e','r','L','a','n','g','u','a','g','e','I','D',0};
748     static const WCHAR szSystemLangID[] = {'S','y','s','t','e','m','L','a','n','g','u','a','g','e','I','D',0};
749     static const WCHAR szProductState[] = {'P','r','o','d','u','c','t','S','t','a','t','e',0};
750     static const WCHAR szLogonUser[] = {'L','o','g','o','n','U','s','e','r',0};
751     static const WCHAR szNetHoodFolder[] = {'N','e','t','H','o','o','d','F','o','l','d','e','r',0};
752     static const WCHAR szPrintHoodFolder[] = {'P','r','i','n','t','H','o','o','d','F','o','l','d','e','r',0};
753     static const WCHAR szRecentFolder[] = {'R','e','c','e','n','t','F','o','l','d','e','r',0};
754     static const WCHAR szComputerName[] = {'C','o','m','p','u','t','e','r','N','a','m','e',0};
755
756     /*
757      * Other things that probably should be set:
758      *
759      * VirtualMemory ShellAdvSupport DefaultUIFont PackagecodeChanging
760      * CaptionHeight BorderTop BorderSide TextHeight RedirectedDllSupport
761      */
762
763     SHGetFolderPathW(NULL, CSIDL_COMMON_APPDATA, NULL, 0, pth);
764     strcatW(pth, szBackSlash);
765     msi_set_property(package->db, szCommonAppDataFolder, pth);
766
767     SHGetFolderPathW(NULL, CSIDL_FAVORITES, NULL, 0, pth);
768     strcatW(pth, szBackSlash);
769     msi_set_property(package->db, szFavoritesFolder, pth);
770
771     SHGetFolderPathW(NULL, CSIDL_FONTS, NULL, 0, pth);
772     strcatW(pth, szBackSlash);
773     msi_set_property(package->db, szFontsFolder, pth);
774
775     SHGetFolderPathW(NULL, CSIDL_SENDTO, NULL, 0, pth);
776     strcatW(pth, szBackSlash);
777     msi_set_property(package->db, szSendToFolder, pth);
778
779     SHGetFolderPathW(NULL, CSIDL_STARTMENU, NULL, 0, pth);
780     strcatW(pth, szBackSlash);
781     msi_set_property(package->db, szStartMenuFolder, pth);
782
783     SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, pth);
784     strcatW(pth, szBackSlash);
785     msi_set_property(package->db, szStartupFolder, pth);
786
787     SHGetFolderPathW(NULL, CSIDL_TEMPLATES, NULL, 0, pth);
788     strcatW(pth, szBackSlash);
789     msi_set_property(package->db, szTemplateFolder, pth);
790
791     SHGetFolderPathW(NULL, CSIDL_DESKTOP, NULL, 0, pth);
792     strcatW(pth, szBackSlash);
793     msi_set_property(package->db, szDesktopFolder, pth);
794
795     /* FIXME: set to AllUsers profile path if ALLUSERS is set */
796     SHGetFolderPathW(NULL, CSIDL_PROGRAMS, NULL, 0, pth);
797     strcatW(pth, szBackSlash);
798     msi_set_property(package->db, szProgramMenuFolder, pth);
799
800     SHGetFolderPathW(NULL, CSIDL_ADMINTOOLS, NULL, 0, pth);
801     strcatW(pth, szBackSlash);
802     msi_set_property(package->db, szAdminToolsFolder, pth);
803
804     SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, pth);
805     strcatW(pth, szBackSlash);
806     msi_set_property(package->db, szAppDataFolder, pth);
807
808     SHGetFolderPathW(NULL, CSIDL_SYSTEM, NULL, 0, pth);
809     strcatW(pth, szBackSlash);
810     msi_set_property(package->db, szSystemFolder, pth);
811     msi_set_property(package->db, szSystem16Folder, pth);
812
813     SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, pth);
814     strcatW(pth, szBackSlash);
815     msi_set_property(package->db, szLocalAppDataFolder, pth);
816
817     SHGetFolderPathW(NULL, CSIDL_MYPICTURES, NULL, 0, pth);
818     strcatW(pth, szBackSlash);
819     msi_set_property(package->db, szMyPicturesFolder, pth);
820
821     SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, 0, pth);
822     strcatW(pth, szBackSlash);
823     msi_set_property(package->db, szPersonalFolder, pth);
824
825     SHGetFolderPathW(NULL, CSIDL_WINDOWS, NULL, 0, pth);
826     strcatW(pth, szBackSlash);
827     msi_set_property(package->db, szWindowsFolder, pth);
828     
829     SHGetFolderPathW(NULL, CSIDL_PRINTHOOD, NULL, 0, pth);
830     strcatW(pth, szBackSlash);
831     msi_set_property(package->db, szPrintHoodFolder, pth);
832
833     SHGetFolderPathW(NULL, CSIDL_NETHOOD, NULL, 0, pth);
834     strcatW(pth, szBackSlash);
835     msi_set_property(package->db, szNetHoodFolder, pth);
836
837     SHGetFolderPathW(NULL, CSIDL_RECENT, NULL, 0, pth);
838     strcatW(pth, szBackSlash);
839     msi_set_property(package->db, szRecentFolder, pth);
840
841     /* Physical Memory is specified in MB. Using total amount. */
842     msex.dwLength = sizeof(msex);
843     GlobalMemoryStatusEx( &msex );
844     sprintfW( bufstr, szIntFormat, (int)(msex.ullTotalPhys / 1024 / 1024) );
845     msi_set_property(package->db, szPhysicalMemory, bufstr);
846
847     SHGetFolderPathW(NULL, CSIDL_WINDOWS, NULL, 0, pth);
848     ptr = strchrW(pth,'\\');
849     if (ptr) *(ptr + 1) = 0;
850     msi_set_property(package->db, szWindowsVolume, pth);
851     
852     GetTempPathW(MAX_PATH,pth);
853     msi_set_property(package->db, szTempFolder, pth);
854
855     /* in a wine environment the user is always admin and privileged */
856     msi_set_property(package->db, szAdminUser, szOne);
857     msi_set_property(package->db, szPrivileged, szOne);
858
859     /* set the os things */
860     OSVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
861     GetVersionExW((OSVERSIONINFOW *)&OSVersion);
862     verval = OSVersion.dwMinorVersion + OSVersion.dwMajorVersion * 100;
863     sprintfW(verstr, szFormat, verval);
864     switch (OSVersion.dwPlatformId)
865     {
866         case VER_PLATFORM_WIN32_WINDOWS:    
867             msi_set_property(package->db, szVersion9x, verstr);
868             break;
869         case VER_PLATFORM_WIN32_NT:
870             msi_set_property(package->db, szVersionNT, verstr);
871             sprintfW(verstr, szFormat,OSVersion.wProductType);
872             msi_set_property(package->db, szMsiNTProductType, verstr);
873             break;
874     }
875     sprintfW(verstr, szFormat, OSVersion.dwBuildNumber);
876     msi_set_property(package->db, szWindowsBuild, verstr);
877     /* just fudge this */
878     msi_set_property(package->db, szServicePackLevel, szSix);
879
880     sprintfW( bufstr, szFormat2, MSI_MAJORVERSION, MSI_MINORVERSION);
881     msi_set_property( package->db, szVersionMsi, bufstr );
882     sprintfW( bufstr, szFormat, MSI_MAJORVERSION * 100);
883     msi_set_property( package->db, szVersionDatabase, bufstr );
884
885     GetNativeSystemInfo( &sys_info );
886     sprintfW( bufstr, szIntFormat, sys_info.wProcessorLevel );
887     if (sys_info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
888     {
889         msi_set_property( package->db, szIntel, bufstr );
890
891         GetSystemDirectoryW( pth, MAX_PATH );
892         PathAddBackslashW( pth );
893         msi_set_property( package->db, szSystemFolder, pth );
894
895         SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES, NULL, 0, pth );
896         PathAddBackslashW( pth );
897         msi_set_property( package->db, szProgramFilesFolder, pth );
898
899         SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES_COMMON, NULL, 0, pth );
900         PathAddBackslashW( pth );
901         msi_set_property( package->db, szCommonFilesFolder, pth );
902     }
903     else if (sys_info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
904     {
905         msi_set_property( package->db, szMsiAMD64, bufstr );
906         msi_set_property( package->db, szMsix64, bufstr );
907         msi_set_property( package->db, szVersionNT64, verstr );
908
909         GetSystemDirectoryW( pth, MAX_PATH );
910         PathAddBackslashW( pth );
911         msi_set_property( package->db, szSystem64Folder, pth );
912
913         GetSystemWow64DirectoryW( pth, MAX_PATH );
914         PathAddBackslashW( pth );
915         msi_set_property( package->db, szSystemFolder, pth );
916
917         SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES, NULL, 0, pth );
918         PathAddBackslashW( pth );
919         msi_set_property( package->db, szProgramFiles64Folder, pth );
920
921         SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILESX86, NULL, 0, pth );
922         PathAddBackslashW( pth );
923         msi_set_property( package->db, szProgramFilesFolder, pth );
924
925         SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES_COMMON, NULL, 0, pth );
926         PathAddBackslashW( pth );
927         msi_set_property( package->db, szCommonFiles64Folder, pth );
928
929         SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES_COMMONX86, NULL, 0, pth );
930         PathAddBackslashW( pth );
931         msi_set_property( package->db, szCommonFilesFolder, pth );
932     }
933
934     /* Screen properties. */
935     dc = GetDC(0);
936     sprintfW( bufstr, szIntFormat, GetDeviceCaps( dc, HORZRES ) );
937     msi_set_property( package->db, szScreenX, bufstr );
938     sprintfW( bufstr, szIntFormat, GetDeviceCaps( dc, VERTRES ));
939     msi_set_property( package->db, szScreenY, bufstr );
940     sprintfW( bufstr, szIntFormat, GetDeviceCaps( dc, BITSPIXEL ));
941     msi_set_property( package->db, szColorBits, bufstr );
942     ReleaseDC(0, dc);
943
944     /* USERNAME and COMPANYNAME */
945     username = msi_dup_property( package->db, szUSERNAME );
946     companyname = msi_dup_property( package->db, szCOMPANYNAME );
947
948     if ((!username || !companyname) &&
949         RegOpenKeyW( HKEY_CURRENT_USER, szUserInfo, &hkey ) == ERROR_SUCCESS)
950     {
951         if (!username &&
952             (username = msi_reg_get_val_str( hkey, szDefName )))
953             msi_set_property( package->db, szUSERNAME, username );
954         if (!companyname &&
955             (companyname = msi_reg_get_val_str( hkey, szDefCompany )))
956             msi_set_property( package->db, szCOMPANYNAME, companyname );
957         CloseHandle( hkey );
958     }
959     if ((!username || !companyname) &&
960         RegOpenKeyW( HKEY_LOCAL_MACHINE, szCurrentVersion, &hkey ) == ERROR_SUCCESS)
961     {
962         if (!username &&
963             (username = msi_reg_get_val_str( hkey, szRegisteredUser )))
964             msi_set_property( package->db, szUSERNAME, username );
965         if (!companyname &&
966             (companyname = msi_reg_get_val_str( hkey, szRegisteredOrganization )))
967             msi_set_property( package->db, szCOMPANYNAME, companyname );
968         CloseHandle( hkey );
969     }
970     msi_free( username );
971     msi_free( companyname );
972
973     if ( set_user_sid_prop( package ) != ERROR_SUCCESS)
974         ERR("Failed to set the UserSID property\n");
975
976     /* Date and time properties */
977     GetSystemTime( &systemtime );
978     if (GetDateFormatW( LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systemtime,
979                         NULL, bufstr, sizeof(bufstr)/sizeof(bufstr[0]) ))
980         msi_set_property( package->db, szDate, bufstr );
981     else
982         ERR("Couldn't set Date property: GetDateFormat failed with error %d\n", GetLastError());
983
984     if (GetTimeFormatW( LOCALE_USER_DEFAULT,
985                         TIME_FORCE24HOURFORMAT | TIME_NOTIMEMARKER,
986                         &systemtime, NULL, bufstr,
987                         sizeof(bufstr)/sizeof(bufstr[0]) ))
988         msi_set_property( package->db, szTime, bufstr );
989     else
990         ERR("Couldn't set Time property: GetTimeFormat failed with error %d\n", GetLastError());
991
992     set_msi_assembly_prop( package );
993
994     langid = GetUserDefaultLangID();
995     sprintfW(bufstr, szIntFormat, langid);
996     msi_set_property( package->db, szUserLanguageID, bufstr );
997
998     langid = GetSystemDefaultLangID();
999     sprintfW(bufstr, szIntFormat, langid);
1000     msi_set_property( package->db, szSystemLangID, bufstr );
1001
1002     sprintfW(bufstr, szIntFormat, MsiQueryProductStateW(package->ProductCode));
1003     msi_set_property( package->db, szProductState, bufstr );
1004
1005     len = 0;
1006     if (!GetUserNameW( NULL, &len ) && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1007     {
1008         WCHAR *username;
1009         if ((username = msi_alloc( len * sizeof(WCHAR) )))
1010         {
1011             if (GetUserNameW( username, &len ))
1012                 msi_set_property( package->db, szLogonUser, username );
1013             msi_free( username );
1014         }
1015     }
1016     len = 0;
1017     if (!GetComputerNameW( NULL, &len ) && GetLastError() == ERROR_BUFFER_OVERFLOW)
1018     {
1019         WCHAR *computername;
1020         if ((computername = msi_alloc( len * sizeof(WCHAR) )))
1021         {
1022             if (GetComputerNameW( computername, &len ))
1023                 msi_set_property( package->db, szComputerName, computername );
1024             msi_free( computername );
1025         }
1026     }
1027 }
1028
1029 static UINT msi_load_summary_properties( MSIPACKAGE *package )
1030 {
1031     UINT rc;
1032     MSIHANDLE suminfo;
1033     MSIHANDLE hdb = alloc_msihandle( &package->db->hdr );
1034     INT count;
1035     DWORD len;
1036     LPWSTR package_code;
1037     static const WCHAR szPackageCode[] = {
1038         'P','a','c','k','a','g','e','C','o','d','e',0};
1039
1040     if (!hdb) {
1041         ERR("Unable to allocate handle\n");
1042         return ERROR_OUTOFMEMORY;
1043     }
1044
1045     rc = MsiGetSummaryInformationW( hdb, NULL, 0, &suminfo );
1046     MsiCloseHandle(hdb);
1047     if (rc != ERROR_SUCCESS)
1048     {
1049         ERR("Unable to open Summary Information\n");
1050         return rc;
1051     }
1052
1053     rc = MsiSummaryInfoGetPropertyW( suminfo, PID_PAGECOUNT, NULL,
1054                                      &count, NULL, NULL, NULL );
1055     if (rc != ERROR_SUCCESS)
1056     {
1057         WARN("Unable to query page count: %d\n", rc);
1058         goto done;
1059     }
1060
1061     /* load package code property */
1062     len = 0;
1063     rc = MsiSummaryInfoGetPropertyW( suminfo, PID_REVNUMBER, NULL,
1064                                      NULL, NULL, NULL, &len );
1065     if (rc != ERROR_MORE_DATA)
1066     {
1067         WARN("Unable to query revision number: %d\n", rc);
1068         rc = ERROR_FUNCTION_FAILED;
1069         goto done;
1070     }
1071
1072     len++;
1073     package_code = msi_alloc( len * sizeof(WCHAR) );
1074     rc = MsiSummaryInfoGetPropertyW( suminfo, PID_REVNUMBER, NULL,
1075                                      NULL, NULL, package_code, &len );
1076     if (rc != ERROR_SUCCESS)
1077     {
1078         WARN("Unable to query rev number: %d\n", rc);
1079         goto done;
1080     }
1081
1082     msi_set_property( package->db, szPackageCode, package_code );
1083     msi_free( package_code );
1084
1085     /* load package attributes */
1086     count = 0;
1087     MsiSummaryInfoGetPropertyW( suminfo, PID_WORDCOUNT, NULL,
1088                                 &count, NULL, NULL, NULL );
1089     package->WordCount = count;
1090
1091 done:
1092     MsiCloseHandle(suminfo);
1093     return rc;
1094 }
1095
1096 static MSIPACKAGE *msi_alloc_package( void )
1097 {
1098     MSIPACKAGE *package;
1099
1100     package = alloc_msiobject( MSIHANDLETYPE_PACKAGE, sizeof (MSIPACKAGE),
1101                                MSI_FreePackage );
1102     if( package )
1103     {
1104         list_init( &package->components );
1105         list_init( &package->features );
1106         list_init( &package->files );
1107         list_init( &package->filepatches );
1108         list_init( &package->tempfiles );
1109         list_init( &package->folders );
1110         list_init( &package->subscriptions );
1111         list_init( &package->appids );
1112         list_init( &package->classes );
1113         list_init( &package->mimes );
1114         list_init( &package->extensions );
1115         list_init( &package->progids );
1116         list_init( &package->RunningActions );
1117         list_init( &package->sourcelist_info );
1118         list_init( &package->sourcelist_media );
1119         list_init( &package->patches );
1120         list_init( &package->binaries );
1121         list_init( &package->cabinet_streams );
1122     }
1123
1124     return package;
1125 }
1126
1127 static UINT msi_load_admin_properties(MSIPACKAGE *package)
1128 {
1129     BYTE *data;
1130     UINT r, sz;
1131
1132     static const WCHAR stmname[] = {'A','d','m','i','n','P','r','o','p','e','r','t','i','e','s',0};
1133
1134     r = read_stream_data(package->db->storage, stmname, FALSE, &data, &sz);
1135     if (r != ERROR_SUCCESS)
1136         return r;
1137
1138     r = msi_parse_command_line(package, (WCHAR *)data, TRUE);
1139
1140     msi_free(data);
1141     return r;
1142 }
1143
1144 void msi_adjust_privilege_properties( MSIPACKAGE *package )
1145 {
1146     /* FIXME: this should depend on the user's privileges */
1147     if (msi_get_property_int( package->db, szAllUsers, 0 ) == 2)
1148     {
1149         TRACE("resetting ALLUSERS property from 2 to 1\n");
1150         msi_set_property( package->db, szAllUsers, szOne );
1151     }
1152     msi_set_property( package->db, szAdminUser, szOne );
1153 }
1154
1155 MSIPACKAGE *MSI_CreatePackage( MSIDATABASE *db, LPCWSTR base_url )
1156 {
1157     static const WCHAR szpi[] = {'%','i',0};
1158     MSIPACKAGE *package;
1159     WCHAR uilevel[10];
1160     UINT r;
1161
1162     TRACE("%p\n", db);
1163
1164     package = msi_alloc_package();
1165     if (package)
1166     {
1167         msiobj_addref( &db->hdr );
1168         package->db = db;
1169
1170         package->WordCount = 0;
1171         package->PackagePath = strdupW( db->path );
1172         package->BaseURL = strdupW( base_url );
1173
1174         create_temp_property_table( package );
1175         msi_clone_properties( package );
1176         msi_adjust_privilege_properties( package );
1177
1178         package->ProductCode = msi_dup_property( package->db, szProductCode );
1179         package->script = msi_alloc_zero( sizeof(MSISCRIPT) );
1180
1181         set_installed_prop( package );
1182         set_installer_properties( package );
1183
1184         sprintfW(uilevel,szpi,gUILevel);
1185         msi_set_property(package->db, szUILevel, uilevel);
1186
1187         r = msi_load_summary_properties( package );
1188         if (r != ERROR_SUCCESS)
1189         {
1190             msiobj_release( &package->hdr );
1191             return NULL;
1192         }
1193
1194         if (package->WordCount & msidbSumInfoSourceTypeAdminImage)
1195             msi_load_admin_properties( package );
1196
1197         package->log_file = INVALID_HANDLE_VALUE;
1198     }
1199     return package;
1200 }
1201
1202 UINT msi_download_file( LPCWSTR szUrl, LPWSTR filename )
1203 {
1204     LPINTERNET_CACHE_ENTRY_INFOW cache_entry;
1205     DWORD size = 0;
1206     HRESULT hr;
1207
1208     /* call will always fail, because size is 0,
1209      * but will return ERROR_FILE_NOT_FOUND first
1210      * if the file doesn't exist
1211      */
1212     GetUrlCacheEntryInfoW( szUrl, NULL, &size );
1213     if ( GetLastError() != ERROR_FILE_NOT_FOUND )
1214     {
1215         cache_entry = msi_alloc( size );
1216         if ( !GetUrlCacheEntryInfoW( szUrl, cache_entry, &size ) )
1217         {
1218             UINT error = GetLastError();
1219             msi_free( cache_entry );
1220             return error;
1221         }
1222
1223         lstrcpyW( filename, cache_entry->lpszLocalFileName );
1224         msi_free( cache_entry );
1225         return ERROR_SUCCESS;
1226     }
1227
1228     hr = URLDownloadToCacheFileW( NULL, szUrl, filename, MAX_PATH, 0, NULL );
1229     if ( FAILED(hr) )
1230     {
1231         WARN("failed to download %s to cache file\n", debugstr_w(szUrl));
1232         return ERROR_FUNCTION_FAILED;
1233     }
1234
1235     return ERROR_SUCCESS;
1236 }
1237
1238 UINT msi_create_empty_local_file( LPWSTR path, LPCWSTR suffix )
1239 {
1240     static const WCHAR szInstaller[] = {
1241         '\\','I','n','s','t','a','l','l','e','r','\\',0};
1242     static const WCHAR fmt[] = {'%','x',0};
1243     DWORD time, len, i, offset;
1244     HANDLE handle;
1245
1246     time = GetTickCount();
1247     GetWindowsDirectoryW( path, MAX_PATH );
1248     strcatW( path, szInstaller );
1249     CreateDirectoryW( path, NULL );
1250
1251     len = strlenW(path);
1252     for (i = 0; i < 0x10000; i++)
1253     {
1254         offset = snprintfW( path + len, MAX_PATH - len, fmt, (time + i) & 0xffff );
1255         memcpy( path + len + offset, suffix, (strlenW( suffix ) + 1) * sizeof(WCHAR) );
1256         handle = CreateFileW( path, GENERIC_WRITE, 0, NULL,
1257                               CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
1258         if (handle != INVALID_HANDLE_VALUE)
1259         {
1260             CloseHandle(handle);
1261             break;
1262         }
1263         if (GetLastError() != ERROR_FILE_EXISTS &&
1264             GetLastError() != ERROR_SHARING_VIOLATION)
1265             return ERROR_FUNCTION_FAILED;
1266     }
1267
1268     return ERROR_SUCCESS;
1269 }
1270
1271 static UINT msi_parse_summary( MSISUMMARYINFO *si, MSIPACKAGE *package )
1272 {
1273     WCHAR *template, *p, *q;
1274     DWORD i, count;
1275
1276     package->version = msi_suminfo_get_int32( si, PID_PAGECOUNT );
1277     TRACE("version: %d\n", package->version);
1278
1279     template = msi_suminfo_dup_string( si, PID_TEMPLATE );
1280     if (!template)
1281         return ERROR_SUCCESS; /* native accepts missing template property */
1282
1283     TRACE("template: %s\n", debugstr_w(template));
1284
1285     p = strchrW( template, ';' );
1286     if (!p)
1287     {
1288         WARN("invalid template string %s\n", debugstr_w(template));
1289         msi_free( template );
1290         return ERROR_PATCH_PACKAGE_INVALID;
1291     }
1292     *p = 0;
1293     if ((q = strchrW( template, ',' ))) *q = 0;
1294     if (!template[0] || !strcmpW( template, szIntel ))
1295         package->platform = PLATFORM_INTEL;
1296     else if (!strcmpW( template, szIntel64 ))
1297         package->platform = PLATFORM_INTEL64;
1298     else if (!strcmpW( template, szX64 ) || !strcmpW( template, szAMD64 ))
1299         package->platform = PLATFORM_X64;
1300     else
1301     {
1302         WARN("unknown platform %s\n", debugstr_w(template));
1303         msi_free( template );
1304         return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1305     }
1306     p++;
1307     if (!*p)
1308     {
1309         msi_free( template );
1310         return ERROR_SUCCESS;
1311     }
1312     count = 1;
1313     for (q = p; (q = strchrW( q, ',' )); q++) count++;
1314
1315     package->langids = msi_alloc( count * sizeof(LANGID) );
1316     if (!package->langids)
1317     {
1318         msi_free( template );
1319         return ERROR_OUTOFMEMORY;
1320     }
1321
1322     i = 0;
1323     while (*p)
1324     {
1325         q = strchrW( p, ',' );
1326         if (q) *q = 0;
1327         package->langids[i] = atoiW( p );
1328         if (!q) break;
1329         p = q + 1;
1330         i++;
1331     }
1332     package->num_langids = i + 1;
1333
1334     msi_free( template );
1335     return ERROR_SUCCESS;
1336 }
1337
1338 static UINT validate_package( MSIPACKAGE *package )
1339 {
1340     BOOL is_wow64;
1341     UINT i;
1342
1343     if (package->platform == PLATFORM_INTEL64)
1344     {
1345         return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1346     }
1347     IsWow64Process( GetCurrentProcess(), &is_wow64 );
1348     if (package->platform == PLATFORM_X64)
1349     {
1350         if (!is_64bit && !is_wow64)
1351             return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1352         if (package->version < 200)
1353             return ERROR_INSTALL_PACKAGE_INVALID;
1354     }
1355     if (!package->num_langids)
1356     {
1357         return ERROR_SUCCESS;
1358     }
1359     for (i = 0; i < package->num_langids; i++)
1360     {
1361         LANGID langid = package->langids[i];
1362
1363         if (PRIMARYLANGID( langid ) == LANG_NEUTRAL)
1364         {
1365             langid = MAKELANGID( PRIMARYLANGID( GetSystemDefaultLangID() ), SUBLANGID( langid ) );
1366         }
1367         if (SUBLANGID( langid ) == SUBLANG_NEUTRAL)
1368         {
1369             langid = MAKELANGID( PRIMARYLANGID( langid ), SUBLANGID( GetSystemDefaultLangID() ) );
1370         }
1371         if (IsValidLocale( langid, LCID_INSTALLED ))
1372             return ERROR_SUCCESS;
1373     }
1374     return ERROR_INSTALL_LANGUAGE_UNSUPPORTED;
1375 }
1376
1377 int msi_track_tempfile( MSIPACKAGE *package, const WCHAR *path )
1378 {
1379     MSITEMPFILE *temp;
1380
1381     TRACE("%s\n", debugstr_w(path));
1382
1383     LIST_FOR_EACH_ENTRY( temp, &package->tempfiles, MSITEMPFILE, entry )
1384     {
1385         if (!strcmpW( path, temp->Path )) return 0;
1386     }
1387     if (!(temp = msi_alloc_zero( sizeof (MSITEMPFILE) ))) return -1;
1388     list_add_head( &package->tempfiles, &temp->entry );
1389     temp->Path = strdupW( path );
1390     return 0;
1391 }
1392
1393 static WCHAR *get_product_code( MSIDATABASE *db )
1394 {
1395     static const WCHAR query[] = {
1396         'S','E','L','E','C','T',' ','`','V','a','l','u','e','`',' ',
1397         'F','R','O','M',' ','`','P','r','o','p','e','r','t','y','`',' ',
1398         'W','H','E','R','E',' ','`','P','r','o','p','e','r','t','y','`','=',
1399         '\'','P','r','o','d','u','c','t','C','o','d','e','\'',0};
1400     MSIQUERY *view;
1401     MSIRECORD *rec;
1402     WCHAR *ret = NULL;
1403
1404     if (MSI_DatabaseOpenViewW( db, query, &view ) != ERROR_SUCCESS)
1405     {
1406         return NULL;
1407     }
1408     if (MSI_ViewExecute( view, 0 ) != ERROR_SUCCESS)
1409     {
1410         MSI_ViewClose( view );
1411         msiobj_release( &view->hdr );
1412         return NULL;
1413     }
1414     if (MSI_ViewFetch( view, &rec ) == ERROR_SUCCESS)
1415     {
1416         ret = strdupW( MSI_RecordGetString( rec, 1 ) );
1417         msiobj_release( &rec->hdr );
1418     }
1419     MSI_ViewClose( view );
1420     msiobj_release( &view->hdr );
1421     return ret;
1422 }
1423
1424 static UINT get_registered_local_package( const WCHAR *product, const WCHAR *package, WCHAR *localfile )
1425 {
1426     MSIINSTALLCONTEXT context;
1427     HKEY product_key, props_key;
1428     WCHAR *registered_package = NULL, unsquashed[GUID_SIZE];
1429     UINT r;
1430
1431     r = msi_locate_product( product, &context );
1432     if (r != ERROR_SUCCESS)
1433         return r;
1434
1435     r = MSIREG_OpenProductKey( product, NULL, context, &product_key, FALSE );
1436     if (r != ERROR_SUCCESS)
1437         return r;
1438
1439     r = MSIREG_OpenInstallProps( product, context, NULL, &props_key, FALSE );
1440     if (r != ERROR_SUCCESS)
1441     {
1442         RegCloseKey( product_key );
1443         return r;
1444     }
1445     r = ERROR_FUNCTION_FAILED;
1446     registered_package = msi_reg_get_val_str( product_key, INSTALLPROPERTY_PACKAGECODEW );
1447     if (!registered_package)
1448         goto done;
1449
1450     unsquash_guid( registered_package, unsquashed );
1451     if (!strcmpiW( package, unsquashed ))
1452     {
1453         WCHAR *filename = msi_reg_get_val_str( props_key, INSTALLPROPERTY_LOCALPACKAGEW );
1454         strcpyW( localfile, filename );
1455         msi_free( filename );
1456         r = ERROR_SUCCESS;
1457     }
1458 done:
1459     msi_free( registered_package );
1460     RegCloseKey( props_key );
1461     RegCloseKey( product_key );
1462     return r;
1463 }
1464
1465 static WCHAR *get_package_code( MSIDATABASE *db )
1466 {
1467     WCHAR *ret;
1468     MSISUMMARYINFO *si;
1469
1470     if (!(si = MSI_GetSummaryInformationW( db->storage, 0 )))
1471     {
1472         WARN("failed to load summary info\n");
1473         return NULL;
1474     }
1475     ret = msi_suminfo_dup_string( si, PID_REVNUMBER );
1476     msiobj_release( &si->hdr );
1477     return ret;
1478 }
1479
1480 static UINT get_local_package( const WCHAR *filename, WCHAR *localfile )
1481 {
1482     WCHAR *product_code, *package_code;
1483     MSIDATABASE *db;
1484     UINT r;
1485
1486     if ((r = MSI_OpenDatabaseW( filename, MSIDBOPEN_READONLY, &db )) != ERROR_SUCCESS)
1487     {
1488         if (GetFileAttributesW( filename ) == INVALID_FILE_ATTRIBUTES)
1489             return ERROR_FILE_NOT_FOUND;
1490         return r;
1491     }
1492     if (!(product_code = get_product_code( db )))
1493     {
1494         msiobj_release( &db->hdr );
1495         return ERROR_INSTALL_PACKAGE_INVALID;
1496     }
1497     if (!(package_code = get_package_code( db )))
1498     {
1499         msi_free( product_code );
1500         msiobj_release( &db->hdr );
1501         return ERROR_INSTALL_PACKAGE_INVALID;
1502     }
1503     r = get_registered_local_package( product_code, package_code, localfile );
1504     msi_free( package_code );
1505     msi_free( product_code );
1506     msiobj_release( &db->hdr );
1507     return r;
1508 }
1509
1510 UINT MSI_OpenPackageW(LPCWSTR szPackage, MSIPACKAGE **pPackage)
1511 {
1512     static const WCHAR dotmsi[] = {'.','m','s','i',0};
1513     MSIDATABASE *db;
1514     MSIPACKAGE *package;
1515     MSIHANDLE handle;
1516     LPWSTR ptr, base_url = NULL;
1517     UINT r;
1518     WCHAR localfile[MAX_PATH], cachefile[MAX_PATH];
1519     LPCWSTR file = szPackage;
1520     DWORD index = 0;
1521     MSISUMMARYINFO *si;
1522     BOOL delete_on_close = FALSE;
1523
1524     TRACE("%s %p\n", debugstr_w(szPackage), pPackage);
1525
1526     localfile[0] = 0;
1527     if( szPackage[0] == '#' )
1528     {
1529         handle = atoiW(&szPackage[1]);
1530         db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
1531         if( !db )
1532         {
1533             IWineMsiRemoteDatabase *remote_database;
1534
1535             remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
1536             if ( !remote_database )
1537                 return ERROR_INVALID_HANDLE;
1538
1539             IWineMsiRemoteDatabase_Release( remote_database );
1540             WARN("MsiOpenPackage not allowed during a custom action!\n");
1541
1542             return ERROR_FUNCTION_FAILED;
1543         }
1544     }
1545     else
1546     {
1547         if ( UrlIsW( szPackage, URLIS_URL ) )
1548         {
1549             r = msi_download_file( szPackage, cachefile );
1550             if (r != ERROR_SUCCESS)
1551                 return r;
1552
1553             file = cachefile;
1554
1555             base_url = strdupW( szPackage );
1556             if (!base_url)
1557                 return ERROR_OUTOFMEMORY;
1558
1559             ptr = strrchrW( base_url, '/' );
1560             if (ptr) *(ptr + 1) = '\0';
1561         }
1562         r = get_local_package( file, localfile );
1563         if (r != ERROR_SUCCESS || GetFileAttributesW( localfile ) == INVALID_FILE_ATTRIBUTES)
1564         {
1565             r = msi_create_empty_local_file( localfile, dotmsi );
1566             if (r != ERROR_SUCCESS)
1567                 return r;
1568
1569             if (!CopyFileW( file, localfile, FALSE ))
1570             {
1571                 r = GetLastError();
1572                 WARN("unable to copy package %s to %s (%u)\n", debugstr_w(file), debugstr_w(localfile), r);
1573                 DeleteFileW( localfile );
1574                 return r;
1575             }
1576             delete_on_close = TRUE;
1577         }
1578         TRACE("opening package %s\n", debugstr_w( localfile ));
1579         r = MSI_OpenDatabaseW( localfile, MSIDBOPEN_TRANSACT, &db );
1580         if (r != ERROR_SUCCESS)
1581             return r;
1582     }
1583     package = MSI_CreatePackage( db, base_url );
1584     msi_free( base_url );
1585     msiobj_release( &db->hdr );
1586     if (!package) return ERROR_INSTALL_PACKAGE_INVALID;
1587     package->localfile = strdupW( localfile );
1588     package->delete_on_close = delete_on_close;
1589
1590     si = MSI_GetSummaryInformationW( db->storage, 0 );
1591     if (!si)
1592     {
1593         WARN("failed to load summary info\n");
1594         msiobj_release( &package->hdr );
1595         return ERROR_INSTALL_PACKAGE_INVALID;
1596     }
1597     r = msi_parse_summary( si, package );
1598     msiobj_release( &si->hdr );
1599     if (r != ERROR_SUCCESS)
1600     {
1601         WARN("failed to parse summary info %u\n", r);
1602         msiobj_release( &package->hdr );
1603         return r;
1604     }
1605     r = validate_package( package );
1606     if (r != ERROR_SUCCESS)
1607     {
1608         msiobj_release( &package->hdr );
1609         return r;
1610     }
1611     msi_set_property( package->db, szDatabase, db->path );
1612
1613     if( UrlIsW( szPackage, URLIS_URL ) )
1614         msi_set_property( package->db, szOriginalDatabase, szPackage );
1615     else if( szPackage[0] == '#' )
1616         msi_set_property( package->db, szOriginalDatabase, db->path );
1617     else
1618     {
1619         WCHAR fullpath[MAX_PATH];
1620
1621         GetFullPathNameW( szPackage, MAX_PATH, fullpath, NULL );
1622         msi_set_property( package->db, szOriginalDatabase, fullpath );
1623     }
1624     msi_set_context( package );
1625
1626     while (1)
1627     {
1628         WCHAR patch_code[GUID_SIZE];
1629         r = MsiEnumPatchesExW( package->ProductCode, NULL, package->Context,
1630                                MSIPATCHSTATE_APPLIED, index, patch_code, NULL, NULL, NULL, NULL );
1631         if (r != ERROR_SUCCESS)
1632             break;
1633
1634         TRACE("found registered patch %s\n", debugstr_w(patch_code));
1635
1636         r = msi_apply_registered_patch( package, patch_code );
1637         if (r != ERROR_SUCCESS)
1638         {
1639             ERR("registered patch failed to apply %u\n", r);
1640             msiobj_release( &package->hdr );
1641             return r;
1642         }
1643         index++;
1644     }
1645     if (index)
1646     {
1647         msi_clone_properties( package );
1648         msi_adjust_privilege_properties( package );
1649     }
1650     if (gszLogFile)
1651         package->log_file = CreateFileW( gszLogFile, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
1652                                          OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
1653     *pPackage = package;
1654     return ERROR_SUCCESS;
1655 }
1656
1657 UINT WINAPI MsiOpenPackageExW(LPCWSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
1658 {
1659     MSIPACKAGE *package = NULL;
1660     UINT ret;
1661
1662     TRACE("%s %08x %p\n", debugstr_w(szPackage), dwOptions, phPackage );
1663
1664     if( !szPackage || !phPackage )
1665         return ERROR_INVALID_PARAMETER;
1666
1667     if ( !*szPackage )
1668     {
1669         FIXME("Should create an empty database and package\n");
1670         return ERROR_FUNCTION_FAILED;
1671     }
1672
1673     if( dwOptions )
1674         FIXME("dwOptions %08x not supported\n", dwOptions);
1675
1676     ret = MSI_OpenPackageW( szPackage, &package );
1677     if( ret == ERROR_SUCCESS )
1678     {
1679         *phPackage = alloc_msihandle( &package->hdr );
1680         if (! *phPackage)
1681             ret = ERROR_NOT_ENOUGH_MEMORY;
1682         msiobj_release( &package->hdr );
1683     }
1684
1685     return ret;
1686 }
1687
1688 UINT WINAPI MsiOpenPackageW(LPCWSTR szPackage, MSIHANDLE *phPackage)
1689 {
1690     return MsiOpenPackageExW( szPackage, 0, phPackage );
1691 }
1692
1693 UINT WINAPI MsiOpenPackageExA(LPCSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
1694 {
1695     LPWSTR szwPack = NULL;
1696     UINT ret;
1697
1698     if( szPackage )
1699     {
1700         szwPack = strdupAtoW( szPackage );
1701         if( !szwPack )
1702             return ERROR_OUTOFMEMORY;
1703     }
1704
1705     ret = MsiOpenPackageExW( szwPack, dwOptions, phPackage );
1706
1707     msi_free( szwPack );
1708
1709     return ret;
1710 }
1711
1712 UINT WINAPI MsiOpenPackageA(LPCSTR szPackage, MSIHANDLE *phPackage)
1713 {
1714     return MsiOpenPackageExA( szPackage, 0, phPackage );
1715 }
1716
1717 MSIHANDLE WINAPI MsiGetActiveDatabase(MSIHANDLE hInstall)
1718 {
1719     MSIPACKAGE *package;
1720     MSIHANDLE handle = 0;
1721     IUnknown *remote_unk;
1722     IWineMsiRemotePackage *remote_package;
1723
1724     TRACE("(%d)\n",hInstall);
1725
1726     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
1727     if( package)
1728     {
1729         handle = alloc_msihandle( &package->db->hdr );
1730         msiobj_release( &package->hdr );
1731     }
1732     else if ((remote_unk = msi_get_remote(hInstall)))
1733     {
1734         if (IUnknown_QueryInterface(remote_unk, &IID_IWineMsiRemotePackage,
1735                                         (LPVOID *)&remote_package) == S_OK)
1736         {
1737             IWineMsiRemotePackage_GetActiveDatabase(remote_package, &handle);
1738             IWineMsiRemotePackage_Release(remote_package);
1739         }
1740         else
1741         {
1742             WARN("remote handle %d is not a package\n", hInstall);
1743         }
1744         IUnknown_Release(remote_unk);
1745     }
1746
1747     return handle;
1748 }
1749
1750 INT MSI_ProcessMessage( MSIPACKAGE *package, INSTALLMESSAGE eMessageType, MSIRECORD *record )
1751 {
1752     static const WCHAR szActionData[] =
1753         {'A','c','t','i','o','n','D','a','t','a',0};
1754     static const WCHAR szSetProgress[] =
1755         {'S','e','t','P','r','o','g','r','e','s','s',0};
1756     static const WCHAR szActionText[] =
1757         {'A','c','t','i','o','n','T','e','x','t',0};
1758     LPWSTR message;
1759     DWORD i, len, total_len, log_type = 0;
1760     INT rc = 0;
1761     char *msg;
1762
1763     TRACE("%x\n", eMessageType);
1764
1765     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_FATALEXIT)
1766         log_type |= INSTALLLOGMODE_FATALEXIT;
1767     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ERROR)
1768         log_type |= INSTALLLOGMODE_ERROR;
1769     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_WARNING)
1770         log_type |= INSTALLLOGMODE_WARNING;
1771     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_USER)
1772         log_type |= INSTALLLOGMODE_USER;
1773     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_INFO)
1774         log_type |= INSTALLLOGMODE_INFO;
1775     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_RESOLVESOURCE)
1776         log_type |= INSTALLLOGMODE_RESOLVESOURCE;
1777     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_OUTOFDISKSPACE)
1778         log_type |= INSTALLLOGMODE_OUTOFDISKSPACE;
1779     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_COMMONDATA)
1780         log_type |= INSTALLLOGMODE_COMMONDATA;
1781     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
1782         log_type |= INSTALLLOGMODE_ACTIONSTART;
1783     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONDATA)
1784         log_type |= INSTALLLOGMODE_ACTIONDATA;
1785     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_PROGRESS)
1786         log_type |= INSTALLLOGMODE_PROGRESS;
1787     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_INITIALIZE)
1788         log_type |= INSTALLLOGMODE_INITIALIZE;
1789     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_TERMINATE)
1790         log_type |= INSTALLLOGMODE_TERMINATE;
1791     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_SHOWDIALOG)
1792         log_type |= INSTALLLOGMODE_SHOWDIALOG;
1793
1794     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
1795     {
1796         static const WCHAR template_s[]=
1797             {'A','c','t','i','o','n',' ','%','s',':',' ','%','s','.',' ',0};
1798         static const WCHAR format[] = 
1799             {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
1800         WCHAR timet[0x100];
1801         LPCWSTR action_text, action;
1802         LPWSTR deformatted = NULL;
1803
1804         GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
1805
1806         action = MSI_RecordGetString(record, 1);
1807         action_text = MSI_RecordGetString(record, 2);
1808
1809         if (!action || !action_text)
1810             return IDOK;
1811
1812         deformat_string(package, action_text, &deformatted);
1813
1814         len = strlenW(timet) + strlenW(action) + strlenW(template_s);
1815         if (deformatted)
1816             len += strlenW(deformatted);
1817         message = msi_alloc(len*sizeof(WCHAR));
1818         sprintfW(message, template_s, timet, action);
1819         if (deformatted)
1820             strcatW(message, deformatted);
1821         msi_free(deformatted);
1822     }
1823     else
1824     {
1825         static const WCHAR format[] = {'%','u',':',' ',0};
1826         UINT count = MSI_RecordGetFieldCount( record );
1827         WCHAR *p;
1828
1829         total_len = 1;
1830         for (i = 1; i <= count; i++)
1831         {
1832             len = 0;
1833             MSI_RecordGetStringW( record, i, NULL, &len );
1834             total_len += len + 13;
1835         }
1836         p = message = msi_alloc( total_len * sizeof(WCHAR) );
1837         if (!p) return ERROR_OUTOFMEMORY;
1838
1839         for (i = 1; i <= count; i++)
1840         {
1841             if (count > 1)
1842             {
1843                 len = sprintfW( p, format, i );
1844                 total_len -= len;
1845                 p += len;
1846             }
1847             len = total_len;
1848             MSI_RecordGetStringW( record, i, p, &len );
1849             total_len -= len;
1850             p += len;
1851             if (count > 1 && total_len)
1852             {
1853                 *p++ = ' ';
1854                 total_len--;
1855             }
1856         }
1857         p[0] = 0;
1858     }
1859
1860     TRACE("%p %p %p %x %x %s\n", gUIHandlerA, gUIHandlerW, gUIHandlerRecord,
1861           gUIFilter, log_type, debugstr_w(message));
1862
1863     /* convert it to ASCII */
1864     len = WideCharToMultiByte( CP_ACP, 0, message, -1, NULL, 0, NULL, NULL );
1865     msg = msi_alloc( len );
1866     WideCharToMultiByte( CP_ACP, 0, message, -1, msg, len, NULL, NULL );
1867
1868     if (gUIHandlerW && (gUIFilter & log_type))
1869     {
1870         rc = gUIHandlerW( gUIContext, eMessageType, message );
1871     }
1872     else if (gUIHandlerA && (gUIFilter & log_type))
1873     {
1874         rc = gUIHandlerA( gUIContext, eMessageType, msg );
1875     }
1876     else if (gUIHandlerRecord && (gUIFilter & log_type))
1877     {
1878         MSIHANDLE rec = MsiCreateRecord( 1 );
1879         MsiRecordSetStringW( rec, 0, message );
1880         rc = gUIHandlerRecord( gUIContext, eMessageType, rec );
1881         MsiCloseHandle( rec );
1882     }
1883
1884     if (!rc && package->log_file != INVALID_HANDLE_VALUE &&
1885         (eMessageType & 0xff000000) != INSTALLMESSAGE_PROGRESS)
1886     {
1887         DWORD written;
1888         WriteFile( package->log_file, msg, len - 1, &written, NULL );
1889         WriteFile( package->log_file, "\n", 1, &written, NULL );
1890     }
1891     msi_free( msg );
1892     msi_free( message );
1893
1894     switch (eMessageType & 0xff000000)
1895     {
1896     case INSTALLMESSAGE_ACTIONDATA:
1897         /* FIXME: format record here instead of in ui_actiondata to get the
1898          * correct action data for external scripts */
1899         ControlEvent_FireSubscribedEvent(package, szActionData, record);
1900         break;
1901     case INSTALLMESSAGE_ACTIONSTART:
1902     {
1903         MSIRECORD *uirow;
1904         LPWSTR deformated;
1905         LPCWSTR action_text = MSI_RecordGetString(record, 2);
1906
1907         deformat_string(package, action_text, &deformated);
1908         uirow = MSI_CreateRecord(1);
1909         MSI_RecordSetStringW(uirow, 1, deformated);
1910         TRACE("INSTALLMESSAGE_ACTIONSTART: %s\n", debugstr_w(deformated));
1911         msi_free(deformated);
1912
1913         ControlEvent_FireSubscribedEvent(package, szActionText, uirow);
1914
1915         msiobj_release(&uirow->hdr);
1916         break;
1917     }
1918     case INSTALLMESSAGE_PROGRESS:
1919         ControlEvent_FireSubscribedEvent(package, szSetProgress, record);
1920         break;
1921     }
1922
1923     return ERROR_SUCCESS;
1924 }
1925
1926 INT WINAPI MsiProcessMessage( MSIHANDLE hInstall, INSTALLMESSAGE eMessageType,
1927                               MSIHANDLE hRecord)
1928 {
1929     UINT ret = ERROR_INVALID_HANDLE;
1930     MSIPACKAGE *package = NULL;
1931     MSIRECORD *record = NULL;
1932
1933     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
1934     if( !package )
1935     {
1936         HRESULT hr;
1937         IWineMsiRemotePackage *remote_package;
1938
1939         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
1940         if (!remote_package)
1941             return ERROR_INVALID_HANDLE;
1942
1943         hr = IWineMsiRemotePackage_ProcessMessage( remote_package, eMessageType, hRecord );
1944
1945         IWineMsiRemotePackage_Release( remote_package );
1946
1947         if (FAILED(hr))
1948         {
1949             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1950                 return HRESULT_CODE(hr);
1951
1952             return ERROR_FUNCTION_FAILED;
1953         }
1954
1955         return ERROR_SUCCESS;
1956     }
1957
1958     record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
1959     if( !record )
1960         goto out;
1961
1962     ret = MSI_ProcessMessage( package, eMessageType, record );
1963
1964 out:
1965     msiobj_release( &package->hdr );
1966     if( record )
1967         msiobj_release( &record->hdr );
1968
1969     return ret;
1970 }
1971
1972 /* property code */
1973
1974 UINT WINAPI MsiSetPropertyA( MSIHANDLE hInstall, LPCSTR szName, LPCSTR szValue )
1975 {
1976     LPWSTR szwName = NULL, szwValue = NULL;
1977     UINT r = ERROR_OUTOFMEMORY;
1978
1979     szwName = strdupAtoW( szName );
1980     if( szName && !szwName )
1981         goto end;
1982
1983     szwValue = strdupAtoW( szValue );
1984     if( szValue && !szwValue )
1985         goto end;
1986
1987     r = MsiSetPropertyW( hInstall, szwName, szwValue);
1988
1989 end:
1990     msi_free( szwName );
1991     msi_free( szwValue );
1992
1993     return r;
1994 }
1995
1996 void msi_reset_folders( MSIPACKAGE *package, BOOL source )
1997 {
1998     MSIFOLDER *folder;
1999
2000     LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
2001     {
2002         if ( source )
2003         {
2004             msi_free( folder->ResolvedSource );
2005             folder->ResolvedSource = NULL;
2006         }
2007         else
2008         {
2009             msi_free( folder->ResolvedTarget );
2010             folder->ResolvedTarget = NULL;
2011         }
2012     }
2013 }
2014
2015 UINT msi_set_property( MSIDATABASE *db, LPCWSTR szName, LPCWSTR szValue )
2016 {
2017     static const WCHAR insert_query[] = {
2018         'I','N','S','E','R','T',' ','I','N','T','O',' ',
2019         '`','_','P','r','o','p','e','r','t','y','`',' ',
2020         '(','`','_','P','r','o','p','e','r','t','y','`',',','`','V','a','l','u','e','`',')',' ',
2021         'V','A','L','U','E','S',' ','(','?',',','?',')',0};
2022     static const WCHAR update_query[] = {
2023         'U','P','D','A','T','E',' ','`','_','P','r','o','p','e','r','t','y','`',' ',
2024         'S','E','T',' ','`','V','a','l','u','e','`',' ','=',' ','?',' ','W','H','E','R','E',' ',
2025         '`','_','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
2026     static const WCHAR delete_query[] = {
2027         'D','E','L','E','T','E',' ','F','R','O','M',' ',
2028         '`','_','P','r','o','p','e','r','t','y','`',' ','W','H','E','R','E',' ',
2029         '`','_','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
2030     MSIQUERY *view;
2031     MSIRECORD *row = NULL;
2032     DWORD sz = 0;
2033     WCHAR query[1024];
2034     UINT rc;
2035
2036     TRACE("%p %s %s\n", db, debugstr_w(szName), debugstr_w(szValue));
2037
2038     if (!szName)
2039         return ERROR_INVALID_PARAMETER;
2040
2041     /* this one is weird... */
2042     if (!szName[0])
2043         return szValue ? ERROR_FUNCTION_FAILED : ERROR_SUCCESS;
2044
2045     rc = msi_get_property(db, szName, 0, &sz);
2046     if (!szValue || !*szValue)
2047     {
2048         sprintfW(query, delete_query, szName);
2049     }
2050     else if (rc == ERROR_MORE_DATA || rc == ERROR_SUCCESS)
2051     {
2052         sprintfW(query, update_query, szName);
2053
2054         row = MSI_CreateRecord(1);
2055         MSI_RecordSetStringW(row, 1, szValue);
2056     }
2057     else
2058     {
2059         strcpyW(query, insert_query);
2060
2061         row = MSI_CreateRecord(2);
2062         MSI_RecordSetStringW(row, 1, szName);
2063         MSI_RecordSetStringW(row, 2, szValue);
2064     }
2065
2066     rc = MSI_DatabaseOpenViewW(db, query, &view);
2067     if (rc == ERROR_SUCCESS)
2068     {
2069         rc = MSI_ViewExecute(view, row);
2070         MSI_ViewClose(view);
2071         msiobj_release(&view->hdr);
2072     }
2073     if (row) msiobj_release(&row->hdr);
2074     return rc;
2075 }
2076
2077 UINT WINAPI MsiSetPropertyW( MSIHANDLE hInstall, LPCWSTR szName, LPCWSTR szValue)
2078 {
2079     MSIPACKAGE *package;
2080     UINT ret;
2081
2082     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
2083     if( !package )
2084     {
2085         HRESULT hr;
2086         BSTR name = NULL, value = NULL;
2087         IWineMsiRemotePackage *remote_package;
2088
2089         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
2090         if (!remote_package)
2091             return ERROR_INVALID_HANDLE;
2092
2093         name = SysAllocString( szName );
2094         value = SysAllocString( szValue );
2095         if ((!name && szName) || (!value && szValue))
2096         {
2097             SysFreeString( name );
2098             SysFreeString( value );
2099             IWineMsiRemotePackage_Release( remote_package );
2100             return ERROR_OUTOFMEMORY;
2101         }
2102
2103         hr = IWineMsiRemotePackage_SetProperty( remote_package, name, value );
2104
2105         SysFreeString( name );
2106         SysFreeString( value );
2107         IWineMsiRemotePackage_Release( remote_package );
2108
2109         if (FAILED(hr))
2110         {
2111             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
2112                 return HRESULT_CODE(hr);
2113
2114             return ERROR_FUNCTION_FAILED;
2115         }
2116
2117         return ERROR_SUCCESS;
2118     }
2119
2120     ret = msi_set_property( package->db, szName, szValue );
2121     if (ret == ERROR_SUCCESS && !strcmpW( szName, szSourceDir ))
2122         msi_reset_folders( package, TRUE );
2123
2124     msiobj_release( &package->hdr );
2125     return ret;
2126 }
2127
2128 static MSIRECORD *msi_get_property_row( MSIDATABASE *db, LPCWSTR name )
2129 {
2130     static const WCHAR query[]= {
2131         'S','E','L','E','C','T',' ','`','V','a','l','u','e','`',' ',
2132         'F','R','O','M',' ' ,'`','_','P','r','o','p','e','r','t','y','`',' ',
2133         'W','H','E','R','E',' ' ,'`','_','P','r','o','p','e','r','t','y','`','=','?',0};
2134     MSIRECORD *rec, *row = NULL;
2135     MSIQUERY *view;
2136     UINT r;
2137
2138     if (!name || !*name)
2139         return NULL;
2140
2141     rec = MSI_CreateRecord(1);
2142     if (!rec)
2143         return NULL;
2144
2145     MSI_RecordSetStringW(rec, 1, name);
2146
2147     r = MSI_DatabaseOpenViewW(db, query, &view);
2148     if (r == ERROR_SUCCESS)
2149     {
2150         MSI_ViewExecute(view, rec);
2151         MSI_ViewFetch(view, &row);
2152         MSI_ViewClose(view);
2153         msiobj_release(&view->hdr);
2154     }
2155     msiobj_release(&rec->hdr);
2156     return row;
2157 }
2158
2159 /* internal function, not compatible with MsiGetPropertyW */
2160 UINT msi_get_property( MSIDATABASE *db, LPCWSTR szName,
2161                        LPWSTR szValueBuf, LPDWORD pchValueBuf )
2162 {
2163     MSIRECORD *row;
2164     UINT rc = ERROR_FUNCTION_FAILED;
2165
2166     row = msi_get_property_row( db, szName );
2167
2168     if (*pchValueBuf > 0)
2169         szValueBuf[0] = 0;
2170
2171     if (row)
2172     {
2173         rc = MSI_RecordGetStringW(row, 1, szValueBuf, pchValueBuf);
2174         msiobj_release(&row->hdr);
2175     }
2176
2177     if (rc == ERROR_SUCCESS)
2178         TRACE("returning %s for property %s\n", debugstr_w(szValueBuf),
2179             debugstr_w(szName));
2180     else if (rc == ERROR_MORE_DATA)
2181         TRACE("need %d sized buffer for %s\n", *pchValueBuf,
2182             debugstr_w(szName));
2183     else
2184     {
2185         *pchValueBuf = 0;
2186         TRACE("property %s not found\n", debugstr_w(szName));
2187     }
2188
2189     return rc;
2190 }
2191
2192 LPWSTR msi_dup_property(MSIDATABASE *db, LPCWSTR prop)
2193 {
2194     DWORD sz = 0;
2195     LPWSTR str;
2196     UINT r;
2197
2198     r = msi_get_property(db, prop, NULL, &sz);
2199     if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
2200         return NULL;
2201
2202     sz++;
2203     str = msi_alloc(sz * sizeof(WCHAR));
2204     r = msi_get_property(db, prop, str, &sz);
2205     if (r != ERROR_SUCCESS)
2206     {
2207         msi_free(str);
2208         str = NULL;
2209     }
2210
2211     return str;
2212 }
2213
2214 int msi_get_property_int( MSIDATABASE *db, LPCWSTR prop, int def )
2215 {
2216     LPWSTR str = msi_dup_property( db, prop );
2217     int val = str ? atoiW(str) : def;
2218     msi_free(str);
2219     return val;
2220 }
2221
2222 static UINT MSI_GetProperty( MSIHANDLE handle, LPCWSTR name,
2223                              awstring *szValueBuf, LPDWORD pchValueBuf )
2224 {
2225     MSIPACKAGE *package;
2226     MSIRECORD *row = NULL;
2227     UINT r = ERROR_FUNCTION_FAILED;
2228     LPCWSTR val = NULL;
2229
2230     TRACE("%u %s %p %p\n", handle, debugstr_w(name),
2231           szValueBuf->str.w, pchValueBuf );
2232
2233     if (!name)
2234         return ERROR_INVALID_PARAMETER;
2235
2236     package = msihandle2msiinfo( handle, MSIHANDLETYPE_PACKAGE );
2237     if (!package)
2238     {
2239         HRESULT hr;
2240         IWineMsiRemotePackage *remote_package;
2241         LPWSTR value = NULL;
2242         BSTR bname;
2243         DWORD len;
2244
2245         remote_package = (IWineMsiRemotePackage *)msi_get_remote( handle );
2246         if (!remote_package)
2247             return ERROR_INVALID_HANDLE;
2248
2249         bname = SysAllocString( name );
2250         if (!bname)
2251         {
2252             IWineMsiRemotePackage_Release( remote_package );
2253             return ERROR_OUTOFMEMORY;
2254         }
2255
2256         len = 0;
2257         hr = IWineMsiRemotePackage_GetProperty( remote_package, bname, NULL, &len );
2258         if (FAILED(hr))
2259             goto done;
2260
2261         len++;
2262         value = msi_alloc(len * sizeof(WCHAR));
2263         if (!value)
2264         {
2265             r = ERROR_OUTOFMEMORY;
2266             goto done;
2267         }
2268
2269         hr = IWineMsiRemotePackage_GetProperty( remote_package, bname, value, &len );
2270         if (FAILED(hr))
2271             goto done;
2272
2273         r = msi_strcpy_to_awstring( value, szValueBuf, pchValueBuf );
2274
2275         /* Bug required by Adobe installers */
2276         if (!szValueBuf->unicode && !szValueBuf->str.a)
2277             *pchValueBuf *= sizeof(WCHAR);
2278
2279 done:
2280         IWineMsiRemotePackage_Release(remote_package);
2281         SysFreeString(bname);
2282         msi_free(value);
2283
2284         if (FAILED(hr))
2285         {
2286             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
2287                 return HRESULT_CODE(hr);
2288
2289             return ERROR_FUNCTION_FAILED;
2290         }
2291
2292         return r;
2293     }
2294
2295     row = msi_get_property_row( package->db, name );
2296     if (row)
2297         val = MSI_RecordGetString( row, 1 );
2298
2299     if (!val)
2300         val = szEmpty;
2301
2302     r = msi_strcpy_to_awstring( val, szValueBuf, pchValueBuf );
2303
2304     if (row)
2305         msiobj_release( &row->hdr );
2306     msiobj_release( &package->hdr );
2307
2308     return r;
2309 }
2310
2311 UINT WINAPI MsiGetPropertyA( MSIHANDLE hInstall, LPCSTR szName,
2312                              LPSTR szValueBuf, LPDWORD pchValueBuf )
2313 {
2314     awstring val;
2315     LPWSTR name;
2316     UINT r;
2317
2318     val.unicode = FALSE;
2319     val.str.a = szValueBuf;
2320
2321     name = strdupAtoW( szName );
2322     if (szName && !name)
2323         return ERROR_OUTOFMEMORY;
2324
2325     r = MSI_GetProperty( hInstall, name, &val, pchValueBuf );
2326     msi_free( name );
2327     return r;
2328 }
2329
2330 UINT WINAPI MsiGetPropertyW( MSIHANDLE hInstall, LPCWSTR szName,
2331                              LPWSTR szValueBuf, LPDWORD pchValueBuf )
2332 {
2333     awstring val;
2334
2335     val.unicode = TRUE;
2336     val.str.w = szValueBuf;
2337
2338     return MSI_GetProperty( hInstall, szName, &val, pchValueBuf );
2339 }
2340
2341 typedef struct _msi_remote_package_impl {
2342     IWineMsiRemotePackage IWineMsiRemotePackage_iface;
2343     MSIHANDLE package;
2344     LONG refs;
2345 } msi_remote_package_impl;
2346
2347 static inline msi_remote_package_impl *impl_from_IWineMsiRemotePackage( IWineMsiRemotePackage *iface )
2348 {
2349     return CONTAINING_RECORD(iface, msi_remote_package_impl, IWineMsiRemotePackage_iface);
2350 }
2351
2352 static HRESULT WINAPI mrp_QueryInterface( IWineMsiRemotePackage *iface,
2353                 REFIID riid,LPVOID *ppobj)
2354 {
2355     if( IsEqualCLSID( riid, &IID_IUnknown ) ||
2356         IsEqualCLSID( riid, &IID_IWineMsiRemotePackage ) )
2357     {
2358         IUnknown_AddRef( iface );
2359         *ppobj = iface;
2360         return S_OK;
2361     }
2362
2363     return E_NOINTERFACE;
2364 }
2365
2366 static ULONG WINAPI mrp_AddRef( IWineMsiRemotePackage *iface )
2367 {
2368     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2369
2370     return InterlockedIncrement( &This->refs );
2371 }
2372
2373 static ULONG WINAPI mrp_Release( IWineMsiRemotePackage *iface )
2374 {
2375     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2376     ULONG r;
2377
2378     r = InterlockedDecrement( &This->refs );
2379     if (r == 0)
2380     {
2381         MsiCloseHandle( This->package );
2382         msi_free( This );
2383     }
2384     return r;
2385 }
2386
2387 static HRESULT WINAPI mrp_SetMsiHandle( IWineMsiRemotePackage *iface, MSIHANDLE handle )
2388 {
2389     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2390     This->package = handle;
2391     return S_OK;
2392 }
2393
2394 static HRESULT WINAPI mrp_GetActiveDatabase( IWineMsiRemotePackage *iface, MSIHANDLE *handle )
2395 {
2396     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2397     IWineMsiRemoteDatabase *rdb = NULL;
2398     HRESULT hr;
2399     MSIHANDLE hdb;
2400
2401     hr = create_msi_remote_database( NULL, (LPVOID *)&rdb );
2402     if (FAILED(hr) || !rdb)
2403     {
2404         ERR("Failed to create remote database\n");
2405         return hr;
2406     }
2407
2408     hdb = MsiGetActiveDatabase(This->package);
2409
2410     hr = IWineMsiRemoteDatabase_SetMsiHandle( rdb, hdb );
2411     if (FAILED(hr))
2412     {
2413         ERR("Failed to set the database handle\n");
2414         return hr;
2415     }
2416
2417     *handle = alloc_msi_remote_handle( (IUnknown *)rdb );
2418     return S_OK;
2419 }
2420
2421 static HRESULT WINAPI mrp_GetProperty( IWineMsiRemotePackage *iface, BSTR property, BSTR value, DWORD *size )
2422 {
2423     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2424     UINT r = MsiGetPropertyW(This->package, property, value, size);
2425     if (r != ERROR_SUCCESS) return HRESULT_FROM_WIN32(r);
2426     return S_OK;
2427 }
2428
2429 static HRESULT WINAPI mrp_SetProperty( IWineMsiRemotePackage *iface, BSTR property, BSTR value )
2430 {
2431     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2432     UINT r = MsiSetPropertyW(This->package, property, value);
2433     return HRESULT_FROM_WIN32(r);
2434 }
2435
2436 static HRESULT WINAPI mrp_ProcessMessage( IWineMsiRemotePackage *iface, INSTALLMESSAGE message, MSIHANDLE record )
2437 {
2438     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2439     UINT r = MsiProcessMessage(This->package, message, record);
2440     return HRESULT_FROM_WIN32(r);
2441 }
2442
2443 static HRESULT WINAPI mrp_DoAction( IWineMsiRemotePackage *iface, BSTR action )
2444 {
2445     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2446     UINT r = MsiDoActionW(This->package, action);
2447     return HRESULT_FROM_WIN32(r);
2448 }
2449
2450 static HRESULT WINAPI mrp_Sequence( IWineMsiRemotePackage *iface, BSTR table, int sequence )
2451 {
2452     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2453     UINT r = MsiSequenceW(This->package, table, sequence);
2454     return HRESULT_FROM_WIN32(r);
2455 }
2456
2457 static HRESULT WINAPI mrp_GetTargetPath( IWineMsiRemotePackage *iface, BSTR folder, BSTR value, DWORD *size )
2458 {
2459     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2460     UINT r = MsiGetTargetPathW(This->package, folder, value, size);
2461     return HRESULT_FROM_WIN32(r);
2462 }
2463
2464 static HRESULT WINAPI mrp_SetTargetPath( IWineMsiRemotePackage *iface, BSTR folder, BSTR value)
2465 {
2466     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2467     UINT r = MsiSetTargetPathW(This->package, folder, value);
2468     return HRESULT_FROM_WIN32(r);
2469 }
2470
2471 static HRESULT WINAPI mrp_GetSourcePath( IWineMsiRemotePackage *iface, BSTR folder, BSTR value, DWORD *size )
2472 {
2473     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2474     UINT r = MsiGetSourcePathW(This->package, folder, value, size);
2475     return HRESULT_FROM_WIN32(r);
2476 }
2477
2478 static HRESULT WINAPI mrp_GetMode( IWineMsiRemotePackage *iface, MSIRUNMODE mode, BOOL *ret )
2479 {
2480     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2481     *ret = MsiGetMode(This->package, mode);
2482     return S_OK;
2483 }
2484
2485 static HRESULT WINAPI mrp_SetMode( IWineMsiRemotePackage *iface, MSIRUNMODE mode, BOOL state )
2486 {
2487     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2488     UINT r = MsiSetMode(This->package, mode, state);
2489     return HRESULT_FROM_WIN32(r);
2490 }
2491
2492 static HRESULT WINAPI mrp_GetFeatureState( IWineMsiRemotePackage *iface, BSTR feature,
2493                                     INSTALLSTATE *installed, INSTALLSTATE *action )
2494 {
2495     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2496     UINT r = MsiGetFeatureStateW(This->package, feature, installed, action);
2497     return HRESULT_FROM_WIN32(r);
2498 }
2499
2500 static HRESULT WINAPI mrp_SetFeatureState( IWineMsiRemotePackage *iface, BSTR feature, INSTALLSTATE state )
2501 {
2502     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2503     UINT r = MsiSetFeatureStateW(This->package, feature, state);
2504     return HRESULT_FROM_WIN32(r);
2505 }
2506
2507 static HRESULT WINAPI mrp_GetComponentState( IWineMsiRemotePackage *iface, BSTR component,
2508                                       INSTALLSTATE *installed, INSTALLSTATE *action )
2509 {
2510     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2511     UINT r = MsiGetComponentStateW(This->package, component, installed, action);
2512     return HRESULT_FROM_WIN32(r);
2513 }
2514
2515 static HRESULT WINAPI mrp_SetComponentState( IWineMsiRemotePackage *iface, BSTR component, INSTALLSTATE state )
2516 {
2517     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2518     UINT r = MsiSetComponentStateW(This->package, component, state);
2519     return HRESULT_FROM_WIN32(r);
2520 }
2521
2522 static HRESULT WINAPI mrp_GetLanguage( IWineMsiRemotePackage *iface, LANGID *language )
2523 {
2524     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2525     *language = MsiGetLanguage(This->package);
2526     return S_OK;
2527 }
2528
2529 static HRESULT WINAPI mrp_SetInstallLevel( IWineMsiRemotePackage *iface, int level )
2530 {
2531     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2532     UINT r = MsiSetInstallLevel(This->package, level);
2533     return HRESULT_FROM_WIN32(r);
2534 }
2535
2536 static HRESULT WINAPI mrp_FormatRecord( IWineMsiRemotePackage *iface, MSIHANDLE record,
2537                                         BSTR *value)
2538 {
2539     DWORD size = 0;
2540     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2541     UINT r = MsiFormatRecordW(This->package, record, NULL, &size);
2542     if (r == ERROR_SUCCESS)
2543     {
2544         *value = SysAllocStringLen(NULL, size);
2545         if (!*value)
2546             return E_OUTOFMEMORY;
2547         size++;
2548         r = MsiFormatRecordW(This->package, record, *value, &size);
2549     }
2550     return HRESULT_FROM_WIN32(r);
2551 }
2552
2553 static HRESULT WINAPI mrp_EvaluateCondition( IWineMsiRemotePackage *iface, BSTR condition )
2554 {
2555     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2556     UINT r = MsiEvaluateConditionW(This->package, condition);
2557     return HRESULT_FROM_WIN32(r);
2558 }
2559
2560 static HRESULT WINAPI mrp_GetFeatureCost( IWineMsiRemotePackage *iface, BSTR feature,
2561                                           INT cost_tree, INSTALLSTATE state, INT *cost )
2562 {
2563     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2564     UINT r = MsiGetFeatureCostW(This->package, feature, cost_tree, state, cost);
2565     return HRESULT_FROM_WIN32(r);
2566 }
2567
2568 static HRESULT WINAPI mrp_EnumComponentCosts( IWineMsiRemotePackage *iface, BSTR component,
2569                                               DWORD index, INSTALLSTATE state, BSTR drive,
2570                                               DWORD *buflen, INT *cost, INT *temp )
2571 {
2572     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2573     UINT r = MsiEnumComponentCostsW(This->package, component, index, state, drive, buflen, cost, temp);
2574     return HRESULT_FROM_WIN32(r);
2575 }
2576
2577 static const IWineMsiRemotePackageVtbl msi_remote_package_vtbl =
2578 {
2579     mrp_QueryInterface,
2580     mrp_AddRef,
2581     mrp_Release,
2582     mrp_SetMsiHandle,
2583     mrp_GetActiveDatabase,
2584     mrp_GetProperty,
2585     mrp_SetProperty,
2586     mrp_ProcessMessage,
2587     mrp_DoAction,
2588     mrp_Sequence,
2589     mrp_GetTargetPath,
2590     mrp_SetTargetPath,
2591     mrp_GetSourcePath,
2592     mrp_GetMode,
2593     mrp_SetMode,
2594     mrp_GetFeatureState,
2595     mrp_SetFeatureState,
2596     mrp_GetComponentState,
2597     mrp_SetComponentState,
2598     mrp_GetLanguage,
2599     mrp_SetInstallLevel,
2600     mrp_FormatRecord,
2601     mrp_EvaluateCondition,
2602     mrp_GetFeatureCost,
2603     mrp_EnumComponentCosts
2604 };
2605
2606 HRESULT create_msi_remote_package( IUnknown *pOuter, LPVOID *ppObj )
2607 {
2608     msi_remote_package_impl* This;
2609
2610     This = msi_alloc( sizeof *This );
2611     if (!This)
2612         return E_OUTOFMEMORY;
2613
2614     This->IWineMsiRemotePackage_iface.lpVtbl = &msi_remote_package_vtbl;
2615     This->package = 0;
2616     This->refs = 1;
2617
2618     *ppObj = This;
2619
2620     return S_OK;
2621 }
2622
2623 UINT msi_package_add_info(MSIPACKAGE *package, DWORD context, DWORD options,
2624                           LPCWSTR property, LPWSTR value)
2625 {
2626     MSISOURCELISTINFO *info;
2627
2628     LIST_FOR_EACH_ENTRY( info, &package->sourcelist_info, MSISOURCELISTINFO, entry )
2629     {
2630         if (!strcmpW( info->value, value )) return ERROR_SUCCESS;
2631     }
2632
2633     info = msi_alloc(sizeof(MSISOURCELISTINFO));
2634     if (!info)
2635         return ERROR_OUTOFMEMORY;
2636
2637     info->context = context;
2638     info->options = options;
2639     info->property = property;
2640     info->value = strdupW(value);
2641     list_add_head(&package->sourcelist_info, &info->entry);
2642
2643     return ERROR_SUCCESS;
2644 }
2645
2646 UINT msi_package_add_media_disk(MSIPACKAGE *package, DWORD context, DWORD options,
2647                                 DWORD disk_id, LPWSTR volume_label, LPWSTR disk_prompt)
2648 {
2649     MSIMEDIADISK *disk;
2650
2651     LIST_FOR_EACH_ENTRY( disk, &package->sourcelist_media, MSIMEDIADISK, entry )
2652     {
2653         if (disk->disk_id == disk_id) return ERROR_SUCCESS;
2654     }
2655
2656     disk = msi_alloc(sizeof(MSIMEDIADISK));
2657     if (!disk)
2658         return ERROR_OUTOFMEMORY;
2659
2660     disk->context = context;
2661     disk->options = options;
2662     disk->disk_id = disk_id;
2663     disk->volume_label = strdupW(volume_label);
2664     disk->disk_prompt = strdupW(disk_prompt);
2665     list_add_head(&package->sourcelist_media, &disk->entry);
2666
2667     return ERROR_SUCCESS;
2668 }