dfhack/library/VersionInfoFactory.cpp

207 lines
5.8 KiB
C++

2009-09-13 18:02:46 -06:00
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
2009-09-13 18:02:46 -06:00
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>
2011-08-16 15:39:18 -06:00
#include <iostream>
using namespace std;
2011-12-31 04:48:42 -07:00
#include "VersionInfoFactory.h"
#include "VersionInfo.h"
#include "Error.h"
2009-11-13 20:46:56 -07:00
using namespace DFHack;
2009-09-13 18:02:46 -06:00
#include <tinyxml.h>
2011-11-04 02:08:29 -06:00
VersionInfoFactory::VersionInfoFactory()
2011-02-24 03:43:33 -07:00
{
error = false;
}
VersionInfoFactory::~VersionInfoFactory()
2011-11-04 02:08:29 -06:00
{
clear();
}
void VersionInfoFactory::clear()
{
2010-08-22 17:29:55 -06:00
// for each stored version, delete
for(size_t i = 0; i < versions.size();i++)
{
2010-08-22 17:29:55 -06:00
delete versions[i];
}
2010-08-22 17:29:55 -06:00
versions.clear();
2011-11-04 02:08:29 -06:00
error = false;
}
2011-02-24 03:43:33 -07:00
VersionInfo * VersionInfoFactory::getVersionInfoByMD5(string hash)
{
VersionInfo * vinfo;
for(size_t i = 0; i < versions.size();i++)
2011-02-24 03:43:33 -07:00
{
if(versions[i]->hasMD5(hash))
return versions[i];
2011-02-24 03:43:33 -07:00
}
return 0;
2011-02-24 03:43:33 -07:00
}
VersionInfo * VersionInfoFactory::getVersionInfoByPETimestamp(uint32_t timestamp)
{
for(size_t i = 0; i < versions.size();i++)
2011-02-24 03:43:33 -07:00
{
if(versions[i]->hasPE(timestamp))
return versions[i];
2011-02-24 03:43:33 -07:00
}
return 0;
2011-02-24 03:43:33 -07:00
}
2010-08-22 17:29:55 -06:00
void VersionInfoFactory::ParseVersion (TiXmlElement* entry, VersionInfo* mem)
2009-09-13 18:02:46 -06:00
{
TiXmlElement* pMemEntry;
2010-08-22 17:29:55 -06:00
const char *cstr_name = entry->Attribute("name");
if (!cstr_name)
throw Error::MemoryXmlBadAttribute("name");
2012-02-11 11:24:44 -07:00
const char *cstr_os = entry->Attribute("os-type");
2010-02-28 13:44:14 -07:00
if (!cstr_os)
2012-02-11 11:24:44 -07:00
throw Error::MemoryXmlBadAttribute("os-type");
2009-09-13 18:02:46 -06:00
string os = cstr_os;
2010-08-22 17:29:55 -06:00
mem->setVersion(cstr_name);
2009-09-13 18:02:46 -06:00
if(os == "windows")
{
mem->setOS(OS_WINDOWS);
2009-09-13 18:02:46 -06:00
// set default image base. this is fixed for base relocation later
mem->setBase(0x400000);
2009-09-13 18:02:46 -06:00
}
else if(os == "linux")
{
mem->setOS(OS_LINUX);
2009-09-13 18:02:46 -06:00
// this is wrong... I'm not going to do base image relocation on linux though.
mem->setBase(0x0);
2009-09-13 18:02:46 -06:00
}
else
{
2012-02-11 11:24:44 -07:00
throw Error::MemoryXmlBadAttribute("os-type");
2009-09-13 18:02:46 -06:00
}
2009-09-13 18:02:46 -06:00
// process additional entries
//cout << "Entry " << cstr_version << " " << cstr_os << endl;
pMemEntry = entry->FirstChildElement()->ToElement();
for(;pMemEntry;pMemEntry=pMemEntry->NextSiblingElement())
{
string type, name, value;
2010-08-22 17:29:55 -06:00
const char *cstr_type = pMemEntry->Value();
2009-09-13 18:02:46 -06:00
type = cstr_type;
2012-02-11 11:24:44 -07:00
if(type == "global-address")
{
2012-02-11 11:24:44 -07:00
const char *cstr_key = pMemEntry->Attribute("name");
if(!cstr_key)
throw Error::MemoryXmlUnderspecifiedEntry(cstr_name);
const char *cstr_value = pMemEntry->Attribute("value");
if(!cstr_value)
throw Error::MemoryXmlUnderspecifiedEntry(cstr_name);
mem->setAddress(cstr_key, strtol(cstr_value, 0, 0));
}
2012-02-11 11:24:44 -07:00
else if (type == "md5-hash")
2010-06-10 09:53:25 -06:00
{
2010-08-22 17:29:55 -06:00
const char *cstr_value = pMemEntry->Attribute("value");
if(!cstr_value)
throw Error::MemoryXmlUnderspecifiedEntry(cstr_name);
mem->addMD5(cstr_value);
}
2012-02-11 11:24:44 -07:00
else if (type == "binary-timestamp")
2009-09-13 18:02:46 -06:00
{
2010-08-22 17:29:55 -06:00
const char *cstr_value = pMemEntry->Attribute("value");
if(!cstr_value)
throw Error::MemoryXmlUnderspecifiedEntry(cstr_name);
mem->addPE(strtol(cstr_value, 0, 16));
2009-09-13 18:02:46 -06:00
}
} // for
} // method
// load the XML file with offsets
bool VersionInfoFactory::loadFile(string path_to_xml)
2009-09-13 18:02:46 -06:00
{
TiXmlDocument doc( path_to_xml.c_str() );
2011-08-16 15:39:18 -06:00
std::cerr << "Loading " << path_to_xml << " ... ";
2010-02-28 13:44:14 -07:00
//bool loadOkay = doc.LoadFile();
if (!doc.LoadFile())
{
error = true;
2011-08-16 15:39:18 -06:00
cerr << "failed!\n";
2010-02-28 13:44:14 -07:00
throw Error::MemoryXmlParse(doc.ErrorDesc(), doc.ErrorId(), doc.ErrorRow(), doc.ErrorCol());
}
2011-08-16 15:39:18 -06:00
else
{
cerr << "OK\n";
}
TiXmlHandle hDoc(&doc);
2009-09-15 14:46:45 -06:00
TiXmlElement* pElem;
TiXmlHandle hRoot(0);
2010-08-22 17:29:55 -06:00
VersionInfo *mem;
2010-02-28 13:44:14 -07:00
// block: name
2009-09-13 18:02:46 -06:00
{
2010-02-28 13:44:14 -07:00
pElem=hDoc.FirstChildElement().Element();
// should always have a valid root but handle gracefully if it does
if (!pElem)
2009-09-13 18:02:46 -06:00
{
2010-02-28 13:44:14 -07:00
error = true;
throw Error::MemoryXmlNoRoot();
2009-09-13 18:02:46 -06:00
}
2010-02-28 13:44:14 -07:00
string m_name=pElem->Value();
2012-02-11 11:24:44 -07:00
if(m_name != "data-definition")
2009-09-13 18:02:46 -06:00
{
2010-02-28 13:44:14 -07:00
error = true;
throw Error::MemoryXmlNoRoot();
2009-09-13 18:02:46 -06:00
}
2010-02-28 13:44:14 -07:00
// save this for later
hRoot=TiXmlHandle(pElem);
2009-09-13 18:02:46 -06:00
}
2010-02-28 13:44:14 -07:00
// transform elements
2009-09-13 18:02:46 -06:00
{
clear();
// For each version
2012-02-11 11:24:44 -07:00
TiXmlElement * pMemInfo=hRoot.FirstChild( "symbol-table" ).Element();
for( ; pMemInfo; pMemInfo=pMemInfo->NextSiblingElement("symbol-table"))
2010-08-22 17:29:55 -06:00
{
const char *name = pMemInfo->Attribute("name");
if(name)
{
VersionInfo *version = new VersionInfo();
ParseVersion( pMemInfo , version );
2010-08-22 17:29:55 -06:00
versions.push_back(version);
}
2010-02-28 13:44:14 -07:00
}
2009-09-13 18:02:46 -06:00
}
2010-02-28 13:44:14 -07:00
error = false;
2012-02-11 11:24:44 -07:00
std::cerr << "Loaded " << versions.size() << " DF symbol tables." << std::endl;
2010-02-28 13:44:14 -07:00
return true;
}