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
23 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/msiformatrecord.asp
34 #include "wine/debug.h"
38 #include "wine/unicode.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
43 static DWORD
deformat_string_internal(MSIPACKAGE
*package
, LPCWSTR ptr
,
44 WCHAR
** data
, DWORD len
, MSIRECORD
* record
,
48 static LPWSTR
build_default_format(MSIRECORD
* record
)
53 static const WCHAR fmt
[] = {'%','i',':',' ','%','s',' ',0};
54 static const WCHAR fmt_null
[] = {'%','i',':',' ',' ',0};
55 static const WCHAR fmt_index
[] = {'%','i',0};
58 DWORD size
, max_len
, len
;
60 count
= MSI_RecordGetFieldCount(record
);
63 buf
= msi_alloc((max_len
+ 1) * sizeof(WCHAR
));
67 for (i
= 1; i
<= count
; i
++)
69 sprintfW(index
,fmt_index
,i
);
70 str
= MSI_RecordGetString(record
, i
);
71 len
= (str
) ? lstrlenW(str
) : 0;
72 len
+= (sizeof(fmt_null
) - 3) + lstrlenW(index
);
78 buf
= msi_realloc(buf
, (max_len
+ 1) * sizeof(WCHAR
));
79 if (!buf
) return NULL
;
83 sprintfW(buf
,fmt
,i
,str
);
85 sprintfW(buf
,fmt_null
,i
);
89 rc
= msi_alloc(size
* sizeof(WCHAR
));
94 rc
= msi_realloc(rc
, size
* sizeof(WCHAR
));
102 static const WCHAR
* scanW(LPCWSTR buf
, WCHAR token
, DWORD len
)
105 for (i
= 0; i
< len
; i
++)
111 /* break out helper functions for deformating */
112 static LPWSTR
deformat_component(MSIPACKAGE
* package
, LPCWSTR key
, DWORD
* sz
)
121 FIXME("component key %s\n", debugstr_w(key
));
122 comp
= get_loaded_component(package
,key
);
125 value
= resolve_folder(package
, comp
->Directory
, FALSE
, FALSE
, NULL
);
126 *sz
= (strlenW(value
)) * sizeof(WCHAR
);
132 static LPWSTR
deformat_file(MSIPACKAGE
* package
, LPCWSTR key
, DWORD
* sz
,
143 file
= get_loaded_file( package
, key
);
148 value
= strdupW( file
->TargetPath
);
149 *sz
= (strlenW(value
)) * sizeof(WCHAR
);
154 size
= GetShortPathNameW( file
->TargetPath
, NULL
, 0 );
158 *sz
= (size
-1) * sizeof (WCHAR
);
160 value
= msi_alloc(size
* sizeof(WCHAR
));
161 GetShortPathNameW( file
->TargetPath
, value
, size
);
165 ERR("Unable to get ShortPath size (%s)\n",
166 debugstr_w( file
->TargetPath
) );
167 value
= strdupW( file
->TargetPath
);
168 *sz
= (lstrlenW(value
)) * sizeof(WCHAR
);
176 static LPWSTR
deformat_environment(MSIPACKAGE
* package
, LPCWSTR key
,
182 sz
= GetEnvironmentVariableW(key
,NULL
,0);
186 value
= msi_alloc(sz
* sizeof(WCHAR
));
187 GetEnvironmentVariableW(key
,value
,sz
);
188 *chunk
= (strlenW(value
)) * sizeof(WCHAR
);
192 ERR("Unknown environment variable %s\n", debugstr_w(key
));
200 static LPWSTR
deformat_NULL(DWORD
* chunk
)
204 value
= msi_alloc(sizeof(WCHAR
)*2);
206 *chunk
= sizeof(WCHAR
);
210 static LPWSTR
deformat_escape(LPCWSTR key
, DWORD
* chunk
)
214 value
= msi_alloc(sizeof(WCHAR
)*2);
216 *chunk
= sizeof(WCHAR
);
222 static BOOL
is_key_number(LPCWSTR key
)
228 while (isdigitW(key
[index
])) index
++;
235 static LPWSTR
deformat_index(MSIRECORD
* record
, LPCWSTR key
, DWORD
* chunk
)
241 TRACE("record index %i\n",index
);
242 value
= msi_dup_record_field(record
,index
);
244 *chunk
= strlenW(value
) * sizeof(WCHAR
);
253 static LPWSTR
deformat_property(MSIPACKAGE
* package
, LPCWSTR key
, DWORD
* chunk
)
260 value
= msi_dup_property( package
, key
);
263 *chunk
= (strlenW(value
)) * sizeof(WCHAR
);
269 * Groups cannot be nested. They are just treated as from { to next }
271 static BOOL
find_next_group(LPCWSTR source
, DWORD len_remaining
,
272 LPWSTR
*group
, LPCWSTR
*mark
,
278 *mark
= scanW(source
,'{',len_remaining
);
282 for (i
= 1; (*mark
- source
) + i
< len_remaining
; i
++)
284 if ((*mark
)[i
] == '}')
293 *mark2
= &(*mark
)[i
];
296 *group
= msi_alloc(i
*sizeof(WCHAR
));
299 memcpy(*group
,&(*mark
)[1],i
*sizeof(WCHAR
));
302 TRACE("Found group %s\n",debugstr_w(*group
));
307 static BOOL
find_next_outermost_key(LPCWSTR source
, DWORD len_remaining
,
308 LPWSTR
*key
, LPCWSTR
*mark
, LPCWSTR
* mark2
,
315 *mark
= scanW(source
,'[',len_remaining
);
322 for (i
= 1; (*mark
- source
) + i
< len_remaining
&& count
> 0; i
++)
324 if ((*mark
)[i
] == '[' && (*mark
)[i
-1] != '\\')
330 else if ((*mark
)[i
] == ']' && (*mark
)[i
-1] != '\\')
339 *mark2
= &(*mark
)[i
-1];
342 *key
= msi_alloc(i
*sizeof(WCHAR
));
343 /* do not have the [] in the key */
345 memcpy(*key
,&(*mark
)[1],i
*sizeof(WCHAR
));
348 TRACE("Found Key %s\n",debugstr_w(*key
));
352 static LPWSTR
deformat_group(MSIPACKAGE
* package
, LPWSTR group
, DWORD len
,
353 MSIRECORD
* record
, DWORD
* size
)
360 static const WCHAR fmt
[] = {'{','%','s','}',0};
363 if (!group
|| group
[0] == 0)
368 /* if no [] then group is returned as is */
370 if (!find_next_outermost_key(group
, len
, &key
, &mark
, &mark2
, &nested
))
372 *size
= (len
+2)*sizeof(WCHAR
);
373 value
= msi_alloc(*size
);
374 sprintfW(value
,fmt
,group
);
375 /* do not return size of the null at the end */
376 *size
= (len
+1)*sizeof(WCHAR
);
382 sz
= deformat_string_internal(package
, group
, &value
, strlenW(group
),
386 *size
= sz
* sizeof(WCHAR
);
389 else if (failcount
< 0)
393 v2
= msi_alloc((sz
+2)*sizeof(WCHAR
));
395 memcpy(&v2
[1],value
,sz
*sizeof(WCHAR
));
399 *size
= (sz
+2)*sizeof(WCHAR
);
413 * return is also in WCHARs
415 static DWORD
deformat_string_internal(MSIPACKAGE
*package
, LPCWSTR ptr
,
416 WCHAR
** data
, DWORD len
, MSIRECORD
* record
,
420 LPCWSTR mark2
= NULL
;
426 LPBYTE newdata
= NULL
;
427 const WCHAR
* progress
= NULL
;
432 TRACE("Deformatting NULL string\n");
437 TRACE("Starting with %s\n",debugstr_wn(ptr
,len
));
439 /* scan for special characters... fast exit */
440 if ((!scanW(ptr
,'[',len
) || (scanW(ptr
,'[',len
) && !scanW(ptr
,']',len
))) &&
441 (scanW(ptr
,'{',len
) && !scanW(ptr
,'}',len
)))
444 *data
= msi_alloc((len
*sizeof(WCHAR
)));
445 memcpy(*data
,ptr
,len
*sizeof(WCHAR
));
446 TRACE("Returning %s\n",debugstr_wn(*data
,len
));
452 while (progress
- ptr
< len
)
454 /* seek out first group if existing */
455 if (find_next_group(progress
, len
- (progress
- ptr
), &key
,
458 value
= deformat_group(package
, key
, strlenW(key
)+1, record
,
464 /* formatted string located */
465 else if (!find_next_outermost_key(progress
, len
- (progress
- ptr
),
466 &key
, &mark
, &mark2
, &nested
))
470 TRACE("after value %s\n", debugstr_wn((LPWSTR
)newdata
,
471 size
/sizeof(WCHAR
)));
472 chunk
= (len
- (progress
- ptr
)) * sizeof(WCHAR
);
473 TRACE("after chunk is %i + %i\n",size
,chunk
);
475 nd2
= msi_realloc(newdata
,(size
+chunk
));
477 nd2
= msi_alloc(chunk
);
480 memcpy(&newdata
[size
],progress
,chunk
);
485 if (mark
!= progress
)
488 DWORD old_size
= size
;
489 INT cnt
= (mark
- progress
);
490 TRACE("%i (%i) characters before marker\n",cnt
,(mark
-progress
));
491 size
+= cnt
* sizeof(WCHAR
);
493 tgt
= msi_alloc(size
);
495 tgt
= msi_realloc(newdata
,size
);
497 memcpy(&newdata
[old_size
],progress
,(cnt
* sizeof(WCHAR
)));
504 TRACE("Nested key... %s\n",debugstr_w(key
));
505 deformat_string_internal(package
, key
, &value
, strlenW(key
)+1,
512 TRACE("Current %s .. %s\n",debugstr_wn((LPWSTR
)newdata
,
513 size
/sizeof(WCHAR
)),debugstr_w(key
));
517 /* only deformat number indexs */
518 if (key
&& is_key_number(key
))
520 value
= deformat_index(record
,key
,&chunk
);
521 if (!chunk
&& failcount
&& *failcount
>= 0)
530 DWORD keylen
= strlenW(key
);
531 chunk
= (keylen
+ 2)*sizeof(WCHAR
);
532 value
= msi_alloc(chunk
);
534 memcpy(&value
[1],key
,keylen
*sizeof(WCHAR
));
535 value
[1+keylen
] = ']';
542 if (key
) switch (key
[0])
545 value
= deformat_NULL(&chunk
);
548 value
= deformat_component(package
,&key
[1],&chunk
);
551 value
= deformat_file(package
,&key
[1], &chunk
, FALSE
);
553 case '!': /* should be short path */
554 value
= deformat_file(package
,&key
[1], &chunk
, TRUE
);
557 value
= deformat_escape(&key
[1],&chunk
);
560 value
= deformat_environment(package
,&key
[1],&chunk
);
563 /* index keys cannot be nested */
564 if (is_key_number(key
))
566 value
= deformat_index(record
,key
,&chunk
);
569 static const WCHAR fmt
[] = {'[','%','s',']',0};
570 value
= msi_alloc(10);
571 sprintfW(value
,fmt
,key
);
572 chunk
= strlenW(value
)*sizeof(WCHAR
);
575 value
= deformat_property(package
,key
,&chunk
);
585 TRACE("value %s, chunk %i size %i\n",debugstr_w((LPWSTR
)value
),
588 nd2
= msi_realloc(newdata
,(size
+ chunk
));
590 nd2
= msi_alloc(chunk
);
592 memcpy(&newdata
[size
],value
,chunk
);
596 else if (failcount
&& *failcount
>=0 )
602 TRACE("after everything %s\n",debugstr_wn((LPWSTR
)newdata
,
603 size
/sizeof(WCHAR
)));
605 *data
= (LPWSTR
)newdata
;
606 return size
/ sizeof(WCHAR
);
610 UINT
MSI_FormatRecordW( MSIPACKAGE
* package
, MSIRECORD
* record
, LPWSTR buffer
,
616 UINT rc
= ERROR_INVALID_PARAMETER
;
618 TRACE("%p %p %p %i\n", package
, record
,buffer
, *size
);
620 rec
= msi_dup_record_field(record
,0);
622 rec
= build_default_format(record
);
624 TRACE("(%s)\n",debugstr_w(rec
));
626 len
= deformat_string_internal(package
,rec
,&deformated
,strlenW(rec
),
633 memcpy(buffer
,deformated
,len
*sizeof(WCHAR
));
641 memcpy(buffer
,deformated
,(*size
)*sizeof(WCHAR
));
642 buffer
[(*size
)-1] = 0;
644 rc
= ERROR_MORE_DATA
;
653 msi_free(deformated
);
657 UINT
MSI_FormatRecordA( MSIPACKAGE
* package
, MSIRECORD
* record
, LPSTR buffer
,
663 UINT rc
= ERROR_INVALID_PARAMETER
;
665 TRACE("%p %p %p %i\n", package
, record
,buffer
, *size
);
667 rec
= msi_dup_record_field(record
,0);
669 rec
= build_default_format(record
);
671 TRACE("(%s)\n",debugstr_w(rec
));
673 len
= deformat_string_internal(package
,rec
,&deformated
,strlenW(rec
),
675 /* If len is zero then WideCharToMultiByte will return 0 indicating
676 * failure, but that will do just as well since we are ignoring
679 lenA
= WideCharToMultiByte(CP_ACP
,0,deformated
,len
,NULL
,0,NULL
,NULL
);
684 WideCharToMultiByte(CP_ACP
,0,deformated
,len
,buffer
,*size
,NULL
, NULL
);
692 rc
= ERROR_MORE_DATA
;
694 buffer
[(*size
)-1] = 0;
703 msi_free(deformated
);
708 UINT WINAPI
MsiFormatRecordW( MSIHANDLE hInstall
, MSIHANDLE hRecord
,
709 LPWSTR szResult
, DWORD
*sz
)
711 UINT r
= ERROR_INVALID_HANDLE
;
715 TRACE("%ld %ld %p %p\n", hInstall
, hRecord
, szResult
, sz
);
717 record
= msihandle2msiinfo( hRecord
, MSIHANDLETYPE_RECORD
);
720 return ERROR_INVALID_HANDLE
;
723 msiobj_release( &record
->hdr
);
725 return ERROR_INVALID_PARAMETER
;
727 return ERROR_SUCCESS
;
730 package
= msihandle2msiinfo( hInstall
, MSIHANDLETYPE_PACKAGE
);
732 r
= MSI_FormatRecordW( package
, record
, szResult
, sz
);
733 msiobj_release( &record
->hdr
);
735 msiobj_release( &package
->hdr
);
739 UINT WINAPI
MsiFormatRecordA( MSIHANDLE hInstall
, MSIHANDLE hRecord
,
740 LPSTR szResult
, DWORD
*sz
)
742 UINT r
= ERROR_INVALID_HANDLE
;
743 MSIPACKAGE
*package
= NULL
;
744 MSIRECORD
*record
= NULL
;
746 TRACE("%ld %ld %p %p\n", hInstall
, hRecord
, szResult
, sz
);
748 record
= msihandle2msiinfo( hRecord
, MSIHANDLETYPE_RECORD
);
751 return ERROR_INVALID_HANDLE
;
754 msiobj_release( &record
->hdr
);
756 return ERROR_INVALID_PARAMETER
;
758 return ERROR_SUCCESS
;
761 package
= msihandle2msiinfo( hInstall
, MSIHANDLETYPE_PACKAGE
);
763 r
= MSI_FormatRecordA( package
, record
, szResult
, sz
);
764 msiobj_release( &record
->hdr
);
766 msiobj_release( &package
->hdr
);