Skip to content

Commit

Permalink
Enable to release binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Aug 13, 2022
1 parent 27754bc commit ad81191
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion api/c/MachO/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,25 @@ using namespace LIEF::MachO;

Macho_Binary_t** macho_parse(const char *file) {
FatBinary* fat = Parser::parse(file).release();
if (fat == nullptr) {
return nullptr;
}

size_t nb_bin = fat->size();

auto** c_macho_binaries = static_cast<Macho_Binary_t**>(
malloc((fat->size() + 1) * sizeof(Macho_Binary_t**)));

for (size_t i = 0; i < nb_bin; ++i) {
Binary* binary = fat->take(i).release();
Binary* binary = fat->at(i);
if (binary != nullptr) {
c_macho_binaries[i] = static_cast<Macho_Binary_t*>(malloc(sizeof(Macho_Binary_t)));
init_c_binary(c_macho_binaries[i], binary);
}
}

fat->release_all_binaries();

c_macho_binaries[nb_bin] = nullptr;
delete fat;

Expand Down
2 changes: 2 additions & 0 deletions include/LIEF/MachO/FatBinary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class LIEF_API FatBinary {
it_binaries end();
it_const_binaries end() const;

void release_all_binaries();

//! Get a pointer to the last MachO::Binary object presents in this Fat Binary.
//! It returns a nullptr if no binary are present.
std::unique_ptr<Binary> pop_back();
Expand Down
6 changes: 6 additions & 0 deletions src/MachO/FatBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ std::vector<uint8_t> FatBinary::raw() {
return buffer;
}

void FatBinary::release_all_binaries() {
for (auto& bin : binaries_) {
bin.release();
}
}

std::ostream& operator<<(std::ostream& os, const FatBinary& fatbinary) {
for (const Binary& binary : fatbinary) {
os << binary;
Expand Down

0 comments on commit ad81191

Please sign in to comment.