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