Skip to content

Commit 406a7ce

Browse files
committed
Restructured code (1/4)
1 parent b8625dc commit 406a7ce

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

NativeCore/Windows/EnumerateRemoteSectionsAndModules.cpp

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -160,60 +160,69 @@ void RC_CallConv EnumerateRemoteSectionsAndModules(RC_Pointer process, Enumerate
160160

161161
section.Category = section.Type == SectionType::Private ? SectionCategory::HEAP : SectionCategory::Unknown;
162162

163-
sections.push_back(std::move(section));
163+
sections.push_back(section);
164164
}
165165
address = reinterpret_cast<size_t>(memInfo.BaseAddress) + memInfo.RegionSize;
166166
}
167167

168168
const auto moduleEnumerator = [&](EnumerateRemoteModuleData* data)
169169
{
170170
if (callbackModule != nullptr)
171+
{
171172
callbackModule(data);
173+
}
172174

173175
if (callbackSection != nullptr)
174176
{
175177
auto it = std::lower_bound(std::begin(sections), std::end(sections), static_cast<LPVOID>(data->BaseAddress), [&sections](const auto& lhs, const LPVOID& rhs)
176-
{
177-
return lhs.BaseAddress < rhs;
178-
});
179-
180-
IMAGE_DOS_HEADER DosHdr = {};
181-
IMAGE_NT_HEADERS NtHdr = {};
178+
{
179+
return lhs.BaseAddress < rhs;
180+
});
182181

183-
ReadRemoteMemory(process, data->BaseAddress, &DosHdr, 0, sizeof(IMAGE_DOS_HEADER));
184-
ReadRemoteMemory(process, PUCHAR(data->BaseAddress) + DosHdr.e_lfanew, &NtHdr, 0, sizeof(IMAGE_NT_HEADERS));
182+
IMAGE_DOS_HEADER imageDosHeader = {};
183+
IMAGE_NT_HEADERS imageNtHeaders = {};
185184

186-
std::vector<IMAGE_SECTION_HEADER> sectionHeaders(NtHdr.FileHeader.NumberOfSections);
187-
ReadRemoteMemory(process, PUCHAR(data->BaseAddress) + DosHdr.e_lfanew + sizeof(IMAGE_NT_HEADERS), sectionHeaders.data(), 0, NtHdr.FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER));
188-
for (auto i = 0; i < NtHdr.FileHeader.NumberOfSections; ++i)
185+
if (!ReadRemoteMemory(process, data->BaseAddress, &imageDosHeader, 0, sizeof(IMAGE_DOS_HEADER))
186+
|| !ReadRemoteMemory(process, PUCHAR(data->BaseAddress) + imageDosHeader.e_lfanew, &imageNtHeaders, 0, sizeof(IMAGE_NT_HEADERS)))
189187
{
190-
auto&& sectionHeader = sectionHeaders[i];
188+
return;
189+
}
191190

191+
std::vector<IMAGE_SECTION_HEADER> sectionHeaders(imageNtHeaders.FileHeader.NumberOfSections);
192+
ReadRemoteMemory(process, PUCHAR(data->BaseAddress) + imageDosHeader.e_lfanew + sizeof(IMAGE_NT_HEADERS), sectionHeaders.data(), 0, imageNtHeaders.FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER));
193+
for (auto&& sectionHeader : sectionHeaders)
194+
{
192195
const auto sectionAddress = reinterpret_cast<size_t>(data->BaseAddress) + sectionHeader.VirtualAddress;
193-
for (auto j = it; j != std::end(sections); ++j)
196+
197+
for (; it != std::end(sections); ++it)
194198
{
195-
if (sectionAddress >= reinterpret_cast<size_t>(j->BaseAddress)
196-
&& sectionAddress < reinterpret_cast<size_t>(j->BaseAddress) + static_cast<size_t>(j->Size)
199+
auto&& section = *it;
200+
201+
if (sectionAddress >= reinterpret_cast<size_t>(section.BaseAddress)
202+
&& sectionAddress < reinterpret_cast<size_t>(section.BaseAddress) + static_cast<size_t>(section.Size)
197203
&& sectionHeader.VirtualAddress + sectionHeader.Misc.VirtualSize <= data->Size)
198204
{
199205
if ((sectionHeader.Characteristics & IMAGE_SCN_CNT_CODE) == IMAGE_SCN_CNT_CODE)
200206
{
201-
j->Category = SectionCategory::CODE;
207+
section.Category = SectionCategory::CODE;
202208
}
203209
else if (sectionHeader.Characteristics & (IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_CNT_UNINITIALIZED_DATA))
204210
{
205-
j->Category = SectionCategory::DATA;
211+
section.Category = SectionCategory::DATA;
206212
}
207213

208-
try {
214+
try
215+
{
209216
// Copy the name because it is not null padded.
210217
char buffer[IMAGE_SIZEOF_SHORT_NAME + 1] = { 0 };
211218
std::memcpy(buffer, sectionHeader.Name, IMAGE_SIZEOF_SHORT_NAME);
212-
MultiByteToUnicode(buffer, j->Name, IMAGE_SIZEOF_SHORT_NAME);
213-
} catch (std::range_error &) {
214-
std::memset(j->Name, 0, sizeof j->Name);
219+
MultiByteToUnicode(buffer, section.Name, IMAGE_SIZEOF_SHORT_NAME);
220+
}
221+
catch (std::range_error &)
222+
{
223+
std::memset(section.Name, 0, sizeof(section.Name));
215224
}
216-
std::memcpy(j->ModulePath, data->Path, std::min(MAX_PATH, PATH_MAXIMUM_LENGTH));
225+
std::memcpy(section.ModulePath, data->Path, std::min(MAX_PATH, PATH_MAXIMUM_LENGTH));
217226

218227
break;
219228
}
@@ -222,8 +231,10 @@ void RC_CallConv EnumerateRemoteSectionsAndModules(RC_Pointer process, Enumerate
222231
}
223232
};
224233

225-
if(EnumerateRemoteModulesNative(process, moduleEnumerator) != ERROR_SUCCESS)
234+
if (EnumerateRemoteModulesNative(process, moduleEnumerator) != ERROR_SUCCESS)
235+
{
226236
EnumerateRemoteModulesWinapi(process, moduleEnumerator);
237+
}
227238

228239
if (callbackSection != nullptr)
229240
{

0 commit comments

Comments
 (0)