push 88671f85dcf7a7cd89c63d6e9f34ad6b6ad2ae64
[wine/hacks.git] / dlls / msi / format.c
blob128f159da4a42d05573e82fb00e79abdd182bf1b
1 /*
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
22 #include <stdarg.h>
23 #include <stdio.h>
25 #define COBJMACROS
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "wine/debug.h"
31 #include "msi.h"
32 #include "winnls.h"
33 #include "objbase.h"
34 #include "oleauto.h"
36 #include "msipriv.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
59 MSIPACKAGE *package;
60 MSIRECORD *record;
61 LPWSTR deformatted;
62 int len;
63 int n;
64 BOOL propfailed;
65 BOOL groupfailed;
66 int groups;
67 } FORMAT;
69 typedef struct _tagFORMSTR
71 struct list entry;
72 int n;
73 int len;
74 int type;
75 BOOL propfound;
76 BOOL nonprop;
77 } FORMSTR;
79 typedef struct _tagSTACK
81 struct list items;
82 } STACK;
84 static STACK *create_stack(void)
86 STACK *stack = msi_alloc(sizeof(STACK));
87 list_init(&stack->items);
88 return stack;
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);
97 msi_free(str);
100 msi_free(stack);
103 static void stack_push(STACK *stack, FORMSTR *str)
105 list_add_head(&stack->items, &str->entry);
108 static FORMSTR *stack_pop(STACK *stack)
110 FORMSTR *ret;
112 if (list_empty(&stack->items))
113 return NULL;
115 ret = LIST_ENTRY(list_head(&stack->items), FORMSTR, entry);
116 list_remove(&ret->entry);
117 return ret;
120 static FORMSTR *stack_find(STACK *stack, int type)
122 FORMSTR *str;
124 LIST_FOR_EACH_ENTRY(str, &stack->items, FORMSTR, entry)
126 if (str->type == type)
127 return str;
130 return NULL;
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)
145 LPWSTR val;
146 LPCWSTR data;
148 if (str->len == 0)
149 return NULL;
151 val = msi_alloc((str->len + 1) * sizeof(WCHAR));
152 data = get_formstr_data(format, str);
153 lstrcpynW(val, data, str->len + 1);
155 return val;
158 static LPWSTR deformat_index(FORMAT *format, FORMSTR *str)
160 LPWSTR val, ret;
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));
167 msi_free(val);
168 return ret;
171 static LPWSTR deformat_property(FORMAT *format, FORMSTR *str)
173 LPWSTR val, ret;
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, val);
180 msi_free(val);
181 return ret;
184 static LPWSTR deformat_component(FORMAT *format, FORMSTR *str)
186 LPWSTR key, ret = NULL;
187 MSICOMPONENT *comp;
188 BOOL source;
190 key = msi_alloc((str->len + 1) * sizeof(WCHAR));
191 lstrcpynW(key, get_formstr_data(format, str), str->len + 1);
193 comp = get_loaded_component(format->package, key);
194 if (!comp)
195 goto done;
197 source = (comp->Action == INSTALLSTATE_SOURCE) ? TRUE : FALSE;
198 ret = resolve_folder(format->package, comp->Directory, source, FALSE, TRUE, NULL);
200 done:
201 msi_free(key);
202 return ret;
205 static LPWSTR deformat_file(FORMAT *format, FORMSTR *str, BOOL shortname)
207 LPWSTR key, ret = NULL;
208 MSIFILE *file;
209 DWORD size;
211 key = msi_alloc((str->len + 1) * sizeof(WCHAR));
212 lstrcpynW(key, get_formstr_data(format, str), str->len + 1);
214 file = get_loaded_file(format->package, key);
215 if (!file)
216 goto done;
218 if (!shortname)
220 ret = strdupW(file->TargetPath);
221 goto done;
224 size = GetShortPathNameW(file->TargetPath, NULL, 0);
225 if (size <= 0)
227 ret = strdupW(file->TargetPath);
228 goto done;
231 size++;
232 ret = msi_alloc(size * sizeof(WCHAR));
233 GetShortPathNameW(file->TargetPath, ret, size);
235 done:
236 msi_free(key);
237 return ret;
240 static LPWSTR deformat_environment(FORMAT *format, FORMSTR *str)
242 LPWSTR key, ret = NULL;
243 DWORD sz;
245 key = msi_alloc((str->len + 1) * sizeof(WCHAR));
246 lstrcpynW(key, get_formstr_data(format, str), str->len + 1);
248 sz = GetEnvironmentVariableW(key, NULL ,0);
249 if (sz <= 0)
250 goto done;
252 sz++;
253 ret = msi_alloc(sz * sizeof(WCHAR));
254 GetEnvironmentVariableW(key, ret, sz);
256 done:
257 msi_free(key);
258 return ret;
261 static LPWSTR deformat_literal(FORMAT *format, FORMSTR *str, BOOL *propfound,
262 BOOL *nonprop, int *type)
264 LPCWSTR data = get_formstr_data(format, str);
265 LPWSTR replaced = NULL;
266 char ch = data[0];
268 if (ch == '\\')
270 str->n++;
271 if (str->len == 1)
273 str->len = 0;
274 replaced = NULL;
276 else
278 str->len = 1;
279 replaced = dup_formstr(format, str);
282 else if (ch == '~')
284 if (str->len != 1)
285 replaced = NULL;
286 else
288 replaced = msi_alloc(sizeof(WCHAR));
289 *replaced = '\0';
292 else if (ch == '%' || ch == '#' || ch == '!' || ch == '$')
294 str->n++;
295 str->len--;
297 switch (ch)
299 case '%':
300 replaced = deformat_environment(format, str); break;
301 case '#':
302 replaced = deformat_file(format, str, FALSE); break;
303 case '!':
304 replaced = deformat_file(format, str, TRUE); break;
305 case '$':
306 replaced = deformat_component(format, str); break;
309 *type = FORMAT_LITERAL;
311 else
313 replaced = deformat_property(format, str);
314 *type = FORMAT_LITERAL;
316 if (replaced)
317 *propfound = TRUE;
318 else
319 format->propfailed = TRUE;
322 return replaced;
325 static LPWSTR build_default_format(const MSIRECORD* record)
327 int i;
328 int count;
329 LPWSTR rc, buf;
330 static const WCHAR fmt[] = {'%','i',':',' ','%','s',' ',0};
331 static const WCHAR fmt_null[] = {'%','i',':',' ',' ',0};
332 static const WCHAR fmt_index[] = {'%','i',0};
333 LPCWSTR str;
334 WCHAR index[10];
335 DWORD size, max_len, len;
337 count = MSI_RecordGetFieldCount(record);
339 max_len = MAX_PATH;
340 buf = msi_alloc((max_len + 1) * sizeof(WCHAR));
342 rc = NULL;
343 size = 1;
344 for (i = 1; i <= count; i++)
346 sprintfW(index, fmt_index, i);
347 str = MSI_RecordGetString(record, i);
348 len = (str) ? lstrlenW(str) : 0;
349 len += (sizeof(fmt_null) - 3) + lstrlenW(index);
350 size += len;
352 if (len > max_len)
354 max_len = len;
355 buf = msi_realloc(buf, (max_len + 1) * sizeof(WCHAR));
356 if (!buf) return NULL;
359 if (str)
360 sprintfW(buf, fmt, i, str);
361 else
362 sprintfW(buf, fmt_null, i);
364 if (!rc)
366 rc = msi_alloc(size * sizeof(WCHAR));
367 lstrcpyW(rc, buf);
369 else
371 rc = msi_realloc(rc, size * sizeof(WCHAR));
372 lstrcatW(rc, buf);
376 msi_free(buf);
377 return rc;
380 static BOOL format_is_number(WCHAR x)
382 return ((x >= '0') && (x <= '9'));
385 static BOOL format_str_is_number(LPWSTR str)
387 LPWSTR ptr;
389 for (ptr = str; *ptr; ptr++)
390 if (!format_is_number(*ptr))
391 return FALSE;
393 return TRUE;
396 static BOOL format_is_alpha(WCHAR x)
398 return (!format_is_number(x) && x != '\0' &&
399 x != '[' && x != ']' && x != '{' && x != '}');
402 static BOOL format_is_literal(WCHAR x)
404 return (format_is_alpha(x) || format_is_number(x));
407 static int format_lex(FORMAT *format, FORMSTR **out)
409 int type, len = 1;
410 FORMSTR *str;
411 LPCWSTR data;
412 WCHAR ch;
414 *out = NULL;
416 if (!format->deformatted)
417 return FORMAT_NULL;
419 *out = msi_alloc_zero(sizeof(FORMSTR));
420 if (!*out)
421 return FORMAT_FAIL;
423 str = *out;
424 str->n = format->n;
425 str->len = 1;
426 data = get_formstr_data(format, str);
428 ch = data[0];
429 switch (ch)
431 case '{': type = FORMAT_LBRACE; break;
432 case '}': type = FORMAT_RBRACE; break;
433 case '[': type = FORMAT_LBRACK; break;
434 case ']': type = FORMAT_RBRACK; break;
435 case '~': type = FORMAT_PROPNULL; break;
436 case '\0': type = FORMAT_NULL; break;
438 default:
439 type = 0;
442 if (type)
444 str->type = type;
445 format->n++;
446 return type;
449 if (ch == '\\')
451 while (data[len] && data[len] != ']')
452 len++;
454 type = FORMAT_ESCAPE;
456 else if (format_is_alpha(ch))
458 while (format_is_literal(data[len]))
459 len++;
461 type = FORMAT_LITERAL;
463 else if (format_is_number(ch))
465 while (format_is_number(data[len]))
466 len++;
468 type = FORMAT_NUMBER;
470 if (data[len] != ']')
472 while (format_is_literal(data[len]))
473 len++;
475 type = FORMAT_LITERAL;
478 else
480 ERR("Got unknown character %c(%x)\n", ch, ch);
481 return FORMAT_ERROR;
484 format->n += len;
485 str->len = len;
486 str->type = type;
488 return type;
491 static FORMSTR *format_replace(FORMAT *format, BOOL propfound, BOOL nonprop,
492 int oldsize, int type, LPWSTR replace)
494 FORMSTR *ret;
495 LPWSTR str, ptr;
496 DWORD size = 0;
497 int n;
499 if (replace)
501 if (!*replace)
502 size = 1;
503 else
504 size = lstrlenW(replace);
507 size -= oldsize;
508 size = format->len + size + 1;
510 if (size <= 1)
512 msi_free(format->deformatted);
513 format->deformatted = NULL;
514 format->len = 0;
515 return NULL;
518 str = msi_alloc(size * sizeof(WCHAR));
519 if (!str)
520 return NULL;
522 str[0] = '\0';
523 memcpy(str, format->deformatted, format->n * sizeof(WCHAR));
524 n = format->n;
526 if (replace)
528 if (!*replace)
530 str[n] = '\0';
531 n++;
533 else
535 lstrcpyW(&str[n], replace);
536 n += lstrlenW(replace);
540 ptr = &format->deformatted[format->n + oldsize];
541 memcpy(&str[n], ptr, (lstrlenW(ptr) + 1) * sizeof(WCHAR));
543 msi_free(format->deformatted);
544 format->deformatted = str;
545 format->len = size - 1;
547 if (!replace)
548 return NULL;
550 ret = msi_alloc_zero(sizeof(FORMSTR));
551 if (!ret)
552 return NULL;
554 ret->len = lstrlenW(replace);
555 ret->type = type;
556 ret->n = format->n;
557 ret->propfound = propfound;
558 ret->nonprop = nonprop;
560 return ret;
563 static LPWSTR replace_stack_group(FORMAT *format, STACK *values,
564 BOOL *propfound, BOOL *nonprop,
565 int *oldsize, int *type)
567 LPWSTR replaced = NULL;
568 FORMSTR *content;
569 FORMSTR *node;
570 int n;
572 *nonprop = FALSE;
573 *propfound = FALSE;
575 node = stack_pop(values);
576 n = node->n;
577 *oldsize = node->len;
578 msi_free(node);
580 while ((node = stack_pop(values)))
582 *oldsize += node->len;
584 if (node->nonprop)
585 *nonprop = TRUE;
587 if (node->propfound)
588 *propfound = TRUE;
590 msi_free(node);
593 content = msi_alloc_zero(sizeof(FORMSTR));
594 content->n = n;
595 content->len = *oldsize;
596 content->type = FORMAT_LITERAL;
598 if (!format->groupfailed && (*oldsize == 2 ||
599 (format->propfailed && !*nonprop)))
601 msi_free(content);
602 return NULL;
604 else if (format->deformatted[content->n + 1] == '{' &&
605 format->deformatted[content->n + content->len - 2] == '}')
607 format->groupfailed = FALSE;
608 content->len = 0;
610 else if (*propfound && !*nonprop &&
611 !format->groupfailed && format->groups == 0)
613 content->n++;
614 content->len -= 2;
616 else
618 if (format->groups != 0)
619 format->groupfailed = TRUE;
621 *nonprop = TRUE;
624 replaced = dup_formstr(format, content);
625 *type = content->type;
626 msi_free(content);
628 if (format->groups == 0)
629 format->propfailed = FALSE;
631 return replaced;
634 static LPWSTR replace_stack_prop(FORMAT *format, STACK *values,
635 BOOL *propfound, BOOL *nonprop,
636 int *oldsize, int *type)
638 LPWSTR replaced = NULL;
639 FORMSTR *content;
640 FORMSTR *node;
641 int n;
643 *propfound = FALSE;
644 *nonprop = FALSE;
646 node = stack_pop(values);
647 n = node->n;
648 *oldsize = node->len;
649 *type = stack_peek(values)->type;
650 msi_free(node);
652 while ((node = stack_pop(values)))
654 *oldsize += node->len;
656 if (*type != FORMAT_ESCAPE &&
657 stack_peek(values) && node->type != *type)
658 *type = FORMAT_LITERAL;
660 msi_free(node);
663 content = msi_alloc_zero(sizeof(FORMSTR));
664 content->n = n + 1;
665 content->len = *oldsize - 2;
666 content->type = *type;
668 if (*type == FORMAT_NUMBER)
670 replaced = deformat_index(format, content);
671 if (replaced)
672 *propfound = TRUE;
673 else
674 format->propfailed = TRUE;
676 if (replaced)
677 *type = format_str_is_number(replaced) ?
678 FORMAT_NUMBER : FORMAT_LITERAL;
680 else if (format->package)
682 replaced = deformat_literal(format, content, propfound, nonprop, type);
684 else
686 *nonprop = TRUE;
687 content->n--;
688 content->len += 2;
689 replaced = dup_formstr(format, content);
692 msi_free(content);
693 return replaced;
696 static UINT replace_stack(FORMAT *format, STACK *stack, STACK *values)
698 LPWSTR replaced = NULL;
699 FORMSTR *beg;
700 FORMSTR *top;
701 FORMSTR *node;
702 BOOL propfound = FALSE;
703 BOOL nonprop = FALSE;
704 BOOL group = FALSE;
705 int oldsize = 0;
706 int type, n;
708 node = stack_peek(values);
709 type = node->type;
710 n = node->n;
712 if (type == FORMAT_LBRACK)
713 replaced = replace_stack_prop(format, values, &propfound,
714 &nonprop, &oldsize, &type);
715 else if (type == FORMAT_LBRACE)
717 replaced = replace_stack_group(format, values, &propfound,
718 &nonprop, &oldsize, &type);
719 group = TRUE;
722 format->n = n;
723 beg = format_replace(format, propfound, nonprop, oldsize, type, replaced);
724 if (!beg)
725 return ERROR_SUCCESS;
727 msi_free(replaced);
728 format->n = beg->n + beg->len;
730 if (type == FORMAT_PROPNULL)
731 format->n++;
733 top = stack_peek(stack);
734 if (top)
736 type = top->type;
738 if ((type == FORMAT_LITERAL || type == FORMAT_NUMBER) &&
739 type == beg->type)
741 top->len += beg->len;
743 if (group)
744 top->nonprop = FALSE;
746 if (type == FORMAT_LITERAL)
747 top->nonprop = beg->nonprop;
749 if (beg->propfound)
750 top->propfound = TRUE;
752 msi_free(beg);
753 return ERROR_SUCCESS;
757 stack_push(stack, beg);
758 return ERROR_SUCCESS;
761 static BOOL verify_format(LPWSTR data)
763 int count = 0;
765 while (*data)
767 if (*data == '[' && *(data - 1) != '\\')
768 count++;
769 else if (*data == ']')
770 count--;
772 data++;
775 if (count > 0)
776 return FALSE;
778 return TRUE;
781 static DWORD deformat_string_internal(MSIPACKAGE *package, LPCWSTR ptr,
782 WCHAR** data, DWORD *len,
783 MSIRECORD* record, INT* failcount)
785 FORMAT format;
786 FORMSTR *str = NULL;
787 STACK *stack, *temp;
788 FORMSTR *node;
789 int type;
791 if (!ptr)
793 *data = NULL;
794 *len = 0;
795 return ERROR_SUCCESS;
798 *data = strdupW(ptr);
799 *len = lstrlenW(ptr);
801 ZeroMemory(&format, sizeof(FORMAT));
802 format.package = package;
803 format.record = record;
804 format.deformatted = *data;
805 format.len = *len;
807 stack = create_stack();
808 temp = create_stack();
810 if (!verify_format(*data))
811 return ERROR_SUCCESS;
813 while ((type = format_lex(&format, &str)) != FORMAT_NULL)
815 if (type == FORMAT_LBRACK || type == FORMAT_LBRACE ||
816 type == FORMAT_LITERAL || type == FORMAT_NUMBER ||
817 type == FORMAT_ESCAPE || type == FORMAT_PROPNULL)
819 if (type == FORMAT_LBRACE)
821 format.propfailed = FALSE;
822 format.groups++;
824 else if (type == FORMAT_ESCAPE &&
825 !stack_find(stack, FORMAT_LBRACK))
827 format.n -= str->len - 1;
828 str->len = 1;
831 stack_push(stack, str);
833 else if (type == FORMAT_RBRACK || type == FORMAT_RBRACE)
835 if (type == FORMAT_RBRACE)
836 format.groups--;
838 stack_push(stack, str);
840 if (stack_find(stack, left_type(type)))
844 node = stack_pop(stack);
845 stack_push(temp, node);
846 } while (node->type != left_type(type));
848 replace_stack(&format, stack, temp);
853 *data = format.deformatted;
854 *len = format.len;
856 msi_free(str);
857 free_stack(stack);
858 free_stack(temp);
860 return ERROR_SUCCESS;
863 UINT MSI_FormatRecordW( MSIPACKAGE* package, MSIRECORD* record, LPWSTR buffer,
864 LPDWORD size )
866 LPWSTR deformated;
867 LPWSTR rec;
868 DWORD len;
869 UINT rc = ERROR_INVALID_PARAMETER;
871 TRACE("%p %p %p %i\n", package, record ,buffer, *size);
873 rec = msi_dup_record_field(record,0);
874 if (!rec)
875 rec = build_default_format(record);
877 TRACE("(%s)\n",debugstr_w(rec));
879 deformat_string_internal(package, rec, &deformated, &len, record, NULL);
880 if (buffer)
882 if (*size>len)
884 memcpy(buffer,deformated,len*sizeof(WCHAR));
885 rc = ERROR_SUCCESS;
886 buffer[len] = 0;
888 else
890 if (*size > 0)
892 memcpy(buffer,deformated,(*size)*sizeof(WCHAR));
893 buffer[(*size)-1] = 0;
895 rc = ERROR_MORE_DATA;
898 else
899 rc = ERROR_SUCCESS;
901 *size = len;
903 msi_free(rec);
904 msi_free(deformated);
905 return rc;
908 UINT WINAPI MsiFormatRecordW( MSIHANDLE hInstall, MSIHANDLE hRecord,
909 LPWSTR szResult, LPDWORD sz )
911 UINT r = ERROR_INVALID_HANDLE;
912 MSIPACKAGE *package;
913 MSIRECORD *record;
915 TRACE("%ld %ld %p %p\n", hInstall, hRecord, szResult, sz);
917 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
918 if (!package)
920 HRESULT hr;
921 IWineMsiRemotePackage *remote_package;
922 BSTR value = NULL;
923 DWORD len;
924 awstring wstr;
926 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
927 if (remote_package)
929 len = 0;
930 hr = IWineMsiRemotePackage_FormatRecord( remote_package, hRecord,
931 NULL, &len );
932 if (FAILED(hr))
933 goto done;
935 len++;
936 value = SysAllocStringLen( NULL, len );
937 if (!value)
939 r = ERROR_OUTOFMEMORY;
940 goto done;
943 hr = IWineMsiRemotePackage_FormatRecord( remote_package, hRecord,
944 value, &len );
945 if (FAILED(hr))
946 goto done;
948 wstr.unicode = TRUE;
949 wstr.str.w = szResult;
950 r = msi_strcpy_to_awstring( value, &wstr, sz );
952 done:
953 IWineMsiRemotePackage_Release( remote_package );
954 SysFreeString( value );
956 if (FAILED(hr))
958 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
959 return HRESULT_CODE(hr);
961 return ERROR_FUNCTION_FAILED;
964 return r;
968 record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
970 if (!record)
971 return ERROR_INVALID_HANDLE;
972 if (!sz)
974 msiobj_release( &record->hdr );
975 if (szResult)
976 return ERROR_INVALID_PARAMETER;
977 else
978 return ERROR_SUCCESS;
981 r = MSI_FormatRecordW( package, record, szResult, sz );
982 msiobj_release( &record->hdr );
983 if (package)
984 msiobj_release( &package->hdr );
985 return r;
988 UINT WINAPI MsiFormatRecordA( MSIHANDLE hInstall, MSIHANDLE hRecord,
989 LPSTR szResult, LPDWORD sz )
991 UINT r;
992 DWORD len, save;
993 LPWSTR value;
995 TRACE("%ld %ld %p %p\n", hInstall, hRecord, szResult, sz);
997 if (!hRecord)
998 return ERROR_INVALID_HANDLE;
1000 if (!sz)
1002 if (szResult)
1003 return ERROR_INVALID_PARAMETER;
1004 else
1005 return ERROR_SUCCESS;
1008 r = MsiFormatRecordW( hInstall, hRecord, NULL, &len );
1009 if (r != ERROR_SUCCESS)
1010 return r;
1012 value = msi_alloc(++len * sizeof(WCHAR));
1013 if (!value)
1014 return ERROR_OUTOFMEMORY;
1016 r = MsiFormatRecordW( hInstall, hRecord, value, &len );
1017 if (r != ERROR_SUCCESS)
1018 goto done;
1020 save = len + 1;
1021 len = WideCharToMultiByte(CP_ACP, 0, value, len + 1, NULL, 0, NULL, NULL);
1022 WideCharToMultiByte(CP_ACP, 0, value, len, szResult, *sz, NULL, NULL);
1024 if (szResult && len > *sz)
1026 if (*sz) szResult[*sz - 1] = '\0';
1027 r = ERROR_MORE_DATA;
1030 *sz = save - 1;
1032 done:
1033 msi_free(value);
1034 return r;