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 index = get_loaded_component(package,key);
92 value = resolve_folder(package, package->components[index].Directory,
94 *sz = (strlenW(value)) * sizeof(WCHAR);
100 static LPWSTR deformat_file(MSIPACKAGE* package, LPCWSTR key, DWORD* sz,
111 index = get_loaded_file(package,key);
116 value = strdupW(package->files[index].TargetPath);
117 *sz = (strlenW(value)) * sizeof(WCHAR);
122 size = GetShortPathNameW(package->files[index].TargetPath, NULL, 0);
126 *sz = (size-1) * sizeof (WCHAR);
128 value = HeapAlloc(GetProcessHeap(),0,size * sizeof(WCHAR));
129 GetShortPathNameW(package->files[index].TargetPath, value,
134 ERR("Unable to get ShortPath size (%s)\n",
135 debugstr_w(package->files[index].TargetPath));
145 static LPWSTR deformat_environment(MSIPACKAGE* package, LPCWSTR key,
151 sz = GetEnvironmentVariableW(key,NULL,0);
155 value = HeapAlloc(GetProcessHeap(),0,sz * sizeof(WCHAR));
156 GetEnvironmentVariableW(&key[1],value,sz);
157 *chunk = (strlenW(value)) * sizeof(WCHAR);
161 ERR("Unknown environment variable %s\n", debugstr_w(key));
169 static LPWSTR deformat_NULL(DWORD* chunk)
173 value = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*2);
175 *chunk = sizeof(WCHAR);
179 static LPWSTR deformat_escape(LPCWSTR key, DWORD* chunk)
183 value = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*2);
185 *chunk = sizeof(WCHAR);
191 static BOOL is_key_number(LPCWSTR key)
197 while (isdigitW(key[index])) index++;
204 static LPWSTR deformat_index(MSIRECORD* record, LPCWSTR key, DWORD* chunk )
210 TRACE("record index %i\n",index);
211 value = load_dynamic_stringW(record,index);
213 *chunk = strlenW(value) * sizeof(WCHAR);
222 static LPWSTR deformat_property(MSIPACKAGE* package, LPCWSTR key, DWORD* chunk)
230 value = load_dynamic_property(package,key, &rc);
232 if (rc == ERROR_SUCCESS)
233 *chunk = (strlenW(value)) * sizeof(WCHAR);
239 * Groups cannot be nested. They are just treated as from { to next }
241 static BOOL find_next_group(LPCWSTR source, DWORD len_remaining,
242 LPWSTR *group, LPCWSTR *mark,
248 *mark = scanW(source,'{',len_remaining);
252 for (i = 1; (*mark - source) + i < len_remaining; i++)
254 if ((*mark)[i] == '}')
263 *mark2 = &(*mark)[i];
266 *group = HeapAlloc(GetProcessHeap(),0,i*sizeof(WCHAR));
269 memcpy(*group,&(*mark)[1],i*sizeof(WCHAR));
272 TRACE("Found group %s\n",debugstr_w(*group));
277 static BOOL find_next_outermost_key(LPCWSTR source, DWORD len_remaining,
278 LPWSTR *key, LPCWSTR *mark, LPCWSTR* mark2,
285 *mark = scanW(source,'[',len_remaining);
292 for (i = 1; (*mark - source) + i < len_remaining && count > 0; i++)
294 if ((*mark)[i] == '[' && (*mark)[i-1] != '\\')
300 else if ((*mark)[i] == ']' && (*mark)[i-1] != '\\')
309 *mark2 = &(*mark)[i-1];
312 *key = HeapAlloc(GetProcessHeap(),0,i*sizeof(WCHAR));
313 /* do not have the [] in the key */
315 memcpy(*key,&(*mark)[1],i*sizeof(WCHAR));
318 TRACE("Found Key %s\n",debugstr_w(*key));
322 static LPWSTR deformat_group(MSIPACKAGE* package, LPWSTR group, DWORD len,
323 MSIRECORD* record, DWORD* size)
330 static const WCHAR fmt[] = {'{','%','s','}',0};
333 if (!group || group[0] == 0)
338 /* if no [] then group is returned as is */
340 if (!find_next_outermost_key(group, len, &key, &mark, &mark2, &nested))
342 *size = (len+2)*sizeof(WCHAR);
343 value = HeapAlloc(GetProcessHeap(),0,*size);
344 sprintfW(value,fmt,group);
345 /* do not return size of the null at the end */
346 *size = (len+1)*sizeof(WCHAR);
350 HeapFree(GetProcessHeap(),0,key);
352 sz = deformat_string_internal(package, group, &value, strlenW(group),
356 *size = sz * sizeof(WCHAR);
359 else if (failcount < 0)
363 v2 = HeapAlloc(GetProcessHeap(),0,(sz+2)*sizeof(WCHAR));
365 memcpy(&v2[1],value,sz*sizeof(WCHAR));
367 HeapFree(GetProcessHeap(),0,value);
369 *size = (sz+2)*sizeof(WCHAR);
382 * return is also in WCHARs
384 static DWORD deformat_string_internal(MSIPACKAGE *package, LPCWSTR ptr,
385 WCHAR** data, DWORD len, MSIRECORD* record,
389 LPCWSTR mark2 = NULL;
395 LPBYTE newdata = NULL;
396 const WCHAR* progress = NULL;
401 TRACE("Deformatting NULL string\n");
406 TRACE("Starting with %s\n",debugstr_wn(ptr,len));
408 /* scan for special characters... fast exit */
409 if ((!scanW(ptr,'[',len) || (scanW(ptr,'[',len) && !scanW(ptr,']',len))) &&
410 (scanW(ptr,'{',len) && !scanW(ptr,'}',len)))
413 *data = HeapAlloc(GetProcessHeap(),0,(len*sizeof(WCHAR)));
414 memcpy(*data,ptr,len*sizeof(WCHAR));
415 TRACE("Returning %s\n",debugstr_wn(*data,len));
421 while (progress - ptr < len)
423 /* seek out first group if existing */
424 if (find_next_group(progress, len - (progress - ptr), &key,
427 value = deformat_group(package, key, strlenW(key)+1, record,
432 /* formatted string located */
433 else if (!find_next_outermost_key(progress, len - (progress - ptr),
434 &key, &mark, &mark2, &nested))
438 TRACE("after value %s \n",debugstr_wn((LPWSTR)newdata,
439 size/sizeof(WCHAR)));
440 chunk = (len - (progress - ptr)) * sizeof(WCHAR);
441 TRACE("after chunk is %li + %li\n",size,chunk);
443 nd2 = HeapReAlloc(GetProcessHeap(),0,newdata,(size+chunk));
445 nd2 = HeapAlloc(GetProcessHeap(),0,chunk);
448 memcpy(&newdata[size],progress,chunk);
453 if (mark != progress)
456 DWORD old_size = size;
457 INT cnt = (mark - progress);
458 TRACE("%i (%i) characters before marker\n",cnt,(mark-progress));
459 size += cnt * sizeof(WCHAR);
461 tgt = HeapAlloc(GetProcessHeap(),0,size);
463 tgt = HeapReAlloc(GetProcessHeap(),0,newdata,size);
465 memcpy(&newdata[old_size],progress,(cnt * sizeof(WCHAR)));
472 TRACE("Nested key... %s\n",debugstr_w(key));
473 deformat_string_internal(package, key, &value, strlenW(key)+1,
476 HeapFree(GetProcessHeap(),0,key);
480 TRACE("Current %s .. %s\n",debugstr_wn((LPWSTR)newdata,
481 size/sizeof(WCHAR)),debugstr_w(key));
485 /* only deformat number indexs */
486 if (key && is_key_number(key))
488 value = deformat_index(record,key,&chunk);
489 if (!chunk && failcount && *failcount >= 0)
498 DWORD keylen = strlenW(key);
499 chunk = (keylen + 2)*sizeof(WCHAR);
500 value = HeapAlloc(GetProcessHeap(),0,chunk);
502 memcpy(&value[1],key,keylen*sizeof(WCHAR));
503 value[1+keylen] = ']';
510 if (key) switch (key[0])
513 value = deformat_NULL(&chunk);
516 value = deformat_component(package,&key[1],&chunk);
519 value = deformat_file(package,&key[1], &chunk, FALSE);
521 case '!': /* should be short path */
522 value = deformat_file(package,&key[1], &chunk, TRUE);
525 value = deformat_escape(&key[1],&chunk);
528 value = deformat_environment(package,&key[1],&chunk);
531 /* index keys cannot be nested */
532 if (is_key_number(key))
534 value = deformat_index(record,key,&chunk);
537 static const WCHAR fmt[] = {'[','%','s',']',0};
538 value = HeapAlloc(GetProcessHeap(),0,10);
539 sprintfW(value,fmt,key);
540 chunk = strlenW(value)*sizeof(WCHAR);
543 value = deformat_property(package,key,&chunk);
548 HeapFree(GetProcessHeap(),0,key);
553 TRACE("value %s, chunk %li size %li\n",debugstr_w((LPWSTR)value),
556 nd2= HeapReAlloc(GetProcessHeap(),0,newdata,(size + chunk));
558 nd2= HeapAlloc(GetProcessHeap(),0,chunk);
560 memcpy(&newdata[size],value,chunk);
562 HeapFree(GetProcessHeap(),0,value);
564 else if (failcount && *failcount >=0 )
570 TRACE("after everything %s\n",debugstr_wn((LPWSTR)newdata,
571 size/sizeof(WCHAR)));
573 *data = (LPWSTR)newdata;
574 return size / sizeof(WCHAR);
578 UINT MSI_FormatRecordW( MSIPACKAGE* package, MSIRECORD* record, LPWSTR buffer,
584 UINT rc = ERROR_INVALID_PARAMETER;
586 TRACE("%p %p %p %li\n",package, record ,buffer, *size);
588 rec = load_dynamic_stringW(record,0);
590 rec = build_default_format(record);
592 TRACE("(%s)\n",debugstr_w(rec));
594 len = deformat_string_internal(package,rec,&deformated,strlenW(rec),
601 memcpy(buffer,deformated,len*sizeof(WCHAR));
609 memcpy(buffer,deformated,(*size)*sizeof(WCHAR));
610 buffer[(*size)-1] = 0;
612 rc = ERROR_MORE_DATA;
620 HeapFree(GetProcessHeap(),0,rec);
621 HeapFree(GetProcessHeap(),0,deformated);
625 UINT MSI_FormatRecordA( MSIPACKAGE* package, MSIRECORD* record, LPSTR buffer,
631 UINT rc = ERROR_INVALID_PARAMETER;
633 TRACE("%p %p %p %li\n",package, record ,buffer, *size);
635 rec = load_dynamic_stringW(record,0);
637 rec = build_default_format(record);
639 TRACE("(%s)\n",debugstr_w(rec));
641 len = deformat_string_internal(package,rec,&deformated,strlenW(rec),
643 lenA = WideCharToMultiByte(CP_ACP,0,deformated,len,NULL,0,NULL,NULL);
647 WideCharToMultiByte(CP_ACP,0,deformated,len,buffer,*size,NULL, NULL);
655 rc = ERROR_MORE_DATA;
656 buffer[(*size)-1] = 0;
664 HeapFree(GetProcessHeap(),0,rec);
665 HeapFree(GetProcessHeap(),0,deformated);
670 UINT WINAPI MsiFormatRecordW( MSIHANDLE hInstall, MSIHANDLE hRecord,
671 LPWSTR szResult, DWORD *sz )
673 UINT r = ERROR_INVALID_HANDLE;
677 TRACE("%ld %ld %p %p\n", hInstall, hRecord, szResult, sz);
679 record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
682 return ERROR_INVALID_HANDLE;
685 msiobj_release( &record->hdr );
687 return ERROR_INVALID_PARAMETER;
689 return ERROR_SUCCESS;
692 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
694 r = MSI_FormatRecordW( package, record, szResult, sz );
695 msiobj_release( &record->hdr );
697 msiobj_release( &package->hdr );
701 UINT WINAPI MsiFormatRecordA( MSIHANDLE hInstall, MSIHANDLE hRecord,
702 LPSTR szResult, DWORD *sz )
704 UINT r = ERROR_INVALID_HANDLE;
705 MSIPACKAGE *package = NULL;
706 MSIRECORD *record = NULL;
708 TRACE("%ld %ld %p %p\n", hInstall, hRecord, szResult, sz);
710 record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
713 return ERROR_INVALID_HANDLE;
716 msiobj_release( &record->hdr );
718 return ERROR_INVALID_PARAMETER;
720 return ERROR_SUCCESS;
723 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
725 r = MSI_FormatRecordA( package, record, szResult, sz );
726 msiobj_release( &record->hdr );
728 msiobj_release( &package->hdr );