Skip to content

Commit

Permalink
Refactor: Change where the output file is created
Browse files Browse the repository at this point in the history
  • Loading branch information
czs108 committed Feb 6, 2024
1 parent 6ae008e commit 0685211
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 19 deletions.
4 changes: 2 additions & 2 deletions include/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ void FreePeImage(
* @public @memberof _PE_IMAGE_INFO
*
* @param image_info The PE image.
* @param file_name The name of the new file.
* @param file The file.
* @return `true` if the method succeeded, otherwise `false`.
*/
bool WriteImageToFile(
const PE_IMAGE_INFO *const image_info,
const TCHAR *const file_name);
const HANDLE file);


/**
Expand Down
19 changes: 2 additions & 17 deletions src/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,30 +102,21 @@ bool LoadPeImage(

bool WriteImageToFile(
const PE_IMAGE_INFO *const image_info,
const TCHAR *const file_name)
const HANDLE file)
{
assert(image_info != NULL);
assert(file_name != NULL);
assert(file != INVALID_HANDLE_VALUE);

bool success = false;
HANDLE file = CreateFile(file_name, GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE)
{
SetLastErrorCode();
goto _Exit;
}

// save the PE headers
const DWORD file_align = image_info->nt_header->OptionalHeader.FileAlignment;
const DWORD header_size = CalcHeadersSize(image_info->image_base, NULL, NULL);
if (WriteAllToFile(file, image_info->image_base, header_size) == false)
{
goto _Exit;
}

// save the data of all sections
const DWORD alignment = image_info->nt_header->OptionalHeader.FileAlignment;
const WORD section_num = image_info->nt_header->FileHeader.NumberOfSections;
const IMAGE_SECTION_HEADER *const section_header = image_info->section_header;
for (WORD i = 0; i != section_num; ++i)
Expand All @@ -150,12 +141,6 @@ bool WriteImageToFile(
success = true;

_Exit:
if (file != INVALID_HANDLE_VALUE)
{
CloseHandle(file);
file = INVALID_HANDLE_VALUE;
}

return success;
}

Expand Down

0 comments on commit 0685211

Please sign in to comment.