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 LPWSTR dup_formstr(FORMAT *format, FORMSTR *str)
151 val = msi_alloc((str->len + 1) * sizeof(WCHAR));
152 data = get_formstr_data(format, str);
153 lstrcpynW(val, data, str->len + 1);
158 static LPWSTR deformat_index(FORMAT *format, FORMSTR *str)
162 val = msi_alloc((str->len + 1) * sizeof(WCHAR));
163 lstrcpynW(val, get_formstr_data(format, str), str->len + 1);
165 ret = msi_dup_record_field(format->record, atoiW(val));
171 static LPWSTR deformat_property(FORMAT *format, FORMSTR *str)
175 val = msi_alloc((str->len + 1) * sizeof(WCHAR));
176 lstrcpynW(val, get_formstr_data(format, str), str->len + 1);
178 ret = msi_dup_property(format->package->db, val);
184 static LPWSTR deformat_component(FORMAT *format, FORMSTR *str)
186 LPWSTR key, ret = NULL;
189 key = msi_alloc((str->len + 1) * sizeof(WCHAR));
190 lstrcpynW(key, get_formstr_data(format, str), str->len + 1);
192 comp = get_loaded_component(format->package, key);
196 if (comp->Action == INSTALLSTATE_SOURCE)
197 ret = resolve_source_folder( format->package, comp->Directory, NULL );
199 ret = resolve_target_folder( format->package, comp->Directory, FALSE, TRUE, NULL );
206 static LPWSTR deformat_file(FORMAT *format, FORMSTR *str, BOOL shortname)
208 LPWSTR key, ret = NULL;
212 key = msi_alloc((str->len + 1) * sizeof(WCHAR));
213 lstrcpynW(key, get_formstr_data(format, str), str->len + 1);
215 file = get_loaded_file(format->package, key);
221 ret = strdupW(file->TargetPath);
225 size = GetShortPathNameW(file->TargetPath, NULL, 0);
228 ret = strdupW(file->TargetPath);
233 ret = msi_alloc(size * sizeof(WCHAR));
234 GetShortPathNameW(file->TargetPath, ret, size);
241 static LPWSTR deformat_environment(FORMAT *format, FORMSTR *str)
243 LPWSTR key, ret = NULL;
246 key = msi_alloc((str->len + 1) * sizeof(WCHAR));
247 lstrcpynW(key, get_formstr_data(format, str), str->len + 1);
249 sz = GetEnvironmentVariableW(key, NULL ,0);
254 ret = msi_alloc(sz * sizeof(WCHAR));
255 GetEnvironmentVariableW(key, ret, sz);
262 static LPWSTR deformat_literal(FORMAT *format, FORMSTR *str, BOOL *propfound,
263 BOOL *nonprop, int *type)
265 LPCWSTR data = get_formstr_data(format, str);
266 LPWSTR replaced = NULL;
280 replaced = dup_formstr(format, str);
289 replaced = msi_alloc(sizeof(WCHAR));
293 else if (ch == '%' || ch == '#' || ch == '!' || ch == '$')
301 replaced = deformat_environment(format, str); break;
303 replaced = deformat_file(format, str, FALSE); break;
305 replaced = deformat_file(format, str, TRUE); break;
307 replaced = deformat_component(format, str); break;
310 *type = FORMAT_LITERAL;
314 replaced = deformat_property(format, str);
315 *type = FORMAT_LITERAL;
320 format->propfailed = TRUE;
326 static LPWSTR build_default_format(const MSIRECORD* record)
331 static const WCHAR fmt[] = {'%','i',':',' ','%','s',' ',0};
332 static const WCHAR fmt_null[] = {'%','i',':',' ',' ',0};
333 static const WCHAR fmt_index[] = {'%','i',0};
336 DWORD size, max_len, len;
338 count = MSI_RecordGetFieldCount(record);
341 buf = msi_alloc((max_len + 1) * sizeof(WCHAR));
345 for (i = 1; i <= count; i++)
347 sprintfW(index, fmt_index, i);
348 str = MSI_RecordGetString(record, i);
349 len = (str) ? lstrlenW(str) : 0;
350 len += (sizeof(fmt_null)/sizeof(fmt_null[0]) - 3) + lstrlenW(index);
356 buf = msi_realloc(buf, (max_len + 1) * sizeof(WCHAR));
357 if (!buf) return NULL;
361 sprintfW(buf, fmt, i, str);
363 sprintfW(buf, fmt_null, i);
367 rc = msi_alloc(size * sizeof(WCHAR));
372 rc = msi_realloc(rc, size * sizeof(WCHAR));
381 static BOOL format_is_number(WCHAR x)
383 return ((x >= '0') && (x <= '9'));
386 static BOOL format_str_is_number(LPWSTR str)
390 for (ptr = str; *ptr; ptr++)
391 if (!format_is_number(*ptr))
397 static BOOL format_is_alpha(WCHAR x)
399 return (!format_is_number(x) && x != '\0' &&
400 x != '[' && x != ']' && x != '{' && x != '}');
403 static BOOL format_is_literal(WCHAR x)
405 return (format_is_alpha(x) || format_is_number(x));
408 static int format_lex(FORMAT *format, FORMSTR **out)
417 if (!format->deformatted)
420 *out = msi_alloc_zero(sizeof(FORMSTR));
427 data = get_formstr_data(format, str);
432 case '{': type = FORMAT_LBRACE; break;
433 case '}': type = FORMAT_RBRACE; break;
434 case '[': type = FORMAT_LBRACK; break;
435 case ']': type = FORMAT_RBRACK; break;
436 case '~': type = FORMAT_PROPNULL; break;
437 case '\0': type = FORMAT_NULL; break;
452 while (data[len] && data[len] != ']')
455 type = FORMAT_ESCAPE;
457 else if (format_is_alpha(ch))
459 while (format_is_literal(data[len]))
462 type = FORMAT_LITERAL;
464 else if (format_is_number(ch))
466 while (format_is_number(data[len]))
469 type = FORMAT_NUMBER;
471 if (data[len] != ']')
473 while (format_is_literal(data[len]))
476 type = FORMAT_LITERAL;
481 ERR("Got unknown character %c(%x)\n", ch, ch);
492 static FORMSTR *format_replace(FORMAT *format, BOOL propfound, BOOL nonprop,
493 int oldsize, int type, LPWSTR replace)
505 size = lstrlenW(replace);
509 size = format->len + size + 1;
513 msi_free(format->deformatted);
514 format->deformatted = NULL;
519 str = msi_alloc(size * sizeof(WCHAR));
524 memcpy(str, format->deformatted, format->n * sizeof(WCHAR));
536 lstrcpyW(&str[n], replace);
537 n += lstrlenW(replace);
541 ptr = &format->deformatted[format->n + oldsize];
542 memcpy(&str[n], ptr, (lstrlenW(ptr) + 1) * sizeof(WCHAR));
544 msi_free(format->deformatted);
545 format->deformatted = str;
546 format->len = size - 1;
548 /* don't reformat the NULL */
549 if (replace && !*replace)
555 ret = msi_alloc_zero(sizeof(FORMSTR));
559 ret->len = lstrlenW(replace);
562 ret->propfound = propfound;
563 ret->nonprop = nonprop;
568 static LPWSTR replace_stack_group(FORMAT *format, STACK *values,
569 BOOL *propfound, BOOL *nonprop,
570 int *oldsize, int *type)
572 LPWSTR replaced = NULL;
580 node = stack_pop(values);
582 *oldsize = node->len;
585 while ((node = stack_pop(values)))
587 *oldsize += node->len;
598 content = msi_alloc_zero(sizeof(FORMSTR));
600 content->len = *oldsize;
601 content->type = FORMAT_LITERAL;
603 if (!format->groupfailed && (*oldsize == 2 ||
604 (format->propfailed && !*nonprop)))
609 else if (format->deformatted[content->n + 1] == '{' &&
610 format->deformatted[content->n + content->len - 2] == '}')
612 format->groupfailed = FALSE;
615 else if (*propfound && !*nonprop &&
616 !format->groupfailed && format->groups == 0)
623 if (format->groups != 0)
624 format->groupfailed = TRUE;
629 replaced = dup_formstr(format, content);
630 *type = content->type;
633 if (format->groups == 0)
634 format->propfailed = FALSE;
639 static LPWSTR replace_stack_prop(FORMAT *format, STACK *values,
640 BOOL *propfound, BOOL *nonprop,
641 int *oldsize, int *type)
643 LPWSTR replaced = NULL;
651 node = stack_pop(values);
653 *oldsize = node->len;
654 *type = stack_peek(values)->type;
657 while ((node = stack_pop(values)))
659 *oldsize += node->len;
661 if (*type != FORMAT_ESCAPE &&
662 stack_peek(values) && node->type != *type)
663 *type = FORMAT_LITERAL;
668 content = msi_alloc_zero(sizeof(FORMSTR));
670 content->len = *oldsize - 2;
671 content->type = *type;
673 if (*type == FORMAT_NUMBER)
675 replaced = deformat_index(format, content);
679 format->propfailed = TRUE;
682 *type = format_str_is_number(replaced) ?
683 FORMAT_NUMBER : FORMAT_LITERAL;
685 else if (format->package)
687 replaced = deformat_literal(format, content, propfound, nonprop, type);
694 replaced = dup_formstr(format, content);
701 static UINT replace_stack(FORMAT *format, STACK *stack, STACK *values)
703 LPWSTR replaced = NULL;
707 BOOL propfound = FALSE;
708 BOOL nonprop = FALSE;
713 node = stack_peek(values);
717 if (type == FORMAT_LBRACK)
718 replaced = replace_stack_prop(format, values, &propfound,
719 &nonprop, &oldsize, &type);
720 else if (type == FORMAT_LBRACE)
722 replaced = replace_stack_group(format, values, &propfound,
723 &nonprop, &oldsize, &type);
728 beg = format_replace(format, propfound, nonprop, oldsize, type, replaced);
730 return ERROR_SUCCESS;
733 format->n = beg->n + beg->len;
735 top = stack_peek(stack);
740 if ((type == FORMAT_LITERAL || type == FORMAT_NUMBER) &&
743 top->len += beg->len;
746 top->nonprop = FALSE;
748 if (type == FORMAT_LITERAL)
749 top->nonprop = beg->nonprop;
752 top->propfound = TRUE;
755 return ERROR_SUCCESS;
759 stack_push(stack, beg);
760 return ERROR_SUCCESS;
763 static BOOL verify_format(LPWSTR data)
769 if (*data == '[' && *(data - 1) != '\\')
771 else if (*data == ']')
783 static DWORD deformat_string_internal(MSIPACKAGE *package, LPCWSTR ptr,
784 WCHAR** data, DWORD *len,
785 MSIRECORD* record, INT* failcount)
797 return ERROR_SUCCESS;
800 *data = strdupW(ptr);
801 *len = lstrlenW(ptr);
803 ZeroMemory(&format, sizeof(FORMAT));
804 format.package = package;
805 format.record = record;
806 format.deformatted = *data;
809 if (!verify_format(*data))
810 return ERROR_SUCCESS;
812 stack = create_stack();
813 temp = create_stack();
815 while ((type = format_lex(&format, &str)) != FORMAT_NULL)
817 if (type == FORMAT_LBRACK || type == FORMAT_LBRACE ||
818 type == FORMAT_LITERAL || type == FORMAT_NUMBER ||
819 type == FORMAT_ESCAPE || type == FORMAT_PROPNULL)
821 if (type == FORMAT_LBRACE)
823 format.propfailed = FALSE;
826 else if (type == FORMAT_ESCAPE &&
827 !stack_find(stack, FORMAT_LBRACK))
829 format.n -= str->len - 1;
833 stack_push(stack, str);
835 else if (type == FORMAT_RBRACK || type == FORMAT_RBRACE)
837 if (type == FORMAT_RBRACE)
840 stack_push(stack, str);
842 if (stack_find(stack, left_type(type)))
846 node = stack_pop(stack);
847 stack_push(temp, node);
848 } while (node->type != left_type(type));
850 replace_stack(&format, stack, temp);
855 *data = format.deformatted;
862 return ERROR_SUCCESS;
865 UINT MSI_FormatRecordW( MSIPACKAGE* package, MSIRECORD* record, LPWSTR buffer,
871 UINT rc = ERROR_INVALID_PARAMETER;
873 TRACE("%p %p %p %p\n", package, record, buffer, size);
875 rec = msi_dup_record_field(record,0);
877 rec = build_default_format(record);
879 TRACE("(%s)\n",debugstr_w(rec));
881 deformat_string_internal(package, rec, &deformated, &len, record, NULL);
886 memcpy(buffer,deformated,len*sizeof(WCHAR));
894 memcpy(buffer,deformated,(*size)*sizeof(WCHAR));
895 buffer[(*size)-1] = 0;
897 rc = ERROR_MORE_DATA;
906 msi_free(deformated);
910 UINT WINAPI MsiFormatRecordW( MSIHANDLE hInstall, MSIHANDLE hRecord,
911 LPWSTR szResult, LPDWORD sz )
913 UINT r = ERROR_INVALID_HANDLE;
917 TRACE("%d %d %p %p\n", hInstall, hRecord, szResult, sz);
919 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
923 IWineMsiRemotePackage *remote_package;
927 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
930 hr = IWineMsiRemotePackage_FormatRecord( remote_package, hRecord,
936 wstr.str.w = szResult;
937 r = msi_strcpy_to_awstring( value, &wstr, sz );
940 IWineMsiRemotePackage_Release( remote_package );
941 SysFreeString( value );
945 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
946 return HRESULT_CODE(hr);
948 return ERROR_FUNCTION_FAILED;
955 record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
958 return ERROR_INVALID_HANDLE;
961 msiobj_release( &record->hdr );
963 return ERROR_INVALID_PARAMETER;
965 return ERROR_SUCCESS;
968 r = MSI_FormatRecordW( package, record, szResult, sz );
969 msiobj_release( &record->hdr );
971 msiobj_release( &package->hdr );
975 UINT WINAPI MsiFormatRecordA( MSIHANDLE hInstall, MSIHANDLE hRecord,
976 LPSTR szResult, LPDWORD sz )
982 TRACE("%d %d %p %p\n", hInstall, hRecord, szResult, sz);
985 return ERROR_INVALID_HANDLE;
990 return ERROR_INVALID_PARAMETER;
992 return ERROR_SUCCESS;
995 r = MsiFormatRecordW( hInstall, hRecord, NULL, &len );
996 if (r != ERROR_SUCCESS)
999 value = msi_alloc(++len * sizeof(WCHAR));
1001 return ERROR_OUTOFMEMORY;
1003 r = MsiFormatRecordW( hInstall, hRecord, value, &len );
1004 if (r != ERROR_SUCCESS)
1008 len = WideCharToMultiByte(CP_ACP, 0, value, len + 1, NULL, 0, NULL, NULL);
1009 WideCharToMultiByte(CP_ACP, 0, value, len, szResult, *sz, NULL, NULL);
1011 if (szResult && len > *sz)
1013 if (*sz) szResult[*sz - 1] = '\0';
1014 r = ERROR_MORE_DATA;