Fixed 'break NN' command (using dbghelp.SymEnumLines).
[wine/multimedia.git] / dlls / msi / format.c
blob5ce4fff2f1c934cf501a08527fbf0bd3994695bf
1 /*
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
26 #include <stdarg.h>
27 #include <stdio.h>
29 #define COBJMACROS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winerror.h"
34 #include "wine/debug.h"
35 #include "msi.h"
36 #include "msipriv.h"
37 #include "winnls.h"
38 #include "wine/unicode.h"
39 #include "action.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
44 static DWORD deformat_string_internal(MSIPACKAGE *package, LPCWSTR ptr,
45 WCHAR** data, DWORD len, MSIRECORD* record,
46 BOOL* in_group);
49 static LPWSTR build_default_format(MSIRECORD* record)
51 int i;
52 int count;
53 LPWSTR rc;
54 static const WCHAR fmt[] = {'%','i',':',' ','[','%','i',']',' ',0};
55 WCHAR buf[11];
57 count = MSI_RecordGetFieldCount(record);
59 rc = msi_alloc((11*count)*sizeof(WCHAR));
60 rc[0] = 0;
61 for (i = 1; i <= count; i++)
63 sprintfW(buf,fmt,i,i);
64 strcatW(rc,buf);
66 return rc;
69 static const WCHAR* scanW(LPCWSTR buf, WCHAR token, DWORD len)
71 DWORD i;
72 for (i = 0; i < len; i++)
73 if (buf[i] == token)
74 return &buf[i];
75 return NULL;
78 /* break out helper functions for deformating */
79 static LPWSTR deformat_component(MSIPACKAGE* package, LPCWSTR key, DWORD* sz)
81 LPWSTR value = NULL;
82 MSICOMPONENT *comp;
84 *sz = 0;
85 if (!package)
86 return NULL;
88 FIXME("component key %s\n", debugstr_w(key));
89 comp = get_loaded_component(package,key);
90 if (comp)
92 value = resolve_folder(package, comp->Directory, FALSE, FALSE, NULL);
93 *sz = (strlenW(value)) * sizeof(WCHAR);
96 return value;
99 static LPWSTR deformat_file(MSIPACKAGE* package, LPCWSTR key, DWORD* sz,
100 BOOL shortname)
102 LPWSTR value = NULL;
103 MSIFILE *file;
105 *sz = 0;
107 if (!package)
108 return NULL;
110 file = get_loaded_file( package, key );
111 if (file)
113 if (!shortname)
115 value = strdupW( file->TargetPath );
116 *sz = (strlenW(value)) * sizeof(WCHAR);
118 else
120 DWORD size = 0;
121 size = GetShortPathNameW( file->TargetPath, NULL, 0 );
123 if (size > 0)
125 *sz = (size-1) * sizeof (WCHAR);
126 size ++;
127 value = msi_alloc(size * sizeof(WCHAR));
128 GetShortPathNameW( file->TargetPath, value, size );
130 else
132 ERR("Unable to get ShortPath size (%s)\n",
133 debugstr_w( file->TargetPath) );
134 value = NULL;
135 *sz = 0;
140 return value;
143 static LPWSTR deformat_environment(MSIPACKAGE* package, LPCWSTR key,
144 DWORD* chunk)
146 LPWSTR value = NULL;
147 DWORD sz;
149 sz = GetEnvironmentVariableW(key,NULL,0);
150 if (sz > 0)
152 sz++;
153 value = msi_alloc(sz * sizeof(WCHAR));
154 GetEnvironmentVariableW(&key[1],value,sz);
155 *chunk = (strlenW(value)) * sizeof(WCHAR);
157 else
159 ERR("Unknown environment variable %s\n", debugstr_w(key));
160 *chunk = 0;
161 value = NULL;
163 return value;
167 static LPWSTR deformat_NULL(DWORD* chunk)
169 LPWSTR value;
171 value = msi_alloc(sizeof(WCHAR)*2);
172 value[0] = 0;
173 *chunk = sizeof(WCHAR);
174 return value;
177 static LPWSTR deformat_escape(LPCWSTR key, DWORD* chunk)
179 LPWSTR value;
181 value = msi_alloc(sizeof(WCHAR)*2);
182 value[0] = key[0];
183 *chunk = sizeof(WCHAR);
185 return value;
189 static BOOL is_key_number(LPCWSTR key)
191 INT index = 0;
192 if (key[0] == 0)
193 return FALSE;
195 while (isdigitW(key[index])) index++;
196 if (key[index] == 0)
197 return TRUE;
198 else
199 return FALSE;
202 static LPWSTR deformat_index(MSIRECORD* record, LPCWSTR key, DWORD* chunk )
204 INT index;
205 LPWSTR value;
207 index = atoiW(key);
208 TRACE("record index %i\n",index);
209 value = msi_dup_record_field(record,index);
210 if (value)
211 *chunk = strlenW(value) * sizeof(WCHAR);
212 else
214 value = NULL;
215 *chunk = 0;
217 return value;
220 static LPWSTR deformat_property(MSIPACKAGE* package, LPCWSTR key, DWORD* chunk)
222 LPWSTR value;
224 if (!package)
225 return NULL;
227 value = msi_dup_property( package, key );
229 if (value)
230 *chunk = (strlenW(value)) * sizeof(WCHAR);
232 return value;
236 * Groups cannot be nested. They are just treated as from { to next }
238 static BOOL find_next_group(LPCWSTR source, DWORD len_remaining,
239 LPWSTR *group, LPCWSTR *mark,
240 LPCWSTR* mark2)
242 int i;
243 BOOL found = FALSE;
245 *mark = scanW(source,'{',len_remaining);
246 if (!*mark)
247 return FALSE;
249 for (i = 1; (*mark - source) + i < len_remaining; i++)
251 if ((*mark)[i] == '}')
253 found = TRUE;
254 break;
257 if (! found)
258 return FALSE;
260 *mark2 = &(*mark)[i];
262 i = *mark2 - *mark;
263 *group = msi_alloc(i*sizeof(WCHAR));
265 i -= 1;
266 memcpy(*group,&(*mark)[1],i*sizeof(WCHAR));
267 (*group)[i] = 0;
269 TRACE("Found group %s\n",debugstr_w(*group));
270 return TRUE;
274 static BOOL find_next_outermost_key(LPCWSTR source, DWORD len_remaining,
275 LPWSTR *key, LPCWSTR *mark, LPCWSTR* mark2,
276 BOOL *nested)
278 INT count = 0;
279 INT total_count = 0;
280 int i;
282 *mark = scanW(source,'[',len_remaining);
283 if (!*mark)
284 return FALSE;
286 count = 1;
287 total_count = 1;
288 *nested = FALSE;
289 for (i = 1; (*mark - source) + i < len_remaining && count > 0; i++)
291 if ((*mark)[i] == '[' && (*mark)[i-1] != '\\')
293 count ++;
294 total_count ++;
295 *nested = TRUE;
297 else if ((*mark)[i] == ']' && (*mark)[i-1] != '\\')
299 count --;
303 if (count > 0)
304 return FALSE;
306 *mark2 = &(*mark)[i-1];
308 i = *mark2 - *mark;
309 *key = msi_alloc(i*sizeof(WCHAR));
310 /* do not have the [] in the key */
311 i -= 1;
312 memcpy(*key,&(*mark)[1],i*sizeof(WCHAR));
313 (*key)[i] = 0;
315 TRACE("Found Key %s\n",debugstr_w(*key));
316 return TRUE;
319 static LPWSTR deformat_group(MSIPACKAGE* package, LPWSTR group, DWORD len,
320 MSIRECORD* record, DWORD* size)
322 LPWSTR value = NULL;
323 LPCWSTR mark, mark2;
324 LPWSTR key;
325 BOOL nested;
326 INT failcount;
327 static const WCHAR fmt[] = {'{','%','s','}',0};
328 UINT sz;
330 if (!group || group[0] == 0)
332 *size = 0;
333 return NULL;
335 /* if no [] then group is returned as is */
337 if (!find_next_outermost_key(group, len, &key, &mark, &mark2, &nested))
339 *size = (len+2)*sizeof(WCHAR);
340 value = msi_alloc(*size);
341 sprintfW(value,fmt,group);
342 /* do not return size of the null at the end */
343 *size = (len+1)*sizeof(WCHAR);
344 return value;
347 msi_free(key);
348 failcount = 0;
349 sz = deformat_string_internal(package, group, &value, strlenW(group),
350 record, &failcount);
351 if (failcount==0)
353 *size = sz * sizeof(WCHAR);
354 return value;
356 else if (failcount < 0)
358 LPWSTR v2;
360 v2 = msi_alloc((sz+2)*sizeof(WCHAR));
361 v2[0] = '{';
362 memcpy(&v2[1],value,sz*sizeof(WCHAR));
363 v2[sz+1]='}';
364 msi_free(value);
366 *size = (sz+2)*sizeof(WCHAR);
367 return v2;
369 else
371 msi_free(value);
372 *size = 0;
373 return NULL;
379 * len is in WCHARs
380 * return is also in WCHARs
382 static DWORD deformat_string_internal(MSIPACKAGE *package, LPCWSTR ptr,
383 WCHAR** data, DWORD len, MSIRECORD* record,
384 INT* failcount)
386 LPCWSTR mark = NULL;
387 LPCWSTR mark2 = NULL;
388 DWORD size=0;
389 DWORD chunk=0;
390 LPWSTR key;
391 LPWSTR value = NULL;
392 DWORD sz;
393 LPBYTE newdata = NULL;
394 const WCHAR* progress = NULL;
395 BOOL nested;
397 if (ptr==NULL)
399 TRACE("Deformatting NULL string\n");
400 *data = NULL;
401 return 0;
404 TRACE("Starting with %s\n",debugstr_wn(ptr,len));
406 /* scan for special characters... fast exit */
407 if ((!scanW(ptr,'[',len) || (scanW(ptr,'[',len) && !scanW(ptr,']',len))) &&
408 (scanW(ptr,'{',len) && !scanW(ptr,'}',len)))
410 /* not formatted */
411 *data = msi_alloc((len*sizeof(WCHAR)));
412 memcpy(*data,ptr,len*sizeof(WCHAR));
413 TRACE("Returning %s\n",debugstr_wn(*data,len));
414 return len;
417 progress = ptr;
419 while (progress - ptr < len)
421 /* seek out first group if existing */
422 if (find_next_group(progress, len - (progress - ptr), &key,
423 &mark, &mark2))
425 value = deformat_group(package, key, strlenW(key)+1, record,
426 &chunk);
427 msi_free( key );
428 key = NULL;
429 nested = FALSE;
431 /* formatted string located */
432 else if (!find_next_outermost_key(progress, len - (progress - ptr),
433 &key, &mark, &mark2, &nested))
435 LPBYTE nd2;
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);
441 if (size)
442 nd2 = msi_realloc(newdata,(size+chunk));
443 else
444 nd2 = msi_alloc(chunk);
446 newdata = nd2;
447 memcpy(&newdata[size],progress,chunk);
448 size+=chunk;
449 break;
452 if (mark != progress)
454 LPBYTE tgt;
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);
459 if (!old_size)
460 tgt = msi_alloc(size);
461 else
462 tgt = msi_realloc(newdata,size);
463 newdata = tgt;
464 memcpy(&newdata[old_size],progress,(cnt * sizeof(WCHAR)));
467 progress = mark;
469 if (nested)
471 TRACE("Nested key... %s\n",debugstr_w(key));
472 deformat_string_internal(package, key, &value, strlenW(key)+1,
473 record, failcount);
475 msi_free(key);
476 key = value;
479 TRACE("Current %s .. %s\n",debugstr_wn((LPWSTR)newdata,
480 size/sizeof(WCHAR)),debugstr_w(key));
482 if (!package)
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)
489 (*failcount)++;
491 else
493 if (failcount)
494 *failcount = -1;
495 if(key)
497 DWORD keylen = strlenW(key);
498 chunk = (keylen + 2)*sizeof(WCHAR);
499 value = msi_alloc(chunk);
500 value[0] = '[';
501 memcpy(&value[1],key,keylen*sizeof(WCHAR));
502 value[1+keylen] = ']';
506 else
508 sz = 0;
509 if (key) switch (key[0])
511 case '~':
512 value = deformat_NULL(&chunk);
513 break;
514 case '$':
515 value = deformat_component(package,&key[1],&chunk);
516 break;
517 case '#':
518 value = deformat_file(package,&key[1], &chunk, FALSE);
519 break;
520 case '!': /* should be short path */
521 value = deformat_file(package,&key[1], &chunk, TRUE);
522 break;
523 case '\\':
524 value = deformat_escape(&key[1],&chunk);
525 break;
526 case '%':
527 value = deformat_environment(package,&key[1],&chunk);
528 break;
529 default:
530 /* index keys cannot be nested */
531 if (is_key_number(key))
532 if (!nested)
533 value = deformat_index(record,key,&chunk);
534 else
536 static const WCHAR fmt[] = {'[','%','s',']',0};
537 value = msi_alloc(10);
538 sprintfW(value,fmt,key);
539 chunk = strlenW(value)*sizeof(WCHAR);
541 else
542 value = deformat_property(package,key,&chunk);
543 break;
547 msi_free(key);
549 if (value!=NULL)
551 LPBYTE nd2;
552 TRACE("value %s, chunk %li size %li\n",debugstr_w((LPWSTR)value),
553 chunk, size);
554 if (size)
555 nd2= msi_realloc(newdata,(size + chunk));
556 else
557 nd2= msi_alloc(chunk);
558 newdata = nd2;
559 memcpy(&newdata[size],value,chunk);
560 size+=chunk;
561 msi_free(value);
563 else if (failcount && *failcount >=0 )
564 (*failcount)++;
566 progress = mark2+1;
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,
578 DWORD *size )
580 LPWSTR deformated;
581 LPWSTR rec;
582 DWORD len;
583 UINT rc = ERROR_INVALID_PARAMETER;
585 TRACE("%p %p %p %li\n",package, record ,buffer, *size);
587 rec = msi_dup_record_field(record,0);
588 if (!rec)
589 rec = build_default_format(record);
591 TRACE("(%s)\n",debugstr_w(rec));
593 len = deformat_string_internal(package,rec,&deformated,strlenW(rec),
594 record, NULL);
596 if (buffer)
598 if (*size>len)
600 memcpy(buffer,deformated,len*sizeof(WCHAR));
601 rc = ERROR_SUCCESS;
602 buffer[len] = 0;
604 else
606 if (*size > 0)
608 memcpy(buffer,deformated,(*size)*sizeof(WCHAR));
609 buffer[(*size)-1] = 0;
611 rc = ERROR_MORE_DATA;
614 else
615 rc = ERROR_SUCCESS;
617 *size = len;
619 msi_free(rec);
620 msi_free(deformated);
621 return rc;
624 UINT MSI_FormatRecordA( MSIPACKAGE* package, MSIRECORD* record, LPSTR buffer,
625 DWORD *size )
627 LPWSTR deformated;
628 LPWSTR rec;
629 DWORD len,lenA;
630 UINT rc = ERROR_INVALID_PARAMETER;
632 TRACE("%p %p %p %li\n",package, record ,buffer, *size);
634 rec = msi_dup_record_field(record,0);
635 if (!rec)
636 rec = build_default_format(record);
638 TRACE("(%s)\n",debugstr_w(rec));
640 len = deformat_string_internal(package,rec,&deformated,strlenW(rec),
641 record, NULL);
642 lenA = WideCharToMultiByte(CP_ACP,0,deformated,len,NULL,0,NULL,NULL);
644 if (buffer)
646 WideCharToMultiByte(CP_ACP,0,deformated,len,buffer,*size,NULL, NULL);
647 if (*size>lenA)
649 rc = ERROR_SUCCESS;
650 buffer[lenA] = 0;
652 else
654 rc = ERROR_MORE_DATA;
655 buffer[(*size)-1] = 0;
658 else
659 rc = ERROR_SUCCESS;
661 *size = lenA;
663 msi_free(rec);
664 msi_free(deformated);
665 return rc;
669 UINT WINAPI MsiFormatRecordW( MSIHANDLE hInstall, MSIHANDLE hRecord,
670 LPWSTR szResult, DWORD *sz )
672 UINT r = ERROR_INVALID_HANDLE;
673 MSIPACKAGE *package;
674 MSIRECORD *record;
676 TRACE("%ld %ld %p %p\n", hInstall, hRecord, szResult, sz);
678 record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
680 if (!record)
681 return ERROR_INVALID_HANDLE;
682 if (!sz)
684 msiobj_release( &record->hdr );
685 if (szResult)
686 return ERROR_INVALID_PARAMETER;
687 else
688 return ERROR_SUCCESS;
691 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
693 r = MSI_FormatRecordW( package, record, szResult, sz );
694 msiobj_release( &record->hdr );
695 if (package)
696 msiobj_release( &package->hdr );
697 return r;
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 );
711 if (!record)
712 return ERROR_INVALID_HANDLE;
713 if (!sz)
715 msiobj_release( &record->hdr );
716 if (szResult)
717 return ERROR_INVALID_PARAMETER;
718 else
719 return ERROR_SUCCESS;
722 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
724 r = MSI_FormatRecordA( package, record, szResult, sz );
725 msiobj_release( &record->hdr );
726 if (package)
727 msiobj_release( &package->hdr );
728 return r;