2011-08-03 07:07:57 -06:00
|
|
|
#include "hexsearch.h"
|
|
|
|
|
|
|
|
|
2012-01-05 15:39:14 -07:00
|
|
|
Hexsearch::Hexsearch(const SearchArgType &args,char * startpos,char * endpos):args_(args),pos_(startpos),startpos_(startpos),endpos_(endpos)
|
2011-08-03 07:07:57 -06:00
|
|
|
{
|
2011-08-03 08:59:06 -06:00
|
|
|
ReparseArgs();
|
2011-08-03 07:07:57 -06:00
|
|
|
}
|
|
|
|
Hexsearch::~Hexsearch()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2011-08-03 08:59:06 -06:00
|
|
|
inline bool Hexsearch::Compare(int a,int b)
|
|
|
|
{
|
|
|
|
if(b==Hexsearch::ANYBYTE)
|
|
|
|
return true;
|
|
|
|
if(a==b)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void Hexsearch::ReparseArgs()
|
|
|
|
{
|
2011-08-04 15:22:25 -06:00
|
|
|
union
|
|
|
|
{
|
|
|
|
uint32_t val;
|
|
|
|
uint8_t bytes[4];
|
|
|
|
}B;
|
2011-08-03 08:59:06 -06:00
|
|
|
SearchArgType targ;
|
|
|
|
targ=args_;
|
|
|
|
args_.clear();
|
|
|
|
for(size_t i=0;i<targ.size();)
|
|
|
|
{
|
|
|
|
if(targ[i]==DWORD_)
|
|
|
|
{
|
|
|
|
i++;
|
2011-08-04 15:22:25 -06:00
|
|
|
B.val=targ[i];
|
2011-08-03 08:59:06 -06:00
|
|
|
for(int j=0;j<4;j++)
|
|
|
|
{
|
2011-08-04 15:22:25 -06:00
|
|
|
args_.push_back(B.bytes[j]);
|
2011-08-03 08:59:06 -06:00
|
|
|
}
|
2011-08-03 09:15:58 -06:00
|
|
|
i++;
|
2011-08-03 08:59:06 -06:00
|
|
|
}
|
|
|
|
else if (targ[i]==ANYDWORD)
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
for(int j=0;j<4;j++)
|
|
|
|
args_.push_back(ANYBYTE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
args_.push_back(targ[i]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-03 17:45:11 -07:00
|
|
|
void * Hexsearch::FindNext() //TODO rewrite using Boyer-Moore algorithm
|
2011-08-03 07:07:57 -06:00
|
|
|
{
|
|
|
|
DFHack::Core &inst=DFHack::Core::getInstance();
|
|
|
|
DFHack::Process *p=inst.p;
|
|
|
|
uint8_t *buf;
|
|
|
|
buf=new uint8_t[args_.size()];
|
|
|
|
while(pos_<endpos_)
|
|
|
|
{
|
|
|
|
bool found=true;
|
|
|
|
p->readByte(pos_,buf[0]);
|
2011-08-03 08:59:06 -06:00
|
|
|
if(Compare(buf[0],args_[0]))
|
2011-08-03 07:07:57 -06:00
|
|
|
{
|
|
|
|
p->read(pos_,args_.size(),buf);
|
|
|
|
for(size_t i=0;i<args_.size();i++)
|
|
|
|
{
|
2011-08-03 08:59:06 -06:00
|
|
|
if(!Compare(buf[i],args_[i]))
|
2011-08-03 07:07:57 -06:00
|
|
|
{
|
|
|
|
pos_+=i;
|
|
|
|
found=false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(found)
|
|
|
|
{
|
|
|
|
pos_+=args_.size();
|
|
|
|
delete [] buf;
|
2011-08-03 07:27:04 -06:00
|
|
|
return pos_-args_.size();
|
2011-08-03 07:07:57 -06:00
|
|
|
}
|
|
|
|
}
|
2012-01-03 17:45:11 -07:00
|
|
|
pos_ = pos_ + 1;
|
2011-08-03 07:07:57 -06:00
|
|
|
}
|
|
|
|
delete [] buf;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-03 17:45:11 -07:00
|
|
|
std::vector<void *> Hexsearch::FindAll()
|
2011-08-03 07:07:57 -06:00
|
|
|
{
|
2012-01-03 17:45:11 -07:00
|
|
|
std::vector<void *> ret;
|
|
|
|
void * cpos=pos_;
|
2011-08-03 07:07:57 -06:00
|
|
|
while(cpos!=0)
|
|
|
|
{
|
|
|
|
cpos=FindNext();
|
|
|
|
if(cpos!=0)
|
|
|
|
ret.push_back(cpos);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
void Hexsearch::PrepareBadCharShift()
|
|
|
|
{
|
|
|
|
BadCharShifts.resize(256,-1);
|
|
|
|
int i=0;
|
|
|
|
for(SearchArgType::reverse_iterator it=args_.rbegin();it!=args_.rend();it++)
|
|
|
|
{
|
|
|
|
BadCharShifts[*it]=i;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Hexsearch::PrepareGoodSuffixTable()
|
|
|
|
{
|
|
|
|
GoodSuffixShift.resize(args_.size()+1,0);
|
|
|
|
|
|
|
|
}
|