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(const 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)
122 comp = get_loaded_component(package,key);
125 source = (comp->Action == INSTALLSTATE_SOURCE) ? TRUE : FALSE;
126 value = resolve_folder(package, comp->Directory, source, FALSE, TRUE, 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 value = strdupW( file->TargetPath );
167 *sz = (lstrlenW(value)) * sizeof(WCHAR);
175 static LPWSTR deformat_environment(MSIPACKAGE* package, LPCWSTR key,
181 sz = GetEnvironmentVariableW(key,NULL,0);
185 value = msi_alloc(sz * sizeof(WCHAR));
186 GetEnvironmentVariableW(key,value,sz);
187 *chunk = (strlenW(value)) * sizeof(WCHAR);
191 ERR("Unknown environment variable %s\n", debugstr_w(key));
199 static LPWSTR deformat_NULL(DWORD* chunk)
203 value = msi_alloc(sizeof(WCHAR)*2);
205 *chunk = sizeof(WCHAR);
209 static LPWSTR deformat_escape(LPCWSTR key, DWORD* chunk)
213 value = msi_alloc(sizeof(WCHAR)*2);
215 *chunk = sizeof(WCHAR);
221 static BOOL is_key_number(LPCWSTR key)
227 while (isdigitW(key[index])) index++;
234 static LPWSTR deformat_index(MSIRECORD* record, LPCWSTR key, DWORD* chunk )
240 TRACE("record index %i\n",index);
241 value = msi_dup_record_field(record,index);
243 *chunk = strlenW(value) * sizeof(WCHAR);
252 static LPWSTR deformat_property(MSIPACKAGE* package, LPCWSTR key, DWORD* chunk)
259 value = msi_dup_property( package, key );
262 *chunk = (strlenW(value)) * sizeof(WCHAR);
268 * Groups cannot be nested. They are just treated as from { to next }
270 static BOOL find_next_group(LPCWSTR source, DWORD len_remaining,
271 LPWSTR *group, LPCWSTR *mark,
277 *mark = scanW(source,'{',len_remaining);
281 for (i = 1; (*mark - source) + i < len_remaining; i++)
283 if ((*mark)[i] == '}')
292 *mark2 = &(*mark)[i];
295 *group = msi_alloc(i*sizeof(WCHAR));
298 memcpy(*group,&(*mark)[1],i*sizeof(WCHAR));
301 TRACE("Found group %s\n",debugstr_w(*group));
306 static BOOL find_next_outermost_key(LPCWSTR source, DWORD len_remaining,
307 LPWSTR *key, LPCWSTR *mark, LPCWSTR* mark2,
314 *mark = scanW(source,'[',len_remaining);
321 for (i = 1; (*mark - source) + i < len_remaining && count > 0; i++)
323 if ((*mark)[i] == '[' && (*mark)[i-1] != '\\')
329 else if ((*mark)[i] == ']' && (*mark)[i-1] != '\\')
338 *mark2 = &(*mark)[i-1];
341 *key = msi_alloc(i*sizeof(WCHAR));
342 /* do not have the [] in the key */
344 memcpy(*key,&(*mark)[1],i*sizeof(WCHAR));
347 TRACE("Found Key %s\n",debugstr_w(*key));
351 static LPWSTR deformat_group(MSIPACKAGE* package, LPWSTR group, DWORD len,
352 MSIRECORD* record, DWORD* size)
359 static const WCHAR fmt[] = {'{','%','s','}',0};
362 if (!group || group[0] == 0)
367 /* if no [] then group is returned as is */
369 if (!find_next_outermost_key(group, len, &key, &mark, &mark2, &nested))
371 *size = (len+2)*sizeof(WCHAR);
372 value = msi_alloc(*size);
373 sprintfW(value,fmt,group);
374 /* do not return size of the null at the end */
375 *size = (len+1)*sizeof(WCHAR);
381 sz = deformat_string_internal(package, group, &value, strlenW(group),
385 *size = sz * sizeof(WCHAR);
388 else if (failcount < 0)
392 v2 = msi_alloc((sz+2)*sizeof(WCHAR));
394 memcpy(&v2[1],value,sz*sizeof(WCHAR));
398 *size = (sz+2)*sizeof(WCHAR);
412 * return is also in WCHARs
414 static DWORD deformat_string_internal(MSIPACKAGE *package, LPCWSTR ptr,
415 WCHAR** data, DWORD len, MSIRECORD* record,
419 LPCWSTR mark2 = NULL;
425 LPBYTE newdata = NULL;
426 const WCHAR* progress = NULL;
431 TRACE("Deformatting NULL string\n");
436 TRACE("Starting with %s\n",debugstr_wn(ptr,len));
438 /* scan for special characters... fast exit */
439 if ((!scanW(ptr,'[',len) || (scanW(ptr,'[',len) && !scanW(ptr,']',len))) &&
440 (scanW(ptr,'{',len) && !scanW(ptr,'}',len)))
443 *data = msi_alloc((len*sizeof(WCHAR)));
444 memcpy(*data,ptr,len*sizeof(WCHAR));
445 TRACE("Returning %s\n",debugstr_wn(*data,len));
451 while (progress - ptr < len)
453 /* seek out first group if existing */
454 if (find_next_group(progress, len - (progress - ptr), &key,
457 value = deformat_group(package, key, strlenW(key)+1, record,
463 /* formatted string located */
464 else if (!find_next_outermost_key(progress, len - (progress - ptr),
465 &key, &mark, &mark2, &nested))
469 TRACE("after value %s\n", debugstr_wn((LPWSTR)newdata,
470 size/sizeof(WCHAR)));
471 chunk = (len - (progress - ptr)) * sizeof(WCHAR);
472 TRACE("after chunk is %i + %i\n",size,chunk);
474 nd2 = msi_realloc(newdata,(size+chunk));
476 nd2 = msi_alloc(chunk);
479 memcpy(&newdata[size],progress,chunk);
484 if (mark != progress)
487 DWORD old_size = size;
488 INT cnt = (mark - progress);
489 TRACE("%i (%i) characters before marker\n",cnt,(mark-progress));
490 size += cnt * sizeof(WCHAR);
492 tgt = msi_alloc(size);
494 tgt = msi_realloc(newdata,size);
496 memcpy(&newdata[old_size],progress,(cnt * sizeof(WCHAR)));
503 TRACE("Nested key... %s\n",debugstr_w(key));
504 deformat_string_internal(package, key, &value, strlenW(key)+1,
511 TRACE("Current %s .. %s\n",debugstr_wn((LPWSTR)newdata,
512 size/sizeof(WCHAR)),debugstr_w(key));
516 /* only deformat number indexs */
517 if (key && is_key_number(key))
519 value = deformat_index(record,key,&chunk);
520 if (!chunk && failcount && *failcount >= 0)
529 DWORD keylen = strlenW(key);
530 chunk = (keylen + 2)*sizeof(WCHAR);
531 value = msi_alloc(chunk);
533 memcpy(&value[1],key,keylen*sizeof(WCHAR));
534 value[1+keylen] = ']';
541 if (key) switch (key[0])
544 value = deformat_NULL(&chunk);
547 value = deformat_component(package,&key[1],&chunk);
550 value = deformat_file(package,&key[1], &chunk, FALSE);
552 case '!': /* should be short path */
553 value = deformat_file(package,&key[1], &chunk, TRUE);
556 value = deformat_escape(&key[1],&chunk);
559 value = deformat_environment(package,&key[1],&chunk);
562 /* index keys cannot be nested */
563 if (is_key_number(key))
565 value = deformat_index(record,key,&chunk);
568 static const WCHAR fmt[] = {'[','%','s',']',0};
569 value = msi_alloc(10);
570 sprintfW(value,fmt,key);
571 chunk = strlenW(value)*sizeof(WCHAR);
574 value = deformat_property(package,key,&chunk);
584 TRACE("value %s, chunk %i size %i\n",debugstr_w((LPWSTR)value),
587 nd2= msi_realloc(newdata,(size + chunk));
589 nd2= msi_alloc(chunk);
591 memcpy(&newdata[size],value,chunk);
595 else if (failcount && *failcount >=0 )
601 TRACE("after everything %s\n",debugstr_wn((LPWSTR)newdata,
602 size/sizeof(WCHAR)));
604 *data = (LPWSTR)newdata;
605 return size / sizeof(WCHAR);
609 UINT MSI_FormatRecordW( MSIPACKAGE* package, MSIRECORD* record, LPWSTR buffer,
615 UINT rc = ERROR_INVALID_PARAMETER;
617 TRACE("%p %p %p %i\n", package, record ,buffer, *size);
619 rec = msi_dup_record_field(record,0);
621 rec = build_default_format(record);
623 TRACE("(%s)\n",debugstr_w(rec));
625 len = deformat_string_internal(package,rec,&deformated,strlenW(rec),
632 memcpy(buffer,deformated,len*sizeof(WCHAR));
640 memcpy(buffer,deformated,(*size)*sizeof(WCHAR));
641 buffer[(*size)-1] = 0;
643 rc = ERROR_MORE_DATA;
652 msi_free(deformated);
656 UINT MSI_FormatRecordA( MSIPACKAGE* package, MSIRECORD* record, LPSTR buffer,
662 UINT rc = ERROR_INVALID_PARAMETER;
664 TRACE("%p %p %p %i\n", package, record ,buffer, *size);
666 rec = msi_dup_record_field(record,0);
668 rec = build_default_format(record);
670 TRACE("(%s)\n",debugstr_w(rec));
672 len = deformat_string_internal(package,rec,&deformated,strlenW(rec),
674 /* If len is zero then WideCharToMultiByte will return 0 indicating
675 * failure, but that will do just as well since we are ignoring
678 lenA = WideCharToMultiByte(CP_ACP,0,deformated,len,NULL,0,NULL,NULL);
683 WideCharToMultiByte(CP_ACP,0,deformated,len,buffer,*size,NULL, NULL);
691 rc = ERROR_MORE_DATA;
693 buffer[(*size)-1] = 0;
702 msi_free(deformated);
707 UINT WINAPI MsiFormatRecordW( MSIHANDLE hInstall, MSIHANDLE hRecord,
708 LPWSTR szResult, DWORD *sz )
710 UINT r = ERROR_INVALID_HANDLE;
714 TRACE("%ld %ld %p %p\n", hInstall, hRecord, szResult, sz);
716 record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
719 return ERROR_INVALID_HANDLE;
722 msiobj_release( &record->hdr );
724 return ERROR_INVALID_PARAMETER;
726 return ERROR_SUCCESS;
729 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
731 r = MSI_FormatRecordW( package, record, szResult, sz );
732 msiobj_release( &record->hdr );
734 msiobj_release( &package->hdr );
738 UINT WINAPI MsiFormatRecordA( MSIHANDLE hInstall, MSIHANDLE hRecord,
739 LPSTR szResult, DWORD *sz )
741 UINT r = ERROR_INVALID_HANDLE;
742 MSIPACKAGE *package = NULL;
743 MSIRECORD *record = NULL;
745 TRACE("%ld %ld %p %p\n", hInstall, hRecord, szResult, sz);
747 record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
750 return ERROR_INVALID_HANDLE;
753 msiobj_release( &record->hdr );
755 return ERROR_INVALID_PARAMETER;
757 return ERROR_SUCCESS;
760 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
762 r = MSI_FormatRecordA( package, record, szResult, sz );
763 msiobj_release( &record->hdr );
765 msiobj_release( &package->hdr );