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"
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',':',' ','%','s',' ',0};
55 static const WCHAR fmt_null[] = {'%','i',':',' ',' ',0};
56 static const WCHAR fmt_index[] = {'%','i',0};
59 DWORD size, max_len, len;
61 count = MSI_RecordGetFieldCount(record);
64 buf = msi_alloc((max_len + 1) * sizeof(WCHAR));
68 for (i = 1; i <= count; i++)
70 sprintfW(index,fmt_index,i);
71 str = MSI_RecordGetString(record, i);
72 len = (str) ? lstrlenW(str) : 0;
73 len += (sizeof(fmt_null) - 3) + lstrlenW(index);
79 buf = msi_realloc(buf, (max_len + 1) * sizeof(WCHAR));
80 if (!buf) return NULL;
84 sprintfW(buf,fmt,i,str);
86 sprintfW(buf,fmt_null,i);
90 rc = msi_alloc(size * sizeof(WCHAR));
95 rc = msi_realloc(rc, size * sizeof(WCHAR));
103 static const WCHAR* scanW(LPCWSTR buf, WCHAR token, DWORD len)
106 for (i = 0; i < len; i++)
112 /* break out helper functions for deformating */
113 static LPWSTR deformat_component(MSIPACKAGE* package, LPCWSTR key, DWORD* sz)
122 FIXME("component key %s\n", debugstr_w(key));
123 comp = get_loaded_component(package,key);
126 value = resolve_folder(package, comp->Directory, FALSE, FALSE, NULL);
127 *sz = (strlenW(value)) * sizeof(WCHAR);
133 static LPWSTR deformat_file(MSIPACKAGE* package, LPCWSTR key, DWORD* sz,
144 file = get_loaded_file( package, key );
149 value = strdupW( file->TargetPath );
150 *sz = (strlenW(value)) * sizeof(WCHAR);
155 size = GetShortPathNameW( file->TargetPath, NULL, 0 );
159 *sz = (size-1) * sizeof (WCHAR);
161 value = msi_alloc(size * sizeof(WCHAR));
162 GetShortPathNameW( file->TargetPath, value, size );
166 ERR("Unable to get ShortPath size (%s)\n",
167 debugstr_w( file->TargetPath) );
168 value = strdupW( file->TargetPath );
169 *sz = (lstrlenW(value)) * sizeof(WCHAR);
177 static LPWSTR deformat_environment(MSIPACKAGE* package, LPCWSTR key,
183 sz = GetEnvironmentVariableW(key,NULL,0);
187 value = msi_alloc(sz * sizeof(WCHAR));
188 GetEnvironmentVariableW(key,value,sz);
189 *chunk = (strlenW(value)) * sizeof(WCHAR);
193 ERR("Unknown environment variable %s\n", debugstr_w(key));
201 static LPWSTR deformat_NULL(DWORD* chunk)
205 value = msi_alloc(sizeof(WCHAR)*2);
207 *chunk = sizeof(WCHAR);
211 static LPWSTR deformat_escape(LPCWSTR key, DWORD* chunk)
215 value = msi_alloc(sizeof(WCHAR)*2);
217 *chunk = sizeof(WCHAR);
223 static BOOL is_key_number(LPCWSTR key)
229 while (isdigitW(key[index])) index++;
236 static LPWSTR deformat_index(MSIRECORD* record, LPCWSTR key, DWORD* chunk )
242 TRACE("record index %i\n",index);
243 value = msi_dup_record_field(record,index);
245 *chunk = strlenW(value) * sizeof(WCHAR);
254 static LPWSTR deformat_property(MSIPACKAGE* package, LPCWSTR key, DWORD* chunk)
261 value = msi_dup_property( package, key );
264 *chunk = (strlenW(value)) * sizeof(WCHAR);
270 * Groups cannot be nested. They are just treated as from { to next }
272 static BOOL find_next_group(LPCWSTR source, DWORD len_remaining,
273 LPWSTR *group, LPCWSTR *mark,
279 *mark = scanW(source,'{',len_remaining);
283 for (i = 1; (*mark - source) + i < len_remaining; i++)
285 if ((*mark)[i] == '}')
294 *mark2 = &(*mark)[i];
297 *group = msi_alloc(i*sizeof(WCHAR));
300 memcpy(*group,&(*mark)[1],i*sizeof(WCHAR));
303 TRACE("Found group %s\n",debugstr_w(*group));
308 static BOOL find_next_outermost_key(LPCWSTR source, DWORD len_remaining,
309 LPWSTR *key, LPCWSTR *mark, LPCWSTR* mark2,
316 *mark = scanW(source,'[',len_remaining);
323 for (i = 1; (*mark - source) + i < len_remaining && count > 0; i++)
325 if ((*mark)[i] == '[' && (*mark)[i-1] != '\\')
331 else if ((*mark)[i] == ']' && (*mark)[i-1] != '\\')
340 *mark2 = &(*mark)[i-1];
343 *key = msi_alloc(i*sizeof(WCHAR));
344 /* do not have the [] in the key */
346 memcpy(*key,&(*mark)[1],i*sizeof(WCHAR));
349 TRACE("Found Key %s\n",debugstr_w(*key));
353 static LPWSTR deformat_group(MSIPACKAGE* package, LPWSTR group, DWORD len,
354 MSIRECORD* record, DWORD* size)
361 static const WCHAR fmt[] = {'{','%','s','}',0};
364 if (!group || group[0] == 0)
369 /* if no [] then group is returned as is */
371 if (!find_next_outermost_key(group, len, &key, &mark, &mark2, &nested))
373 *size = (len+2)*sizeof(WCHAR);
374 value = msi_alloc(*size);
375 sprintfW(value,fmt,group);
376 /* do not return size of the null at the end */
377 *size = (len+1)*sizeof(WCHAR);
383 sz = deformat_string_internal(package, group, &value, strlenW(group),
387 *size = sz * sizeof(WCHAR);
390 else if (failcount < 0)
394 v2 = msi_alloc((sz+2)*sizeof(WCHAR));
396 memcpy(&v2[1],value,sz*sizeof(WCHAR));
400 *size = (sz+2)*sizeof(WCHAR);
414 * return is also in WCHARs
416 static DWORD deformat_string_internal(MSIPACKAGE *package, LPCWSTR ptr,
417 WCHAR** data, DWORD len, MSIRECORD* record,
421 LPCWSTR mark2 = NULL;
427 LPBYTE newdata = NULL;
428 const WCHAR* progress = NULL;
433 TRACE("Deformatting NULL string\n");
438 TRACE("Starting with %s\n",debugstr_wn(ptr,len));
440 /* scan for special characters... fast exit */
441 if ((!scanW(ptr,'[',len) || (scanW(ptr,'[',len) && !scanW(ptr,']',len))) &&
442 (scanW(ptr,'{',len) && !scanW(ptr,'}',len)))
445 *data = msi_alloc((len*sizeof(WCHAR)));
446 memcpy(*data,ptr,len*sizeof(WCHAR));
447 TRACE("Returning %s\n",debugstr_wn(*data,len));
453 while (progress - ptr < len)
455 /* seek out first group if existing */
456 if (find_next_group(progress, len - (progress - ptr), &key,
459 value = deformat_group(package, key, strlenW(key)+1, record,
465 /* formatted string located */
466 else if (!find_next_outermost_key(progress, len - (progress - ptr),
467 &key, &mark, &mark2, &nested))
471 TRACE("after value %s\n", debugstr_wn((LPWSTR)newdata,
472 size/sizeof(WCHAR)));
473 chunk = (len - (progress - ptr)) * sizeof(WCHAR);
474 TRACE("after chunk is %i + %i\n",size,chunk);
476 nd2 = msi_realloc(newdata,(size+chunk));
478 nd2 = msi_alloc(chunk);
481 memcpy(&newdata[size],progress,chunk);
486 if (mark != progress)
489 DWORD old_size = size;
490 INT cnt = (mark - progress);
491 TRACE("%i (%i) characters before marker\n",cnt,(mark-progress));
492 size += cnt * sizeof(WCHAR);
494 tgt = msi_alloc(size);
496 tgt = msi_realloc(newdata,size);
498 memcpy(&newdata[old_size],progress,(cnt * sizeof(WCHAR)));
505 TRACE("Nested key... %s\n",debugstr_w(key));
506 deformat_string_internal(package, key, &value, strlenW(key)+1,
513 TRACE("Current %s .. %s\n",debugstr_wn((LPWSTR)newdata,
514 size/sizeof(WCHAR)),debugstr_w(key));
518 /* only deformat number indexs */
519 if (key && is_key_number(key))
521 value = deformat_index(record,key,&chunk);
522 if (!chunk && failcount && *failcount >= 0)
531 DWORD keylen = strlenW(key);
532 chunk = (keylen + 2)*sizeof(WCHAR);
533 value = msi_alloc(chunk);
535 memcpy(&value[1],key,keylen*sizeof(WCHAR));
536 value[1+keylen] = ']';
543 if (key) switch (key[0])
546 value = deformat_NULL(&chunk);
549 value = deformat_component(package,&key[1],&chunk);
552 value = deformat_file(package,&key[1], &chunk, FALSE);
554 case '!': /* should be short path */
555 value = deformat_file(package,&key[1], &chunk, TRUE);
558 value = deformat_escape(&key[1],&chunk);
561 value = deformat_environment(package,&key[1],&chunk);
564 /* index keys cannot be nested */
565 if (is_key_number(key))
567 value = deformat_index(record,key,&chunk);
570 static const WCHAR fmt[] = {'[','%','s',']',0};
571 value = msi_alloc(10);
572 sprintfW(value,fmt,key);
573 chunk = strlenW(value)*sizeof(WCHAR);
576 value = deformat_property(package,key,&chunk);
586 TRACE("value %s, chunk %i size %i\n",debugstr_w((LPWSTR)value),
589 nd2= msi_realloc(newdata,(size + chunk));
591 nd2= msi_alloc(chunk);
593 memcpy(&newdata[size],value,chunk);
597 else if (failcount && *failcount >=0 )
603 TRACE("after everything %s\n",debugstr_wn((LPWSTR)newdata,
604 size/sizeof(WCHAR)));
606 *data = (LPWSTR)newdata;
607 return size / sizeof(WCHAR);
611 UINT MSI_FormatRecordW( MSIPACKAGE* package, MSIRECORD* record, LPWSTR buffer,
617 UINT rc = ERROR_INVALID_PARAMETER;
619 TRACE("%p %p %p %i\n", package, record ,buffer, *size);
621 rec = msi_dup_record_field(record,0);
623 rec = build_default_format(record);
625 TRACE("(%s)\n",debugstr_w(rec));
627 len = deformat_string_internal(package,rec,&deformated,strlenW(rec),
634 memcpy(buffer,deformated,len*sizeof(WCHAR));
642 memcpy(buffer,deformated,(*size)*sizeof(WCHAR));
643 buffer[(*size)-1] = 0;
645 rc = ERROR_MORE_DATA;
654 msi_free(deformated);
658 UINT MSI_FormatRecordA( MSIPACKAGE* package, MSIRECORD* record, LPSTR buffer,
664 UINT rc = ERROR_INVALID_PARAMETER;
666 TRACE("%p %p %p %i\n", package, record ,buffer, *size);
668 rec = msi_dup_record_field(record,0);
670 rec = build_default_format(record);
672 TRACE("(%s)\n",debugstr_w(rec));
674 len = deformat_string_internal(package,rec,&deformated,strlenW(rec),
676 /* If len is zero then WideCharToMultiByte will return 0 indicating
677 * failure, but that will do just as well since we are ignoring
680 lenA = WideCharToMultiByte(CP_ACP,0,deformated,len,NULL,0,NULL,NULL);
685 WideCharToMultiByte(CP_ACP,0,deformated,len,buffer,*size,NULL, NULL);
693 rc = ERROR_MORE_DATA;
695 buffer[(*size)-1] = 0;
704 msi_free(deformated);
709 UINT WINAPI MsiFormatRecordW( MSIHANDLE hInstall, MSIHANDLE hRecord,
710 LPWSTR szResult, DWORD *sz )
712 UINT r = ERROR_INVALID_HANDLE;
716 TRACE("%ld %ld %p %p\n", hInstall, hRecord, szResult, sz);
718 record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
721 return ERROR_INVALID_HANDLE;
724 msiobj_release( &record->hdr );
726 return ERROR_INVALID_PARAMETER;
728 return ERROR_SUCCESS;
731 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
733 r = MSI_FormatRecordW( package, record, szResult, sz );
734 msiobj_release( &record->hdr );
736 msiobj_release( &package->hdr );
740 UINT WINAPI MsiFormatRecordA( MSIHANDLE hInstall, MSIHANDLE hRecord,
741 LPSTR szResult, DWORD *sz )
743 UINT r = ERROR_INVALID_HANDLE;
744 MSIPACKAGE *package = NULL;
745 MSIRECORD *record = NULL;
747 TRACE("%ld %ld %p %p\n", hInstall, hRecord, szResult, sz);
749 record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
752 return ERROR_INVALID_HANDLE;
755 msiobj_release( &record->hdr );
757 return ERROR_INVALID_PARAMETER;
759 return ERROR_SUCCESS;
762 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
764 r = MSI_FormatRecordA( package, record, szResult, sz );
765 msiobj_release( &record->hdr );
767 msiobj_release( &package->hdr );