2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Mike McCormack for CodeWeavers
5 * Copyright 2005 Aric Stewart for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/debug.h"
37 #include "msiserver.h"
38 #include "wine/unicode.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
42 /* types arranged by precedence */
43 #define FORMAT_NULL 0x0001
44 #define FORMAT_LITERAL 0x0002
45 #define FORMAT_NUMBER 0x0004
46 #define FORMAT_LBRACK 0x0010
47 #define FORMAT_LBRACE 0x0020
48 #define FORMAT_RBRACK 0x0011
49 #define FORMAT_RBRACE 0x0021
50 #define FORMAT_ESCAPE 0x0040
51 #define FORMAT_PROPNULL 0x0080
52 #define FORMAT_ERROR 0x1000
53 #define FORMAT_FAIL 0x2000
55 #define left_type(x) (x & 0xF0)
57 typedef struct _tagFORMAT
69 typedef struct _tagFORMSTR
79 typedef struct _tagSTACK
84 static STACK
*create_stack(void)
86 STACK
*stack
= msi_alloc(sizeof(STACK
));
87 list_init(&stack
->items
);
91 static void free_stack(STACK
*stack
)
93 while (!list_empty(&stack
->items
))
95 FORMSTR
*str
= LIST_ENTRY(list_head(&stack
->items
), FORMSTR
, entry
);
96 list_remove(&str
->entry
);
103 static void stack_push(STACK
*stack
, FORMSTR
*str
)
105 list_add_head(&stack
->items
, &str
->entry
);
108 static FORMSTR
*stack_pop(STACK
*stack
)
112 if (list_empty(&stack
->items
))
115 ret
= LIST_ENTRY(list_head(&stack
->items
), FORMSTR
, entry
);
116 list_remove(&ret
->entry
);
120 static FORMSTR
*stack_find(STACK
*stack
, int type
)
124 LIST_FOR_EACH_ENTRY(str
, &stack
->items
, FORMSTR
, entry
)
126 if (str
->type
== type
)
133 static FORMSTR
*stack_peek(STACK
*stack
)
135 return LIST_ENTRY(list_head(&stack
->items
), FORMSTR
, entry
);
138 static LPCWSTR
get_formstr_data(FORMAT
*format
, FORMSTR
*str
)
140 return &format
->deformatted
[str
->n
];
143 static WCHAR
*dup_formstr( FORMAT
*format
, FORMSTR
*str
, int *ret_len
)
147 if (!str
->len
) return NULL
;
148 if ((val
= msi_alloc( (str
->len
+ 1) * sizeof(WCHAR
) )))
150 memcpy( val
, get_formstr_data(format
, str
), str
->len
* sizeof(WCHAR
) );
157 static WCHAR
*deformat_index( FORMAT
*format
, FORMSTR
*str
, int *ret_len
)
163 if (!(val
= msi_alloc( (str
->len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
164 lstrcpynW(val
, get_formstr_data(format
, str
), str
->len
+ 1);
165 field
= atoiW( val
);
168 if (MSI_RecordIsNull( format
->record
, field
) ||
169 MSI_RecordGetStringW( format
->record
, field
, NULL
, &len
)) return NULL
;
172 if (!(ret
= msi_alloc( len
* sizeof(WCHAR
) ))) return NULL
;
174 if (MSI_RecordGetStringW( format
->record
, field
, ret
, &len
))
183 static WCHAR
*deformat_property( FORMAT
*format
, FORMSTR
*str
, int *ret_len
)
189 if (!(prop
= msi_alloc( (str
->len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
190 lstrcpynW( prop
, get_formstr_data(format
, str
), str
->len
+ 1 );
192 r
= msi_get_property( format
->package
->db
, prop
, NULL
, &len
);
193 if (r
!= ERROR_SUCCESS
&& r
!= ERROR_MORE_DATA
)
199 if ((ret
= msi_alloc( len
* sizeof(WCHAR
) )))
200 msi_get_property( format
->package
->db
, prop
, ret
, &len
);
206 static WCHAR
*deformat_component( FORMAT
*format
, FORMSTR
*str
, int *ret_len
)
211 if (!(key
= msi_alloc( (str
->len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
212 lstrcpynW(key
, get_formstr_data(format
, str
), str
->len
+ 1);
214 if (!(comp
= msi_get_loaded_component( format
->package
, key
)))
219 if (comp
->Action
== INSTALLSTATE_SOURCE
)
220 ret
= msi_resolve_source_folder( format
->package
, comp
->Directory
, NULL
);
222 ret
= strdupW( msi_get_target_folder( format
->package
, comp
->Directory
) );
224 if (ret
) *ret_len
= strlenW( ret
);
230 static WCHAR
*deformat_file( FORMAT
*format
, FORMSTR
*str
, BOOL shortname
, int *ret_len
)
232 WCHAR
*key
, *ret
= NULL
;
236 if (!(key
= msi_alloc( (str
->len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
237 lstrcpynW(key
, get_formstr_data(format
, str
), str
->len
+ 1);
239 if (!(file
= msi_get_loaded_file( format
->package
, key
))) goto done
;
242 if ((ret
= strdupW( file
->TargetPath
))) len
= strlenW( ret
);
245 if ((len
= GetShortPathNameW(file
->TargetPath
, NULL
, 0)) <= 0)
247 if ((ret
= strdupW( file
->TargetPath
))) len
= strlenW( ret
);
251 if ((ret
= msi_alloc( len
* sizeof(WCHAR
) )))
252 len
= GetShortPathNameW( file
->TargetPath
, ret
, len
);
260 static WCHAR
*deformat_environment( FORMAT
*format
, FORMSTR
*str
, int *ret_len
)
262 WCHAR
*key
, *ret
= NULL
;
265 if (!(key
= msi_alloc((str
->len
+ 1) * sizeof(WCHAR
)))) return NULL
;
266 lstrcpynW(key
, get_formstr_data(format
, str
), str
->len
+ 1);
268 if ((len
= GetEnvironmentVariableW( key
, NULL
, 0 )))
271 if ((ret
= msi_alloc( len
* sizeof(WCHAR
) )))
272 *ret_len
= GetEnvironmentVariableW( key
, ret
, len
);
278 static WCHAR
*deformat_literal( FORMAT
*format
, FORMSTR
*str
, BOOL
*propfound
,
279 BOOL
*nonprop
, int *type
, int *len
)
281 LPCWSTR data
= get_formstr_data(format
, str
);
282 WCHAR
*replaced
= NULL
;
296 replaced
= dup_formstr( format
, str
, len
);
303 else if ((replaced
= msi_alloc( sizeof(WCHAR
) )))
309 else if (ch
== '%' || ch
== '#' || ch
== '!' || ch
== '$')
317 replaced
= deformat_environment( format
, str
, len
); break;
319 replaced
= deformat_file( format
, str
, FALSE
, len
); break;
321 replaced
= deformat_file( format
, str
, TRUE
, len
); break;
323 replaced
= deformat_component( format
, str
, len
); break;
326 *type
= FORMAT_LITERAL
;
330 replaced
= deformat_property( format
, str
, len
);
331 *type
= FORMAT_LITERAL
;
336 format
->propfailed
= TRUE
;
342 static LPWSTR
build_default_format(const MSIRECORD
* record
)
347 static const WCHAR fmt
[] = {'%','i',':',' ','%','s',' ',0};
348 static const WCHAR fmt_null
[] = {'%','i',':',' ',' ',0};
349 static const WCHAR fmt_index
[] = {'%','i',0};
352 DWORD size
, max_len
, len
;
354 count
= MSI_RecordGetFieldCount(record
);
357 buf
= msi_alloc((max_len
+ 1) * sizeof(WCHAR
));
361 for (i
= 1; i
<= count
; i
++)
363 sprintfW(index
, fmt_index
, i
);
364 str
= MSI_RecordGetString(record
, i
);
365 len
= (str
) ? lstrlenW(str
) : 0;
366 len
+= (sizeof(fmt_null
)/sizeof(fmt_null
[0]) - 3) + lstrlenW(index
);
372 buf
= msi_realloc(buf
, (max_len
+ 1) * sizeof(WCHAR
));
381 sprintfW(buf
, fmt
, i
, str
);
383 sprintfW(buf
, fmt_null
, i
);
387 rc
= msi_alloc(size
* sizeof(WCHAR
));
392 rc
= msi_realloc(rc
, size
* sizeof(WCHAR
));
401 static BOOL
format_is_number(WCHAR x
)
403 return ((x
>= '0') && (x
<= '9'));
406 static BOOL
format_str_is_number(LPWSTR str
)
410 for (ptr
= str
; *ptr
; ptr
++)
411 if (!format_is_number(*ptr
))
417 static BOOL
format_is_alpha(WCHAR x
)
419 return (!format_is_number(x
) && x
!= '\0' &&
420 x
!= '[' && x
!= ']' && x
!= '{' && x
!= '}');
423 static BOOL
format_is_literal(WCHAR x
)
425 return (format_is_alpha(x
) || format_is_number(x
));
428 static int format_lex(FORMAT
*format
, FORMSTR
**out
)
437 if (!format
->deformatted
)
440 *out
= msi_alloc_zero(sizeof(FORMSTR
));
447 data
= get_formstr_data(format
, str
);
452 case '{': type
= FORMAT_LBRACE
; break;
453 case '}': type
= FORMAT_RBRACE
; break;
454 case '[': type
= FORMAT_LBRACK
; break;
455 case ']': type
= FORMAT_RBRACK
; break;
456 case '~': type
= FORMAT_PROPNULL
; break;
457 case '\0': type
= FORMAT_NULL
; break;
472 while (data
[len
] && data
[len
] != ']')
475 type
= FORMAT_ESCAPE
;
477 else if (format_is_alpha(ch
))
479 while (format_is_literal(data
[len
]))
482 type
= FORMAT_LITERAL
;
484 else if (format_is_number(ch
))
486 while (format_is_number(data
[len
]))
489 type
= FORMAT_NUMBER
;
491 if (data
[len
] != ']')
493 while (format_is_literal(data
[len
]))
496 type
= FORMAT_LITERAL
;
501 ERR("Got unknown character %c(%x)\n", ch
, ch
);
512 static FORMSTR
*format_replace( FORMAT
*format
, BOOL propfound
, BOOL nonprop
,
513 int oldsize
, int type
, WCHAR
*replace
, int len
)
529 size
= format
->len
+ size
+ 1;
533 msi_free(format
->deformatted
);
534 format
->deformatted
= NULL
;
539 str
= msi_alloc(size
* sizeof(WCHAR
));
544 memcpy(str
, format
->deformatted
, format
->n
* sizeof(WCHAR
));
549 if (!len
) str
[n
++] = 0;
552 memcpy( str
+ n
, replace
, len
* sizeof(WCHAR
) );
558 ptr
= &format
->deformatted
[format
->n
+ oldsize
];
559 memcpy(&str
[n
], ptr
, (lstrlenW(ptr
) + 1) * sizeof(WCHAR
));
561 msi_free(format
->deformatted
);
562 format
->deformatted
= str
;
563 format
->len
= size
- 1;
565 /* don't reformat the NULL */
572 ret
= msi_alloc_zero(sizeof(FORMSTR
));
579 ret
->propfound
= propfound
;
580 ret
->nonprop
= nonprop
;
585 static WCHAR
*replace_stack_group( FORMAT
*format
, STACK
*values
,
586 BOOL
*propfound
, BOOL
*nonprop
,
587 int *oldsize
, int *type
, int *len
)
590 FORMSTR
*content
, *node
;
596 node
= stack_pop(values
);
598 *oldsize
= node
->len
;
601 while ((node
= stack_pop(values
)))
603 *oldsize
+= node
->len
;
614 content
= msi_alloc_zero(sizeof(FORMSTR
));
616 content
->len
= *oldsize
;
617 content
->type
= FORMAT_LITERAL
;
619 if (!format
->groupfailed
&& (*oldsize
== 2 ||
620 (format
->propfailed
&& !*nonprop
)))
625 else if (format
->deformatted
[content
->n
+ 1] == '{' &&
626 format
->deformatted
[content
->n
+ content
->len
- 2] == '}')
628 format
->groupfailed
= FALSE
;
631 else if (*propfound
&& !*nonprop
&&
632 !format
->groupfailed
&& format
->groups
== 0)
639 if (format
->groups
!= 0)
640 format
->groupfailed
= TRUE
;
645 replaced
= dup_formstr( format
, content
, len
);
646 *type
= content
->type
;
649 if (format
->groups
== 0)
650 format
->propfailed
= FALSE
;
655 static WCHAR
*replace_stack_prop( FORMAT
*format
, STACK
*values
,
656 BOOL
*propfound
, BOOL
*nonprop
,
657 int *oldsize
, int *type
, int *len
)
660 FORMSTR
*content
, *node
;
666 node
= stack_pop(values
);
668 *oldsize
= node
->len
;
669 *type
= stack_peek(values
)->type
;
672 while ((node
= stack_pop(values
)))
674 *oldsize
+= node
->len
;
676 if (*type
!= FORMAT_ESCAPE
&&
677 stack_peek(values
) && node
->type
!= *type
)
678 *type
= FORMAT_LITERAL
;
683 content
= msi_alloc_zero(sizeof(FORMSTR
));
685 content
->len
= *oldsize
- 2;
686 content
->type
= *type
;
688 if (*type
== FORMAT_NUMBER
)
690 replaced
= deformat_index( format
, content
, len
);
694 format
->propfailed
= TRUE
;
697 *type
= format_str_is_number(replaced
) ?
698 FORMAT_NUMBER
: FORMAT_LITERAL
;
700 else if (format
->package
)
702 replaced
= deformat_literal( format
, content
, propfound
, nonprop
, type
, len
);
709 replaced
= dup_formstr( format
, content
, len
);
715 static UINT
replace_stack(FORMAT
*format
, STACK
*stack
, STACK
*values
)
717 WCHAR
*replaced
= NULL
;
718 FORMSTR
*beg
, *top
, *node
;
719 BOOL propfound
= FALSE
, nonprop
= FALSE
, group
= FALSE
;
720 int type
, n
, len
= 0, oldsize
= 0;
722 node
= stack_peek(values
);
726 if (type
== FORMAT_LBRACK
)
727 replaced
= replace_stack_prop( format
, values
, &propfound
,
728 &nonprop
, &oldsize
, &type
, &len
);
729 else if (type
== FORMAT_LBRACE
)
731 replaced
= replace_stack_group( format
, values
, &propfound
,
732 &nonprop
, &oldsize
, &type
, &len
);
737 beg
= format_replace( format
, propfound
, nonprop
, oldsize
, type
, replaced
, len
);
740 return ERROR_SUCCESS
;
742 format
->n
= beg
->n
+ beg
->len
;
744 top
= stack_peek(stack
);
749 if ((type
== FORMAT_LITERAL
|| type
== FORMAT_NUMBER
) &&
752 top
->len
+= beg
->len
;
755 top
->nonprop
= FALSE
;
757 if (type
== FORMAT_LITERAL
)
758 top
->nonprop
= beg
->nonprop
;
761 top
->propfound
= TRUE
;
764 return ERROR_SUCCESS
;
768 stack_push(stack
, beg
);
769 return ERROR_SUCCESS
;
772 static BOOL
verify_format(LPWSTR data
)
778 if (*data
== '[' && *(data
- 1) != '\\')
780 else if (*data
== ']')
792 static DWORD
deformat_string_internal(MSIPACKAGE
*package
, LPCWSTR ptr
,
793 WCHAR
** data
, DWORD
*len
,
794 MSIRECORD
* record
, INT
* failcount
)
806 return ERROR_SUCCESS
;
809 *data
= strdupW(ptr
);
810 *len
= lstrlenW(ptr
);
812 ZeroMemory(&format
, sizeof(FORMAT
));
813 format
.package
= package
;
814 format
.record
= record
;
815 format
.deformatted
= *data
;
818 if (!verify_format(*data
))
819 return ERROR_SUCCESS
;
821 stack
= create_stack();
822 temp
= create_stack();
824 while ((type
= format_lex(&format
, &str
)) != FORMAT_NULL
)
826 if (type
== FORMAT_LBRACK
|| type
== FORMAT_LBRACE
||
827 type
== FORMAT_LITERAL
|| type
== FORMAT_NUMBER
||
828 type
== FORMAT_ESCAPE
|| type
== FORMAT_PROPNULL
)
830 if (type
== FORMAT_LBRACE
)
832 format
.propfailed
= FALSE
;
835 else if (type
== FORMAT_ESCAPE
&&
836 !stack_find(stack
, FORMAT_LBRACK
))
838 format
.n
-= str
->len
- 1;
842 stack_push(stack
, str
);
844 else if (type
== FORMAT_RBRACK
|| type
== FORMAT_RBRACE
)
846 if (type
== FORMAT_RBRACE
)
849 stack_push(stack
, str
);
851 if (stack_find(stack
, left_type(type
)))
855 node
= stack_pop(stack
);
856 stack_push(temp
, node
);
857 } while (node
->type
!= left_type(type
));
859 replace_stack(&format
, stack
, temp
);
864 *data
= format
.deformatted
;
871 return ERROR_SUCCESS
;
874 UINT
MSI_FormatRecordW( MSIPACKAGE
* package
, MSIRECORD
* record
, LPWSTR buffer
,
877 WCHAR
*format
, *deformated
;
878 UINT rc
= ERROR_INVALID_PARAMETER
;
881 TRACE("%p %p %p %p\n", package
, record
, buffer
, size
);
883 if (!(format
= msi_dup_record_field( record
, 0 )))
884 format
= build_default_format( record
);
886 TRACE("%s\n", debugstr_w(format
));
888 deformat_string_internal( package
, format
, &deformated
, &len
, record
, NULL
);
893 memcpy(buffer
,deformated
,len
*sizeof(WCHAR
));
901 memcpy(buffer
,deformated
,(*size
)*sizeof(WCHAR
));
902 buffer
[(*size
)-1] = 0;
904 rc
= ERROR_MORE_DATA
;
907 else rc
= ERROR_SUCCESS
;
911 msi_free( deformated
);
915 UINT WINAPI
MsiFormatRecordW( MSIHANDLE hInstall
, MSIHANDLE hRecord
,
916 LPWSTR szResult
, LPDWORD sz
)
918 UINT r
= ERROR_INVALID_HANDLE
;
922 TRACE("%d %d %p %p\n", hInstall
, hRecord
, szResult
, sz
);
924 package
= msihandle2msiinfo( hInstall
, MSIHANDLETYPE_PACKAGE
);
928 IWineMsiRemotePackage
*remote_package
;
932 remote_package
= (IWineMsiRemotePackage
*)msi_get_remote( hInstall
);
935 hr
= IWineMsiRemotePackage_FormatRecord( remote_package
, hRecord
,
941 wstr
.str
.w
= szResult
;
942 r
= msi_strcpy_to_awstring( value
, SysStringLen(value
), &wstr
, sz
);
945 IWineMsiRemotePackage_Release( remote_package
);
946 SysFreeString( value
);
950 if (HRESULT_FACILITY(hr
) == FACILITY_WIN32
)
951 return HRESULT_CODE(hr
);
953 return ERROR_FUNCTION_FAILED
;
960 record
= msihandle2msiinfo( hRecord
, MSIHANDLETYPE_RECORD
);
963 return ERROR_INVALID_HANDLE
;
966 msiobj_release( &record
->hdr
);
968 return ERROR_INVALID_PARAMETER
;
970 return ERROR_SUCCESS
;
973 r
= MSI_FormatRecordW( package
, record
, szResult
, sz
);
974 msiobj_release( &record
->hdr
);
976 msiobj_release( &package
->hdr
);
980 UINT WINAPI
MsiFormatRecordA( MSIHANDLE hInstall
, MSIHANDLE hRecord
,
981 LPSTR szResult
, LPDWORD sz
)
987 TRACE("%d %d %p %p\n", hInstall
, hRecord
, szResult
, sz
);
990 return ERROR_INVALID_HANDLE
;
995 return ERROR_INVALID_PARAMETER
;
997 return ERROR_SUCCESS
;
1000 r
= MsiFormatRecordW( hInstall
, hRecord
, NULL
, &len
);
1001 if (r
!= ERROR_SUCCESS
)
1004 value
= msi_alloc(++len
* sizeof(WCHAR
));
1006 return ERROR_OUTOFMEMORY
;
1008 r
= MsiFormatRecordW( hInstall
, hRecord
, value
, &len
);
1009 if (r
!= ERROR_SUCCESS
)
1013 len
= WideCharToMultiByte(CP_ACP
, 0, value
, len
+ 1, NULL
, 0, NULL
, NULL
);
1014 WideCharToMultiByte(CP_ACP
, 0, value
, len
, szResult
, *sz
, NULL
, NULL
);
1016 if (szResult
&& len
> *sz
)
1018 if (*sz
) szResult
[*sz
- 1] = '\0';
1019 r
= ERROR_MORE_DATA
;
1029 /* wrapper to resist a need for a full rewrite right now */
1030 DWORD
deformat_string( MSIPACKAGE
*package
, const WCHAR
*fmt
, WCHAR
**data
)
1037 if (!(rec
= MSI_CreateRecord( 1 ))) return 0;
1039 MSI_RecordSetStringW( rec
, 0, fmt
);
1040 MSI_FormatRecordW( package
, rec
, NULL
, &len
);
1041 if (!(*data
= msi_alloc( ++len
* sizeof(WCHAR
) )))
1043 msiobj_release( &rec
->hdr
);
1046 MSI_FormatRecordW( package
, rec
, *data
, &len
);
1047 msiobj_release( &rec
->hdr
);