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