Skip to content
Permalink
Browse files
Improve API of Mach-O's Header
API changes:
  - 'has_flag' renamed to 'has'
  - 'add' to add a flag - added
  - 'remove' to remove a flag - added
  - operator+= to add a flag - added
  - operator-= to remove a flag - added
  • Loading branch information
romainthomas committed Aug 1, 2017
1 parent 5666351 commit cbe8354
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
@@ -54,7 +54,7 @@ void init_MachO_Header_class(py::module& m) {
"Binary's type ( " RST_CLASS_REF(lief.MachO.FILE_TYPES) ")")

.def_property("flags",
&Header::flags_list,
static_cast<getter_t<uint32_t>>(&Header::flags),
static_cast<setter_t<uint32_t>>(&Header::flags),
"Binary's flags ( " RST_CLASS_REF(lief.MachO.HEADER_FLAGS) ")")

@@ -73,6 +73,26 @@ void init_MachO_Header_class(py::module& m) {
static_cast<setter_t<uint32_t>>(&Header::reserved),
"")

.def_property_readonly("flags_list",
&Header::flags_list,
"" RST_CLASS_REF(lief.PE.HEADER_FLAGS) " as a list")

.def("add",
static_cast<void (Header::*)(HEADER_FLAGS)>(&Header::add),
"Add the given " RST_CLASS_REF(lief.MachO.HEADER_FLAGS) "",
"flag"_a)

.def("remove",
static_cast<void (Header::*)(HEADER_FLAGS)>(&Header::remove),
"Remove the given " RST_CLASS_REF(lief.MachO.HEADER_FLAGS) "",
"flag"_a)

.def("has",
static_cast<bool (Header::*)(HEADER_FLAGS) const>(&Header::has),
"``True`` if the given " RST_CLASS_REF(lief.MachO.HEADER_FLAGS) " is in the "
":attr:`~lief.MachO.Header.flags`",
"flag"_a)


.def("__eq__", &Header::operator==)
.def("__ne__", &Header::operator!=)
@@ -81,6 +101,13 @@ void init_MachO_Header_class(py::module& m) {
return LIEF::Hash::hash(header);
})

.def(py::self += HEADER_FLAGS())
.def(py::self -= HEADER_FLAGS())

.def("__contains__",
static_cast<bool (Header::*)(HEADER_FLAGS) const>(&Header::has),
"Check if the given " RST_CLASS_REF(lief.MachO.HEADER_FLAGS) " is present")


.def("__str__",
[] (const Header& header)
@@ -64,16 +64,13 @@ def print_header(binary):

print("== Header ==")
header = binary.header
flags = ""
for flag in header.flags:
flag = str(flag).split(".")[-1]
flags += (flag if len(flags) == 0 else " - " + flag)
flags_str = " - ".join([str(s).split(".")[-1] for s in header.flags_list])

print(format_str.format("Magic:", str(header.magic).split(".")[-1]))
print(format_str.format("CPU Type:", str(header.cpu_type).split(".")[-1]))
print(format_hex.format("CPU sub-type:", header.cpu_subtype))
print(format_str.format("File Type:", str(header.file_type).split(".")[-1]))
print(format_str.format("Flags:", flags))
print(format_str.format("Flags:", flags_str))
print(format_dec.format("Number of commands:", header.nb_cmds))
print(format_hex.format("Size of commands:", header.sizeof_cmds))
print(format_hex.format("Reserved:", header.reserved))
@@ -44,12 +44,14 @@ class DLL_PUBLIC Header : public Visitable {
uint32_t cpu_subtype(void) const;
FILE_TYPES file_type(void) const;
std::set<HEADER_FLAGS> flags_list(void) const;
bool has_flag(HEADER_FLAGS flag) const;
bool has(HEADER_FLAGS flag) const;
uint32_t nb_cmds(void) const;
uint32_t sizeof_cmds(void) const;
uint32_t flags(void) const;
uint32_t reserved(void) const;

void add(HEADER_FLAGS flag);

//! @brief LIEF abstract object type
OBJECT_TYPES abstract_object_type(void) const;

@@ -65,9 +67,12 @@ class DLL_PUBLIC Header : public Visitable {
void nb_cmds(uint32_t ncmds);
void sizeof_cmds(uint32_t sizeofcmds);
void flags(uint32_t flags);
void remove_flag(HEADER_FLAGS flag);
void remove(HEADER_FLAGS flag);
void reserved(uint32_t reserved);

Header& operator+=(HEADER_FLAGS c);
Header& operator-=(HEADER_FLAGS c);

bool operator==(const Header& rhs) const;
bool operator!=(const Header& rhs) const;

@@ -153,27 +153,27 @@ ENDIANNESS Header::abstract_endianness(void) const {
std::set<HEADER_FLAGS> Header::flags_list(void) const {
std::set<HEADER_FLAGS> flags;

auto has_flag = [this] (HEADER_FLAGS flag) {
return (static_cast<uint32_t>(flag) & this->flags_) > 0;
};

std::copy_if(
std::begin(header_flags_array),
std::end(header_flags_array),
std::inserter(flags, std::begin(flags)),
has_flag);
std::bind(&Header::has, this, std::placeholders::_1));

return flags;
}


bool Header::has_flag(HEADER_FLAGS flag) const {
return (this->flags_ & static_cast<uint32_t>(flag)) > 0;
bool Header::has(HEADER_FLAGS flag) const {
return (this->flags() & static_cast<uint32_t>(flag)) > 0;
}


void Header::remove_flag(HEADER_FLAGS flag) {
this->flags_ &= ~static_cast<uint32_t>(flag);
void Header::add(HEADER_FLAGS flag) {
this->flags(this->flags() | static_cast<uint32_t>(flag));
}

void Header::remove(HEADER_FLAGS flag) {
this->flags(this->flags() & ~static_cast<uint32_t>(flag));
}


@@ -231,6 +231,16 @@ bool Header::operator!=(const Header& rhs) const {
}


Header& Header::operator+=(HEADER_FLAGS c) {
this->add(c);
return *this;
}

Header& Header::operator-=(HEADER_FLAGS c) {
this->remove(c);
return *this;
}


std::ostream& operator<<(std::ostream& os, const Header& hdr) {

0 comments on commit cbe8354

Please sign in to comment.