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