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