2 * Copyright (c) 2007, 2008 Hyogeol Lee <hyogeollee@gmail.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include <sys/types.h>
37 * @file cpp_demangle.c
38 * @brief Decode IA-64 C++ ABI style implementation.
40 * IA-64 standard ABI(Itanium C++ ABI) references.
42 * http://www.codesourcery.com/cxx-abi/abi.html#mangling \n
43 * http://www.codesourcery.com/cxx-abi/abi-mangling.html
46 /** @brief Dynamic vector data for string. */
56 #define BUFFER_GROWFACTOR 1.618
57 #define VECTOR_DEF_CAPACITY 8
58 #define ELFTC_ISDIGIT(C) (isdigit((C) & 0xFF))
61 TYPE_PTR
, TYPE_REF
, TYPE_CMX
, TYPE_IMG
, TYPE_EXT
, TYPE_RST
, TYPE_VAT
,
65 struct vector_type_qualifier
{
66 size_t size
, capacity
;
67 enum type_qualifier
*q_container
;
68 struct vector_str ext_name
;
72 READ_FAIL
, READ_NEST
, READ_TMPL
, READ_EXPR
, READ_EXPL
, READ_LOCAL
,
73 READ_TYPE
, READ_FUNC
, READ_PTRMEM
76 struct vector_read_cmd
{
77 size_t size
, capacity
;
78 enum read_cmd
*r_container
;
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 */
94 const char *cur
; /* current mangled name ptr */
95 const char *last_sname
; /* last source name */
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
104 #define SIMPLE_HASH(x,y) (64 * x + y)
106 static size_t get_strlen_sum(const struct vector_str
*v
);
107 static bool vector_str_grow(struct vector_str
*v
);
110 get_strlen_sum(const struct vector_str
*v
)
119 for (i
= 0; i
< v
->size
; ++i
)
120 len
+= strlen(v
->container
[i
]);
126 * @brief Deallocate resource in vector_str.
129 vector_str_dest(struct vector_str
*v
)
136 for (i
= 0; i
< v
->size
; ++i
)
137 free(v
->container
[i
]);
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.
150 vector_str_find(const struct vector_str
*v
, const char *o
, size_t l
)
154 if (v
== NULL
|| o
== NULL
)
157 for (i
= 0; i
< v
->size
; ++i
)
158 if (strncmp(v
->container
[i
], o
, l
) == 0)
165 * @brief Get new allocated flat string from vector.
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.
173 vector_str_get_flat(const struct vector_str
*v
, size_t *l
)
175 ssize_t elem_pos
, elem_size
, rtn_size
;
179 if (v
== NULL
|| v
->size
== 0)
182 if ((rtn_size
= get_strlen_sum(v
)) == 0)
185 if ((rtn
= malloc(sizeof(char) * (rtn_size
+ 1))) == NULL
)
189 for (i
= 0; i
< v
->size
; ++i
) {
190 elem_size
= strlen(v
->container
[i
]);
192 memcpy(rtn
+ elem_pos
, v
->container
[i
], elem_size
);
194 elem_pos
+= elem_size
;
197 rtn
[rtn_size
] = '\0';
206 vector_str_grow(struct vector_str
*v
)
214 assert(v
->capacity
> 0);
216 tmp_cap
= v
->capacity
* BUFFER_GROWFACTOR
;
218 assert(tmp_cap
> v
->capacity
);
220 if ((tmp_ctn
= malloc(sizeof(char *) * tmp_cap
)) == NULL
)
223 for (i
= 0; i
< v
->size
; ++i
)
224 tmp_ctn
[i
] = v
->container
[i
];
228 v
->container
= tmp_ctn
;
229 v
->capacity
= tmp_cap
;
235 * @brief Initialize vector_str.
236 * @return false at failed, true at success.
239 vector_str_init(struct vector_str
*v
)
246 v
->capacity
= VECTOR_DEF_CAPACITY
;
248 assert(v
->capacity
> 0);
250 if ((v
->container
= malloc(sizeof(char *) * v
->capacity
)) == NULL
)
253 assert(v
->container
!= NULL
);
259 * @brief Remove last element in vector_str.
260 * @return false at failed, true at success.
263 vector_str_pop(struct vector_str
*v
)
274 free(v
->container
[v
->size
]);
275 v
->container
[v
->size
] = NULL
;
281 * @brief Push back string to vector.
282 * @return false at failed, true at success.
285 vector_str_push(struct vector_str
*v
, const char *str
, size_t len
)
288 if (v
== NULL
|| str
== NULL
)
291 if (v
->size
== v
->capacity
&& vector_str_grow(v
) == false)
294 if ((v
->container
[v
->size
] = malloc(sizeof(char) * (len
+ 1))) == NULL
)
297 snprintf(v
->container
[v
->size
], len
+ 1, "%s", str
);
305 * @brief Push front org vector to det vector.
306 * @return false at failed, true at success.
309 vector_str_push_vector_head(struct vector_str
*dst
, struct vector_str
*org
)
311 size_t i
, j
, tmp_cap
;
314 if (dst
== NULL
|| org
== NULL
)
317 tmp_cap
= (dst
->size
+ org
->size
) * BUFFER_GROWFACTOR
;
319 if ((tmp_ctn
= malloc(sizeof(char *) * tmp_cap
)) == NULL
)
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
)
332 for (i
= 0; i
< dst
->size
; ++i
)
333 tmp_ctn
[i
+ org
->size
] = dst
->container
[i
];
335 free(dst
->container
);
337 dst
->container
= tmp_ctn
;
338 dst
->capacity
= tmp_cap
;
339 dst
->size
+= org
->size
;
345 * @brief Get new allocated flat string from vector between begin and end.
347 * If r_len is not NULL, string length will be returned.
348 * @return NULL at failed or NUL terminated new allocated string.
351 vector_str_substr(const struct vector_str
*v
, size_t begin
, size_t end
,
357 if (v
== NULL
|| begin
> end
)
361 for (i
= begin
; i
< end
+ 1; ++i
)
362 len
+= strlen(v
->container
[i
]);
364 if ((rtn
= malloc(sizeof(char) * (len
+ 1))) == NULL
)
371 for (i
= begin
; i
< end
+ 1; ++i
) {
372 len
= strlen(v
->container
[i
]);
373 memcpy(rtn
+ cur
, v
->container
[i
], len
);
381 static void cpp_demangle_data_dest(struct cpp_demangle_data
*);
382 static int cpp_demangle_data_init(struct cpp_demangle_data
*,
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 *,
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_binary(struct cpp_demangle_data
*,
401 const char *, size_t);
402 static int cpp_demangle_read_expression_unary(struct cpp_demangle_data
*,
403 const char *, size_t);
404 static int cpp_demangle_read_expression_trinary(struct cpp_demangle_data
*,
405 const char *, size_t, const char *, size_t);
406 static int cpp_demangle_read_function(struct cpp_demangle_data
*, int *,
407 struct vector_type_qualifier
*);
408 static int cpp_demangle_read_local_name(struct cpp_demangle_data
*);
409 static int cpp_demangle_read_name(struct cpp_demangle_data
*);
410 static int cpp_demangle_read_nested_name(struct cpp_demangle_data
*);
411 static int cpp_demangle_read_number(struct cpp_demangle_data
*, long *);
412 static int cpp_demangle_read_nv_offset(struct cpp_demangle_data
*);
413 static int cpp_demangle_read_offset(struct cpp_demangle_data
*);
414 static int cpp_demangle_read_offset_number(struct cpp_demangle_data
*);
415 static int cpp_demangle_read_pointer_to_member(struct cpp_demangle_data
*);
416 static int cpp_demangle_read_sname(struct cpp_demangle_data
*);
417 static int cpp_demangle_read_subst(struct cpp_demangle_data
*);
418 static int cpp_demangle_read_subst_std(struct cpp_demangle_data
*);
419 static int cpp_demangle_read_subst_stdtmpl(struct cpp_demangle_data
*,
420 const char *, size_t);
421 static int cpp_demangle_read_tmpl_arg(struct cpp_demangle_data
*);
422 static int cpp_demangle_read_tmpl_args(struct cpp_demangle_data
*);
423 static int cpp_demangle_read_tmpl_param(struct cpp_demangle_data
*);
424 static int cpp_demangle_read_type(struct cpp_demangle_data
*, int);
425 static int cpp_demangle_read_uqname(struct cpp_demangle_data
*);
426 static int cpp_demangle_read_v_offset(struct cpp_demangle_data
*);
427 static char *decode_fp_to_double(const char *, size_t);
428 static char *decode_fp_to_float(const char *, size_t);
429 static char *decode_fp_to_float128(const char *, size_t);
430 static char *decode_fp_to_float80(const char *, size_t);
431 static char *decode_fp_to_long_double(const char *, size_t);
432 static int hex_to_dec(char);
433 static void vector_read_cmd_dest(struct vector_read_cmd
*);
434 static int vector_read_cmd_find(struct vector_read_cmd
*, enum read_cmd
);
435 static int vector_read_cmd_init(struct vector_read_cmd
*);
436 static int vector_read_cmd_pop(struct vector_read_cmd
*);
437 static int vector_read_cmd_push(struct vector_read_cmd
*, enum read_cmd
);
438 static void vector_type_qualifier_dest(struct vector_type_qualifier
*);
439 static int vector_type_qualifier_init(struct vector_type_qualifier
*);
440 static int vector_type_qualifier_push(struct vector_type_qualifier
*,
441 enum type_qualifier
);
444 * @brief Decode the input string by IA-64 C++ ABI style.
446 * GNU GCC v3 use IA-64 standard ABI.
447 * @return New allocated demangled string or NULL if failed.
448 * @todo 1. Testing and more test case. 2. Code cleaning.
451 __cxa_demangle_gnu3(const char *org
)
453 struct cpp_demangle_data ddata
;
461 // Try demangling as a type for short encodings
462 if (((org_len
= strlen(org
)) < 2) || (org
[0] != '_' || org
[1] != 'Z' )) {
463 if (!cpp_demangle_data_init(&ddata
, org
))
465 if (!cpp_demangle_read_type(&ddata
, 0))
467 rtn
= vector_str_get_flat(&ddata
.output
, (size_t *) NULL
);
470 if (org_len
> 11 && !strncmp(org
, "_GLOBAL__I_", 11)) {
471 if ((rtn
= malloc(org_len
+ 19)) == NULL
)
473 snprintf(rtn
, org_len
+ 19,
474 "global constructors keyed to %s", org
+ 11);
479 if (!cpp_demangle_data_init(&ddata
, org
+ 2))
484 if (!cpp_demangle_read_encoding(&ddata
))
488 while (*ddata
.cur
!= '\0') {
490 * Breaking at some gcc info at tail. e.g) @@GLIBCXX_3.4
492 if (*ddata
.cur
== '@' && *(ddata
.cur
+ 1) == '@')
494 if (!cpp_demangle_read_type(&ddata
, 1))
496 if (limit
++ > CPP_DEMANGLE_TRY_LIMIT
)
500 if (ddata
.output
.size
== 0)
502 if (ddata
.paren
&& !vector_str_push(&ddata
.output
, ")", 1))
504 if (ddata
.mem_vat
&& !vector_str_push(&ddata
.output
, " volatile", 9))
506 if (ddata
.mem_cst
&& !vector_str_push(&ddata
.output
, " const", 6))
508 if (ddata
.mem_rst
&& !vector_str_push(&ddata
.output
, " restrict", 9))
511 rtn
= vector_str_get_flat(&ddata
.output
, (size_t *) NULL
);
514 cpp_demangle_data_dest(&ddata
);
520 cpp_demangle_data_dest(struct cpp_demangle_data
*d
)
526 vector_read_cmd_dest(&d
->cmd
);
527 vector_str_dest(&d
->class_type
);
528 vector_str_dest(&d
->tmpl
);
529 vector_str_dest(&d
->subst
);
530 vector_str_dest(&d
->output_tmp
);
531 vector_str_dest(&d
->output
);
535 cpp_demangle_data_init(struct cpp_demangle_data
*d
, const char *cur
)
538 if (d
== NULL
|| cur
== NULL
)
541 if (!vector_str_init(&d
->output
))
543 if (!vector_str_init(&d
->output_tmp
))
545 if (!vector_str_init(&d
->subst
))
547 if (!vector_str_init(&d
->tmpl
))
549 if (!vector_str_init(&d
->class_type
))
551 if (!vector_read_cmd_init(&d
->cmd
))
554 assert(d
->output
.container
!= NULL
);
555 assert(d
->output_tmp
.container
!= NULL
);
556 assert(d
->subst
.container
!= NULL
);
557 assert(d
->tmpl
.container
!= NULL
);
558 assert(d
->class_type
.container
!= NULL
);
567 d
->last_sname
= NULL
;
573 vector_str_dest(&d
->class_type
);
575 vector_str_dest(&d
->tmpl
);
577 vector_str_dest(&d
->subst
);
579 vector_str_dest(&d
->output_tmp
);
581 vector_str_dest(&d
->output
);
587 cpp_demangle_push_fp(struct cpp_demangle_data
*ddata
,
588 char *(*decoder
)(const char *, size_t))
595 if (ddata
== NULL
|| decoder
== NULL
)
599 while (*ddata
->cur
!= 'E')
603 if ((f
= decoder(fp
, ddata
->cur
- fp
)) == NULL
)
607 if ((len
= strlen(f
)) > 0 &&
608 cpp_demangle_push_str(ddata
, f
, len
))
617 cpp_demangle_push_str(struct cpp_demangle_data
*ddata
, const char *str
,
621 if (ddata
== NULL
|| str
== NULL
|| len
== 0)
624 if (ddata
->push_head
> 0)
625 return (vector_str_push(&ddata
->output_tmp
, str
, len
));
627 return (vector_str_push(&ddata
->output
, str
, len
));
631 cpp_demangle_push_subst(struct cpp_demangle_data
*ddata
, const char *str
,
635 if (ddata
== NULL
|| str
== NULL
|| len
== 0)
638 if (!vector_str_find(&ddata
->subst
, str
, len
))
639 return (vector_str_push(&ddata
->subst
, str
, len
));
645 cpp_demangle_push_subst_v(struct cpp_demangle_data
*ddata
, struct vector_str
*v
)
651 if (ddata
== NULL
|| v
== NULL
)
654 if ((str
= vector_str_get_flat(v
, &str_len
)) == NULL
)
657 rtn
= cpp_demangle_push_subst(ddata
, str
, str_len
);
664 cpp_demangle_push_type_qualifier(struct cpp_demangle_data
*ddata
,
665 struct vector_type_qualifier
*v
, const char *type_str
)
667 struct vector_str subst_v
;
668 size_t idx
, e_idx
, e_len
;
672 if (ddata
== NULL
|| v
== NULL
)
675 if ((idx
= v
->size
) == 0)
679 if (type_str
!= NULL
) {
680 if (!vector_str_init(&subst_v
))
682 if (!vector_str_push(&subst_v
, type_str
, strlen(type_str
)))
688 switch (v
->q_container
[idx
- 1]) {
690 if (!cpp_demangle_push_str(ddata
, "*", 1))
692 if (type_str
!= NULL
) {
693 if (!vector_str_push(&subst_v
, "*", 1))
695 if (!cpp_demangle_push_subst_v(ddata
, &subst_v
))
701 if (!cpp_demangle_push_str(ddata
, "&", 1))
703 if (type_str
!= NULL
) {
704 if (!vector_str_push(&subst_v
, "&", 1))
706 if (!cpp_demangle_push_subst_v(ddata
, &subst_v
))
712 if (!cpp_demangle_push_str(ddata
, " complex", 8))
714 if (type_str
!= NULL
) {
715 if (!vector_str_push(&subst_v
, " complex", 8))
717 if (!cpp_demangle_push_subst_v(ddata
, &subst_v
))
723 if (!cpp_demangle_push_str(ddata
, " imaginary", 10))
725 if (type_str
!= NULL
) {
726 if (!vector_str_push(&subst_v
, " imaginary", 10))
728 if (!cpp_demangle_push_subst_v(ddata
, &subst_v
))
734 if (e_idx
> v
->ext_name
.size
- 1)
736 if ((e_len
= strlen(v
->ext_name
.container
[e_idx
])) == 0)
738 if ((buf
= malloc(sizeof(char) * (e_len
+ 1))) == NULL
)
742 memcpy(buf
+ 1, v
->ext_name
.container
[e_idx
], e_len
);
744 if (!cpp_demangle_push_str(ddata
, buf
, e_len
+ 1)) {
749 if (type_str
!= NULL
) {
750 if (!vector_str_push(&subst_v
, buf
,
755 if (!cpp_demangle_push_subst_v(ddata
, &subst_v
)) {
765 if (!cpp_demangle_push_str(ddata
, " restrict", 9))
767 if (type_str
!= NULL
) {
768 if (!vector_str_push(&subst_v
, " restrict", 9))
770 if (!cpp_demangle_push_subst_v(ddata
, &subst_v
))
776 if (!cpp_demangle_push_str(ddata
, " volatile", 9))
778 if (type_str
!= NULL
) {
779 if (!vector_str_push(&subst_v
, " volatile", 9))
781 if (!cpp_demangle_push_subst_v(ddata
, &subst_v
))
787 if (!cpp_demangle_push_str(ddata
, " const", 6))
789 if (type_str
!= NULL
) {
790 if (!vector_str_push(&subst_v
, " const", 6))
792 if (!cpp_demangle_push_subst_v(ddata
, &subst_v
))
803 if (type_str
!= NULL
)
804 vector_str_dest(&subst_v
);
810 cpp_demangle_get_subst(struct cpp_demangle_data
*ddata
, size_t idx
)
814 if (ddata
== NULL
|| ddata
->subst
.size
<= idx
)
816 if ((len
= strlen(ddata
->subst
.container
[idx
])) == 0)
818 if (!cpp_demangle_push_str(ddata
, ddata
->subst
.container
[idx
], len
))
828 cpp_demangle_get_tmpl_param(struct cpp_demangle_data
*ddata
, size_t idx
)
832 if (ddata
== NULL
|| ddata
->tmpl
.size
<= idx
)
834 if ((len
= strlen(ddata
->tmpl
.container
[idx
])) == 0)
836 if (!cpp_demangle_push_str(ddata
, ddata
->tmpl
.container
[idx
], len
))
845 cpp_demangle_read_array(struct cpp_demangle_data
*ddata
)
847 size_t i
, num_len
, exp_len
, p_idx
, idx
;
851 if (ddata
== NULL
|| *(++ddata
->cur
) == '\0')
854 if (*ddata
->cur
== '_') {
855 if (*(++ddata
->cur
) == '\0')
858 if (!cpp_demangle_read_type(ddata
, 0))
861 if (!cpp_demangle_push_str(ddata
, "[]", 2))
864 if (ELFTC_ISDIGIT(*ddata
->cur
) != 0) {
866 while (ELFTC_ISDIGIT(*ddata
->cur
) != 0)
868 if (*ddata
->cur
!= '_')
870 num_len
= ddata
->cur
- num
;
872 if (*(++ddata
->cur
) == '\0')
874 if (!cpp_demangle_read_type(ddata
, 0))
876 if (!cpp_demangle_push_str(ddata
, "[", 1))
878 if (!cpp_demangle_push_str(ddata
, num
, num_len
))
880 if (!cpp_demangle_push_str(ddata
, "]", 1))
883 p_idx
= ddata
->output
.size
;
884 if (!cpp_demangle_read_expression(ddata
))
886 if ((exp
= vector_str_substr(&ddata
->output
, p_idx
,
887 ddata
->output
.size
- 1, &exp_len
)) == NULL
)
889 idx
= ddata
->output
.size
;
890 for (i
= p_idx
; i
< idx
; ++i
)
891 if (!vector_str_pop(&ddata
->output
)) {
895 if (*ddata
->cur
!= '_') {
900 if (*ddata
->cur
== '\0') {
904 if (!cpp_demangle_read_type(ddata
, 0)) {
908 if (!cpp_demangle_push_str(ddata
, "[", 1)) {
912 if (!cpp_demangle_push_str(ddata
, exp
, exp_len
)) {
916 if (!cpp_demangle_push_str(ddata
, "]", 1)) {
928 cpp_demangle_read_expr_primary(struct cpp_demangle_data
*ddata
)
932 if (ddata
== NULL
|| *(++ddata
->cur
) == '\0')
935 if (*ddata
->cur
== '_' && *(ddata
->cur
+ 1) == 'Z') {
937 if (*ddata
->cur
== '\0')
939 if (!cpp_demangle_read_encoding(ddata
))
945 switch (*ddata
->cur
) {
947 switch (*(++ddata
->cur
)) {
949 return (cpp_demangle_push_str(ddata
, "false", 5));
951 return (cpp_demangle_push_str(ddata
, "true", 4));
958 return (cpp_demangle_push_fp(ddata
, decode_fp_to_double
));
962 if (sizeof(long double) == 10)
963 return (cpp_demangle_push_fp(ddata
,
964 decode_fp_to_double
));
965 return (cpp_demangle_push_fp(ddata
, decode_fp_to_float80
));
969 return (cpp_demangle_push_fp(ddata
, decode_fp_to_float
));
973 if (sizeof(long double) == 16)
974 return (cpp_demangle_push_fp(ddata
,
975 decode_fp_to_double
));
976 return (cpp_demangle_push_fp(ddata
, decode_fp_to_float128
));
987 if (*(++ddata
->cur
) == 'n') {
988 if (!cpp_demangle_push_str(ddata
, "-", 1))
993 while (*ddata
->cur
!= 'E') {
994 if (!ELFTC_ISDIGIT(*ddata
->cur
))
999 return (cpp_demangle_push_str(ddata
, num
, ddata
->cur
- num
));
1007 cpp_demangle_read_expression(struct cpp_demangle_data
*ddata
)
1010 if (ddata
== NULL
|| *ddata
->cur
== '\0')
1013 switch (SIMPLE_HASH(*ddata
->cur
, *(ddata
->cur
+ 1))) {
1014 case SIMPLE_HASH('s', 't'):
1016 return (cpp_demangle_read_type(ddata
, 0));
1018 case SIMPLE_HASH('s', 'r'):
1020 if (!cpp_demangle_read_type(ddata
, 0))
1022 if (!cpp_demangle_read_uqname(ddata
))
1024 if (*ddata
->cur
== 'I')
1025 return (cpp_demangle_read_tmpl_args(ddata
));
1028 case SIMPLE_HASH('a', 'a'):
1031 return (cpp_demangle_read_expression_binary(ddata
, "&&", 2));
1033 case SIMPLE_HASH('a', 'd'):
1034 /* operator & (unary) */
1036 return (cpp_demangle_read_expression_unary(ddata
, "&", 1));
1038 case SIMPLE_HASH('a', 'n'):
1041 return (cpp_demangle_read_expression_binary(ddata
, "&", 1));
1043 case SIMPLE_HASH('a', 'N'):
1046 return (cpp_demangle_read_expression_binary(ddata
, "&=", 2));
1048 case SIMPLE_HASH('a', 'S'):
1051 return (cpp_demangle_read_expression_binary(ddata
, "=", 1));
1053 case SIMPLE_HASH('c', 'l'):
1056 return (cpp_demangle_read_expression_binary(ddata
, "()", 2));
1058 case SIMPLE_HASH('c', 'm'):
1061 return (cpp_demangle_read_expression_binary(ddata
, ",", 1));
1063 case SIMPLE_HASH('c', 'o'):
1066 return (cpp_demangle_read_expression_binary(ddata
, "~", 1));
1068 case SIMPLE_HASH('c', 'v'):
1069 /* operator (cast) */
1071 return (cpp_demangle_read_expression_binary(ddata
, "(cast)", 6));
1073 case SIMPLE_HASH('d', 'a'):
1074 /* operator delete [] */
1076 return (cpp_demangle_read_expression_unary(ddata
, "delete []", 9));
1078 case SIMPLE_HASH('d', 'e'):
1079 /* operator * (unary) */
1081 return (cpp_demangle_read_expression_unary(ddata
, "*", 1));
1083 case SIMPLE_HASH('d', 'l'):
1084 /* operator delete */
1086 return (cpp_demangle_read_expression_unary(ddata
, "delete", 6));
1088 case SIMPLE_HASH('d', 'v'):
1091 return (cpp_demangle_read_expression_binary(ddata
, "/", 1));
1093 case SIMPLE_HASH('d', 'V'):
1096 return (cpp_demangle_read_expression_binary(ddata
, "/=", 2));
1098 case SIMPLE_HASH('e', 'o'):
1101 return (cpp_demangle_read_expression_binary(ddata
, "^", 1));
1103 case SIMPLE_HASH('e', 'O'):
1106 return (cpp_demangle_read_expression_binary(ddata
, "^=", 2));
1108 case SIMPLE_HASH('e', 'q'):
1111 return (cpp_demangle_read_expression_binary(ddata
, "==", 2));
1113 case SIMPLE_HASH('g', 'e'):
1116 return (cpp_demangle_read_expression_binary(ddata
, ">=", 2));
1118 case SIMPLE_HASH('g', 't'):
1121 return (cpp_demangle_read_expression_binary(ddata
, ">", 1));
1123 case SIMPLE_HASH('i', 'x'):
1126 return (cpp_demangle_read_expression_binary(ddata
, "[]", 2));
1128 case SIMPLE_HASH('l', 'e'):
1131 return (cpp_demangle_read_expression_binary(ddata
, "<=", 2));
1133 case SIMPLE_HASH('l', 's'):
1136 return (cpp_demangle_read_expression_binary(ddata
, "<<", 2));
1138 case SIMPLE_HASH('l', 'S'):
1141 return (cpp_demangle_read_expression_binary(ddata
, "<<=", 3));
1143 case SIMPLE_HASH('l', 't'):
1146 return (cpp_demangle_read_expression_binary(ddata
, "<", 1));
1148 case SIMPLE_HASH('m', 'i'):
1151 return (cpp_demangle_read_expression_binary(ddata
, "-", 1));
1153 case SIMPLE_HASH('m', 'I'):
1156 return (cpp_demangle_read_expression_binary(ddata
, "-=", 2));
1158 case SIMPLE_HASH('m', 'l'):
1161 return (cpp_demangle_read_expression_binary(ddata
, "*", 1));
1163 case SIMPLE_HASH('m', 'L'):
1166 return (cpp_demangle_read_expression_binary(ddata
, "*=", 2));
1168 case SIMPLE_HASH('m', 'm'):
1171 return (cpp_demangle_read_expression_binary(ddata
, "--", 2));
1173 case SIMPLE_HASH('n', 'a'):
1174 /* operator new[] */
1176 return (cpp_demangle_read_expression_unary(ddata
, "new []", 6));
1178 case SIMPLE_HASH('n', 'e'):
1181 return (cpp_demangle_read_expression_binary(ddata
, "!=", 2));
1183 case SIMPLE_HASH('n', 'g'):
1184 /* operator - (unary) */
1186 return (cpp_demangle_read_expression_unary(ddata
, "-", 1));
1188 case SIMPLE_HASH('n', 't'):
1191 return (cpp_demangle_read_expression_binary(ddata
, "!", 1));
1193 case SIMPLE_HASH('n', 'w'):
1196 return (cpp_demangle_read_expression_unary(ddata
, "new", 3));
1198 case SIMPLE_HASH('o', 'o'):
1201 return (cpp_demangle_read_expression_binary(ddata
, "||", 2));
1203 case SIMPLE_HASH('o', 'r'):
1206 return (cpp_demangle_read_expression_binary(ddata
, "|", 1));
1208 case SIMPLE_HASH('o', 'R'):
1211 return (cpp_demangle_read_expression_binary(ddata
, "|=", 2));
1213 case SIMPLE_HASH('p', 'l'):
1216 return (cpp_demangle_read_expression_binary(ddata
, "+", 1));
1218 case SIMPLE_HASH('p', 'L'):
1221 return (cpp_demangle_read_expression_binary(ddata
, "+=", 2));
1223 case SIMPLE_HASH('p', 'm'):
1226 return (cpp_demangle_read_expression_binary(ddata
, "->*", 3));
1228 case SIMPLE_HASH('p', 'p'):
1231 return (cpp_demangle_read_expression_binary(ddata
, "++", 2));
1233 case SIMPLE_HASH('p', 's'):
1234 /* operator + (unary) */
1236 return (cpp_demangle_read_expression_unary(ddata
, "+", 1));
1238 case SIMPLE_HASH('p', 't'):
1241 return (cpp_demangle_read_expression_binary(ddata
, "->", 2));
1243 case SIMPLE_HASH('q', 'u'):
1246 return (cpp_demangle_read_expression_trinary(ddata
, "?", 1,
1249 case SIMPLE_HASH('r', 'm'):
1252 return (cpp_demangle_read_expression_binary(ddata
, "%", 1));
1254 case SIMPLE_HASH('r', 'M'):
1257 return (cpp_demangle_read_expression_binary(ddata
, "%=", 2));
1259 case SIMPLE_HASH('r', 's'):
1262 return (cpp_demangle_read_expression_binary(ddata
, ">>", 2));
1264 case SIMPLE_HASH('r', 'S'):
1267 return (cpp_demangle_read_expression_binary(ddata
, ">>=", 3));
1269 case SIMPLE_HASH('r', 'z'):
1270 /* operator sizeof */
1272 return (cpp_demangle_read_expression_unary(ddata
, "sizeof", 6));
1274 case SIMPLE_HASH('s', 'v'):
1275 /* operator sizeof */
1277 return (cpp_demangle_read_expression_unary(ddata
, "sizeof", 6));
1280 switch (*ddata
->cur
) {
1282 return (cpp_demangle_read_expr_primary(ddata
));
1284 return (cpp_demangle_read_tmpl_param(ddata
));
1291 cpp_demangle_read_expression_binary(struct cpp_demangle_data
*ddata
,
1292 const char *name
, size_t len
)
1295 if (ddata
== NULL
|| name
== NULL
|| len
== 0)
1297 if (!cpp_demangle_read_expression(ddata
))
1299 if (!cpp_demangle_push_str(ddata
, name
, len
))
1302 return (cpp_demangle_read_expression(ddata
));
1306 cpp_demangle_read_expression_unary(struct cpp_demangle_data
*ddata
,
1307 const char *name
, size_t len
)
1310 if (ddata
== NULL
|| name
== NULL
|| len
== 0)
1312 if (!cpp_demangle_read_expression(ddata
))
1315 return (cpp_demangle_push_str(ddata
, name
, len
));
1319 cpp_demangle_read_expression_trinary(struct cpp_demangle_data
*ddata
,
1320 const char *name1
, size_t len1
, const char *name2
, size_t len2
)
1323 if (ddata
== NULL
|| name1
== NULL
|| len1
== 0 || name2
== NULL
||
1327 if (!cpp_demangle_read_expression(ddata
))
1329 if (!cpp_demangle_push_str(ddata
, name1
, len1
))
1331 if (!cpp_demangle_read_expression(ddata
))
1333 if (!cpp_demangle_push_str(ddata
, name2
, len2
))
1336 return (cpp_demangle_read_expression(ddata
));
1340 cpp_demangle_read_function(struct cpp_demangle_data
*ddata
, int *ext_c
,
1341 struct vector_type_qualifier
*v
)
1343 size_t class_type_size
, class_type_len
, limit
;
1344 const char *class_type
;
1346 if (ddata
== NULL
|| *ddata
->cur
!= 'F' || v
== NULL
)
1350 if (*ddata
->cur
== 'Y') {
1355 if (!cpp_demangle_read_type(ddata
, 0))
1357 if (*ddata
->cur
!= 'E') {
1358 if (!cpp_demangle_push_str(ddata
, "(", 1))
1360 if (vector_read_cmd_find(&ddata
->cmd
, READ_PTRMEM
)) {
1361 if ((class_type_size
= ddata
->class_type
.size
) == 0)
1364 ddata
->class_type
.container
[class_type_size
- 1];
1365 if (class_type
== NULL
)
1367 if ((class_type_len
= strlen(class_type
)) == 0)
1369 if (!cpp_demangle_push_str(ddata
, class_type
,
1372 if (!cpp_demangle_push_str(ddata
, "::*", 3))
1376 if (!cpp_demangle_push_type_qualifier(ddata
, v
,
1377 (const char *) NULL
))
1379 vector_type_qualifier_dest(v
);
1380 if (!vector_type_qualifier_init(v
))
1384 if (!cpp_demangle_push_str(ddata
, ")(", 2))
1389 if (!cpp_demangle_read_type(ddata
, 0))
1391 if (*ddata
->cur
== 'E')
1393 if (limit
++ > CPP_DEMANGLE_TRY_LIMIT
)
1397 if (vector_read_cmd_find(&ddata
->cmd
, READ_PTRMEM
) == 1) {
1398 if (!cpp_demangle_push_type_qualifier(ddata
, v
,
1399 (const char *) NULL
))
1401 vector_type_qualifier_dest(v
);
1402 if (!vector_type_qualifier_init(v
))
1406 if (!cpp_demangle_push_str(ddata
, ")", 1))
1415 /* read encoding, encoding are function name, data name, special-name */
1417 cpp_demangle_read_encoding(struct cpp_demangle_data
*ddata
)
1420 if (ddata
== NULL
|| *ddata
->cur
== '\0')
1424 switch (SIMPLE_HASH(*ddata
->cur
, *(ddata
->cur
+ 1))) {
1425 case SIMPLE_HASH('G', 'V'):
1426 /* sentry object for 1 time init */
1427 if (!cpp_demangle_push_str(ddata
, "guard variable for ", 20))
1432 case SIMPLE_HASH('T', 'c'):
1433 /* virtual function covariant override thunk */
1434 if (!cpp_demangle_push_str(ddata
,
1435 "virtual function covariant override ", 36))
1438 if (*ddata
->cur
== '\0')
1440 if (!cpp_demangle_read_offset(ddata
))
1442 if (!cpp_demangle_read_offset(ddata
))
1444 return (cpp_demangle_read_encoding(ddata
));
1446 case SIMPLE_HASH('T', 'D'):
1447 /* typeinfo common proxy */
1450 case SIMPLE_HASH('T', 'h'):
1451 /* virtual function non-virtual override thunk */
1452 if (cpp_demangle_push_str(ddata
,
1453 "virtual function non-virtual override ", 38) == 0)
1456 if (*ddata
->cur
== '\0')
1458 if (!cpp_demangle_read_nv_offset(ddata
))
1460 return (cpp_demangle_read_encoding(ddata
));
1462 case SIMPLE_HASH('T', 'I'):
1463 /* typeinfo structure */
1465 case SIMPLE_HASH('T', 'S'):
1466 /* RTTI name (NTBS) */
1467 if (!cpp_demangle_push_str(ddata
, "typeinfo for ", 14))
1470 if (*ddata
->cur
== '\0')
1472 return (cpp_demangle_read_type(ddata
, 1));
1474 case SIMPLE_HASH('T', 'T'):
1476 if (!cpp_demangle_push_str(ddata
, "VTT for ", 8))
1479 return (cpp_demangle_read_type(ddata
, 1));
1481 case SIMPLE_HASH('T', 'v'):
1482 /* virtual function virtual override thunk */
1483 if (!cpp_demangle_push_str(ddata
,
1484 "virtual function virtual override ", 34))
1487 if (*ddata
->cur
== '\0')
1489 if (!cpp_demangle_read_v_offset(ddata
))
1491 return (cpp_demangle_read_encoding(ddata
));
1493 case SIMPLE_HASH('T', 'V'):
1495 if (!cpp_demangle_push_str(ddata
, "vtable for ", 12))
1498 if (*ddata
->cur
== '\0')
1500 return (cpp_demangle_read_type(ddata
, 1));
1503 return (cpp_demangle_read_name(ddata
));
1507 cpp_demangle_read_local_name(struct cpp_demangle_data
*ddata
)
1513 if (*(++ddata
->cur
) == '\0')
1515 if (!cpp_demangle_read_encoding(ddata
))
1520 if (!cpp_demangle_read_type(ddata
, 1))
1522 if (*ddata
->cur
== 'E')
1524 if (limit
++ > CPP_DEMANGLE_TRY_LIMIT
)
1527 if (*(++ddata
->cur
) == '\0')
1529 if (ddata
->paren
== true) {
1530 if (!cpp_demangle_push_str(ddata
, ")", 1))
1532 ddata
->paren
= false;
1534 if (*ddata
->cur
== 's')
1537 if (!cpp_demangle_push_str(ddata
, "::", 2))
1539 if (!cpp_demangle_read_name(ddata
))
1542 if (*ddata
->cur
== '_') {
1544 while (ELFTC_ISDIGIT(*ddata
->cur
) != 0)
1552 cpp_demangle_read_name(struct cpp_demangle_data
*ddata
)
1554 struct vector_str
*output
, v
;
1555 size_t p_idx
, subst_str_len
;
1559 if (ddata
== NULL
|| *ddata
->cur
== '\0')
1562 output
= ddata
->push_head
> 0 ? &ddata
->output_tmp
: &ddata
->output
;
1566 switch (*ddata
->cur
) {
1568 return (cpp_demangle_read_subst(ddata
));
1570 return (cpp_demangle_read_nested_name(ddata
));
1572 return (cpp_demangle_read_local_name(ddata
));
1575 if (!vector_str_init(&v
))
1578 p_idx
= output
->size
;
1580 if (!cpp_demangle_read_uqname(ddata
))
1582 if ((subst_str
= vector_str_substr(output
, p_idx
, output
->size
- 1,
1583 &subst_str_len
)) == NULL
)
1585 if (subst_str_len
> 8 && strstr(subst_str
, "operator") != NULL
) {
1589 if (!vector_str_push(&v
, subst_str
, subst_str_len
))
1591 if (!cpp_demangle_push_subst_v(ddata
, &v
))
1594 if (*ddata
->cur
== 'I') {
1595 p_idx
= output
->size
;
1596 if (!cpp_demangle_read_tmpl_args(ddata
))
1599 if ((subst_str
= vector_str_substr(output
, p_idx
,
1600 output
->size
- 1, &subst_str_len
)) == NULL
)
1602 if (!vector_str_push(&v
, subst_str
, subst_str_len
))
1604 if (!cpp_demangle_push_subst_v(ddata
, &v
))
1612 vector_str_dest(&v
);
1618 cpp_demangle_read_nested_name(struct cpp_demangle_data
*ddata
)
1620 struct vector_str
*output
, v
;
1621 size_t limit
, p_idx
, subst_str_len
;
1625 if (ddata
== NULL
|| *ddata
->cur
!= 'N')
1627 if (*(++ddata
->cur
) == '\0')
1630 while (*ddata
->cur
== 'r' || *ddata
->cur
== 'V' ||
1631 *ddata
->cur
== 'K') {
1632 switch (*ddata
->cur
) {
1634 ddata
->mem_rst
= true;
1637 ddata
->mem_vat
= true;
1640 ddata
->mem_cst
= true;
1646 output
= ddata
->push_head
> 0 ? &ddata
->output_tmp
: &ddata
->output
;
1647 if (!vector_str_init(&v
))
1653 p_idx
= output
->size
;
1654 switch (*ddata
->cur
) {
1656 if (!cpp_demangle_read_tmpl_args(ddata
))
1660 if (!cpp_demangle_read_subst(ddata
))
1664 if (!cpp_demangle_read_tmpl_param(ddata
))
1668 if (!cpp_demangle_read_uqname(ddata
))
1672 if ((subst_str
= vector_str_substr(output
, p_idx
,
1673 output
->size
- 1, &subst_str_len
)) == NULL
)
1675 if (!vector_str_push(&v
, subst_str
, subst_str_len
)) {
1681 if (!cpp_demangle_push_subst_v(ddata
, &v
))
1683 if (*ddata
->cur
== 'E')
1685 else if (*ddata
->cur
!= 'I' &&
1686 *ddata
->cur
!= 'C' && *ddata
->cur
!= 'D') {
1687 if (!cpp_demangle_push_str(ddata
, "::", 2))
1689 if (!vector_str_push(&v
, "::", 2))
1692 if (limit
++ > CPP_DEMANGLE_TRY_LIMIT
)
1700 vector_str_dest(&v
);
1707 * number ::= [n] <decimal>
1710 cpp_demangle_read_number(struct cpp_demangle_data
*ddata
, long *rtn
)
1712 long len
, negative_factor
;
1714 if (ddata
== NULL
|| rtn
== NULL
)
1717 negative_factor
= 1;
1718 if (*ddata
->cur
== 'n') {
1719 negative_factor
= -1;
1723 if (ELFTC_ISDIGIT(*ddata
->cur
) == 0)
1727 if ((len
= strtol(ddata
->cur
, (char **) NULL
, 10)) == 0 &&
1731 while (ELFTC_ISDIGIT(*ddata
->cur
) != 0)
1735 assert(negative_factor
== 1 || negative_factor
== -1);
1737 *rtn
= len
* negative_factor
;
1743 cpp_demangle_read_nv_offset(struct cpp_demangle_data
*ddata
)
1749 if (!cpp_demangle_push_str(ddata
, "offset : ", 9))
1752 return (cpp_demangle_read_offset_number(ddata
));
1755 /* read offset, offset are nv-offset, v-offset */
1757 cpp_demangle_read_offset(struct cpp_demangle_data
*ddata
)
1763 if (*ddata
->cur
== 'h') {
1765 return (cpp_demangle_read_nv_offset(ddata
));
1766 } else if (*ddata
->cur
== 'v') {
1768 return (cpp_demangle_read_v_offset(ddata
));
1775 cpp_demangle_read_offset_number(struct cpp_demangle_data
*ddata
)
1780 if (ddata
== NULL
|| *ddata
->cur
== '\0')
1783 /* offset could be negative */
1784 if (*ddata
->cur
== 'n') {
1786 start
= ddata
->cur
+ 1;
1792 while (*ddata
->cur
!= '_')
1795 if (negative
&& !cpp_demangle_push_str(ddata
, "-", 1))
1798 assert(start
!= NULL
);
1800 if (!cpp_demangle_push_str(ddata
, start
, ddata
->cur
- start
))
1802 if (!cpp_demangle_push_str(ddata
, " ", 1))
1811 cpp_demangle_read_pointer_to_member(struct cpp_demangle_data
*ddata
)
1813 size_t class_type_len
, i
, idx
, p_idx
;
1814 int p_func_type
, rtn
;
1817 if (ddata
== NULL
|| *ddata
->cur
!= 'M' || *(++ddata
->cur
) == '\0')
1820 p_idx
= ddata
->output
.size
;
1821 if (!cpp_demangle_read_type(ddata
, 0))
1824 if ((class_type
= vector_str_substr(&ddata
->output
, p_idx
,
1825 ddata
->output
.size
- 1, &class_type_len
)) == NULL
)
1829 idx
= ddata
->output
.size
;
1830 for (i
= p_idx
; i
< idx
; ++i
)
1831 if (!vector_str_pop(&ddata
->output
))
1834 if (!vector_read_cmd_push(&ddata
->cmd
, READ_PTRMEM
))
1837 if (!vector_str_push(&ddata
->class_type
, class_type
, class_type_len
))
1840 p_func_type
= ddata
->func_type
;
1841 if (!cpp_demangle_read_type(ddata
, 0))
1844 if (p_func_type
== ddata
->func_type
) {
1845 if (!cpp_demangle_push_str(ddata
, " ", 1))
1847 if (!cpp_demangle_push_str(ddata
, class_type
, class_type_len
))
1849 if (!cpp_demangle_push_str(ddata
, "::*", 3))
1855 if (!vector_str_pop(&ddata
->class_type
))
1858 if (!vector_read_cmd_pop(&ddata
->cmd
))
1866 /* read source-name, source-name is <len> <ID> */
1868 cpp_demangle_read_sname(struct cpp_demangle_data
*ddata
)
1872 if (ddata
== NULL
|| cpp_demangle_read_number(ddata
, &len
) == 0 ||
1873 len
<= 0 || cpp_demangle_push_str(ddata
, ddata
->cur
, len
) == 0)
1876 assert(ddata
->output
.size
> 0);
1877 if (vector_read_cmd_find(&ddata
->cmd
, READ_TMPL
) == 0)
1879 ddata
->output
.container
[ddata
->output
.size
- 1];
1887 cpp_demangle_read_subst(struct cpp_demangle_data
*ddata
)
1891 if (ddata
== NULL
|| *ddata
->cur
== '\0')
1894 /* abbreviations of the form Sx */
1895 switch (SIMPLE_HASH(*ddata
->cur
, *(ddata
->cur
+ 1))) {
1896 case SIMPLE_HASH('S', 'a'):
1897 /* std::allocator */
1898 if (cpp_demangle_push_str(ddata
, "std::allocator", 14) == 0)
1901 if (*ddata
->cur
== 'I')
1902 return (cpp_demangle_read_subst_stdtmpl(ddata
,
1903 "std::allocator", 14));
1906 case SIMPLE_HASH('S', 'b'):
1907 /* std::basic_string */
1908 if (!cpp_demangle_push_str(ddata
, "std::basic_string", 17))
1911 if (*ddata
->cur
== 'I')
1912 return (cpp_demangle_read_subst_stdtmpl(ddata
,
1913 "std::basic_string", 17));
1916 case SIMPLE_HASH('S', 'd'):
1917 /* std::basic_iostream<char, std::char_traits<char> > */
1918 if (!cpp_demangle_push_str(ddata
, "std::iostream", 19))
1920 ddata
->last_sname
= "iostream";
1922 if (*ddata
->cur
== 'I')
1923 return (cpp_demangle_read_subst_stdtmpl(ddata
,
1924 "std::iostream", 19));
1927 case SIMPLE_HASH('S', 'i'):
1928 /* std::basic_istream<char, std::char_traits<char> > */
1929 if (!cpp_demangle_push_str(ddata
, "std::istream", 18))
1931 ddata
->last_sname
= "istream";
1933 if (*ddata
->cur
== 'I')
1934 return (cpp_demangle_read_subst_stdtmpl(ddata
,
1935 "std::istream", 18));
1938 case SIMPLE_HASH('S', 'o'):
1939 /* std::basic_ostream<char, std::char_traits<char> > */
1940 if (!cpp_demangle_push_str(ddata
, "std::ostream", 18))
1942 ddata
->last_sname
= "istream";
1944 if (*ddata
->cur
== 'I')
1945 return (cpp_demangle_read_subst_stdtmpl(ddata
,
1946 "std::ostream", 18));
1949 case SIMPLE_HASH('S', 's'):
1951 * std::basic_string<char, std::char_traits<char>,
1952 * std::allocator<char> >
1956 if (!cpp_demangle_push_str(ddata
, "std::string", 11))
1958 ddata
->last_sname
= "string";
1960 if (*ddata
->cur
== 'I')
1961 return (cpp_demangle_read_subst_stdtmpl(ddata
,
1962 "std::string", 11));
1965 case SIMPLE_HASH('S', 't'):
1967 return (cpp_demangle_read_subst_std(ddata
));
1970 if (*(++ddata
->cur
) == '\0')
1974 if (*ddata
->cur
== '_')
1975 return (cpp_demangle_get_subst(ddata
, 0));
1978 /* substitution number is base 36 */
1979 if ((nth
= strtol(ddata
->cur
, (char **) NULL
, 36)) == 0 &&
1983 /* first was '_', so increase one */
1986 while (*ddata
->cur
!= '_')
1991 return (cpp_demangle_get_subst(ddata
, nth
));
1999 cpp_demangle_read_subst_std(struct cpp_demangle_data
*ddata
)
2001 struct vector_str
*output
, v
;
2002 size_t p_idx
, subst_str_len
;
2009 if (!vector_str_init(&v
))
2014 if (!cpp_demangle_push_str(ddata
, "std::", 5))
2017 if (!vector_str_push(&v
, "std::", 5))
2022 output
= ddata
->push_head
> 0 ? &ddata
->output_tmp
: &ddata
->output
;
2024 p_idx
= output
->size
;
2025 if (!cpp_demangle_read_uqname(ddata
))
2028 if ((subst_str
= vector_str_substr(output
, p_idx
, output
->size
- 1,
2029 &subst_str_len
)) == NULL
)
2032 if (!vector_str_push(&v
, subst_str
, subst_str_len
))
2035 if (!cpp_demangle_push_subst_v(ddata
, &v
))
2038 if (*ddata
->cur
== 'I') {
2039 p_idx
= output
->size
;
2040 if (!cpp_demangle_read_tmpl_args(ddata
))
2043 if ((subst_str
= vector_str_substr(output
, p_idx
,
2044 output
->size
- 1, &subst_str_len
)) == NULL
)
2046 if (!vector_str_push(&v
, subst_str
, subst_str_len
))
2048 if (!cpp_demangle_push_subst_v(ddata
, &v
))
2055 vector_str_dest(&v
);
2061 cpp_demangle_read_subst_stdtmpl(struct cpp_demangle_data
*ddata
,
2062 const char *str
, size_t len
)
2064 struct vector_str
*output
;
2065 size_t p_idx
, substr_len
;
2067 char *subst_str
, *substr
;
2069 if (ddata
== NULL
|| str
== NULL
|| len
== 0)
2072 output
= ddata
->push_head
> 0 ? &ddata
->output_tmp
: &ddata
->output
;
2074 p_idx
= output
->size
;
2078 if (!cpp_demangle_read_tmpl_args(ddata
))
2080 if ((substr
= vector_str_substr(output
, p_idx
, output
->size
- 1,
2081 &substr_len
)) == NULL
)
2085 if ((subst_str
= malloc(sizeof(char) * (substr_len
+ len
+ 1))) ==
2089 memcpy(subst_str
, str
, len
);
2090 memcpy(subst_str
+ len
, substr
, substr_len
);
2091 subst_str
[substr_len
+ len
] = '\0';
2093 if (!cpp_demangle_push_subst(ddata
, subst_str
, substr_len
+ len
))
2105 cpp_demangle_read_tmpl_arg(struct cpp_demangle_data
*ddata
)
2108 if (ddata
== NULL
|| *ddata
->cur
== '\0')
2111 switch (*ddata
->cur
) {
2113 return (cpp_demangle_read_expr_primary(ddata
));
2115 return (cpp_demangle_read_expression(ddata
));
2118 return (cpp_demangle_read_type(ddata
, 0));
2122 cpp_demangle_read_tmpl_args(struct cpp_demangle_data
*ddata
)
2124 struct vector_str
*v
;
2125 size_t arg_len
, idx
, limit
, size
;
2128 if (ddata
== NULL
|| *ddata
->cur
== '\0')
2133 if (!vector_read_cmd_push(&ddata
->cmd
, READ_TMPL
))
2136 if (!cpp_demangle_push_str(ddata
, "<", 1))
2140 v
= ddata
->push_head
> 0 ? &ddata
->output_tmp
: &ddata
->output
;
2143 if (!cpp_demangle_read_tmpl_arg(ddata
))
2145 if ((arg
= vector_str_substr(v
, idx
, v
->size
- 1, &arg_len
)) ==
2148 if (!vector_str_find(&ddata
->tmpl
, arg
, arg_len
) &&
2149 !vector_str_push(&ddata
->tmpl
, arg
, arg_len
)) {
2156 if (*ddata
->cur
== 'E') {
2160 if (!strncmp(v
->container
[size
- 1], ">", 1)) {
2161 if (!cpp_demangle_push_str(ddata
, " >", 2))
2163 } else if (!cpp_demangle_push_str(ddata
, ">", 1))
2166 } else if (*ddata
->cur
!= 'I' &&
2167 !cpp_demangle_push_str(ddata
, ", ", 2))
2170 if (limit
++ > CPP_DEMANGLE_TRY_LIMIT
)
2174 return (vector_read_cmd_pop(&ddata
->cmd
));
2178 * Read template parameter that forms in 'T[number]_'.
2179 * This function much like to read_subst but only for types.
2182 cpp_demangle_read_tmpl_param(struct cpp_demangle_data
*ddata
)
2186 if (ddata
== NULL
|| *ddata
->cur
!= 'T')
2191 if (*ddata
->cur
== '_')
2192 return (cpp_demangle_get_tmpl_param(ddata
, 0));
2196 if ((nth
= strtol(ddata
->cur
, (char **) NULL
, 36)) == 0 &&
2203 while (*ddata
->cur
!= '_')
2208 return (cpp_demangle_get_tmpl_param(ddata
, nth
));
2216 cpp_demangle_read_type(struct cpp_demangle_data
*ddata
, int delimit
)
2218 struct vector_type_qualifier v
;
2219 struct vector_str
*output
;
2220 size_t p_idx
, type_str_len
;
2221 int extern_c
, is_builtin
;
2228 output
= &ddata
->output
;
2229 if (ddata
->output
.size
> 0 && !strncmp(ddata
->output
.container
[ddata
->output
.size
- 1], ">", 1)) {
2231 output
= &ddata
->output_tmp
;
2232 } else if (delimit
== 1) {
2233 if (ddata
->paren
== false) {
2234 if (!cpp_demangle_push_str(ddata
, "(", 1))
2236 if (ddata
->output
.size
< 2)
2238 ddata
->paren
= true;
2239 ddata
->pfirst
= true;
2240 /* Need pop function name */
2241 if (ddata
->subst
.size
== 1 &&
2242 !vector_str_pop(&ddata
->subst
))
2247 ddata
->pfirst
= false;
2248 else if (*ddata
->cur
!= 'I' &&
2249 !cpp_demangle_push_str(ddata
, ", ", 2))
2253 assert(output
!= NULL
);
2255 * [r, V, K] [P, R, C, G, U] builtin, function, class-enum, array
2256 * pointer-to-member, template-param, template-template-param, subst
2259 if (!vector_type_qualifier_init(&v
))
2264 p_idx
= output
->size
;
2268 switch (*ddata
->cur
) {
2271 if (!cpp_demangle_push_str(ddata
, "signed char", 11))
2278 if (!cpp_demangle_read_array(ddata
))
2285 if (!cpp_demangle_push_str(ddata
, "bool", 4))
2292 if (!vector_type_qualifier_push(&v
, TYPE_CMX
))
2299 if (!cpp_demangle_push_str(ddata
, "char", 4))
2306 if (!cpp_demangle_push_str(ddata
, "double", 6))
2313 if (!cpp_demangle_push_str(ddata
, "long double", 11))
2320 if (!cpp_demangle_push_str(ddata
, "float", 5))
2327 if (!cpp_demangle_read_function(ddata
, &extern_c
, &v
))
2334 if (!cpp_demangle_push_str(ddata
, "__float128", 10))
2341 if (!vector_type_qualifier_push(&v
, TYPE_IMG
))
2348 if (!cpp_demangle_push_str(ddata
, "unsigned char", 13))
2355 if (!cpp_demangle_push_str(ddata
, "int", 3))
2362 if (!cpp_demangle_push_str(ddata
, "unsigned int", 12))
2369 if (!vector_type_qualifier_push(&v
, TYPE_CST
))
2376 if (!cpp_demangle_push_str(ddata
, "long", 4))
2383 if (!cpp_demangle_push_str(ddata
, "unsigned long", 13))
2390 /* pointer to member */
2391 if (!cpp_demangle_read_pointer_to_member(ddata
))
2398 if (!cpp_demangle_push_str(ddata
, "__int128", 8))
2404 /* unsigned __int128 */
2405 if (!cpp_demangle_push_str(ddata
, "unsigned _;int128", 17))
2412 if (!vector_type_qualifier_push(&v
, TYPE_PTR
))
2419 if (!vector_type_qualifier_push(&v
, TYPE_RST
))
2426 if (!vector_type_qualifier_push(&v
, TYPE_REF
))
2432 /* short, local string */
2433 if (!cpp_demangle_push_str(ddata
, "short", 5))
2440 if (!cpp_demangle_read_subst(ddata
))
2446 /* unsigned short */
2447 if (!cpp_demangle_push_str(ddata
, "unsigned short", 14))
2453 /* template parameter */
2454 if (!cpp_demangle_read_tmpl_param(ddata
))
2460 /* vendor extended builtin */
2462 if (!cpp_demangle_read_sname(ddata
))
2468 /* vendor extended type qualifier */
2469 if (!cpp_demangle_read_number(ddata
, &len
))
2473 if (!vector_str_push(&v
.ext_name
, ddata
->cur
, len
))
2480 if (!cpp_demangle_push_str(ddata
, "void", 4))
2487 if (!vector_type_qualifier_push(&v
, TYPE_VAT
))
2494 if (!cpp_demangle_push_str(ddata
, "wchar_t", 6))
2501 if (!cpp_demangle_push_str(ddata
, "long long", 9))
2507 /* unsigned long long */
2508 if (!cpp_demangle_push_str(ddata
, "unsigned long long", 18))
2515 if (!cpp_demangle_push_str(ddata
, "ellipsis", 8))
2521 if (!cpp_demangle_read_name(ddata
))
2526 if ((type_str
= vector_str_substr(output
, p_idx
, output
->size
- 1,
2527 &type_str_len
)) == NULL
)
2530 if (is_builtin
== 0) {
2531 if (!vector_str_find(&ddata
->subst
, type_str
, type_str_len
) &&
2532 !vector_str_push(&ddata
->subst
, type_str
, type_str_len
))
2536 if (!cpp_demangle_push_type_qualifier(ddata
, &v
, type_str
))
2540 vector_type_qualifier_dest(&v
);
2542 if (ddata
->push_head
> 0) {
2543 if (*ddata
->cur
== 'I' && cpp_demangle_read_tmpl_args(ddata
)
2547 if (--ddata
->push_head
> 0)
2550 if (!vector_str_push(&ddata
->output_tmp
, " ", 1))
2553 if (!vector_str_push_vector_head(&ddata
->output
,
2554 &ddata
->output_tmp
))
2557 vector_str_dest(&ddata
->output_tmp
);
2558 if (!vector_str_init(&ddata
->output_tmp
))
2561 if (!cpp_demangle_push_str(ddata
, "(", 1))
2564 ddata
->paren
= true;
2565 ddata
->pfirst
= true;
2571 vector_type_qualifier_dest(&v
);
2577 * read unqualified-name, unqualified name are operator-name, ctor-dtor-name,
2581 cpp_demangle_read_uqname(struct cpp_demangle_data
*ddata
)
2585 if (ddata
== NULL
|| *ddata
->cur
== '\0')
2589 switch (SIMPLE_HASH(*ddata
->cur
, *(ddata
->cur
+ 1))) {
2590 case SIMPLE_HASH('a', 'a'):
2592 if (!cpp_demangle_push_str(ddata
, "operator&&", 10))
2597 case SIMPLE_HASH('a', 'd'):
2598 /* operator & (unary) */
2599 if (!cpp_demangle_push_str(ddata
, "operator&", 9))
2604 case SIMPLE_HASH('a', 'n'):
2606 if (!cpp_demangle_push_str(ddata
, "operator&", 9))
2611 case SIMPLE_HASH('a', 'N'):
2613 if (!cpp_demangle_push_str(ddata
, "operator&=", 10))
2618 case SIMPLE_HASH('a', 'S'):
2620 if (!cpp_demangle_push_str(ddata
, "operator=", 9))
2625 case SIMPLE_HASH('c', 'l'):
2627 if (!cpp_demangle_push_str(ddata
, "operator()", 10))
2632 case SIMPLE_HASH('c', 'm'):
2634 if (!cpp_demangle_push_str(ddata
, "operator,", 9))
2639 case SIMPLE_HASH('c', 'o'):
2641 if (!cpp_demangle_push_str(ddata
, "operator~", 9))
2646 case SIMPLE_HASH('c', 'v'):
2647 /* operator (cast) */
2648 if (!cpp_demangle_push_str(ddata
, "operator(cast)", 14))
2651 return (cpp_demangle_read_type(ddata
, 1));
2653 case SIMPLE_HASH('d', 'a'):
2654 /* operator delete [] */
2655 if (!cpp_demangle_push_str(ddata
, "operator delete []", 18))
2660 case SIMPLE_HASH('d', 'e'):
2661 /* operator * (unary) */
2662 if (!cpp_demangle_push_str(ddata
, "operator*", 9))
2667 case SIMPLE_HASH('d', 'l'):
2668 /* operator delete */
2669 if (!cpp_demangle_push_str(ddata
, "operator delete", 15))
2674 case SIMPLE_HASH('d', 'v'):
2676 if (!cpp_demangle_push_str(ddata
, "operator/", 9))
2681 case SIMPLE_HASH('d', 'V'):
2683 if (!cpp_demangle_push_str(ddata
, "operator/=", 10))
2688 case SIMPLE_HASH('e', 'o'):
2690 if (!cpp_demangle_push_str(ddata
, "operator^", 9))
2695 case SIMPLE_HASH('e', 'O'):
2697 if (!cpp_demangle_push_str(ddata
, "operator^=", 10))
2702 case SIMPLE_HASH('e', 'q'):
2704 if (!cpp_demangle_push_str(ddata
, "operator==", 10))
2709 case SIMPLE_HASH('g', 'e'):
2711 if (!cpp_demangle_push_str(ddata
, "operator>=", 10))
2716 case SIMPLE_HASH('g', 't'):
2718 if (!cpp_demangle_push_str(ddata
, "operator>", 9))
2723 case SIMPLE_HASH('i', 'x'):
2725 if (!cpp_demangle_push_str(ddata
, "operator[]", 10))
2730 case SIMPLE_HASH('l', 'e'):
2732 if (!cpp_demangle_push_str(ddata
, "operator<=", 10))
2737 case SIMPLE_HASH('l', 's'):
2739 if (!cpp_demangle_push_str(ddata
, "operator<<", 10))
2744 case SIMPLE_HASH('l', 'S'):
2746 if (!cpp_demangle_push_str(ddata
, "operator<<=", 11))
2751 case SIMPLE_HASH('l', 't'):
2753 if (!cpp_demangle_push_str(ddata
, "operator<", 9))
2758 case SIMPLE_HASH('m', 'i'):
2760 if (!cpp_demangle_push_str(ddata
, "operator-", 9))
2765 case SIMPLE_HASH('m', 'I'):
2767 if (!cpp_demangle_push_str(ddata
, "operator-=", 10))
2772 case SIMPLE_HASH('m', 'l'):
2774 if (!cpp_demangle_push_str(ddata
, "operator*", 9))
2779 case SIMPLE_HASH('m', 'L'):
2781 if (!cpp_demangle_push_str(ddata
, "operator*=", 10))
2786 case SIMPLE_HASH('m', 'm'):
2788 if (!cpp_demangle_push_str(ddata
, "operator--", 10))
2793 case SIMPLE_HASH('n', 'a'):
2794 /* operator new[] */
2795 if (!cpp_demangle_push_str(ddata
, "operator new []", 15))
2800 case SIMPLE_HASH('n', 'e'):
2802 if (!cpp_demangle_push_str(ddata
, "operator!=", 10))
2807 case SIMPLE_HASH('n', 'g'):
2808 /* operator - (unary) */
2809 if (!cpp_demangle_push_str(ddata
, "operator-", 9))
2814 case SIMPLE_HASH('n', 't'):
2816 if (!cpp_demangle_push_str(ddata
, "operator!", 9))
2821 case SIMPLE_HASH('n', 'w'):
2823 if (!cpp_demangle_push_str(ddata
, "operator new", 12))
2828 case SIMPLE_HASH('o', 'o'):
2830 if (!cpp_demangle_push_str(ddata
, "operator||", 10))
2835 case SIMPLE_HASH('o', 'r'):
2837 if (!cpp_demangle_push_str(ddata
, "operator|", 9))
2842 case SIMPLE_HASH('o', 'R'):
2844 if (!cpp_demangle_push_str(ddata
, "operator|=", 10))
2849 case SIMPLE_HASH('p', 'l'):
2851 if (!cpp_demangle_push_str(ddata
, "operator+", 9))
2856 case SIMPLE_HASH('p', 'L'):
2858 if (!cpp_demangle_push_str(ddata
, "operator+=", 10))
2863 case SIMPLE_HASH('p', 'm'):
2865 if (!cpp_demangle_push_str(ddata
, "operator->*", 11))
2870 case SIMPLE_HASH('p', 'p'):
2872 if (!cpp_demangle_push_str(ddata
, "operator++", 10))
2877 case SIMPLE_HASH('p', 's'):
2878 /* operator + (unary) */
2879 if (!cpp_demangle_push_str(ddata
, "operator+", 9))
2884 case SIMPLE_HASH('p', 't'):
2886 if (!cpp_demangle_push_str(ddata
, "operator->", 10))
2891 case SIMPLE_HASH('q', 'u'):
2893 if (!cpp_demangle_push_str(ddata
, "operator?", 9))
2898 case SIMPLE_HASH('r', 'm'):
2900 if (!cpp_demangle_push_str(ddata
, "operator%", 9))
2905 case SIMPLE_HASH('r', 'M'):
2907 if (!cpp_demangle_push_str(ddata
, "operator%=", 10))
2912 case SIMPLE_HASH('r', 's'):
2914 if (!cpp_demangle_push_str(ddata
, "operator>>", 10))
2919 case SIMPLE_HASH('r', 'S'):
2921 if (!cpp_demangle_push_str(ddata
, "operator>>=", 11))
2926 case SIMPLE_HASH('r', 'z'):
2927 /* operator sizeof */
2928 if (!cpp_demangle_push_str(ddata
, "operator sizeof ", 16))
2933 case SIMPLE_HASH('s', 'r'):
2934 /* scope resolution operator */
2935 if (!cpp_demangle_push_str(ddata
, "scope resolution operator ",
2941 case SIMPLE_HASH('s', 'v'):
2942 /* operator sizeof */
2943 if (!cpp_demangle_push_str(ddata
, "operator sizeof ", 16))
2949 /* vendor extened operator */
2950 if (*ddata
->cur
== 'v' && ELFTC_ISDIGIT(*(ddata
->cur
+ 1))) {
2951 if (!cpp_demangle_push_str(ddata
, "vendor extened operator ",
2954 if (!cpp_demangle_push_str(ddata
, ddata
->cur
+ 1, 1))
2957 return (cpp_demangle_read_sname(ddata
));
2960 /* ctor-dtor-name */
2961 switch (SIMPLE_HASH(*ddata
->cur
, *(ddata
->cur
+ 1))) {
2962 case SIMPLE_HASH('C', '1'):
2964 case SIMPLE_HASH('C', '2'):
2966 case SIMPLE_HASH('C', '3'):
2967 if (ddata
->last_sname
== NULL
)
2969 if ((len
= strlen(ddata
->last_sname
)) == 0)
2971 if (!cpp_demangle_push_str(ddata
, "::", 2))
2973 if (!cpp_demangle_push_str(ddata
, ddata
->last_sname
, len
))
2978 case SIMPLE_HASH('D', '0'):
2980 case SIMPLE_HASH('D', '1'):
2982 case SIMPLE_HASH('D', '2'):
2983 if (ddata
->last_sname
== NULL
)
2985 if ((len
= strlen(ddata
->last_sname
)) == 0)
2987 if (!cpp_demangle_push_str(ddata
, "::~", 3))
2989 if (!cpp_demangle_push_str(ddata
, ddata
->last_sname
, len
))
2996 if (ELFTC_ISDIGIT(*ddata
->cur
) != 0)
2997 return (cpp_demangle_read_sname(ddata
));
3003 cpp_demangle_read_v_offset(struct cpp_demangle_data
*ddata
)
3009 if (!cpp_demangle_push_str(ddata
, "offset : ", 9))
3012 if (!cpp_demangle_read_offset_number(ddata
))
3015 if (!cpp_demangle_push_str(ddata
, "virtual offset : ", 17))
3018 return (!cpp_demangle_read_offset_number(ddata
));
3022 * Decode floating point representation to string
3023 * Return new allocated string or NULL
3026 * Replace these functions to macro.
3029 decode_fp_to_double(const char *p
, size_t len
)
3032 size_t rtn_len
, limit
, i
;
3036 if (p
== NULL
|| len
== 0 || len
% 2 != 0 || len
/ 2 > sizeof(double))
3039 memset(&f
, 0, sizeof(double));
3041 for (i
= 0; i
< len
/ 2; ++i
) {
3042 byte
= hex_to_dec(p
[len
- i
* 2 - 1]) +
3043 hex_to_dec(p
[len
- i
* 2 - 2]) * 16;
3045 if (byte
< 0 || byte
> 255)
3048 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
3049 ((unsigned char *)&f
)[i
] = (unsigned char)(byte
);
3050 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
3051 ((unsigned char *)&f
)[sizeof(double) - i
- 1] =
3052 (unsigned char)(byte
);
3053 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
3059 if ((rtn
= malloc(sizeof(char) * rtn_len
)) == NULL
)
3062 if (snprintf(rtn
, rtn_len
, "%fld", f
) >= (int)rtn_len
) {
3064 if (limit
++ > FLOAT_SPRINTF_TRY_LIMIT
)
3066 rtn_len
*= BUFFER_GROWFACTOR
;
3074 decode_fp_to_float(const char *p
, size_t len
)
3076 size_t i
, rtn_len
, limit
;
3081 if (p
== NULL
|| len
== 0 || len
% 2 != 0 || len
/ 2 > sizeof(float))
3084 memset(&f
, 0, sizeof(float));
3086 for (i
= 0; i
< len
/ 2; ++i
) {
3087 byte
= hex_to_dec(p
[len
- i
* 2 - 1]) +
3088 hex_to_dec(p
[len
- i
* 2 - 2]) * 16;
3089 if (byte
< 0 || byte
> 255)
3091 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
3092 ((unsigned char *)&f
)[i
] = (unsigned char)(byte
);
3093 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
3094 ((unsigned char *)&f
)[sizeof(float) - i
- 1] =
3095 (unsigned char)(byte
);
3096 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
3102 if ((rtn
= malloc(sizeof(char) * rtn_len
)) == NULL
)
3105 if (snprintf(rtn
, rtn_len
, "%ff", f
) >= (int)rtn_len
) {
3107 if (limit
++ > FLOAT_SPRINTF_TRY_LIMIT
)
3109 rtn_len
*= BUFFER_GROWFACTOR
;
3117 decode_fp_to_float128(const char *p
, size_t len
)
3120 size_t rtn_len
, limit
, i
;
3122 unsigned char buf
[FLOAT_QUADRUPLE_BYTES
];
3125 switch(sizeof(long double)) {
3126 case FLOAT_QUADRUPLE_BYTES
:
3127 return (decode_fp_to_long_double(p
, len
));
3128 case FLOAT_EXTENED_BYTES
:
3129 if (p
== NULL
|| len
== 0 || len
% 2 != 0 ||
3130 len
/ 2 > FLOAT_QUADRUPLE_BYTES
)
3133 memset(buf
, 0, FLOAT_QUADRUPLE_BYTES
);
3135 for (i
= 0; i
< len
/ 2; ++i
) {
3136 byte
= hex_to_dec(p
[len
- i
* 2 - 1]) +
3137 hex_to_dec(p
[len
- i
* 2 - 2]) * 16;
3138 if (byte
< 0 || byte
> 255)
3140 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
3141 buf
[i
] = (unsigned char)(byte
);
3142 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
3143 buf
[FLOAT_QUADRUPLE_BYTES
- i
-1] =
3144 (unsigned char)(byte
);
3145 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
3147 memset(&f
, 0, FLOAT_EXTENED_BYTES
);
3149 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
3150 memcpy(&f
, buf
, FLOAT_EXTENED_BYTES
);
3151 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
3152 memcpy(&f
, buf
+ 6, FLOAT_EXTENED_BYTES
);
3153 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
3158 if ((rtn
= malloc(sizeof(char) * rtn_len
)) == NULL
)
3161 if (snprintf(rtn
, rtn_len
, "%Lfd", f
) >= (int)rtn_len
) {
3163 if (limit
++ > FLOAT_SPRINTF_TRY_LIMIT
)
3165 rtn_len
*= BUFFER_GROWFACTOR
;
3176 decode_fp_to_float80(const char *p
, size_t len
)
3179 size_t rtn_len
, limit
, i
;
3181 unsigned char buf
[FLOAT_EXTENED_BYTES
];
3184 switch(sizeof(long double)) {
3185 case FLOAT_QUADRUPLE_BYTES
:
3186 if (p
== NULL
|| len
== 0 || len
% 2 != 0 ||
3187 len
/ 2 > FLOAT_EXTENED_BYTES
)
3190 memset(buf
, 0, FLOAT_EXTENED_BYTES
);
3192 for (i
= 0; i
< len
/ 2; ++i
) {
3193 byte
= hex_to_dec(p
[len
- i
* 2 - 1]) +
3194 hex_to_dec(p
[len
- i
* 2 - 2]) * 16;
3196 if (byte
< 0 || byte
> 255)
3199 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
3200 buf
[i
] = (unsigned char)(byte
);
3201 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
3202 buf
[FLOAT_EXTENED_BYTES
- i
-1] =
3203 (unsigned char)(byte
);
3204 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
3207 memset(&f
, 0, FLOAT_QUADRUPLE_BYTES
);
3209 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
3210 memcpy(&f
, buf
, FLOAT_EXTENED_BYTES
);
3211 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
3212 memcpy((unsigned char *)(&f
) + 6, buf
, FLOAT_EXTENED_BYTES
);
3213 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
3218 if ((rtn
= malloc(sizeof(char) * rtn_len
)) == NULL
)
3221 if (snprintf(rtn
, rtn_len
, "%Lfd", f
) >= (int)rtn_len
) {
3223 if (limit
++ > FLOAT_SPRINTF_TRY_LIMIT
)
3225 rtn_len
*= BUFFER_GROWFACTOR
;
3230 case FLOAT_EXTENED_BYTES
:
3231 return (decode_fp_to_long_double(p
, len
));
3238 decode_fp_to_long_double(const char *p
, size_t len
)
3241 size_t rtn_len
, limit
, i
;
3245 if (p
== NULL
|| len
== 0 || len
% 2 != 0 ||
3246 len
/ 2 > sizeof(long double))
3249 memset(&f
, 0, sizeof(long double));
3251 for (i
= 0; i
< len
/ 2; ++i
) {
3252 byte
= hex_to_dec(p
[len
- i
* 2 - 1]) +
3253 hex_to_dec(p
[len
- i
* 2 - 2]) * 16;
3255 if (byte
< 0 || byte
> 255)
3258 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
3259 ((unsigned char *)&f
)[i
] = (unsigned char)(byte
);
3260 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
3261 ((unsigned char *)&f
)[sizeof(long double) - i
- 1] =
3262 (unsigned char)(byte
);
3263 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
3269 if ((rtn
= malloc(sizeof(char) * rtn_len
)) == NULL
)
3272 if (snprintf(rtn
, rtn_len
, "%Lfd", f
) >= (int)rtn_len
) {
3274 if (limit
++ > FLOAT_SPRINTF_TRY_LIMIT
)
3276 rtn_len
*= BUFFER_GROWFACTOR
;
3283 /* Simple hex to integer function used by decode_to_* function. */
3327 vector_read_cmd_dest(struct vector_read_cmd
*v
)
3333 free(v
->r_container
);
3336 /* return -1 at failed, 0 at not found, 1 at found. */
3338 vector_read_cmd_find(struct vector_read_cmd
*v
, enum read_cmd dst
)
3342 if (v
== NULL
|| dst
== READ_FAIL
)
3345 for (i
= 0; i
< v
->size
; ++i
)
3346 if (v
->r_container
[i
] == dst
)
3353 vector_read_cmd_init(struct vector_read_cmd
*v
)
3360 v
->capacity
= VECTOR_DEF_CAPACITY
;
3362 if ((v
->r_container
= malloc(sizeof(enum read_cmd
) * v
->capacity
))
3370 vector_read_cmd_pop(struct vector_read_cmd
*v
)
3373 if (v
== NULL
|| v
->size
== 0)
3377 v
->r_container
[v
->size
] = READ_FAIL
;
3383 vector_read_cmd_push(struct vector_read_cmd
*v
, enum read_cmd cmd
)
3385 enum read_cmd
*tmp_r_ctn
;
3392 if (v
->size
== v
->capacity
) {
3393 tmp_cap
= v
->capacity
* BUFFER_GROWFACTOR
;
3394 if ((tmp_r_ctn
= malloc(sizeof(enum read_cmd
) * tmp_cap
))
3397 for (i
= 0; i
< v
->size
; ++i
)
3398 tmp_r_ctn
[i
] = v
->r_container
[i
];
3399 free(v
->r_container
);
3400 v
->r_container
= tmp_r_ctn
;
3401 v
->capacity
= tmp_cap
;
3404 v
->r_container
[v
->size
] = cmd
;
3411 vector_type_qualifier_dest(struct vector_type_qualifier
*v
)
3417 free(v
->q_container
);
3418 vector_str_dest(&v
->ext_name
);
3421 /* size, capacity, ext_name */
3423 vector_type_qualifier_init(struct vector_type_qualifier
*v
)
3430 v
->capacity
= VECTOR_DEF_CAPACITY
;
3432 if ((v
->q_container
= malloc(sizeof(enum type_qualifier
) * v
->capacity
))
3436 assert(v
->q_container
!= NULL
);
3438 if (vector_str_init(&v
->ext_name
) == false) {
3439 free(v
->q_container
);
3447 vector_type_qualifier_push(struct vector_type_qualifier
*v
,
3448 enum type_qualifier t
)
3450 enum type_qualifier
*tmp_ctn
;
3457 if (v
->size
== v
->capacity
) {
3458 tmp_cap
= v
->capacity
* BUFFER_GROWFACTOR
;
3459 if ((tmp_ctn
= malloc(sizeof(enum type_qualifier
) * tmp_cap
))
3462 for (i
= 0; i
< v
->size
; ++i
)
3463 tmp_ctn
[i
] = v
->q_container
[i
];
3464 free(v
->q_container
);
3465 v
->q_container
= tmp_ctn
;
3466 v
->capacity
= tmp_cap
;
3469 v
->q_container
[v
->size
] = t
;