Fix gcc 4.0 -Wpointer-sign warnings.
[wine] / dlls / msi / helpers.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2005 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 /*
22  * Here are helper functions formally in action.c that are used by a variaty of
23  * actions and functions.
24  */
25
26 #include <stdarg.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "wine/debug.h"
32 #include "msipriv.h"
33 #include "winuser.h"
34 #include "wine/unicode.h"
35 #include "action.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(msi);
38
39 static const WCHAR cszTargetDir[] = {'T','A','R','G','E','T','D','I','R',0};
40 static const WCHAR cszDatabase[]={'D','A','T','A','B','A','S','E',0};
41
42 const WCHAR cszSourceDir[] = {'S','o','u','r','c','e','D','i','r',0};
43 const WCHAR szProductCode[]= {'P','r','o','d','u','c','t','C','o','d','e',0};
44 const WCHAR cszRootDrive[] = {'R','O','O','T','D','R','I','V','E',0};
45 const WCHAR cszbs[]={'\\',0};
46
47 DWORD build_version_dword(LPCWSTR version_string)
48 {
49     SHORT major,minor;
50     WORD build;
51     DWORD rc = 0x00000000;
52     LPCWSTR ptr1;
53
54     ptr1 = version_string;
55
56     if (!ptr1)
57         return rc;
58     else
59         major = atoiW(ptr1);
60
61
62     if(ptr1)
63         ptr1 = strchrW(ptr1,'.');
64     if (ptr1)
65     {
66         ptr1++;
67         minor = atoiW(ptr1);
68     }
69     else
70         minor = 0;
71
72     if (ptr1)
73         ptr1 = strchrW(ptr1,'.');
74
75     if (ptr1)
76     {
77         ptr1++;
78         build = atoiW(ptr1);
79     }
80     else
81         build = 0;
82
83     rc = MAKELONG(build,MAKEWORD(minor,major));
84     TRACE("%s -> 0x%lx\n",debugstr_w(version_string),rc);
85     return rc;
86 }
87
88 UINT build_icon_path(MSIPACKAGE *package, LPCWSTR icon_name, 
89                             LPWSTR *FilePath)
90 {
91     LPWSTR ProductCode;
92     LPWSTR SystemFolder;
93     LPWSTR dest;
94     UINT rc;
95
96     static const WCHAR szInstaller[] = 
97         {'M','i','c','r','o','s','o','f','t','\\',
98          'I','n','s','t','a','l','l','e','r','\\',0};
99     static const WCHAR szFolder[] =
100         {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
101
102     ProductCode = load_dynamic_property(package,szProductCode,&rc);
103     if (!ProductCode)
104         return rc;
105
106     SystemFolder = load_dynamic_property(package,szFolder,NULL);
107
108     dest = build_directory_name(3, SystemFolder, szInstaller, ProductCode);
109
110     create_full_pathW(dest);
111
112     *FilePath = build_directory_name(2, dest, icon_name);
113
114     HeapFree(GetProcessHeap(),0,SystemFolder);
115     HeapFree(GetProcessHeap(),0,ProductCode);
116     HeapFree(GetProcessHeap(),0,dest);
117     return ERROR_SUCCESS;
118 }
119
120 WCHAR *load_dynamic_stringW(MSIRECORD *row, INT index)
121 {
122     UINT rc;
123     DWORD sz;
124     LPWSTR ret;
125    
126     sz = 0; 
127     if (MSI_RecordIsNull(row,index))
128         return NULL;
129
130     rc = MSI_RecordGetStringW(row,index,NULL,&sz);
131
132     /* having an empty string is different than NULL */
133     if (sz == 0)
134     {
135         ret = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR));
136         ret[0] = 0;
137         return ret;
138     }
139
140     sz ++;
141     ret = HeapAlloc(GetProcessHeap(),0,sz * sizeof (WCHAR));
142     rc = MSI_RecordGetStringW(row,index,ret,&sz);
143     if (rc!=ERROR_SUCCESS)
144     {
145         ERR("Unable to load dynamic string\n");
146         HeapFree(GetProcessHeap(), 0, ret);
147         ret = NULL;
148     }
149     return ret;
150 }
151
152 LPWSTR load_dynamic_property(MSIPACKAGE *package, LPCWSTR prop, UINT* rc)
153 {
154     DWORD sz = 0;
155     LPWSTR str;
156     UINT r;
157
158     r = MSI_GetPropertyW(package, prop, NULL, &sz);
159     if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
160     {
161         if (rc)
162             *rc = r;
163         return NULL;
164     }
165     sz++;
166     str = HeapAlloc(GetProcessHeap(),0,sz*sizeof(WCHAR));
167     r = MSI_GetPropertyW(package, prop, str, &sz);
168     if (r != ERROR_SUCCESS)
169     {
170         HeapFree(GetProcessHeap(),0,str);
171         str = NULL;
172     }
173     if (rc)
174         *rc = r;
175     return str;
176 }
177
178 int get_loaded_component(MSIPACKAGE* package, LPCWSTR Component )
179 {
180     int rc = -1;
181     DWORD i;
182
183     for (i = 0; i < package->loaded_components; i++)
184     {
185         if (strcmpW(Component,package->components[i].Component)==0)
186         {
187             rc = i;
188             break;
189         }
190     }
191     return rc;
192 }
193
194 int get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
195 {
196     int rc = -1;
197     DWORD i;
198
199     for (i = 0; i < package->loaded_features; i++)
200     {
201         if (strcmpW(Feature,package->features[i].Feature)==0)
202         {
203             rc = i;
204             break;
205         }
206     }
207     return rc;
208 }
209
210 int get_loaded_file(MSIPACKAGE* package, LPCWSTR file)
211 {
212     int rc = -1;
213     DWORD i;
214
215     for (i = 0; i < package->loaded_files; i++)
216     {
217         if (strcmpW(file,package->files[i].File)==0)
218         {
219             rc = i;
220             break;
221         }
222     }
223     return rc;
224 }
225
226 int track_tempfile(MSIPACKAGE *package, LPCWSTR name, LPCWSTR path)
227 {
228     DWORD i;
229     DWORD index;
230
231     if (!package)
232         return -2;
233
234     for (i=0; i < package->loaded_files; i++)
235         if (strcmpW(package->files[i].File,name)==0)
236             return -1;
237
238     index = package->loaded_files;
239     package->loaded_files++;
240     if (package->loaded_files== 1)
241         package->files = HeapAlloc(GetProcessHeap(),0,sizeof(MSIFILE));
242     else
243         package->files = HeapReAlloc(GetProcessHeap(),0,
244             package->files , package->loaded_files * sizeof(MSIFILE));
245
246     memset(&package->files[index],0,sizeof(MSIFILE));
247
248     package->files[index].File = strdupW(name);
249     package->files[index].TargetPath = strdupW(path);
250     package->files[index].Temporary = TRUE;
251
252     TRACE("Tracking tempfile (%s)\n",debugstr_w(package->files[index].File));  
253
254     return 0;
255 }
256
257 LPWSTR resolve_folder(MSIPACKAGE *package, LPCWSTR name, BOOL source, 
258                       BOOL set_prop, MSIFOLDER **folder)
259 {
260     DWORD i;
261     LPWSTR p, path = NULL;
262
263     TRACE("Working to resolve %s\n",debugstr_w(name));
264
265     if (!name)
266         return NULL;
267
268     /* special resolving for Target and Source root dir */
269     if (strcmpW(name,cszTargetDir)==0 || strcmpW(name,cszSourceDir)==0)
270     {
271         if (!source)
272         {
273             LPWSTR check_path;
274             check_path = load_dynamic_property(package,cszTargetDir,NULL);
275             if (!check_path)
276             {
277                 check_path = load_dynamic_property(package,cszRootDrive,NULL);
278                 if (set_prop)
279                     MSI_SetPropertyW(package,cszTargetDir,check_path);
280             }
281
282             /* correct misbuilt target dir */
283             path = build_directory_name(2, check_path, NULL);
284             if (strcmpiW(path,check_path)!=0)
285                 MSI_SetPropertyW(package,cszTargetDir,path);
286
287             if (folder)
288             {
289                 for (i = 0; i < package->loaded_folders; i++)
290                 {
291                     if (strcmpW(package->folders[i].Directory,name)==0)
292                         break;
293                 }
294                 *folder = &(package->folders[i]);
295             }
296             return path;
297         }
298         else
299         {
300             path = load_dynamic_property(package,cszSourceDir,NULL);
301             if (!path)
302             {
303                 path = load_dynamic_property(package,cszDatabase,NULL);
304                 if (path)
305                 {
306                     p = strrchrW(path,'\\');
307                     if (p)
308                         *(p+1) = 0;
309                 }
310             }
311             if (folder)
312             {
313                 for (i = 0; i < package->loaded_folders; i++)
314                 {
315                     if (strcmpW(package->folders[i].Directory,name)==0)
316                         break;
317                 }
318                 *folder = &(package->folders[i]);
319             }
320             return path;
321         }
322     }
323
324     for (i = 0; i < package->loaded_folders; i++)
325     {
326         if (strcmpW(package->folders[i].Directory,name)==0)
327             break;
328     }
329
330     if (i >= package->loaded_folders)
331         return NULL;
332
333     if (folder)
334         *folder = &(package->folders[i]);
335
336     if (!source && package->folders[i].ResolvedTarget)
337     {
338         path = strdupW(package->folders[i].ResolvedTarget);
339         TRACE("   already resolved to %s\n",debugstr_w(path));
340         return path;
341     }
342     else if (source && package->folders[i].ResolvedSource)
343     {
344         path = strdupW(package->folders[i].ResolvedSource);
345         TRACE("   (source)already resolved to %s\n",debugstr_w(path));
346         return path;
347     }
348     else if (!source && package->folders[i].Property)
349     {
350         path = build_directory_name(2, package->folders[i].Property, NULL);
351                     
352         TRACE("   internally set to %s\n",debugstr_w(path));
353         if (set_prop)
354             MSI_SetPropertyW(package,name,path);
355         return path;
356     }
357
358     if (package->folders[i].ParentIndex >= 0)
359     {
360         LPWSTR parent = package->folders[package->folders[i].ParentIndex].Directory;
361
362         TRACE(" ! Parent is %s\n", debugstr_w(parent));
363
364         p = resolve_folder(package, parent, source, set_prop, NULL);
365         if (!source)
366         {
367             TRACE("   TargetDefault = %s\n",
368                     debugstr_w(package->folders[i].TargetDefault));
369
370             path = build_directory_name(3, p, 
371                             package->folders[i].TargetDefault, NULL);
372             package->folders[i].ResolvedTarget = strdupW(path);
373             TRACE("   resolved into %s\n",debugstr_w(path));
374             if (set_prop)
375                 MSI_SetPropertyW(package,name,path);
376         }
377         else 
378         {
379             if (package->folders[i].SourceDefault && 
380                 package->folders[i].SourceDefault[0]!='.')
381                 path = build_directory_name(3, p, package->folders[i].SourceDefault, NULL);
382             else
383                 path = strdupW(p);
384             TRACE("   (source)resolved into %s\n",debugstr_w(path));
385             package->folders[i].ResolvedSource = strdupW(path);
386         }
387         HeapFree(GetProcessHeap(),0,p);
388     }
389     return path;
390 }
391
392 /* wrapper to resist a need for a full rewrite right now */
393 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
394 {
395     if (ptr)
396     {
397         MSIRECORD *rec = MSI_CreateRecord(1);
398         DWORD size = 0;
399
400         MSI_RecordSetStringW(rec,0,ptr);
401         MSI_FormatRecordW(package,rec,NULL,&size);
402         if (size >= 0)
403         {
404             size++;
405             *data = HeapAlloc(GetProcessHeap(),0,size*sizeof(WCHAR));
406             if (size > 1)
407                 MSI_FormatRecordW(package,rec,*data,&size);
408             else
409                 *data[0] = 0;
410             msiobj_release( &rec->hdr );
411             return sizeof(WCHAR)*size;
412         }
413         msiobj_release( &rec->hdr );
414     }
415
416     *data = NULL;
417     return 0;
418 }
419
420 UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
421 {
422     UINT count;
423     LPWSTR *newbuf = NULL;
424     if (script >= TOTAL_SCRIPTS)
425     {
426         FIXME("Unknown script requested %i\n",script);
427         return ERROR_FUNCTION_FAILED;
428     }
429     TRACE("Scheduling Action %s in script %i\n",debugstr_w(action), script);
430     
431     count = package->script->ActionCount[script];
432     package->script->ActionCount[script]++;
433     if (count != 0)
434         newbuf = HeapReAlloc(GetProcessHeap(),0,
435                         package->script->Actions[script],
436                         package->script->ActionCount[script]* sizeof(LPWSTR));
437     else
438         newbuf = HeapAlloc(GetProcessHeap(),0, sizeof(LPWSTR));
439
440     newbuf[count] = strdupW(action);
441     package->script->Actions[script] = newbuf;
442
443    return ERROR_SUCCESS;
444 }
445
446 static void remove_tracked_tempfiles(MSIPACKAGE* package)
447 {
448     DWORD i;
449
450     if (!package)
451         return;
452
453     for (i = 0; i < package->loaded_files; i++)
454     {
455         if (package->files[i].Temporary)
456         {
457             TRACE("Cleaning up %s\n",debugstr_w(package->files[i].TargetPath));
458             DeleteFileW(package->files[i].TargetPath);
459         }
460
461     }
462 }
463
464 /* Called when the package is being closed */
465 void ACTION_free_package_structures( MSIPACKAGE* package)
466 {
467     INT i;
468     
469     TRACE("Freeing package action data\n");
470
471     remove_tracked_tempfiles(package);
472
473     /* No dynamic buffers in features */
474     if (package->features && package->loaded_features > 0)
475         HeapFree(GetProcessHeap(),0,package->features);
476
477     for (i = 0; i < package->loaded_folders; i++)
478     {
479         HeapFree(GetProcessHeap(),0,package->folders[i].Directory);
480         HeapFree(GetProcessHeap(),0,package->folders[i].TargetDefault);
481         HeapFree(GetProcessHeap(),0,package->folders[i].SourceDefault);
482         HeapFree(GetProcessHeap(),0,package->folders[i].ResolvedTarget);
483         HeapFree(GetProcessHeap(),0,package->folders[i].ResolvedSource);
484         HeapFree(GetProcessHeap(),0,package->folders[i].Property);
485     }
486     if (package->folders && package->loaded_folders > 0)
487         HeapFree(GetProcessHeap(),0,package->folders);
488
489     for (i = 0; i < package->loaded_components; i++)
490         HeapFree(GetProcessHeap(),0,package->components[i].FullKeypath);
491
492     if (package->components && package->loaded_components > 0)
493         HeapFree(GetProcessHeap(),0,package->components);
494
495     for (i = 0; i < package->loaded_files; i++)
496     {
497         HeapFree(GetProcessHeap(),0,package->files[i].File);
498         HeapFree(GetProcessHeap(),0,package->files[i].FileName);
499         HeapFree(GetProcessHeap(),0,package->files[i].ShortName);
500         HeapFree(GetProcessHeap(),0,package->files[i].Version);
501         HeapFree(GetProcessHeap(),0,package->files[i].Language);
502         HeapFree(GetProcessHeap(),0,package->files[i].SourcePath);
503         HeapFree(GetProcessHeap(),0,package->files[i].TargetPath);
504     }
505
506     if (package->files && package->loaded_files > 0)
507         HeapFree(GetProcessHeap(),0,package->files);
508
509     /* clean up extension, progid, class and verb structures */
510     for (i = 0; i < package->loaded_classes; i++)
511     {
512         HeapFree(GetProcessHeap(),0,package->classes[i].Description);
513         HeapFree(GetProcessHeap(),0,package->classes[i].FileTypeMask);
514         HeapFree(GetProcessHeap(),0,package->classes[i].IconPath);
515         HeapFree(GetProcessHeap(),0,package->classes[i].DefInprocHandler);
516         HeapFree(GetProcessHeap(),0,package->classes[i].DefInprocHandler32);
517         HeapFree(GetProcessHeap(),0,package->classes[i].Argument);
518         HeapFree(GetProcessHeap(),0,package->classes[i].ProgIDText);
519     }
520
521     if (package->classes && package->loaded_classes > 0)
522         HeapFree(GetProcessHeap(),0,package->classes);
523
524     for (i = 0; i < package->loaded_extensions; i++)
525     {
526         HeapFree(GetProcessHeap(),0,package->extensions[i].ProgIDText);
527     }
528
529     if (package->extensions && package->loaded_extensions > 0)
530         HeapFree(GetProcessHeap(),0,package->extensions);
531
532     for (i = 0; i < package->loaded_progids; i++)
533     {
534         HeapFree(GetProcessHeap(),0,package->progids[i].ProgID);
535         HeapFree(GetProcessHeap(),0,package->progids[i].Description);
536         HeapFree(GetProcessHeap(),0,package->progids[i].IconPath);
537     }
538
539     if (package->progids && package->loaded_progids > 0)
540         HeapFree(GetProcessHeap(),0,package->progids);
541
542     for (i = 0; i < package->loaded_verbs; i++)
543     {
544         HeapFree(GetProcessHeap(),0,package->verbs[i].Verb);
545         HeapFree(GetProcessHeap(),0,package->verbs[i].Command);
546         HeapFree(GetProcessHeap(),0,package->verbs[i].Argument);
547     }
548
549     if (package->verbs && package->loaded_verbs > 0)
550         HeapFree(GetProcessHeap(),0,package->verbs);
551
552     for (i = 0; i < package->loaded_mimes; i++)
553         HeapFree(GetProcessHeap(),0,package->mimes[i].ContentType);
554
555     if (package->mimes && package->loaded_mimes > 0)
556         HeapFree(GetProcessHeap(),0,package->mimes);
557
558     for (i = 0; i < package->loaded_appids; i++)
559     {
560         HeapFree(GetProcessHeap(),0,package->appids[i].RemoteServerName);
561         HeapFree(GetProcessHeap(),0,package->appids[i].LocalServer);
562         HeapFree(GetProcessHeap(),0,package->appids[i].ServiceParameters);
563         HeapFree(GetProcessHeap(),0,package->appids[i].DllSurrogate);
564     }
565
566     if (package->appids && package->loaded_appids > 0)
567         HeapFree(GetProcessHeap(),0,package->appids);
568
569     if (package->script)
570     {
571         for (i = 0; i < TOTAL_SCRIPTS; i++)
572         {
573             int j;
574             for (j = 0; j < package->script->ActionCount[i]; j++)
575                 HeapFree(GetProcessHeap(),0,package->script->Actions[i][j]);
576         
577             HeapFree(GetProcessHeap(),0,package->script->Actions[i]);
578         }
579
580         for (i = 0; i < package->script->UniqueActionsCount; i++)
581             HeapFree(GetProcessHeap(),0,package->script->UniqueActions[i]);
582
583         HeapFree(GetProcessHeap(),0,package->script->UniqueActions);
584         HeapFree(GetProcessHeap(),0,package->script);
585     }
586
587     HeapFree(GetProcessHeap(),0,package->PackagePath);
588     HeapFree(GetProcessHeap(),0,package->msiFilePath);
589
590     /* cleanup control event subscriptions */
591     ControlEvent_CleanupSubscriptions(package);
592 }
593
594 /*
595  *  build_directory_name()
596  *
597  *  This function is to save messing round with directory names
598  *  It handles adding backslashes between path segments, 
599  *   and can add \ at the end of the directory name if told to.
600  *
601  *  It takes a variable number of arguments.
602  *  It always allocates a new string for the result, so make sure
603  *   to free the return value when finished with it.
604  *
605  *  The first arg is the number of path segments that follow.
606  *  The arguments following count are a list of path segments.
607  *  A path segment may be NULL.
608  *
609  *  Path segments will be added with a \ separating them.
610  *  A \ will not be added after the last segment, however if the
611  *    last segment is NULL, then the last character will be a \
612  * 
613  */
614 LPWSTR build_directory_name(DWORD count, ...)
615 {
616     DWORD sz = 1, i;
617     LPWSTR dir;
618     va_list va;
619
620     va_start(va,count);
621     for(i=0; i<count; i++)
622     {
623         LPCWSTR str = va_arg(va,LPCWSTR);
624         if (str)
625             sz += strlenW(str) + 1;
626     }
627     va_end(va);
628
629     dir = HeapAlloc(GetProcessHeap(), 0, sz*sizeof(WCHAR));
630     dir[0]=0;
631
632     va_start(va,count);
633     for(i=0; i<count; i++)
634     {
635         LPCWSTR str = va_arg(va,LPCWSTR);
636         if (!str)
637             continue;
638         strcatW(dir, str);
639         if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
640             strcatW(dir, cszbs);
641     }
642     return dir;
643 }
644
645 /***********************************************************************
646  *            create_full_pathW
647  *
648  * Recursively create all directories in the path.
649  *
650  * shamelessly stolen from setupapi/queue.c
651  */
652 BOOL create_full_pathW(const WCHAR *path)
653 {
654     BOOL ret = TRUE;
655     int len;
656     WCHAR *new_path;
657
658     new_path = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1) *
659                                               sizeof(WCHAR));
660
661     strcpyW(new_path, path);
662
663     while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
664     new_path[len - 1] = 0;
665
666     while(!CreateDirectoryW(new_path, NULL))
667     {
668         WCHAR *slash;
669         DWORD last_error = GetLastError();
670         if(last_error == ERROR_ALREADY_EXISTS)
671             break;
672
673         if(last_error != ERROR_PATH_NOT_FOUND)
674         {
675             ret = FALSE;
676             break;
677         }
678
679         if(!(slash = strrchrW(new_path, '\\')))
680         {
681             ret = FALSE;
682             break;
683         }
684
685         len = slash - new_path;
686         new_path[len] = 0;
687         if(!create_full_pathW(new_path))
688         {
689             ret = FALSE;
690             break;
691         }
692         new_path[len] = '\\';
693     }
694
695     HeapFree(GetProcessHeap(), 0, new_path);
696     return ret;
697 }
698
699 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
700 {
701     MSIRECORD * row;
702
703     row = MSI_CreateRecord(4);
704     MSI_RecordSetInteger(row,1,a);
705     MSI_RecordSetInteger(row,2,b);
706     MSI_RecordSetInteger(row,3,c);
707     MSI_RecordSetInteger(row,4,d);
708     MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
709     msiobj_release(&row->hdr);
710
711     msi_dialog_check_messages(NULL);
712 }
713
714 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
715 {
716     static const WCHAR Query_t[] = 
717         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
718          '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
719          'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=', 
720          ' ','\'','%','s','\'',0};
721     WCHAR message[1024];
722     MSIRECORD * row = 0;
723     DWORD size;
724     static const WCHAR szActionData[] = 
725         {'A','c','t','i','o','n','D','a','t','a',0};
726
727     if (!package->LastAction || strcmpW(package->LastAction,action))
728     {
729         row = MSI_QueryGetRecord(package->db, Query_t, action);
730         if (!row)
731             return;
732
733         if (MSI_RecordIsNull(row,3))
734         {
735             msiobj_release(&row->hdr);
736             return;
737         }
738
739         /* update the cached actionformat */
740         HeapFree(GetProcessHeap(),0,package->ActionFormat);
741         package->ActionFormat = load_dynamic_stringW(row,3);
742
743         HeapFree(GetProcessHeap(),0,package->LastAction);
744         package->LastAction = strdupW(action);
745
746         msiobj_release(&row->hdr);
747     }
748
749     MSI_RecordSetStringW(record,0,package->ActionFormat);
750     size = 1024;
751     MSI_FormatRecordW(package,record,message,&size);
752
753     row = MSI_CreateRecord(1);
754     MSI_RecordSetStringW(row,1,message);
755  
756     MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
757
758     ControlEvent_FireSubscribedEvent(package,szActionData, row);
759
760     msiobj_release(&row->hdr);
761 }
762
763 BOOL ACTION_VerifyComponentForAction(MSIPACKAGE* package, INT index, 
764                                             INSTALLSTATE check )
765 {
766     if (package->components[index].Installed == check)
767         return FALSE;
768
769     if (package->components[index].ActionRequest == check)
770         return TRUE;
771     else
772         return FALSE;
773 }
774
775 BOOL ACTION_VerifyFeatureForAction(MSIPACKAGE* package, INT index, 
776                                             INSTALLSTATE check )
777 {
778     if (package->features[index].Installed == check)
779         return FALSE;
780
781     if (package->features[index].ActionRequest == check)
782         return TRUE;
783     else
784         return FALSE;
785 }
786
787 void reduce_to_longfilename(WCHAR* filename)
788 {
789     LPWSTR p = strchrW(filename,'|');
790     if (p)
791         memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
792 }
793
794 void reduce_to_shortfilename(WCHAR* filename)
795 {
796     LPWSTR p = strchrW(filename,'|');
797     if (p)
798         *p = 0;
799 }
800
801 LPWSTR create_component_advertise_string(MSIPACKAGE* package, 
802                 MSICOMPONENT* component, LPCWSTR feature)
803 {
804     LPWSTR productid=NULL;
805     GUID clsid;
806     WCHAR productid_85[21];
807     WCHAR component_85[21];
808     /*
809      * I have a fair bit of confusion as to when a < is used and when a > is
810      * used. I do not think i have it right...
811      *
812      * Ok it appears that the > is used if there is a guid for the compoenent
813      * and the < is used if not.
814      */
815     static WCHAR fmt1[] = {'%','s','%','s','<',0,0};
816     static WCHAR fmt2[] = {'%','s','%','s','>','%','s',0,0};
817     LPWSTR output = NULL;
818     DWORD sz = 0;
819
820     memset(productid_85,0,sizeof(productid_85));
821     memset(component_85,0,sizeof(component_85));
822
823     productid = load_dynamic_property(package,szProductCode,NULL);
824     CLSIDFromString(productid, &clsid);
825     
826     encode_base85_guid(&clsid,productid_85);
827
828     CLSIDFromString(component->ComponentId, &clsid);
829     encode_base85_guid(&clsid,component_85);
830
831     TRACE("Doing something with this... %s %s %s\n", 
832             debugstr_w(productid_85), debugstr_w(feature),
833             debugstr_w(component_85));
834  
835     sz = lstrlenW(productid_85) + lstrlenW(feature);
836     if (component)
837         sz += lstrlenW(component_85);
838
839     sz+=3;
840     sz *= sizeof(WCHAR);
841            
842     output = HeapAlloc(GetProcessHeap(),0,sz);
843     memset(output,0,sz);
844
845     if (component)
846         sprintfW(output,fmt2,productid_85,feature,component_85);
847     else
848         sprintfW(output,fmt1,productid_85,feature);
849
850     HeapFree(GetProcessHeap(),0,productid);
851     
852     return output;
853 }
854
855 /* update compoennt state based on a feature change */
856 void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
857 {
858     int i;
859     INSTALLSTATE newstate;
860     MSIFEATURE *feature;
861
862     i = get_loaded_feature(package,szFeature);
863     if (i < 0)
864         return;
865
866     feature = &package->features[i];
867     newstate = feature->ActionRequest;
868
869     for( i = 0; i < feature->ComponentCount; i++)
870     {
871         MSICOMPONENT* component = &package->components[feature->Components[i]];
872
873         TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
874             newstate, debugstr_w(component->Component), component->Installed, 
875             component->Action, component->ActionRequest);
876         
877         if (!component->Enabled)
878             continue;
879         else
880         {
881             if (newstate == INSTALLSTATE_LOCAL)
882             {
883                 component->ActionRequest = INSTALLSTATE_LOCAL;
884                 component->Action = INSTALLSTATE_LOCAL;
885             }
886             else 
887             {
888                 int j,k;
889
890                 component->ActionRequest = newstate;
891                 component->Action = newstate;
892
893                 /*if any other feature wants is local we need to set it local*/
894                 for (j = 0; 
895                      j < package->loaded_features &&
896                      component->ActionRequest != INSTALLSTATE_LOCAL; 
897                      j++)
898                 {
899                     for (k = 0; k < package->features[j].ComponentCount; k++)
900                         if ( package->features[j].Components[k] ==
901                              feature->Components[i] )
902                         {
903                             if (package->features[j].ActionRequest == 
904                                 INSTALLSTATE_LOCAL)
905                             {
906                                 TRACE("Saved by %s\n", debugstr_w(package->features[j].Feature));
907                                 component->ActionRequest = INSTALLSTATE_LOCAL;
908                                 component->Action = INSTALLSTATE_LOCAL;
909                             }
910                             break;
911                         }
912                 }
913             }
914         }
915         TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
916             newstate, debugstr_w(component->Component), component->Installed, 
917             component->Action, component->ActionRequest);
918     } 
919 }
920
921 UINT register_unique_action(MSIPACKAGE *package, LPCWSTR action)
922 {
923     UINT count;
924     LPWSTR *newbuf = NULL;
925
926     if (!package || !package->script)
927         return FALSE;
928
929     TRACE("Registering Action %s as having fun\n",debugstr_w(action));
930     
931     count = package->script->UniqueActionsCount;
932     package->script->UniqueActionsCount++;
933     if (count != 0)
934         newbuf = HeapReAlloc(GetProcessHeap(),0,
935                         package->script->UniqueActions,
936                         package->script->UniqueActionsCount* sizeof(LPWSTR));
937     else
938         newbuf = HeapAlloc(GetProcessHeap(),0, sizeof(LPWSTR));
939
940     newbuf[count] = strdupW(action);
941     package->script->UniqueActions = newbuf;
942
943    return ERROR_SUCCESS;
944 }
945
946 BOOL check_unique_action(MSIPACKAGE *package, LPCWSTR action)
947 {
948     INT i;
949
950     if (!package || !package->script)
951         return FALSE;
952
953     for (i = 0; i < package->script->UniqueActionsCount; i++)
954         if (!strcmpW(package->script->UniqueActions[i],action))
955             return TRUE;
956
957     return FALSE;
958 }
959
960 WCHAR* generate_error_string(MSIPACKAGE *package, UINT error, DWORD count, ... )
961 {
962     static const WCHAR query[] = {'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ','F','R','O','M',' ','`','E','r','r','o','r','`',' ','W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ','%','i',0};
963
964     MSIRECORD *rec;
965     MSIRECORD *row;
966     DWORD size = 0;
967     DWORD i;
968     va_list va;
969     LPCWSTR str;
970     LPWSTR data;
971
972     row = MSI_QueryGetRecord(package->db, query, error);
973     if (!row)
974         return 0;
975
976     rec = MSI_CreateRecord(count+2);
977
978     str = MSI_RecordGetString(row,1);
979     MSI_RecordSetStringW(rec,0,str);
980     msiobj_release( &row->hdr );
981     MSI_RecordSetInteger(rec,1,error);
982
983     va_start(va,count);
984     for (i = 0; i < count; i++)
985     {
986         str = va_arg(va,LPCWSTR);
987         MSI_RecordSetStringW(rec,(i+2),str);
988     }
989     va_end(va);
990
991     MSI_FormatRecordW(package,rec,NULL,&size);
992     if (size >= 0)
993     {
994         size++;
995         data = HeapAlloc(GetProcessHeap(),0,size*sizeof(WCHAR));
996         if (size > 1)
997             MSI_FormatRecordW(package,rec,data,&size);
998         else
999             data[0] = 0;
1000         msiobj_release( &rec->hdr );
1001         return data;
1002     }
1003
1004     msiobj_release( &rec->hdr );
1005     data = NULL;
1006     return data;
1007 }