[MSI] Sync with Wine Staging 2.16. CORE-13762
[reactos.git] / dll / win32 / msi / format.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2005 Mike McCormack for CodeWeavers
5 * Copyright 2005 Aric Stewart for CodeWeavers
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "msipriv.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(msi);
25
26 /* types arranged by precedence */
27 #define FORMAT_NULL 0x0001
28 #define FORMAT_LITERAL 0x0002
29 #define FORMAT_NUMBER 0x0004
30 #define FORMAT_LBRACK 0x0010
31 #define FORMAT_LBRACE 0x0020
32 #define FORMAT_RBRACK 0x0011
33 #define FORMAT_RBRACE 0x0021
34 #define FORMAT_ESCAPE 0x0040
35 #define FORMAT_PROPNULL 0x0080
36 #define FORMAT_ERROR 0x1000
37 #define FORMAT_FAIL 0x2000
38
39 #define left_type(x) (x & 0xF0)
40
41 typedef struct _tagFORMAT
42 {
43 MSIPACKAGE *package;
44 MSIRECORD *record;
45 LPWSTR deformatted;
46 int len;
47 int n;
48 BOOL propfailed;
49 BOOL groupfailed;
50 int groups;
51 } FORMAT;
52
53 typedef struct _tagFORMSTR
54 {
55 struct list entry;
56 int n;
57 int len;
58 int type;
59 BOOL propfound;
60 BOOL nonprop;
61 } FORMSTR;
62
63 typedef struct _tagSTACK
64 {
65 struct list items;
66 } STACK;
67
68 static STACK *create_stack(void)
69 {
70 STACK *stack = msi_alloc(sizeof(STACK));
71 list_init(&stack->items);
72 return stack;
73 }
74
75 static void free_stack(STACK *stack)
76 {
77 while (!list_empty(&stack->items))
78 {
79 FORMSTR *str = LIST_ENTRY(list_head(&stack->items), FORMSTR, entry);
80 list_remove(&str->entry);
81 msi_free(str);
82 }
83
84 msi_free(stack);
85 }
86
87 static void stack_push(STACK *stack, FORMSTR *str)
88 {
89 list_add_head(&stack->items, &str->entry);
90 }
91
92 static FORMSTR *stack_pop(STACK *stack)
93 {
94 FORMSTR *ret;
95
96 if (list_empty(&stack->items))
97 return NULL;
98
99 ret = LIST_ENTRY(list_head(&stack->items), FORMSTR, entry);
100 list_remove(&ret->entry);
101 return ret;
102 }
103
104 static FORMSTR *stack_find(STACK *stack, int type)
105 {
106 FORMSTR *str;
107
108 LIST_FOR_EACH_ENTRY(str, &stack->items, FORMSTR, entry)
109 {
110 if (str->type == type)
111 return str;
112 }
113
114 return NULL;
115 }
116
117 static FORMSTR *stack_peek(STACK *stack)
118 {
119 return LIST_ENTRY(list_head(&stack->items), FORMSTR, entry);
120 }
121
122 static LPCWSTR get_formstr_data(FORMAT *format, FORMSTR *str)
123 {
124 return &format->deformatted[str->n];
125 }
126
127 static WCHAR *dup_formstr( FORMAT *format, FORMSTR *str, int *ret_len )
128 {
129 WCHAR *val;
130
131 if (!str->len) return NULL;
132 if ((val = msi_alloc( (str->len + 1) * sizeof(WCHAR) )))
133 {
134 memcpy( val, get_formstr_data(format, str), str->len * sizeof(WCHAR) );
135 val[str->len] = 0;
136 *ret_len = str->len;
137 }
138 return val;
139 }
140
141 static WCHAR *deformat_index( FORMAT *format, FORMSTR *str, int *ret_len )
142 {
143 WCHAR *val, *ret;
144 DWORD len;
145 int field;
146
147 if (!(val = msi_alloc( (str->len + 1) * sizeof(WCHAR) ))) return NULL;
148 lstrcpynW(val, get_formstr_data(format, str), str->len + 1);
149 field = atoiW( val );
150 msi_free( val );
151
152 if (MSI_RecordIsNull( format->record, field ) ||
153 MSI_RecordGetStringW( format->record, field, NULL, &len )) return NULL;
154
155 len++;
156 if (!(ret = msi_alloc( len * sizeof(WCHAR) ))) return NULL;
157 ret[0] = 0;
158 if (MSI_RecordGetStringW( format->record, field, ret, &len ))
159 {
160 msi_free( ret );
161 return NULL;
162 }
163 *ret_len = len;
164 return ret;
165 }
166
167 static WCHAR *deformat_property( FORMAT *format, FORMSTR *str, int *ret_len )
168 {
169 WCHAR *prop, *ret;
170 DWORD len = 0;
171 UINT r;
172
173 if (!(prop = msi_alloc( (str->len + 1) * sizeof(WCHAR) ))) return NULL;
174 lstrcpynW( prop, get_formstr_data(format, str), str->len + 1 );
175
176 r = msi_get_property( format->package->db, prop, NULL, &len );
177 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
178 {
179 msi_free( prop );
180 return NULL;
181 }
182 len++;
183 if ((ret = msi_alloc( len * sizeof(WCHAR) )))
184 msi_get_property( format->package->db, prop, ret, &len );
185 msi_free( prop );
186 *ret_len = len;
187 return ret;
188 }
189
190 static WCHAR *deformat_component( FORMAT *format, FORMSTR *str, int *ret_len )
191 {
192 WCHAR *key, *ret;
193 MSICOMPONENT *comp;
194
195 if (!(key = msi_alloc( (str->len + 1) * sizeof(WCHAR) ))) return NULL;
196 lstrcpynW(key, get_formstr_data(format, str), str->len + 1);
197
198 if (!(comp = msi_get_loaded_component( format->package, key )))
199 {
200 msi_free( key );
201 return NULL;
202 }
203 if (comp->Action == INSTALLSTATE_SOURCE)
204 ret = msi_resolve_source_folder( format->package, comp->Directory, NULL );
205 else
206 ret = strdupW( msi_get_target_folder( format->package, comp->Directory ) );
207
208 if (ret) *ret_len = strlenW( ret );
209 else *ret_len = 0;
210 msi_free( key );
211 return ret;
212 }
213
214 static WCHAR *deformat_file( FORMAT *format, FORMSTR *str, BOOL shortname, int *ret_len )
215 {
216 WCHAR *key, *ret = NULL;
217 const MSIFILE *file;
218 DWORD len = 0;
219
220 if (!(key = msi_alloc( (str->len + 1) * sizeof(WCHAR) ))) return NULL;
221 lstrcpynW(key, get_formstr_data(format, str), str->len + 1);
222
223 if (!(file = msi_get_loaded_file( format->package, key ))) goto done;
224 if (!shortname)
225 {
226 if ((ret = strdupW( file->TargetPath ))) len = strlenW( ret );
227 goto done;
228 }
229 if ((len = GetShortPathNameW(file->TargetPath, NULL, 0)) <= 0)
230 {
231 if ((ret = strdupW( file->TargetPath ))) len = strlenW( ret );
232 goto done;
233 }
234 len++;
235 if ((ret = msi_alloc( len * sizeof(WCHAR) )))
236 len = GetShortPathNameW( file->TargetPath, ret, len );
237
238 done:
239 msi_free( key );
240 *ret_len = len;
241 return ret;
242 }
243
244 static WCHAR *deformat_environment( FORMAT *format, FORMSTR *str, int *ret_len )
245 {
246 WCHAR *key, *ret = NULL;
247 DWORD len;
248
249 if (!(key = msi_alloc((str->len + 1) * sizeof(WCHAR)))) return NULL;
250 lstrcpynW(key, get_formstr_data(format, str), str->len + 1);
251
252 if ((len = GetEnvironmentVariableW( key, NULL, 0 )))
253 {
254 len++;
255 if ((ret = msi_alloc( len * sizeof(WCHAR) )))
256 *ret_len = GetEnvironmentVariableW( key, ret, len );
257 }
258 msi_free( key );
259 return ret;
260 }
261
262 static WCHAR *deformat_literal( FORMAT *format, FORMSTR *str, BOOL *propfound,
263 int *type, int *len )
264 {
265 LPCWSTR data = get_formstr_data(format, str);
266 WCHAR *replaced = NULL;
267 char ch = data[0];
268
269 if (ch == '\\')
270 {
271 str->n++;
272 if (str->len == 1)
273 {
274 str->len = 0;
275 replaced = NULL;
276 }
277 else
278 {
279 str->len = 1;
280 replaced = dup_formstr( format, str, len );
281 }
282 }
283 else if (ch == '~')
284 {
285 if (str->len != 1)
286 replaced = NULL;
287 else if ((replaced = msi_alloc( sizeof(WCHAR) )))
288 {
289 *replaced = 0;
290 *len = 0;
291 }
292 }
293 else if (ch == '%' || ch == '#' || ch == '!' || ch == '$')
294 {
295 str->n++;
296 str->len--;
297
298 switch (ch)
299 {
300 case '%':
301 replaced = deformat_environment( format, str, len ); break;
302 case '#':
303 replaced = deformat_file( format, str, FALSE, len ); break;
304 case '!':
305 replaced = deformat_file( format, str, TRUE, len ); break;
306 case '$':
307 replaced = deformat_component( format, str, len ); break;
308 }
309
310 *type = FORMAT_LITERAL;
311 }
312 else
313 {
314 replaced = deformat_property( format, str, len );
315 *type = FORMAT_LITERAL;
316
317 if (replaced)
318 *propfound = TRUE;
319 else
320 format->propfailed = TRUE;
321 }
322
323 return replaced;
324 }
325
326 static LPWSTR build_default_format(const MSIRECORD* record)
327 {
328 int i;
329 int count;
330 WCHAR *rc, buf[26];
331 static const WCHAR fmt[] = {'%','i',':',' ','[','%','i',']',' ',0};
332 DWORD size;
333
334 count = MSI_RecordGetFieldCount(record);
335
336 rc = msi_alloc(1);
337 rc[0] = 0;
338 size = 1;
339
340 for (i = 1; i <= count; i++)
341 {
342 sprintfW(buf, fmt, i, i);
343 size += lstrlenW(buf);
344 rc = msi_realloc(rc, size * sizeof(WCHAR));
345 lstrcatW(rc, buf);
346 }
347
348 return rc;
349 }
350
351 static BOOL format_is_number(WCHAR x)
352 {
353 return ((x >= '0') && (x <= '9'));
354 }
355
356 static BOOL format_str_is_number(LPWSTR str)
357 {
358 LPWSTR ptr;
359
360 for (ptr = str; *ptr; ptr++)
361 if (!format_is_number(*ptr))
362 return FALSE;
363
364 return TRUE;
365 }
366
367 static BOOL format_is_alpha(WCHAR x)
368 {
369 return (!format_is_number(x) && x != '\0' &&
370 x != '[' && x != ']' && x != '{' && x != '}');
371 }
372
373 static BOOL format_is_literal(WCHAR x)
374 {
375 return (format_is_alpha(x) || format_is_number(x));
376 }
377
378 static int format_lex(FORMAT *format, FORMSTR **out)
379 {
380 int type, len = 1;
381 FORMSTR *str;
382 LPCWSTR data;
383 WCHAR ch;
384
385 *out = NULL;
386
387 if (!format->deformatted)
388 return FORMAT_NULL;
389
390 *out = msi_alloc_zero(sizeof(FORMSTR));
391 if (!*out)
392 return FORMAT_FAIL;
393
394 str = *out;
395 str->n = format->n;
396 str->len = 1;
397 data = get_formstr_data(format, str);
398
399 ch = data[0];
400 switch (ch)
401 {
402 case '{': type = FORMAT_LBRACE; break;
403 case '}': type = FORMAT_RBRACE; break;
404 case '[': type = FORMAT_LBRACK; break;
405 case ']': type = FORMAT_RBRACK; break;
406 case '~': type = FORMAT_PROPNULL; break;
407 case '\0': type = FORMAT_NULL; break;
408
409 default:
410 type = 0;
411 }
412
413 if (type)
414 {
415 str->type = type;
416 format->n++;
417 return type;
418 }
419
420 if (ch == '\\')
421 {
422 while (data[len] && data[len] != ']')
423 len++;
424
425 type = FORMAT_ESCAPE;
426 }
427 else if (format_is_alpha(ch))
428 {
429 while (format_is_literal(data[len]))
430 len++;
431
432 type = FORMAT_LITERAL;
433 }
434 else if (format_is_number(ch))
435 {
436 while (format_is_number(data[len]))
437 len++;
438
439 type = FORMAT_NUMBER;
440
441 if (data[len] != ']')
442 {
443 while (format_is_literal(data[len]))
444 len++;
445
446 type = FORMAT_LITERAL;
447 }
448 }
449 else
450 {
451 ERR("Got unknown character %c(%x)\n", ch, ch);
452 return FORMAT_ERROR;
453 }
454
455 format->n += len;
456 str->len = len;
457 str->type = type;
458
459 return type;
460 }
461
462 static FORMSTR *format_replace( FORMAT *format, BOOL propfound, BOOL nonprop,
463 int oldsize, int type, WCHAR *replace, int len )
464 {
465 FORMSTR *ret;
466 LPWSTR str, ptr;
467 DWORD size = 0;
468 int n;
469
470 if (replace)
471 {
472 if (!len)
473 size = 1;
474 else
475 size = len;
476 }
477
478 size -= oldsize;
479 size = format->len + size + 1;
480
481 if (size <= 1)
482 {
483 msi_free(format->deformatted);
484 format->deformatted = NULL;
485 format->len = 0;
486 return NULL;
487 }
488
489 str = msi_alloc(size * sizeof(WCHAR));
490 if (!str)
491 return NULL;
492
493 str[0] = '\0';
494 memcpy(str, format->deformatted, format->n * sizeof(WCHAR));
495 n = format->n;
496
497 if (replace)
498 {
499 if (!len) str[n++] = 0;
500 else
501 {
502 memcpy( str + n, replace, len * sizeof(WCHAR) );
503 n += len;
504 str[n] = 0;
505 }
506 }
507
508 ptr = &format->deformatted[format->n + oldsize];
509 memcpy(&str[n], ptr, (lstrlenW(ptr) + 1) * sizeof(WCHAR));
510
511 msi_free(format->deformatted);
512 format->deformatted = str;
513 format->len = size - 1;
514
515 /* don't reformat the NULL */
516 if (replace && !len)
517 format->n++;
518
519 if (!replace)
520 return NULL;
521
522 ret = msi_alloc_zero(sizeof(FORMSTR));
523 if (!ret)
524 return NULL;
525
526 ret->len = len;
527 ret->type = type;
528 ret->n = format->n;
529 ret->propfound = propfound;
530 ret->nonprop = nonprop;
531
532 return ret;
533 }
534
535 static WCHAR *replace_stack_group( FORMAT *format, STACK *values,
536 BOOL *propfound, BOOL *nonprop,
537 int *oldsize, int *type, int *len )
538 {
539 WCHAR *replaced;
540 FORMSTR *content, *node;
541 int n;
542
543 *nonprop = FALSE;
544 *propfound = FALSE;
545
546 node = stack_pop(values);
547 n = node->n;
548 *oldsize = node->len;
549 msi_free(node);
550
551 while ((node = stack_pop(values)))
552 {
553 *oldsize += node->len;
554
555 if (node->nonprop)
556 *nonprop = TRUE;
557
558 if (node->propfound)
559 *propfound = TRUE;
560
561 msi_free(node);
562 }
563
564 content = msi_alloc_zero(sizeof(FORMSTR));
565 content->n = n;
566 content->len = *oldsize;
567 content->type = FORMAT_LITERAL;
568
569 if (!format->groupfailed && (*oldsize == 2 ||
570 (format->propfailed && !*nonprop)))
571 {
572 msi_free(content);
573 return NULL;
574 }
575 else if (format->deformatted[content->n + 1] == '{' &&
576 format->deformatted[content->n + content->len - 2] == '}')
577 {
578 format->groupfailed = FALSE;
579 content->len = 0;
580 }
581 else if (*propfound && !*nonprop &&
582 !format->groupfailed && format->groups == 0)
583 {
584 content->n++;
585 content->len -= 2;
586 }
587 else
588 {
589 if (format->groups != 0)
590 format->groupfailed = TRUE;
591
592 *nonprop = TRUE;
593 }
594
595 replaced = dup_formstr( format, content, len );
596 *type = content->type;
597 msi_free(content);
598
599 if (format->groups == 0)
600 format->propfailed = FALSE;
601
602 return replaced;
603 }
604
605 static WCHAR *replace_stack_prop( FORMAT *format, STACK *values,
606 BOOL *propfound, BOOL *nonprop,
607 int *oldsize, int *type, int *len )
608 {
609 WCHAR *replaced;
610 FORMSTR *content, *node;
611 int n;
612
613 *propfound = FALSE;
614 *nonprop = FALSE;
615
616 node = stack_pop(values);
617 n = node->n;
618 *oldsize = node->len;
619 *type = stack_peek(values)->type;
620 msi_free(node);
621
622 while ((node = stack_pop(values)))
623 {
624 *oldsize += node->len;
625
626 if (*type != FORMAT_ESCAPE &&
627 stack_peek(values) && node->type != *type)
628 *type = FORMAT_LITERAL;
629
630 msi_free(node);
631 }
632
633 content = msi_alloc_zero(sizeof(FORMSTR));
634 content->n = n + 1;
635 content->len = *oldsize - 2;
636 content->type = *type;
637
638 if (*type == FORMAT_NUMBER && format->record)
639 {
640 replaced = deformat_index( format, content, len );
641 if (replaced)
642 *propfound = TRUE;
643 else
644 format->propfailed = TRUE;
645
646 if (replaced)
647 *type = format_str_is_number(replaced) ?
648 FORMAT_NUMBER : FORMAT_LITERAL;
649 }
650 else if (format->package)
651 {
652 replaced = deformat_literal( format, content, propfound, type, len );
653 }
654 else
655 {
656 *nonprop = TRUE;
657 content->n--;
658 content->len += 2;
659 replaced = dup_formstr( format, content, len );
660 }
661 msi_free(content);
662 return replaced;
663 }
664
665 static UINT replace_stack(FORMAT *format, STACK *stack, STACK *values)
666 {
667 WCHAR *replaced = NULL;
668 FORMSTR *beg, *top, *node;
669 BOOL propfound = FALSE, nonprop = FALSE, group = FALSE;
670 int type, n, len = 0, oldsize = 0;
671
672 node = stack_peek(values);
673 type = node->type;
674 n = node->n;
675
676 if (type == FORMAT_LBRACK)
677 replaced = replace_stack_prop( format, values, &propfound,
678 &nonprop, &oldsize, &type, &len );
679 else if (type == FORMAT_LBRACE)
680 {
681 replaced = replace_stack_group( format, values, &propfound,
682 &nonprop, &oldsize, &type, &len );
683 group = TRUE;
684 }
685
686 format->n = n;
687 beg = format_replace( format, propfound, nonprop, oldsize, type, replaced, len );
688 msi_free(replaced);
689 if (!beg)
690 return ERROR_SUCCESS;
691
692 format->n = beg->n + beg->len;
693
694 top = stack_peek(stack);
695 if (top)
696 {
697 type = top->type;
698
699 if ((type == FORMAT_LITERAL || type == FORMAT_NUMBER) &&
700 type == beg->type)
701 {
702 top->len += beg->len;
703
704 if (group)
705 top->nonprop = FALSE;
706
707 if (type == FORMAT_LITERAL)
708 top->nonprop = beg->nonprop;
709
710 if (beg->propfound)
711 top->propfound = TRUE;
712
713 msi_free(beg);
714 return ERROR_SUCCESS;
715 }
716 }
717
718 stack_push(stack, beg);
719 return ERROR_SUCCESS;
720 }
721
722 static BOOL verify_format(LPWSTR data)
723 {
724 int count = 0;
725
726 while (*data)
727 {
728 if (*data == '[' && *(data - 1) != '\\')
729 count++;
730 else if (*data == ']')
731 count--;
732
733 data++;
734 }
735
736 if (count > 0)
737 return FALSE;
738
739 return TRUE;
740 }
741
742 static DWORD deformat_string_internal(MSIPACKAGE *package, LPCWSTR ptr,
743 WCHAR** data, DWORD *len,
744 MSIRECORD* record)
745 {
746 FORMAT format;
747 FORMSTR *str = NULL;
748 STACK *stack, *temp;
749 FORMSTR *node;
750 int type;
751
752 if (!ptr)
753 {
754 *data = NULL;
755 *len = 0;
756 return ERROR_SUCCESS;
757 }
758
759 *data = strdupW(ptr);
760 *len = lstrlenW(ptr);
761
762 ZeroMemory(&format, sizeof(FORMAT));
763 format.package = package;
764 format.record = record;
765 format.deformatted = *data;
766 format.len = *len;
767
768 if (!verify_format(*data))
769 return ERROR_SUCCESS;
770
771 stack = create_stack();
772 temp = create_stack();
773
774 while ((type = format_lex(&format, &str)) != FORMAT_NULL)
775 {
776 if (type == FORMAT_LBRACK || type == FORMAT_LBRACE ||
777 type == FORMAT_LITERAL || type == FORMAT_NUMBER ||
778 type == FORMAT_ESCAPE || type == FORMAT_PROPNULL)
779 {
780 if (type == FORMAT_LBRACE)
781 {
782 format.propfailed = FALSE;
783 format.groups++;
784 }
785 else if (type == FORMAT_ESCAPE &&
786 !stack_find(stack, FORMAT_LBRACK))
787 {
788 format.n -= str->len - 1;
789 str->len = 1;
790 }
791
792 stack_push(stack, str);
793 }
794 else if (type == FORMAT_RBRACK || type == FORMAT_RBRACE)
795 {
796 if (type == FORMAT_RBRACE)
797 format.groups--;
798
799 stack_push(stack, str);
800
801 if (stack_find(stack, left_type(type)))
802 {
803 do
804 {
805 node = stack_pop(stack);
806 stack_push(temp, node);
807 } while (node->type != left_type(type));
808
809 replace_stack(&format, stack, temp);
810 }
811 }
812 }
813
814 *data = format.deformatted;
815 *len = format.len;
816
817 msi_free(str);
818 free_stack(stack);
819 free_stack(temp);
820
821 return ERROR_SUCCESS;
822 }
823
824 UINT MSI_FormatRecordW( MSIPACKAGE* package, MSIRECORD* record, LPWSTR buffer,
825 LPDWORD size )
826 {
827 WCHAR *format, *deformated = NULL;
828 UINT rc = ERROR_INVALID_PARAMETER;
829 DWORD len;
830 MSIRECORD *record_deformated;
831 int field_count, i;
832
833 TRACE("%p %p %p %p\n", package, record, buffer, size);
834 dump_record(record);
835
836 if (!(format = msi_dup_record_field( record, 0 )))
837 format = build_default_format( record );
838
839 field_count = MSI_RecordGetFieldCount(record);
840 record_deformated = MSI_CloneRecord(record);
841 if (!record_deformated)
842 {
843 rc = ERROR_OUTOFMEMORY;
844 goto end;
845 }
846 MSI_RecordSetStringW(record_deformated, 0, format);
847 for (i = 1; i <= field_count; i++)
848 {
849 if (MSI_RecordGetString(record, i))
850 {
851 deformat_string_internal(package, MSI_RecordGetString(record, i), &deformated, &len, NULL);
852 MSI_RecordSetStringW(record_deformated, i, deformated);
853 msi_free(deformated);
854 }
855 }
856
857 deformat_string_internal(package, format, &deformated, &len, record_deformated);
858 if (buffer)
859 {
860 if (*size>len)
861 {
862 memcpy(buffer,deformated,len*sizeof(WCHAR));
863 rc = ERROR_SUCCESS;
864 buffer[len] = 0;
865 }
866 else
867 {
868 if (*size > 0)
869 {
870 memcpy(buffer,deformated,(*size)*sizeof(WCHAR));
871 buffer[(*size)-1] = 0;
872 }
873 rc = ERROR_MORE_DATA;
874 }
875 }
876 else rc = ERROR_SUCCESS;
877
878 *size = len;
879 msiobj_release(&record_deformated->hdr);
880 end:
881 msi_free( format );
882 msi_free( deformated );
883 return rc;
884 }
885
886 UINT WINAPI MsiFormatRecordW( MSIHANDLE hInstall, MSIHANDLE hRecord,
887 LPWSTR szResult, LPDWORD sz )
888 {
889 UINT r = ERROR_INVALID_HANDLE;
890 MSIPACKAGE *package;
891 MSIRECORD *record;
892
893 TRACE("%d %d %p %p\n", hInstall, hRecord, szResult, sz);
894
895 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
896 if (!package)
897 {
898 HRESULT hr;
899 IWineMsiRemotePackage *remote_package;
900 BSTR value = NULL;
901 awstring wstr;
902
903 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
904 if (remote_package)
905 {
906 hr = IWineMsiRemotePackage_FormatRecord( remote_package, hRecord,
907 &value );
908 if (FAILED(hr))
909 goto done;
910
911 wstr.unicode = TRUE;
912 wstr.str.w = szResult;
913 r = msi_strcpy_to_awstring( value, SysStringLen(value), &wstr, sz );
914
915 done:
916 IWineMsiRemotePackage_Release( remote_package );
917 SysFreeString( value );
918
919 if (FAILED(hr))
920 {
921 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
922 return HRESULT_CODE(hr);
923
924 return ERROR_FUNCTION_FAILED;
925 }
926
927 return r;
928 }
929 }
930
931 record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
932
933 if (!record)
934 return ERROR_INVALID_HANDLE;
935 if (!sz)
936 {
937 msiobj_release( &record->hdr );
938 if (szResult)
939 return ERROR_INVALID_PARAMETER;
940 else
941 return ERROR_SUCCESS;
942 }
943
944 r = MSI_FormatRecordW( package, record, szResult, sz );
945 msiobj_release( &record->hdr );
946 if (package)
947 msiobj_release( &package->hdr );
948 return r;
949 }
950
951 UINT WINAPI MsiFormatRecordA( MSIHANDLE hInstall, MSIHANDLE hRecord,
952 LPSTR szResult, LPDWORD sz )
953 {
954 UINT r;
955 DWORD len, save;
956 LPWSTR value;
957
958 TRACE("%d %d %p %p\n", hInstall, hRecord, szResult, sz);
959
960 if (!hRecord)
961 return ERROR_INVALID_HANDLE;
962
963 if (!sz)
964 {
965 if (szResult)
966 return ERROR_INVALID_PARAMETER;
967 else
968 return ERROR_SUCCESS;
969 }
970
971 r = MsiFormatRecordW( hInstall, hRecord, NULL, &len );
972 if (r != ERROR_SUCCESS)
973 return r;
974
975 value = msi_alloc(++len * sizeof(WCHAR));
976 if (!value)
977 return ERROR_OUTOFMEMORY;
978
979 r = MsiFormatRecordW( hInstall, hRecord, value, &len );
980 if (r != ERROR_SUCCESS)
981 goto done;
982
983 save = len + 1;
984 len = WideCharToMultiByte(CP_ACP, 0, value, len + 1, NULL, 0, NULL, NULL);
985 WideCharToMultiByte(CP_ACP, 0, value, len, szResult, *sz, NULL, NULL);
986
987 if (szResult && len > *sz)
988 {
989 if (*sz) szResult[*sz - 1] = '\0';
990 r = ERROR_MORE_DATA;
991 }
992
993 *sz = save - 1;
994
995 done:
996 msi_free(value);
997 return r;
998 }
999
1000 /* wrapper to resist a need for a full rewrite right now */
1001 DWORD deformat_string( MSIPACKAGE *package, const WCHAR *fmt, WCHAR **data )
1002 {
1003 DWORD len;
1004 MSIRECORD *rec;
1005
1006 *data = NULL;
1007 if (!fmt) return 0;
1008 if (!(rec = MSI_CreateRecord( 1 ))) return 0;
1009
1010 MSI_RecordSetStringW( rec, 0, fmt );
1011 MSI_FormatRecordW( package, rec, NULL, &len );
1012 if (!(*data = msi_alloc( ++len * sizeof(WCHAR) )))
1013 {
1014 msiobj_release( &rec->hdr );
1015 return 0;
1016 }
1017 MSI_FormatRecordW( package, rec, *data, &len );
1018 msiobj_release( &rec->hdr );
1019 return len;
1020 }