Skip to content

Commit

Permalink
Cleanup std::vector with span when it's suitable
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Nov 10, 2023
1 parent e25dcc3 commit 262ad97
Show file tree
Hide file tree
Showing 21 changed files with 105 additions and 95 deletions.
10 changes: 4 additions & 6 deletions api/python/src/Abstract/pySection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "pySafeString.hpp"
#include "typing.hpp"
#include "nanobind/extra/memoryview.hpp"
#include "nanobind/utils.hpp"

#include "LIEF/Abstract/Section.hpp"

Expand Down Expand Up @@ -55,8 +56,7 @@ void create<Section>(nb::module_& m) {

.def_prop_ro("fullname",
[] (const Section& obj) {
const std::string& fullname = obj.fullname();
return nb::bytes(fullname.data(), fullname.size());
return nb::to_bytes(obj.fullname());
},
"Return the **fullname** of the section including the trailing bytes"_doc)

Expand All @@ -77,8 +77,7 @@ void create<Section>(nb::module_& m) {

.def_prop_rw("content",
[] (const Section& self) {
const span<const uint8_t> content = self.content();
return nb::memoryview::from_memory(content.data(), content.size());
return nanobind::to_memoryview(self.content());
},
nb::overload_cast<const std::vector<uint8_t>&>(&Section::content),
"Section's content"_doc)
Expand Down Expand Up @@ -114,8 +113,7 @@ void create<Section>(nb::module_& m) {
"str"_a, "pos"_a = 0)

.def("search",
[] (const Section& self,
nb::bytes bytes, size_t pos) -> search_result
[] (const Section& self, nb::bytes bytes, size_t pos) -> search_result
{
std::string raw_str(bytes.c_str(), bytes.size());
const std::vector<uint8_t> raw = {
Expand Down
5 changes: 4 additions & 1 deletion api/python/src/MachO/objects/pyThreadCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <sstream>
#include <nanobind/stl/string.h>
#include <nanobind/stl/vector.h>
#include "nanobind/utils.hpp"

#include "LIEF/MachO/ThreadCommand.hpp"

Expand Down Expand Up @@ -52,7 +53,9 @@ void create<ThreadCommand>(nb::module_& m) {


.def_prop_rw("state",
nb::overload_cast<>(&ThreadCommand::state, nb::const_),
[] (const ThreadCommand& self) {
return nb::to_memoryview(self.state());
},
nb::overload_cast<const std::vector<uint8_t>&>(&ThreadCommand::state),
R"delim(
The actual thread state as a vector of bytes. Depending on the architecture(),
Expand Down
9 changes: 3 additions & 6 deletions api/python/src/PE/objects/debug/pyRepro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <sstream>
#include <nanobind/stl/string.h>
#include <nanobind/extra/memoryview.hpp>
#include "nanobind/utils.hpp"

namespace LIEF::PE::py {

Expand All @@ -35,14 +36,10 @@ void create<Repro>(nb::module_& m) {
)delim"_doc)
.def_prop_rw("hash",
[] (const Repro& repro) {
const span<const uint8_t> hash = repro.hash();
return nb::memoryview::from_memory(hash.data(), hash.size());
return nb::to_memoryview(repro.hash());
},
[] (Repro& repro, nb::bytes bytes) {
const auto* start = reinterpret_cast<const uint8_t*>(bytes.c_str());
const auto* end = start + bytes.size();
std::vector<uint8_t> hash(start, end);
repro.hash(std::move(hash));
repro.hash(nb::to_vector(bytes));
}, "The hash associated with the reproducible build"_doc)
LIEF_DEFAULT_STR(Repro);
}
Expand Down
16 changes: 6 additions & 10 deletions api/python/src/PE/objects/pyBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "pyErr.hpp"
#include "pyIterator.hpp"
#include "nanobind/extra/memoryview.hpp"
#include "nanobind/utils.hpp"

#include <nanobind/stl/string.h>
#include <nanobind/stl/vector.h>
Expand Down Expand Up @@ -197,8 +198,7 @@ void create<Binary>(nb::module_& m) {

.def("authentihash",
[] (const Binary& bin, ALGORITHMS algo) {
const std::vector<uint8_t>& data = bin.authentihash(algo);
return nb::bytes(reinterpret_cast<const char*>(data.data()), data.size());
return nb::to_bytes(bin.authentihash(algo));
},
"Compute the authentihash according to the " RST_CLASS_REF(lief.PE.ALGORITHMS) " "
"given in the first parameter"_doc,
Expand Down Expand Up @@ -235,29 +235,25 @@ void create<Binary>(nb::module_& m) {

.def_prop_ro("authentihash_md5",
[] (const Binary& bin) {
const std::vector<uint8_t>& data = bin.authentihash(ALGORITHMS::MD5);
return nb::bytes(reinterpret_cast<const char*>(data.data()), data.size());
return nb::to_bytes(bin.authentihash(ALGORITHMS::MD5));
},
"Authentihash **MD5** value"_doc)

.def_prop_ro("authentihash_sha1",
[] (const Binary& bin) {
const std::vector<uint8_t>& data = bin.authentihash(ALGORITHMS::SHA_1);
return nb::bytes(reinterpret_cast<const char*>(data.data()), data.size());
return nb::to_bytes(bin.authentihash(ALGORITHMS::SHA_1));
},
"Authentihash **SHA1** value"_doc)

.def_prop_ro("authentihash_sha256",
[] (const Binary& bin) {
const std::vector<uint8_t>& data = bin.authentihash(ALGORITHMS::SHA_256);
return nb::bytes(reinterpret_cast<const char*>(data.data()), data.size());
return nb::to_bytes(bin.authentihash(ALGORITHMS::SHA_256));
},
"Authentihash **SHA-256** value"_doc)

.def_prop_ro("authentihash_sha512",
[] (const Binary& bin) {
const std::vector<uint8_t>& data = bin.authentihash(ALGORITHMS::SHA_512);
return nb::bytes(reinterpret_cast<const char*>(data.data()), data.size());
return nb::to_bytes(bin.authentihash(ALGORITHMS::SHA_512));
},
"Authentihash **SHA-512** value"_doc)

Expand Down
5 changes: 3 additions & 2 deletions api/python/src/PE/objects/pySection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <nanobind/stl/string.h>
#include <nanobind/stl/set.h>
#include <nanobind/stl/vector.h>
#include "nanobind/utils.hpp"


#define PY_ENUM(x) to_string(x), x

Expand Down Expand Up @@ -167,8 +169,7 @@ void create<Section>(nb::module_& m) {

.def_prop_ro("padding",
[] (const Section& sec) {
const std::vector<uint8_t>& data = sec.padding();
return nb::bytes(reinterpret_cast<const char*>(data.data()), data.size());
return nb::to_bytes(sec.padding());
},
"Section padding content as bytes"_doc)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <string>
#include <sstream>
#include <nanobind/stl/string.h>
#include "nanobind/utils.hpp"

namespace LIEF::PE::py {

Expand All @@ -35,9 +36,8 @@ void create<GenericType>(nb::module_& m) {
"OID of the original attribute"_doc)

.def_prop_ro("raw_content",
[] (const GenericType& type) -> nb::bytes {
const std::vector<uint8_t>& raw = type.raw_content();
return nb::bytes(reinterpret_cast<const char*>(raw.data()), raw.size());
[] (const GenericType& type) {
return nb::to_memoryview(type.raw_content());
}, "Original DER blob of the attribute"_doc);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string>
#include <sstream>
#include <nanobind/stl/string.h>
#include "nanobind/utils.hpp"

namespace LIEF::PE::py {

Expand Down Expand Up @@ -47,8 +48,7 @@ void create<PKCS9MessageDigest>(nb::module_& m) {

.def_prop_ro("digest",
[] (const PKCS9MessageDigest& digest) {
const std::vector<uint8_t>& data = digest.digest();
return nb::bytes(reinterpret_cast<const char*>(data.data()), data.size());
return nb::to_bytes(digest.digest());
}, "Message digeset as a blob of bytes as described in the RFC"_doc);
}

Expand Down
19 changes: 7 additions & 12 deletions api/python/src/PE/objects/signature/pyRsaInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string>
#include <sstream>
#include <nanobind/stl/string.h>
#include "nanobind/utils.hpp"

namespace LIEF::PE::py {

Expand All @@ -37,40 +38,34 @@ void create<RsaInfo>(nb::module_& m) {

.def_prop_ro("N",
[] (const RsaInfo& info) {
const std::vector<uint8_t>& data = info.N();
return nb::bytes(reinterpret_cast<const char*>(data.data()), data.size());
return nb::to_bytes(info.N());
},
"RSA public modulus (in bytes)"_doc)

.def_prop_ro("E",
[] (const RsaInfo& info) {
const std::vector<uint8_t>& data = info.E();
return nb::bytes(reinterpret_cast<const char*>(data.data()), data.size());
return nb::to_bytes(info.E());
}, "RSA public exponent (in bytes)"_doc)

.def_prop_ro("D",
[] (const RsaInfo& info) {
const std::vector<uint8_t>& data = info.D();
return nb::bytes(reinterpret_cast<const char*>(data.data()), data.size());
return nb::to_bytes(info.D());
}, "RSA private exponent (in bytes)"_doc)

.def_prop_ro("P",
[] (const RsaInfo& info) {
const std::vector<uint8_t>& data = info.P();
return nb::bytes(reinterpret_cast<const char*>(data.data()), data.size());
return nb::to_bytes(info.P());
}, "First prime factor (in bytes)"_doc)

.def_prop_ro("Q",
[] (const RsaInfo& info) {
const std::vector<uint8_t>& data = info.Q();
return nb::bytes(reinterpret_cast<const char*>(data.data()), data.size());
return nb::to_bytes(info.Q());
}, "Second prime factor (in bytes)")

.def_prop_ro("key_size",
&RsaInfo::key_size, "Size of the public modulus in bits"_doc)

.def_prop_ro("__len__",
&RsaInfo::key_size)
.def_prop_ro("__len__", &RsaInfo::key_size)

LIEF_DEFAULT_STR(RsaInfo);
}
Expand Down
4 changes: 2 additions & 2 deletions api/python/src/PE/objects/signature/pySignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <nanobind/stl/string.h>
#include <nanobind/stl/vector.h>
#include <nanobind/stl/unique_ptr.h>
#include "nanobind/utils.hpp"

#include "enums_wrapper.hpp"

Expand Down Expand Up @@ -219,8 +220,7 @@ void create<Signature>(nb::module_& m) {

.def_prop_ro("raw_der",
[] (const Signature& sig) {
const std::vector<uint8_t>& raw = sig.raw_der();
return nb::bytes(reinterpret_cast<const char*>(raw.data()), raw.size());
return nb::to_memoryview(sig.raw_der());
},
"Return the raw original signature as a byte object"_doc,
nb::rv_policy::reference_internal)
Expand Down
9 changes: 4 additions & 5 deletions api/python/src/PE/objects/signature/pySignerInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <string>
#include <sstream>
#include <nanobind/stl/string.h>
#include "nanobind/utils.hpp"

namespace LIEF::PE::py {

Expand Down Expand Up @@ -58,9 +59,8 @@ void create<SignerInfo>(nb::module_& m) {
"Should be 1"_doc)

.def_prop_ro("serial_number",
[] (const SignerInfo& info) -> nb::bytes {
const std::vector<uint8_t>& data = info.serial_number();
return nb::bytes(reinterpret_cast<const char*>(data.data()), data.size());
[] (const SignerInfo& info) {
return nb::to_bytes(info.serial_number());
},
"The X509 serial number used to sign the signed-data (see: :attr:`lief.PE.x509.serial_number`)"_doc)

Expand All @@ -83,8 +83,7 @@ void create<SignerInfo>(nb::module_& m) {

.def_prop_ro("encrypted_digest",
[] (const SignerInfo& info) {
const std::vector<uint8_t>& data = info.encrypted_digest();
return nb::bytes(reinterpret_cast<const char*>(data.data()), data.size());
return nb::to_bytes(info.encrypted_digest());
},
"Return the signature created by the signing certificate's private key"_doc)

Expand Down
14 changes: 6 additions & 8 deletions api/python/src/PE/objects/signature/pyx509.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <nanobind/stl/unique_ptr.h>
#include <nanobind/stl/vector.h>
#include <nanobind/stl/array.h>
#include "nanobind/utils.hpp"

namespace LIEF::PE::py {

Expand Down Expand Up @@ -96,9 +97,8 @@ void create<x509>(nb::module_& m) {
"X.509 version. (1=v1, 2=v2, 3=v3)"_doc)

.def_prop_ro("serial_number",
[] (const x509& crt) -> nb::bytes {
const std::vector<uint8_t>& sn = crt.serial_number();
return nb::bytes(reinterpret_cast<const char*>(sn.data()), sn.size());
[] (const x509& crt) {
return nb::to_bytes(crt.serial_number());
},
"Unique id for certificate issued by a specific CA."_doc)

Expand Down Expand Up @@ -127,9 +127,8 @@ void create<x509>(nb::module_& m) {
"Subject of the certificate"_doc)

.def_prop_ro("raw",
[] (const x509& crt) -> nb::bytes {
const std::vector<uint8_t>& raw = crt.raw();
return nb::bytes(reinterpret_cast<const char*>(raw.data()), raw.size());
[] (const x509& crt) {
return nb::to_bytes(crt.raw());
},
"The raw bytes associated with this x509 cert (DER encoded)"_doc)

Expand Down Expand Up @@ -160,8 +159,7 @@ void create<x509>(nb::module_& m) {

.def_prop_ro("signature",
[] (const x509& cert) {
const std::vector<uint8_t>& sig = cert.signature();
return nb::bytes(reinterpret_cast<const char*>(sig.data()), sig.size());
return nb::to_bytes(cert.signature());
}, "The signature of the certificate")

.def("verify",
Expand Down
24 changes: 23 additions & 1 deletion api/python/src/nanobind/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,32 @@
#include <nanobind/nanobind.h>
#include <LIEF/BinaryStream/SpanStream.hpp>
#include <vector>
#include <memory.h>
#include <memory>

#include "nanobind/extra/memoryview.hpp"

NAMESPACE_BEGIN(NB_NAMESPACE)

inline nanobind::bytes to_bytes(const std::vector<uint8_t>& vec) {
return nanobind::bytes(reinterpret_cast<const char*>(vec.data()), vec.size());
}

inline nanobind::bytes to_bytes(LIEF::span<const uint8_t> sp) {
return nanobind::bytes(reinterpret_cast<const char*>(sp.data()), sp.size());
}

inline nanobind::bytes to_bytes(const std::string& str) {
return nanobind::bytes(str.data(), str.size());
}

inline nanobind::memoryview to_memoryview(LIEF::span<const uint8_t> sp) {
return nanobind::memoryview::from_memory(sp.data(), sp.size());
}

inline nanobind::memoryview to_memoryview(const std::vector<uint8_t>& vec) {
return nanobind::memoryview::from_memory(vec.data(), vec.size());
}

inline std::vector<uint8_t> to_vector(nanobind::bytes bytes) {
const auto* ptr = reinterpret_cast<const uint8_t*>(bytes.c_str());
return {ptr, ptr + bytes.size()};
Expand Down
10 changes: 8 additions & 2 deletions include/LIEF/MachO/ThreadCommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "LIEF/visibility.h"
#include "LIEF/types.hpp"
#include "LIEF/span.hpp"

#include "LIEF/MachO/LoadCommand.hpp"

Expand Down Expand Up @@ -70,8 +71,13 @@ class LIEF_API ThreadCommand : public LoadCommand {

//! The actual thread state as a vector of bytes. Depending on the architecture(),
//! these data can be casted into x86_thread_state_t, x86_thread_state64_t, ...
const std::vector<uint8_t>& state() const;
std::vector<uint8_t>& state();
span<const uint8_t> state() const {
return state_;
}

span<uint8_t> state() {
return state_;
}

//! Return the initial Program Counter regardless of the underlying architecture.
//! This value, when non null, can be used to determine the binary's entrypoint.
Expand Down
Loading

0 comments on commit 262ad97

Please sign in to comment.