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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
44 static DWORD deformat_string_internal(MSIPACKAGE *package, LPCWSTR ptr,
45 WCHAR** data, DWORD len, MSIRECORD* record,
49 static LPWSTR build_default_format(MSIRECORD* record)
54 static const WCHAR fmt[] = {'%','i',':',' ','[','%','i',']',' ',0};
57 count = MSI_RecordGetFieldCount(record);
59 rc = HeapAlloc(GetProcessHeap(),0,(11*count)*sizeof(WCHAR));
61 for (i = 1; i <= count; i++)
63 sprintfW(buf,fmt,i,i);
69 static const WCHAR* scanW(LPCWSTR buf, WCHAR token, DWORD len)
72 for (i = 0; i < len; i++)
78 /* break out helper functions for deformating */
79 static LPWSTR deformat_component(MSIPACKAGE* package, LPCWSTR key, DWORD* sz)
88 ERR("POORLY HANDLED DEFORMAT.. [$componentkey] \n");
89 comp = get_loaded_component(package,key);
92 value = resolve_folder(package, comp->Directory, FALSE, FALSE, NULL);
93 *sz = (strlenW(value)) * sizeof(WCHAR);
99 static LPWSTR deformat_file(MSIPACKAGE* package, LPCWSTR key, DWORD* sz,
110 index = get_loaded_file(package,key);
115 value = strdupW(package->files[index].TargetPath);
116 *sz = (strlenW(value)) * sizeof(WCHAR);
121 size = GetShortPathNameW(package->files[index].TargetPath, NULL, 0);
125 *sz = (size-1) * sizeof (WCHAR);
127 value = HeapAlloc(GetProcessHeap(),0,size * sizeof(WCHAR));
128 GetShortPathNameW(package->files[index].TargetPath, value,
133 ERR("Unable to get ShortPath size (%s)\n",
134 debugstr_w(package->files[index].TargetPath));
144 static LPWSTR deformat_environment(MSIPACKAGE* package, LPCWSTR key,
150 sz = GetEnvironmentVariableW(key,NULL,0);
154 value = HeapAlloc(GetProcessHeap(),0,sz * sizeof(WCHAR));
155 GetEnvironmentVariableW(&key[1],value,sz);
156 *chunk = (strlenW(value)) * sizeof(WCHAR);
160 ERR("Unknown environment variable %s\n", debugstr_w(key));
168 static LPWSTR deformat_NULL(DWORD* chunk)
172 value = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*2);
174 *chunk = sizeof(WCHAR);
178 static LPWSTR deformat_escape(LPCWSTR key, DWORD* chunk)
182 value = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*2);
184 *chunk = sizeof(WCHAR);
190 static BOOL is_key_number(LPCWSTR key)
196 while (isdigitW(key[index])) index++;
203 static LPWSTR deformat_index(MSIRECORD* record, LPCWSTR key, DWORD* chunk )
209 TRACE("record index %i\n",index);
210 value = load_dynamic_stringW(record,index);
212 *chunk = strlenW(value) * sizeof(WCHAR);
221 static LPWSTR deformat_property(MSIPACKAGE* package, LPCWSTR key, DWORD* chunk)
229 value = load_dynamic_property(package,key, &rc);
231 if (rc == ERROR_SUCCESS)
232 *chunk = (strlenW(value)) * sizeof(WCHAR);
238 * Groups cannot be nested. They are just treated as from { to next }
240 static BOOL find_next_group(LPCWSTR source, DWORD len_remaining,
241 LPWSTR *group, LPCWSTR *mark,
247 *mark = scanW(source,'{',len_remaining);
251 for (i = 1; (*mark - source) + i < len_remaining; i++)
253 if ((*mark)[i] == '}')
262 *mark2 = &(*mark)[i];
265 *group = HeapAlloc(GetProcessHeap(),0,i*sizeof(WCHAR));
268 memcpy(*group,&(*mark)[1],i*sizeof(WCHAR));
271 TRACE("Found group %s\n",debugstr_w(*group));
276 static BOOL find_next_outermost_key(LPCWSTR source, DWORD len_remaining,
277 LPWSTR *key, LPCWSTR *mark, LPCWSTR* mark2,
284 *mark = scanW(source,'[',len_remaining);
291 for (i = 1; (*mark - source) + i < len_remaining && count > 0; i++)
293 if ((*mark)[i] == '[' && (*mark)[i-1] != '\\')
299 else if ((*mark)[i] == ']' && (*mark)[i-1] != '\\')
308 *mark2 = &(*mark)[i-1];
311 *key = HeapAlloc(GetProcessHeap(),0,i*sizeof(WCHAR));
312 /* do not have the [] in the key */
314 memcpy(*key,&(*mark)[1],i*sizeof(WCHAR));
317 TRACE("Found Key %s\n",debugstr_w(*key));
321 static LPWSTR deformat_group(MSIPACKAGE* package, LPWSTR group, DWORD len,
322 MSIRECORD* record, DWORD* size)
329 static const WCHAR fmt[] = {'{','%','s','}',0};
332 if (!group || group[0] == 0)
337 /* if no [] then group is returned as is */
339 if (!find_next_outermost_key(group, len, &key, &mark, &mark2, &nested))
341 *size = (len+2)*sizeof(WCHAR);
342 value = HeapAlloc(GetProcessHeap(),0,*size);
343 sprintfW(value,fmt,group);
344 /* do not return size of the null at the end */
345 *size = (len+1)*sizeof(WCHAR);
349 HeapFree(GetProcessHeap(),0,key);
351 sz = deformat_string_internal(package, group, &value, strlenW(group),
355 *size = sz * sizeof(WCHAR);
358 else if (failcount < 0)
362 v2 = HeapAlloc(GetProcessHeap(),0,(sz+2)*sizeof(WCHAR));
364 memcpy(&v2[1],value,sz*sizeof(WCHAR));
366 HeapFree(GetProcessHeap(),0,value);
368 *size = (sz+2)*sizeof(WCHAR);
381 * return is also in WCHARs
383 static DWORD deformat_string_internal(MSIPACKAGE *package, LPCWSTR ptr,
384 WCHAR** data, DWORD len, MSIRECORD* record,
388 LPCWSTR mark2 = NULL;
394 LPBYTE newdata = NULL;
395 const WCHAR* progress = NULL;
400 TRACE("Deformatting NULL string\n");
405 TRACE("Starting with %s\n",debugstr_wn(ptr,len));
407 /* scan for special characters... fast exit */
408 if ((!scanW(ptr,'[',len) || (scanW(ptr,'[',len) && !scanW(ptr,']',len))) &&
409 (scanW(ptr,'{',len) && !scanW(ptr,'}',len)))
412 *data = HeapAlloc(GetProcessHeap(),0,(len*sizeof(WCHAR)));
413 memcpy(*data,ptr,len*sizeof(WCHAR));
414 TRACE("Returning %s\n",debugstr_wn(*data,len));
420 while (progress - ptr < len)
422 /* seek out first group if existing */
423 if (find_next_group(progress, len - (progress - ptr), &key,
426 value = deformat_group(package, key, strlenW(key)+1, record,
431 /* formatted string located */
432 else if (!find_next_outermost_key(progress, len - (progress - ptr),
433 &key, &mark, &mark2, &nested))
437 TRACE("after value %s \n",debugstr_wn((LPWSTR)newdata,
438 size/sizeof(WCHAR)));
439 chunk = (len - (progress - ptr)) * sizeof(WCHAR);
440 TRACE("after chunk is %li + %li\n",size,chunk);
442 nd2 = HeapReAlloc(GetProcessHeap(),0,newdata,(size+chunk));
444 nd2 = HeapAlloc(GetProcessHeap(),0,chunk);
447 memcpy(&newdata[size],progress,chunk);
452 if (mark != progress)
455 DWORD old_size = size;
456 INT cnt = (mark - progress);
457 TRACE("%i (%i) characters before marker\n",cnt,(mark-progress));
458 size += cnt * sizeof(WCHAR);
460 tgt = HeapAlloc(GetProcessHeap(),0,size);
462 tgt = HeapReAlloc(GetProcessHeap(),0,newdata,size);
464 memcpy(&newdata[old_size],progress,(cnt * sizeof(WCHAR)));
471 TRACE("Nested key... %s\n",debugstr_w(key));
472 deformat_string_internal(package, key, &value, strlenW(key)+1,
475 HeapFree(GetProcessHeap(),0,key);
479 TRACE("Current %s .. %s\n",debugstr_wn((LPWSTR)newdata,
480 size/sizeof(WCHAR)),debugstr_w(key));
484 /* only deformat number indexs */
485 if (key && is_key_number(key))
487 value = deformat_index(record,key,&chunk);
488 if (!chunk && failcount && *failcount >= 0)
497 DWORD keylen = strlenW(key);
498 chunk = (keylen + 2)*sizeof(WCHAR);
499 value = HeapAlloc(GetProcessHeap(),0,chunk);
501 memcpy(&value[1],key,keylen*sizeof(WCHAR));
502 value[1+keylen] = ']';
509 if (key) switch (key[0])
512 value = deformat_NULL(&chunk);
515 value = deformat_component(package,&key[1],&chunk);
518 value = deformat_file(package,&key[1], &chunk, FALSE);
520 case '!': /* should be short path */
521 value = deformat_file(package,&key[1], &chunk, TRUE);
524 value = deformat_escape(&key[1],&chunk);
527 value = deformat_environment(package,&key[1],&chunk);
530 /* index keys cannot be nested */
531 if (is_key_number(key))
533 value = deformat_index(record,key,&chunk);
536 static const WCHAR fmt[] = {'[','%','s',']',0};
537 value = HeapAlloc(GetProcessHeap(),0,10);
538 sprintfW(value,fmt,key);
539 chunk = strlenW(value)*sizeof(WCHAR);
542 value = deformat_property(package,key,&chunk);
547 HeapFree(GetProcessHeap(),0,key);
552 TRACE("value %s, chunk %li size %li\n",debugstr_w((LPWSTR)value),
555 nd2= HeapReAlloc(GetProcessHeap(),0,newdata,(size + chunk));
557 nd2= HeapAlloc(GetProcessHeap(),0,chunk);
559 memcpy(&newdata[size],value,chunk);
561 HeapFree(GetProcessHeap(),0,value);
563 else if (failcount && *failcount >=0 )
569 TRACE("after everything %s\n",debugstr_wn((LPWSTR)newdata,
570 size/sizeof(WCHAR)));
572 *data = (LPWSTR)newdata;
573 return size / sizeof(WCHAR);
577 UINT MSI_FormatRecordW( MSIPACKAGE* package, MSIRECORD* record, LPWSTR buffer,
583 UINT rc = ERROR_INVALID_PARAMETER;
585 TRACE("%p %p %p %li\n",package, record ,buffer, *size);
587 rec = load_dynamic_stringW(record,0);
589 rec = build_default_format(record);
591 TRACE("(%s)\n",debugstr_w(rec));
593 len = deformat_string_internal(package,rec,&deformated,strlenW(rec),
600 memcpy(buffer,deformated,len*sizeof(WCHAR));
608 memcpy(buffer,deformated,(*size)*sizeof(WCHAR));
609 buffer[(*size)-1] = 0;
611 rc = ERROR_MORE_DATA;
619 HeapFree(GetProcessHeap(),0,rec);
620 HeapFree(GetProcessHeap(),0,deformated);
624 UINT MSI_FormatRecordA( MSIPACKAGE* package, MSIRECORD* record, LPSTR buffer,
630 UINT rc = ERROR_INVALID_PARAMETER;
632 TRACE("%p %p %p %li\n",package, record ,buffer, *size);
634 rec = load_dynamic_stringW(record,0);
636 rec = build_default_format(record);
638 TRACE("(%s)\n",debugstr_w(rec));
640 len = deformat_string_internal(package,rec,&deformated,strlenW(rec),
642 lenA = WideCharToMultiByte(CP_ACP,0,deformated,len,NULL,0,NULL,NULL);
646 WideCharToMultiByte(CP_ACP,0,deformated,len,buffer,*size,NULL, NULL);
654 rc = ERROR_MORE_DATA;
655 buffer[(*size)-1] = 0;
663 HeapFree(GetProcessHeap(),0,rec);
664 HeapFree(GetProcessHeap(),0,deformated);
669 UINT WINAPI MsiFormatRecordW( MSIHANDLE hInstall, MSIHANDLE hRecord,
670 LPWSTR szResult, DWORD *sz )
672 UINT r = ERROR_INVALID_HANDLE;
676 TRACE("%ld %ld %p %p\n", hInstall, hRecord, szResult, sz);
678 record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
681 return ERROR_INVALID_HANDLE;
684 msiobj_release( &record->hdr );
686 return ERROR_INVALID_PARAMETER;
688 return ERROR_SUCCESS;
691 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
693 r = MSI_FormatRecordW( package, record, szResult, sz );
694 msiobj_release( &record->hdr );
696 msiobj_release( &package->hdr );
700 UINT WINAPI MsiFormatRecordA( MSIHANDLE hInstall, MSIHANDLE hRecord,
701 LPSTR szResult, DWORD *sz )
703 UINT r = ERROR_INVALID_HANDLE;
704 MSIPACKAGE *package = NULL;
705 MSIRECORD *record = NULL;
707 TRACE("%ld %ld %p %p\n", hInstall, hRecord, szResult, sz);
709 record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
712 return ERROR_INVALID_HANDLE;
715 msiobj_release( &record->hdr );
717 return ERROR_INVALID_PARAMETER;
719 return ERROR_SUCCESS;
722 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
724 r = MSI_FormatRecordA( package, record, szResult, sz );
725 msiobj_release( &record->hdr );
727 msiobj_release( &package->hdr );