1 /*- 2 * Copyright (c) 2007, 2008 Hyogeol Lee <hyogeollee@gmail.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 #include <sys/types.h> 27 #include <assert.h> 28 #include <ctype.h> 29 #include <errno.h> 30 #include <limits.h> 31 #include <stdbool.h> 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 36 /** 37 * @file cpp_demangle.c 38 * @brief Decode IA-64 C++ ABI style implementation. 39 * 40 * IA-64 standard ABI(Itanium C++ ABI) references. 41 * 42 * http://www.codesourcery.com/cxx-abi/abi.html#mangling \n 43 * http://www.codesourcery.com/cxx-abi/abi-mangling.html 44 */ 45 46 /** @brief Dynamic vector data for string. */ 47 struct vector_str { 48 /** Current size */ 49 size_t size; 50 /** Total capacity */ 51 size_t capacity; 52 /** String array */ 53 char **container; 54 }; 55 56 #define BUFFER_GROWFACTOR 1.618 57 #define VECTOR_DEF_CAPACITY 8 58 #define ELFTC_ISDIGIT(C) (isdigit((C) & 0xFF)) 59 60 enum type_qualifier { 61 TYPE_PTR, TYPE_REF, TYPE_CMX, TYPE_IMG, TYPE_EXT, TYPE_RST, TYPE_VAT, 62 TYPE_CST, TYPE_VEC 63 }; 64 65 struct vector_type_qualifier { 66 size_t size, capacity; 67 enum type_qualifier *q_container; 68 struct vector_str ext_name; 69 }; 70 71 enum read_cmd { 72 READ_FAIL, READ_NEST, READ_TMPL, READ_EXPR, READ_EXPL, READ_LOCAL, 73 READ_TYPE, READ_FUNC, READ_PTRMEM 74 }; 75 76 struct vector_read_cmd { 77 size_t size, capacity; 78 enum read_cmd *r_container; 79 }; 80 81 struct cpp_demangle_data { 82 struct vector_str output; /* output string vector */ 83 struct vector_str output_tmp; 84 struct vector_str subst; /* substitution string vector */ 85 struct vector_str tmpl; 86 struct vector_str class_type; 87 struct vector_read_cmd cmd; 88 bool paren; /* parenthesis opened */ 89 bool pfirst; /* first element of parameter */ 90 bool mem_rst; /* restrict member function */ 91 bool mem_vat; /* volatile member function */ 92 bool mem_cst; /* const member function */ 93 int func_type; 94 const char *cur; /* current mangled name ptr */ 95 const char *last_sname; /* last source name */ 96 int push_head; 97 }; 98 99 #define CPP_DEMANGLE_TRY_LIMIT 128 100 #define FLOAT_SPRINTF_TRY_LIMIT 5 101 #define FLOAT_QUADRUPLE_BYTES 16 102 #define FLOAT_EXTENED_BYTES 10 103 104 #define SIMPLE_HASH(x,y) (64 * x + y) 105 106 static size_t get_strlen_sum(const struct vector_str *v); 107 static bool vector_str_grow(struct vector_str *v); 108 109 static size_t 110 get_strlen_sum(const struct vector_str *v) 111 { 112 size_t i, len = 0; 113 114 if (v == NULL) 115 return (0); 116 117 assert(v->size > 0); 118 119 for (i = 0; i < v->size; ++i) 120 len += strlen(v->container[i]); 121 122 return (len); 123 } 124 125 /** 126 * @brief Deallocate resource in vector_str. 127 */ 128 static void 129 vector_str_dest(struct vector_str *v) 130 { 131 size_t i; 132 133 if (v == NULL) 134 return; 135 136 for (i = 0; i < v->size; ++i) 137 free(v->container[i]); 138 139 free(v->container); 140 } 141 142 /** 143 * @brief Find string in vector_str. 144 * @param v Destination vector. 145 * @param o String to find. 146 * @param l Length of the string. 147 * @return -1 at failed, 0 at not found, 1 at found. 148 */ 149 static int 150 vector_str_find(const struct vector_str *v, const char *o, size_t l) 151 { 152 size_t i; 153 154 if (v == NULL || o == NULL) 155 return (-1); 156 157 for (i = 0; i < v->size; ++i) 158 if (strncmp(v->container[i], o, l) == 0) 159 return (1); 160 161 return (0); 162 } 163 164 /** 165 * @brief Get new allocated flat string from vector. 166 * 167 * If l is not NULL, return length of the string. 168 * @param v Destination vector. 169 * @param l Length of the string. 170 * @return NULL at failed or NUL terminated new allocated string. 171 */ 172 static char * 173 vector_str_get_flat(const struct vector_str *v, size_t *l) 174 { 175 ssize_t elem_pos, elem_size, rtn_size; 176 size_t i; 177 char *rtn; 178 179 if (v == NULL || v->size == 0) 180 return (NULL); 181 182 if ((rtn_size = get_strlen_sum(v)) == 0) 183 return (NULL); 184 185 if ((rtn = malloc(sizeof(char) * (rtn_size + 1))) == NULL) 186 return (NULL); 187 188 elem_pos = 0; 189 for (i = 0; i < v->size; ++i) { 190 elem_size = strlen(v->container[i]); 191 192 memcpy(rtn + elem_pos, v->container[i], elem_size); 193 194 elem_pos += elem_size; 195 } 196 197 rtn[rtn_size] = '\0'; 198 199 if (l != NULL) 200 *l = rtn_size; 201 202 return (rtn); 203 } 204 205 static bool 206 vector_str_grow(struct vector_str *v) 207 { 208 size_t i, tmp_cap; 209 char **tmp_ctn; 210 211 if (v == NULL) 212 return (false); 213 214 assert(v->capacity > 0); 215 216 tmp_cap = v->capacity * BUFFER_GROWFACTOR; 217 218 assert(tmp_cap > v->capacity); 219 220 if ((tmp_ctn = malloc(sizeof(char *) * tmp_cap)) == NULL) 221 return (false); 222 223 for (i = 0; i < v->size; ++i) 224 tmp_ctn[i] = v->container[i]; 225 226 free(v->container); 227 228 v->container = tmp_ctn; 229 v->capacity = tmp_cap; 230 231 return (true); 232 } 233 234 /** 235 * @brief Initialize vector_str. 236 * @return false at failed, true at success. 237 */ 238 static bool 239 vector_str_init(struct vector_str *v) 240 { 241 242 if (v == NULL) 243 return (false); 244 245 v->size = 0; 246 v->capacity = VECTOR_DEF_CAPACITY; 247 248 assert(v->capacity > 0); 249 250 if ((v->container = malloc(sizeof(char *) * v->capacity)) == NULL) 251 return (false); 252 253 assert(v->container != NULL); 254 255 return (true); 256 } 257 258 /** 259 * @brief Remove last element in vector_str. 260 * @return false at failed, true at success. 261 */ 262 static bool 263 vector_str_pop(struct vector_str *v) 264 { 265 266 if (v == NULL) 267 return (false); 268 269 if (v->size == 0) 270 return (true); 271 272 --v->size; 273 274 free(v->container[v->size]); 275 v->container[v->size] = NULL; 276 277 return (true); 278 } 279 280 /** 281 * @brief Push back string to vector. 282 * @return false at failed, true at success. 283 */ 284 static bool 285 vector_str_push(struct vector_str *v, const char *str, size_t len) 286 { 287 288 if (v == NULL || str == NULL) 289 return (false); 290 291 if (v->size == v->capacity && vector_str_grow(v) == false) 292 return (false); 293 294 if ((v->container[v->size] = malloc(sizeof(char) * (len + 1))) == NULL) 295 return (false); 296 297 snprintf(v->container[v->size], len + 1, "%s", str); 298 299 ++v->size; 300 301 return (true); 302 } 303 304 /** 305 * @brief Push front org vector to det vector. 306 * @return false at failed, true at success. 307 */ 308 static bool 309 vector_str_push_vector_head(struct vector_str *dst, struct vector_str *org) 310 { 311 size_t i, j, tmp_cap; 312 char **tmp_ctn; 313 314 if (dst == NULL || org == NULL) 315 return (false); 316 317 tmp_cap = (dst->size + org->size) * BUFFER_GROWFACTOR; 318 319 if ((tmp_ctn = malloc(sizeof(char *) * tmp_cap)) == NULL) 320 return (false); 321 322 for (i = 0; i < org->size; ++i) 323 if ((tmp_ctn[i] = strdup(org->container[i])) == NULL) { 324 for (j = 0; j < i; ++j) 325 free(tmp_ctn[j]); 326 327 free(tmp_ctn); 328 329 return (false); 330 } 331 332 for (i = 0; i < dst->size; ++i) 333 tmp_ctn[i + org->size] = dst->container[i]; 334 335 free(dst->container); 336 337 dst->container = tmp_ctn; 338 dst->capacity = tmp_cap; 339 dst->size += org->size; 340 341 return (true); 342 } 343 344 /** 345 * @brief Get new allocated flat string from vector between begin and end. 346 * 347 * If r_len is not NULL, string length will be returned. 348 * @return NULL at failed or NUL terminated new allocated string. 349 */ 350 static char * 351 vector_str_substr(const struct vector_str *v, size_t begin, size_t end, 352 size_t *r_len) 353 { 354 size_t cur, i, len; 355 char *rtn; 356 357 if (v == NULL || begin > end) 358 return (NULL); 359 360 len = 0; 361 for (i = begin; i < end + 1; ++i) 362 len += strlen(v->container[i]); 363 364 if ((rtn = malloc(sizeof(char) * (len + 1))) == NULL) 365 return (NULL); 366 367 if (r_len != NULL) 368 *r_len = len; 369 370 cur = 0; 371 for (i = begin; i < end + 1; ++i) { 372 len = strlen(v->container[i]); 373 memcpy(rtn + cur, v->container[i], len); 374 cur += len; 375 } 376 rtn[cur] = '\0'; 377 378 return (rtn); 379 } 380 381 static void cpp_demangle_data_dest(struct cpp_demangle_data *); 382 static int cpp_demangle_data_init(struct cpp_demangle_data *, 383 const char *); 384 static int cpp_demangle_get_subst(struct cpp_demangle_data *, size_t); 385 static int cpp_demangle_get_tmpl_param(struct cpp_demangle_data *, size_t); 386 static int cpp_demangle_push_fp(struct cpp_demangle_data *, 387 char *(*)(const char *, size_t)); 388 static int cpp_demangle_push_str(struct cpp_demangle_data *, const char *, 389 size_t); 390 static int cpp_demangle_push_subst(struct cpp_demangle_data *, 391 const char *, size_t); 392 static int cpp_demangle_push_subst_v(struct cpp_demangle_data *, 393 struct vector_str *); 394 static int cpp_demangle_push_type_qualifier(struct cpp_demangle_data *, 395 struct vector_type_qualifier *, const char *); 396 static int cpp_demangle_read_array(struct cpp_demangle_data *); 397 static int cpp_demangle_read_encoding(struct cpp_demangle_data *); 398 static int cpp_demangle_read_expr_primary(struct cpp_demangle_data *); 399 static int cpp_demangle_read_expression(struct cpp_demangle_data *); 400 static int cpp_demangle_read_expression_flat(struct cpp_demangle_data *, 401 char **); 402 static int cpp_demangle_read_expression_binary(struct cpp_demangle_data *, 403 const char *, size_t); 404 static int cpp_demangle_read_expression_unary(struct cpp_demangle_data *, 405 const char *, size_t); 406 static int cpp_demangle_read_expression_trinary(struct cpp_demangle_data *, 407 const char *, size_t, const char *, size_t); 408 static int cpp_demangle_read_function(struct cpp_demangle_data *, int *, 409 struct vector_type_qualifier *); 410 static int cpp_demangle_local_source_name(struct cpp_demangle_data *ddata); 411 static int cpp_demangle_read_local_name(struct cpp_demangle_data *); 412 static int cpp_demangle_read_name(struct cpp_demangle_data *); 413 static int cpp_demangle_read_name_flat(struct cpp_demangle_data *, 414 char**); 415 static int cpp_demangle_read_nested_name(struct cpp_demangle_data *); 416 static int cpp_demangle_read_number(struct cpp_demangle_data *, long *); 417 static int cpp_demangle_read_number_as_string(struct cpp_demangle_data *, 418 char **); 419 static int cpp_demangle_read_nv_offset(struct cpp_demangle_data *); 420 static int cpp_demangle_read_offset(struct cpp_demangle_data *); 421 static int cpp_demangle_read_offset_number(struct cpp_demangle_data *); 422 static int cpp_demangle_read_pointer_to_member(struct cpp_demangle_data *); 423 static int cpp_demangle_read_sname(struct cpp_demangle_data *); 424 static int cpp_demangle_read_subst(struct cpp_demangle_data *); 425 static int cpp_demangle_read_subst_std(struct cpp_demangle_data *); 426 static int cpp_demangle_read_subst_stdtmpl(struct cpp_demangle_data *, 427 const char *, size_t); 428 static int cpp_demangle_read_tmpl_arg(struct cpp_demangle_data *); 429 static int cpp_demangle_read_tmpl_args(struct cpp_demangle_data *); 430 static int cpp_demangle_read_tmpl_param(struct cpp_demangle_data *); 431 static int cpp_demangle_read_type(struct cpp_demangle_data *, int); 432 static int cpp_demangle_read_type_flat(struct cpp_demangle_data *, 433 char **); 434 static int cpp_demangle_read_uqname(struct cpp_demangle_data *); 435 static int cpp_demangle_read_v_offset(struct cpp_demangle_data *); 436 static char *decode_fp_to_double(const char *, size_t); 437 static char *decode_fp_to_float(const char *, size_t); 438 static char *decode_fp_to_float128(const char *, size_t); 439 static char *decode_fp_to_float80(const char *, size_t); 440 static char *decode_fp_to_long_double(const char *, size_t); 441 static int hex_to_dec(char); 442 static void vector_read_cmd_dest(struct vector_read_cmd *); 443 static int vector_read_cmd_find(struct vector_read_cmd *, enum read_cmd); 444 static int vector_read_cmd_init(struct vector_read_cmd *); 445 static int vector_read_cmd_pop(struct vector_read_cmd *); 446 static int vector_read_cmd_push(struct vector_read_cmd *, enum read_cmd); 447 static void vector_type_qualifier_dest(struct vector_type_qualifier *); 448 static int vector_type_qualifier_init(struct vector_type_qualifier *); 449 static int vector_type_qualifier_push(struct vector_type_qualifier *, 450 enum type_qualifier); 451 452 /** 453 * @brief Decode the input string by IA-64 C++ ABI style. 454 * 455 * GNU GCC v3 use IA-64 standard ABI. 456 * @return New allocated demangled string or NULL if failed. 457 * @todo 1. Testing and more test case. 2. Code cleaning. 458 */ 459 char * 460 __cxa_demangle_gnu3(const char *org) 461 { 462 struct cpp_demangle_data ddata; 463 ssize_t org_len; 464 unsigned int limit; 465 char *rtn = NULL; 466 467 if (org == NULL) 468 return (NULL); 469 470 org_len = strlen(org); 471 if (org_len > 11 && !strncmp(org, "_GLOBAL__I_", 11)) { 472 if ((rtn = malloc(org_len + 19)) == NULL) 473 return (NULL); 474 snprintf(rtn, org_len + 19, 475 "global constructors keyed to %s", org + 11); 476 return (rtn); 477 } 478 479 // Try demangling as a type for short encodings 480 if ((org_len < 2) || (org[0] != '_' || org[1] != 'Z' )) { 481 if (!cpp_demangle_data_init(&ddata, org)) 482 return (NULL); 483 if (!cpp_demangle_read_type(&ddata, 0)) 484 goto clean; 485 rtn = vector_str_get_flat(&ddata.output, (size_t *) NULL); 486 goto clean; 487 } 488 489 490 if (!cpp_demangle_data_init(&ddata, org + 2)) 491 return (NULL); 492 493 rtn = NULL; 494 495 if (!cpp_demangle_read_encoding(&ddata)) 496 goto clean; 497 498 limit = 0; 499 while (*ddata.cur != '\0') { 500 /* 501 * Breaking at some gcc info at tail. e.g) @@GLIBCXX_3.4 502 */ 503 if (*ddata.cur == '@' && *(ddata.cur + 1) == '@') 504 break; 505 if (!cpp_demangle_read_type(&ddata, 1)) 506 goto clean; 507 if (limit++ > CPP_DEMANGLE_TRY_LIMIT) 508 goto clean; 509 } 510 511 if (ddata.output.size == 0) 512 goto clean; 513 if (ddata.paren && !vector_str_push(&ddata.output, ")", 1)) 514 goto clean; 515 if (ddata.mem_vat && !vector_str_push(&ddata.output, " volatile", 9)) 516 goto clean; 517 if (ddata.mem_cst && !vector_str_push(&ddata.output, " const", 6)) 518 goto clean; 519 if (ddata.mem_rst && !vector_str_push(&ddata.output, " restrict", 9)) 520 goto clean; 521 522 rtn = vector_str_get_flat(&ddata.output, (size_t *) NULL); 523 524 clean: 525 cpp_demangle_data_dest(&ddata); 526 527 return (rtn); 528 } 529 530 static void 531 cpp_demangle_data_dest(struct cpp_demangle_data *d) 532 { 533 534 if (d == NULL) 535 return; 536 537 vector_read_cmd_dest(&d->cmd); 538 vector_str_dest(&d->class_type); 539 vector_str_dest(&d->tmpl); 540 vector_str_dest(&d->subst); 541 vector_str_dest(&d->output_tmp); 542 vector_str_dest(&d->output); 543 } 544 545 static int 546 cpp_demangle_data_init(struct cpp_demangle_data *d, const char *cur) 547 { 548 549 if (d == NULL || cur == NULL) 550 return (0); 551 552 if (!vector_str_init(&d->output)) 553 return (0); 554 if (!vector_str_init(&d->output_tmp)) 555 goto clean1; 556 if (!vector_str_init(&d->subst)) 557 goto clean2; 558 if (!vector_str_init(&d->tmpl)) 559 goto clean3; 560 if (!vector_str_init(&d->class_type)) 561 goto clean4; 562 if (!vector_read_cmd_init(&d->cmd)) 563 goto clean5; 564 565 assert(d->output.container != NULL); 566 assert(d->output_tmp.container != NULL); 567 assert(d->subst.container != NULL); 568 assert(d->tmpl.container != NULL); 569 assert(d->class_type.container != NULL); 570 571 d->paren = false; 572 d->pfirst = false; 573 d->mem_rst = false; 574 d->mem_vat = false; 575 d->mem_cst = false; 576 d->func_type = 0; 577 d->cur = cur; 578 d->last_sname = NULL; 579 d->push_head = 0; 580 581 return (1); 582 583 clean5: 584 vector_str_dest(&d->class_type); 585 clean4: 586 vector_str_dest(&d->tmpl); 587 clean3: 588 vector_str_dest(&d->subst); 589 clean2: 590 vector_str_dest(&d->output_tmp); 591 clean1: 592 vector_str_dest(&d->output); 593 594 return (0); 595 } 596 597 static int 598 cpp_demangle_push_fp(struct cpp_demangle_data *ddata, 599 char *(*decoder)(const char *, size_t)) 600 { 601 size_t len; 602 int rtn; 603 const char *fp; 604 char *f; 605 606 if (ddata == NULL || decoder == NULL) 607 return (0); 608 609 fp = ddata->cur; 610 while (*ddata->cur != 'E') 611 ++ddata->cur; 612 613 if ((f = decoder(fp, ddata->cur - fp)) == NULL) 614 return (0); 615 616 rtn = 0; 617 if ((len = strlen(f)) > 0) 618 rtn = cpp_demangle_push_str(ddata, f, len); 619 620 free(f); 621 622 ++ddata->cur; 623 624 return (rtn); 625 } 626 627 static int 628 cpp_demangle_push_str(struct cpp_demangle_data *ddata, const char *str, 629 size_t len) 630 { 631 632 if (ddata == NULL || str == NULL || len == 0) 633 return (0); 634 635 if (ddata->push_head > 0) 636 return (vector_str_push(&ddata->output_tmp, str, len)); 637 638 return (vector_str_push(&ddata->output, str, len)); 639 } 640 641 static int 642 cpp_demangle_push_subst(struct cpp_demangle_data *ddata, const char *str, 643 size_t len) 644 { 645 646 if (ddata == NULL || str == NULL || len == 0) 647 return (0); 648 649 if (!vector_str_find(&ddata->subst, str, len)) 650 return (vector_str_push(&ddata->subst, str, len)); 651 652 return (1); 653 } 654 655 static int 656 cpp_demangle_push_subst_v(struct cpp_demangle_data *ddata, struct vector_str *v) 657 { 658 size_t str_len; 659 int rtn; 660 char *str; 661 662 if (ddata == NULL || v == NULL) 663 return (0); 664 665 if ((str = vector_str_get_flat(v, &str_len)) == NULL) 666 return (0); 667 668 rtn = cpp_demangle_push_subst(ddata, str, str_len); 669 670 free(str); 671 672 return (rtn); 673 } 674 675 static int 676 cpp_demangle_push_type_qualifier(struct cpp_demangle_data *ddata, 677 struct vector_type_qualifier *v, const char *type_str) 678 { 679 struct vector_str subst_v; 680 size_t idx, e_idx, e_len; 681 int rtn; 682 char *buf; 683 684 if (ddata == NULL || v == NULL) 685 return (0); 686 687 if ((idx = v->size) == 0) 688 return (1); 689 690 rtn = 0; 691 if (type_str != NULL) { 692 if (!vector_str_init(&subst_v)) 693 return (0); 694 if (!vector_str_push(&subst_v, type_str, strlen(type_str))) 695 goto clean; 696 } 697 698 e_idx = 0; 699 while (idx > 0) { 700 switch (v->q_container[idx - 1]) { 701 case TYPE_PTR: 702 if (!cpp_demangle_push_str(ddata, "*", 1)) 703 goto clean; 704 if (type_str != NULL) { 705 if (!vector_str_push(&subst_v, "*", 1)) 706 goto clean; 707 if (!cpp_demangle_push_subst_v(ddata, 708 &subst_v)) 709 goto clean; 710 } 711 break; 712 713 case TYPE_REF: 714 if (!cpp_demangle_push_str(ddata, "&", 1)) 715 goto clean; 716 if (type_str != NULL) { 717 if (!vector_str_push(&subst_v, "&", 1)) 718 goto clean; 719 if (!cpp_demangle_push_subst_v(ddata, 720 &subst_v)) 721 goto clean; 722 } 723 break; 724 725 case TYPE_CMX: 726 if (!cpp_demangle_push_str(ddata, " complex", 8)) 727 goto clean; 728 if (type_str != NULL) { 729 if (!vector_str_push(&subst_v, " complex", 8)) 730 goto clean; 731 if (!cpp_demangle_push_subst_v(ddata, 732 &subst_v)) 733 goto clean; 734 } 735 break; 736 737 case TYPE_IMG: 738 if (!cpp_demangle_push_str(ddata, " imaginary", 10)) 739 goto clean; 740 if (type_str != NULL) { 741 if (!vector_str_push(&subst_v, " imaginary", 742 10)) 743 goto clean; 744 if (!cpp_demangle_push_subst_v(ddata, 745 &subst_v)) 746 goto clean; 747 } 748 break; 749 750 case TYPE_EXT: 751 if (v->ext_name.size == 0 || 752 e_idx > v->ext_name.size - 1) 753 goto clean; 754 if ((e_len = strlen(v->ext_name.container[e_idx])) == 755 0) 756 goto clean; 757 if ((buf = malloc(e_len + 2)) == NULL) 758 goto clean; 759 snprintf(buf, e_len + 2, " %s", 760 v->ext_name.container[e_idx]); 761 762 if (!cpp_demangle_push_str(ddata, buf, e_len + 1)) { 763 free(buf); 764 goto clean; 765 } 766 767 if (type_str != NULL) { 768 if (!vector_str_push(&subst_v, buf, 769 e_len + 1)) { 770 free(buf); 771 goto clean; 772 } 773 if (!cpp_demangle_push_subst_v(ddata, 774 &subst_v)) { 775 free(buf); 776 goto clean; 777 } 778 } 779 free(buf); 780 ++e_idx; 781 break; 782 783 case TYPE_RST: 784 if (!cpp_demangle_push_str(ddata, " restrict", 9)) 785 goto clean; 786 if (type_str != NULL) { 787 if (!vector_str_push(&subst_v, " restrict", 9)) 788 goto clean; 789 if (!cpp_demangle_push_subst_v(ddata, 790 &subst_v)) 791 goto clean; 792 } 793 break; 794 795 case TYPE_VAT: 796 if (!cpp_demangle_push_str(ddata, " volatile", 9)) 797 goto clean; 798 if (type_str != NULL) { 799 if (!vector_str_push(&subst_v, " volatile", 9)) 800 goto clean; 801 if (!cpp_demangle_push_subst_v(ddata, 802 &subst_v)) 803 goto clean; 804 } 805 break; 806 807 case TYPE_CST: 808 if (!cpp_demangle_push_str(ddata, " const", 6)) 809 goto clean; 810 if (type_str != NULL) { 811 if (!vector_str_push(&subst_v, " const", 6)) 812 goto clean; 813 if (!cpp_demangle_push_subst_v(ddata, 814 &subst_v)) 815 goto clean; 816 } 817 break; 818 819 case TYPE_VEC: 820 if (v->ext_name.size == 0 || 821 e_idx > v->ext_name.size - 1) 822 goto clean; 823 if ((e_len = strlen(v->ext_name.container[e_idx])) == 824 0) 825 goto clean; 826 if ((buf = malloc(e_len + 12)) == NULL) 827 goto clean; 828 snprintf(buf, e_len + 12, " __vector(%s)", 829 v->ext_name.container[e_idx]); 830 if (!cpp_demangle_push_str(ddata, buf, e_len + 11)) { 831 free(buf); 832 goto clean; 833 } 834 if (type_str != NULL) { 835 if (!vector_str_push(&subst_v, buf, 836 e_len + 11)) { 837 free(buf); 838 goto clean; 839 } 840 if (!cpp_demangle_push_subst_v(ddata, 841 &subst_v)) { 842 free(buf); 843 goto clean; 844 } 845 } 846 free(buf); 847 ++e_idx; 848 break; 849 }; 850 --idx; 851 } 852 853 rtn = 1; 854 clean: 855 if (type_str != NULL) 856 vector_str_dest(&subst_v); 857 858 return (rtn); 859 } 860 861 static int 862 cpp_demangle_get_subst(struct cpp_demangle_data *ddata, size_t idx) 863 { 864 size_t len; 865 866 if (ddata == NULL || ddata->subst.size <= idx) 867 return (0); 868 if ((len = strlen(ddata->subst.container[idx])) == 0) 869 return (0); 870 if (!cpp_demangle_push_str(ddata, ddata->subst.container[idx], len)) 871 return (0); 872 873 /* skip '_' */ 874 ++ddata->cur; 875 876 return (1); 877 } 878 879 static int 880 cpp_demangle_get_tmpl_param(struct cpp_demangle_data *ddata, size_t idx) 881 { 882 size_t len; 883 884 if (ddata == NULL || ddata->tmpl.size <= idx) 885 return (0); 886 if ((len = strlen(ddata->tmpl.container[idx])) == 0) 887 return (0); 888 if (!cpp_demangle_push_str(ddata, ddata->tmpl.container[idx], len)) 889 return (0); 890 891 ++ddata->cur; 892 893 return (1); 894 } 895 896 static int 897 cpp_demangle_read_array(struct cpp_demangle_data *ddata) 898 { 899 size_t i, num_len, exp_len, p_idx, idx; 900 const char *num; 901 char *exp; 902 903 if (ddata == NULL || *(++ddata->cur) == '\0') 904 return (0); 905 906 if (*ddata->cur == '_') { 907 if (*(++ddata->cur) == '\0') 908 return (0); 909 910 if (!cpp_demangle_read_type(ddata, 0)) 911 return (0); 912 913 if (!cpp_demangle_push_str(ddata, "[]", 2)) 914 return (0); 915 } else { 916 if (ELFTC_ISDIGIT(*ddata->cur) != 0) { 917 num = ddata->cur; 918 while (ELFTC_ISDIGIT(*ddata->cur) != 0) 919 ++ddata->cur; 920 if (*ddata->cur != '_') 921 return (0); 922 num_len = ddata->cur - num; 923 assert(num_len > 0); 924 if (*(++ddata->cur) == '\0') 925 return (0); 926 if (!cpp_demangle_read_type(ddata, 0)) 927 return (0); 928 if (!cpp_demangle_push_str(ddata, "[", 1)) 929 return (0); 930 if (!cpp_demangle_push_str(ddata, num, num_len)) 931 return (0); 932 if (!cpp_demangle_push_str(ddata, "]", 1)) 933 return (0); 934 } else { 935 p_idx = ddata->output.size; 936 if (!cpp_demangle_read_expression(ddata)) 937 return (0); 938 if ((exp = vector_str_substr(&ddata->output, p_idx, 939 ddata->output.size - 1, &exp_len)) == NULL) 940 return (0); 941 idx = ddata->output.size; 942 for (i = p_idx; i < idx; ++i) 943 if (!vector_str_pop(&ddata->output)) { 944 free(exp); 945 return (0); 946 } 947 if (*ddata->cur != '_') { 948 free(exp); 949 return (0); 950 } 951 ++ddata->cur; 952 if (*ddata->cur == '\0') { 953 free(exp); 954 return (0); 955 } 956 if (!cpp_demangle_read_type(ddata, 0)) { 957 free(exp); 958 return (0); 959 } 960 if (!cpp_demangle_push_str(ddata, "[", 1)) { 961 free(exp); 962 return (0); 963 } 964 if (!cpp_demangle_push_str(ddata, exp, exp_len)) { 965 free(exp); 966 return (0); 967 } 968 if (!cpp_demangle_push_str(ddata, "]", 1)) { 969 free(exp); 970 return (0); 971 } 972 free(exp); 973 } 974 } 975 976 return (1); 977 } 978 979 static int 980 cpp_demangle_read_expr_primary(struct cpp_demangle_data *ddata) 981 { 982 const char *num; 983 984 if (ddata == NULL || *(++ddata->cur) == '\0') 985 return (0); 986 987 if (*ddata->cur == '_' && *(ddata->cur + 1) == 'Z') { 988 ddata->cur += 2; 989 if (*ddata->cur == '\0') 990 return (0); 991 if (!cpp_demangle_read_encoding(ddata)) 992 return (0); 993 ++ddata->cur; 994 return (1); 995 } 996 997 switch (*ddata->cur) { 998 case 'b': 999 if (*(ddata->cur + 2) != 'E') 1000 return (0); 1001 switch (*(++ddata->cur)) { 1002 case '0': 1003 ddata->cur += 2; 1004 return (cpp_demangle_push_str(ddata, "false", 5)); 1005 case '1': 1006 ddata->cur += 2; 1007 return (cpp_demangle_push_str(ddata, "true", 4)); 1008 default: 1009 return (0); 1010 }; 1011 1012 case 'd': 1013 ++ddata->cur; 1014 return (cpp_demangle_push_fp(ddata, decode_fp_to_double)); 1015 1016 case 'e': 1017 ++ddata->cur; 1018 if (sizeof(long double) == 10) 1019 return (cpp_demangle_push_fp(ddata, 1020 decode_fp_to_double)); 1021 return (cpp_demangle_push_fp(ddata, decode_fp_to_float80)); 1022 1023 case 'f': 1024 ++ddata->cur; 1025 return (cpp_demangle_push_fp(ddata, decode_fp_to_float)); 1026 1027 case 'g': 1028 ++ddata->cur; 1029 if (sizeof(long double) == 16) 1030 return (cpp_demangle_push_fp(ddata, 1031 decode_fp_to_double)); 1032 return (cpp_demangle_push_fp(ddata, decode_fp_to_float128)); 1033 1034 case 'i': 1035 case 'j': 1036 case 'l': 1037 case 'm': 1038 case 'n': 1039 case 's': 1040 case 't': 1041 case 'x': 1042 case 'y': 1043 if (*(++ddata->cur) == 'n') { 1044 if (!cpp_demangle_push_str(ddata, "-", 1)) 1045 return (0); 1046 ++ddata->cur; 1047 } 1048 num = ddata->cur; 1049 while (*ddata->cur != 'E') { 1050 if (!ELFTC_ISDIGIT(*ddata->cur)) 1051 return (0); 1052 ++ddata->cur; 1053 } 1054 ++ddata->cur; 1055 return (cpp_demangle_push_str(ddata, num, 1056 ddata->cur - num - 1)); 1057 1058 default: 1059 return (0); 1060 }; 1061 } 1062 1063 static int 1064 cpp_demangle_read_expression(struct cpp_demangle_data *ddata) 1065 { 1066 1067 if (ddata == NULL || *ddata->cur == '\0') 1068 return (0); 1069 1070 switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) { 1071 case SIMPLE_HASH('s', 't'): 1072 ddata->cur += 2; 1073 return (cpp_demangle_read_type(ddata, 0)); 1074 1075 case SIMPLE_HASH('s', 'r'): 1076 ddata->cur += 2; 1077 if (!cpp_demangle_read_type(ddata, 0)) 1078 return (0); 1079 if (!cpp_demangle_read_uqname(ddata)) 1080 return (0); 1081 if (*ddata->cur == 'I') 1082 return (cpp_demangle_read_tmpl_args(ddata)); 1083 return (1); 1084 1085 case SIMPLE_HASH('a', 'a'): 1086 /* operator && */ 1087 ddata->cur += 2; 1088 return (cpp_demangle_read_expression_binary(ddata, "&&", 2)); 1089 1090 case SIMPLE_HASH('a', 'd'): 1091 /* operator & (unary) */ 1092 ddata->cur += 2; 1093 return (cpp_demangle_read_expression_unary(ddata, "&", 1)); 1094 1095 case SIMPLE_HASH('a', 'n'): 1096 /* operator & */ 1097 ddata->cur += 2; 1098 return (cpp_demangle_read_expression_binary(ddata, "&", 1)); 1099 1100 case SIMPLE_HASH('a', 'N'): 1101 /* operator &= */ 1102 ddata->cur += 2; 1103 return (cpp_demangle_read_expression_binary(ddata, "&=", 2)); 1104 1105 case SIMPLE_HASH('a', 'S'): 1106 /* operator = */ 1107 ddata->cur += 2; 1108 return (cpp_demangle_read_expression_binary(ddata, "=", 1)); 1109 1110 case SIMPLE_HASH('c', 'l'): 1111 /* operator () */ 1112 ddata->cur += 2; 1113 return (cpp_demangle_read_expression_binary(ddata, "()", 2)); 1114 1115 case SIMPLE_HASH('c', 'm'): 1116 /* operator , */ 1117 ddata->cur += 2; 1118 return (cpp_demangle_read_expression_binary(ddata, ",", 1)); 1119 1120 case SIMPLE_HASH('c', 'o'): 1121 /* operator ~ */ 1122 ddata->cur += 2; 1123 return (cpp_demangle_read_expression_binary(ddata, "~", 1)); 1124 1125 case SIMPLE_HASH('c', 'v'): 1126 /* operator (cast) */ 1127 ddata->cur += 2; 1128 return (cpp_demangle_read_expression_binary(ddata, "(cast)", 6)); 1129 1130 case SIMPLE_HASH('d', 'a'): 1131 /* operator delete [] */ 1132 ddata->cur += 2; 1133 return (cpp_demangle_read_expression_unary(ddata, "delete []", 9)); 1134 1135 case SIMPLE_HASH('d', 'e'): 1136 /* operator * (unary) */ 1137 ddata->cur += 2; 1138 return (cpp_demangle_read_expression_unary(ddata, "*", 1)); 1139 1140 case SIMPLE_HASH('d', 'l'): 1141 /* operator delete */ 1142 ddata->cur += 2; 1143 return (cpp_demangle_read_expression_unary(ddata, "delete", 6)); 1144 1145 case SIMPLE_HASH('d', 'v'): 1146 /* operator / */ 1147 ddata->cur += 2; 1148 return (cpp_demangle_read_expression_binary(ddata, "/", 1)); 1149 1150 case SIMPLE_HASH('d', 'V'): 1151 /* operator /= */ 1152 ddata->cur += 2; 1153 return (cpp_demangle_read_expression_binary(ddata, "/=", 2)); 1154 1155 case SIMPLE_HASH('e', 'o'): 1156 /* operator ^ */ 1157 ddata->cur += 2; 1158 return (cpp_demangle_read_expression_binary(ddata, "^", 1)); 1159 1160 case SIMPLE_HASH('e', 'O'): 1161 /* operator ^= */ 1162 ddata->cur += 2; 1163 return (cpp_demangle_read_expression_binary(ddata, "^=", 2)); 1164 1165 case SIMPLE_HASH('e', 'q'): 1166 /* operator == */ 1167 ddata->cur += 2; 1168 return (cpp_demangle_read_expression_binary(ddata, "==", 2)); 1169 1170 case SIMPLE_HASH('g', 'e'): 1171 /* operator >= */ 1172 ddata->cur += 2; 1173 return (cpp_demangle_read_expression_binary(ddata, ">=", 2)); 1174 1175 case SIMPLE_HASH('g', 't'): 1176 /* operator > */ 1177 ddata->cur += 2; 1178 return (cpp_demangle_read_expression_binary(ddata, ">", 1)); 1179 1180 case SIMPLE_HASH('i', 'x'): 1181 /* operator [] */ 1182 ddata->cur += 2; 1183 return (cpp_demangle_read_expression_binary(ddata, "[]", 2)); 1184 1185 case SIMPLE_HASH('l', 'e'): 1186 /* operator <= */ 1187 ddata->cur += 2; 1188 return (cpp_demangle_read_expression_binary(ddata, "<=", 2)); 1189 1190 case SIMPLE_HASH('l', 's'): 1191 /* operator << */ 1192 ddata->cur += 2; 1193 return (cpp_demangle_read_expression_binary(ddata, "<<", 2)); 1194 1195 case SIMPLE_HASH('l', 'S'): 1196 /* operator <<= */ 1197 ddata->cur += 2; 1198 return (cpp_demangle_read_expression_binary(ddata, "<<=", 3)); 1199 1200 case SIMPLE_HASH('l', 't'): 1201 /* operator < */ 1202 ddata->cur += 2; 1203 return (cpp_demangle_read_expression_binary(ddata, "<", 1)); 1204 1205 case SIMPLE_HASH('m', 'i'): 1206 /* operator - */ 1207 ddata->cur += 2; 1208 return (cpp_demangle_read_expression_binary(ddata, "-", 1)); 1209 1210 case SIMPLE_HASH('m', 'I'): 1211 /* operator -= */ 1212 ddata->cur += 2; 1213 return (cpp_demangle_read_expression_binary(ddata, "-=", 2)); 1214 1215 case SIMPLE_HASH('m', 'l'): 1216 /* operator * */ 1217 ddata->cur += 2; 1218 return (cpp_demangle_read_expression_binary(ddata, "*", 1)); 1219 1220 case SIMPLE_HASH('m', 'L'): 1221 /* operator *= */ 1222 ddata->cur += 2; 1223 return (cpp_demangle_read_expression_binary(ddata, "*=", 2)); 1224 1225 case SIMPLE_HASH('m', 'm'): 1226 /* operator -- */ 1227 ddata->cur += 2; 1228 return (cpp_demangle_read_expression_binary(ddata, "--", 2)); 1229 1230 case SIMPLE_HASH('n', 'a'): 1231 /* operator new[] */ 1232 ddata->cur += 2; 1233 return (cpp_demangle_read_expression_unary(ddata, "new []", 6)); 1234 1235 case SIMPLE_HASH('n', 'e'): 1236 /* operator != */ 1237 ddata->cur += 2; 1238 return (cpp_demangle_read_expression_binary(ddata, "!=", 2)); 1239 1240 case SIMPLE_HASH('n', 'g'): 1241 /* operator - (unary) */ 1242 ddata->cur += 2; 1243 return (cpp_demangle_read_expression_unary(ddata, "-", 1)); 1244 1245 case SIMPLE_HASH('n', 't'): 1246 /* operator ! */ 1247 ddata->cur += 2; 1248 return (cpp_demangle_read_expression_binary(ddata, "!", 1)); 1249 1250 case SIMPLE_HASH('n', 'w'): 1251 /* operator new */ 1252 ddata->cur += 2; 1253 return (cpp_demangle_read_expression_unary(ddata, "new", 3)); 1254 1255 case SIMPLE_HASH('o', 'o'): 1256 /* operator || */ 1257 ddata->cur += 2; 1258 return (cpp_demangle_read_expression_binary(ddata, "||", 2)); 1259 1260 case SIMPLE_HASH('o', 'r'): 1261 /* operator | */ 1262 ddata->cur += 2; 1263 return (cpp_demangle_read_expression_binary(ddata, "|", 1)); 1264 1265 case SIMPLE_HASH('o', 'R'): 1266 /* operator |= */ 1267 ddata->cur += 2; 1268 return (cpp_demangle_read_expression_binary(ddata, "|=", 2)); 1269 1270 case SIMPLE_HASH('p', 'l'): 1271 /* operator + */ 1272 ddata->cur += 2; 1273 return (cpp_demangle_read_expression_binary(ddata, "+", 1)); 1274 1275 case SIMPLE_HASH('p', 'L'): 1276 /* operator += */ 1277 ddata->cur += 2; 1278 return (cpp_demangle_read_expression_binary(ddata, "+=", 2)); 1279 1280 case SIMPLE_HASH('p', 'm'): 1281 /* operator ->* */ 1282 ddata->cur += 2; 1283 return (cpp_demangle_read_expression_binary(ddata, "->*", 3)); 1284 1285 case SIMPLE_HASH('p', 'p'): 1286 /* operator ++ */ 1287 ddata->cur += 2; 1288 return (cpp_demangle_read_expression_binary(ddata, "++", 2)); 1289 1290 case SIMPLE_HASH('p', 's'): 1291 /* operator + (unary) */ 1292 ddata->cur += 2; 1293 return (cpp_demangle_read_expression_unary(ddata, "+", 1)); 1294 1295 case SIMPLE_HASH('p', 't'): 1296 /* operator -> */ 1297 ddata->cur += 2; 1298 return (cpp_demangle_read_expression_binary(ddata, "->", 2)); 1299 1300 case SIMPLE_HASH('q', 'u'): 1301 /* operator ? */ 1302 ddata->cur += 2; 1303 return (cpp_demangle_read_expression_trinary(ddata, "?", 1, 1304 ":", 1)); 1305 1306 case SIMPLE_HASH('r', 'm'): 1307 /* operator % */ 1308 ddata->cur += 2; 1309 return (cpp_demangle_read_expression_binary(ddata, "%", 1)); 1310 1311 case SIMPLE_HASH('r', 'M'): 1312 /* operator %= */ 1313 ddata->cur += 2; 1314 return (cpp_demangle_read_expression_binary(ddata, "%=", 2)); 1315 1316 case SIMPLE_HASH('r', 's'): 1317 /* operator >> */ 1318 ddata->cur += 2; 1319 return (cpp_demangle_read_expression_binary(ddata, ">>", 2)); 1320 1321 case SIMPLE_HASH('r', 'S'): 1322 /* operator >>= */ 1323 ddata->cur += 2; 1324 return (cpp_demangle_read_expression_binary(ddata, ">>=", 3)); 1325 1326 case SIMPLE_HASH('r', 'z'): 1327 /* operator sizeof */ 1328 ddata->cur += 2; 1329 return (cpp_demangle_read_expression_unary(ddata, "sizeof", 6)); 1330 1331 case SIMPLE_HASH('s', 'v'): 1332 /* operator sizeof */ 1333 ddata->cur += 2; 1334 return (cpp_demangle_read_expression_unary(ddata, "sizeof", 6)); 1335 }; 1336 1337 switch (*ddata->cur) { 1338 case 'L': 1339 return (cpp_demangle_read_expr_primary(ddata)); 1340 case 'T': 1341 return (cpp_demangle_read_tmpl_param(ddata)); 1342 }; 1343 1344 return (0); 1345 } 1346 1347 static int 1348 cpp_demangle_read_expression_flat(struct cpp_demangle_data *ddata, char **str) 1349 { 1350 struct vector_str *output; 1351 size_t i, p_idx, idx, exp_len; 1352 char *exp; 1353 1354 output = ddata->push_head > 0 ? &ddata->output_tmp : 1355 &ddata->output; 1356 1357 p_idx = output->size; 1358 1359 if (!cpp_demangle_read_expression(ddata)) 1360 return (0); 1361 1362 if ((exp = vector_str_substr(output, p_idx, output->size - 1, 1363 &exp_len)) == NULL) 1364 return (0); 1365 1366 idx = output->size; 1367 for (i = p_idx; i < idx; ++i) { 1368 if (!vector_str_pop(output)) { 1369 free(exp); 1370 return (0); 1371 } 1372 } 1373 1374 *str = exp; 1375 1376 return (1); 1377 } 1378 1379 static int 1380 cpp_demangle_read_expression_binary(struct cpp_demangle_data *ddata, 1381 const char *name, size_t len) 1382 { 1383 1384 if (ddata == NULL || name == NULL || len == 0) 1385 return (0); 1386 if (!cpp_demangle_read_expression(ddata)) 1387 return (0); 1388 if (!cpp_demangle_push_str(ddata, name, len)) 1389 return (0); 1390 1391 return (cpp_demangle_read_expression(ddata)); 1392 } 1393 1394 static int 1395 cpp_demangle_read_expression_unary(struct cpp_demangle_data *ddata, 1396 const char *name, size_t len) 1397 { 1398 1399 if (ddata == NULL || name == NULL || len == 0) 1400 return (0); 1401 if (!cpp_demangle_read_expression(ddata)) 1402 return (0); 1403 1404 return (cpp_demangle_push_str(ddata, name, len)); 1405 } 1406 1407 static int 1408 cpp_demangle_read_expression_trinary(struct cpp_demangle_data *ddata, 1409 const char *name1, size_t len1, const char *name2, size_t len2) 1410 { 1411 1412 if (ddata == NULL || name1 == NULL || len1 == 0 || name2 == NULL || 1413 len2 == 0) 1414 return (0); 1415 1416 if (!cpp_demangle_read_expression(ddata)) 1417 return (0); 1418 if (!cpp_demangle_push_str(ddata, name1, len1)) 1419 return (0); 1420 if (!cpp_demangle_read_expression(ddata)) 1421 return (0); 1422 if (!cpp_demangle_push_str(ddata, name2, len2)) 1423 return (0); 1424 1425 return (cpp_demangle_read_expression(ddata)); 1426 } 1427 1428 static int 1429 cpp_demangle_read_function(struct cpp_demangle_data *ddata, int *ext_c, 1430 struct vector_type_qualifier *v) 1431 { 1432 size_t class_type_size, class_type_len, limit; 1433 const char *class_type; 1434 1435 if (ddata == NULL || *ddata->cur != 'F' || v == NULL) 1436 return (0); 1437 1438 ++ddata->cur; 1439 if (*ddata->cur == 'Y') { 1440 if (ext_c != NULL) 1441 *ext_c = 1; 1442 ++ddata->cur; 1443 } 1444 if (!cpp_demangle_read_type(ddata, 0)) 1445 return (0); 1446 if (*ddata->cur != 'E') { 1447 if (!cpp_demangle_push_str(ddata, "(", 1)) 1448 return (0); 1449 if (vector_read_cmd_find(&ddata->cmd, READ_PTRMEM)) { 1450 if ((class_type_size = ddata->class_type.size) == 0) 1451 return (0); 1452 class_type = 1453 ddata->class_type.container[class_type_size - 1]; 1454 if (class_type == NULL) 1455 return (0); 1456 if ((class_type_len = strlen(class_type)) == 0) 1457 return (0); 1458 if (!cpp_demangle_push_str(ddata, class_type, 1459 class_type_len)) 1460 return (0); 1461 if (!cpp_demangle_push_str(ddata, "::*", 3)) 1462 return (0); 1463 ++ddata->func_type; 1464 } else { 1465 if (!cpp_demangle_push_type_qualifier(ddata, v, 1466 (const char *) NULL)) 1467 return (0); 1468 vector_type_qualifier_dest(v); 1469 if (!vector_type_qualifier_init(v)) 1470 return (0); 1471 } 1472 1473 if (!cpp_demangle_push_str(ddata, ")(", 2)) 1474 return (0); 1475 1476 limit = 0; 1477 for (;;) { 1478 if (!cpp_demangle_read_type(ddata, 0)) 1479 return (0); 1480 if (*ddata->cur == 'E') 1481 break; 1482 if (limit++ > CPP_DEMANGLE_TRY_LIMIT) 1483 return (0); 1484 } 1485 1486 if (vector_read_cmd_find(&ddata->cmd, READ_PTRMEM) == 1) { 1487 if (!cpp_demangle_push_type_qualifier(ddata, v, 1488 (const char *) NULL)) 1489 return (0); 1490 vector_type_qualifier_dest(v); 1491 if (!vector_type_qualifier_init(v)) 1492 return (0); 1493 } 1494 1495 if (!cpp_demangle_push_str(ddata, ")", 1)) 1496 return (0); 1497 } 1498 1499 ++ddata->cur; 1500 1501 return (1); 1502 } 1503 1504 /* read encoding, encoding are function name, data name, special-name */ 1505 static int 1506 cpp_demangle_read_encoding(struct cpp_demangle_data *ddata) 1507 { 1508 char *name, *type, *num_str; 1509 long offset; 1510 int rtn; 1511 1512 if (ddata == NULL || *ddata->cur == '\0') 1513 return (0); 1514 1515 /* special name */ 1516 switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) { 1517 case SIMPLE_HASH('G', 'A'): 1518 if (!cpp_demangle_push_str(ddata, "hidden alias for ", 17)) 1519 return (0); 1520 ddata->cur += 2; 1521 if (*ddata->cur == '\0') 1522 return (0); 1523 return (cpp_demangle_read_encoding(ddata)); 1524 1525 case SIMPLE_HASH('G', 'R'): 1526 if (!cpp_demangle_push_str(ddata, "reference temporary #", 21)) 1527 return (0); 1528 ddata->cur += 2; 1529 if (*ddata->cur == '\0') 1530 return (0); 1531 if (!cpp_demangle_read_name_flat(ddata, &name)) 1532 return (0); 1533 rtn = 0; 1534 if (!cpp_demangle_read_number_as_string(ddata, &num_str)) 1535 goto clean1; 1536 if (!cpp_demangle_push_str(ddata, num_str, strlen(num_str))) 1537 goto clean2; 1538 if (!cpp_demangle_push_str(ddata, " for ", 5)) 1539 goto clean2; 1540 if (!cpp_demangle_push_str(ddata, name, strlen(name))) 1541 goto clean2; 1542 rtn = 1; 1543 clean2: 1544 free(num_str); 1545 clean1: 1546 free(name); 1547 return (rtn); 1548 1549 case SIMPLE_HASH('G', 'T'): 1550 ddata->cur += 2; 1551 if (*ddata->cur == '\0') 1552 return (0); 1553 switch (*ddata->cur) { 1554 case 'n': 1555 if (!cpp_demangle_push_str(ddata, 1556 "non-transaction clone for ", 26)) 1557 return (0); 1558 case 't': 1559 default: 1560 if (!cpp_demangle_push_str(ddata, 1561 "transaction clone for ", 22)) 1562 return (0); 1563 } 1564 ++ddata->cur; 1565 return (cpp_demangle_read_encoding(ddata)); 1566 1567 case SIMPLE_HASH('G', 'V'): 1568 /* sentry object for 1 time init */ 1569 if (!cpp_demangle_push_str(ddata, "guard variable for ", 20)) 1570 return (0); 1571 ddata->cur += 2; 1572 break; 1573 1574 case SIMPLE_HASH('T', 'c'): 1575 /* virtual function covariant override thunk */ 1576 if (!cpp_demangle_push_str(ddata, 1577 "virtual function covariant override ", 36)) 1578 return (0); 1579 ddata->cur += 2; 1580 if (*ddata->cur == '\0') 1581 return (0); 1582 if (!cpp_demangle_read_offset(ddata)) 1583 return (0); 1584 if (!cpp_demangle_read_offset(ddata)) 1585 return (0); 1586 return (cpp_demangle_read_encoding(ddata)); 1587 1588 case SIMPLE_HASH('T', 'C'): 1589 /* construction vtable */ 1590 if (!cpp_demangle_push_str(ddata, "construction vtable for ", 1591 24)) 1592 return (0); 1593 ddata->cur += 2; 1594 if (*ddata->cur == '\0') 1595 return (0); 1596 if (!cpp_demangle_read_type_flat(ddata, &type)) 1597 return (0); 1598 rtn = 0; 1599 if (!cpp_demangle_read_number(ddata, &offset)) 1600 goto clean3; 1601 if (*ddata->cur++ != '_') 1602 goto clean3; 1603 if (!cpp_demangle_read_type(ddata, 0)) 1604 goto clean3; 1605 if (!cpp_demangle_push_str(ddata, "-in-", 4)) 1606 goto clean3; 1607 if (!cpp_demangle_push_str(ddata, type, strlen(type))) 1608 goto clean3; 1609 rtn = 1; 1610 clean3: 1611 free(type); 1612 return (rtn); 1613 1614 case SIMPLE_HASH('T', 'D'): 1615 /* typeinfo common proxy */ 1616 break; 1617 1618 case SIMPLE_HASH('T', 'F'): 1619 /* typeinfo fn */ 1620 if (!cpp_demangle_push_str(ddata, "typeinfo fn for ", 16)) 1621 return (0); 1622 ddata->cur += 2; 1623 if (*ddata->cur == '\0') 1624 return (0); 1625 return (cpp_demangle_read_type(ddata, 0)); 1626 1627 case SIMPLE_HASH('T', 'h'): 1628 /* virtual function non-virtual override thunk */ 1629 if (!cpp_demangle_push_str(ddata, 1630 "virtual function non-virtual override ", 38)) 1631 return (0); 1632 ddata->cur += 2; 1633 if (*ddata->cur == '\0') 1634 return (0); 1635 if (!cpp_demangle_read_nv_offset(ddata)) 1636 return (0); 1637 return (cpp_demangle_read_encoding(ddata)); 1638 1639 case SIMPLE_HASH('T', 'H'): 1640 /* TLS init function */ 1641 if (!cpp_demangle_push_str(ddata, "TLS init function for ", 1642 22)) 1643 return (0); 1644 ddata->cur += 2; 1645 if (*ddata->cur == '\0') 1646 return (0); 1647 break; 1648 1649 case SIMPLE_HASH('T', 'I'): 1650 /* typeinfo structure */ 1651 if (!cpp_demangle_push_str(ddata, "typeinfo for ", 13)) 1652 return (0); 1653 ddata->cur += 2; 1654 if (*ddata->cur == '\0') 1655 return (0); 1656 return (cpp_demangle_read_type(ddata, 0)); 1657 1658 case SIMPLE_HASH('T', 'J'): 1659 /* java class */ 1660 if (!cpp_demangle_push_str(ddata, "java Class for ", 15)) 1661 return (0); 1662 ddata->cur += 2; 1663 if (*ddata->cur == '\0') 1664 return (0); 1665 return (cpp_demangle_read_type(ddata, 0)); 1666 1667 case SIMPLE_HASH('T', 'S'): 1668 /* RTTI name (NTBS) */ 1669 if (!cpp_demangle_push_str(ddata, "typeinfo name for ", 18)) 1670 return (0); 1671 ddata->cur += 2; 1672 if (*ddata->cur == '\0') 1673 return (0); 1674 return (cpp_demangle_read_type(ddata, 0)); 1675 1676 case SIMPLE_HASH('T', 'T'): 1677 /* VTT table */ 1678 if (!cpp_demangle_push_str(ddata, "VTT for ", 8)) 1679 return (0); 1680 ddata->cur += 2; 1681 if (*ddata->cur == '\0') 1682 return (0); 1683 return (cpp_demangle_read_type(ddata, 0)); 1684 1685 case SIMPLE_HASH('T', 'v'): 1686 /* virtual function virtual override thunk */ 1687 if (!cpp_demangle_push_str(ddata, 1688 "virtual function virtual override ", 34)) 1689 return (0); 1690 ddata->cur += 2; 1691 if (*ddata->cur == '\0') 1692 return (0); 1693 if (!cpp_demangle_read_v_offset(ddata)) 1694 return (0); 1695 return (cpp_demangle_read_encoding(ddata)); 1696 1697 case SIMPLE_HASH('T', 'V'): 1698 /* virtual table */ 1699 if (!cpp_demangle_push_str(ddata, "vtable for ", 12)) 1700 return (0); 1701 ddata->cur += 2; 1702 if (*ddata->cur == '\0') 1703 return (0); 1704 return (cpp_demangle_read_type(ddata, 0)); 1705 1706 case SIMPLE_HASH('T', 'W'): 1707 /* TLS wrapper function */ 1708 if (!cpp_demangle_push_str(ddata, "TLS wrapper function for ", 1709 25)) 1710 return (0); 1711 ddata->cur += 2; 1712 if (*ddata->cur == '\0') 1713 return (0); 1714 break; 1715 }; 1716 1717 return (cpp_demangle_read_name(ddata)); 1718 } 1719 1720 static int 1721 cpp_demangle_read_local_name(struct cpp_demangle_data *ddata) 1722 { 1723 size_t limit; 1724 1725 if (ddata == NULL) 1726 return (0); 1727 if (*(++ddata->cur) == '\0') 1728 return (0); 1729 if (!cpp_demangle_read_encoding(ddata)) 1730 return (0); 1731 1732 limit = 0; 1733 for (;;) { 1734 if (!cpp_demangle_read_type(ddata, 1)) 1735 return (0); 1736 if (*ddata->cur == 'E') 1737 break; 1738 if (limit++ > CPP_DEMANGLE_TRY_LIMIT) 1739 return (0); 1740 } 1741 if (*(++ddata->cur) == '\0') 1742 return (0); 1743 if (ddata->paren == true) { 1744 if (!cpp_demangle_push_str(ddata, ")", 1)) 1745 return (0); 1746 ddata->paren = false; 1747 } 1748 if (*ddata->cur == 's') 1749 ++ddata->cur; 1750 else { 1751 if (!cpp_demangle_push_str(ddata, "::", 2)) 1752 return (0); 1753 if (!cpp_demangle_read_name(ddata)) 1754 return (0); 1755 } 1756 if (*ddata->cur == '_') { 1757 ++ddata->cur; 1758 while (ELFTC_ISDIGIT(*ddata->cur) != 0) 1759 ++ddata->cur; 1760 } 1761 1762 return (1); 1763 } 1764 1765 static int 1766 cpp_demangle_read_name(struct cpp_demangle_data *ddata) 1767 { 1768 struct vector_str *output, v; 1769 size_t p_idx, subst_str_len; 1770 int rtn; 1771 char *subst_str; 1772 1773 if (ddata == NULL || *ddata->cur == '\0') 1774 return (0); 1775 1776 output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output; 1777 1778 subst_str = NULL; 1779 1780 switch (*ddata->cur) { 1781 case 'S': 1782 return (cpp_demangle_read_subst(ddata)); 1783 case 'N': 1784 return (cpp_demangle_read_nested_name(ddata)); 1785 case 'Z': 1786 return (cpp_demangle_read_local_name(ddata)); 1787 }; 1788 1789 if (!vector_str_init(&v)) 1790 return (0); 1791 1792 p_idx = output->size; 1793 rtn = 0; 1794 if (!cpp_demangle_read_uqname(ddata)) 1795 goto clean; 1796 if ((subst_str = vector_str_substr(output, p_idx, output->size - 1, 1797 &subst_str_len)) == NULL) 1798 goto clean; 1799 if (subst_str_len > 8 && strstr(subst_str, "operator") != NULL) { 1800 rtn = 1; 1801 goto clean; 1802 } 1803 if (!vector_str_push(&v, subst_str, subst_str_len)) 1804 goto clean; 1805 if (!cpp_demangle_push_subst_v(ddata, &v)) 1806 goto clean; 1807 1808 if (*ddata->cur == 'I') { 1809 p_idx = output->size; 1810 if (!cpp_demangle_read_tmpl_args(ddata)) 1811 goto clean; 1812 free(subst_str); 1813 if ((subst_str = vector_str_substr(output, p_idx, 1814 output->size - 1, &subst_str_len)) == NULL) 1815 goto clean; 1816 if (!vector_str_push(&v, subst_str, subst_str_len)) 1817 goto clean; 1818 if (!cpp_demangle_push_subst_v(ddata, &v)) 1819 goto clean; 1820 } 1821 1822 rtn = 1; 1823 1824 clean: 1825 free(subst_str); 1826 vector_str_dest(&v); 1827 1828 return (rtn); 1829 } 1830 1831 static int 1832 cpp_demangle_read_name_flat(struct cpp_demangle_data *ddata, char **str) 1833 { 1834 struct vector_str *output; 1835 size_t i, p_idx, idx, name_len; 1836 char *name; 1837 1838 output = ddata->push_head > 0 ? &ddata->output_tmp : 1839 &ddata->output; 1840 1841 p_idx = output->size; 1842 1843 if (!cpp_demangle_read_name(ddata)) 1844 return (0); 1845 1846 if ((name = vector_str_substr(output, p_idx, output->size - 1, 1847 &name_len)) == NULL) 1848 return (0); 1849 1850 idx = output->size; 1851 for (i = p_idx; i < idx; ++i) { 1852 if (!vector_str_pop(output)) { 1853 free(name); 1854 return (0); 1855 } 1856 } 1857 1858 *str = name; 1859 1860 return (1); 1861 } 1862 1863 static int 1864 cpp_demangle_read_nested_name(struct cpp_demangle_data *ddata) 1865 { 1866 struct vector_str *output, v; 1867 size_t limit, p_idx, subst_str_len; 1868 int rtn; 1869 char *subst_str; 1870 1871 if (ddata == NULL || *ddata->cur != 'N') 1872 return (0); 1873 if (*(++ddata->cur) == '\0') 1874 return (0); 1875 1876 while (*ddata->cur == 'r' || *ddata->cur == 'V' || 1877 *ddata->cur == 'K') { 1878 switch (*ddata->cur) { 1879 case 'r': 1880 ddata->mem_rst = true; 1881 break; 1882 case 'V': 1883 ddata->mem_vat = true; 1884 break; 1885 case 'K': 1886 ddata->mem_cst = true; 1887 break; 1888 }; 1889 ++ddata->cur; 1890 } 1891 1892 output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output; 1893 if (!vector_str_init(&v)) 1894 return (0); 1895 1896 rtn = 0; 1897 limit = 0; 1898 for (;;) { 1899 p_idx = output->size; 1900 switch (*ddata->cur) { 1901 case 'I': 1902 if (!cpp_demangle_read_tmpl_args(ddata)) 1903 goto clean; 1904 break; 1905 case 'S': 1906 if (!cpp_demangle_read_subst(ddata)) 1907 goto clean; 1908 break; 1909 case 'T': 1910 if (!cpp_demangle_read_tmpl_param(ddata)) 1911 goto clean; 1912 break; 1913 default: 1914 if (!cpp_demangle_read_uqname(ddata)) 1915 goto clean; 1916 }; 1917 1918 if ((subst_str = vector_str_substr(output, p_idx, 1919 output->size - 1, &subst_str_len)) == NULL) 1920 goto clean; 1921 if (!vector_str_push(&v, subst_str, subst_str_len)) { 1922 free(subst_str); 1923 goto clean; 1924 } 1925 free(subst_str); 1926 1927 if (!cpp_demangle_push_subst_v(ddata, &v)) 1928 goto clean; 1929 if (*ddata->cur == 'E') 1930 break; 1931 else if (*ddata->cur != 'I' && 1932 *ddata->cur != 'C' && *ddata->cur != 'D') { 1933 if (!cpp_demangle_push_str(ddata, "::", 2)) 1934 goto clean; 1935 if (!vector_str_push(&v, "::", 2)) 1936 goto clean; 1937 } 1938 if (limit++ > CPP_DEMANGLE_TRY_LIMIT) 1939 goto clean; 1940 } 1941 1942 ++ddata->cur; 1943 rtn = 1; 1944 1945 clean: 1946 vector_str_dest(&v); 1947 1948 return (rtn); 1949 } 1950 1951 /* 1952 * read number 1953 * number ::= [n] <decimal> 1954 */ 1955 static int 1956 cpp_demangle_read_number(struct cpp_demangle_data *ddata, long *rtn) 1957 { 1958 long len, negative_factor; 1959 1960 if (ddata == NULL || rtn == NULL) 1961 return (0); 1962 1963 negative_factor = 1; 1964 if (*ddata->cur == 'n') { 1965 negative_factor = -1; 1966 1967 ++ddata->cur; 1968 } 1969 if (ELFTC_ISDIGIT(*ddata->cur) == 0) 1970 return (0); 1971 1972 errno = 0; 1973 if ((len = strtol(ddata->cur, (char **) NULL, 10)) == 0 && 1974 errno != 0) 1975 return (0); 1976 1977 while (ELFTC_ISDIGIT(*ddata->cur) != 0) 1978 ++ddata->cur; 1979 1980 assert(len >= 0); 1981 assert(negative_factor == 1 || negative_factor == -1); 1982 1983 *rtn = len * negative_factor; 1984 1985 return (1); 1986 } 1987 1988 static int 1989 cpp_demangle_read_number_as_string(struct cpp_demangle_data *ddata, char **str) 1990 { 1991 long n; 1992 1993 if (!cpp_demangle_read_number(ddata, &n)) { 1994 *str = NULL; 1995 return (0); 1996 } 1997 1998 if (asprintf(str, "%ld", n) < 0) { 1999 *str = NULL; 2000 return (0); 2001 } 2002 2003 return (1); 2004 } 2005 2006 static int 2007 cpp_demangle_read_nv_offset(struct cpp_demangle_data *ddata) 2008 { 2009 2010 if (ddata == NULL) 2011 return (0); 2012 2013 if (!cpp_demangle_push_str(ddata, "offset : ", 9)) 2014 return (0); 2015 2016 return (cpp_demangle_read_offset_number(ddata)); 2017 } 2018 2019 /* read offset, offset are nv-offset, v-offset */ 2020 static int 2021 cpp_demangle_read_offset(struct cpp_demangle_data *ddata) 2022 { 2023 2024 if (ddata == NULL) 2025 return (0); 2026 2027 if (*ddata->cur == 'h') { 2028 ++ddata->cur; 2029 return (cpp_demangle_read_nv_offset(ddata)); 2030 } else if (*ddata->cur == 'v') { 2031 ++ddata->cur; 2032 return (cpp_demangle_read_v_offset(ddata)); 2033 } 2034 2035 return (0); 2036 } 2037 2038 static int 2039 cpp_demangle_read_offset_number(struct cpp_demangle_data *ddata) 2040 { 2041 bool negative; 2042 const char *start; 2043 2044 if (ddata == NULL || *ddata->cur == '\0') 2045 return (0); 2046 2047 /* offset could be negative */ 2048 if (*ddata->cur == 'n') { 2049 negative = true; 2050 start = ddata->cur + 1; 2051 } else { 2052 negative = false; 2053 start = ddata->cur; 2054 } 2055 2056 while (*ddata->cur != '_') 2057 ++ddata->cur; 2058 2059 if (negative && !cpp_demangle_push_str(ddata, "-", 1)) 2060 return (0); 2061 2062 assert(start != NULL); 2063 2064 if (!cpp_demangle_push_str(ddata, start, ddata->cur - start)) 2065 return (0); 2066 if (!cpp_demangle_push_str(ddata, " ", 1)) 2067 return (0); 2068 2069 ++ddata->cur; 2070 2071 return (1); 2072 } 2073 2074 static int 2075 cpp_demangle_read_pointer_to_member(struct cpp_demangle_data *ddata) 2076 { 2077 size_t class_type_len, i, idx, p_idx; 2078 int p_func_type, rtn; 2079 char *class_type; 2080 2081 if (ddata == NULL || *ddata->cur != 'M' || *(++ddata->cur) == '\0') 2082 return (0); 2083 2084 p_idx = ddata->output.size; 2085 if (!cpp_demangle_read_type(ddata, 0)) 2086 return (0); 2087 2088 if ((class_type = vector_str_substr(&ddata->output, p_idx, 2089 ddata->output.size - 1, &class_type_len)) == NULL) 2090 return (0); 2091 2092 rtn = 0; 2093 idx = ddata->output.size; 2094 for (i = p_idx; i < idx; ++i) 2095 if (!vector_str_pop(&ddata->output)) 2096 goto clean1; 2097 2098 if (!vector_read_cmd_push(&ddata->cmd, READ_PTRMEM)) 2099 goto clean1; 2100 2101 if (!vector_str_push(&ddata->class_type, class_type, class_type_len)) 2102 goto clean2; 2103 2104 p_func_type = ddata->func_type; 2105 if (!cpp_demangle_read_type(ddata, 0)) 2106 goto clean3; 2107 2108 if (p_func_type == ddata->func_type) { 2109 if (!cpp_demangle_push_str(ddata, " ", 1)) 2110 goto clean3; 2111 if (!cpp_demangle_push_str(ddata, class_type, class_type_len)) 2112 goto clean3; 2113 if (!cpp_demangle_push_str(ddata, "::*", 3)) 2114 goto clean3; 2115 } 2116 2117 rtn = 1; 2118 clean3: 2119 if (!vector_str_pop(&ddata->class_type)) 2120 rtn = 0; 2121 clean2: 2122 if (!vector_read_cmd_pop(&ddata->cmd)) 2123 rtn = 0; 2124 clean1: 2125 free(class_type); 2126 2127 return (rtn); 2128 } 2129 2130 /* read source-name, source-name is <len> <ID> */ 2131 static int 2132 cpp_demangle_read_sname(struct cpp_demangle_data *ddata) 2133 { 2134 long len; 2135 int err; 2136 2137 if (ddata == NULL || cpp_demangle_read_number(ddata, &len) == 0 || 2138 len <= 0) 2139 return (0); 2140 2141 if (len == 12 && (memcmp("_GLOBAL__N_1", ddata->cur, 12) == 0)) 2142 err = cpp_demangle_push_str(ddata, "(anonymous namespace)", 21); 2143 else 2144 err = cpp_demangle_push_str(ddata, ddata->cur, len); 2145 2146 if (err == 0) 2147 return (0); 2148 2149 assert(ddata->output.size > 0); 2150 if (vector_read_cmd_find(&ddata->cmd, READ_TMPL) == 0) 2151 ddata->last_sname = 2152 ddata->output.container[ddata->output.size - 1]; 2153 2154 ddata->cur += len; 2155 2156 return (1); 2157 } 2158 2159 static int 2160 cpp_demangle_read_subst(struct cpp_demangle_data *ddata) 2161 { 2162 long nth; 2163 2164 if (ddata == NULL || *ddata->cur == '\0') 2165 return (0); 2166 2167 /* abbreviations of the form Sx */ 2168 switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) { 2169 case SIMPLE_HASH('S', 'a'): 2170 /* std::allocator */ 2171 if (cpp_demangle_push_str(ddata, "std::allocator", 14) == 0) 2172 return (0); 2173 ddata->cur += 2; 2174 if (*ddata->cur == 'I') 2175 return (cpp_demangle_read_subst_stdtmpl(ddata, 2176 "std::allocator", 14)); 2177 return (1); 2178 2179 case SIMPLE_HASH('S', 'b'): 2180 /* std::basic_string */ 2181 if (!cpp_demangle_push_str(ddata, "std::basic_string", 17)) 2182 return (0); 2183 ddata->cur += 2; 2184 if (*ddata->cur == 'I') 2185 return (cpp_demangle_read_subst_stdtmpl(ddata, 2186 "std::basic_string", 17)); 2187 return (1); 2188 2189 case SIMPLE_HASH('S', 'd'): 2190 /* std::basic_iostream<char, std::char_traits<char> > */ 2191 if (!cpp_demangle_push_str(ddata, "std::iostream", 19)) 2192 return (0); 2193 ddata->last_sname = "iostream"; 2194 ddata->cur += 2; 2195 if (*ddata->cur == 'I') 2196 return (cpp_demangle_read_subst_stdtmpl(ddata, 2197 "std::iostream", 19)); 2198 return (1); 2199 2200 case SIMPLE_HASH('S', 'i'): 2201 /* std::basic_istream<char, std::char_traits<char> > */ 2202 if (!cpp_demangle_push_str(ddata, "std::istream", 18)) 2203 return (0); 2204 ddata->last_sname = "istream"; 2205 ddata->cur += 2; 2206 if (*ddata->cur == 'I') 2207 return (cpp_demangle_read_subst_stdtmpl(ddata, 2208 "std::istream", 18)); 2209 return (1); 2210 2211 case SIMPLE_HASH('S', 'o'): 2212 /* std::basic_ostream<char, std::char_traits<char> > */ 2213 if (!cpp_demangle_push_str(ddata, "std::ostream", 18)) 2214 return (0); 2215 ddata->last_sname = "istream"; 2216 ddata->cur += 2; 2217 if (*ddata->cur == 'I') 2218 return (cpp_demangle_read_subst_stdtmpl(ddata, 2219 "std::ostream", 18)); 2220 return (1); 2221 2222 case SIMPLE_HASH('S', 's'): 2223 /* 2224 * std::basic_string<char, std::char_traits<char>, 2225 * std::allocator<char> > 2226 * 2227 * a.k.a std::string 2228 */ 2229 if (!cpp_demangle_push_str(ddata, "std::string", 11)) 2230 return (0); 2231 ddata->last_sname = "string"; 2232 ddata->cur += 2; 2233 if (*ddata->cur == 'I') 2234 return (cpp_demangle_read_subst_stdtmpl(ddata, 2235 "std::string", 11)); 2236 return (1); 2237 2238 case SIMPLE_HASH('S', 't'): 2239 /* std:: */ 2240 return (cpp_demangle_read_subst_std(ddata)); 2241 }; 2242 2243 if (*(++ddata->cur) == '\0') 2244 return (0); 2245 2246 /* substitution */ 2247 if (*ddata->cur == '_') 2248 return (cpp_demangle_get_subst(ddata, 0)); 2249 else { 2250 errno = 0; 2251 /* substitution number is base 36 */ 2252 if ((nth = strtol(ddata->cur, (char **) NULL, 36)) == 0 && 2253 errno != 0) 2254 return (0); 2255 2256 /* first was '_', so increase one */ 2257 ++nth; 2258 2259 while (*ddata->cur != '_') 2260 ++ddata->cur; 2261 2262 assert(nth > 0); 2263 2264 return (cpp_demangle_get_subst(ddata, nth)); 2265 } 2266 2267 /* NOTREACHED */ 2268 return (0); 2269 } 2270 2271 static int 2272 cpp_demangle_read_subst_std(struct cpp_demangle_data *ddata) 2273 { 2274 struct vector_str *output, v; 2275 size_t p_idx, subst_str_len; 2276 int rtn; 2277 char *subst_str; 2278 2279 if (ddata == NULL) 2280 return (0); 2281 2282 if (!vector_str_init(&v)) 2283 return (0); 2284 2285 subst_str = NULL; 2286 rtn = 0; 2287 if (!cpp_demangle_push_str(ddata, "std::", 5)) 2288 goto clean; 2289 2290 if (!vector_str_push(&v, "std::", 5)) 2291 goto clean; 2292 2293 ddata->cur += 2; 2294 2295 output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output; 2296 2297 p_idx = output->size; 2298 if (!cpp_demangle_read_uqname(ddata)) 2299 goto clean; 2300 2301 if ((subst_str = vector_str_substr(output, p_idx, output->size - 1, 2302 &subst_str_len)) == NULL) 2303 goto clean; 2304 2305 if (!vector_str_push(&v, subst_str, subst_str_len)) 2306 goto clean; 2307 2308 if (!cpp_demangle_push_subst_v(ddata, &v)) 2309 goto clean; 2310 2311 if (*ddata->cur == 'I') { 2312 p_idx = output->size; 2313 if (!cpp_demangle_read_tmpl_args(ddata)) 2314 goto clean; 2315 free(subst_str); 2316 if ((subst_str = vector_str_substr(output, p_idx, 2317 output->size - 1, &subst_str_len)) == NULL) 2318 goto clean; 2319 if (!vector_str_push(&v, subst_str, subst_str_len)) 2320 goto clean; 2321 if (!cpp_demangle_push_subst_v(ddata, &v)) 2322 goto clean; 2323 } 2324 2325 rtn = 1; 2326 clean: 2327 free(subst_str); 2328 vector_str_dest(&v); 2329 2330 return (rtn); 2331 } 2332 2333 static int 2334 cpp_demangle_read_subst_stdtmpl(struct cpp_demangle_data *ddata, 2335 const char *str, size_t len) 2336 { 2337 struct vector_str *output; 2338 size_t p_idx, substr_len; 2339 int rtn; 2340 char *subst_str, *substr; 2341 2342 if (ddata == NULL || str == NULL || len == 0) 2343 return (0); 2344 2345 output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output; 2346 2347 p_idx = output->size; 2348 substr = NULL; 2349 subst_str = NULL; 2350 2351 if (!cpp_demangle_read_tmpl_args(ddata)) 2352 return (0); 2353 if ((substr = vector_str_substr(output, p_idx, output->size - 1, 2354 &substr_len)) == NULL) 2355 return (0); 2356 2357 rtn = 0; 2358 if ((subst_str = malloc(sizeof(char) * (substr_len + len + 1))) == 2359 NULL) 2360 goto clean; 2361 2362 memcpy(subst_str, str, len); 2363 memcpy(subst_str + len, substr, substr_len); 2364 subst_str[substr_len + len] = '\0'; 2365 2366 if (!cpp_demangle_push_subst(ddata, subst_str, substr_len + len)) 2367 goto clean; 2368 2369 rtn = 1; 2370 clean: 2371 free(subst_str); 2372 free(substr); 2373 2374 return (rtn); 2375 } 2376 2377 static int 2378 cpp_demangle_read_tmpl_arg(struct cpp_demangle_data *ddata) 2379 { 2380 2381 if (ddata == NULL || *ddata->cur == '\0') 2382 return (0); 2383 2384 switch (*ddata->cur) { 2385 case 'L': 2386 return (cpp_demangle_read_expr_primary(ddata)); 2387 case 'X': 2388 return (cpp_demangle_read_expression(ddata)); 2389 }; 2390 2391 return (cpp_demangle_read_type(ddata, 0)); 2392 } 2393 2394 static int 2395 cpp_demangle_read_tmpl_args(struct cpp_demangle_data *ddata) 2396 { 2397 struct vector_str *v; 2398 size_t arg_len, idx, limit, size; 2399 char *arg; 2400 2401 if (ddata == NULL || *ddata->cur == '\0') 2402 return (0); 2403 2404 ++ddata->cur; 2405 2406 if (!vector_read_cmd_push(&ddata->cmd, READ_TMPL)) 2407 return (0); 2408 2409 if (!cpp_demangle_push_str(ddata, "<", 1)) 2410 return (0); 2411 2412 limit = 0; 2413 v = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output; 2414 for (;;) { 2415 idx = v->size; 2416 if (!cpp_demangle_read_tmpl_arg(ddata)) 2417 return (0); 2418 if ((arg = vector_str_substr(v, idx, v->size - 1, &arg_len)) == 2419 NULL) 2420 return (0); 2421 if (!vector_str_find(&ddata->tmpl, arg, arg_len) && 2422 !vector_str_push(&ddata->tmpl, arg, arg_len)) { 2423 free(arg); 2424 return (0); 2425 } 2426 2427 free(arg); 2428 2429 if (*ddata->cur == 'E') { 2430 ++ddata->cur; 2431 size = v->size; 2432 assert(size > 0); 2433 if (!strncmp(v->container[size - 1], ">", 1)) { 2434 if (!cpp_demangle_push_str(ddata, " >", 2)) 2435 return (0); 2436 } else if (!cpp_demangle_push_str(ddata, ">", 1)) 2437 return (0); 2438 break; 2439 } else if (*ddata->cur != 'I' && 2440 !cpp_demangle_push_str(ddata, ", ", 2)) 2441 return (0); 2442 2443 if (limit++ > CPP_DEMANGLE_TRY_LIMIT) 2444 return (0); 2445 } 2446 2447 return (vector_read_cmd_pop(&ddata->cmd)); 2448 } 2449 2450 /* 2451 * Read template parameter that forms in 'T[number]_'. 2452 * This function much like to read_subst but only for types. 2453 */ 2454 static int 2455 cpp_demangle_read_tmpl_param(struct cpp_demangle_data *ddata) 2456 { 2457 long nth; 2458 2459 if (ddata == NULL || *ddata->cur != 'T') 2460 return (0); 2461 2462 ++ddata->cur; 2463 2464 if (*ddata->cur == '_') 2465 return (cpp_demangle_get_tmpl_param(ddata, 0)); 2466 else { 2467 2468 errno = 0; 2469 if ((nth = strtol(ddata->cur, (char **) NULL, 36)) == 0 && 2470 errno != 0) 2471 return (0); 2472 2473 /* T_ is first */ 2474 ++nth; 2475 2476 while (*ddata->cur != '_') 2477 ++ddata->cur; 2478 2479 assert(nth > 0); 2480 2481 return (cpp_demangle_get_tmpl_param(ddata, nth)); 2482 } 2483 2484 /* NOTREACHED */ 2485 return (0); 2486 } 2487 2488 static int 2489 cpp_demangle_read_type(struct cpp_demangle_data *ddata, int delimit) 2490 { 2491 struct vector_type_qualifier v; 2492 struct vector_str *output; 2493 size_t p_idx, type_str_len; 2494 int extern_c, is_builtin; 2495 long len; 2496 char *type_str, *exp_str, *num_str; 2497 2498 if (ddata == NULL) 2499 return (0); 2500 2501 output = &ddata->output; 2502 if (ddata->output.size > 0 && !strncmp(ddata->output.container[ddata->output.size - 1], ">", 1)) { 2503 ddata->push_head++; 2504 output = &ddata->output_tmp; 2505 } else if (delimit == 1) { 2506 if (ddata->paren == false) { 2507 if (!cpp_demangle_push_str(ddata, "(", 1)) 2508 return (0); 2509 if (ddata->output.size < 2) 2510 return (0); 2511 ddata->paren = true; 2512 ddata->pfirst = true; 2513 /* Need pop function name */ 2514 if (ddata->subst.size == 1 && 2515 !vector_str_pop(&ddata->subst)) 2516 return (0); 2517 } 2518 2519 if (ddata->pfirst) 2520 ddata->pfirst = false; 2521 else if (*ddata->cur != 'I' && 2522 !cpp_demangle_push_str(ddata, ", ", 2)) 2523 return (0); 2524 } 2525 2526 assert(output != NULL); 2527 /* 2528 * [r, V, K] [P, R, C, G, U] builtin, function, class-enum, array 2529 * pointer-to-member, template-param, template-template-param, subst 2530 */ 2531 2532 if (!vector_type_qualifier_init(&v)) 2533 return (0); 2534 2535 extern_c = 0; 2536 is_builtin = 1; 2537 p_idx = output->size; 2538 type_str = exp_str = num_str = NULL; 2539 again: 2540 /* builtin type */ 2541 switch (*ddata->cur) { 2542 case 'a': 2543 /* signed char */ 2544 if (!cpp_demangle_push_str(ddata, "signed char", 11)) 2545 goto clean; 2546 ++ddata->cur; 2547 goto rtn; 2548 2549 case 'A': 2550 /* array type */ 2551 if (!cpp_demangle_read_array(ddata)) 2552 goto clean; 2553 is_builtin = 0; 2554 goto rtn; 2555 2556 case 'b': 2557 /* bool */ 2558 if (!cpp_demangle_push_str(ddata, "bool", 4)) 2559 goto clean; 2560 ++ddata->cur; 2561 goto rtn; 2562 2563 case 'C': 2564 /* complex pair */ 2565 if (!vector_type_qualifier_push(&v, TYPE_CMX)) 2566 goto clean; 2567 ++ddata->cur; 2568 goto again; 2569 2570 case 'c': 2571 /* char */ 2572 if (!cpp_demangle_push_str(ddata, "char", 4)) 2573 goto clean; 2574 ++ddata->cur; 2575 goto rtn; 2576 2577 case 'd': 2578 /* double */ 2579 if (!cpp_demangle_push_str(ddata, "double", 6)) 2580 goto clean; 2581 ++ddata->cur; 2582 goto rtn; 2583 2584 case 'D': 2585 ++ddata->cur; 2586 switch (*ddata->cur) { 2587 case 'd': 2588 /* IEEE 754r decimal floating point (64 bits) */ 2589 if (!cpp_demangle_push_str(ddata, "decimal64", 9)) 2590 goto clean; 2591 ++ddata->cur; 2592 break; 2593 case 'e': 2594 /* IEEE 754r decimal floating point (128 bits) */ 2595 if (!cpp_demangle_push_str(ddata, "decimal128", 10)) 2596 goto clean; 2597 ++ddata->cur; 2598 break; 2599 case 'f': 2600 /* IEEE 754r decimal floating point (32 bits) */ 2601 if (!cpp_demangle_push_str(ddata, "decimal32", 9)) 2602 goto clean; 2603 ++ddata->cur; 2604 break; 2605 case 'h': 2606 /* IEEE 754r half-precision floating point (16 bits) */ 2607 if (!cpp_demangle_push_str(ddata, "half", 4)) 2608 goto clean; 2609 ++ddata->cur; 2610 break; 2611 case 'i': 2612 /* char32_t */ 2613 if (!cpp_demangle_push_str(ddata, "char32_t", 8)) 2614 goto clean; 2615 ++ddata->cur; 2616 break; 2617 case 'n': 2618 /* std::nullptr_t (i.e., decltype(nullptr)) */ 2619 if (!cpp_demangle_push_str(ddata, "decltype(nullptr)", 2620 17)) 2621 goto clean; 2622 ++ddata->cur; 2623 break; 2624 case 's': 2625 /* char16_t */ 2626 if (!cpp_demangle_push_str(ddata, "char16_t", 8)) 2627 goto clean; 2628 ++ddata->cur; 2629 break; 2630 case 'v': 2631 /* gcc vector_size extension. */ 2632 ++ddata->cur; 2633 if (*ddata->cur == '_') { 2634 ++ddata->cur; 2635 if (!cpp_demangle_read_expression_flat(ddata, 2636 &exp_str)) 2637 goto clean; 2638 if (!vector_str_push(&v.ext_name, exp_str, 2639 strlen(exp_str))) 2640 goto clean; 2641 } else { 2642 if (!cpp_demangle_read_number_as_string(ddata, 2643 &num_str)) 2644 goto clean; 2645 if (!vector_str_push(&v.ext_name, num_str, 2646 strlen(num_str))) 2647 goto clean; 2648 } 2649 if (*ddata->cur != '_') 2650 goto clean; 2651 ++ddata->cur; 2652 if (!vector_type_qualifier_push(&v, TYPE_VEC)) 2653 goto clean; 2654 goto again; 2655 default: 2656 goto clean; 2657 } 2658 goto rtn; 2659 2660 case 'e': 2661 /* long double */ 2662 if (!cpp_demangle_push_str(ddata, "long double", 11)) 2663 goto clean; 2664 ++ddata->cur; 2665 goto rtn; 2666 2667 case 'f': 2668 /* float */ 2669 if (!cpp_demangle_push_str(ddata, "float", 5)) 2670 goto clean; 2671 ++ddata->cur; 2672 goto rtn; 2673 2674 case 'F': 2675 /* function */ 2676 if (!cpp_demangle_read_function(ddata, &extern_c, &v)) 2677 goto clean; 2678 is_builtin = 0; 2679 goto rtn; 2680 2681 case 'g': 2682 /* __float128 */ 2683 if (!cpp_demangle_push_str(ddata, "__float128", 10)) 2684 goto clean; 2685 ++ddata->cur; 2686 goto rtn; 2687 2688 case 'G': 2689 /* imaginary */ 2690 if (!vector_type_qualifier_push(&v, TYPE_IMG)) 2691 goto clean; 2692 ++ddata->cur; 2693 goto again; 2694 2695 case 'h': 2696 /* unsigned char */ 2697 if (!cpp_demangle_push_str(ddata, "unsigned char", 13)) 2698 goto clean; 2699 ++ddata->cur; 2700 goto rtn; 2701 2702 case 'i': 2703 /* int */ 2704 if (!cpp_demangle_push_str(ddata, "int", 3)) 2705 goto clean; 2706 ++ddata->cur; 2707 goto rtn; 2708 2709 case 'j': 2710 /* unsigned int */ 2711 if (!cpp_demangle_push_str(ddata, "unsigned int", 12)) 2712 goto clean; 2713 ++ddata->cur; 2714 goto rtn; 2715 2716 case 'K': 2717 /* const */ 2718 if (!vector_type_qualifier_push(&v, TYPE_CST)) 2719 goto clean; 2720 ++ddata->cur; 2721 goto again; 2722 2723 case 'l': 2724 /* long */ 2725 if (!cpp_demangle_push_str(ddata, "long", 4)) 2726 goto clean; 2727 ++ddata->cur; 2728 goto rtn; 2729 2730 case 'm': 2731 /* unsigned long */ 2732 if (!cpp_demangle_push_str(ddata, "unsigned long", 13)) 2733 goto clean; 2734 2735 ++ddata->cur; 2736 2737 goto rtn; 2738 case 'M': 2739 /* pointer to member */ 2740 if (!cpp_demangle_read_pointer_to_member(ddata)) 2741 goto clean; 2742 is_builtin = 0; 2743 goto rtn; 2744 2745 case 'n': 2746 /* __int128 */ 2747 if (!cpp_demangle_push_str(ddata, "__int128", 8)) 2748 goto clean; 2749 ++ddata->cur; 2750 goto rtn; 2751 2752 case 'o': 2753 /* unsigned __int128 */ 2754 if (!cpp_demangle_push_str(ddata, "unsigned __int128", 17)) 2755 goto clean; 2756 ++ddata->cur; 2757 goto rtn; 2758 2759 case 'P': 2760 /* pointer */ 2761 if (!vector_type_qualifier_push(&v, TYPE_PTR)) 2762 goto clean; 2763 ++ddata->cur; 2764 goto again; 2765 2766 case 'r': 2767 /* restrict */ 2768 if (!vector_type_qualifier_push(&v, TYPE_RST)) 2769 goto clean; 2770 ++ddata->cur; 2771 goto again; 2772 2773 case 'R': 2774 /* reference */ 2775 if (!vector_type_qualifier_push(&v, TYPE_REF)) 2776 goto clean; 2777 ++ddata->cur; 2778 goto again; 2779 2780 case 's': 2781 /* short, local string */ 2782 if (!cpp_demangle_push_str(ddata, "short", 5)) 2783 goto clean; 2784 ++ddata->cur; 2785 goto rtn; 2786 2787 case 'S': 2788 /* substitution */ 2789 if (!cpp_demangle_read_subst(ddata)) 2790 goto clean; 2791 is_builtin = 0; 2792 goto rtn; 2793 2794 case 't': 2795 /* unsigned short */ 2796 if (!cpp_demangle_push_str(ddata, "unsigned short", 14)) 2797 goto clean; 2798 ++ddata->cur; 2799 goto rtn; 2800 2801 case 'T': 2802 /* template parameter */ 2803 if (!cpp_demangle_read_tmpl_param(ddata)) 2804 goto clean; 2805 is_builtin = 0; 2806 goto rtn; 2807 2808 case 'u': 2809 /* vendor extended builtin */ 2810 ++ddata->cur; 2811 if (!cpp_demangle_read_sname(ddata)) 2812 goto clean; 2813 is_builtin = 0; 2814 goto rtn; 2815 2816 case 'U': 2817 /* vendor extended type qualifier */ 2818 if (!cpp_demangle_read_number(ddata, &len)) 2819 goto clean; 2820 if (len <= 0) 2821 goto clean; 2822 if (!vector_str_push(&v.ext_name, ddata->cur, len)) 2823 return (0); 2824 ddata->cur += len; 2825 if (!vector_type_qualifier_push(&v, TYPE_EXT)) 2826 goto clean; 2827 goto again; 2828 2829 case 'v': 2830 /* void */ 2831 if (!cpp_demangle_push_str(ddata, "void", 4)) 2832 goto clean; 2833 ++ddata->cur; 2834 goto rtn; 2835 2836 case 'V': 2837 /* volatile */ 2838 if (!vector_type_qualifier_push(&v, TYPE_VAT)) 2839 goto clean; 2840 ++ddata->cur; 2841 goto again; 2842 2843 case 'w': 2844 /* wchar_t */ 2845 if (!cpp_demangle_push_str(ddata, "wchar_t", 6)) 2846 goto clean; 2847 ++ddata->cur; 2848 goto rtn; 2849 2850 case 'x': 2851 /* long long */ 2852 if (!cpp_demangle_push_str(ddata, "long long", 9)) 2853 goto clean; 2854 ++ddata->cur; 2855 goto rtn; 2856 2857 case 'y': 2858 /* unsigned long long */ 2859 if (!cpp_demangle_push_str(ddata, "unsigned long long", 18)) 2860 goto clean; 2861 ++ddata->cur; 2862 goto rtn; 2863 2864 case 'z': 2865 /* ellipsis */ 2866 if (!cpp_demangle_push_str(ddata, "ellipsis", 8)) 2867 goto clean; 2868 ++ddata->cur; 2869 goto rtn; 2870 }; 2871 2872 if (!cpp_demangle_read_name(ddata)) 2873 goto clean; 2874 2875 is_builtin = 0; 2876 rtn: 2877 if ((type_str = vector_str_substr(output, p_idx, output->size - 1, 2878 &type_str_len)) == NULL) 2879 goto clean; 2880 2881 if (is_builtin == 0) { 2882 if (!vector_str_find(&ddata->subst, type_str, type_str_len) && 2883 !vector_str_push(&ddata->subst, type_str, type_str_len)) 2884 goto clean; 2885 } 2886 2887 if (!cpp_demangle_push_type_qualifier(ddata, &v, type_str)) 2888 goto clean; 2889 2890 free(type_str); 2891 free(exp_str); 2892 free(num_str); 2893 vector_type_qualifier_dest(&v); 2894 2895 if (ddata->push_head > 0) { 2896 if (*ddata->cur == 'I' && cpp_demangle_read_tmpl_args(ddata) 2897 == 0) 2898 return (0); 2899 2900 if (--ddata->push_head > 0) 2901 return (1); 2902 2903 if (!vector_str_push(&ddata->output_tmp, " ", 1)) 2904 return (0); 2905 2906 if (!vector_str_push_vector_head(&ddata->output, 2907 &ddata->output_tmp)) 2908 return (0); 2909 2910 vector_str_dest(&ddata->output_tmp); 2911 if (!vector_str_init(&ddata->output_tmp)) 2912 return (0); 2913 2914 if (!cpp_demangle_push_str(ddata, "(", 1)) 2915 return (0); 2916 2917 ddata->paren = true; 2918 ddata->pfirst = true; 2919 } 2920 2921 return (1); 2922 clean: 2923 free(type_str); 2924 free(exp_str); 2925 free(num_str); 2926 vector_type_qualifier_dest(&v); 2927 2928 return (0); 2929 } 2930 2931 static int 2932 cpp_demangle_read_type_flat(struct cpp_demangle_data *ddata, char **str) 2933 { 2934 struct vector_str *output; 2935 size_t i, p_idx, idx, type_len; 2936 char *type; 2937 2938 output = ddata->push_head > 0 ? &ddata->output_tmp : 2939 &ddata->output; 2940 2941 p_idx = output->size; 2942 2943 if (!cpp_demangle_read_type(ddata, 0)) 2944 return (0); 2945 2946 if ((type = vector_str_substr(output, p_idx, output->size - 1, 2947 &type_len)) == NULL) 2948 return (0); 2949 2950 idx = output->size; 2951 for (i = p_idx; i < idx; ++i) { 2952 if (!vector_str_pop(output)) { 2953 free(type); 2954 return (0); 2955 } 2956 } 2957 2958 *str = type; 2959 2960 return (1); 2961 } 2962 2963 /* 2964 * read unqualified-name, unqualified name are operator-name, ctor-dtor-name, 2965 * source-name 2966 */ 2967 static int 2968 cpp_demangle_read_uqname(struct cpp_demangle_data *ddata) 2969 { 2970 size_t len; 2971 2972 if (ddata == NULL || *ddata->cur == '\0') 2973 return (0); 2974 2975 /* operator name */ 2976 switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) { 2977 case SIMPLE_HASH('a', 'a'): 2978 /* operator && */ 2979 if (!cpp_demangle_push_str(ddata, "operator&&", 10)) 2980 return (0); 2981 ddata->cur += 2; 2982 return (1); 2983 2984 case SIMPLE_HASH('a', 'd'): 2985 /* operator & (unary) */ 2986 if (!cpp_demangle_push_str(ddata, "operator&", 9)) 2987 return (0); 2988 ddata->cur += 2; 2989 return (1); 2990 2991 case SIMPLE_HASH('a', 'n'): 2992 /* operator & */ 2993 if (!cpp_demangle_push_str(ddata, "operator&", 9)) 2994 return (0); 2995 ddata->cur += 2; 2996 return (1); 2997 2998 case SIMPLE_HASH('a', 'N'): 2999 /* operator &= */ 3000 if (!cpp_demangle_push_str(ddata, "operator&=", 10)) 3001 return (0); 3002 ddata->cur += 2; 3003 return (1); 3004 3005 case SIMPLE_HASH('a', 'S'): 3006 /* operator = */ 3007 if (!cpp_demangle_push_str(ddata, "operator=", 9)) 3008 return (0); 3009 ddata->cur += 2; 3010 return (1); 3011 3012 case SIMPLE_HASH('c', 'l'): 3013 /* operator () */ 3014 if (!cpp_demangle_push_str(ddata, "operator()", 10)) 3015 return (0); 3016 ddata->cur += 2; 3017 return (1); 3018 3019 case SIMPLE_HASH('c', 'm'): 3020 /* operator , */ 3021 if (!cpp_demangle_push_str(ddata, "operator,", 9)) 3022 return (0); 3023 ddata->cur += 2; 3024 return (1); 3025 3026 case SIMPLE_HASH('c', 'o'): 3027 /* operator ~ */ 3028 if (!cpp_demangle_push_str(ddata, "operator~", 9)) 3029 return (0); 3030 ddata->cur += 2; 3031 return (1); 3032 3033 case SIMPLE_HASH('c', 'v'): 3034 /* operator (cast) */ 3035 if (!cpp_demangle_push_str(ddata, "operator(cast)", 14)) 3036 return (0); 3037 ddata->cur += 2; 3038 return (cpp_demangle_read_type(ddata, 1)); 3039 3040 case SIMPLE_HASH('d', 'a'): 3041 /* operator delete [] */ 3042 if (!cpp_demangle_push_str(ddata, "operator delete []", 18)) 3043 return (0); 3044 ddata->cur += 2; 3045 return (1); 3046 3047 case SIMPLE_HASH('d', 'e'): 3048 /* operator * (unary) */ 3049 if (!cpp_demangle_push_str(ddata, "operator*", 9)) 3050 return (0); 3051 ddata->cur += 2; 3052 return (1); 3053 3054 case SIMPLE_HASH('d', 'l'): 3055 /* operator delete */ 3056 if (!cpp_demangle_push_str(ddata, "operator delete", 15)) 3057 return (0); 3058 ddata->cur += 2; 3059 return (1); 3060 3061 case SIMPLE_HASH('d', 'v'): 3062 /* operator / */ 3063 if (!cpp_demangle_push_str(ddata, "operator/", 9)) 3064 return (0); 3065 ddata->cur += 2; 3066 return (1); 3067 3068 case SIMPLE_HASH('d', 'V'): 3069 /* operator /= */ 3070 if (!cpp_demangle_push_str(ddata, "operator/=", 10)) 3071 return (0); 3072 ddata->cur += 2; 3073 return (1); 3074 3075 case SIMPLE_HASH('e', 'o'): 3076 /* operator ^ */ 3077 if (!cpp_demangle_push_str(ddata, "operator^", 9)) 3078 return (0); 3079 ddata->cur += 2; 3080 return (1); 3081 3082 case SIMPLE_HASH('e', 'O'): 3083 /* operator ^= */ 3084 if (!cpp_demangle_push_str(ddata, "operator^=", 10)) 3085 return (0); 3086 ddata->cur += 2; 3087 return (1); 3088 3089 case SIMPLE_HASH('e', 'q'): 3090 /* operator == */ 3091 if (!cpp_demangle_push_str(ddata, "operator==", 10)) 3092 return (0); 3093 ddata->cur += 2; 3094 return (1); 3095 3096 case SIMPLE_HASH('g', 'e'): 3097 /* operator >= */ 3098 if (!cpp_demangle_push_str(ddata, "operator>=", 10)) 3099 return (0); 3100 ddata->cur += 2; 3101 return (1); 3102 3103 case SIMPLE_HASH('g', 't'): 3104 /* operator > */ 3105 if (!cpp_demangle_push_str(ddata, "operator>", 9)) 3106 return (0); 3107 ddata->cur += 2; 3108 return (1); 3109 3110 case SIMPLE_HASH('i', 'x'): 3111 /* operator [] */ 3112 if (!cpp_demangle_push_str(ddata, "operator[]", 10)) 3113 return (0); 3114 ddata->cur += 2; 3115 return (1); 3116 3117 case SIMPLE_HASH('l', 'e'): 3118 /* operator <= */ 3119 if (!cpp_demangle_push_str(ddata, "operator<=", 10)) 3120 return (0); 3121 ddata->cur += 2; 3122 return (1); 3123 3124 case SIMPLE_HASH('l', 's'): 3125 /* operator << */ 3126 if (!cpp_demangle_push_str(ddata, "operator<<", 10)) 3127 return (0); 3128 ddata->cur += 2; 3129 return (1); 3130 3131 case SIMPLE_HASH('l', 'S'): 3132 /* operator <<= */ 3133 if (!cpp_demangle_push_str(ddata, "operator<<=", 11)) 3134 return (0); 3135 ddata->cur += 2; 3136 return (1); 3137 3138 case SIMPLE_HASH('l', 't'): 3139 /* operator < */ 3140 if (!cpp_demangle_push_str(ddata, "operator<", 9)) 3141 return (0); 3142 ddata->cur += 2; 3143 return (1); 3144 3145 case SIMPLE_HASH('m', 'i'): 3146 /* operator - */ 3147 if (!cpp_demangle_push_str(ddata, "operator-", 9)) 3148 return (0); 3149 ddata->cur += 2; 3150 return (1); 3151 3152 case SIMPLE_HASH('m', 'I'): 3153 /* operator -= */ 3154 if (!cpp_demangle_push_str(ddata, "operator-=", 10)) 3155 return (0); 3156 ddata->cur += 2; 3157 return (1); 3158 3159 case SIMPLE_HASH('m', 'l'): 3160 /* operator * */ 3161 if (!cpp_demangle_push_str(ddata, "operator*", 9)) 3162 return (0); 3163 ddata->cur += 2; 3164 return (1); 3165 3166 case SIMPLE_HASH('m', 'L'): 3167 /* operator *= */ 3168 if (!cpp_demangle_push_str(ddata, "operator*=", 10)) 3169 return (0); 3170 ddata->cur += 2; 3171 return (1); 3172 3173 case SIMPLE_HASH('m', 'm'): 3174 /* operator -- */ 3175 if (!cpp_demangle_push_str(ddata, "operator--", 10)) 3176 return (0); 3177 ddata->cur += 2; 3178 return (1); 3179 3180 case SIMPLE_HASH('n', 'a'): 3181 /* operator new[] */ 3182 if (!cpp_demangle_push_str(ddata, "operator new []", 15)) 3183 return (0); 3184 ddata->cur += 2; 3185 return (1); 3186 3187 case SIMPLE_HASH('n', 'e'): 3188 /* operator != */ 3189 if (!cpp_demangle_push_str(ddata, "operator!=", 10)) 3190 return (0); 3191 ddata->cur += 2; 3192 return (1); 3193 3194 case SIMPLE_HASH('n', 'g'): 3195 /* operator - (unary) */ 3196 if (!cpp_demangle_push_str(ddata, "operator-", 9)) 3197 return (0); 3198 ddata->cur += 2; 3199 return (1); 3200 3201 case SIMPLE_HASH('n', 't'): 3202 /* operator ! */ 3203 if (!cpp_demangle_push_str(ddata, "operator!", 9)) 3204 return (0); 3205 ddata->cur += 2; 3206 return (1); 3207 3208 case SIMPLE_HASH('n', 'w'): 3209 /* operator new */ 3210 if (!cpp_demangle_push_str(ddata, "operator new", 12)) 3211 return (0); 3212 ddata->cur += 2; 3213 return (1); 3214 3215 case SIMPLE_HASH('o', 'o'): 3216 /* operator || */ 3217 if (!cpp_demangle_push_str(ddata, "operator||", 10)) 3218 return (0); 3219 ddata->cur += 2; 3220 return (1); 3221 3222 case SIMPLE_HASH('o', 'r'): 3223 /* operator | */ 3224 if (!cpp_demangle_push_str(ddata, "operator|", 9)) 3225 return (0); 3226 ddata->cur += 2; 3227 return (1); 3228 3229 case SIMPLE_HASH('o', 'R'): 3230 /* operator |= */ 3231 if (!cpp_demangle_push_str(ddata, "operator|=", 10)) 3232 return (0); 3233 ddata->cur += 2; 3234 return (1); 3235 3236 case SIMPLE_HASH('p', 'l'): 3237 /* operator + */ 3238 if (!cpp_demangle_push_str(ddata, "operator+", 9)) 3239 return (0); 3240 ddata->cur += 2; 3241 return (1); 3242 3243 case SIMPLE_HASH('p', 'L'): 3244 /* operator += */ 3245 if (!cpp_demangle_push_str(ddata, "operator+=", 10)) 3246 return (0); 3247 ddata->cur += 2; 3248 return (1); 3249 3250 case SIMPLE_HASH('p', 'm'): 3251 /* operator ->* */ 3252 if (!cpp_demangle_push_str(ddata, "operator->*", 11)) 3253 return (0); 3254 ddata->cur += 2; 3255 return (1); 3256 3257 case SIMPLE_HASH('p', 'p'): 3258 /* operator ++ */ 3259 if (!cpp_demangle_push_str(ddata, "operator++", 10)) 3260 return (0); 3261 ddata->cur += 2; 3262 return (1); 3263 3264 case SIMPLE_HASH('p', 's'): 3265 /* operator + (unary) */ 3266 if (!cpp_demangle_push_str(ddata, "operator+", 9)) 3267 return (0); 3268 ddata->cur += 2; 3269 return (1); 3270 3271 case SIMPLE_HASH('p', 't'): 3272 /* operator -> */ 3273 if (!cpp_demangle_push_str(ddata, "operator->", 10)) 3274 return (0); 3275 ddata->cur += 2; 3276 return (1); 3277 3278 case SIMPLE_HASH('q', 'u'): 3279 /* operator ? */ 3280 if (!cpp_demangle_push_str(ddata, "operator?", 9)) 3281 return (0); 3282 ddata->cur += 2; 3283 return (1); 3284 3285 case SIMPLE_HASH('r', 'm'): 3286 /* operator % */ 3287 if (!cpp_demangle_push_str(ddata, "operator%", 9)) 3288 return (0); 3289 ddata->cur += 2; 3290 return (1); 3291 3292 case SIMPLE_HASH('r', 'M'): 3293 /* operator %= */ 3294 if (!cpp_demangle_push_str(ddata, "operator%=", 10)) 3295 return (0); 3296 ddata->cur += 2; 3297 return (1); 3298 3299 case SIMPLE_HASH('r', 's'): 3300 /* operator >> */ 3301 if (!cpp_demangle_push_str(ddata, "operator>>", 10)) 3302 return (0); 3303 ddata->cur += 2; 3304 return (1); 3305 3306 case SIMPLE_HASH('r', 'S'): 3307 /* operator >>= */ 3308 if (!cpp_demangle_push_str(ddata, "operator>>=", 11)) 3309 return (0); 3310 ddata->cur += 2; 3311 return (1); 3312 3313 case SIMPLE_HASH('r', 'z'): 3314 /* operator sizeof */ 3315 if (!cpp_demangle_push_str(ddata, "operator sizeof ", 16)) 3316 return (0); 3317 ddata->cur += 2; 3318 return (1); 3319 3320 case SIMPLE_HASH('s', 'r'): 3321 /* scope resolution operator */ 3322 if (!cpp_demangle_push_str(ddata, "scope resolution operator ", 3323 26)) 3324 return (0); 3325 ddata->cur += 2; 3326 return (1); 3327 3328 case SIMPLE_HASH('s', 'v'): 3329 /* operator sizeof */ 3330 if (!cpp_demangle_push_str(ddata, "operator sizeof ", 16)) 3331 return (0); 3332 ddata->cur += 2; 3333 return (1); 3334 }; 3335 3336 /* vendor extened operator */ 3337 if (*ddata->cur == 'v' && ELFTC_ISDIGIT(*(ddata->cur + 1))) { 3338 if (!cpp_demangle_push_str(ddata, "vendor extened operator ", 3339 24)) 3340 return (0); 3341 if (!cpp_demangle_push_str(ddata, ddata->cur + 1, 1)) 3342 return (0); 3343 ddata->cur += 2; 3344 return (cpp_demangle_read_sname(ddata)); 3345 } 3346 3347 /* ctor-dtor-name */ 3348 switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) { 3349 case SIMPLE_HASH('C', '1'): 3350 /* FALLTHROUGH */ 3351 case SIMPLE_HASH('C', '2'): 3352 /* FALLTHROUGH */ 3353 case SIMPLE_HASH('C', '3'): 3354 if (ddata->last_sname == NULL) 3355 return (0); 3356 if ((len = strlen(ddata->last_sname)) == 0) 3357 return (0); 3358 if (!cpp_demangle_push_str(ddata, "::", 2)) 3359 return (0); 3360 if (!cpp_demangle_push_str(ddata, ddata->last_sname, len)) 3361 return (0); 3362 ddata->cur +=2; 3363 return (1); 3364 3365 case SIMPLE_HASH('D', '0'): 3366 /* FALLTHROUGH */ 3367 case SIMPLE_HASH('D', '1'): 3368 /* FALLTHROUGH */ 3369 case SIMPLE_HASH('D', '2'): 3370 if (ddata->last_sname == NULL) 3371 return (0); 3372 if ((len = strlen(ddata->last_sname)) == 0) 3373 return (0); 3374 if (!cpp_demangle_push_str(ddata, "::~", 3)) 3375 return (0); 3376 if (!cpp_demangle_push_str(ddata, ddata->last_sname, len)) 3377 return (0); 3378 ddata->cur +=2; 3379 return (1); 3380 }; 3381 3382 /* source name */ 3383 if (ELFTC_ISDIGIT(*ddata->cur) != 0) 3384 return (cpp_demangle_read_sname(ddata)); 3385 3386 /* local source name */ 3387 if (*ddata->cur == 'L') 3388 return (cpp_demangle_local_source_name(ddata)); 3389 3390 return (1); 3391 } 3392 3393 /* 3394 * Read local source name. 3395 * 3396 * References: 3397 * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31775 3398 * http://gcc.gnu.org/viewcvs?view=rev&revision=124467 3399 */ 3400 static int 3401 cpp_demangle_local_source_name(struct cpp_demangle_data *ddata) 3402 { 3403 /* L */ 3404 if (ddata == NULL || *ddata->cur != 'L') 3405 return (0); 3406 ++ddata->cur; 3407 3408 /* source name */ 3409 if (!cpp_demangle_read_sname(ddata)) 3410 return (0); 3411 3412 /* discriminator */ 3413 if (*ddata->cur == '_') { 3414 ++ddata->cur; 3415 while (ELFTC_ISDIGIT(*ddata->cur) != 0) 3416 ++ddata->cur; 3417 } 3418 3419 return (1); 3420 } 3421 3422 static int 3423 cpp_demangle_read_v_offset(struct cpp_demangle_data *ddata) 3424 { 3425 3426 if (ddata == NULL) 3427 return (0); 3428 3429 if (!cpp_demangle_push_str(ddata, "offset : ", 9)) 3430 return (0); 3431 3432 if (!cpp_demangle_read_offset_number(ddata)) 3433 return (0); 3434 3435 if (!cpp_demangle_push_str(ddata, "virtual offset : ", 17)) 3436 return (0); 3437 3438 return (!cpp_demangle_read_offset_number(ddata)); 3439 } 3440 3441 /* 3442 * Decode floating point representation to string 3443 * Return new allocated string or NULL 3444 * 3445 * Todo 3446 * Replace these functions to macro. 3447 */ 3448 static char * 3449 decode_fp_to_double(const char *p, size_t len) 3450 { 3451 double f; 3452 size_t rtn_len, limit, i; 3453 int byte; 3454 char *rtn; 3455 3456 if (p == NULL || len == 0 || len % 2 != 0 || len / 2 > sizeof(double)) 3457 return (NULL); 3458 3459 memset(&f, 0, sizeof(double)); 3460 3461 for (i = 0; i < len / 2; ++i) { 3462 byte = hex_to_dec(p[len - i * 2 - 1]) + 3463 hex_to_dec(p[len - i * 2 - 2]) * 16; 3464 3465 if (byte < 0 || byte > 255) 3466 return (NULL); 3467 3468 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3469 ((unsigned char *)&f)[i] = (unsigned char)(byte); 3470 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3471 ((unsigned char *)&f)[sizeof(double) - i - 1] = 3472 (unsigned char)(byte); 3473 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3474 } 3475 3476 rtn_len = 64; 3477 limit = 0; 3478 again: 3479 if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) 3480 return (NULL); 3481 3482 if (snprintf(rtn, rtn_len, "%fld", f) >= (int)rtn_len) { 3483 free(rtn); 3484 if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) 3485 return (NULL); 3486 rtn_len *= BUFFER_GROWFACTOR; 3487 goto again; 3488 } 3489 3490 return rtn; 3491 } 3492 3493 static char * 3494 decode_fp_to_float(const char *p, size_t len) 3495 { 3496 size_t i, rtn_len, limit; 3497 float f; 3498 int byte; 3499 char *rtn; 3500 3501 if (p == NULL || len == 0 || len % 2 != 0 || len / 2 > sizeof(float)) 3502 return (NULL); 3503 3504 memset(&f, 0, sizeof(float)); 3505 3506 for (i = 0; i < len / 2; ++i) { 3507 byte = hex_to_dec(p[len - i * 2 - 1]) + 3508 hex_to_dec(p[len - i * 2 - 2]) * 16; 3509 if (byte < 0 || byte > 255) 3510 return (NULL); 3511 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3512 ((unsigned char *)&f)[i] = (unsigned char)(byte); 3513 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3514 ((unsigned char *)&f)[sizeof(float) - i - 1] = 3515 (unsigned char)(byte); 3516 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3517 } 3518 3519 rtn_len = 64; 3520 limit = 0; 3521 again: 3522 if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) 3523 return (NULL); 3524 3525 if (snprintf(rtn, rtn_len, "%ff", f) >= (int)rtn_len) { 3526 free(rtn); 3527 if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) 3528 return (NULL); 3529 rtn_len *= BUFFER_GROWFACTOR; 3530 goto again; 3531 } 3532 3533 return rtn; 3534 } 3535 3536 static char * 3537 decode_fp_to_float128(const char *p, size_t len) 3538 { 3539 long double f; 3540 size_t rtn_len, limit, i; 3541 int byte; 3542 unsigned char buf[FLOAT_QUADRUPLE_BYTES]; 3543 char *rtn; 3544 3545 switch(sizeof(long double)) { 3546 case FLOAT_QUADRUPLE_BYTES: 3547 return (decode_fp_to_long_double(p, len)); 3548 case FLOAT_EXTENED_BYTES: 3549 if (p == NULL || len == 0 || len % 2 != 0 || 3550 len / 2 > FLOAT_QUADRUPLE_BYTES) 3551 return (NULL); 3552 3553 memset(buf, 0, FLOAT_QUADRUPLE_BYTES); 3554 3555 for (i = 0; i < len / 2; ++i) { 3556 byte = hex_to_dec(p[len - i * 2 - 1]) + 3557 hex_to_dec(p[len - i * 2 - 2]) * 16; 3558 if (byte < 0 || byte > 255) 3559 return (NULL); 3560 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3561 buf[i] = (unsigned char)(byte); 3562 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3563 buf[FLOAT_QUADRUPLE_BYTES - i -1] = 3564 (unsigned char)(byte); 3565 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3566 } 3567 memset(&f, 0, FLOAT_EXTENED_BYTES); 3568 3569 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3570 memcpy(&f, buf, FLOAT_EXTENED_BYTES); 3571 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3572 memcpy(&f, buf + 6, FLOAT_EXTENED_BYTES); 3573 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3574 3575 rtn_len = 256; 3576 limit = 0; 3577 again: 3578 if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) 3579 return (NULL); 3580 3581 if (snprintf(rtn, rtn_len, "%Lfd", f) >= (int)rtn_len) { 3582 free(rtn); 3583 if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) 3584 return (NULL); 3585 rtn_len *= BUFFER_GROWFACTOR; 3586 goto again; 3587 } 3588 3589 return (rtn); 3590 default: 3591 return (NULL); 3592 } 3593 } 3594 3595 static char * 3596 decode_fp_to_float80(const char *p, size_t len) 3597 { 3598 long double f; 3599 size_t rtn_len, limit, i; 3600 int byte; 3601 unsigned char buf[FLOAT_EXTENED_BYTES]; 3602 char *rtn; 3603 3604 switch(sizeof(long double)) { 3605 case FLOAT_QUADRUPLE_BYTES: 3606 if (p == NULL || len == 0 || len % 2 != 0 || 3607 len / 2 > FLOAT_EXTENED_BYTES) 3608 return (NULL); 3609 3610 memset(buf, 0, FLOAT_EXTENED_BYTES); 3611 3612 for (i = 0; i < len / 2; ++i) { 3613 byte = hex_to_dec(p[len - i * 2 - 1]) + 3614 hex_to_dec(p[len - i * 2 - 2]) * 16; 3615 3616 if (byte < 0 || byte > 255) 3617 return (NULL); 3618 3619 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3620 buf[i] = (unsigned char)(byte); 3621 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3622 buf[FLOAT_EXTENED_BYTES - i -1] = 3623 (unsigned char)(byte); 3624 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3625 } 3626 3627 memset(&f, 0, FLOAT_QUADRUPLE_BYTES); 3628 3629 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3630 memcpy(&f, buf, FLOAT_EXTENED_BYTES); 3631 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3632 memcpy((unsigned char *)(&f) + 6, buf, FLOAT_EXTENED_BYTES); 3633 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3634 3635 rtn_len = 256; 3636 limit = 0; 3637 again: 3638 if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) 3639 return (NULL); 3640 3641 if (snprintf(rtn, rtn_len, "%Lfd", f) >= (int)rtn_len) { 3642 free(rtn); 3643 if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) 3644 return (NULL); 3645 rtn_len *= BUFFER_GROWFACTOR; 3646 goto again; 3647 } 3648 3649 return (rtn); 3650 case FLOAT_EXTENED_BYTES: 3651 return (decode_fp_to_long_double(p, len)); 3652 default: 3653 return (NULL); 3654 } 3655 } 3656 3657 static char * 3658 decode_fp_to_long_double(const char *p, size_t len) 3659 { 3660 long double f; 3661 size_t rtn_len, limit, i; 3662 int byte; 3663 char *rtn; 3664 3665 if (p == NULL || len == 0 || len % 2 != 0 || 3666 len / 2 > sizeof(long double)) 3667 return (NULL); 3668 3669 memset(&f, 0, sizeof(long double)); 3670 3671 for (i = 0; i < len / 2; ++i) { 3672 byte = hex_to_dec(p[len - i * 2 - 1]) + 3673 hex_to_dec(p[len - i * 2 - 2]) * 16; 3674 3675 if (byte < 0 || byte > 255) 3676 return (NULL); 3677 3678 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3679 ((unsigned char *)&f)[i] = (unsigned char)(byte); 3680 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3681 ((unsigned char *)&f)[sizeof(long double) - i - 1] = 3682 (unsigned char)(byte); 3683 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3684 } 3685 3686 rtn_len = 256; 3687 limit = 0; 3688 again: 3689 if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) 3690 return (NULL); 3691 3692 if (snprintf(rtn, rtn_len, "%Lfd", f) >= (int)rtn_len) { 3693 free(rtn); 3694 if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) 3695 return (NULL); 3696 rtn_len *= BUFFER_GROWFACTOR; 3697 goto again; 3698 } 3699 3700 return (rtn); 3701 } 3702 3703 /* Simple hex to integer function used by decode_to_* function. */ 3704 static int 3705 hex_to_dec(char c) 3706 { 3707 3708 switch (c) { 3709 case '0': 3710 return (0); 3711 case '1': 3712 return (1); 3713 case '2': 3714 return (2); 3715 case '3': 3716 return (3); 3717 case '4': 3718 return (4); 3719 case '5': 3720 return (5); 3721 case '6': 3722 return (6); 3723 case '7': 3724 return (7); 3725 case '8': 3726 return (8); 3727 case '9': 3728 return (9); 3729 case 'a': 3730 return (10); 3731 case 'b': 3732 return (11); 3733 case 'c': 3734 return (12); 3735 case 'd': 3736 return (13); 3737 case 'e': 3738 return (14); 3739 case 'f': 3740 return (15); 3741 default: 3742 return (-1); 3743 }; 3744 } 3745 3746 static void 3747 vector_read_cmd_dest(struct vector_read_cmd *v) 3748 { 3749 3750 if (v == NULL) 3751 return; 3752 3753 free(v->r_container); 3754 } 3755 3756 /* return -1 at failed, 0 at not found, 1 at found. */ 3757 static int 3758 vector_read_cmd_find(struct vector_read_cmd *v, enum read_cmd dst) 3759 { 3760 size_t i; 3761 3762 if (v == NULL || dst == READ_FAIL) 3763 return (-1); 3764 3765 for (i = 0; i < v->size; ++i) 3766 if (v->r_container[i] == dst) 3767 return (1); 3768 3769 return (0); 3770 } 3771 3772 static int 3773 vector_read_cmd_init(struct vector_read_cmd *v) 3774 { 3775 3776 if (v == NULL) 3777 return (0); 3778 3779 v->size = 0; 3780 v->capacity = VECTOR_DEF_CAPACITY; 3781 3782 if ((v->r_container = malloc(sizeof(enum read_cmd) * v->capacity)) 3783 == NULL) 3784 return (0); 3785 3786 return (1); 3787 } 3788 3789 static int 3790 vector_read_cmd_pop(struct vector_read_cmd *v) 3791 { 3792 3793 if (v == NULL || v->size == 0) 3794 return (0); 3795 3796 --v->size; 3797 v->r_container[v->size] = READ_FAIL; 3798 3799 return (1); 3800 } 3801 3802 static int 3803 vector_read_cmd_push(struct vector_read_cmd *v, enum read_cmd cmd) 3804 { 3805 enum read_cmd *tmp_r_ctn; 3806 size_t tmp_cap; 3807 size_t i; 3808 3809 if (v == NULL) 3810 return (0); 3811 3812 if (v->size == v->capacity) { 3813 tmp_cap = v->capacity * BUFFER_GROWFACTOR; 3814 if ((tmp_r_ctn = malloc(sizeof(enum read_cmd) * tmp_cap)) 3815 == NULL) 3816 return (0); 3817 for (i = 0; i < v->size; ++i) 3818 tmp_r_ctn[i] = v->r_container[i]; 3819 free(v->r_container); 3820 v->r_container = tmp_r_ctn; 3821 v->capacity = tmp_cap; 3822 } 3823 3824 v->r_container[v->size] = cmd; 3825 ++v->size; 3826 3827 return (1); 3828 } 3829 3830 static void 3831 vector_type_qualifier_dest(struct vector_type_qualifier *v) 3832 { 3833 3834 if (v == NULL) 3835 return; 3836 3837 free(v->q_container); 3838 vector_str_dest(&v->ext_name); 3839 } 3840 3841 /* size, capacity, ext_name */ 3842 static int 3843 vector_type_qualifier_init(struct vector_type_qualifier *v) 3844 { 3845 3846 if (v == NULL) 3847 return (0); 3848 3849 v->size = 0; 3850 v->capacity = VECTOR_DEF_CAPACITY; 3851 3852 if ((v->q_container = malloc(sizeof(enum type_qualifier) * v->capacity)) 3853 == NULL) 3854 return (0); 3855 3856 assert(v->q_container != NULL); 3857 3858 if (vector_str_init(&v->ext_name) == false) { 3859 free(v->q_container); 3860 return (0); 3861 } 3862 3863 return (1); 3864 } 3865 3866 static int 3867 vector_type_qualifier_push(struct vector_type_qualifier *v, 3868 enum type_qualifier t) 3869 { 3870 enum type_qualifier *tmp_ctn; 3871 size_t tmp_cap; 3872 size_t i; 3873 3874 if (v == NULL) 3875 return (0); 3876 3877 if (v->size == v->capacity) { 3878 tmp_cap = v->capacity * BUFFER_GROWFACTOR; 3879 if ((tmp_ctn = malloc(sizeof(enum type_qualifier) * tmp_cap)) 3880 == NULL) 3881 return (0); 3882 for (i = 0; i < v->size; ++i) 3883 tmp_ctn[i] = v->q_container[i]; 3884 free(v->q_container); 3885 v->q_container = tmp_ctn; 3886 v->capacity = tmp_cap; 3887 } 3888 3889 v->q_container[v->size] = t; 3890 ++v->size; 3891 3892 return (1); 3893 } 3894