File: Core.cpp

package info (click to toggle)
edb-debugger 1.3.0-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,124 kB
  • sloc: cpp: 46,241; xml: 4,998; ansic: 3,088; sh: 52; asm: 33; makefile: 5
file content (231 lines) | stat: -rw-r--r-- 4,731 bytes parent folder | download | duplicates (4)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

#include "Core.hpp"
#include "Exception.hpp"
#include <QtDebug>

namespace {

constexpr uint32_t Signature = 0x1A736166;

}

// TODO(eteran): this class is generally a good candiate for more RAII
// and usage of the QFile class instead of std::ifstream

namespace Fas {

void Core::load(const std::string &fileName) {
	fileName_ = fileName;

	try {
		open();
		loadHeader();
		loadFasSymbols();
		deleteUndefinedSymbols();
		deleteAssemblyTimeVariable();
		deleteCannotBeForwardReferenced();
		deleteNegativeSymbols();
		deleteSpecialMarkers();
		qDebug() << fasSymbols_.size();
		deleteAnonymousSymbols();
		loadSymbols();
	} catch (std::exception &e) {
		qWarning() << e.what();
	}
}

void Core::open() {
	ifs_.open(fileName_, std::ios::binary);
	if (!ifs_.is_open()) {
		throw Exception("*.fas file not loaded.");
	}
}

void Core::loadHeader() {
	ifs_.seekg(0);

	if (!ifs_.read((char *)&header_, sizeof(Header))) {
		throw Exception("*.fas Header not loaded.");
	}

	if (header_.signature != Signature) {
		throw Exception("*.fas signature fail");
	}

	if (header_.lengthOfHeader != 64) {
		throw Exception("*.fas header size not supported");
	}
}

void Core::loadFasSymbols() {
	ifs_.seekg(header_.offsetOfSymbolsTable);

	auto size  = header_.lengthOfSymbolsTable;
	auto count = size / sizeof(Fas::Symbol);
	for (uint i = 0; i < count; ++i) {
		auto symbol = loadFasSymbol();
		fasSymbols_.push_back(symbol);
	}
}

Fas::Symbol Core::loadFasSymbol() {
	Fas::Symbol symbol;

	if (!ifs_.read((char *)&symbol, sizeof(Fas::Symbol))) {
		throw Exception("*.fas symbol not loaded");
	}

	return symbol;
}

void Core::deleteUndefinedSymbols() {
	auto it = std::begin(fasSymbols_);
	while (it != std::end(fasSymbols_)) {
		uint16_t wasDefined = it->flags & 1;
		if (!wasDefined) {
			it = fasSymbols_.erase(it);
		} else {
			++it;
		}
	}
}

void Core::deleteAssemblyTimeVariable() {
	auto it = std::begin(fasSymbols_);
	while (it != std::end(fasSymbols_)) {
		uint16_t isAssemblyTimeVariable = it->flags & 0x2;
		if (isAssemblyTimeVariable) {
			it = fasSymbols_.erase(it);
		} else {
			++it;
		}
	}
}

void Core::deleteCannotBeForwardReferenced() {
	auto it = std::begin(fasSymbols_);
	while (it != std::end(fasSymbols_)) {
		uint16_t cannotBeForwardReferenced = it->flags & 0x4;
		if (cannotBeForwardReferenced) {
			it = fasSymbols_.erase(it);
		} else {
			++it;
		}
	}
}

void Core::deleteSpecialMarkers() {
	auto it = std::begin(fasSymbols_);
	while (it != std::end(fasSymbols_)) {
		uint16_t isSpecialMarker = it->flags & 0x400;
		if (isSpecialMarker) {
			it = fasSymbols_.erase(it);
		} else {
			++it;
		}
	}
}

void Core::deleteNegativeSymbols() {
	auto it = std::begin(fasSymbols_);
	while (it != std::end(fasSymbols_)) {
		if (it->valueSign) {
			it = fasSymbols_.erase(it);
		} else {
			++it;
		}
	}
}

void Core::deleteAnonymousSymbols() {
	auto it = std::begin(fasSymbols_);
	while (it != std::end(fasSymbols_)) {
		bool isAnonymous = it->preprocessedSign == 0 && it->preprocessed == 0;
		if (isAnonymous) {
			it = fasSymbols_.erase(it);
		} else {
			++it;
		}
	}
}

void Core::loadSymbols() {
	for (auto fasSymbol : fasSymbols_) {
		checkAbsoluteValue(fasSymbol);
		loadSymbolFromFasSymbol(fasSymbol);
	}
}

void Core::checkAbsoluteValue(const Fas::Symbol &fasSymbol) {
	if (fasSymbol.typeOfValue != ValueTypes::ABSOLUTE_VALUE) {
		throw Exception(" Support only absolute value");
	}
}

void Core::loadSymbolFromFasSymbol(const Fas::Symbol &fasSymbol) {
	PluginSymbol symbol;

	symbol.value = fasSymbol.value;
	symbol.size  = fasSymbol.sizeOfData;

	if (fasSymbol.preprocessedSign) {
		// in the strings table
		symbol.name = cstr2string(fasSymbol);
	} else {
		// in the preprocessed pascal style
		symbol.name = pascal2string(fasSymbol);
	}

	symbols_.push_back(symbol);
}

std::string Core::cstr2string(const Symbol &fasSymbol) {

	constexpr int MaxLength = 64;

	auto offset = header_.offsetOfSymbolsTable + fasSymbol.preprocessed;
	char cstr[MaxLength];
	auto count = 0;

	ifs_.seekg(offset);
	char *c = cstr;
	while (true) {
		ifs_.read(c, 1);
		if (count >= (MaxLength - 1)) break;
		if (*c == 0) break;
		++c;
		++count;
	}

	cstr[count] = '\0';

	std::string str = cstr;
	return str;
}

std::string Core::pascal2string(const Fas::Symbol &fasSymbol) {

	auto offset = header_.offsetOfPreprocessedSource + fasSymbol.preprocessed;
	uint8_t len;
	char pascal[64];

	ifs_.seekg(offset);

	if (!ifs_.read((char *)&len, sizeof(len))) {
		throw Exception("Length of pascal string not loaded");
	}

	if (!ifs_.read(pascal, len)) {
		throw Exception("Pascal string not loaded");
	}
	pascal[len] = '\0';

	std::string str = pascal;
	return str;
}

const PluginSymbols &Core::getSymbols() {
	return symbols_;
}

}