Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
branchseer committed Jan 10, 2024
1 parent 2c3e9d1 commit 1537a56
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 36 deletions.
33 changes: 18 additions & 15 deletions src/postject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

#include "./postject.hpp"

postject::ExecutableFormat postject::get_executable_format(const std::vector<uint8_t>& executable) {
postject::ExecutableFormat postject::get_executable_format(
const std::vector<uint8_t>& executable) {
if (LIEF::ELF::is_elf(executable)) {
return ExecutableFormat::kELF;
} else if (LIEF::MachO::is_macho(executable)) {
Expand All @@ -19,10 +20,11 @@ postject::ExecutableFormat postject::get_executable_format(const std::vector<uin
return ExecutableFormat::kUnknown;
}

postject::InjectResult postject::inject_into_elf(const std::vector<uint8_t>& executable,
const std::string& note_name,
const std::vector<uint8_t>& data,
bool overwrite) {
postject::InjectResult postject::inject_into_elf(
const std::vector<uint8_t>& executable,
const std::string& note_name,
const std::vector<uint8_t>& data,
bool overwrite) {
InjectResult result;
std::unique_ptr<LIEF::ELF::Binary> binary =
LIEF::ELF::Parser::parse(executable);
Expand Down Expand Up @@ -59,11 +61,12 @@ postject::InjectResult postject::inject_into_elf(const std::vector<uint8_t>& exe
return result;
}

postject::InjectResult postject::inject_into_macho(const std::vector<uint8_t>& executable,
const std::string& segment_name,
const std::string& section_name,
const std::vector<uint8_t>& data,
bool overwrite) {
postject::InjectResult postject::inject_into_macho(
const std::vector<uint8_t>& executable,
const std::string& segment_name,
const std::string& section_name,
const std::vector<uint8_t>& data,
bool overwrite) {
InjectResult result;
std::unique_ptr<LIEF::MachO::FatBinary> fat_binary =
LIEF::MachO::Parser::parse(executable);
Expand All @@ -80,7 +83,6 @@ postject::InjectResult postject::inject_into_macho(const std::vector<uint8_t>& e

if (existing_section) {
if (!overwrite) {

result.type = InjectResultType::kAlreadyExists;
return result;
}
Expand Down Expand Up @@ -115,10 +117,11 @@ postject::InjectResult postject::inject_into_macho(const std::vector<uint8_t>& e
return result;
}

postject::InjectResult postject::inject_into_pe(const std::vector<uint8_t>& executable,
const std::string& resource_name,
const std::vector<uint8_t>& data,
bool overwrite) {
postject::InjectResult postject::inject_into_pe(
const std::vector<uint8_t>& executable,
const std::string& resource_name,
const std::vector<uint8_t>& data,
bool overwrite) {
InjectResult result;

std::unique_ptr<LIEF::PE::Binary> binary =
Expand Down
30 changes: 9 additions & 21 deletions src/postject_wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "./postject.hpp"


std::vector<uint8_t> vec_from_val(const emscripten::val& value) {
// We are using `convertJSArrayToNumberVector()` instead of `vecFromJSArray()`
// because it is faster. It is okay if we use it without additional type
Expand All @@ -13,7 +12,8 @@ std::vector<uint8_t> vec_from_val(const emscripten::val& value) {
return emscripten::convertJSArrayToNumberVector<uint8_t>(value);
}

postject::ExecutableFormat get_executable_format(const emscripten::val& executable) {
postject::ExecutableFormat get_executable_format(
const emscripten::val& executable) {
return postject::get_executable_format(vec_from_val(executable));
}

Expand All @@ -24,52 +24,40 @@ emscripten::val inject_result_to_val(postject::InjectResult injectResult) {
std::vector<uint8_t> output = std::move(injectResult.output);
emscripten::val view{
emscripten::typed_memory_view(output.size(), output.data())};
auto output_data = emscripten::val::global("Uint8Array").new_(output.size());
auto output_data =
emscripten::val::global("Uint8Array").new_(output.size());
output_data.call<void>("set", view);
object.set("data", emscripten::val(output_data));
} else {
object.set("data", emscripten::val::undefined());
}
return object;

}

emscripten::val inject_into_elf(const emscripten::val& executable,
const std::string& note_name,
const emscripten::val& data,
bool overwrite) {
return inject_result_to_val(postject::inject_into_elf(
vec_from_val(executable),
note_name,
vec_from_val(data),
overwrite
));
vec_from_val(executable), note_name, vec_from_val(data), overwrite));
}

emscripten::val inject_into_macho(const emscripten::val& executable,
const std::string& segment_name,
const std::string& section_name,
const emscripten::val& data,
bool overwrite) {
return inject_result_to_val(postject::inject_into_macho(
vec_from_val(executable),
segment_name,
section_name,
vec_from_val(data),
overwrite
));
return inject_result_to_val(
postject::inject_into_macho(vec_from_val(executable), segment_name,
section_name, vec_from_val(data), overwrite));
}

emscripten::val inject_into_pe(const emscripten::val& executable,
const std::string& resource_name,
const emscripten::val& data,
bool overwrite) {
return inject_result_to_val(postject::inject_into_pe(
vec_from_val(executable),
resource_name,
vec_from_val(data),
overwrite
));
vec_from_val(executable), resource_name, vec_from_val(data), overwrite));
}

EMSCRIPTEN_BINDINGS(postject) {
Expand Down

0 comments on commit 1537a56

Please sign in to comment.