forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVersionInfoFactory.cpp
More file actions
225 lines (204 loc) · 6.48 KB
/
Copy pathVersionInfoFactory.cpp
File metadata and controls
225 lines (204 loc) · 6.48 KB
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
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2012 Petr Mrázek (peterix@gmail.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "Internal.h"
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <iostream>
using namespace std;
#include "VersionInfoFactory.h"
#include "VersionInfo.h"
#include "Error.h"
using namespace DFHack;
#include <tinyxml.h>
VersionInfoFactory::VersionInfoFactory()
{
error = false;
}
VersionInfoFactory::~VersionInfoFactory()
{
clear();
}
void VersionInfoFactory::clear()
{
// for each stored version, delete
for(size_t i = 0; i < versions.size();i++)
{
delete versions[i];
}
versions.clear();
error = false;
}
VersionInfo * VersionInfoFactory::getVersionInfoByMD5(string hash)
{
for(size_t i = 0; i < versions.size();i++)
{
if(versions[i]->hasMD5(hash))
return versions[i];
}
return 0;
}
VersionInfo * VersionInfoFactory::getVersionInfoByPETimestamp(uint32_t timestamp)
{
for(size_t i = 0; i < versions.size();i++)
{
if(versions[i]->hasPE(timestamp))
return versions[i];
}
return 0;
}
void VersionInfoFactory::ParseVersion (TiXmlElement* entry, VersionInfo* mem)
{
bool no_vtables = getenv("DFHACK_NO_VTABLES");
bool no_globals = getenv("DFHACK_NO_GLOBALS");
TiXmlElement* pMemEntry;
const char *cstr_name = entry->Attribute("name");
if (!cstr_name)
throw Error::SymbolsXmlBadAttribute("name");
const char *cstr_os = entry->Attribute("os-type");
if (!cstr_os)
throw Error::SymbolsXmlBadAttribute("os-type");
string os = cstr_os;
mem->setVersion(cstr_name);
if(os == "windows")
{
mem->setOS(OS_WINDOWS);
// set default image base. this is fixed for base relocation later
mem->setBase(0x400000);
}
else if(os == "linux")
{
mem->setOS(OS_LINUX);
// this is wrong... I'm not going to do base image relocation on linux though.
mem->setBase(0x8048000);
}
else if(os == "darwin")
{
mem->setOS(OS_APPLE);
// this is wrong... I'm not going to do base image relocation on linux though.
mem->setBase(0x1000);
}
else
{
return; // ignore it if it's invalid
}
// process additional entries
//cout << "Entry " << cstr_version << " " << cstr_os << endl;
pMemEntry = entry->FirstChildElement()->ToElement();
for(;pMemEntry;pMemEntry=pMemEntry->NextSiblingElement())
{
string type, name, value;
const char *cstr_type = pMemEntry->Value();
type = cstr_type;
bool is_vtable = (type == "vtable-address");
if(is_vtable || type == "global-address")
{
const char *cstr_key = pMemEntry->Attribute("name");
if(!cstr_key)
throw Error::SymbolsXmlUnderspecifiedEntry(cstr_name);
const char *cstr_value = pMemEntry->Attribute("value");
if(!cstr_value)
{
cerr << "Dummy symbol table entry: " << cstr_key << endl;
continue;
}
if ((is_vtable && no_vtables) || (!is_vtable && no_globals))
continue;
uint32_t addr = strtol(cstr_value, 0, 0);
if (is_vtable)
mem->setVTable(cstr_key, addr);
else
mem->setAddress(cstr_key, addr);
}
else if (type == "md5-hash")
{
const char *cstr_value = pMemEntry->Attribute("value");
fprintf(stderr, "%s: MD5: %s\n", cstr_name, cstr_value);
if(!cstr_value)
throw Error::SymbolsXmlUnderspecifiedEntry(cstr_name);
mem->addMD5(cstr_value);
}
else if (type == "binary-timestamp")
{
const char *cstr_value = pMemEntry->Attribute("value");
fprintf(stderr, "%s: PE: %s\n", cstr_name, cstr_value);
if(!cstr_value)
throw Error::SymbolsXmlUnderspecifiedEntry(cstr_name);
mem->addPE(strtol(cstr_value, 0, 16));
}
} // for
} // method
// load the XML file with offsets
bool VersionInfoFactory::loadFile(string path_to_xml)
{
TiXmlDocument doc( path_to_xml.c_str() );
std::cerr << "Loading " << path_to_xml << " ... ";
//bool loadOkay = doc.LoadFile();
if (!doc.LoadFile())
{
error = true;
cerr << "failed!\n";
throw Error::SymbolsXmlParse(doc.ErrorDesc(), doc.ErrorId(), doc.ErrorRow(), doc.ErrorCol());
}
else
{
cerr << "OK\n";
}
TiXmlHandle hDoc(&doc);
TiXmlElement* pElem;
TiXmlHandle hRoot(0);
// block: name
{
pElem=hDoc.FirstChildElement().Element();
// should always have a valid root but handle gracefully if it does
if (!pElem)
{
error = true;
throw Error::SymbolsXmlNoRoot();
}
string m_name=pElem->Value();
if(m_name != "data-definition")
{
error = true;
throw Error::SymbolsXmlNoRoot();
}
// save this for later
hRoot=TiXmlHandle(pElem);
}
// transform elements
{
clear();
// For each version
TiXmlElement * pMemInfo=hRoot.FirstChild( "symbol-table" ).Element();
for( ; pMemInfo; pMemInfo=pMemInfo->NextSiblingElement("symbol-table"))
{
const char *name = pMemInfo->Attribute("name");
if(name)
{
VersionInfo *version = new VersionInfo();
ParseVersion( pMemInfo , version );
versions.push_back(version);
}
}
}
error = false;
std::cerr << "Loaded " << versions.size() << " DF symbol tables." << std::endl;
return true;
}