retdec
sec_seg.h
Go to the documentation of this file.
1 
7 #ifndef RETDEC_FILEFORMAT_TYPES_SEC_SEG_SEC_SEG_H
8 #define RETDEC_FILEFORMAT_TYPES_SEC_SEG_SEC_SEG_H
9 
10 #include <string>
11 #include <vector>
12 
13 #include <llvm/ADT/StringRef.h>
14 
15 namespace retdec {
16 namespace fileformat {
17 
18 class FileFormat;
19 
23 class SecSeg
24 {
25  public:
26  enum class Type
27  {
29  CODE,
30  DATA,
31  CODE_DATA,
32  CONST_DATA,
33  BSS,
34  DEBUG,
35  INFO
36  };
37  private:
38  std::string crc32;
39  std::string md5;
40  std::string sha256;
41  std::string name;
42  llvm::StringRef bytes;
44  unsigned long long index = 0;
45  unsigned long long offset = 0;
46  unsigned long long fileSize = 0;
47  unsigned long long address = 0;
48  unsigned long long memorySize = 0;
49  unsigned long long entrySize = 0;
50  double entropy = 0.0;
51  bool memorySizeIsValid = false;
52  bool entrySizeIsValid = false;
53  bool isInMemory = false;
54  bool loaded = false;
55  bool isEntropyValid = false;
56 
57  void computeHashes();
58  public:
59  virtual ~SecSeg() = default;
60 
63  bool isUndefined() const;
64  bool isCode() const;
65  bool isData() const;
66  bool isCodeAndData() const;
67  bool isConstData() const;
68  bool isBss() const;
69  bool isDebug() const;
70  bool isInfo() const;
71  bool isSomeData() const;
72  bool isSomeCode() const;
73  bool isDataOnly() const;
74  bool isReadOnly() const;
76 
79  virtual bool isValid(const FileFormat *sOwner) const;
81 
84  std::string getCrc32() const;
85  std::string getMd5() const;
86  std::string getSha256() const;
87  std::string getName() const;
88  const char* getNameAsCStr() const;
89  const llvm::StringRef getBytes(unsigned long long sOffset = 0, unsigned long long sSize = 0) const;
90  SecSeg::Type getType() const;
91  unsigned long long getIndex() const;
92  unsigned long long getOffset() const;
93  unsigned long long getEndOffset() const;
94  unsigned long long getSizeInFile() const;
95  unsigned long long getLoadedSize() const;
96  unsigned long long getAddress() const;
97  unsigned long long getEndAddress() const;
98  bool getSizeInMemory(unsigned long long &sMemorySize) const;
99  bool getSizeOfOneEntry(unsigned long long &sEntrySize) const;
100  bool getMemory() const;
101  bool getEntropy(double &res) const;
102 
110  template<typename NumberType>
111  NumberType getBytesAtOffsetAsNumber(unsigned long long sOffset) const
112  {
113  if(bytes.size() < sizeof(NumberType) || sOffset > bytes.size() - sizeof(NumberType))
114  {
115  return {};
116  }
117 
118  auto rawBytes = bytes.data();
119  return *reinterpret_cast<const NumberType *>(&rawBytes[sOffset]);
120  }
122 
125  bool getBits(std::string &sResult) const;
126  bool getBytes(std::vector<unsigned char> &sResult, unsigned long long sOffset = 0, unsigned long long sSize = 0) const;
127  bool getString(std::string &sResult, unsigned long long sOffset = 0, unsigned long long sSize = 0) const;
128  bool getHexBytes(std::string &sResult) const;
130 
133  void setName(std::string sName);
134  void setType(SecSeg::Type sType);
135  void setIndex(unsigned long long sIndex);
136  void setOffset(unsigned long long sOffset);
137  void setSizeInFile(unsigned long long sFileSize);
138  void setAddress(unsigned long long sAddress);
139  void setSizeInMemory(unsigned long long sMemorySize);
140  void setSizeOfOneEntry(unsigned long long sEntrySize);
141  void setMemory(bool sMemory);
143 
146  void computeEntropy();
147  void invalidateMemorySize();
148  void invalidateEntrySize();
149  void load(const FileFormat *sOwner);
150  void dump(std::string &sDump) const;
151  bool hasCrc32() const;
152  bool hasMd5() const;
153  bool hasSha256() const;
154  bool hasEmptyName() const;
155  bool belong(unsigned long long sAddress) const;
156  bool operator<(const SecSeg &sOther) const;
158 };
159 
160 } // namespace fileformat
161 } // namespace retdec
162 
163 #endif
Definition: file_format.h:45
Definition: sec_seg.h:24
bool getString(std::string &sResult, unsigned long long sOffset=0, unsigned long long sSize=0) const
Definition: sec_seg.cpp:416
bool isUndefined() const
Definition: sec_seg.cpp:39
NumberType getBytesAtOffsetAsNumber(unsigned long long sOffset) const
Definition: sec_seg.h:111
bool getSizeInMemory(unsigned long long &sMemorySize) const
Definition: sec_seg.cpp:323
void setSizeInFile(unsigned long long sFileSize)
Definition: sec_seg.cpp:478
void computeEntropy()
Definition: sec_seg.cpp:524
bool hasSha256() const
Definition: sec_seg.cpp:684
bool isConstData() const
Definition: sec_seg.cpp:75
double entropy
entropy in <0,8>
Definition: sec_seg.h:50
void computeHashes()
Definition: sec_seg.cpp:27
bool getBits(std::string &sResult) const
Definition: sec_seg.cpp:379
unsigned long long getEndAddress() const
Definition: sec_seg.cpp:305
void invalidateEntrySize()
Definition: sec_seg.cpp:561
const char * getNameAsCStr() const
Definition: sec_seg.cpp:214
bool getEntropy(double &res) const
Definition: sec_seg.cpp:364
Type
Definition: sec_seg.h:27
@ INFO
auxiliary information
@ CODE_DATA
code and (or) data
bool isSomeData() const
Definition: sec_seg.cpp:110
const llvm::StringRef getBytes(unsigned long long sOffset=0, unsigned long long sSize=0) const
Definition: sec_seg.cpp:227
bool isBss() const
Definition: sec_seg.cpp:84
llvm::StringRef bytes
reference to content of section or segment
Definition: sec_seg.h:42
unsigned long long getOffset() const
Definition: sec_seg.cpp:259
void setAddress(unsigned long long sAddress)
Definition: sec_seg.cpp:487
bool getSizeOfOneEntry(unsigned long long &sEntrySize) const
Definition: sec_seg.cpp:340
bool isDataOnly() const
Definition: sec_seg.cpp:126
std::string getCrc32() const
Definition: sec_seg.cpp:166
unsigned long long fileSize
size in file
Definition: sec_seg.h:46
bool getHexBytes(std::string &sResult) const
Definition: sec_seg.cpp:432
Type type
type
Definition: sec_seg.h:43
virtual bool isValid(const FileFormat *sOwner) const
Definition: sec_seg.cpp:144
void dump(std::string &sDump) const
Definition: sec_seg.cpp:594
SecSeg::Type getType() const
Definition: sec_seg.cpp:241
unsigned long long getSizeInFile() const
Definition: sec_seg.cpp:278
void load(const FileFormat *sOwner)
Definition: sec_seg.cpp:572
bool getMemory() const
Definition: sec_seg.cpp:354
bool hasEmptyName() const
Definition: sec_seg.cpp:692
void setOffset(unsigned long long sOffset)
Definition: sec_seg.cpp:469
std::string name
name of section or segment
Definition: sec_seg.h:41
unsigned long long getIndex() const
Definition: sec_seg.cpp:250
bool isInMemory
true if the section or segment will appear in the memory image of a process
Definition: sec_seg.h:53
bool isData() const
Definition: sec_seg.cpp:57
bool entrySizeIsValid
size of one entry in section or segment
Definition: sec_seg.h:52
virtual ~SecSeg()=default
bool isCode() const
Definition: sec_seg.cpp:48
unsigned long long getLoadedSize() const
Definition: sec_seg.cpp:287
void setSizeOfOneEntry(unsigned long long sEntrySize)
Definition: sec_seg.cpp:506
bool isInfo() const
Definition: sec_seg.cpp:102
bool isEntropyValid
true if entropy has been computed
Definition: sec_seg.h:55
void invalidateMemorySize()
Definition: sec_seg.cpp:549
std::string sha256
SHA256 of section or segment data.
Definition: sec_seg.h:40
std::string crc32
CRC32 of section or segment data.
Definition: sec_seg.h:38
bool belong(unsigned long long sAddress) const
Definition: sec_seg.cpp:701
bool loaded
true if content of section or segment was successfully loaded from input file
Definition: sec_seg.h:54
bool memorySizeIsValid
true if size in memory is valid
Definition: sec_seg.h:51
unsigned long long offset
start offset in file
Definition: sec_seg.h:45
bool operator<(const SecSeg &sOther) const
Definition: sec_seg.cpp:710
void setIndex(unsigned long long sIndex)
Definition: sec_seg.cpp:460
unsigned long long address
start address in memory
Definition: sec_seg.h:47
unsigned long long getEndOffset() const
Definition: sec_seg.cpp:268
bool isSomeCode() const
Definition: sec_seg.cpp:118
unsigned long long entrySize
size of one entry in file
Definition: sec_seg.h:49
unsigned long long getAddress() const
Definition: sec_seg.cpp:296
void setType(SecSeg::Type sType)
Definition: sec_seg.cpp:451
bool isReadOnly() const
Definition: sec_seg.cpp:134
bool isCodeAndData() const
Definition: sec_seg.cpp:66
unsigned long long index
index
Definition: sec_seg.h:44
std::string getName() const
Definition: sec_seg.cpp:193
std::string getSha256() const
Definition: sec_seg.cpp:184
unsigned long long memorySize
size in memory
Definition: sec_seg.h:48
void setMemory(bool sMemory)
Definition: sec_seg.cpp:516
bool hasCrc32() const
Definition: sec_seg.cpp:666
std::string md5
MD5 of section or segment data.
Definition: sec_seg.h:39
std::string getMd5() const
Definition: sec_seg.cpp:175
bool isDebug() const
Definition: sec_seg.cpp:93
bool hasMd5() const
Definition: sec_seg.cpp:675
void setSizeInMemory(unsigned long long sMemorySize)
Definition: sec_seg.cpp:496
void setName(std::string sName)
Definition: sec_seg.cpp:442
Definition: archive_wrapper.h:19