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