File: demangle.c

package info (click to toggle)
mspdebug 0.25-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,700 kB
  • sloc: ansic: 67,821; makefile: 201; xml: 19
file content (117 lines) | stat: -rw-r--r-- 2,582 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* MSPDebug - debugging tool for MSP430 MCUs
 * Copyright (C) 2009-2012 Daniel Beer
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "opdb.h"
#include "stab.h"

/* Buffer in which the demangler result will be constructed. */
struct dmbuf {
	char		*out;
	size_t		max_len;
	size_t		len;
};

/* Add a chunk of text to the buffer */
static int dm_append(struct dmbuf *d, const char *text, size_t len)
{
	size_t i;

	if (d->len + len + 1 > d->max_len)
		return -1;

	for (i = 0; i < len; i++) {
		if (!text[i])
			return -1;

		d->out[d->len++] = text[i];
	}

	d->out[d->len] = 0;
	return 0;
}

/* Demangle a single component (possibly part of a nested name). */
static int dm_component(struct dmbuf *d, const char *text, const char **out)
{
	char *next;
	unsigned int len = strtoul(text, &next, 10);

	if (next == text)
		return -1;

	if (dm_append(d, next, len) < 0)
		return -1;

	if (out)
		*out = next + len;

	return 0;
}

/* Demangler interface */
int demangle(const char *raw, char *out, size_t max_len)
{
	struct dmbuf d;

	d.out = out;
	d.max_len = max_len;
	d.len = 0;

	if (*raw != '_' || raw[1] != 'Z')
		return -1;
	raw += 2;

	if (*raw == 'N') {
		const char *next;

		/* Skip CV qualifiers */
		raw++;
		while (*raw == 'v' || *raw == 'V' || *raw == 'K')
			raw++;

		next = raw;

		while (*next != 'C' && *next != 'D' && *next != 'E') {
			const char *comp = next;

			if (d.len > 0 && dm_append(&d, "::", 2) < 0)
				return -1;
			if (dm_component(&d, comp, &next) < 0)
				return -1;

			if (*next == 'C' || *next == 'D') {
				/* Constructor/Destructor */
				if (dm_append(&d, "::", 2) < 0)
					return -1;
				if (*next == 'D' && dm_append(&d, "~", 1) < 0)
					return -1;
				if (dm_component(&d, comp, NULL) < 0)
					return -1;
			}
		}
	} else {
		if (dm_component(&d, raw, NULL) < 0)
			return -1;
	}

	return d.len;
}