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