-
Notifications
You must be signed in to change notification settings - Fork 3
/
patterngenerator.h
68 lines (55 loc) · 2.18 KB
/
patterngenerator.h
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
#ifndef PATTERNGENERATOR_H
#define PATTERNGENERATOR_H
#define WILDCARD_PATTERN "??"
#define WILDCARD_CHARACTER '?'
#include <algorithm>
#include <string>
#include <list>
#include <redasm/disassembler/types/symboltable.h>
#include <redasm/disassembler/disassembler.h>
#include <redasm/database/signaturedb.h>
#include <redasm/support/hash.h>
#include <json.hpp>
using json = nlohmann::json;
struct BytePattern
{
u32 symboltype;
std::string name, pattern;
u16 hash() const { return REDasm::Hash::crc16(name + pattern); }
};
class PatternGenerator: private std::list<BytePattern>
{
public:
using std::list<BytePattern>::begin;
using std::list<BytePattern>::end;
using std::list<BytePattern>::size;
public:
PatternGenerator();
void setPrefix(const std::string& prefix);
void setSuffix(const std::string& suffix);
bool disassemble(const BytePattern& bytepattern);
bool writePatterns(REDasm::SignatureDB& sigdb);
bool writePatternsSource(json& patterns);
void push(const BytePattern& bp);
public:
virtual std::string name() const = 0;
virtual std::string assembler() const = 0;
virtual bool test(const std::string& infile) = 0;
virtual void generate(const std::string& infile) = 0;
private:
bool appendAllPatterns(REDasm::Signature* signature, const BytePattern &bytepattern) const;
u16 chunkChecksum(const std::string& chunk) const;
std::string getChunk(const std::string& s, int offset, bool *wildcard) const;
std::string fullName(const std::string& name) const;
REDasm::Disassembler *createDisassembler(REDasm::AbstractBuffer *buffer);
protected:
void pushPattern(u32 symboltype, const std::string& name, const std::string& pattern);
bool isBytePatternValid(const BytePattern& bytepattern) const;
protected:
static std::string subPattern(const std::string& pattern, size_t pos, size_t len);
static void wildcard(std::string& subPattern, size_t pos, size_t n);
private:
std::string m_outfolder, m_prefix, m_suffix;
std::set<u16> m_done;
};
#endif // PATTERNGENERATOR_H