pe_section.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #include <string.h>
  2. #include "utils.h"
  3. #include "pe_section.h"
  4. namespace pe_bliss
  5. {
  6. using namespace pe_win;
  7. //Section structure default constructor
  8. section::section()
  9. :old_size_(static_cast<size_t>(-1))
  10. {
  11. memset(&header_, 0, sizeof(image_section_header));
  12. }
  13. //Sets the name of section (8 characters maximum)
  14. void section::set_name(const std::string& name)
  15. {
  16. memset(header_.Name, 0, sizeof(header_.Name));
  17. memcpy(header_.Name, name.c_str(), std::min<size_t>(name.length(), sizeof(header_.Name)));
  18. }
  19. //Returns section name
  20. const std::string section::get_name() const
  21. {
  22. char buf[9] = {0};
  23. memcpy(buf, header_.Name, 8);
  24. return std::string(buf);
  25. }
  26. //Set flag (attribute) of section
  27. section& section::set_flag(uint32_t flag, bool setflag)
  28. {
  29. if(setflag)
  30. header_.Characteristics |= flag;
  31. else
  32. header_.Characteristics &= ~flag;
  33. return *this;
  34. }
  35. //Sets "readable" attribute of section
  36. section& section::readable(bool readable)
  37. {
  38. return set_flag(image_scn_mem_read, readable);
  39. }
  40. //Sets "writeable" attribute of section
  41. section& section::writeable(bool writeable)
  42. {
  43. return set_flag(image_scn_mem_write, writeable);
  44. }
  45. //Sets "executable" attribute of section
  46. section& section::executable(bool executable)
  47. {
  48. return set_flag(image_scn_mem_execute, executable);
  49. }
  50. //Sets "shared" attribute of section
  51. section& section::shared(bool shared)
  52. {
  53. return set_flag(image_scn_mem_shared, shared);
  54. }
  55. //Sets "discardable" attribute of section
  56. section& section::discardable(bool discardable)
  57. {
  58. return set_flag(image_scn_mem_discardable, discardable);
  59. }
  60. //Returns true if section is readable
  61. bool section::readable() const
  62. {
  63. return (header_.Characteristics & image_scn_mem_read) != 0;
  64. }
  65. //Returns true if section is writeable
  66. bool section::writeable() const
  67. {
  68. return (header_.Characteristics & image_scn_mem_write) != 0;
  69. }
  70. //Returns true if section is executable
  71. bool section::executable() const
  72. {
  73. return (header_.Characteristics & image_scn_mem_execute) != 0;
  74. }
  75. bool section::shared() const
  76. {
  77. return (header_.Characteristics & image_scn_mem_shared) != 0;
  78. }
  79. bool section::discardable() const
  80. {
  81. return (header_.Characteristics & image_scn_mem_discardable) != 0;
  82. }
  83. //Returns true if section has no RAW data
  84. bool section::empty() const
  85. {
  86. if(old_size_ != static_cast<size_t>(-1)) //If virtual memory is mapped, check raw data length (old_size_)
  87. return old_size_ == 0;
  88. else
  89. return raw_data_.empty();
  90. }
  91. //Returns raw section data from file image
  92. std::string& section::get_raw_data()
  93. {
  94. unmap_virtual();
  95. return raw_data_;
  96. }
  97. //Sets raw section data from file image
  98. void section::set_raw_data(const std::string& data)
  99. {
  100. old_size_ = static_cast<size_t>(-1);
  101. raw_data_ = data;
  102. }
  103. //Returns raw section data from file image
  104. const std::string& section::get_raw_data() const
  105. {
  106. unmap_virtual();
  107. return raw_data_;
  108. }
  109. //Returns mapped virtual section data
  110. const std::string& section::get_virtual_data(uint32_t section_alignment) const
  111. {
  112. map_virtual(section_alignment);
  113. return raw_data_;
  114. }
  115. //Returns mapped virtual section data
  116. std::string& section::get_virtual_data(uint32_t section_alignment)
  117. {
  118. map_virtual(section_alignment);
  119. return raw_data_;
  120. }
  121. //Maps virtual section data
  122. void section::map_virtual(uint32_t section_alignment) const
  123. {
  124. uint32_t aligned_virtual_size = get_aligned_virtual_size(section_alignment);
  125. if(old_size_ == static_cast<size_t>(-1) && aligned_virtual_size && aligned_virtual_size > raw_data_.length())
  126. {
  127. old_size_ = raw_data_.length();
  128. raw_data_.resize(aligned_virtual_size, 0);
  129. }
  130. }
  131. //Unmaps virtual section data
  132. void section::unmap_virtual() const
  133. {
  134. if(old_size_ != static_cast<size_t>(-1))
  135. {
  136. raw_data_.resize(old_size_, 0);
  137. old_size_ = static_cast<size_t>(-1);
  138. }
  139. }
  140. //Returns section virtual size
  141. uint32_t section::get_virtual_size() const
  142. {
  143. return header_.Misc.VirtualSize;
  144. }
  145. //Returns section virtual address
  146. uint32_t section::get_virtual_address() const
  147. {
  148. return header_.VirtualAddress;
  149. }
  150. //Returns size of section raw data
  151. uint32_t section::get_size_of_raw_data() const
  152. {
  153. return header_.SizeOfRawData;
  154. }
  155. //Returns pointer to raw section data in PE file
  156. uint32_t section::get_pointer_to_raw_data() const
  157. {
  158. return header_.PointerToRawData;
  159. }
  160. //Returns section characteristics
  161. uint32_t section::get_characteristics() const
  162. {
  163. return header_.Characteristics;
  164. }
  165. //Returns raw image section header
  166. const pe_win::image_section_header& section::get_raw_header() const
  167. {
  168. return header_;
  169. }
  170. //Returns raw image section header
  171. pe_win::image_section_header& section::get_raw_header()
  172. {
  173. return header_;
  174. }
  175. //Calculates aligned virtual section size
  176. uint32_t section::get_aligned_virtual_size(uint32_t section_alignment) const
  177. {
  178. if(get_size_of_raw_data())
  179. {
  180. if(!get_virtual_size())
  181. {
  182. //If section virtual size is zero
  183. //Set aligned virtual size of section as aligned raw size
  184. return pe_utils::align_up(get_size_of_raw_data(), section_alignment);
  185. }
  186. }
  187. return pe_utils::align_up(get_virtual_size(), section_alignment);
  188. }
  189. //Calculates aligned raw section size
  190. uint32_t section::get_aligned_raw_size(uint32_t file_alignment) const
  191. {
  192. if(get_size_of_raw_data())
  193. return pe_utils::align_up(get_size_of_raw_data(), file_alignment);
  194. else
  195. return 0;
  196. }
  197. //Sets size of raw section data
  198. void section::set_size_of_raw_data(uint32_t size_of_raw_data)
  199. {
  200. header_.SizeOfRawData = size_of_raw_data;
  201. }
  202. //Sets pointer to section raw data
  203. void section::set_pointer_to_raw_data(uint32_t pointer_to_raw_data)
  204. {
  205. header_.PointerToRawData = pointer_to_raw_data;
  206. }
  207. //Sets section characteristics
  208. void section::set_characteristics(uint32_t characteristics)
  209. {
  210. header_.Characteristics = characteristics;
  211. }
  212. //Sets section virtual size
  213. void section::set_virtual_size(uint32_t virtual_size)
  214. {
  215. header_.Misc.VirtualSize = virtual_size;
  216. }
  217. //Sets section virtual address
  218. void section::set_virtual_address(uint32_t virtual_address)
  219. {
  220. header_.VirtualAddress = virtual_address;
  221. }
  222. //Section by file offset finder helper (4gb max)
  223. section_by_raw_offset::section_by_raw_offset(uint32_t offset)
  224. :offset_(offset)
  225. {}
  226. bool section_by_raw_offset::operator()(const section& s) const
  227. {
  228. return (s.get_pointer_to_raw_data() <= offset_)
  229. && (s.get_pointer_to_raw_data() + s.get_size_of_raw_data() > offset_);
  230. }
  231. section_ptr_finder::section_ptr_finder(const section& s)
  232. :s_(s)
  233. {}
  234. bool section_ptr_finder::operator()(const section& s) const
  235. {
  236. return &s == &s_;
  237. }
  238. }