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