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