178ff88bab90bf3bd29de3939bc51d4600f80f07
[reactos.git] / reactos / lib / sdk / crt / wine / undname.c
1 /*
2 * Demangle VC++ symbols into C function prototypes
3 *
4 * Copyright 2000 Jon Griffiths
5 * 2004 Eric Pouech
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #define __WINE_DEBUG_CHANNEL__
23 #include <precomp.h>
24 #include <assert.h>
25
26 #include <internal/wine/msvcrt.h>
27 #include <internal/wine/cppexcept.h>
28
29 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
30
31 /* TODO:
32 * - document a bit (grammar + functions)
33 * - back-port this new code into tools/winedump/msmangle.c
34 */
35
36 #define UNDNAME_COMPLETE (0x0000)
37 #define UNDNAME_NO_LEADING_UNDERSCORES (0x0001) /* Don't show __ in calling convention */
38 #define UNDNAME_NO_MS_KEYWORDS (0x0002) /* Don't show calling convention at all */
39 #define UNDNAME_NO_FUNCTION_RETURNS (0x0004) /* Don't show function/method return value */
40 #define UNDNAME_NO_ALLOCATION_MODEL (0x0008)
41 #define UNDNAME_NO_ALLOCATION_LANGUAGE (0x0010)
42 #define UNDNAME_NO_MS_THISTYPE (0x0020)
43 #define UNDNAME_NO_CV_THISTYPE (0x0040)
44 #define UNDNAME_NO_THISTYPE (0x0060)
45 #define UNDNAME_NO_ACCESS_SPECIFIERS (0x0080) /* Don't show access specifier (public/protected/private) */
46 #define UNDNAME_NO_THROW_SIGNATURES (0x0100)
47 #define UNDNAME_NO_MEMBER_TYPE (0x0200) /* Don't show static/virtual specifier */
48 #define UNDNAME_NO_RETURN_UDT_MODEL (0x0400)
49 #define UNDNAME_32_BIT_DECODE (0x0800)
50 #define UNDNAME_NAME_ONLY (0x1000) /* Only report the variable/method name */
51 #define UNDNAME_NO_ARGUMENTS (0x2000) /* Don't show method arguments */
52 #define UNDNAME_NO_SPECIAL_SYMS (0x4000)
53 #define UNDNAME_NO_COMPLEX_TYPE (0x8000)
54
55 /* How data types modifiers are stored:
56 * M (in the following definitions) is defined for
57 * 'A', 'B', 'C' and 'D' as follows
58 * {<A>}: ""
59 * {<B>}: "const "
60 * {<C>}: "volatile "
61 * {<D>}: "const volatile "
62 *
63 * in arguments:
64 * P<M>x {<M>}x*
65 * Q<M>x {<M>}x* const
66 * A<M>x {<M>}x&
67 * in data fields:
68 * same as for arguments and also the following
69 * ?<M>x {<M>}x
70 *
71 */
72
73 #define MAX_ARRAY_ELTS 32
74 struct array
75 {
76 unsigned start; /* first valid reference in array */
77 unsigned num; /* total number of used elts */
78 unsigned max;
79 char* elts[MAX_ARRAY_ELTS];
80 };
81
82 /* Structure holding a parsed symbol */
83 struct parsed_symbol
84 {
85 unsigned flags; /* the UNDNAME_ flags used for demangling */
86 malloc_func_t mem_alloc_ptr; /* internal allocator */
87 free_func_t mem_free_ptr; /* internal deallocator */
88
89 const char* current; /* pointer in input (mangled) string */
90 char* result; /* demangled string */
91
92 struct array names; /* array of names for back reference */
93 struct array stack; /* stack of parsed strings */
94
95 void* alloc_list; /* linked list of allocated blocks */
96 unsigned avail_in_first; /* number of available bytes in head block */
97 };
98
99 /* Type for parsing mangled types */
100 struct datatype_t
101 {
102 const char* left;
103 const char* right;
104 };
105
106 /******************************************************************
107 * und_alloc
108 *
109 * Internal allocator. Uses a simple linked list of large blocks
110 * where we use a poor-man allocator. It's fast, and since all
111 * allocation is pool, memory management is easy (esp. freeing).
112 */
113 static void* und_alloc(struct parsed_symbol* sym, size_t len)
114 {
115 void* ptr;
116
117 #define BLOCK_SIZE 1024
118 #define AVAIL_SIZE (1024 - sizeof(void*))
119
120 if (len > AVAIL_SIZE)
121 {
122 /* allocate a specific block */
123 ptr = sym->mem_alloc_ptr(sizeof(void*) + len);
124 if (!ptr) return NULL;
125 *(void**)ptr = sym->alloc_list;
126 sym->alloc_list = ptr;
127 sym->avail_in_first = 0;
128 ptr = (char*)sym->alloc_list + sizeof(void*);
129 }
130 else
131 {
132 if (len > sym->avail_in_first)
133 {
134 /* add a new block */
135 ptr = sym->mem_alloc_ptr(BLOCK_SIZE);
136 if (!ptr) return NULL;
137 *(void**)ptr = sym->alloc_list;
138 sym->alloc_list = ptr;
139 sym->avail_in_first = AVAIL_SIZE;
140 }
141 /* grab memory from head block */
142 ptr = (char*)sym->alloc_list + BLOCK_SIZE - sym->avail_in_first;
143 sym->avail_in_first -= len;
144 }
145 return ptr;
146 #undef BLOCK_SIZE
147 #undef AVAIL_SIZE
148 }
149
150 /******************************************************************
151 * und_free
152 * Frees all the blocks in the list of large blocks allocated by
153 * und_alloc.
154 */
155 static void und_free_all(struct parsed_symbol* sym)
156 {
157 void* next;
158
159 while (sym->alloc_list)
160 {
161 next = *(void**)sym->alloc_list;
162 if(sym->mem_free_ptr) sym->mem_free_ptr(sym->alloc_list);
163 sym->alloc_list = next;
164 }
165 sym->avail_in_first = 0;
166 }
167
168 /******************************************************************
169 * str_array_init
170 * Initialises an array of strings
171 */
172 static void str_array_init(struct array* a)
173 {
174 a->start = a->num = a->max = 0;
175 }
176
177 /******************************************************************
178 * str_array_push
179 * Adding a new string to an array
180 */
181 static void str_array_push(struct parsed_symbol* sym, const char* ptr, int len,
182 struct array* a)
183 {
184 assert(ptr);
185 assert(a);
186 assert(a->num < MAX_ARRAY_ELTS);
187 if (len == -1) len = strlen(ptr);
188 a->elts[a->num] = und_alloc(sym, len + 1);
189 assert(a->elts[a->num]);
190 memcpy(a->elts[a->num], ptr, len);
191 a->elts[a->num][len] = '\0';
192 if (++a->num >= a->max) a->max = a->num;
193 {
194 int i;
195 char c;
196
197 for (i = a->max - 1; i >= 0; i--)
198 {
199 c = '>';
200 if (i < a->start) c = '-';
201 else if (i >= a->num) c = '}';
202 TRACE("%p\t%d%c %s\n", a, i, c, a->elts[i]);
203 }
204 }
205 }
206
207 /******************************************************************
208 * str_array_get_ref
209 * Extracts a reference from an existing array (doing proper type
210 * checking)
211 */
212 static char* str_array_get_ref(struct array* cref, unsigned idx)
213 {
214 assert(cref);
215 if (cref->start + idx >= cref->max)
216 {
217 WARN("Out of bounds: %p %d + %d >= %d\n",
218 cref, cref->start, idx, cref->max);
219 return NULL;
220 }
221 TRACE("Returning %p[%d] => %s\n",
222 cref, idx, cref->elts[cref->start + idx]);
223 return cref->elts[cref->start + idx];
224 }
225
226 /******************************************************************
227 * str_printf
228 * Helper for printf type of command (only %s and %c are implemented)
229 * while dynamically allocating the buffer
230 */
231 static char* str_printf(struct parsed_symbol* sym, const char* format, ...)
232 {
233 va_list args;
234 size_t len = 1, i, sz;
235 char* tmp;
236 char* p;
237 char* t;
238
239 va_start(args, format);
240 for (i = 0; format[i]; i++)
241 {
242 if (format[i] == '%')
243 {
244 switch (format[++i])
245 {
246 case 's': t = va_arg(args, char*); if (t) len += strlen(t); break;
247 case 'c': (void)va_arg(args, int); len++; break;
248 default: i--; /* fall thru */
249 case '%': len++; break;
250 }
251 }
252 else len++;
253 }
254 va_end(args);
255 if (!(tmp = und_alloc(sym, len))) return NULL;
256 va_start(args, format);
257 for (p = tmp, i = 0; format[i]; i++)
258 {
259 if (format[i] == '%')
260 {
261 switch (format[++i])
262 {
263 case 's':
264 t = va_arg(args, char*);
265 if (t)
266 {
267 sz = strlen(t);
268 memcpy(p, t, sz);
269 p += sz;
270 }
271 break;
272 case 'c':
273 *p++ = (char)va_arg(args, int);
274 break;
275 default: i--; /* fall thru */
276 case '%': *p++ = '%'; break;
277 }
278 }
279 else *p++ = format[i];
280 }
281 va_end(args);
282 *p = '\0';
283 return tmp;
284 }
285
286 /* forward declaration */
287 static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
288 struct array* pmt, BOOL in_args);
289
290 static const char* get_number(struct parsed_symbol* sym)
291 {
292 char* ptr;
293 BOOL sgn = FALSE;
294
295 if (*sym->current == '?')
296 {
297 sgn = TRUE;
298 sym->current++;
299 }
300 if (*sym->current >= '0' && *sym->current <= '8')
301 {
302 ptr = und_alloc(sym, 3);
303 if (sgn) ptr[0] = '-';
304 ptr[sgn ? 1 : 0] = *sym->current + 1;
305 ptr[sgn ? 2 : 1] = '\0';
306 sym->current++;
307 }
308 else if (*sym->current == '9')
309 {
310 ptr = und_alloc(sym, 4);
311 if (sgn) ptr[0] = '-';
312 ptr[sgn ? 1 : 0] = '1';
313 ptr[sgn ? 2 : 1] = '0';
314 ptr[sgn ? 3 : 2] = '\0';
315 sym->current++;
316 }
317 else if (*sym->current >= 'A' && *sym->current <= 'P')
318 {
319 long ret = 0;
320
321 while (*sym->current >= 'A' && *sym->current <= 'P')
322 {
323 ret *= 16;
324 ret += *sym->current++ - 'A';
325 }
326 if (*sym->current != '@') return NULL;
327
328 ptr = und_alloc(sym, 17);
329 sprintf(ptr, "%s%ld", sgn ? "-" : "", ret);
330 sym->current++;
331 }
332 else return NULL;
333 return ptr;
334 }
335
336 /******************************************************************
337 * get_args
338 * Parses a list of function/method arguments, creates a string corresponding
339 * to the arguments' list.
340 */
341 static char* get_args(struct parsed_symbol* sym, struct array* pmt_ref, BOOL z_term,
342 char open_char, char close_char)
343
344 {
345 struct datatype_t ct;
346 struct array arg_collect;
347 char* args_str = NULL;
348 unsigned int i;
349
350 str_array_init(&arg_collect);
351
352 /* Now come the function arguments */
353 while (*sym->current)
354 {
355 /* Decode each data type and append it to the argument list */
356 if (*sym->current == '@')
357 {
358 sym->current++;
359 break;
360 }
361 if (!demangle_datatype(sym, &ct, pmt_ref, TRUE))
362 return NULL;
363 /* 'void' terminates an argument list in a function */
364 if (z_term && !strcmp(ct.left, "void")) break;
365 str_array_push(sym, str_printf(sym, "%s%s", ct.left, ct.right), -1,
366 &arg_collect);
367 if (!strcmp(ct.left, "...")) break;
368 }
369 /* Functions are always terminated by 'Z'. If we made it this far and
370 * don't find it, we have incorrectly identified a data type.
371 */
372 if (z_term && *sym->current++ != 'Z') return NULL;
373
374 if (arg_collect.num == 0 ||
375 (arg_collect.num == 1 && !strcmp(arg_collect.elts[0], "void")))
376 return str_printf(sym, "%cvoid%c", open_char, close_char);
377 for (i = 1; i < arg_collect.num; i++)
378 {
379 args_str = str_printf(sym, "%s,%s", args_str, arg_collect.elts[i]);
380 }
381
382 if (close_char == '>' && args_str && args_str[strlen(args_str) - 1] == '>')
383 args_str = str_printf(sym, "%c%s%s %c",
384 open_char, arg_collect.elts[0], args_str, close_char);
385 else
386 args_str = str_printf(sym, "%c%s%s%c",
387 open_char, arg_collect.elts[0], args_str, close_char);
388
389 return args_str;
390 }
391
392 /******************************************************************
393 * get_modifier
394 * Parses the type modifier. Always returns a static string
395 */
396 static BOOL get_modifier(char ch, const char** ret)
397 {
398 switch (ch)
399 {
400 case 'A': *ret = NULL; break;
401 case 'B': *ret = "const"; break;
402 case 'C': *ret = "volatile"; break;
403 case 'D': *ret = "const volatile"; break;
404 default: return FALSE;
405 }
406 return TRUE;
407 }
408
409 static BOOL get_modified_type(struct datatype_t *ct, struct parsed_symbol* sym,
410 struct array *pmt_ref, char modif, BOOL in_args)
411 {
412 const char* modifier;
413 const char* str_modif;
414
415 switch (modif)
416 {
417 case 'A': str_modif = " &"; break;
418 case 'B': str_modif = " & volatile"; break;
419 case 'P': str_modif = " *"; break;
420 case 'Q': str_modif = " * const"; break;
421 case 'R': str_modif = " * volatile"; break;
422 case 'S': str_modif = " * const volatile"; break;
423 case '?': str_modif = ""; break;
424 default: return FALSE;
425 }
426
427 if (get_modifier(*sym->current++, &modifier))
428 {
429 unsigned mark = sym->stack.num;
430 struct datatype_t sub_ct;
431
432 /* multidimensional arrays */
433 if (*sym->current == 'Y')
434 {
435 const char* n1;
436 int num;
437
438 sym->current++;
439 if (!(n1 = get_number(sym))) return FALSE;
440 num = atoi(n1);
441
442 if (str_modif[0] == ' ' && !modifier)
443 str_modif++;
444
445 if (modifier)
446 {
447 str_modif = str_printf(sym, " (%s%s)", modifier, str_modif);
448 modifier = NULL;
449 }
450 else
451 str_modif = str_printf(sym, " (%s)", str_modif);
452
453 while (num--)
454 str_modif = str_printf(sym, "%s[%s]", str_modif, get_number(sym));
455 }
456
457 /* Recurse to get the referred-to type */
458 if (!demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
459 return FALSE;
460 if (modifier)
461 ct->left = str_printf(sym, "%s %s%s", sub_ct.left, modifier, str_modif );
462 else
463 {
464 /* don't insert a space between duplicate '*' */
465 if (!in_args && str_modif[0] && str_modif[1] == '*' && sub_ct.left[strlen(sub_ct.left)-1] == '*')
466 str_modif++;
467 ct->left = str_printf(sym, "%s%s", sub_ct.left, str_modif );
468 }
469 ct->right = sub_ct.right;
470 sym->stack.num = mark;
471 }
472 return TRUE;
473 }
474
475 /******************************************************************
476 * get_literal_string
477 * Gets the literal name from the current position in the mangled
478 * symbol to the first '@' character. It pushes the parsed name to
479 * the symbol names stack and returns a pointer to it or NULL in
480 * case of an error.
481 */
482 static char* get_literal_string(struct parsed_symbol* sym)
483 {
484 const char *ptr = sym->current;
485
486 do {
487 if (!((*sym->current >= 'A' && *sym->current <= 'Z') ||
488 (*sym->current >= 'a' && *sym->current <= 'z') ||
489 (*sym->current >= '0' && *sym->current <= '9') ||
490 *sym->current == '_' || *sym->current == '$')) {
491 TRACE("Failed at '%c' in %s\n", *sym->current, ptr);
492 return NULL;
493 }
494 } while (*++sym->current != '@');
495 sym->current++;
496 str_array_push(sym, ptr, sym->current - 1 - ptr, &sym->names);
497
498 return str_array_get_ref(&sym->names, sym->names.num - sym->names.start - 1);
499 }
500
501 /******************************************************************
502 * get_template_name
503 * Parses a name with a template argument list and returns it as
504 * a string.
505 * In a template argument list the back reference to the names
506 * table is separately created. '0' points to the class component
507 * name with the template arguments. We use the same stack array
508 * to hold the names but save/restore the stack state before/after
509 * parsing the template argument list.
510 */
511 static char* get_template_name(struct parsed_symbol* sym)
512 {
513 char *name, *args;
514 unsigned num_mark = sym->names.num;
515 unsigned start_mark = sym->names.start;
516 unsigned stack_mark = sym->stack.num;
517 struct array array_pmt;
518
519 sym->names.start = sym->names.num;
520 if (!(name = get_literal_string(sym)))
521 return FALSE;
522 str_array_init(&array_pmt);
523 args = get_args(sym, &array_pmt, FALSE, '<', '>');
524 if (args != NULL)
525 name = str_printf(sym, "%s%s", name, args);
526 sym->names.num = num_mark;
527 sym->names.start = start_mark;
528 sym->stack.num = stack_mark;
529 return name;
530 }
531
532 /******************************************************************
533 * get_class
534 * Parses class as a list of parent-classes, terminated by '@' and stores the
535 * result in 'a' array. Each parent-classes, as well as the inner element
536 * (either field/method name or class name), are represented in the mangled
537 * name by a literal name ([a-zA-Z0-9_]+ terminated by '@') or a back reference
538 * ([0-9]) or a name with template arguments ('?$' literal name followed by the
539 * template argument list). The class name components appear in the reverse
540 * order in the mangled name, e.g aaa@bbb@ccc@@ will be demangled to
541 * ccc::bbb::aaa
542 * For each of these class name components a string will be allocated in the
543 * array.
544 */
545 static BOOL get_class(struct parsed_symbol* sym)
546 {
547 const char* name = NULL;
548
549 while (*sym->current != '@')
550 {
551 switch (*sym->current)
552 {
553 case '\0': return FALSE;
554
555 case '0': case '1': case '2': case '3':
556 case '4': case '5': case '6': case '7':
557 case '8': case '9':
558 name = str_array_get_ref(&sym->names, *sym->current++ - '0');
559 break;
560 case '?':
561 if (*++sym->current == '$')
562 {
563 sym->current++;
564 if ((name = get_template_name(sym)))
565 str_array_push(sym, name, -1, &sym->names);
566 }
567 break;
568 default:
569 name = get_literal_string(sym);
570 break;
571 }
572 if (!name)
573 return FALSE;
574 str_array_push(sym, name, -1, &sym->stack);
575 }
576 sym->current++;
577 return TRUE;
578 }
579
580 /******************************************************************
581 * get_class_string
582 * From an array collected by get_class in sym->stack, constructs the
583 * corresponding (allocated) string
584 */
585 static char* get_class_string(struct parsed_symbol* sym, int start)
586 {
587 int i;
588 size_t len, sz;
589 char* ret;
590 struct array *a = &sym->stack;
591
592 for (len = 0, i = start; i < a->num; i++)
593 {
594 assert(a->elts[i]);
595 len += 2 + strlen(a->elts[i]);
596 }
597 if (!(ret = und_alloc(sym, len - 1))) return NULL;
598 for (len = 0, i = a->num - 1; i >= start; i--)
599 {
600 sz = strlen(a->elts[i]);
601 memcpy(ret + len, a->elts[i], sz);
602 len += sz;
603 if (i > start)
604 {
605 ret[len++] = ':';
606 ret[len++] = ':';
607 }
608 }
609 ret[len] = '\0';
610 return ret;
611 }
612
613 /******************************************************************
614 * get_class_name
615 * Wrapper around get_class and get_class_string.
616 */
617 static char* get_class_name(struct parsed_symbol* sym)
618 {
619 unsigned mark = sym->stack.num;
620 char* s = NULL;
621
622 if (get_class(sym))
623 s = get_class_string(sym, mark);
624 sym->stack.num = mark;
625 return s;
626 }
627
628 /******************************************************************
629 * get_calling_convention
630 * Returns a static string corresponding to the calling convention described
631 * by char 'ch'. Sets export to TRUE iff the calling convention is exported.
632 */
633 static BOOL get_calling_convention(char ch, const char** call_conv,
634 const char** exported, unsigned flags)
635 {
636 *call_conv = *exported = NULL;
637
638 if (!(flags & (UNDNAME_NO_MS_KEYWORDS | UNDNAME_NO_ALLOCATION_LANGUAGE)))
639 {
640 if (flags & UNDNAME_NO_LEADING_UNDERSCORES)
641 {
642 if (((ch - 'A') % 2) == 1) *exported = "dll_export ";
643 switch (ch)
644 {
645 case 'A': case 'B': *call_conv = "cdecl"; break;
646 case 'C': case 'D': *call_conv = "pascal"; break;
647 case 'E': case 'F': *call_conv = "thiscall"; break;
648 case 'G': case 'H': *call_conv = "stdcall"; break;
649 case 'I': case 'J': *call_conv = "fastcall"; break;
650 case 'K': case 'L': break;
651 case 'M': *call_conv = "clrcall"; break;
652 default: ERR("Unknown calling convention %c\n", ch); return FALSE;
653 }
654 }
655 else
656 {
657 if (((ch - 'A') % 2) == 1) *exported = "__dll_export ";
658 switch (ch)
659 {
660 case 'A': case 'B': *call_conv = "__cdecl"; break;
661 case 'C': case 'D': *call_conv = "__pascal"; break;
662 case 'E': case 'F': *call_conv = "__thiscall"; break;
663 case 'G': case 'H': *call_conv = "__stdcall"; break;
664 case 'I': case 'J': *call_conv = "__fastcall"; break;
665 case 'K': case 'L': break;
666 case 'M': *call_conv = "__clrcall"; break;
667 default: ERR("Unknown calling convention %c\n", ch); return FALSE;
668 }
669 }
670 }
671 return TRUE;
672 }
673
674 /*******************************************************************
675 * get_simple_type
676 * Return a string containing an allocated string for a simple data type
677 */
678 static const char* get_simple_type(char c)
679 {
680 const char* type_string;
681
682 switch (c)
683 {
684 case 'C': type_string = "signed char"; break;
685 case 'D': type_string = "char"; break;
686 case 'E': type_string = "unsigned char"; break;
687 case 'F': type_string = "short"; break;
688 case 'G': type_string = "unsigned short"; break;
689 case 'H': type_string = "int"; break;
690 case 'I': type_string = "unsigned int"; break;
691 case 'J': type_string = "long"; break;
692 case 'K': type_string = "unsigned long"; break;
693 case 'M': type_string = "float"; break;
694 case 'N': type_string = "double"; break;
695 case 'O': type_string = "long double"; break;
696 case 'X': type_string = "void"; break;
697 case 'Z': type_string = "..."; break;
698 default: type_string = NULL; break;
699 }
700 return type_string;
701 }
702
703 /*******************************************************************
704 * get_extended_type
705 * Return a string containing an allocated string for a simple data type
706 */
707 static const char* get_extended_type(char c)
708 {
709 const char* type_string;
710
711 switch (c)
712 {
713 case 'D': type_string = "__int8"; break;
714 case 'E': type_string = "unsigned __int8"; break;
715 case 'F': type_string = "__int16"; break;
716 case 'G': type_string = "unsigned __int16"; break;
717 case 'H': type_string = "__int32"; break;
718 case 'I': type_string = "unsigned __int32"; break;
719 case 'J': type_string = "__int64"; break;
720 case 'K': type_string = "unsigned __int64"; break;
721 case 'L': type_string = "__int128"; break;
722 case 'M': type_string = "unsigned __int128"; break;
723 case 'N': type_string = "bool"; break;
724 case 'W': type_string = "wchar_t"; break;
725 default: type_string = NULL; break;
726 }
727 return type_string;
728 }
729
730 /*******************************************************************
731 * demangle_datatype
732 *
733 * Attempt to demangle a C++ data type, which may be datatype.
734 * a datatype type is made up of a number of simple types. e.g:
735 * char** = (pointer to (pointer to (char)))
736 */
737 static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
738 struct array* pmt_ref, BOOL in_args)
739 {
740 char dt;
741 BOOL add_pmt = TRUE;
742
743 assert(ct);
744 ct->left = ct->right = NULL;
745
746 switch (dt = *sym->current++)
747 {
748 case '_':
749 /* MS type: __int8,__int16 etc */
750 ct->left = get_extended_type(*sym->current++);
751 break;
752 case 'C': case 'D': case 'E': case 'F': case 'G':
753 case 'H': case 'I': case 'J': case 'K': case 'M':
754 case 'N': case 'O': case 'X': case 'Z':
755 /* Simple data types */
756 ct->left = get_simple_type(dt);
757 add_pmt = FALSE;
758 break;
759 case 'T': /* union */
760 case 'U': /* struct */
761 case 'V': /* class */
762 case 'Y': /* cointerface */
763 /* Class/struct/union/cointerface */
764 {
765 const char* struct_name = NULL;
766 const char* type_name = NULL;
767
768 if (!(struct_name = get_class_name(sym)))
769 goto done;
770 if (!(sym->flags & UNDNAME_NO_COMPLEX_TYPE))
771 {
772 switch (dt)
773 {
774 case 'T': type_name = "union "; break;
775 case 'U': type_name = "struct "; break;
776 case 'V': type_name = "class "; break;
777 case 'Y': type_name = "cointerface "; break;
778 }
779 }
780 ct->left = str_printf(sym, "%s%s", type_name, struct_name);
781 }
782 break;
783 case '?':
784 /* not all the time is seems */
785 if (in_args)
786 {
787 const char* ptr;
788 if (!(ptr = get_number(sym))) goto done;
789 ct->left = str_printf(sym, "`template-parameter-%s'", ptr);
790 }
791 else
792 {
793 if (!get_modified_type(ct, sym, pmt_ref, '?', in_args)) goto done;
794 }
795 break;
796 case 'A': /* reference */
797 case 'B': /* volatile reference */
798 if (!get_modified_type(ct, sym, pmt_ref, dt, in_args)) goto done;
799 break;
800 case 'Q': /* const pointer */
801 case 'R': /* volatile pointer */
802 case 'S': /* const volatile pointer */
803 if (!get_modified_type(ct, sym, pmt_ref, in_args ? dt : 'P', in_args)) goto done;
804 break;
805 case 'P': /* Pointer */
806 if (isdigit(*sym->current))
807 {
808 /* FIXME: P6 = Function pointer, others who knows.. */
809 if (*sym->current++ == '6')
810 {
811 char* args = NULL;
812 const char* call_conv;
813 const char* exported;
814 struct datatype_t sub_ct;
815 unsigned mark = sym->stack.num;
816
817 if (!get_calling_convention(*sym->current++,
818 &call_conv, &exported,
819 sym->flags & ~UNDNAME_NO_ALLOCATION_LANGUAGE) ||
820 !demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
821 goto done;
822
823 args = get_args(sym, pmt_ref, TRUE, '(', ')');
824 if (!args) goto done;
825 sym->stack.num = mark;
826
827 ct->left = str_printf(sym, "%s%s (%s*",
828 sub_ct.left, sub_ct.right, call_conv);
829 ct->right = str_printf(sym, ")%s", args);
830 }
831 else goto done;
832 }
833 else if (!get_modified_type(ct, sym, pmt_ref, 'P', in_args)) goto done;
834 break;
835 case 'W':
836 if (*sym->current == '4')
837 {
838 char* enum_name;
839 sym->current++;
840 if (!(enum_name = get_class_name(sym)))
841 goto done;
842 if (sym->flags & UNDNAME_NO_COMPLEX_TYPE)
843 ct->left = enum_name;
844 else
845 ct->left = str_printf(sym, "enum %s", enum_name);
846 }
847 else goto done;
848 break;
849 case '0': case '1': case '2': case '3': case '4':
850 case '5': case '6': case '7': case '8': case '9':
851 /* Referring back to previously parsed type */
852 /* left and right are pushed as two separate strings */
853 ct->left = str_array_get_ref(pmt_ref, (dt - '0') * 2);
854 ct->right = str_array_get_ref(pmt_ref, (dt - '0') * 2 + 1);
855 if (!ct->left) goto done;
856 add_pmt = FALSE;
857 break;
858 case '$':
859 switch (*sym->current++)
860 {
861 case '0':
862 if (!(ct->left = get_number(sym))) goto done;
863 break;
864 case 'D':
865 {
866 const char* ptr;
867 if (!(ptr = get_number(sym))) goto done;
868 ct->left = str_printf(sym, "`template-parameter%s'", ptr);
869 }
870 break;
871 case 'F':
872 {
873 const char* p1;
874 const char* p2;
875 if (!(p1 = get_number(sym))) goto done;
876 if (!(p2 = get_number(sym))) goto done;
877 ct->left = str_printf(sym, "{%s,%s}", p1, p2);
878 }
879 break;
880 case 'G':
881 {
882 const char* p1;
883 const char* p2;
884 const char* p3;
885 if (!(p1 = get_number(sym))) goto done;
886 if (!(p2 = get_number(sym))) goto done;
887 if (!(p3 = get_number(sym))) goto done;
888 ct->left = str_printf(sym, "{%s,%s,%s}", p1, p2, p3);
889 }
890 break;
891 case 'Q':
892 {
893 const char* ptr;
894 if (!(ptr = get_number(sym))) goto done;
895 ct->left = str_printf(sym, "`non-type-template-parameter%s'", ptr);
896 }
897 break;
898 case '$':
899 if (*sym->current == 'C')
900 {
901 const char* ptr;
902
903 sym->current++;
904 if (!get_modifier(*sym->current++, &ptr)) goto done;
905 if (!demangle_datatype(sym, ct, pmt_ref, in_args)) goto done;
906 ct->left = str_printf(sym, "%s %s", ct->left, ptr);
907 }
908 break;
909 }
910 break;
911 default :
912 ERR("Unknown type %c\n", dt);
913 break;
914 }
915 if (add_pmt && pmt_ref && in_args)
916 {
917 /* left and right are pushed as two separate strings */
918 str_array_push(sym, ct->left ? ct->left : "", -1, pmt_ref);
919 str_array_push(sym, ct->right ? ct->right : "", -1, pmt_ref);
920 }
921 done:
922
923 return ct->left != NULL;
924 }
925
926 /******************************************************************
927 * handle_data
928 * Does the final parsing and handling for a variable or a field in
929 * a class.
930 */
931 static BOOL handle_data(struct parsed_symbol* sym)
932 {
933 const char* access = NULL;
934 const char* member_type = NULL;
935 const char* modifier = NULL;
936 struct datatype_t ct;
937 char* name = NULL;
938 BOOL ret = FALSE;
939
940 /* 0 private static
941 * 1 protected static
942 * 2 public static
943 * 3 private non-static
944 * 4 protected non-static
945 * 5 public non-static
946 * 6 ?? static
947 * 7 ?? static
948 */
949
950 if (!(sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS))
951 {
952 /* we only print the access for static members */
953 switch (*sym->current)
954 {
955 case '0': access = "private: "; break;
956 case '1': access = "protected: "; break;
957 case '2': access = "public: "; break;
958 }
959 }
960
961 if (!(sym->flags & UNDNAME_NO_MEMBER_TYPE))
962 {
963 if (*sym->current >= '0' && *sym->current <= '2')
964 member_type = "static ";
965 }
966
967 name = get_class_string(sym, 0);
968
969 switch (*sym->current++)
970 {
971 case '0': case '1': case '2':
972 case '3': case '4': case '5':
973 {
974 unsigned mark = sym->stack.num;
975 struct array pmt;
976
977 str_array_init(&pmt);
978
979 if (!demangle_datatype(sym, &ct, &pmt, FALSE)) goto done;
980 if (!get_modifier(*sym->current++, &modifier)) goto done;
981 sym->stack.num = mark;
982 }
983 break;
984 case '6' : /* compiler generated static */
985 case '7' : /* compiler generated static */
986 ct.left = ct.right = NULL;
987 if (!get_modifier(*sym->current++, &modifier)) goto done;
988 if (*sym->current != '@')
989 {
990 char* cls = NULL;
991
992 if (!(cls = get_class_name(sym)))
993 goto done;
994 ct.right = str_printf(sym, "{for `%s'}", cls);
995 }
996 break;
997 case '8':
998 case '9':
999 modifier = ct.left = ct.right = NULL;
1000 break;
1001 default: goto done;
1002 }
1003 if (sym->flags & UNDNAME_NAME_ONLY) ct.left = ct.right = modifier = NULL;
1004
1005 sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s", access,
1006 member_type, ct.left,
1007 modifier && ct.left ? " " : NULL, modifier,
1008 modifier || ct.left ? " " : NULL, name, ct.right);
1009 ret = TRUE;
1010 done:
1011 return ret;
1012 }
1013
1014 /******************************************************************
1015 * handle_method
1016 * Does the final parsing and handling for a function or a method in
1017 * a class.
1018 */
1019 static BOOL handle_method(struct parsed_symbol* sym, BOOL cast_op)
1020 {
1021 char accmem;
1022 const char* access = NULL;
1023 const char* member_type = NULL;
1024 struct datatype_t ct_ret;
1025 const char* call_conv;
1026 const char* modifier = NULL;
1027 const char* exported;
1028 const char* args_str = NULL;
1029 const char* name = NULL;
1030 BOOL ret = FALSE;
1031 unsigned mark;
1032 struct array array_pmt;
1033
1034 /* FIXME: why 2 possible letters for each option?
1035 * 'A' private:
1036 * 'B' private:
1037 * 'C' private: static
1038 * 'D' private: static
1039 * 'E' private: virtual
1040 * 'F' private: virtual
1041 * 'G' private: thunk
1042 * 'H' private: thunk
1043 * 'I' protected:
1044 * 'J' protected:
1045 * 'K' protected: static
1046 * 'L' protected: static
1047 * 'M' protected: virtual
1048 * 'N' protected: virtual
1049 * 'O' protected: thunk
1050 * 'P' protected: thunk
1051 * 'Q' public:
1052 * 'R' public:
1053 * 'S' public: static
1054 * 'T' public: static
1055 * 'U' public: virtual
1056 * 'V' public: virtual
1057 * 'W' public: thunk
1058 * 'X' public: thunk
1059 * 'Y'
1060 * 'Z'
1061 */
1062 accmem = *sym->current++;
1063 if (accmem < 'A' || accmem > 'Z') goto done;
1064
1065 if (!(sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS))
1066 {
1067 switch ((accmem - 'A') / 8)
1068 {
1069 case 0: access = "private: "; break;
1070 case 1: access = "protected: "; break;
1071 case 2: access = "public: "; break;
1072 }
1073 }
1074 if (!(sym->flags & UNDNAME_NO_MEMBER_TYPE))
1075 {
1076 if (accmem <= 'X')
1077 {
1078 switch ((accmem - 'A') % 8)
1079 {
1080 case 2: case 3: member_type = "static "; break;
1081 case 4: case 5: member_type = "virtual "; break;
1082 case 6: case 7:
1083 access = str_printf(sym, "[thunk]:%s", access);
1084 member_type = "virtual ";
1085 break;
1086 }
1087 }
1088 }
1089
1090 name = get_class_string(sym, 0);
1091
1092 if ((accmem - 'A') % 8 == 6 || (accmem - '8') % 8 == 7) /* a thunk */
1093 name = str_printf(sym, "%s`adjustor{%s}' ", name, get_number(sym));
1094
1095 if (accmem <= 'X')
1096 {
1097 if (((accmem - 'A') % 8) != 2 && ((accmem - 'A') % 8) != 3)
1098 {
1099 /* Implicit 'this' pointer */
1100 /* If there is an implicit this pointer, const modifier follows */
1101 if (!get_modifier(*sym->current, &modifier)) goto done;
1102 sym->current++;
1103 }
1104 }
1105
1106 if (!get_calling_convention(*sym->current++, &call_conv, &exported,
1107 sym->flags))
1108 goto done;
1109
1110 str_array_init(&array_pmt);
1111
1112 /* Return type, or @ if 'void' */
1113 if (*sym->current == '@')
1114 {
1115 ct_ret.left = "void";
1116 ct_ret.right = NULL;
1117 sym->current++;
1118 }
1119 else
1120 {
1121 if (!demangle_datatype(sym, &ct_ret, &array_pmt, FALSE))
1122 goto done;
1123 }
1124 if (sym->flags & UNDNAME_NO_FUNCTION_RETURNS)
1125 ct_ret.left = ct_ret.right = NULL;
1126 if (cast_op)
1127 {
1128 name = str_printf(sym, "%s%s%s", name, ct_ret.left, ct_ret.right);
1129 ct_ret.left = ct_ret.right = NULL;
1130 }
1131
1132 mark = sym->stack.num;
1133 if (!(args_str = get_args(sym, &array_pmt, TRUE, '(', ')'))) goto done;
1134 if (sym->flags & UNDNAME_NAME_ONLY) args_str = modifier = NULL;
1135 sym->stack.num = mark;
1136
1137 /* Note: '()' after 'Z' means 'throws', but we don't care here
1138 * Yet!!! FIXME
1139 */
1140 sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s%s%s%s%s",
1141 access, member_type, ct_ret.left,
1142 (ct_ret.left && !ct_ret.right) ? " " : NULL,
1143 call_conv, call_conv ? " " : NULL, exported,
1144 name, args_str, modifier,
1145 modifier ? " " : NULL, ct_ret.right);
1146 ret = TRUE;
1147 done:
1148 return ret;
1149 }
1150
1151 /******************************************************************
1152 * handle_template
1153 * Does the final parsing and handling for a name with templates
1154 */
1155 static BOOL handle_template(struct parsed_symbol* sym)
1156 {
1157 const char* name;
1158 const char* args;
1159
1160 assert(*sym->current++ == '$');
1161 if (!(name = get_literal_string(sym))) return FALSE;
1162 if (!(args = get_args(sym, NULL, FALSE, '<', '>'))) return FALSE;
1163 sym->result = str_printf(sym, "%s%s", name, args);
1164 return TRUE;
1165 }
1166
1167 /*******************************************************************
1168 * symbol_demangle
1169 * Demangle a C++ linker symbol
1170 */
1171 static BOOL symbol_demangle(struct parsed_symbol* sym)
1172 {
1173 BOOL ret = FALSE;
1174 unsigned do_after = 0;
1175 static CHAR dashed_null[] = "--null--";
1176
1177 /* FIXME seems wrong as name, as it demangles a simple data type */
1178 if (sym->flags & UNDNAME_NO_ARGUMENTS)
1179 {
1180 struct datatype_t ct;
1181
1182 if (demangle_datatype(sym, &ct, NULL, FALSE))
1183 {
1184 sym->result = str_printf(sym, "%s%s", ct.left, ct.right);
1185 ret = TRUE;
1186 }
1187 goto done;
1188 }
1189
1190 /* MS mangled names always begin with '?' */
1191 if (*sym->current != '?') return FALSE;
1192 str_array_init(&sym->names);
1193 str_array_init(&sym->stack);
1194 sym->current++;
1195
1196 /* Then function name or operator code */
1197 if (*sym->current == '?' && sym->current[1] != '$')
1198 {
1199 const char* function_name = NULL;
1200
1201 /* C++ operator code (one character, or two if the first is '_') */
1202 switch (*++sym->current)
1203 {
1204 case '0': do_after = 1; break;
1205 case '1': do_after = 2; break;
1206 case '2': function_name = "operator new"; break;
1207 case '3': function_name = "operator delete"; break;
1208 case '4': function_name = "operator="; break;
1209 case '5': function_name = "operator>>"; break;
1210 case '6': function_name = "operator<<"; break;
1211 case '7': function_name = "operator!"; break;
1212 case '8': function_name = "operator=="; break;
1213 case '9': function_name = "operator!="; break;
1214 case 'A': function_name = "operator[]"; break;
1215 case 'B': function_name = "operator "; do_after = 3; break;
1216 case 'C': function_name = "operator->"; break;
1217 case 'D': function_name = "operator*"; break;
1218 case 'E': function_name = "operator++"; break;
1219 case 'F': function_name = "operator--"; break;
1220 case 'G': function_name = "operator-"; break;
1221 case 'H': function_name = "operator+"; break;
1222 case 'I': function_name = "operator&"; break;
1223 case 'J': function_name = "operator->*"; break;
1224 case 'K': function_name = "operator/"; break;
1225 case 'L': function_name = "operator%"; break;
1226 case 'M': function_name = "operator<"; break;
1227 case 'N': function_name = "operator<="; break;
1228 case 'O': function_name = "operator>"; break;
1229 case 'P': function_name = "operator>="; break;
1230 case 'Q': function_name = "operator,"; break;
1231 case 'R': function_name = "operator()"; break;
1232 case 'S': function_name = "operator~"; break;
1233 case 'T': function_name = "operator^"; break;
1234 case 'U': function_name = "operator|"; break;
1235 case 'V': function_name = "operator&&"; break;
1236 case 'W': function_name = "operator||"; break;
1237 case 'X': function_name = "operator*="; break;
1238 case 'Y': function_name = "operator+="; break;
1239 case 'Z': function_name = "operator-="; break;
1240 case '_':
1241 switch (*++sym->current)
1242 {
1243 case '0': function_name = "operator/="; break;
1244 case '1': function_name = "operator%="; break;
1245 case '2': function_name = "operator>>="; break;
1246 case '3': function_name = "operator<<="; break;
1247 case '4': function_name = "operator&="; break;
1248 case '5': function_name = "operator|="; break;
1249 case '6': function_name = "operator^="; break;
1250 case '7': function_name = "`vftable'"; break;
1251 case '8': function_name = "`vbtable'"; break;
1252 case '9': function_name = "`vcall'"; break;
1253 case 'A': function_name = "`typeof'"; break;
1254 case 'B': function_name = "`local static guard'"; break;
1255 case 'C': function_name = "`string'"; do_after = 4; break;
1256 case 'D': function_name = "`vbase destructor'"; break;
1257 case 'E': function_name = "`vector deleting destructor'"; break;
1258 case 'F': function_name = "`default constructor closure'"; break;
1259 case 'G': function_name = "`scalar deleting destructor'"; break;
1260 case 'H': function_name = "`vector constructor iterator'"; break;
1261 case 'I': function_name = "`vector destructor iterator'"; break;
1262 case 'J': function_name = "`vector vbase constructor iterator'"; break;
1263 case 'K': function_name = "`virtual displacement map'"; break;
1264 case 'L': function_name = "`eh vector constructor iterator'"; break;
1265 case 'M': function_name = "`eh vector destructor iterator'"; break;
1266 case 'N': function_name = "`eh vector vbase constructor iterator'"; break;
1267 case 'O': function_name = "`copy constructor closure'"; break;
1268 case 'R':
1269 sym->flags |= UNDNAME_NO_FUNCTION_RETURNS;
1270 switch (*++sym->current)
1271 {
1272 case '0':
1273 {
1274 struct datatype_t ct;
1275 struct array pmt;
1276
1277 sym->current++;
1278 str_array_init(&pmt);
1279 demangle_datatype(sym, &ct, &pmt, FALSE);
1280 function_name = str_printf(sym, "%s%s `RTTI Type Descriptor'",
1281 ct.left, ct.right);
1282 sym->current--;
1283 }
1284 break;
1285 case '1':
1286 {
1287 const char* n1, *n2, *n3, *n4;
1288 sym->current++;
1289 n1 = get_number(sym);
1290 n2 = get_number(sym);
1291 n3 = get_number(sym);
1292 n4 = get_number(sym);
1293 sym->current--;
1294 function_name = str_printf(sym, "`RTTI Base Class Descriptor at (%s,%s,%s,%s)'",
1295 n1, n2, n3, n4);
1296 }
1297 break;
1298 case '2': function_name = "`RTTI Base Class Array'"; break;
1299 case '3': function_name = "`RTTI Class Hierarchy Descriptor'"; break;
1300 case '4': function_name = "`RTTI Complete Object Locator'"; break;
1301 default:
1302 ERR("Unknown RTTI operator: _R%c\n", *sym->current);
1303 break;
1304 }
1305 break;
1306 case 'S': function_name = "`local vftable'"; break;
1307 case 'T': function_name = "`local vftable constructor closure'"; break;
1308 case 'U': function_name = "operator new[]"; break;
1309 case 'V': function_name = "operator delete[]"; break;
1310 case 'X': function_name = "`placement delete closure'"; break;
1311 case 'Y': function_name = "`placement delete[] closure'"; break;
1312 default:
1313 ERR("Unknown operator: _%c\n", *sym->current);
1314 return FALSE;
1315 }
1316 break;
1317 default:
1318 /* FIXME: Other operators */
1319 ERR("Unknown operator: %c\n", *sym->current);
1320 return FALSE;
1321 }
1322 sym->current++;
1323 switch (do_after)
1324 {
1325 case 1: case 2:
1326 sym->stack.num = sym->stack.max = 1;
1327 sym->stack.elts[0] = dashed_null;
1328 break;
1329 case 4:
1330 sym->result = (char*)function_name;
1331 ret = TRUE;
1332 goto done;
1333 default:
1334 str_array_push(sym, function_name, -1, &sym->stack);
1335 break;
1336 }
1337 }
1338 else if (*sym->current == '$')
1339 {
1340 /* Strange construct, it's a name with a template argument list
1341 and that's all. */
1342 sym->current++;
1343 ret = (sym->result = get_template_name(sym)) != NULL;
1344 goto done;
1345 }
1346 else if (*sym->current == '?' && sym->current[1] == '$')
1347 do_after = 5;
1348
1349 /* Either a class name, or '@' if the symbol is not a class member */
1350 switch (*sym->current)
1351 {
1352 case '@': sym->current++; break;
1353 case '$': break;
1354 default:
1355 /* Class the function is associated with, terminated by '@@' */
1356 if (!get_class(sym)) goto done;
1357 break;
1358 }
1359
1360 switch (do_after)
1361 {
1362 case 0: default: break;
1363 case 1: case 2:
1364 /* it's time to set the member name for ctor & dtor */
1365 if (sym->stack.num <= 1) goto done;
1366 if (do_after == 1)
1367 sym->stack.elts[0] = sym->stack.elts[1];
1368 else
1369 sym->stack.elts[0] = str_printf(sym, "~%s", sym->stack.elts[1]);
1370 /* ctors and dtors don't have return type */
1371 sym->flags |= UNDNAME_NO_FUNCTION_RETURNS;
1372 break;
1373 case 3:
1374 sym->flags &= ~UNDNAME_NO_FUNCTION_RETURNS;
1375 break;
1376 case 5:
1377 sym->names.start = 1;
1378 break;
1379 }
1380
1381 /* Function/Data type and access level */
1382 if (*sym->current >= '0' && *sym->current <= '9')
1383 ret = handle_data(sym);
1384 else if (*sym->current >= 'A' && *sym->current <= 'Z')
1385 ret = handle_method(sym, do_after == 3);
1386 else if (*sym->current == '$')
1387 ret = handle_template(sym);
1388 else ret = FALSE;
1389 done:
1390 if (ret) assert(sym->result);
1391 else WARN("Failed at %s\n", sym->current);
1392
1393 return ret;
1394 }
1395
1396 /*********************************************************************
1397 * __unDNameEx (MSVCRT.@)
1398 *
1399 * Demangle a C++ identifier.
1400 *
1401 * PARAMS
1402 * buffer [O] If not NULL, the place to put the demangled string
1403 * mangled [I] Mangled name of the function
1404 * buflen [I] Length of buffer
1405 * memget [I] Function to allocate memory with
1406 * memfree [I] Function to free memory with
1407 * unknown [?] Unknown, possibly a call back
1408 * flags [I] Flags determining demangled format
1409 *
1410 * RETURNS
1411 * Success: A string pointing to the unmangled name, allocated with memget.
1412 * Failure: NULL.
1413 */
1414 char* CDECL __unDNameEx(char* buffer, const char* mangled, int buflen,
1415 malloc_func_t memget, free_func_t memfree,
1416 void* unknown, unsigned short int flags)
1417 {
1418 struct parsed_symbol sym;
1419 const char* result;
1420
1421 TRACE("(%p,%s,%d,%p,%p,%p,%x)\n",
1422 buffer, mangled, buflen, memget, memfree, unknown, flags);
1423
1424 /* The flags details is not documented by MS. However, it looks exactly
1425 * like the UNDNAME_ manifest constants from imagehlp.h and dbghelp.h
1426 * So, we copied those (on top of the file)
1427 */
1428 memset(&sym, 0, sizeof(struct parsed_symbol));
1429 if (flags & UNDNAME_NAME_ONLY)
1430 flags |= UNDNAME_NO_FUNCTION_RETURNS | UNDNAME_NO_ACCESS_SPECIFIERS |
1431 UNDNAME_NO_MEMBER_TYPE | UNDNAME_NO_ALLOCATION_LANGUAGE |
1432 UNDNAME_NO_COMPLEX_TYPE;
1433
1434 sym.flags = flags;
1435 sym.mem_alloc_ptr = memget;
1436 sym.mem_free_ptr = memfree;
1437 sym.current = mangled;
1438
1439 result = symbol_demangle(&sym) ? sym.result : mangled;
1440 if (buffer && buflen)
1441 {
1442 lstrcpynA( buffer, result, buflen);
1443 }
1444 else
1445 {
1446 buffer = memget(strlen(result) + 1);
1447 if (buffer) strcpy(buffer, result);
1448 }
1449
1450 und_free_all(&sym);
1451
1452 return buffer;
1453 }
1454
1455
1456 /*********************************************************************
1457 * __unDName (MSVCRT.@)
1458 */
1459 char* CDECL __unDName(char* buffer, const char* mangled, int buflen,
1460 malloc_func_t memget, free_func_t memfree,
1461 unsigned short int flags)
1462 {
1463 return __unDNameEx(buffer, mangled, buflen, memget, memfree, NULL, flags);
1464 }