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 break; 1559 case 't': 1560 default: 1561 if (!cpp_demangle_push_str(ddata, 1562 "transaction clone for ", 22)) 1563 return (0); 1564 break; 1565 } 1566 ++ddata->cur; 1567 return (cpp_demangle_read_encoding(ddata)); 1568 1569 case SIMPLE_HASH('G', 'V'): 1570 /* sentry object for 1 time init */ 1571 if (!cpp_demangle_push_str(ddata, "guard variable for ", 20)) 1572 return (0); 1573 ddata->cur += 2; 1574 break; 1575 1576 case SIMPLE_HASH('T', 'c'): 1577 /* virtual function covariant override thunk */ 1578 if (!cpp_demangle_push_str(ddata, 1579 "virtual function covariant override ", 36)) 1580 return (0); 1581 ddata->cur += 2; 1582 if (*ddata->cur == '\0') 1583 return (0); 1584 if (!cpp_demangle_read_offset(ddata)) 1585 return (0); 1586 if (!cpp_demangle_read_offset(ddata)) 1587 return (0); 1588 return (cpp_demangle_read_encoding(ddata)); 1589 1590 case SIMPLE_HASH('T', 'C'): 1591 /* construction vtable */ 1592 if (!cpp_demangle_push_str(ddata, "construction vtable for ", 1593 24)) 1594 return (0); 1595 ddata->cur += 2; 1596 if (*ddata->cur == '\0') 1597 return (0); 1598 if (!cpp_demangle_read_type_flat(ddata, &type)) 1599 return (0); 1600 rtn = 0; 1601 if (!cpp_demangle_read_number(ddata, &offset)) 1602 goto clean3; 1603 if (*ddata->cur++ != '_') 1604 goto clean3; 1605 if (!cpp_demangle_read_type(ddata, 0)) 1606 goto clean3; 1607 if (!cpp_demangle_push_str(ddata, "-in-", 4)) 1608 goto clean3; 1609 if (!cpp_demangle_push_str(ddata, type, strlen(type))) 1610 goto clean3; 1611 rtn = 1; 1612 clean3: 1613 free(type); 1614 return (rtn); 1615 1616 case SIMPLE_HASH('T', 'D'): 1617 /* typeinfo common proxy */ 1618 break; 1619 1620 case SIMPLE_HASH('T', 'F'): 1621 /* typeinfo fn */ 1622 if (!cpp_demangle_push_str(ddata, "typeinfo fn for ", 16)) 1623 return (0); 1624 ddata->cur += 2; 1625 if (*ddata->cur == '\0') 1626 return (0); 1627 return (cpp_demangle_read_type(ddata, 0)); 1628 1629 case SIMPLE_HASH('T', 'h'): 1630 /* virtual function non-virtual override thunk */ 1631 if (!cpp_demangle_push_str(ddata, 1632 "virtual function non-virtual override ", 38)) 1633 return (0); 1634 ddata->cur += 2; 1635 if (*ddata->cur == '\0') 1636 return (0); 1637 if (!cpp_demangle_read_nv_offset(ddata)) 1638 return (0); 1639 return (cpp_demangle_read_encoding(ddata)); 1640 1641 case SIMPLE_HASH('T', 'H'): 1642 /* TLS init function */ 1643 if (!cpp_demangle_push_str(ddata, "TLS init function for ", 1644 22)) 1645 return (0); 1646 ddata->cur += 2; 1647 if (*ddata->cur == '\0') 1648 return (0); 1649 break; 1650 1651 case SIMPLE_HASH('T', 'I'): 1652 /* typeinfo structure */ 1653 if (!cpp_demangle_push_str(ddata, "typeinfo for ", 13)) 1654 return (0); 1655 ddata->cur += 2; 1656 if (*ddata->cur == '\0') 1657 return (0); 1658 return (cpp_demangle_read_type(ddata, 0)); 1659 1660 case SIMPLE_HASH('T', 'J'): 1661 /* java class */ 1662 if (!cpp_demangle_push_str(ddata, "java Class for ", 15)) 1663 return (0); 1664 ddata->cur += 2; 1665 if (*ddata->cur == '\0') 1666 return (0); 1667 return (cpp_demangle_read_type(ddata, 0)); 1668 1669 case SIMPLE_HASH('T', 'S'): 1670 /* RTTI name (NTBS) */ 1671 if (!cpp_demangle_push_str(ddata, "typeinfo name for ", 18)) 1672 return (0); 1673 ddata->cur += 2; 1674 if (*ddata->cur == '\0') 1675 return (0); 1676 return (cpp_demangle_read_type(ddata, 0)); 1677 1678 case SIMPLE_HASH('T', 'T'): 1679 /* VTT table */ 1680 if (!cpp_demangle_push_str(ddata, "VTT for ", 8)) 1681 return (0); 1682 ddata->cur += 2; 1683 if (*ddata->cur == '\0') 1684 return (0); 1685 return (cpp_demangle_read_type(ddata, 0)); 1686 1687 case SIMPLE_HASH('T', 'v'): 1688 /* virtual function virtual override thunk */ 1689 if (!cpp_demangle_push_str(ddata, 1690 "virtual function virtual override ", 34)) 1691 return (0); 1692 ddata->cur += 2; 1693 if (*ddata->cur == '\0') 1694 return (0); 1695 if (!cpp_demangle_read_v_offset(ddata)) 1696 return (0); 1697 return (cpp_demangle_read_encoding(ddata)); 1698 1699 case SIMPLE_HASH('T', 'V'): 1700 /* virtual table */ 1701 if (!cpp_demangle_push_str(ddata, "vtable for ", 12)) 1702 return (0); 1703 ddata->cur += 2; 1704 if (*ddata->cur == '\0') 1705 return (0); 1706 return (cpp_demangle_read_type(ddata, 0)); 1707 1708 case SIMPLE_HASH('T', 'W'): 1709 /* TLS wrapper function */ 1710 if (!cpp_demangle_push_str(ddata, "TLS wrapper function for ", 1711 25)) 1712 return (0); 1713 ddata->cur += 2; 1714 if (*ddata->cur == '\0') 1715 return (0); 1716 break; 1717 } 1718 1719 return (cpp_demangle_read_name(ddata)); 1720 } 1721 1722 static int 1723 cpp_demangle_read_local_name(struct cpp_demangle_data *ddata) 1724 { 1725 size_t limit; 1726 1727 if (ddata == NULL) 1728 return (0); 1729 if (*(++ddata->cur) == '\0') 1730 return (0); 1731 if (!cpp_demangle_read_encoding(ddata)) 1732 return (0); 1733 1734 limit = 0; 1735 for (;;) { 1736 if (!cpp_demangle_read_type(ddata, 1)) 1737 return (0); 1738 if (*ddata->cur == 'E') 1739 break; 1740 if (limit++ > CPP_DEMANGLE_TRY_LIMIT) 1741 return (0); 1742 } 1743 if (*(++ddata->cur) == '\0') 1744 return (0); 1745 if (ddata->paren == true) { 1746 if (!cpp_demangle_push_str(ddata, ")", 1)) 1747 return (0); 1748 ddata->paren = false; 1749 } 1750 if (*ddata->cur == 's') 1751 ++ddata->cur; 1752 else { 1753 if (!cpp_demangle_push_str(ddata, "::", 2)) 1754 return (0); 1755 if (!cpp_demangle_read_name(ddata)) 1756 return (0); 1757 } 1758 if (*ddata->cur == '_') { 1759 ++ddata->cur; 1760 while (ELFTC_ISDIGIT(*ddata->cur) != 0) 1761 ++ddata->cur; 1762 } 1763 1764 return (1); 1765 } 1766 1767 static int 1768 cpp_demangle_read_name(struct cpp_demangle_data *ddata) 1769 { 1770 struct vector_str *output, v; 1771 size_t p_idx, subst_str_len; 1772 int rtn; 1773 char *subst_str; 1774 1775 if (ddata == NULL || *ddata->cur == '\0') 1776 return (0); 1777 1778 output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output; 1779 1780 subst_str = NULL; 1781 1782 switch (*ddata->cur) { 1783 case 'S': 1784 return (cpp_demangle_read_subst(ddata)); 1785 case 'N': 1786 return (cpp_demangle_read_nested_name(ddata)); 1787 case 'Z': 1788 return (cpp_demangle_read_local_name(ddata)); 1789 } 1790 1791 if (!vector_str_init(&v)) 1792 return (0); 1793 1794 p_idx = output->size; 1795 rtn = 0; 1796 if (!cpp_demangle_read_uqname(ddata)) 1797 goto clean; 1798 if ((subst_str = vector_str_substr(output, p_idx, output->size - 1, 1799 &subst_str_len)) == NULL) 1800 goto clean; 1801 if (subst_str_len > 8 && strstr(subst_str, "operator") != NULL) { 1802 rtn = 1; 1803 goto clean; 1804 } 1805 if (!vector_str_push(&v, subst_str, subst_str_len)) 1806 goto clean; 1807 if (!cpp_demangle_push_subst_v(ddata, &v)) 1808 goto clean; 1809 1810 if (*ddata->cur == 'I') { 1811 p_idx = output->size; 1812 if (!cpp_demangle_read_tmpl_args(ddata)) 1813 goto clean; 1814 free(subst_str); 1815 if ((subst_str = vector_str_substr(output, p_idx, 1816 output->size - 1, &subst_str_len)) == NULL) 1817 goto clean; 1818 if (!vector_str_push(&v, subst_str, subst_str_len)) 1819 goto clean; 1820 if (!cpp_demangle_push_subst_v(ddata, &v)) 1821 goto clean; 1822 } 1823 1824 rtn = 1; 1825 1826 clean: 1827 free(subst_str); 1828 vector_str_dest(&v); 1829 1830 return (rtn); 1831 } 1832 1833 static int 1834 cpp_demangle_read_name_flat(struct cpp_demangle_data *ddata, char **str) 1835 { 1836 struct vector_str *output; 1837 size_t i, p_idx, idx, name_len; 1838 char *name; 1839 1840 output = ddata->push_head > 0 ? &ddata->output_tmp : 1841 &ddata->output; 1842 1843 p_idx = output->size; 1844 1845 if (!cpp_demangle_read_name(ddata)) 1846 return (0); 1847 1848 if ((name = vector_str_substr(output, p_idx, output->size - 1, 1849 &name_len)) == NULL) 1850 return (0); 1851 1852 idx = output->size; 1853 for (i = p_idx; i < idx; ++i) { 1854 if (!vector_str_pop(output)) { 1855 free(name); 1856 return (0); 1857 } 1858 } 1859 1860 *str = name; 1861 1862 return (1); 1863 } 1864 1865 static int 1866 cpp_demangle_read_nested_name(struct cpp_demangle_data *ddata) 1867 { 1868 struct vector_str *output, v; 1869 size_t limit, p_idx, subst_str_len; 1870 int rtn; 1871 char *subst_str; 1872 1873 if (ddata == NULL || *ddata->cur != 'N') 1874 return (0); 1875 if (*(++ddata->cur) == '\0') 1876 return (0); 1877 1878 while (*ddata->cur == 'r' || *ddata->cur == 'V' || 1879 *ddata->cur == 'K') { 1880 switch (*ddata->cur) { 1881 case 'r': 1882 ddata->mem_rst = true; 1883 break; 1884 case 'V': 1885 ddata->mem_vat = true; 1886 break; 1887 case 'K': 1888 ddata->mem_cst = true; 1889 break; 1890 } 1891 ++ddata->cur; 1892 } 1893 1894 output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output; 1895 if (!vector_str_init(&v)) 1896 return (0); 1897 1898 rtn = 0; 1899 limit = 0; 1900 for (;;) { 1901 p_idx = output->size; 1902 switch (*ddata->cur) { 1903 case 'I': 1904 if (!cpp_demangle_read_tmpl_args(ddata)) 1905 goto clean; 1906 break; 1907 case 'S': 1908 if (!cpp_demangle_read_subst(ddata)) 1909 goto clean; 1910 break; 1911 case 'T': 1912 if (!cpp_demangle_read_tmpl_param(ddata)) 1913 goto clean; 1914 break; 1915 default: 1916 if (!cpp_demangle_read_uqname(ddata)) 1917 goto clean; 1918 } 1919 1920 if ((subst_str = vector_str_substr(output, p_idx, 1921 output->size - 1, &subst_str_len)) == NULL) 1922 goto clean; 1923 if (!vector_str_push(&v, subst_str, subst_str_len)) { 1924 free(subst_str); 1925 goto clean; 1926 } 1927 free(subst_str); 1928 1929 if (!cpp_demangle_push_subst_v(ddata, &v)) 1930 goto clean; 1931 if (*ddata->cur == 'E') 1932 break; 1933 else if (*ddata->cur != 'I' && 1934 *ddata->cur != 'C' && *ddata->cur != 'D') { 1935 if (!cpp_demangle_push_str(ddata, "::", 2)) 1936 goto clean; 1937 if (!vector_str_push(&v, "::", 2)) 1938 goto clean; 1939 } 1940 if (limit++ > CPP_DEMANGLE_TRY_LIMIT) 1941 goto clean; 1942 } 1943 1944 ++ddata->cur; 1945 rtn = 1; 1946 1947 clean: 1948 vector_str_dest(&v); 1949 1950 return (rtn); 1951 } 1952 1953 /* 1954 * read number 1955 * number ::= [n] <decimal> 1956 */ 1957 static int 1958 cpp_demangle_read_number(struct cpp_demangle_data *ddata, long *rtn) 1959 { 1960 long len, negative_factor; 1961 1962 if (ddata == NULL || rtn == NULL) 1963 return (0); 1964 1965 negative_factor = 1; 1966 if (*ddata->cur == 'n') { 1967 negative_factor = -1; 1968 1969 ++ddata->cur; 1970 } 1971 if (ELFTC_ISDIGIT(*ddata->cur) == 0) 1972 return (0); 1973 1974 errno = 0; 1975 if ((len = strtol(ddata->cur, (char **) NULL, 10)) == 0 && 1976 errno != 0) 1977 return (0); 1978 1979 while (ELFTC_ISDIGIT(*ddata->cur) != 0) 1980 ++ddata->cur; 1981 1982 assert(len >= 0); 1983 assert(negative_factor == 1 || negative_factor == -1); 1984 1985 *rtn = len * negative_factor; 1986 1987 return (1); 1988 } 1989 1990 static int 1991 cpp_demangle_read_number_as_string(struct cpp_demangle_data *ddata, char **str) 1992 { 1993 long n; 1994 1995 if (!cpp_demangle_read_number(ddata, &n)) { 1996 *str = NULL; 1997 return (0); 1998 } 1999 2000 if (asprintf(str, "%ld", n) < 0) { 2001 *str = NULL; 2002 return (0); 2003 } 2004 2005 return (1); 2006 } 2007 2008 static int 2009 cpp_demangle_read_nv_offset(struct cpp_demangle_data *ddata) 2010 { 2011 2012 if (ddata == NULL) 2013 return (0); 2014 2015 if (!cpp_demangle_push_str(ddata, "offset : ", 9)) 2016 return (0); 2017 2018 return (cpp_demangle_read_offset_number(ddata)); 2019 } 2020 2021 /* read offset, offset are nv-offset, v-offset */ 2022 static int 2023 cpp_demangle_read_offset(struct cpp_demangle_data *ddata) 2024 { 2025 2026 if (ddata == NULL) 2027 return (0); 2028 2029 if (*ddata->cur == 'h') { 2030 ++ddata->cur; 2031 return (cpp_demangle_read_nv_offset(ddata)); 2032 } else if (*ddata->cur == 'v') { 2033 ++ddata->cur; 2034 return (cpp_demangle_read_v_offset(ddata)); 2035 } 2036 2037 return (0); 2038 } 2039 2040 static int 2041 cpp_demangle_read_offset_number(struct cpp_demangle_data *ddata) 2042 { 2043 bool negative; 2044 const char *start; 2045 2046 if (ddata == NULL || *ddata->cur == '\0') 2047 return (0); 2048 2049 /* offset could be negative */ 2050 if (*ddata->cur == 'n') { 2051 negative = true; 2052 start = ddata->cur + 1; 2053 } else { 2054 negative = false; 2055 start = ddata->cur; 2056 } 2057 2058 while (*ddata->cur != '_') 2059 ++ddata->cur; 2060 2061 if (negative && !cpp_demangle_push_str(ddata, "-", 1)) 2062 return (0); 2063 2064 assert(start != NULL); 2065 2066 if (!cpp_demangle_push_str(ddata, start, ddata->cur - start)) 2067 return (0); 2068 if (!cpp_demangle_push_str(ddata, " ", 1)) 2069 return (0); 2070 2071 ++ddata->cur; 2072 2073 return (1); 2074 } 2075 2076 static int 2077 cpp_demangle_read_pointer_to_member(struct cpp_demangle_data *ddata) 2078 { 2079 size_t class_type_len, i, idx, p_idx; 2080 int p_func_type, rtn; 2081 char *class_type; 2082 2083 if (ddata == NULL || *ddata->cur != 'M' || *(++ddata->cur) == '\0') 2084 return (0); 2085 2086 p_idx = ddata->output.size; 2087 if (!cpp_demangle_read_type(ddata, 0)) 2088 return (0); 2089 2090 if ((class_type = vector_str_substr(&ddata->output, p_idx, 2091 ddata->output.size - 1, &class_type_len)) == NULL) 2092 return (0); 2093 2094 rtn = 0; 2095 idx = ddata->output.size; 2096 for (i = p_idx; i < idx; ++i) 2097 if (!vector_str_pop(&ddata->output)) 2098 goto clean1; 2099 2100 if (!vector_read_cmd_push(&ddata->cmd, READ_PTRMEM)) 2101 goto clean1; 2102 2103 if (!vector_str_push(&ddata->class_type, class_type, class_type_len)) 2104 goto clean2; 2105 2106 p_func_type = ddata->func_type; 2107 if (!cpp_demangle_read_type(ddata, 0)) 2108 goto clean3; 2109 2110 if (p_func_type == ddata->func_type) { 2111 if (!cpp_demangle_push_str(ddata, " ", 1)) 2112 goto clean3; 2113 if (!cpp_demangle_push_str(ddata, class_type, class_type_len)) 2114 goto clean3; 2115 if (!cpp_demangle_push_str(ddata, "::*", 3)) 2116 goto clean3; 2117 } 2118 2119 rtn = 1; 2120 clean3: 2121 if (!vector_str_pop(&ddata->class_type)) 2122 rtn = 0; 2123 clean2: 2124 if (!vector_read_cmd_pop(&ddata->cmd)) 2125 rtn = 0; 2126 clean1: 2127 free(class_type); 2128 2129 return (rtn); 2130 } 2131 2132 /* read source-name, source-name is <len> <ID> */ 2133 static int 2134 cpp_demangle_read_sname(struct cpp_demangle_data *ddata) 2135 { 2136 long len; 2137 int err; 2138 2139 if (ddata == NULL || cpp_demangle_read_number(ddata, &len) == 0 || 2140 len <= 0) 2141 return (0); 2142 2143 if (len == 12 && (memcmp("_GLOBAL__N_1", ddata->cur, 12) == 0)) 2144 err = cpp_demangle_push_str(ddata, "(anonymous namespace)", 21); 2145 else 2146 err = cpp_demangle_push_str(ddata, ddata->cur, len); 2147 2148 if (err == 0) 2149 return (0); 2150 2151 assert(ddata->output.size > 0); 2152 if (vector_read_cmd_find(&ddata->cmd, READ_TMPL) == 0) 2153 ddata->last_sname = 2154 ddata->output.container[ddata->output.size - 1]; 2155 2156 ddata->cur += len; 2157 2158 return (1); 2159 } 2160 2161 static int 2162 cpp_demangle_read_subst(struct cpp_demangle_data *ddata) 2163 { 2164 long nth; 2165 2166 if (ddata == NULL || *ddata->cur == '\0') 2167 return (0); 2168 2169 /* abbreviations of the form Sx */ 2170 switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) { 2171 case SIMPLE_HASH('S', 'a'): 2172 /* std::allocator */ 2173 if (cpp_demangle_push_str(ddata, "std::allocator", 14) == 0) 2174 return (0); 2175 ddata->cur += 2; 2176 if (*ddata->cur == 'I') 2177 return (cpp_demangle_read_subst_stdtmpl(ddata, 2178 "std::allocator", 14)); 2179 return (1); 2180 2181 case SIMPLE_HASH('S', 'b'): 2182 /* std::basic_string */ 2183 if (!cpp_demangle_push_str(ddata, "std::basic_string", 17)) 2184 return (0); 2185 ddata->cur += 2; 2186 if (*ddata->cur == 'I') 2187 return (cpp_demangle_read_subst_stdtmpl(ddata, 2188 "std::basic_string", 17)); 2189 return (1); 2190 2191 case SIMPLE_HASH('S', 'd'): 2192 /* std::basic_iostream<char, std::char_traits<char> > */ 2193 if (!cpp_demangle_push_str(ddata, "std::basic_iostream", 19)) 2194 return (0); 2195 ddata->last_sname = "basic_iostream"; 2196 ddata->cur += 2; 2197 if (*ddata->cur == 'I') 2198 return (cpp_demangle_read_subst_stdtmpl(ddata, 2199 "std::basic_iostream", 19)); 2200 return (1); 2201 2202 case SIMPLE_HASH('S', 'i'): 2203 /* std::basic_istream<char, std::char_traits<char> > */ 2204 if (!cpp_demangle_push_str(ddata, "std::basic_istream", 18)) 2205 return (0); 2206 ddata->last_sname = "basic_istream"; 2207 ddata->cur += 2; 2208 if (*ddata->cur == 'I') 2209 return (cpp_demangle_read_subst_stdtmpl(ddata, 2210 "std::basic_istream", 18)); 2211 return (1); 2212 2213 case SIMPLE_HASH('S', 'o'): 2214 /* std::basic_ostream<char, std::char_traits<char> > */ 2215 if (!cpp_demangle_push_str(ddata, "std::basic_ostream", 18)) 2216 return (0); 2217 ddata->last_sname = "basic_ostream"; 2218 ddata->cur += 2; 2219 if (*ddata->cur == 'I') 2220 return (cpp_demangle_read_subst_stdtmpl(ddata, 2221 "std::basic_ostream", 18)); 2222 return (1); 2223 2224 case SIMPLE_HASH('S', 's'): 2225 /* 2226 * std::basic_string<char, std::char_traits<char>, 2227 * std::allocator<char> > 2228 * 2229 * a.k.a std::string 2230 */ 2231 if (!cpp_demangle_push_str(ddata, "std::string", 11)) 2232 return (0); 2233 ddata->last_sname = "string"; 2234 ddata->cur += 2; 2235 if (*ddata->cur == 'I') 2236 return (cpp_demangle_read_subst_stdtmpl(ddata, 2237 "std::string", 11)); 2238 return (1); 2239 2240 case SIMPLE_HASH('S', 't'): 2241 /* std:: */ 2242 return (cpp_demangle_read_subst_std(ddata)); 2243 } 2244 2245 if (*(++ddata->cur) == '\0') 2246 return (0); 2247 2248 /* substitution */ 2249 if (*ddata->cur == '_') 2250 return (cpp_demangle_get_subst(ddata, 0)); 2251 else { 2252 errno = 0; 2253 /* substitution number is base 36 */ 2254 if ((nth = strtol(ddata->cur, (char **) NULL, 36)) == 0 && 2255 errno != 0) 2256 return (0); 2257 2258 /* first was '_', so increase one */ 2259 ++nth; 2260 2261 while (*ddata->cur != '_') 2262 ++ddata->cur; 2263 2264 assert(nth > 0); 2265 2266 return (cpp_demangle_get_subst(ddata, nth)); 2267 } 2268 2269 /* NOTREACHED */ 2270 return (0); 2271 } 2272 2273 static int 2274 cpp_demangle_read_subst_std(struct cpp_demangle_data *ddata) 2275 { 2276 struct vector_str *output, v; 2277 size_t p_idx, subst_str_len; 2278 int rtn; 2279 char *subst_str; 2280 2281 if (ddata == NULL) 2282 return (0); 2283 2284 if (!vector_str_init(&v)) 2285 return (0); 2286 2287 subst_str = NULL; 2288 rtn = 0; 2289 if (!cpp_demangle_push_str(ddata, "std::", 5)) 2290 goto clean; 2291 2292 if (!vector_str_push(&v, "std::", 5)) 2293 goto clean; 2294 2295 ddata->cur += 2; 2296 2297 output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output; 2298 2299 p_idx = output->size; 2300 if (!cpp_demangle_read_uqname(ddata)) 2301 goto clean; 2302 2303 if ((subst_str = vector_str_substr(output, p_idx, output->size - 1, 2304 &subst_str_len)) == NULL) 2305 goto clean; 2306 2307 if (!vector_str_push(&v, subst_str, subst_str_len)) 2308 goto clean; 2309 2310 if (!cpp_demangle_push_subst_v(ddata, &v)) 2311 goto clean; 2312 2313 if (*ddata->cur == 'I') { 2314 p_idx = output->size; 2315 if (!cpp_demangle_read_tmpl_args(ddata)) 2316 goto clean; 2317 free(subst_str); 2318 if ((subst_str = vector_str_substr(output, p_idx, 2319 output->size - 1, &subst_str_len)) == NULL) 2320 goto clean; 2321 if (!vector_str_push(&v, subst_str, subst_str_len)) 2322 goto clean; 2323 if (!cpp_demangle_push_subst_v(ddata, &v)) 2324 goto clean; 2325 } 2326 2327 rtn = 1; 2328 clean: 2329 free(subst_str); 2330 vector_str_dest(&v); 2331 2332 return (rtn); 2333 } 2334 2335 static int 2336 cpp_demangle_read_subst_stdtmpl(struct cpp_demangle_data *ddata, 2337 const char *str, size_t len) 2338 { 2339 struct vector_str *output; 2340 size_t p_idx, substr_len; 2341 int rtn; 2342 char *subst_str, *substr; 2343 2344 if (ddata == NULL || str == NULL || len == 0) 2345 return (0); 2346 2347 output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output; 2348 2349 p_idx = output->size; 2350 substr = NULL; 2351 subst_str = NULL; 2352 2353 if (!cpp_demangle_read_tmpl_args(ddata)) 2354 return (0); 2355 if ((substr = vector_str_substr(output, p_idx, output->size - 1, 2356 &substr_len)) == NULL) 2357 return (0); 2358 2359 rtn = 0; 2360 if ((subst_str = malloc(sizeof(char) * (substr_len + len + 1))) == 2361 NULL) 2362 goto clean; 2363 2364 memcpy(subst_str, str, len); 2365 memcpy(subst_str + len, substr, substr_len); 2366 subst_str[substr_len + len] = '\0'; 2367 2368 if (!cpp_demangle_push_subst(ddata, subst_str, substr_len + len)) 2369 goto clean; 2370 2371 rtn = 1; 2372 clean: 2373 free(subst_str); 2374 free(substr); 2375 2376 return (rtn); 2377 } 2378 2379 static int 2380 cpp_demangle_read_tmpl_arg(struct cpp_demangle_data *ddata) 2381 { 2382 2383 if (ddata == NULL || *ddata->cur == '\0') 2384 return (0); 2385 2386 switch (*ddata->cur) { 2387 case 'L': 2388 return (cpp_demangle_read_expr_primary(ddata)); 2389 case 'X': 2390 return (cpp_demangle_read_expression(ddata)); 2391 } 2392 2393 return (cpp_demangle_read_type(ddata, 0)); 2394 } 2395 2396 static int 2397 cpp_demangle_read_tmpl_args(struct cpp_demangle_data *ddata) 2398 { 2399 struct vector_str *v; 2400 size_t arg_len, idx, limit, size; 2401 char *arg; 2402 2403 if (ddata == NULL || *ddata->cur == '\0') 2404 return (0); 2405 2406 ++ddata->cur; 2407 2408 if (!vector_read_cmd_push(&ddata->cmd, READ_TMPL)) 2409 return (0); 2410 2411 if (!cpp_demangle_push_str(ddata, "<", 1)) 2412 return (0); 2413 2414 limit = 0; 2415 v = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output; 2416 for (;;) { 2417 idx = v->size; 2418 if (!cpp_demangle_read_tmpl_arg(ddata)) 2419 return (0); 2420 if ((arg = vector_str_substr(v, idx, v->size - 1, &arg_len)) == 2421 NULL) 2422 return (0); 2423 if (!vector_str_find(&ddata->tmpl, arg, arg_len) && 2424 !vector_str_push(&ddata->tmpl, arg, arg_len)) { 2425 free(arg); 2426 return (0); 2427 } 2428 2429 free(arg); 2430 2431 if (*ddata->cur == 'E') { 2432 ++ddata->cur; 2433 size = v->size; 2434 assert(size > 0); 2435 if (!strncmp(v->container[size - 1], ">", 1)) { 2436 if (!cpp_demangle_push_str(ddata, " >", 2)) 2437 return (0); 2438 } else if (!cpp_demangle_push_str(ddata, ">", 1)) 2439 return (0); 2440 break; 2441 } else if (*ddata->cur != 'I' && 2442 !cpp_demangle_push_str(ddata, ", ", 2)) 2443 return (0); 2444 2445 if (limit++ > CPP_DEMANGLE_TRY_LIMIT) 2446 return (0); 2447 } 2448 2449 return (vector_read_cmd_pop(&ddata->cmd)); 2450 } 2451 2452 /* 2453 * Read template parameter that forms in 'T[number]_'. 2454 * This function much like to read_subst but only for types. 2455 */ 2456 static int 2457 cpp_demangle_read_tmpl_param(struct cpp_demangle_data *ddata) 2458 { 2459 long nth; 2460 2461 if (ddata == NULL || *ddata->cur != 'T') 2462 return (0); 2463 2464 ++ddata->cur; 2465 2466 if (*ddata->cur == '_') 2467 return (cpp_demangle_get_tmpl_param(ddata, 0)); 2468 else { 2469 2470 errno = 0; 2471 if ((nth = strtol(ddata->cur, (char **) NULL, 36)) == 0 && 2472 errno != 0) 2473 return (0); 2474 2475 /* T_ is first */ 2476 ++nth; 2477 2478 while (*ddata->cur != '_') 2479 ++ddata->cur; 2480 2481 assert(nth > 0); 2482 2483 return (cpp_demangle_get_tmpl_param(ddata, nth)); 2484 } 2485 2486 /* NOTREACHED */ 2487 return (0); 2488 } 2489 2490 static int 2491 cpp_demangle_read_type(struct cpp_demangle_data *ddata, int delimit) 2492 { 2493 struct vector_type_qualifier v; 2494 struct vector_str *output; 2495 size_t p_idx, type_str_len; 2496 int extern_c, is_builtin; 2497 long len; 2498 char *type_str, *exp_str, *num_str; 2499 2500 if (ddata == NULL) 2501 return (0); 2502 2503 output = &ddata->output; 2504 if (ddata->output.size > 0 && !strncmp(ddata->output.container[ddata->output.size - 1], ">", 1)) { 2505 ddata->push_head++; 2506 output = &ddata->output_tmp; 2507 } else if (delimit == 1) { 2508 if (ddata->paren == false) { 2509 if (!cpp_demangle_push_str(ddata, "(", 1)) 2510 return (0); 2511 if (ddata->output.size < 2) 2512 return (0); 2513 ddata->paren = true; 2514 ddata->pfirst = true; 2515 /* Need pop function name */ 2516 if (ddata->subst.size == 1 && 2517 !vector_str_pop(&ddata->subst)) 2518 return (0); 2519 } 2520 2521 if (ddata->pfirst) 2522 ddata->pfirst = false; 2523 else if (*ddata->cur != 'I' && 2524 !cpp_demangle_push_str(ddata, ", ", 2)) 2525 return (0); 2526 } 2527 2528 assert(output != NULL); 2529 /* 2530 * [r, V, K] [P, R, C, G, U] builtin, function, class-enum, array 2531 * pointer-to-member, template-param, template-template-param, subst 2532 */ 2533 2534 if (!vector_type_qualifier_init(&v)) 2535 return (0); 2536 2537 extern_c = 0; 2538 is_builtin = 1; 2539 p_idx = output->size; 2540 type_str = exp_str = num_str = NULL; 2541 again: 2542 /* builtin type */ 2543 switch (*ddata->cur) { 2544 case 'a': 2545 /* signed char */ 2546 if (!cpp_demangle_push_str(ddata, "signed char", 11)) 2547 goto clean; 2548 ++ddata->cur; 2549 goto rtn; 2550 2551 case 'A': 2552 /* array type */ 2553 if (!cpp_demangle_read_array(ddata)) 2554 goto clean; 2555 is_builtin = 0; 2556 goto rtn; 2557 2558 case 'b': 2559 /* bool */ 2560 if (!cpp_demangle_push_str(ddata, "bool", 4)) 2561 goto clean; 2562 ++ddata->cur; 2563 goto rtn; 2564 2565 case 'C': 2566 /* complex pair */ 2567 if (!vector_type_qualifier_push(&v, TYPE_CMX)) 2568 goto clean; 2569 ++ddata->cur; 2570 goto again; 2571 2572 case 'c': 2573 /* char */ 2574 if (!cpp_demangle_push_str(ddata, "char", 4)) 2575 goto clean; 2576 ++ddata->cur; 2577 goto rtn; 2578 2579 case 'd': 2580 /* double */ 2581 if (!cpp_demangle_push_str(ddata, "double", 6)) 2582 goto clean; 2583 ++ddata->cur; 2584 goto rtn; 2585 2586 case 'D': 2587 ++ddata->cur; 2588 switch (*ddata->cur) { 2589 case 'd': 2590 /* IEEE 754r decimal floating point (64 bits) */ 2591 if (!cpp_demangle_push_str(ddata, "decimal64", 9)) 2592 goto clean; 2593 ++ddata->cur; 2594 break; 2595 case 'e': 2596 /* IEEE 754r decimal floating point (128 bits) */ 2597 if (!cpp_demangle_push_str(ddata, "decimal128", 10)) 2598 goto clean; 2599 ++ddata->cur; 2600 break; 2601 case 'f': 2602 /* IEEE 754r decimal floating point (32 bits) */ 2603 if (!cpp_demangle_push_str(ddata, "decimal32", 9)) 2604 goto clean; 2605 ++ddata->cur; 2606 break; 2607 case 'h': 2608 /* IEEE 754r half-precision floating point (16 bits) */ 2609 if (!cpp_demangle_push_str(ddata, "half", 4)) 2610 goto clean; 2611 ++ddata->cur; 2612 break; 2613 case 'i': 2614 /* char32_t */ 2615 if (!cpp_demangle_push_str(ddata, "char32_t", 8)) 2616 goto clean; 2617 ++ddata->cur; 2618 break; 2619 case 'n': 2620 /* std::nullptr_t (i.e., decltype(nullptr)) */ 2621 if (!cpp_demangle_push_str(ddata, "decltype(nullptr)", 2622 17)) 2623 goto clean; 2624 ++ddata->cur; 2625 break; 2626 case 's': 2627 /* char16_t */ 2628 if (!cpp_demangle_push_str(ddata, "char16_t", 8)) 2629 goto clean; 2630 ++ddata->cur; 2631 break; 2632 case 'v': 2633 /* gcc vector_size extension. */ 2634 ++ddata->cur; 2635 if (*ddata->cur == '_') { 2636 ++ddata->cur; 2637 if (!cpp_demangle_read_expression_flat(ddata, 2638 &exp_str)) 2639 goto clean; 2640 if (!vector_str_push(&v.ext_name, exp_str, 2641 strlen(exp_str))) 2642 goto clean; 2643 } else { 2644 if (!cpp_demangle_read_number_as_string(ddata, 2645 &num_str)) 2646 goto clean; 2647 if (!vector_str_push(&v.ext_name, num_str, 2648 strlen(num_str))) 2649 goto clean; 2650 } 2651 if (*ddata->cur != '_') 2652 goto clean; 2653 ++ddata->cur; 2654 if (!vector_type_qualifier_push(&v, TYPE_VEC)) 2655 goto clean; 2656 goto again; 2657 default: 2658 goto clean; 2659 } 2660 goto rtn; 2661 2662 case 'e': 2663 /* long double */ 2664 if (!cpp_demangle_push_str(ddata, "long double", 11)) 2665 goto clean; 2666 ++ddata->cur; 2667 goto rtn; 2668 2669 case 'f': 2670 /* float */ 2671 if (!cpp_demangle_push_str(ddata, "float", 5)) 2672 goto clean; 2673 ++ddata->cur; 2674 goto rtn; 2675 2676 case 'F': 2677 /* function */ 2678 if (!cpp_demangle_read_function(ddata, &extern_c, &v)) 2679 goto clean; 2680 is_builtin = 0; 2681 goto rtn; 2682 2683 case 'g': 2684 /* __float128 */ 2685 if (!cpp_demangle_push_str(ddata, "__float128", 10)) 2686 goto clean; 2687 ++ddata->cur; 2688 goto rtn; 2689 2690 case 'G': 2691 /* imaginary */ 2692 if (!vector_type_qualifier_push(&v, TYPE_IMG)) 2693 goto clean; 2694 ++ddata->cur; 2695 goto again; 2696 2697 case 'h': 2698 /* unsigned char */ 2699 if (!cpp_demangle_push_str(ddata, "unsigned char", 13)) 2700 goto clean; 2701 ++ddata->cur; 2702 goto rtn; 2703 2704 case 'i': 2705 /* int */ 2706 if (!cpp_demangle_push_str(ddata, "int", 3)) 2707 goto clean; 2708 ++ddata->cur; 2709 goto rtn; 2710 2711 case 'j': 2712 /* unsigned int */ 2713 if (!cpp_demangle_push_str(ddata, "unsigned int", 12)) 2714 goto clean; 2715 ++ddata->cur; 2716 goto rtn; 2717 2718 case 'K': 2719 /* const */ 2720 if (!vector_type_qualifier_push(&v, TYPE_CST)) 2721 goto clean; 2722 ++ddata->cur; 2723 goto again; 2724 2725 case 'l': 2726 /* long */ 2727 if (!cpp_demangle_push_str(ddata, "long", 4)) 2728 goto clean; 2729 ++ddata->cur; 2730 goto rtn; 2731 2732 case 'm': 2733 /* unsigned long */ 2734 if (!cpp_demangle_push_str(ddata, "unsigned long", 13)) 2735 goto clean; 2736 2737 ++ddata->cur; 2738 2739 goto rtn; 2740 case 'M': 2741 /* pointer to member */ 2742 if (!cpp_demangle_read_pointer_to_member(ddata)) 2743 goto clean; 2744 is_builtin = 0; 2745 goto rtn; 2746 2747 case 'n': 2748 /* __int128 */ 2749 if (!cpp_demangle_push_str(ddata, "__int128", 8)) 2750 goto clean; 2751 ++ddata->cur; 2752 goto rtn; 2753 2754 case 'o': 2755 /* unsigned __int128 */ 2756 if (!cpp_demangle_push_str(ddata, "unsigned __int128", 17)) 2757 goto clean; 2758 ++ddata->cur; 2759 goto rtn; 2760 2761 case 'P': 2762 /* pointer */ 2763 if (!vector_type_qualifier_push(&v, TYPE_PTR)) 2764 goto clean; 2765 ++ddata->cur; 2766 goto again; 2767 2768 case 'r': 2769 /* restrict */ 2770 if (!vector_type_qualifier_push(&v, TYPE_RST)) 2771 goto clean; 2772 ++ddata->cur; 2773 goto again; 2774 2775 case 'R': 2776 /* reference */ 2777 if (!vector_type_qualifier_push(&v, TYPE_REF)) 2778 goto clean; 2779 ++ddata->cur; 2780 goto again; 2781 2782 case 's': 2783 /* short, local string */ 2784 if (!cpp_demangle_push_str(ddata, "short", 5)) 2785 goto clean; 2786 ++ddata->cur; 2787 goto rtn; 2788 2789 case 'S': 2790 /* substitution */ 2791 if (!cpp_demangle_read_subst(ddata)) 2792 goto clean; 2793 is_builtin = 0; 2794 goto rtn; 2795 2796 case 't': 2797 /* unsigned short */ 2798 if (!cpp_demangle_push_str(ddata, "unsigned short", 14)) 2799 goto clean; 2800 ++ddata->cur; 2801 goto rtn; 2802 2803 case 'T': 2804 /* template parameter */ 2805 if (!cpp_demangle_read_tmpl_param(ddata)) 2806 goto clean; 2807 is_builtin = 0; 2808 goto rtn; 2809 2810 case 'u': 2811 /* vendor extended builtin */ 2812 ++ddata->cur; 2813 if (!cpp_demangle_read_sname(ddata)) 2814 goto clean; 2815 is_builtin = 0; 2816 goto rtn; 2817 2818 case 'U': 2819 /* vendor extended type qualifier */ 2820 if (!cpp_demangle_read_number(ddata, &len)) 2821 goto clean; 2822 if (len <= 0) 2823 goto clean; 2824 if (!vector_str_push(&v.ext_name, ddata->cur, len)) 2825 return (0); 2826 ddata->cur += len; 2827 if (!vector_type_qualifier_push(&v, TYPE_EXT)) 2828 goto clean; 2829 goto again; 2830 2831 case 'v': 2832 /* void */ 2833 if (!cpp_demangle_push_str(ddata, "void", 4)) 2834 goto clean; 2835 ++ddata->cur; 2836 goto rtn; 2837 2838 case 'V': 2839 /* volatile */ 2840 if (!vector_type_qualifier_push(&v, TYPE_VAT)) 2841 goto clean; 2842 ++ddata->cur; 2843 goto again; 2844 2845 case 'w': 2846 /* wchar_t */ 2847 if (!cpp_demangle_push_str(ddata, "wchar_t", 7)) 2848 goto clean; 2849 ++ddata->cur; 2850 goto rtn; 2851 2852 case 'x': 2853 /* long long */ 2854 if (!cpp_demangle_push_str(ddata, "long long", 9)) 2855 goto clean; 2856 ++ddata->cur; 2857 goto rtn; 2858 2859 case 'y': 2860 /* unsigned long long */ 2861 if (!cpp_demangle_push_str(ddata, "unsigned long long", 18)) 2862 goto clean; 2863 ++ddata->cur; 2864 goto rtn; 2865 2866 case 'z': 2867 /* ellipsis */ 2868 if (!cpp_demangle_push_str(ddata, "...", 3)) 2869 goto clean; 2870 ++ddata->cur; 2871 goto rtn; 2872 } 2873 2874 if (!cpp_demangle_read_name(ddata)) 2875 goto clean; 2876 2877 is_builtin = 0; 2878 rtn: 2879 if ((type_str = vector_str_substr(output, p_idx, output->size - 1, 2880 &type_str_len)) == NULL) 2881 goto clean; 2882 2883 if (is_builtin == 0) { 2884 if (!vector_str_find(&ddata->subst, type_str, type_str_len) && 2885 !vector_str_push(&ddata->subst, type_str, type_str_len)) 2886 goto clean; 2887 } 2888 2889 if (!cpp_demangle_push_type_qualifier(ddata, &v, type_str)) 2890 goto clean; 2891 2892 free(type_str); 2893 free(exp_str); 2894 free(num_str); 2895 vector_type_qualifier_dest(&v); 2896 2897 if (ddata->push_head > 0) { 2898 if (*ddata->cur == 'I' && cpp_demangle_read_tmpl_args(ddata) 2899 == 0) 2900 return (0); 2901 2902 if (--ddata->push_head > 0) 2903 return (1); 2904 2905 if (!vector_str_push(&ddata->output_tmp, " ", 1)) 2906 return (0); 2907 2908 if (!vector_str_push_vector_head(&ddata->output, 2909 &ddata->output_tmp)) 2910 return (0); 2911 2912 vector_str_dest(&ddata->output_tmp); 2913 if (!vector_str_init(&ddata->output_tmp)) 2914 return (0); 2915 2916 if (!cpp_demangle_push_str(ddata, "(", 1)) 2917 return (0); 2918 2919 ddata->paren = true; 2920 ddata->pfirst = true; 2921 } 2922 2923 return (1); 2924 clean: 2925 free(type_str); 2926 free(exp_str); 2927 free(num_str); 2928 vector_type_qualifier_dest(&v); 2929 2930 return (0); 2931 } 2932 2933 static int 2934 cpp_demangle_read_type_flat(struct cpp_demangle_data *ddata, char **str) 2935 { 2936 struct vector_str *output; 2937 size_t i, p_idx, idx, type_len; 2938 char *type; 2939 2940 output = ddata->push_head > 0 ? &ddata->output_tmp : 2941 &ddata->output; 2942 2943 p_idx = output->size; 2944 2945 if (!cpp_demangle_read_type(ddata, 0)) 2946 return (0); 2947 2948 if ((type = vector_str_substr(output, p_idx, output->size - 1, 2949 &type_len)) == NULL) 2950 return (0); 2951 2952 idx = output->size; 2953 for (i = p_idx; i < idx; ++i) { 2954 if (!vector_str_pop(output)) { 2955 free(type); 2956 return (0); 2957 } 2958 } 2959 2960 *str = type; 2961 2962 return (1); 2963 } 2964 2965 /* 2966 * read unqualified-name, unqualified name are operator-name, ctor-dtor-name, 2967 * source-name 2968 */ 2969 static int 2970 cpp_demangle_read_uqname(struct cpp_demangle_data *ddata) 2971 { 2972 size_t len; 2973 2974 if (ddata == NULL || *ddata->cur == '\0') 2975 return (0); 2976 2977 /* operator name */ 2978 switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) { 2979 case SIMPLE_HASH('a', 'a'): 2980 /* operator && */ 2981 if (!cpp_demangle_push_str(ddata, "operator&&", 10)) 2982 return (0); 2983 ddata->cur += 2; 2984 return (1); 2985 2986 case SIMPLE_HASH('a', 'd'): 2987 /* operator & (unary) */ 2988 if (!cpp_demangle_push_str(ddata, "operator&", 9)) 2989 return (0); 2990 ddata->cur += 2; 2991 return (1); 2992 2993 case SIMPLE_HASH('a', 'n'): 2994 /* operator & */ 2995 if (!cpp_demangle_push_str(ddata, "operator&", 9)) 2996 return (0); 2997 ddata->cur += 2; 2998 return (1); 2999 3000 case SIMPLE_HASH('a', 'N'): 3001 /* operator &= */ 3002 if (!cpp_demangle_push_str(ddata, "operator&=", 10)) 3003 return (0); 3004 ddata->cur += 2; 3005 return (1); 3006 3007 case SIMPLE_HASH('a', 'S'): 3008 /* operator = */ 3009 if (!cpp_demangle_push_str(ddata, "operator=", 9)) 3010 return (0); 3011 ddata->cur += 2; 3012 return (1); 3013 3014 case SIMPLE_HASH('c', 'l'): 3015 /* operator () */ 3016 if (!cpp_demangle_push_str(ddata, "operator()", 10)) 3017 return (0); 3018 ddata->cur += 2; 3019 return (1); 3020 3021 case SIMPLE_HASH('c', 'm'): 3022 /* operator , */ 3023 if (!cpp_demangle_push_str(ddata, "operator,", 9)) 3024 return (0); 3025 ddata->cur += 2; 3026 return (1); 3027 3028 case SIMPLE_HASH('c', 'o'): 3029 /* operator ~ */ 3030 if (!cpp_demangle_push_str(ddata, "operator~", 9)) 3031 return (0); 3032 ddata->cur += 2; 3033 return (1); 3034 3035 case SIMPLE_HASH('c', 'v'): 3036 /* operator (cast) */ 3037 if (!cpp_demangle_push_str(ddata, "operator(cast)", 14)) 3038 return (0); 3039 ddata->cur += 2; 3040 return (cpp_demangle_read_type(ddata, 1)); 3041 3042 case SIMPLE_HASH('d', 'a'): 3043 /* operator delete [] */ 3044 if (!cpp_demangle_push_str(ddata, "operator delete []", 18)) 3045 return (0); 3046 ddata->cur += 2; 3047 return (1); 3048 3049 case SIMPLE_HASH('d', 'e'): 3050 /* operator * (unary) */ 3051 if (!cpp_demangle_push_str(ddata, "operator*", 9)) 3052 return (0); 3053 ddata->cur += 2; 3054 return (1); 3055 3056 case SIMPLE_HASH('d', 'l'): 3057 /* operator delete */ 3058 if (!cpp_demangle_push_str(ddata, "operator delete", 15)) 3059 return (0); 3060 ddata->cur += 2; 3061 return (1); 3062 3063 case SIMPLE_HASH('d', 'v'): 3064 /* operator / */ 3065 if (!cpp_demangle_push_str(ddata, "operator/", 9)) 3066 return (0); 3067 ddata->cur += 2; 3068 return (1); 3069 3070 case SIMPLE_HASH('d', 'V'): 3071 /* operator /= */ 3072 if (!cpp_demangle_push_str(ddata, "operator/=", 10)) 3073 return (0); 3074 ddata->cur += 2; 3075 return (1); 3076 3077 case SIMPLE_HASH('e', 'o'): 3078 /* operator ^ */ 3079 if (!cpp_demangle_push_str(ddata, "operator^", 9)) 3080 return (0); 3081 ddata->cur += 2; 3082 return (1); 3083 3084 case SIMPLE_HASH('e', 'O'): 3085 /* operator ^= */ 3086 if (!cpp_demangle_push_str(ddata, "operator^=", 10)) 3087 return (0); 3088 ddata->cur += 2; 3089 return (1); 3090 3091 case SIMPLE_HASH('e', 'q'): 3092 /* operator == */ 3093 if (!cpp_demangle_push_str(ddata, "operator==", 10)) 3094 return (0); 3095 ddata->cur += 2; 3096 return (1); 3097 3098 case SIMPLE_HASH('g', 'e'): 3099 /* operator >= */ 3100 if (!cpp_demangle_push_str(ddata, "operator>=", 10)) 3101 return (0); 3102 ddata->cur += 2; 3103 return (1); 3104 3105 case SIMPLE_HASH('g', 't'): 3106 /* operator > */ 3107 if (!cpp_demangle_push_str(ddata, "operator>", 9)) 3108 return (0); 3109 ddata->cur += 2; 3110 return (1); 3111 3112 case SIMPLE_HASH('i', 'x'): 3113 /* operator [] */ 3114 if (!cpp_demangle_push_str(ddata, "operator[]", 10)) 3115 return (0); 3116 ddata->cur += 2; 3117 return (1); 3118 3119 case SIMPLE_HASH('l', 'e'): 3120 /* operator <= */ 3121 if (!cpp_demangle_push_str(ddata, "operator<=", 10)) 3122 return (0); 3123 ddata->cur += 2; 3124 return (1); 3125 3126 case SIMPLE_HASH('l', 's'): 3127 /* operator << */ 3128 if (!cpp_demangle_push_str(ddata, "operator<<", 10)) 3129 return (0); 3130 ddata->cur += 2; 3131 return (1); 3132 3133 case SIMPLE_HASH('l', 'S'): 3134 /* operator <<= */ 3135 if (!cpp_demangle_push_str(ddata, "operator<<=", 11)) 3136 return (0); 3137 ddata->cur += 2; 3138 return (1); 3139 3140 case SIMPLE_HASH('l', 't'): 3141 /* operator < */ 3142 if (!cpp_demangle_push_str(ddata, "operator<", 9)) 3143 return (0); 3144 ddata->cur += 2; 3145 return (1); 3146 3147 case SIMPLE_HASH('m', 'i'): 3148 /* operator - */ 3149 if (!cpp_demangle_push_str(ddata, "operator-", 9)) 3150 return (0); 3151 ddata->cur += 2; 3152 return (1); 3153 3154 case SIMPLE_HASH('m', 'I'): 3155 /* operator -= */ 3156 if (!cpp_demangle_push_str(ddata, "operator-=", 10)) 3157 return (0); 3158 ddata->cur += 2; 3159 return (1); 3160 3161 case SIMPLE_HASH('m', 'l'): 3162 /* operator * */ 3163 if (!cpp_demangle_push_str(ddata, "operator*", 9)) 3164 return (0); 3165 ddata->cur += 2; 3166 return (1); 3167 3168 case SIMPLE_HASH('m', 'L'): 3169 /* operator *= */ 3170 if (!cpp_demangle_push_str(ddata, "operator*=", 10)) 3171 return (0); 3172 ddata->cur += 2; 3173 return (1); 3174 3175 case SIMPLE_HASH('m', 'm'): 3176 /* operator -- */ 3177 if (!cpp_demangle_push_str(ddata, "operator--", 10)) 3178 return (0); 3179 ddata->cur += 2; 3180 return (1); 3181 3182 case SIMPLE_HASH('n', 'a'): 3183 /* operator new[] */ 3184 if (!cpp_demangle_push_str(ddata, "operator new []", 15)) 3185 return (0); 3186 ddata->cur += 2; 3187 return (1); 3188 3189 case SIMPLE_HASH('n', 'e'): 3190 /* operator != */ 3191 if (!cpp_demangle_push_str(ddata, "operator!=", 10)) 3192 return (0); 3193 ddata->cur += 2; 3194 return (1); 3195 3196 case SIMPLE_HASH('n', 'g'): 3197 /* operator - (unary) */ 3198 if (!cpp_demangle_push_str(ddata, "operator-", 9)) 3199 return (0); 3200 ddata->cur += 2; 3201 return (1); 3202 3203 case SIMPLE_HASH('n', 't'): 3204 /* operator ! */ 3205 if (!cpp_demangle_push_str(ddata, "operator!", 9)) 3206 return (0); 3207 ddata->cur += 2; 3208 return (1); 3209 3210 case SIMPLE_HASH('n', 'w'): 3211 /* operator new */ 3212 if (!cpp_demangle_push_str(ddata, "operator new", 12)) 3213 return (0); 3214 ddata->cur += 2; 3215 return (1); 3216 3217 case SIMPLE_HASH('o', 'o'): 3218 /* operator || */ 3219 if (!cpp_demangle_push_str(ddata, "operator||", 10)) 3220 return (0); 3221 ddata->cur += 2; 3222 return (1); 3223 3224 case SIMPLE_HASH('o', 'r'): 3225 /* operator | */ 3226 if (!cpp_demangle_push_str(ddata, "operator|", 9)) 3227 return (0); 3228 ddata->cur += 2; 3229 return (1); 3230 3231 case SIMPLE_HASH('o', 'R'): 3232 /* operator |= */ 3233 if (!cpp_demangle_push_str(ddata, "operator|=", 10)) 3234 return (0); 3235 ddata->cur += 2; 3236 return (1); 3237 3238 case SIMPLE_HASH('p', 'l'): 3239 /* operator + */ 3240 if (!cpp_demangle_push_str(ddata, "operator+", 9)) 3241 return (0); 3242 ddata->cur += 2; 3243 return (1); 3244 3245 case SIMPLE_HASH('p', 'L'): 3246 /* operator += */ 3247 if (!cpp_demangle_push_str(ddata, "operator+=", 10)) 3248 return (0); 3249 ddata->cur += 2; 3250 return (1); 3251 3252 case SIMPLE_HASH('p', 'm'): 3253 /* operator ->* */ 3254 if (!cpp_demangle_push_str(ddata, "operator->*", 11)) 3255 return (0); 3256 ddata->cur += 2; 3257 return (1); 3258 3259 case SIMPLE_HASH('p', 'p'): 3260 /* operator ++ */ 3261 if (!cpp_demangle_push_str(ddata, "operator++", 10)) 3262 return (0); 3263 ddata->cur += 2; 3264 return (1); 3265 3266 case SIMPLE_HASH('p', 's'): 3267 /* operator + (unary) */ 3268 if (!cpp_demangle_push_str(ddata, "operator+", 9)) 3269 return (0); 3270 ddata->cur += 2; 3271 return (1); 3272 3273 case SIMPLE_HASH('p', 't'): 3274 /* operator -> */ 3275 if (!cpp_demangle_push_str(ddata, "operator->", 10)) 3276 return (0); 3277 ddata->cur += 2; 3278 return (1); 3279 3280 case SIMPLE_HASH('q', 'u'): 3281 /* operator ? */ 3282 if (!cpp_demangle_push_str(ddata, "operator?", 9)) 3283 return (0); 3284 ddata->cur += 2; 3285 return (1); 3286 3287 case SIMPLE_HASH('r', 'm'): 3288 /* operator % */ 3289 if (!cpp_demangle_push_str(ddata, "operator%", 9)) 3290 return (0); 3291 ddata->cur += 2; 3292 return (1); 3293 3294 case SIMPLE_HASH('r', 'M'): 3295 /* operator %= */ 3296 if (!cpp_demangle_push_str(ddata, "operator%=", 10)) 3297 return (0); 3298 ddata->cur += 2; 3299 return (1); 3300 3301 case SIMPLE_HASH('r', 's'): 3302 /* operator >> */ 3303 if (!cpp_demangle_push_str(ddata, "operator>>", 10)) 3304 return (0); 3305 ddata->cur += 2; 3306 return (1); 3307 3308 case SIMPLE_HASH('r', 'S'): 3309 /* operator >>= */ 3310 if (!cpp_demangle_push_str(ddata, "operator>>=", 11)) 3311 return (0); 3312 ddata->cur += 2; 3313 return (1); 3314 3315 case SIMPLE_HASH('r', 'z'): 3316 /* operator sizeof */ 3317 if (!cpp_demangle_push_str(ddata, "operator sizeof ", 16)) 3318 return (0); 3319 ddata->cur += 2; 3320 return (1); 3321 3322 case SIMPLE_HASH('s', 'r'): 3323 /* scope resolution operator */ 3324 if (!cpp_demangle_push_str(ddata, "scope resolution operator ", 3325 26)) 3326 return (0); 3327 ddata->cur += 2; 3328 return (1); 3329 3330 case SIMPLE_HASH('s', 'v'): 3331 /* operator sizeof */ 3332 if (!cpp_demangle_push_str(ddata, "operator sizeof ", 16)) 3333 return (0); 3334 ddata->cur += 2; 3335 return (1); 3336 } 3337 3338 /* vendor extened operator */ 3339 if (*ddata->cur == 'v' && ELFTC_ISDIGIT(*(ddata->cur + 1))) { 3340 if (!cpp_demangle_push_str(ddata, "vendor extened operator ", 3341 24)) 3342 return (0); 3343 if (!cpp_demangle_push_str(ddata, ddata->cur + 1, 1)) 3344 return (0); 3345 ddata->cur += 2; 3346 return (cpp_demangle_read_sname(ddata)); 3347 } 3348 3349 /* ctor-dtor-name */ 3350 switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) { 3351 case SIMPLE_HASH('C', '1'): 3352 /* FALLTHROUGH */ 3353 case SIMPLE_HASH('C', '2'): 3354 /* FALLTHROUGH */ 3355 case SIMPLE_HASH('C', '3'): 3356 if (ddata->last_sname == NULL) 3357 return (0); 3358 if ((len = strlen(ddata->last_sname)) == 0) 3359 return (0); 3360 if (!cpp_demangle_push_str(ddata, "::", 2)) 3361 return (0); 3362 if (!cpp_demangle_push_str(ddata, ddata->last_sname, len)) 3363 return (0); 3364 ddata->cur +=2; 3365 return (1); 3366 3367 case SIMPLE_HASH('D', '0'): 3368 /* FALLTHROUGH */ 3369 case SIMPLE_HASH('D', '1'): 3370 /* FALLTHROUGH */ 3371 case SIMPLE_HASH('D', '2'): 3372 if (ddata->last_sname == NULL) 3373 return (0); 3374 if ((len = strlen(ddata->last_sname)) == 0) 3375 return (0); 3376 if (!cpp_demangle_push_str(ddata, "::~", 3)) 3377 return (0); 3378 if (!cpp_demangle_push_str(ddata, ddata->last_sname, len)) 3379 return (0); 3380 ddata->cur +=2; 3381 return (1); 3382 } 3383 3384 /* source name */ 3385 if (ELFTC_ISDIGIT(*ddata->cur) != 0) 3386 return (cpp_demangle_read_sname(ddata)); 3387 3388 /* local source name */ 3389 if (*ddata->cur == 'L') 3390 return (cpp_demangle_local_source_name(ddata)); 3391 3392 return (1); 3393 } 3394 3395 /* 3396 * Read local source name. 3397 * 3398 * References: 3399 * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31775 3400 * http://gcc.gnu.org/viewcvs?view=rev&revision=124467 3401 */ 3402 static int 3403 cpp_demangle_local_source_name(struct cpp_demangle_data *ddata) 3404 { 3405 /* L */ 3406 if (ddata == NULL || *ddata->cur != 'L') 3407 return (0); 3408 ++ddata->cur; 3409 3410 /* source name */ 3411 if (!cpp_demangle_read_sname(ddata)) 3412 return (0); 3413 3414 /* discriminator */ 3415 if (*ddata->cur == '_') { 3416 ++ddata->cur; 3417 while (ELFTC_ISDIGIT(*ddata->cur) != 0) 3418 ++ddata->cur; 3419 } 3420 3421 return (1); 3422 } 3423 3424 static int 3425 cpp_demangle_read_v_offset(struct cpp_demangle_data *ddata) 3426 { 3427 3428 if (ddata == NULL) 3429 return (0); 3430 3431 if (!cpp_demangle_push_str(ddata, "offset : ", 9)) 3432 return (0); 3433 3434 if (!cpp_demangle_read_offset_number(ddata)) 3435 return (0); 3436 3437 if (!cpp_demangle_push_str(ddata, "virtual offset : ", 17)) 3438 return (0); 3439 3440 return (!cpp_demangle_read_offset_number(ddata)); 3441 } 3442 3443 /* 3444 * Decode floating point representation to string 3445 * Return new allocated string or NULL 3446 * 3447 * Todo 3448 * Replace these functions to macro. 3449 */ 3450 static char * 3451 decode_fp_to_double(const char *p, size_t len) 3452 { 3453 double f; 3454 size_t rtn_len, limit, i; 3455 int byte; 3456 char *rtn; 3457 3458 if (p == NULL || len == 0 || len % 2 != 0 || len / 2 > sizeof(double)) 3459 return (NULL); 3460 3461 memset(&f, 0, sizeof(double)); 3462 3463 for (i = 0; i < len / 2; ++i) { 3464 byte = hex_to_dec(p[len - i * 2 - 1]) + 3465 hex_to_dec(p[len - i * 2 - 2]) * 16; 3466 3467 if (byte < 0 || byte > 255) 3468 return (NULL); 3469 3470 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3471 ((unsigned char *)&f)[i] = (unsigned char)(byte); 3472 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3473 ((unsigned char *)&f)[sizeof(double) - i - 1] = 3474 (unsigned char)(byte); 3475 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3476 } 3477 3478 rtn_len = 64; 3479 limit = 0; 3480 again: 3481 if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) 3482 return (NULL); 3483 3484 if (snprintf(rtn, rtn_len, "%fld", f) >= (int)rtn_len) { 3485 free(rtn); 3486 if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) 3487 return (NULL); 3488 rtn_len *= BUFFER_GROWFACTOR; 3489 goto again; 3490 } 3491 3492 return rtn; 3493 } 3494 3495 static char * 3496 decode_fp_to_float(const char *p, size_t len) 3497 { 3498 size_t i, rtn_len, limit; 3499 float f; 3500 int byte; 3501 char *rtn; 3502 3503 if (p == NULL || len == 0 || len % 2 != 0 || len / 2 > sizeof(float)) 3504 return (NULL); 3505 3506 memset(&f, 0, sizeof(float)); 3507 3508 for (i = 0; i < len / 2; ++i) { 3509 byte = hex_to_dec(p[len - i * 2 - 1]) + 3510 hex_to_dec(p[len - i * 2 - 2]) * 16; 3511 if (byte < 0 || byte > 255) 3512 return (NULL); 3513 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3514 ((unsigned char *)&f)[i] = (unsigned char)(byte); 3515 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3516 ((unsigned char *)&f)[sizeof(float) - i - 1] = 3517 (unsigned char)(byte); 3518 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3519 } 3520 3521 rtn_len = 64; 3522 limit = 0; 3523 again: 3524 if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) 3525 return (NULL); 3526 3527 if (snprintf(rtn, rtn_len, "%ff", f) >= (int)rtn_len) { 3528 free(rtn); 3529 if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) 3530 return (NULL); 3531 rtn_len *= BUFFER_GROWFACTOR; 3532 goto again; 3533 } 3534 3535 return rtn; 3536 } 3537 3538 static char * 3539 decode_fp_to_float128(const char *p, size_t len) 3540 { 3541 long double f; 3542 size_t rtn_len, limit, i; 3543 int byte; 3544 unsigned char buf[FLOAT_QUADRUPLE_BYTES]; 3545 char *rtn; 3546 3547 switch(sizeof(long double)) { 3548 case FLOAT_QUADRUPLE_BYTES: 3549 return (decode_fp_to_long_double(p, len)); 3550 case FLOAT_EXTENED_BYTES: 3551 if (p == NULL || len == 0 || len % 2 != 0 || 3552 len / 2 > FLOAT_QUADRUPLE_BYTES) 3553 return (NULL); 3554 3555 memset(buf, 0, FLOAT_QUADRUPLE_BYTES); 3556 3557 for (i = 0; i < len / 2; ++i) { 3558 byte = hex_to_dec(p[len - i * 2 - 1]) + 3559 hex_to_dec(p[len - i * 2 - 2]) * 16; 3560 if (byte < 0 || byte > 255) 3561 return (NULL); 3562 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3563 buf[i] = (unsigned char)(byte); 3564 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3565 buf[FLOAT_QUADRUPLE_BYTES - i -1] = 3566 (unsigned char)(byte); 3567 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3568 } 3569 memset(&f, 0, FLOAT_EXTENED_BYTES); 3570 3571 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3572 memcpy(&f, buf, FLOAT_EXTENED_BYTES); 3573 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3574 memcpy(&f, buf + 6, FLOAT_EXTENED_BYTES); 3575 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3576 3577 rtn_len = 256; 3578 limit = 0; 3579 again: 3580 if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) 3581 return (NULL); 3582 3583 if (snprintf(rtn, rtn_len, "%Lfd", f) >= (int)rtn_len) { 3584 free(rtn); 3585 if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) 3586 return (NULL); 3587 rtn_len *= BUFFER_GROWFACTOR; 3588 goto again; 3589 } 3590 3591 return (rtn); 3592 default: 3593 return (NULL); 3594 } 3595 } 3596 3597 static char * 3598 decode_fp_to_float80(const char *p, size_t len) 3599 { 3600 long double f; 3601 size_t rtn_len, limit, i; 3602 int byte; 3603 unsigned char buf[FLOAT_EXTENED_BYTES]; 3604 char *rtn; 3605 3606 switch(sizeof(long double)) { 3607 case FLOAT_QUADRUPLE_BYTES: 3608 if (p == NULL || len == 0 || len % 2 != 0 || 3609 len / 2 > FLOAT_EXTENED_BYTES) 3610 return (NULL); 3611 3612 memset(buf, 0, FLOAT_EXTENED_BYTES); 3613 3614 for (i = 0; i < len / 2; ++i) { 3615 byte = hex_to_dec(p[len - i * 2 - 1]) + 3616 hex_to_dec(p[len - i * 2 - 2]) * 16; 3617 3618 if (byte < 0 || byte > 255) 3619 return (NULL); 3620 3621 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3622 buf[i] = (unsigned char)(byte); 3623 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3624 buf[FLOAT_EXTENED_BYTES - i -1] = 3625 (unsigned char)(byte); 3626 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3627 } 3628 3629 memset(&f, 0, FLOAT_QUADRUPLE_BYTES); 3630 3631 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3632 memcpy(&f, buf, FLOAT_EXTENED_BYTES); 3633 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3634 memcpy((unsigned char *)(&f) + 6, buf, FLOAT_EXTENED_BYTES); 3635 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3636 3637 rtn_len = 256; 3638 limit = 0; 3639 again: 3640 if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) 3641 return (NULL); 3642 3643 if (snprintf(rtn, rtn_len, "%Lfd", f) >= (int)rtn_len) { 3644 free(rtn); 3645 if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) 3646 return (NULL); 3647 rtn_len *= BUFFER_GROWFACTOR; 3648 goto again; 3649 } 3650 3651 return (rtn); 3652 case FLOAT_EXTENED_BYTES: 3653 return (decode_fp_to_long_double(p, len)); 3654 default: 3655 return (NULL); 3656 } 3657 } 3658 3659 static char * 3660 decode_fp_to_long_double(const char *p, size_t len) 3661 { 3662 long double f; 3663 size_t rtn_len, limit, i; 3664 int byte; 3665 char *rtn; 3666 3667 if (p == NULL || len == 0 || len % 2 != 0 || 3668 len / 2 > sizeof(long double)) 3669 return (NULL); 3670 3671 memset(&f, 0, sizeof(long double)); 3672 3673 for (i = 0; i < len / 2; ++i) { 3674 byte = hex_to_dec(p[len - i * 2 - 1]) + 3675 hex_to_dec(p[len - i * 2 - 2]) * 16; 3676 3677 if (byte < 0 || byte > 255) 3678 return (NULL); 3679 3680 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3681 ((unsigned char *)&f)[i] = (unsigned char)(byte); 3682 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3683 ((unsigned char *)&f)[sizeof(long double) - i - 1] = 3684 (unsigned char)(byte); 3685 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3686 } 3687 3688 rtn_len = 256; 3689 limit = 0; 3690 again: 3691 if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) 3692 return (NULL); 3693 3694 if (snprintf(rtn, rtn_len, "%Lfd", f) >= (int)rtn_len) { 3695 free(rtn); 3696 if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) 3697 return (NULL); 3698 rtn_len *= BUFFER_GROWFACTOR; 3699 goto again; 3700 } 3701 3702 return (rtn); 3703 } 3704 3705 /* Simple hex to integer function used by decode_to_* function. */ 3706 static int 3707 hex_to_dec(char c) 3708 { 3709 3710 switch (c) { 3711 case '0': 3712 return (0); 3713 case '1': 3714 return (1); 3715 case '2': 3716 return (2); 3717 case '3': 3718 return (3); 3719 case '4': 3720 return (4); 3721 case '5': 3722 return (5); 3723 case '6': 3724 return (6); 3725 case '7': 3726 return (7); 3727 case '8': 3728 return (8); 3729 case '9': 3730 return (9); 3731 case 'a': 3732 return (10); 3733 case 'b': 3734 return (11); 3735 case 'c': 3736 return (12); 3737 case 'd': 3738 return (13); 3739 case 'e': 3740 return (14); 3741 case 'f': 3742 return (15); 3743 default: 3744 return (-1); 3745 } 3746 } 3747 3748 static void 3749 vector_read_cmd_dest(struct vector_read_cmd *v) 3750 { 3751 3752 if (v == NULL) 3753 return; 3754 3755 free(v->r_container); 3756 } 3757 3758 /* return -1 at failed, 0 at not found, 1 at found. */ 3759 static int 3760 vector_read_cmd_find(struct vector_read_cmd *v, enum read_cmd dst) 3761 { 3762 size_t i; 3763 3764 if (v == NULL || dst == READ_FAIL) 3765 return (-1); 3766 3767 for (i = 0; i < v->size; ++i) 3768 if (v->r_container[i] == dst) 3769 return (1); 3770 3771 return (0); 3772 } 3773 3774 static int 3775 vector_read_cmd_init(struct vector_read_cmd *v) 3776 { 3777 3778 if (v == NULL) 3779 return (0); 3780 3781 v->size = 0; 3782 v->capacity = VECTOR_DEF_CAPACITY; 3783 3784 if ((v->r_container = malloc(sizeof(enum read_cmd) * v->capacity)) 3785 == NULL) 3786 return (0); 3787 3788 return (1); 3789 } 3790 3791 static int 3792 vector_read_cmd_pop(struct vector_read_cmd *v) 3793 { 3794 3795 if (v == NULL || v->size == 0) 3796 return (0); 3797 3798 --v->size; 3799 v->r_container[v->size] = READ_FAIL; 3800 3801 return (1); 3802 } 3803 3804 static int 3805 vector_read_cmd_push(struct vector_read_cmd *v, enum read_cmd cmd) 3806 { 3807 enum read_cmd *tmp_r_ctn; 3808 size_t tmp_cap; 3809 size_t i; 3810 3811 if (v == NULL) 3812 return (0); 3813 3814 if (v->size == v->capacity) { 3815 tmp_cap = v->capacity * BUFFER_GROWFACTOR; 3816 if ((tmp_r_ctn = malloc(sizeof(enum read_cmd) * tmp_cap)) 3817 == NULL) 3818 return (0); 3819 for (i = 0; i < v->size; ++i) 3820 tmp_r_ctn[i] = v->r_container[i]; 3821 free(v->r_container); 3822 v->r_container = tmp_r_ctn; 3823 v->capacity = tmp_cap; 3824 } 3825 3826 v->r_container[v->size] = cmd; 3827 ++v->size; 3828 3829 return (1); 3830 } 3831 3832 static void 3833 vector_type_qualifier_dest(struct vector_type_qualifier *v) 3834 { 3835 3836 if (v == NULL) 3837 return; 3838 3839 free(v->q_container); 3840 vector_str_dest(&v->ext_name); 3841 } 3842 3843 /* size, capacity, ext_name */ 3844 static int 3845 vector_type_qualifier_init(struct vector_type_qualifier *v) 3846 { 3847 3848 if (v == NULL) 3849 return (0); 3850 3851 v->size = 0; 3852 v->capacity = VECTOR_DEF_CAPACITY; 3853 3854 if ((v->q_container = malloc(sizeof(enum type_qualifier) * v->capacity)) 3855 == NULL) 3856 return (0); 3857 3858 assert(v->q_container != NULL); 3859 3860 if (vector_str_init(&v->ext_name) == false) { 3861 free(v->q_container); 3862 return (0); 3863 } 3864 3865 return (1); 3866 } 3867 3868 static int 3869 vector_type_qualifier_push(struct vector_type_qualifier *v, 3870 enum type_qualifier t) 3871 { 3872 enum type_qualifier *tmp_ctn; 3873 size_t tmp_cap; 3874 size_t i; 3875 3876 if (v == NULL) 3877 return (0); 3878 3879 if (v->size == v->capacity) { 3880 tmp_cap = v->capacity * BUFFER_GROWFACTOR; 3881 if ((tmp_ctn = malloc(sizeof(enum type_qualifier) * tmp_cap)) 3882 == NULL) 3883 return (0); 3884 for (i = 0; i < v->size; ++i) 3885 tmp_ctn[i] = v->q_container[i]; 3886 free(v->q_container); 3887 v->q_container = tmp_ctn; 3888 v->capacity = tmp_cap; 3889 } 3890 3891 v->q_container[v->size] = t; 3892 ++v->size; 3893 3894 return (1); 3895 } 3896