retdec
pattern.h
Go to the documentation of this file.
1 
7 #ifndef RETDEC_COMMON_PATTERN_H
8 #define RETDEC_COMMON_PATTERN_H
9 
10 #include <string>
11 #include <optional>
12 
13 #include "retdec/common/address.h"
14 
15 namespace retdec {
16 namespace common {
17 
21 class Pattern
22 {
23  public:
24  class Match
25  {
26  public:
27  Match();
28  static Match unknown(
29  const retdec::common::Address& offset
31  const retdec::common::Address& address
33  std::optional<unsigned> size = std::nullopt,
34  std::optional<unsigned> entrySize = std::nullopt);
35  static Match integral(
36  const retdec::common::Address& offset
38  const retdec::common::Address& address
40  std::optional<unsigned> size = std::nullopt,
41  std::optional<unsigned> entrySize = std::nullopt);
42  static Match floatingPoint(
43  const retdec::common::Address& offset
45  const retdec::common::Address& address
47  std::optional<unsigned> size = std::nullopt,
48  std::optional<unsigned> entrySize = std::nullopt);
49 
50  bool operator==(const Match& val) const;
51  bool operator!=(const Match& val) const;
52 
55  bool isOffsetDefined() const;
56  bool isAddressDefined() const;
57  bool isSizeDefined() const;
58  bool isEntrySizeDefined() const;
59  bool isTypeUnknown() const;
60  bool isTypeIntegral() const;
61  bool isTypeFloatingPoint() const;
63 
66  void setOffset(const retdec::common::Address& offset);
67  void setAddress(const retdec::common::Address& address);
68  void setSize(const unsigned size);
69  void setEntrySize(const unsigned entrySize);
70  void setIsTypeUnknown();
71  void setIsTypeIntegral();
74 
79  std::optional<unsigned> getSize() const;
80  std::optional<unsigned> getEntrySize() const;
82 
83  private:
84  enum class eType
85  {
86  UNKNOWN,
87  INTEGRAL,
89  };
90 
91  private:
92  Match(const retdec::common::Address& offset,
93  const retdec::common::Address& address,
94  std::optional<unsigned> size,
95  std::optional<unsigned> entrySize,
96  eType type);
97 
98  private:
101  std::optional<unsigned> _size;
102  std::optional<unsigned> _entrySize;
104  };
105 
106  public:
107  Pattern();
108  static Pattern other(
109  const std::string& name = "",
110  const std::string& description = "",
111  const std::string& yaraRuleName = "");
112  static Pattern otherLittle(
113  const std::string& name = "",
114  const std::string& description = "",
115  const std::string& yaraRuleName = "");
116  static Pattern otherBig(
117  const std::string& name = "",
118  const std::string& description = "",
119  const std::string& yaraRuleName = "");
120  static Pattern crypto(
121  const std::string& name = "",
122  const std::string& description = "",
123  const std::string& yaraRuleName = "");
124  static Pattern cryptoLittle(
125  const std::string& name = "",
126  const std::string& description = "",
127  const std::string& yaraRuleName = "");
128  static Pattern cryptoBig(
129  const std::string& name = "",
130  const std::string& description = "",
131  const std::string& yaraRuleName = "");
132  static Pattern malware(
133  const std::string& name = "",
134  const std::string& description = "",
135  const std::string& yaraRuleName = "");
136  static Pattern malwareLittle(
137  const std::string& name = "",
138  const std::string& description = "",
139  const std::string& yaraRuleName = "");
140  static Pattern malwareBig(
141  const std::string& name = "",
142  const std::string& description = "",
143  const std::string& yaraRuleName = "");
144 
145  bool operator==(const Pattern& val) const;
146  bool operator!=(const Pattern& val) const;
147 
150  bool isTypeOther() const;
151  bool isTypeCrypto() const;
152  bool isTypeMalware() const;
153  bool isEndianUnknown() const;
154  bool isEndianLittle() const;
155  bool isEndianBig() const;
157 
160  void setName(const std::string& name);
161  void setDescription(const std::string& description);
162  void setYaraRuleName(const std::string& yaraRuleName);
163  void setIsTypeOther();
164  void setIsTypeCrypto();
165  void setIsTypeMalware();
166  void setIsEndianUnknown();
167  void setIsEndianLittle();
168  void setIsEndianBig();
170 
173  std::string getName() const;
174  std::string getDescription() const;
175  std::string getYaraRuleName() const;
177 
178  private:
179  enum class eType
180  {
181  OTHER,
182  CRYPTO,
183  MALWARE
184  };
185 
186  enum class eEndian
187  {
188  UNKNOWN,
189  LITTLE,
190  BIG
191  };
192 
193  private:
194  Pattern(const std::string& name,
195  const std::string& description,
196  const std::string& yaraRuleName,
197  eType type,
198  eEndian endian);
199 
200  public:
201  std::vector<Match> matches;
202 
203  private:
204  std::string _name;
205  std::string _description;
206  std::string _yaraRuleName;
209 };
210 
211 using PatternContainer = std::vector<Pattern>;
212 
213 } // namespace common
214 } // namespace retdec
215 
216 #endif
Definition: address.h:21
static const uint64_t Undefined
Definition: address.h:47
Definition: pattern.h:25
bool isTypeIntegral() const
Definition: pattern.cpp:92
eType _type
Definition: pattern.h:103
void setEntrySize(const unsigned entrySize)
Definition: pattern.cpp:117
bool isOffsetDefined() const
Definition: pattern.cpp:67
std::optional< unsigned > getEntrySize() const
Definition: pattern.cpp:152
bool isTypeUnknown() const
Definition: pattern.cpp:87
std::optional< unsigned > _entrySize
Definition: pattern.h:102
void setIsTypeFloatingPoint()
Definition: pattern.cpp:132
retdec::common::Address _offset
Definition: pattern.h:99
void setIsTypeUnknown()
Definition: pattern.cpp:122
retdec::common::Address _address
Definition: pattern.h:100
bool isSizeDefined() const
Definition: pattern.cpp:77
bool isAddressDefined() const
Definition: pattern.cpp:72
bool isEntrySizeDefined() const
Definition: pattern.cpp:82
void setIsTypeIntegral()
Definition: pattern.cpp:127
bool isTypeFloatingPoint() const
Definition: pattern.cpp:97
Match()
Definition: pattern.cpp:19
std::optional< unsigned > getSize() const
Definition: pattern.cpp:147
void setSize(const unsigned size)
Definition: pattern.cpp:112
std::optional< unsigned > _size
Definition: pattern.h:101
eType
Definition: pattern.h:85
void setAddress(const retdec::common::Address &address)
Definition: pattern.cpp:107
retdec::common::Address getOffset() const
Definition: pattern.cpp:137
bool operator!=(const Match &val) const
Definition: pattern.cpp:170
static Match unknown(const retdec::common::Address &offset=retdec::common::Address::Undefined, const retdec::common::Address &address=retdec::common::Address::Undefined, std::optional< unsigned > size=std::nullopt, std::optional< unsigned > entrySize=std::nullopt)
Definition: pattern.cpp:40
bool operator==(const Match &val) const
Definition: pattern.cpp:161
void setOffset(const retdec::common::Address &offset)
Definition: pattern.cpp:102
static Match integral(const retdec::common::Address &offset=retdec::common::Address::Undefined, const retdec::common::Address &address=retdec::common::Address::Undefined, std::optional< unsigned > size=std::nullopt, std::optional< unsigned > entrySize=std::nullopt)
Definition: pattern.cpp:49
static Match floatingPoint(const retdec::common::Address &offset=retdec::common::Address::Undefined, const retdec::common::Address &address=retdec::common::Address::Undefined, std::optional< unsigned > size=std::nullopt, std::optional< unsigned > entrySize=std::nullopt)
Definition: pattern.cpp:58
retdec::common::Address getAddress() const
Definition: pattern.cpp:142
Definition: pattern.h:22
static Pattern malwareLittle(const std::string &name="", const std::string &description="", const std::string &yaraRuleName="")
Definition: pattern.cpp:237
static Pattern malwareBig(const std::string &name="", const std::string &description="", const std::string &yaraRuleName="")
Definition: pattern.cpp:242
Pattern()
Definition: pattern.cpp:181
eType
Definition: pattern.h:180
bool isEndianBig() const
Definition: pattern.cpp:272
std::string _name
Definition: pattern.h:204
std::string getName() const
Definition: pattern.cpp:322
static Pattern other(const std::string &name="", const std::string &description="", const std::string &yaraRuleName="")
Definition: pattern.cpp:202
bool isEndianUnknown() const
Definition: pattern.cpp:262
static Pattern otherBig(const std::string &name="", const std::string &description="", const std::string &yaraRuleName="")
Definition: pattern.cpp:212
void setIsTypeMalware()
Definition: pattern.cpp:302
std::string _description
Definition: pattern.h:205
std::string getYaraRuleName() const
Definition: pattern.cpp:332
static Pattern cryptoBig(const std::string &name="", const std::string &description="", const std::string &yaraRuleName="")
Definition: pattern.cpp:227
void setIsTypeCrypto()
Definition: pattern.cpp:297
void setName(const std::string &name)
Definition: pattern.cpp:277
bool isEndianLittle() const
Definition: pattern.cpp:267
static Pattern cryptoLittle(const std::string &name="", const std::string &description="", const std::string &yaraRuleName="")
Definition: pattern.cpp:222
static Pattern malware(const std::string &name="", const std::string &description="", const std::string &yaraRuleName="")
Definition: pattern.cpp:232
void setIsEndianBig()
Definition: pattern.cpp:317
bool isTypeCrypto() const
Definition: pattern.cpp:252
bool operator==(const Pattern &val) const
Definition: pattern.cpp:341
static Pattern crypto(const std::string &name="", const std::string &description="", const std::string &yaraRuleName="")
Definition: pattern.cpp:217
void setYaraRuleName(const std::string &yaraRuleName)
Definition: pattern.cpp:287
eEndian
Definition: pattern.h:187
std::string _yaraRuleName
Definition: pattern.h:206
eEndian _endian
Definition: pattern.h:208
void setIsEndianUnknown()
Definition: pattern.cpp:307
bool isTypeMalware() const
Definition: pattern.cpp:257
void setIsEndianLittle()
Definition: pattern.cpp:312
bool isTypeOther() const
Definition: pattern.cpp:247
std::string getDescription() const
Definition: pattern.cpp:327
eType _type
Definition: pattern.h:207
void setIsTypeOther()
Definition: pattern.cpp:292
std::vector< Match > matches
Definition: pattern.h:201
void setDescription(const std::string &description)
Definition: pattern.cpp:282
static Pattern otherLittle(const std::string &name="", const std::string &description="", const std::string &yaraRuleName="")
Definition: pattern.cpp:207
bool operator!=(const Pattern &val) const
Definition: pattern.cpp:351
Address, address pair and other derived class representation.
std::vector< Pattern > PatternContainer
Definition: pattern.h:211
Definition: archive_wrapper.h:19