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