Skip to content

Commit

Permalink
shorten some code in switch
Browse files Browse the repository at this point in the history
  • Loading branch information
manorit2001 committed Oct 22, 2021
1 parent 01f0a50 commit 84a2bce
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
17 changes: 17 additions & 0 deletions src/libinjector/win/method_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,20 @@ bool is_fun_error(drakvuf_t drakvuf, drakvuf_trap_info_t* info, const char* err)
}
return false;
}

bool open_host_file(injector_t injector, const char* mode)
{
injector->host_file = fopen(injector->binary_path, mode);

if (!injector->host_file)
{
PRINT_DEBUG("Failed to open host file\n");
injector->rc = INJECTOR_FAILED_WITH_ERROR_CODE;
injector->error_code.code = errno;
injector->error_code.string = "HOST_FAILED_FOPEN";
injector->error_code.valid = true;

return false;
}
return true;
}
1 change: 1 addition & 0 deletions src/libinjector/win/method_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
#include "win_utils.h"

bool setup_create_file(drakvuf_t drakvuf, drakvuf_trap_info_t* info);
bool open_host_file(injector_t injector, const char* mode);
bool is_fun_error(drakvuf_t drakvuf, drakvuf_trap_info_t* info, const char* err);

#endif
11 changes: 1 addition & 10 deletions src/libinjector/win/methods/win_read_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,9 @@ event_response_t handle_readfile_x64(drakvuf_t drakvuf, drakvuf_trap_info_t* inf
return cleanup(injector, info);

injector->file_handle = info->regs->rax;
injector->host_file = fopen(injector->binary_path, "wb");

if (!injector->host_file)
{
PRINT_DEBUG("Failed to open host file\n");
injector->rc = INJECTOR_FAILED_WITH_ERROR_CODE;
injector->error_code.code = errno;
injector->error_code.string = "HOST_FAILED_FOPEN";
injector->error_code.valid = true;

if (!open_host_file(injector, "wb"))
return cleanup(injector, info);
}

PRINT_DEBUG("Reading file...\n");

Expand Down
12 changes: 2 additions & 10 deletions src/libinjector/win/methods/win_write_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,10 @@ event_response_t handle_writefile_x64(drakvuf_t drakvuf, drakvuf_trap_info_t* in
return cleanup(injector, info);

injector->file_handle = info->regs->rax;
injector->host_file = fopen(injector->binary_path, "rb");

if (!injector->host_file)
{
PRINT_DEBUG("Failed to open host file\n");
injector->rc = INJECTOR_FAILED_WITH_ERROR_CODE;
injector->error_code.code = errno;
injector->error_code.string = "HOST_FAILED_FOPEN";
injector->error_code.valid = true;

if(!open_host_file(injector, "rb"))
return cleanup(injector, info);
}

}
// fall through
case STEP6: // read chunk from host and write to guest
Expand Down

0 comments on commit 84a2bce

Please sign in to comment.