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