@@ -126,8 +126,7 @@ namespace hat::process {
126126 return {scanBytes, sizeOfImage};
127127 }
128128
129- std::span<std::byte> hat::process::module::get_section_data (const std::string_view name) const {
130- auto * bytes = reinterpret_cast <std::byte*>(this ->address ());
129+ std::span<std::byte> module::get_section_data (const std::string_view name) const {
131130 const auto & ntHeaders = getNTHeaders (*this );
132131
133132 const auto * sectionHeader = IMAGE_FIRST_SECTION (&ntHeaders);
@@ -138,15 +137,40 @@ namespace hat::process {
138137 };
139138 if (sectionName == name) {
140139 return {
141- bytes + sectionHeader->VirtualAddress ,
140+ reinterpret_cast <std::byte*>( this -> address () + sectionHeader->VirtualAddress ) ,
142141 static_cast <size_t >(sectionHeader->Misc .VirtualSize )
143142 };
144143 }
145144 }
146145 return {};
147146 }
148147
149- void hat::process::module::for_each_segment (const std::function<bool (std::span<std::byte>, hat::protection)>& callback) const {
148+ void module::for_each_section (const std::function<bool (std::string_view, std::span<std::byte>, hat::protection)>& callback) const {
149+ const auto & ntHeaders = getNTHeaders (*this );
150+
151+ const auto * sectionHeader = IMAGE_FIRST_SECTION (&ntHeaders);
152+ for (WORD i = 0 ; i < ntHeaders.FileHeader .NumberOfSections ; i++, sectionHeader++) {
153+ const std::string_view name{
154+ reinterpret_cast <const char *>(sectionHeader->Name ),
155+ strnlen_s (reinterpret_cast <const char *>(sectionHeader->Name ), IMAGE_SIZEOF_SHORT_NAME )
156+ };
157+ const std::span data{
158+ reinterpret_cast <std::byte*>(this ->address () + sectionHeader->VirtualAddress ),
159+ sectionHeader->Misc .VirtualSize
160+ };
161+
162+ hat::protection prot{};
163+ if (sectionHeader->Characteristics & IMAGE_SCN_MEM_READ ) prot |= hat::protection::Read;
164+ if (sectionHeader->Characteristics & IMAGE_SCN_MEM_WRITE ) prot |= hat::protection::Write;
165+ if (sectionHeader->Characteristics & IMAGE_SCN_MEM_EXECUTE ) prot |= hat::protection::Execute;
166+
167+ if (!callback (name, data, prot)) {
168+ break ;
169+ }
170+ }
171+ }
172+
173+ void module::for_each_segment (const std::function<bool (std::span<std::byte>, hat::protection)>& callback) const {
150174 const auto & ntHeaders = getNTHeaders (*this );
151175
152176 const auto * sectionHeader = IMAGE_FIRST_SECTION (&ntHeaders);
0 commit comments